![]() |
The abstract widget system | ![]() |
Everything that TeXmacs shows on the screen besides the typeset document itself (menus, icon bars, side tools, the footer, dialogs, auxiliary windows) is built from widgets. TeXmacs does not talk directly to a particular toolkit: the kernel only knows an abstract widget class, a fixed set of widget constructors and a message protocol. Each graphical port implements these constructors and messages in terms of its own toolkit: the native Qt widgets (Qt port, the default), the native AppKit controls of macOS (Cocoa port), elements drawn every frame by the immediate mode layout library Clay (Vue port), or the TeXmacs own toolkit Widkit on top of X11, SDL or Qt (the X11, SDL and Qtwk ports). On top of this, almost the entire user interface is described in Scheme by a small declarative language, which is interpreted at run time into calls to the C++ constructors.
This part of the developer documentation describes the internals of this machinery: the C++ interface, the way the main window is organized, the Scheme language and its interpreter, the Qt implementation (the reference port), how the other ports map the same interface, and what has to be done to add a new kind of widget or to port TeXmacs to another toolkit. The use of the Scheme widget language (how to write a menu or a dialog) is explained in the user-level tutorial “Extending the graphical user interface”, which the present documents complement rather than repeat.
The system can be seen as a stack of five layers. Data flows from top to bottom when a widget is built, and events and commands flow from bottom to top when the user interacts with it.
The Scheme widget language. Menus and widgets are defined with menu-bind, tm-menu and tm-widget, using keywords such as =>, ->, hlist, toggle or refreshable. These macros are defined in
kernel/gui/menu-define.scm
and
kernel/gui/gui-markup.scm
.
Menu items. Calling a function defined in this way does not create any widget: it returns a plain Scheme list (a menu item), in which the dynamic parts are represented by closures. The grammar of menu items is given at the start of
kernel/gui/menu-widget.scm
.
The interpreter. The function make-menu-widget of
kernel/gui/menu-widget.scm
walks through a menu item and calls the glued C++ constructors (widget-hmenu, widget-text, widget-toggle, …), wrapping Scheme closures into C++ command and promise<widget> objects.
The abstract C++ interface. The class widget and the constructors such as horizontal_menu, text_widget or texmacs_widget are declared in
Graphics/Gui/widget.hpp
; the message protocol (slots) in
Graphics/Gui/message.hpp
; the system-wide GUI routines in
Graphics/Gui/gui.hpp
. The kernel (for instance
Texmacs/Window/tm_window.cpp
) only uses this interface.
The port. The constructors and the message handlers are implemented by a plug-in in
src/src/Plugins
, selected at configuration time. The ports differ in when the toolkit objects are made:
in the Qt port most widgets are first stored as passive descriptions (qt_ui_element_rep) and only materialized into QWidgets, QActions or QLayoutItems when the toolkit needs them;
the Cocoa port follows the same design (ns_ui_element_rep, materialized into NSViews or NSMenuItems);
the Vue port also stores descriptions (vue_ui_rep), but never builds persistent toolkit objects: at every frame the widget tree of a window is walked again and emits Clay elements, which are laid out and drawn;
the Widkit based ports build Widkit widgets at once (
widkit_wrapper.cpp
), which draw themselves with the TeXmacs renderer in windows provided by X11, SDL or Qt.
Schematically, for the menu entry “New” of the “File” menu:
(=> "File" … ("New" (new-document)) …) Scheme DSL
| gui-make, $-macros
v
(=> "File" … ("New" #<procedure>) …) menu item
| make-menu-widget
v
pulldown_button (text_widget ("File"), promise) abstract C++ widget
| Plugins/Qt, Plugins/NS, Plugins/Vue
v
QAction + QTMLazyMenu, filled on aboutToShow () Qt objects
NSMenuItem + TMLazyMenu, filled on menuNeedsUpdate: Cocoa objects
Clay elements, promise evaluated when opened Vue (each frame) |
src/src/Graphics/Gui/
The abstract interface:
widget.hpp
,
message.hpp
,
gui.hpp
,
window.hpp
and
widget.cpp
.
src/src/Kernel/Abstractions/command.hpp,
src/src/Kernel/Containers/promise.hpp
Commands and promises, the two kinds of closures passed to widgets.
src/src/Texmacs/Window/
The kernel side of windows:
tm_window.cpp
(the class tm_window_rep, menus and icon bars of a window, auxiliary windows),
tm_frame.cpp
(the tm_frame_rep part of the server),
tm_dialogue.cpp
(dialogs and interactive commands) and
tm_button.cpp
(widgets displaying typeset boxes).
src/src/Scheme/Glue/build-glue-basic.scm
The Scheme names of the widget constructors (widget-hmenu and friends).
src/TeXmacs/progs/kernel/gui/
The Scheme side:
gui-markup.scm
,
menu-define.scm
,
menu-widget.scm
,
menu-convert.scm
and the examples in
menu-test.scm
.
src/src/Plugins/Qt/
The Qt port (with a variant in
Plugins/Qt6
).
src/src/Plugins/NS/
The native Cocoa port of macOS (--with-gui=cocoa, macro AQUATEXMACS), modelled on the Qt port; it uses the Objective-C helpers of
Plugins/MacOS
, which the Qt port also uses on macOS.
src/src/Plugins/Vue/
The Vue port (--with-gui=vue, macro VUETEXMACS): SDL 3 windows and events, widgets laid out in immediate mode by Clay (
clay.h
), drawing with MuPDF or on the GPU.
src/src/Plugins/Widkit/
The TeXmacs own widget kit, used by three ports which only provide windows, events and drawing:
Plugins/X11
(--with-gui=x11),
Plugins/SDL
(--with-gui=sdl) and
Plugins/Qtwk
(--with-gui=qtwk, Qt as a platform layer only).
How a port is selected and built, and the details of each port, are described in the chapter on “graphical ports”, in particular “The Vue port” and “The Cocoa port”.
The user-level description of the Scheme widget language, with many examples, is found in “Extending the graphical user interface” and its complete keyword list in the “Widgets reference guide”. Window manipulation from Scheme is documented in “Manipulating TeXmacs windows”.
The older document “The graphical user interface” describes the original X11 toolkit of TeXmacs (the widget, event and attribute classes that now live in
Plugins/Widkit
). The abstract interface described here replaced direct use of Widkit in the kernel: the Qt, Cocoa and Vue ports do not use that event model, which survives only inside the X11, SDL and Qtwk ports.