![]() |
Adding new widgets and porting to other toolkits | ![]() |
A new kind of widget has to be added at every layer of the stack. The simplest way to proceed is to follow an existing widget of similar nature through all the layers; for an input widget, toggle_widget is a good model:
| Layer | File | For the toggle |
| C++ declaration | Graphics/Gui/widget.hpp |
toggle_widget (command cmd, bool on, int style) |
| Qt constructor | Plugins/Qt/qt_widget.cpp |
qt_ui_element_rep::create (qt_widget_rep::toggle_widget, …) |
| Qt rendering | Plugins/Qt/qt_ui_element.cpp |
case toggle_widget in as_qwidget, qt_toggle_command_rep |
| Cocoa constructor | Plugins/NS/ns_widget.mm |
ns_ui_element_rep::create (ns_widget_rep::toggle_widget, …) |
| Cocoa rendering | Plugins/NS/ns_ui_element.mm |
case toggle_widget in as_nsview |
| Vue constructor | Plugins/Vue/vue_widget.cpp |
VUE_WIDGET(toggle_widget, command, cmd, bool, on, int, style) |
| Vue layout | Plugins/Vue/vue_widget.cpp |
type == "toggle_widget" in vue_ui_rep::do_layout and vue_ui_rep::render |
| Widkit | Plugins/Widkit/Basic/widkit_wrapper.cpp |
toggle_widget wrapper |
| Glue | Scheme/Glue/build-glue-basic.scm |
(widget-toggle toggle_widget (widget command bool int)) |
| Markup macro | kernel/gui/gui-markup.scm |
$toggle |
| Keyword | kernel/gui/menu-define.scm |
gui-make-toggle, entry toggle of gui-make-table |
| Interpreter | kernel/gui/menu-widget.scm |
grammar (toggle :%2), make-toggle, menu-expand-toggle |
In more detail, suppose that we want to add a widget constructor foo_widget (command cmd, string val, int style) with a keyword (foo cmd val) (these names are only an illustration).
Declare foo_widget in
Graphics/Gui/widget.hpp
, with a comment describing its semantics. Every port has to implement it, otherwise TeXmacs will not link.
In the Qt port, add a value foo_widget to the enumeration qt_widget_rep::types in
Plugins/Qt/qt_widget.hpp
and the corresponding string, at the same position, in qt_widget_type_strings (in type_as_string).
Implement the constructor in
Plugins/Qt/qt_widget.cpp
:
widget foo_widget (command cmd, string val, int style) {
if (headless_mode) return headless_widget ();
qt_widget wid = qt_ui_element_rep::create (qt_widget_rep::foo_widget,
cmd, val, style);
return abstract (wid);
} |
and add foo_widget to the list of types handled by qt_ui_element_rep::get_payload.
In
Plugins/Qt/qt_ui_element.cpp
, add a case to as_qwidget which unpacks the payload with exactly the same tuple type (triple<command, string, int>), creates the Qt widget with parent_widget as parent, stores it in qwid and connects its signals to a QTMCommand. If the command needs arguments, wrap it in a small command_rep which reads the state of the Qt widget, as qt_toggle_command_rep does, and call cmd (list_object (…)). Add foo_widget to the list of types for which as_qlayoutitem wraps as_qwidget in a QWidgetItem (otherwise it returns NULL and the widget does not appear in lists). If the widget may occur in menus or tool bars, also add a case to as_qaction, which otherwise fails.
Make the same changes in
Plugins/Qt6
.
In the Cocoa port, proceed as for Qt: a value in the enumeration ns_widget_rep::types and its string in ns_widget_type_strings (
Plugins/NS/ns_widget.h
), the constructor in
Plugins/NS/ns_widget.mm
with ns_ui_element_rep::create, the type in the list of ns_ui_element_rep::get_payload, and cases in as_nsview (an NSView for dialogs and bars) and, for menus, in as_menuitem (
Plugins/NS/ns_ui_element.mm
).
In the Vue port, one line
VUE_WIDGET(foo_widget, command, cmd, string, val, int, style); |
in
Plugins/Vue/vue_widget.cpp
generates the constructor, a payload structure vue_foo_widget and the type name "foo_widget" of the vue_ui_rep it returns. Then add a case if (type == "foo_widget") to vue_ui_rep::do_layout, which opens the payload, declares the Clay elements of the widget for the current frame (with ids derived from the serial number id of the widget, so that they are stable from frame to frame), reads the clicks of the previous events with button_logic, and pushes the command onto cmd_list instead of calling it. State which the user changes (a toggle which is clicked) is stored back into the payload. Custom drawing (with the TeXmacs renderer) goes into a case of vue_ui_rep::render, reached through a Clay custom element whose userData is render_ref (). Widgets with a persistent state of their own (text inputs, trees, the ink widget) are rather subclasses of vue_widget_rep.
In
Plugins/Widkit/Basic/widkit_wrapper.cpp
(for the X11, SDL and Qtwk ports), write a wrapper building a Widkit widget; a composition of existing widgets is enough to start with (setting_toggle_widget is a toggle and a text in a horizontal_list).
If the widget needs new messages (for instance to change its value from the kernel), add a slot as described in the next subsection.
Add a line to
Scheme/Glue/build-glue-basic.scm
, next to the other widgets:
(widget-foo foo_widget (widget command string int)) |
The generated file
Scheme/Glue/glue_basic.cpp
is kept in the repository and must be regenerated, with a working Guile, by running ./build-glue build-glue-basic.scm glue_basic.cpp in
src/src/Scheme/Glue
(this is what the GLUE target of src/src/makefile does). The script build-auto-doc, called by build-glue, also updates
progs/prog/glue-symbols.scm
. The argument types must be known to the glue generator; types such as command, promise_widget, array_widget or array_string are already supported (see
Scheme/Scheme/glue.cpp
).
In
kernel/gui/gui-markup.scm
, define the macro which builds the menu item, turning the parts that must be recomputed into closures:
(tm-define-macro ($foo cmd val)
(:synopsis "Make a foo widget")
‘(list 'foo (lambda (answer) ,cmd) (lambda () ,val))) |
In
kernel/gui/menu-define.scm
, add the translation function and register it in gui-make-table:
(define (gui-make-foo x)
(require-format x '(foo :%2))
‘($foo ,@(cdr x)))
(define-table gui-make-table
…
(foo ,gui-make-foo)
…) |
(from another module, extend-table can be used instead).
In
kernel/gui/menu-widget.scm
, add (foo :%2) to the :menu-item grammar, write the builder and register it in make-menu-items-table:
(define (make-foo p style)
(with (tag cmd val) p
(widget-foo (object->command (menu-protect cmd)) (val) style)))
(foo (:%2)
,(lambda (p style bar?) (list (make-foo p style)))) |
Add an entry to menu-expand-table which evaluates the closures carrying state (here val) and replaces the others by their source, as menu-expand-toggle does. Otherwise a change of val is invisible in the expansion, and the cached widget will be reused by get_menu_widget and refresh widgets.
Optionally, add the corresponding build-* and markup-* functions to
kernel/gui/menu-convert.scm
, so that the widget also works with the markup interface.
Document the keyword in the “Widgets reference guide”.
Add SLOT_FOO to the enumeration slot_id in
Graphics/Gui/message.hpp
, before slot_id__LAST.
Add "SLOT_FOO" at the same position in the array of slot_name in
Graphics/Gui/widget.cpp
.
Add typed helper functions to
message.hpp
, for example
inline void
set_foo (widget w, string s) {
send<string> (w, SLOT_FOO, s);
} |
Handle the slot in the send, query, read or write methods of the widgets concerned, in each port, opening the blackbox with the same type (check_type<string> (val, s) in Qt and Cocoa, check_open<string> (val, s) in Vue). For slots of the main widget, remember that there are two implementations in Qt (qt_tm_widget_rep and qt_tm_embedded_widget_rep) and in Cocoa (ns_tm_widget_rep and ns_tm_embedded_widget_rep), and that qt_widget_rep::query and qt_headless_widget_rep::query provide defaults for common queries. In Widkit, a slot is usually translated into an event in wk_widget_rep::send or query (
widkit_wrapper.cpp
).
A graphical port is a directory in
src/src/Plugins
which implements:
the system-wide routines of
Graphics/Gui/gui.hpp
;
all widget constructors of
Graphics/Gui/widget.hpp
, with the messages they are expected to understand;
a class simple_widget_rep providing canvases;
a renderer for the screen, pictures and fonts, as explained in “The renderer interface”;
a main loop which queues events and calls the interpose handler.
The Qt port (“The Qt implementation”) is the reference. The current ports fall into three families, described in the last section of this page:
| Port | Directory | Widgets | Windows, events | Drawing |
| Qt | Plugins/Qt( Qt6) |
native Qt, built lazily | Qt | QPainter |
| Cocoa | Plugins/NS |
native AppKit, built lazily | AppKit | Core Graphics |
| Vue | Plugins/Vue |
Clay, immediate mode | SDL 3 | MuPDF, or the GPU |
| X11 | Plugins/X11 |
Widkit | Xlib | Xlib |
| SDL | Plugins/SDL |
Widkit | SDL 3 | MuPDF |
| Qtwk | Plugins/Qtwk |
Widkit | Qt | QPainter |
The Widkit ports implement window_rep of
window.hpp
and leave the widgets to
Plugins/Widkit
, a complete toolkit of its own whose widgets are drawn with the TeXmacs renderer and communicate with events (see the historical document “The graphical user interface”);
widkit_wrapper.cpp
maps the abstract constructors and slots to Widkit. A new port for a platform without a usable native toolkit can start in the same way, providing only windows, events, a renderer and the routines of
gui.hpp
, as the SDL port does in about 2500 lines.
The port is selected at configuration time (see “Selecting and building a port”): configure --with-gui=qt|qtwk|x11|cocoa|sdl|vue (
misc/m4/tm_gui.m4
) sets CONFIG_GUI, defines one of the macros QTTEXMACS, AQUATEXMACS, X11TEXMACS, SDLTEXMACS or VUETEXMACS (Qtwk defines both QTWKTEXMACS and QTTEXMACS), and
src/src/makefile.in
compiles the corresponding plug-in directories (X11 Widkit, SDL Widkit, Qtwk Widkit with a few files of Qt, NS with MacOS, Vue).
CMakeLists.txt
has the cache variable TEXMACS_GUI, which accepts Qt (Qt6, Qt5, Qt4), Vue, SDL and X11, but not Cocoa nor Qtwk.
About two hundred places outside the plug-ins test these macros (search for QTTEXMACS, then for the others), for instance
Edit/editor.hpp
and
Texmacs/Window/tm_button.cpp
, which choose the header defining simple_widget_rep. The fallback in most of these places is the X11/Widkit behaviour, so a new port has to add its own branches: the Cocoa port mostly joins the Qt branches (#if defined (QTTEXMACS) || defined (AQUATEXMACS), for instance for exec_delayed in
Scheme/Scheme/object.cpp
), while Vue adds branches of its own (VUETEXMACS). Remember that QTTEXMACS alone also holds for Qtwk, whose widgets are those of Widkit (the run-time predicates below exclude it).
At run time, gui_version () and the functions gui_is_qt (), gui_is_vue () and gui_is_x () of
Kernel/Abstractions/basic.cpp
are glued as gui-version, qt-gui?, vue-gui? and x-gui?; ns-gui?, qt5-gui? and friends are defined from gui-version in
kernel/boot/abbrevs.scm
:
| Port | gui-version | qt-gui? | x-gui? | vue-gui? |
| Qt | "qt5", "qt6" | yes | no | no |
| Qtwk | "qt5", "qt6" | no | yes | no |
| Cocoa | "ns" | yes | no | no |
| Vue | "vue" | no | no | yes |
| SDL | "sdl" | no | yes | no |
| X11 | "x11" | no | yes | no |
qt-gui? means “implements the widgets and dialogs of the Qt port”: the Scheme code uses it to choose native dialogs, shortcuts and menus, which is why it holds for Cocoa and not for Qtwk, whose gui-version is nevertheless that of Qt. x-gui? means “has the historical X11 look and feel” (the Widkit ports): Scheme then asks itself before overwriting a file, which the native save panels of the other ports do. A new port should decide which of these predicates it satisfies, and look for the places in
TeXmacs/progs
which test them: a feature hidden behind (qt-gui?) is silently missing elsewhere (in Vue, the color menus fall back to the Scheme color picker for this reason).
gui.hpp
| Routine | Obligation | In Qt |
| gui_open, gui_close | create and destroy the application object | qt_gui.cpp |
| gui_interpose | remember the handler to call from the main loop | qt_gui.cpp |
| gui_start_loop | run the main loop until the last window is closed | qt_gui_rep::event_loop |
| gui_root_extents, gui_maximal_extents | screen size in SI | qt_gui.cpp |
| gui_refresh | retranslate and redraw after a language change | refresh_language |
| gui_version | the name of the port (see above) | qt_gui.cpp |
| set_default_font, get_default_font | fonts used in widgets | qt_gui.cpp |
| load_system_font | optional system fonts | qt_gui.cpp |
| set_selection, get_selection, clear_selection | clipboards (named "primary", "mouse", …) with format conversions | qt_gui_rep |
| beep | qt_gui.cpp |
|
| needs_update | schedule a pass of the main loop | qt_gui_rep::need_update |
| check_event | tell the typesetter whether user events are pending, so that it can interrupt long repaints | qt_gui_rep::check_event |
| show_help_balloon | tooltip at a position, hidden on the next event | qt_gui_rep |
| show_wait_indicator | message during long operations, removed when the message is empty | qt_gui_rep |
| external_event | events from other devices | qt_gui.cpp |
| gui_interrupted | usually in terms of check_event | Graphics/Renderer/basic_renderer.cpp |
The routines get_default_styled_font and get_widget_size and the global variables use_native_menubar, tm_style_sheet, tm_style_density and use_mini_bars are defined in
widget.cpp
and need not be provided (the variables are interpreted by the port). image_gc is declared but not called by the kernel; Qt and Cocoa leave it empty, while Vue and SDL clear the image cache of their MuPDF renderer with it.
The other ports implement these routines in
ns_gui.mm
(Cocoa),
vue_gui.cpp
(Vue),
sdl_gui.cpp
,
x_gui.cpp
and
x_init.cpp
, and
qtwk_gui.cpp
.
All constructors of
widget.hpp
must exist. It is legitimate to start with trivial implementations for rarely used widgets (the Qt and Cocoa ports have none for ink_widget, empty_widget and wait_widget; Vue implements all three, and Widkit falls back to the plain tabs for the responsive ones), but the following are essential:
texmacs_widget (mask, quit), which must understand the canvas slots, SLOT_SCROLLABLE (to receive the editor), the menu, icon and tool slots with their visibility counterparts, the footer and interactive slots, SLOT_KEYBOARD_FOCUS and SLOT_DESTROY; with mask 0 it must return an embeddable variant;
window widgets, which must understand SLOT_IDENTIFIER, SLOT_VISIBILITY, SLOT_NAME, SLOT_MODIFIED, SLOT_SIZE, SLOT_POSITION, SLOT_FULL_SCREEN, SLOT_REFRESH and SLOT_DESTROY, run the quit command when closed by the user, and report moves, resizes and destruction to the kernel with notify_window_move, notify_window_resize and notify_window_destroy (so that geometries are remembered);
the menu widgets (horizontal_menu, vertical_menu, menu_button, pulldown_button, pullright_button, text_widget, xpm_widget, menu_separator, balloon_widget), which must be usable both in the menu bar, in menus and in tool bars;
popup_widget and popup_window_widget, used for the contextual menu;
the lists, glue and input widgets used by dialogs, and refresh_widget and refreshable_widget.
A few general rules, learned from the Qt port and confirmed by the Cocoa and Vue ports, will save much trouble:
Keep descriptions lazy. Store the arguments of a constructor and build toolkit objects only when needed, possibly several times. Submenus must evaluate their promise each time they are shown, and must not cache the result beyond the time the menu is open (Cocoa refills its TMLazyMenu in menuNeedsUpdate:, Vue evaluates the promise when the menu opens and drops it when it closes).
Never run TeXmacs code from toolkit callbacks. Queue key presses, mouse events, resizes and commands, and process them from the main loop, followed by the interpose handler registered with gui_interpose and by the repainting of the canvases. The Scheme side already defers menu actions with exec-delayed; a port which defines neither QTTEXMACS nor AQUATEXMACS gets the implementation of exec_delayed of
Scheme/Scheme/object.cpp
, whose pending commands are run by exec_pending_commands from the interpose handler of the server.
Be careful with ownership. Abstract widgets are reference counted and may be destroyed late; toolkit objects should be owned by the toolkit hierarchy (windows) and referenced from TeXmacs widgets through guarded pointers. Conversely, whatever the toolkit keeps after a widget was replaced must keep the widget alive: Vue draws the render commands of the last layout again until the next one, and these name their widgets by raw pointers, so the layout holds a reference to each of them (render_ref, release_layout_widgets); the commands and the interpose handler may replace menus, tools and dialogs at any time, after which the windows are laid out again before anything is drawn.
Replace, do not mutate. The kernel installs whole new widgets for menus and bars; it never edits an existing menu. Installing a new menu bar while a menu is open must be postponed (Cocoa also has one menu bar per main window, installed when the window becomes main).
Implement SLOT_REFRESH globally or per window, keeping in mind that windows_refresh only sends it to the auxiliary windows (see “Windows, the main TeXmacs widget and the flow of events”), and that a refresh widget which is not shown at the time (a closed menu, a hidden tool) must still see the message when it is shown again.
Messages may arrive at the “wrong” widget. The kernel sends SLOT_FULL_SCREEN to the window widget, but the title (SLOT_NAME) to the main widget; in Qt both are the same object. Ports where the window wraps the main widget must forward such messages in the right direction (Vue and Cocoa forward the full screen mode from the window to the main widget, and Vue the title from the main widget to its window). Use the debugging flag of unhandled messages to find such cases: the Cocoa and Vue ports reuse the flags of Qt (-debug-qt, -debug-qt-widgets).
Follow the Qt conventions for text. Labels and inputs are in the Cork encoding and must be converted once, at the boundary; file names may be in Cork or in UTF-8 (Cocoa has to_nsstring_utf8, which keeps a string that looks like UTF-8, as to_qstring does). Keys must be named as in Qt (space, S-tab, enter, …); the composition of an input method is sent to the editor as a key pre-edit:cursor:text, and a text committed at once is delivered one character per key (see QTMWidget, TMView).
Report the geometry of windows, but not of popups, with notify_window_move and notify_window_resize; window systems where a move is modal (macOS) may require polling the geometry once per frame, as Vue does.
Plan for testing without a human. Other programs often cannot capture or drive the windows of TeXmacs (on macOS in particular), so the Cocoa and Vue ports have test hooks driven by environment variables: snapshots of the windows, synthetic keys and clicks, scripted steps (TEXMACS_NS_SNAPSHOT, TEXMACS_NS_PRESS, …; TEXMACS_VUE_SCRIPT, TEXMACS_VUE_SNAPSHOT with the scripts of
tests/Plugins/Vue
). Vue also runs headless (option -headless, with the SDL video driver dummy): the windows are then virtual, but the widgets are built and laid out as usual, whereas the Qt port replaces every widget by a headless_widget in that mode.
The class simple_widget_rep must provide the virtual methods listed at the end of
widget.hpp
(see “The abstract widget interface in C++”), deliver events to them (key names in the TeXmacs format, as produced for instance by
Plugins/Qt/QTMKeyboard.cpp
and QTMWidget; mouse events with kinds such as "press-left" and modifiers), and repaint invalidated regions by calling handle_repaint with a renderer drawing on the canvas. It must also understand the canvas slots (SLOT_EXTENTS, SLOT_SCROLL_POSITION, SLOT_VISIBLE_PART, SLOT_ZOOM_FACTOR, SLOT_INVALIDATE, SLOT_INVALIDATE_ALL, SLOT_INVALID, SLOT_CURSOR, SLOT_MOUSE_GRAB, SLOT_KEYBOARD_FOCUS, SLOT_WINDOW, …), which the editor uses constantly. The Qt class qt_simple_widget_rep and its QTMWidget are a good starting point; ns_simple_widget_rep (with TMView) and vue_simple_widget_rep follow the same model. All three paint into a backing store of the size of the visible part, shift it when the view scrolls and repaint only the strips uncovered.
The Cocoa port (
Plugins/NS
) is a transposition of the Qt port to AppKit, file by file: ns_widget_rep plays the role of qt_widget_rep, with the same enumeration of types, and ns_ui_element_rep stores the arguments of most constructors in a blackbox (its create templates are those of qt_ui_element_rep). The two ways of materializing a description are as_nsview (an NSView, laid out with NSStackView and NSGridView, for dialogs and icon bars) and as_menuitem (an NSMenuItem, for the menus); submenus are TMLazyMenus. The main widget (
ns_tm_widget.mm
) puts the bars, the side tools and the footer around the canvas in an NSWindow; dialogs and standard panels are in
ns_dialogues.mm
. Messages are handled in send and query as in Qt, and the event loop of
ns_gui.mm
queues events and commands as qt_gui_rep does. Since it implements the same widgets, qt-gui? holds and the generic code treats AQUATEXMACS as Qt. See “The Cocoa port”.
The Vue port (
Plugins/Vue
) keeps the tree of abstract widgets, but builds no toolkit objects from it. Most constructors are generated by the macro VUE_WIDGET, which defines a payload structure for the arguments and returns a vue_ui_rep, a pair of a type name and a blackbox; a few widgets with a state of their own are subclasses of vue_widget_rep (the main widget vue_texmacs_widget_rep, the canvas vue_simple_widget_rep, text inputs, the window widget vue_plain_window_widget_rep, trees, ink). At each frame, for each window, the main loop of
vue_gui.cpp
:
collects the SDL events into the input state of the window (pointer, buttons, key, wheel);
calls do_layout on the content of the window between Clay_BeginLayout and Clay_EndLayout; each widget declares its Clay elements, with ids derived from its serial number, and reacts to the input of the previous layout (button_logic asks Clay whether the pointer is over an element): a click pushes a command onto cmd_list, a pulldown button evaluates its promise, the canvas passes the pending key or mouse event to its handle_* methods. post_layout may ask for another pass (at most five), for instance when a window has to fit its contents;
runs the commands of cmd_list, then the interpose handler, then repaints the invalid regions of the editors into their backing stores;
draws the render commands of the layout: rectangles, borders, texts and images by the renderer of the port (MuPDF, or the GPU path of
vue_gpu.cpp
), and “custom” elements by calling back render of the widget which declared them (the canvas copies its backing store, check boxes and arrows are drawn with the TeXmacs renderer).
The messages are handled in send and query of these classes (vue_ui_rep::send for the descriptions), mostly by storing the new value, which the next layout shows. Menus and popups are separate undecorated SDL windows laid out in the same way. Since nothing survives a frame but the abstract widgets and the state kept in their payloads, there is no synchronization between a toolkit hierarchy and the TeXmacs widgets; the price is that every widget must be cheap to lay out. See “The Vue port”.
These three ports share the widgets of
Plugins/Widkit
: the constructors of
widkit_wrapper.cpp
build Widkit widgets at once, wk_widget_rep::send and query translate the slots into Widkit events, and the main widget is the texmacs_widget_rep of
Widkit/Misc/texmacs_widget.cpp
. Window widgets (
window_widget.cpp
) are put into a window_rep of the port, which owns a renderer, receives the events of the window system and passes them to the widgets. A port therefore only provides window_rep, the routines of
gui.hpp
, the main loop and a renderer: x_window_rep with Xlib, sdl_window_rep with a MuPDF backing store, and qtwk_window_rep with Qt windows and the renderer of the Qt port. The widgets look the same everywhere and are less complete than the native ones (no responsive tabs, for instance).