![]() |
The Qt implementation | ![]() |
The Qt port lives in
src/src/Plugins/Qt
. A second copy with the same structure,
Plugins/Qt6
, is used by the configure build when the option --enable-qt-new is given (it is the default on Android); it adds for instance QTMMainTabWindow. The CMake build always uses Plugins/Qt. Both variants support Qt 5 and 6 through QT_VERSION tests. Changes to the widget layer must usually be made in both directories. The description below refers to Plugins/Qt.
Two other ports depend on this one. The Qtwk port (--with-gui=qtwk) compiles only the platform files of Plugins/Qt (renderer, fonts, pictures, pipes, sockets) and uses the Widkit widgets instead of the classes described here; since it also defines QTTEXMACS, code which must only apply to native Qt widgets tests QTWKTEXMACS as well. The Cocoa port (
Plugins/NS
) shares no code with this one, but copies its structure class by class (ns_widget_rep, ns_ui_element_rep with as_nsview and as_menuitem, ns_tm_widget_rep, ns_simple_widget_rep, ns_gui_rep), so that what follows mostly applies to it too. How the other ports differ is summarized in “Adding new widgets and porting to other toolkits”.
The files relevant for widgets are:
the base class qt_widget_rep and the implementation of all the constructors of
widget.hpp
;
the class qt_ui_element_rep, which implements most constructors;
windows (qt_window_widget_rep) and popups (qt_popup_widget_rep);
the main TeXmacs widget (qt_tm_widget_rep) and its embedded variant (qt_tm_embedded_widget_rep);
the canvas class qt_simple_widget_rep (alias simple_widget_rep), with the Qt widgets QTMWidget and QTMScrollView;
contextual menus (qt_menu_rep);
input fields and dialogs;
Qt helper classes: QTMCommand, QTMLazyMenu, QTMAction, QTMLineEdit, QTMRefreshWidget, QTMRefreshableWidget, …;
the qt_gui_rep singleton the_gui, the event queue and the functions of
gui.hpp
.
All widgets of the port derive from qt_widget_rep, itself derived from widget_rep. It holds
array<widget> children, the subwidgets, kept alive by the reference counting of widget (filled with add_child and add_children);
QPointer<QWidget> qwid, a guarded pointer to the last Qt widget created for it (if any);
types type, a tag taken from an enumeration with one value per kind of widget (horizontal_menu, text_widget, texmacs_widget, …), mirrored by strings in type_as_string for debugging;
a serial number id.
The casts concrete (widget) and abstract (qt_widget) convert between abstract and Qt widgets.
A TeXmacs widget can end up in very different places: in a menu bar, in a menu, in a toolbar, in a layout, in a window. Qt uses different classes for these, so qt_widget_rep has four virtual methods which create the corresponding Qt object on demand:
Returns a new QAction, to be inserted in a QMenu, a menu bar or a toolbar. Buttons with submenus return an action carrying a lazy menu.
Returns a new QWidget. The pointer qwid is set to it, but ownership goes to the caller (usually through the parent or a layout).
Returns a layout item: for lists a QHBoxLayout or QVBoxLayout filled recursively with the layout items of the children, for glue a QSpacerItem, and otherwise the result of as_qwidget wrapped in a QWidgetItem (the default).
For menus and lists, returns the actions of all children (as_qaction of each), which is how a vertical_menu fills a QMenu and a horizontal_menu fills a menu bar or a toolbar.
The general rule is that each call builds new Qt objects and gives them to the caller. The only Qt objects owned by TeXmacs widgets are the windows: a qt_window_widget_rep owns its top-level QWidget, which in turn owns (as Qt parent) all widgets built for its contents.
qt_widget_rep also has the virtual methods plain_window_widget, make_popup_widget, popup_window_widget and tooltip_window_widget, to which the global functions of the same name delegate. The default plain_window_widget creates a QTMPlainWindow, puts the layout item or widget of the contents into it and returns a new qt_window_widget_rep; subclasses such as qt_tm_widget_rep (which already is a window) or qt_inputs_list_widget_rep override it.
qt_widget_rep::send handles a few slots valid for any widget (SLOT_KEYBOARD_FOCUS, SLOT_KEYBOARD_FOCUS_ON, which looks for a child QWidget whose object name is the requested field, SLOT_NAME, SLOT_DESTROY, which is forwarded to the children) and ignores the others. query returns neutral values for common queries (identifier 0, default sizes, invisible bars) and fails otherwise; read, write and notify do nothing. When the debugging flag DEBUG_QT_WIDGETS is on (debug option qt-widgets), unhandled messages are logged, which is the easiest way to find out which slots a widget is expected to support.
When TeXmacs runs without a display (headless_mode), every constructor returns a headless_widget (), an instance of qt_headless_widget_rep which accepts all messages and never creates Qt objects. (The Vue port does it differently: it builds and lays out the real widgets in virtual windows, so that dialogs can be tested without a display.)
Most constructors do not create any Qt object. They create a qt_ui_element_rep, which merely stores its type and its arguments in a blackbox (the payload):
widget text_widget (string s, int style, color col, bool tsp) {
if (headless_mode) return headless_widget ();
qt_widget wid = qt_ui_element_rep::create (qt_widget_rep::text_widget,
s, style, col, tsp);
return abstract (wid);
} |
The static templates qt_ui_element_rep::create pack one to six arguments into pair, triple, …, sextuple and call the constructor qt_ui_element_rep (types, blackbox). Container constructors also register their subwidgets with add_children, so that the whole description tree stays alive.
The actual Qt objects are created later, in the methods as_qaction, as_qwidget, as_qlayoutitem and get_qactionlist of qt_ui_element_rep, which are big switch statements on type. Each case unpacks the payload with the same tuple type that was used for packing, for example:
case text_widget:
{
typedef quartet<string, int, color, bool> T;
T x = open_box<T>(load);
QLabel* w = new QLabel(parent_widget);
w->setText (to_qstring (x.x1));
…
qt_apply_tm_style (w, x.x2, x.x3);
qwid = w;
} |
The static function get_payload gives access to the payload of another UI element; it is used when the rendering of a widget depends on its child. For instance a menu_button whose label is an xpm_widget becomes a QToolButton with an icon, whereas one with a text_widget becomes a QPushButton.
This lazy design has several advantages. The same description can be materialized several times and in different forms (a menu may appear both in the menu bar and in a toolbar); building a menu item that is never shown costs almost nothing; and dynamic menus are cheap, because the description is rebuilt by Scheme but Qt objects are only created for what is displayed. Accordingly, the results must not be cached in general: the comment “DON'T try to always cache the action returned: this breaks dynamic menus!” in as_qaction is to be taken seriously. The exception is get_qactionlist, which caches its list in cachedActionList, since menu bars and toolbars take the actions from it.
A few constructors do not use UI elements because their widgets have state or a complex behaviour: input_text_widget (qt_input_text_widget_rep), inputs_list_widget (qt_inputs_list_widget_rep with qt_field_widget_rep fields), file_chooser_widget (qt_chooser_widget_rep), color_picker_widget (qt_color_picker_widget_rep), printer_widget (qt_printer_widget_rep), wrapped_widget (qt_wrapped_widget_rep), the colored glue_widget (qt_glue_widget_rep) and texmacs_widget. empty_widget, ink_widget and wait_widget are not implemented, and extend_widget returns its first argument.
void
QTMLazyMenu::force () {
BEGIN_SLOT
QList<QAction*>* list = concrete (promise_widget())->get_qactionlist();
transferActions (list);
END_SLOT
} |
Hence the Scheme closure behind the promise is called each time the menu opens. as_qwidget does the same for buttons inside dialogs and toolbars, using a QToolButton or QPushButton with the lazy menu.
QTMCommand is a QObject wrapping a command; its slot apply does not execute the command but queues it:
void
QTMCommand::apply() {
BEGIN_SLOT
if (!is_nil (cmd)) {
the_gui->process_command (cmd);
… |
All inputs of the TeXmacs kernel go through the queue of qt_gui_rep: process_keypress, process_keyboard_focus, process_mouse and process_resize (called by QTMWidget) and process_command append queued_events tagged by qp_type (QP_KEYPRESS, QP_MOUSE, QP_COMMAND, QP_COMMAND_ARGS, …) and call need_update, which starts a zero-delay timer. The timer calls qt_gui_rep::update, which:
queues the pending delayed commands (exec_delayed of Scheme) when their time has come;
processes the queued events with process_queued_events, dispatching them to the handle_* methods of the target qt_simple_widget_rep or applying the commands;
calls the interpose handler of the server, which lets the editors apply their changes and update the menus, and repaints all canvases with qt_simple_widget_rep::repaint_all; when only ordinary key presses were processed in this pass, these two steps are postponed by a few milliseconds, so that fast typing is handled in batches;
restarts the timer, so that the interpose handler also runs periodically when nothing happens.
All Qt slots of the port are enclosed in the macros BEGIN_SLOT and END_SLOT, which catch TeXmacs exceptions so that they do not propagate through Qt.
Input widgets need to pass values to their commands. Instead of subclassing QTMCommand, the port wraps the user command in small command classes which read the state of the Qt widget when applied: qt_toggle_command_rep (checkbox state), qt_enum_command_rep (current text of a combo box), qt_choice_command_rep (selected items of a list, as a Scheme list), … For example:
void apply () { if (qwid) cmd (list_object (object (qwid->isChecked()))); } |
write with SLOT_SCROLLABLE replaces the central canvas by the QWidget of the new editor (main_widget);
SLOT_MAIN_MENU calls install_main_menu (or postpones it, see above);
SLOT_MAIN_ICONS, SLOT_MODE_ICONS, … replace the buttons of a tool bar with the actions of get_qactionlist ();
SLOT_SIDE_TOOLS and the other tool slots put as_qwidget () of the new contents into the corresponding dock widget;
the visibility slots update visibility and call update_visibility;
canvas slots such as SLOT_EXTENTS, SLOT_SCROLL_POSITION, SLOT_ZOOM_FACTOR or SLOT_INVALIDATE are forwarded to main_widget;
SLOT_INTERACTIVE_MODE, SLOT_INTERACTIVE_PROMPT and SLOT_INTERACTIVE_INPUT show the prompt and the input field in the footer, with QTMInteractiveInputHelper committing the answer.
With mask 0, texmacs_widget returns a qt_tm_embedded_widget_rep instead: no window and no bars, just the canvas, for editors embedded in dialogs.
qt_simple_widget_rep is the base class of the editor and of box widgets. Its as_qwidget creates a QTMWidget (derived from QTMScrollView, a QAbstractScrollArea), which translates Qt events into calls of the_gui->process_keypress, process_mouse, … and paints from a backing pixmap. Painting is driven by TeXmacs: SLOT_INVALIDATE records invalid rectangles, and repaint_all (called from qt_gui_rep::update) calls handle_repaint on the invalid regions of all canvases with a renderer drawing into the backing store. Since the QTMWidget may be created after the kernel started to send messages (for instance extents or scroll positions), the last value sent to each slot is remembered in sent_slots and replayed with reapply_sent_slots when the Qt widget is created. The renderer itself is described in “The renderer interface”.
refresh_widget and refreshable_widget are UI elements whose as_qwidget creates a QTMRefreshWidget or a QTMRefreshableWidget. Both connect to the signal tmSlotRefresh (string) of the global QTMGuiHelper, compute their contents a first time with the kind "init", and recompute them in the slot doRefresh:
QTMRefreshWidget::recompute evaluates the Scheme expression (vertical (link name)), computes its menu-expand and, if it differs from the current one, builds the widget with make_menu_widget (reusing an entry of its private cache when menu_caching is set);
QTMRefreshableWidget::recompute calls the Scheme closure and uses the widget it returns, unless it is the same object as before.
In both cases the old QWidget is scheduled for deletion and replaced by as_qwidget of the new contents; if the enclosing window had a fixed size, it is adjusted. The signal is emitted by QTMGuiHelper::emitTmSlotRefresh, which is called when a qt_window_widget_rep receives SLOT_REFRESH. Consequently a refresh sent to any window reaches all refresh widgets; the kind argument is what limits the work. (Cocoa does the same with the notification TMRefresh; Vue obtains the same effect lazily, see “Windows, the main TeXmacs widget and the flow of events”.)
When the interface language changes, gui_refresh calls qt_gui_rep::refresh_language, which makes QTMGuiHelper emit its refresh signal; QTMActions listen to it to retranslate their text.
qt_input_text_widget_rep creates a QTMLineEdit, with a QCompleter offering the proposals (or file names); the helper QTMInputTextWidgetHelper calls the command with the text when the user validates or leaves the field. The type string of the field becomes the Qt object name of the line edit, which is what SLOT_KEYBOARD_FOCUS_ON looks for. It may have the form name#serial:type, which QTMLineEdit::set_type splits; the type selects special behaviour (password, file names, search and replace fields) and fields whose serial starts with form- are committed continuously.
qt_inputs_list_widget_rep implements the multi-field dialogs of tm_frame_rep::interactive; its fields are qt_field_widget_reps, returned by SLOT_FORM_FIELD. For simple questions it may use a native message box. The file chooser, the color picker and the print dialog are implemented with the corresponding Qt dialogs (QTMFileDialog, …).