GNU TeXmacsUser manualReferenceAboutEnglish
Adding new widgets and porting to other toolkits

1.Adding a new kind of widget

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).

1.1.The C++ side

  1. 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.

  2. 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).

  3. 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.

  4. 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.

  5. Make the same changes in

    Plugins/Qt6

    .

  6. 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

    ).

  7. 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.

  8. 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.

1.2.Glue

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

).

1.3.The Scheme side

  1. 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)))
  2. 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).

  3. 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))))
  4. 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.

  5. Optionally, add the corresponding build-* and markup-* functions to

    kernel/gui/menu-convert.scm

    , so that the widget also works with the markup interface.

  6. Document the keyword in the “Widgets reference guide”.

1.4.Adding a slot

  1. Add SLOT_FOO to the enumeration slot_id in

    Graphics/Gui/message.hpp

    , before slot_id__LAST.

  2. Add "SLOT_FOO" at the same position in the array of slot_name in

    Graphics/Gui/widget.cpp

    .

  3. Add typed helper functions to

    message.hpp

    , for example

    inline void
    set_foo (widget w, string s) {
      send<string> (w, SLOT_FOO, s);
    }
  4. 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

    ).

2.Porting TeXmacs to another toolkit

2.1.What a port consists of

A graphical port is a directory in

src/src/Plugins

which implements:

  1. the system-wide routines of

    Graphics/Gui/gui.hpp

    ;

  2. all widget constructors of

    Graphics/Gui/widget.hpp

    , with the messages they are expected to understand;

  3. a class simple_widget_rep providing canvases;

  4. a renderer for the screen, pictures and fonts, as explained in “The renderer interface”;

  5. 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.

2.2.Build integration

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).

2.3.The routines of
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

.

2.4.Widgets and messages

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:

A few general rules, learned from the Qt port and confirmed by the Cocoa and Vue ports, will save much trouble:

2.5.Canvases

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.

3.How the existing ports implement the interface

3.1.Cocoa: native controls, on the model of Qt

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”.

3.2.Vue: immediate mode with Clay

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

:

  1. collects the SDL events into the input state of the window (pointer, buttons, key, wheel);

  2. 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;

  3. runs the commands of cmd_list, then the interpose handler, then repaints the invalid regions of the editors into their backing stores;

  4. 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”.

3.3.X11, SDL and Qtwk: the Widkit toolkit

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).

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".