GNU TeXmacsUser manualReferenceAboutEnglish
The abstract widget interface in C++

1.Overview

The kernel of TeXmacs manipulates widgets exclusively through the interface declared in the directory

Graphics/Gui

:

widget.hpp

The abstract classes widget_rep and widget, the style flags WIDGET_STYLE_* and the list of all widget constructors that a port has to provide.

message.hpp

The enumeration slot_id of message slots, the class slot, and typed helper functions such as set_size, send_keyboard_focus or set_main_menu which encode messages into the generic calls of widget_rep.

gui.hpp

System-wide routines (opening the GUI, running the main loop, clipboard, default fonts, help balloons, interruption checks).

window.hpp

An abstract class window_rep for toolkit windows. It is only used by the ports which build on Widkit, which implement it with x_window_rep (X11), sdl_window_rep (SDL) and qtwk_window_rep (Qtwk). The Qt, Cocoa and Vue ports do not implement it (Vue has an unrelated class vue_window_rep of its own).

widget.cpp

The few port-independent parts: connection management, the default message handlers, slot_name and get_default_styled_font.

A port implements all these declarations; the kernel (in particular

Texmacs/Window

and

Edit/Interface

) only calls them. In the same spirit, C++ code never needs to know which concrete class hides behind a widget.

2.The class widget

widget is a reference-counted pointer to a widget_rep, built with the usual ABSTRACT_NULL macros of

Kernel/Abstractions/basic.hpp

. A widget can be nil, which is used for instance as the “no widget” value of read. The representation class is:

class widget_rep: public abstract_struct {
protected:
  list<widget_connection> in;
  list<widget_connection> out;
public:
  virtual void send (slot s, blackbox val);
  virtual blackbox query (slot s, int type_id);
  virtual widget read (slot s, blackbox index);
  virtual void write (slot s, blackbox index, widget w);
  virtual void notify (slot s, blackbox new_val);
  virtual void connect (slot s, widget w2, slot s2);
  virtual void deconnect (slot s, widget w2, slot s2);
  …
};

The five virtual methods form the whole message protocol:

void send (slot s, blackbox val)
(set a value or trigger an action)

Sends the value val to the slot s. This is used both for setting state (the size, the title, the visibility, …) and for delivering events (a key press, a request to repaint, a refresh).

blackbox query (slot s, int type_id)
(obtain information)

Returns the value of type type_id stored in the slot s. The type identifier is passed so that the implementation can check that the caller expects the right type.

widget read (slot s, blackbox index)
(access a subwidget)

Returns the subwidget of kind s at position index (the index is often empty). Example: get_canvas reads SLOT_CANVAS, get_form_field reads the i-th input of a dialog.

void write (slot s, blackbox index, widget w)
(replace a subwidget)

Installs w as the subwidget of kind s. This is how menus, icon bars, side tools and the editor canvas are installed into the main window.

void notify (slot s, blackbox new_val)
(report a state change)

Informs the widget that the state variable in slot s changed. The default implementation in

widget.cpp

forwards the value to all widgets connected with connect, by calling their send.

The default implementations of send, query, read and write in

widget.cpp

simply fail (FAILED ("no default implementation")), so that every concrete widget class must decide which slots it understands. Ports usually derive from a common base class which provides lenient defaults (the Qt base class qt_widget_rep for instance ignores unknown messages sent to it and returns neutral values for common queries). The base classes of the other ports are ns_widget_rep (Cocoa,

ns_widget.h

), vue_widget_rep (Vue,

vue_widget.hpp

) and, for the ports built on Widkit, wk_widget_rep (

wk_widget.hpp

), whose send and query translate the messages into Widkit events (

widkit_wrapper.cpp

).

The connection mechanism (connect, deconnect and the lists in and out of widget_connections) is a remnant of a more ambitious design; the destructor of widget_rep removes all connections, but the current kernel does not rely on connections between widgets.

Because widgets are allocated with the TeXmacs fast allocator,

widget.hpp

declares a specialization of tm_delete<widget_rep>, which uses the virtual method derived_this to find the start of the most derived object before freeing it. Ports with their own base class do the same (see tm_delete<qt_widget_rep> in

Plugins/Qt/qt_widget.cpp

and tm_delete<vue_widget_rep> in

Plugins/Vue/vue_widget.cpp

).

3.Blackboxes and typed messages

The values carried by messages are blackboxes (

Kernel/Abstractions/blackbox.hpp

): type-erased, reference counted containers. close_box<T> (x) wraps a value, open_box<T> (bb) unwraps it (and asserts that the type is right) and type_box (bb) returns the type identifier, which is type_helper<T>::id for a value of type T. Messages with several arguments use the tuple types pair, triple, quartet and quintuple of

Kernel/Containers/ntuple.hpp

.

Nobody calls send or query with explicit blackboxes. Instead,

message.hpp

provides templates which do the packing:

template<class T1, class T2> void
send (widget w, slot s, T1 val1, T2 val2) {
  typedef pair<T1,T2> T;
  w->send (s, close_box<T> (T (val1, val2)));
}

template<class T1, class T2> void
query (widget w, slot s, T1& val1, T2& val2) {
  typedef pair<T1,T2> T;
  T p= open_box<T> (w->query (s, type_helper<T>::id));
  val1= p.x1; val2= p.x2;
}

and, on top of them, one inline function per message, which is what the kernel really uses:

inline void
set_size (widget w, SI width, SI height) {
  send<SI,SI> (w, SLOT_SIZE, width, height);
}

The receiving side must therefore open the blackbox with exactly the same type. The Qt port provides check_type<T> (val, s), check_type_id<T> (type_id, s) and check_type_void (index, s) in

Plugins/Qt/qt_utilities.hpp

to catch mismatches early (the Cocoa and Vue ports have their own copies, in

ns_utilities.h

and

vue_widget.cpp

), and uses the abbreviations coord2=pair<SI,SI> and coord4=quartet<SI,SI,SI,SI>.

Coordinates and sizes passed in messages are in SI units (see the constant PIXEL in

renderer.hpp

), with the conventions of the TeXmacs renderer.

4.Slots

A slot is a thin wrapper around the enumeration slot_id of

message.hpp

; the function slot_name in

widget.cpp

returns a printable name for debugging. The enumeration ends with slot_id__LAST, which is used by some ports to size tables indexed by slots (for instance sent_slots in qt_simple_widget_rep and ns_simple_widget_rep).

Warning 1. The array of names in slot_name must be kept in the same order as the enumeration. It currently contains "SLOT_SHRINKING_FACTOR" at the position of SLOT_ZOOM_FACTOR, an old name of this slot.

The slots fall into four groups. In the following tables, “send”, “query”, “read”, “write” and “notify” indicate which method of widget_rep is used by the helper functions.

4.1.General slots

These slots may be understood by any widget, but most of them only make sense for window widgets or for canvases.

Slot Helpers Meaning
SLOT_IDENTIFIER get_identifier, set_identifier, is_attached query/send int: the low-level identifier of the window of a widget, 0 if detached
SLOT_WINDOW get_window read: the top-level window widget containing the widget
SLOT_VISIBILITY set_visibility send bool: map or unmap a window
SLOT_FULL_SCREEN set_full_screen send bool
SLOT_NAME set_name send string: the window title
SLOT_MODIFIED set_modified send bool: decorate the title of a modified document
SLOT_SIZE set_size, get_size, notify_size two SI
SLOT_POSITION set_position, get_position, notify_position two SI, relative to the parent
SLOT_UPDATE send_update geometry may have to be recomputed
SLOT_REFRESH send_refresh send string kind: recompute dynamic subwidgets
SLOT_KEYBOARD send_keyboard a key press (string, time_t)
SLOT_KEYBOARD_FOCUS send_keyboard_focus, notify_keyboard_focus, query_keyboard_focus obtain or test the keyboard focus
SLOT_KEYBOARD_FOCUS_ON send_keyboard_focus_on focus a named field inside a widget
SLOT_MOUSE send_mouse a mouse event (kind, position, modifiers, time)
SLOT_MOUSE_GRAB send_mouse_grab, notify_mouse_grab, query_mouse_grab obtain or test the mouse grab
SLOT_MOUSE_POINTER send_mouse_pointer change the shape of the pointer
SLOT_INVALIDATE, SLOT_INVALIDATE_ALL send_invalidate, send_invalidate_all schedule repainting
SLOT_INVALID query_invalid are there pending regions to repaint?
SLOT_REPAINT send_repaint repaint a region on a given renderer
SLOT_DELAYED_MESSAGE send_delayed_message deliver a message after a delay
SLOT_DESTROY send_destroy the widget is about to be destroyed

4.2.Canvas slots

A canvas is a scrollable area. The main window and the editor widgets understand these slots.

Slot Helpers Meaning
SLOT_ZOOM_FACTOR set_zoom_factor send double
SLOT_EXTENTS set_extents, get_extents four SI: the size of the scrollable contents
SLOT_VISIBLE_PART get_visible_part query four SI
SLOT_SCROLLBARS_VISIBILITY set_scrollbars_visibility send int
SLOT_SCROLL_POSITION set_scroll_position, get_scroll_position two SI
SLOT_CANVAS get_canvas read: the canvas itself
SLOT_SCROLLABLE set_scrollable write: install the widget shown in the canvas (the editor)
SLOT_CURSOR send_cursor current cursor position (used by input methods)

4.3.Slots of the main TeXmacs widget

These slots are specific to the widget created by texmacs_widget (see “Windows, the main TeXmacs widget and the flow of events”). Each bar comes with a visibility slot (send and query a bool) and a contents slot (write a widget).

Slot Helpers Meaning
SLOT_HEADER_VISIBILITY set_header_visibility, get_header_visibility all menus and icon bars
SLOT_MAIN_MENU set_main_menu the menu bar
SLOT_MAIN_ICONS, SLOT_MODE_ICONS, SLOT_FOCUS_ICONS, SLOT_USER_ICONS set_main_icons, set_mode_icons, set_focus_icons, set_user_icons the four icon bars
SLOT_SIDE_TOOLS, SLOT_LEFT_TOOLS set_side_tools, set_left_tools right and left side tools
SLOT_BOTTOM_TOOLS, SLOT_EXTRA_TOOLS set_bottom_tools, set_extra_tools tools below the document
SLOT_*_VISIBILITY set_*_visibility, get_*_visibility one for each bar above and for the footer
SLOT_LEFT_FOOTER, SLOT_RIGHT_FOOTER set_left_footer, set_right_footer send string: status messages
SLOT_INTERACTIVE_MODE set_interactive_mode, get_interactive_mode footer used for input
SLOT_INTERACTIVE_PROMPT set_interactive_prompt write: the prompt
SLOT_INTERACTIVE_INPUT set_interactive_input, get_interactive_input write the input widget, query the answer

4.4.Dialog slots

Slot Helpers Meaning
SLOT_FORM_FIELD get_form_field read: the i-th field of an inputs_list_widget
SLOT_STRING_INPUT get_string_input, set_string_input the text of an input field
SLOT_INPUT_TYPE set_input_type the type of an input field ("string", "password", …)
SLOT_INPUT_PROPOSAL add_input_proposal add a proposal (history)
SLOT_FILE, SLOT_DIRECTORY set_file, get_file, set_directory, get_directory file chooser: send a string, read the input widget

5.Commands and promises

Widgets receive behaviour from the kernel through two kinds of closures.

Commands.
A command (
Kernel/Abstractions/command.hpp

) is a reference-counted pointer to a command_rep with the virtual methods apply () and apply (object args). Commands can be built from a plain function pointer, from a callback with two void* arguments, or, most often, by deriving a small class:

class ia_command_rep: public command_rep {
  tm_window_rep* win;
public:
  ia_command_rep (tm_window_rep* win2): win (win2) {}
  void apply () { win->interactive_return (); }
  tm_ostream& print (tm_ostream& out) { return out << "<command ia>"; }
};

(from

Texmacs/Window/tm_window.cpp

). Commands built from Scheme closures are instances of object_command_rep (

Scheme/Scheme/object.cpp

), created by as_command (object) and exported to Scheme as object->command; their apply (object args) calls the closure with the elements of the list args. This is how input widgets pass their result: every port calls cmd (list_object (…)) with the text, the state of a toggle or the selected items. From Scheme, command-eval and command-apply invoke a command.

Promises.
A promise<T> (
Kernel/Containers/promise.hpp

) is a delayed computation of a value of type T: its representation has one virtual method T eval (), and p () evaluates it. Widget promises are used for the contents of submenus, which are only computed when the submenu is about to be shown (pulldown_button and pullright_button). The Scheme closure is wrapped by as_promise_widget, exported as object->promise-widget, whose eval calls the closure and checks that it returned a widget. Evaluating the promise again each time the menu opens is what makes such menus dynamic, so ports must not cache the result.

Since these closures may execute arbitrary Scheme code, ports never call them directly from inside a toolkit callback: they queue them and run them from the main loop (see the section on events in “Windows, the main TeXmacs widget and the flow of events”).

6.The widget constructors

Every widget is created by one of the global functions declared in

widget.hpp

. The kernel calls some of them directly; most of them are exported to Scheme in

Scheme/Glue/build-glue-basic.scm

under the names listed below, and used by the interpreter of

kernel/gui/menu-widget.scm

. The third column gives the keyword of the Scheme widget language that produces them.

6.1.Window and top-level widgets

C++ Scheme glue Used for
plain_window_widget (w, name, quit) (via alt-window-create-quit) decorated window
popup_window_widget (w, name) (via alt-window-create-popup) undecorated window
tooltip_window_widget (w, name) (via alt-window-create-tooltip) tooltip window
destroy_window_widget (w) destroy a window widget
texmacs_widget (mask, quit) the main window contents
popup_widget (w) contextual popup menu
file_chooser_widget (cmd, type, prompt) file dialog
inputs_list_widget (cmd, prompts) dialog with several inputs
printer_widget (cmd, file) widget-printer print dialog
color_picker_widget (cmd, bg, proposals) widget-color-picker color-input

6.2.Menus and buttons

C++ Scheme glue Keyword
horizontal_menu (a) widget-hmenu horizontal
vertical_menu (a) widget-vmenu vertical
tile_menu (a, cols) widget-tmenu tile
minibar_menu (a) widget-minibar-menu minibar
menu_separator (vertical) widget-separator ---, /
menu_group (name, style) widget-menu-group group
pulldown_button (w, pw) widget-pulldown-button =>
pullright_button (w, pw) widget-pullright-button ->
menu_button (w, cmd, pre, ks, style) widget-menu-button ("label" action)
balloon_widget (w, help) widget-balloon balloon
text_widget (s, style, col, tsp) widget-text text, labels
xpm_widget (file) widget-xpm icon

In menu_button, pre is a check mark prefix ("v", "*", "o" or empty) and ks the keyboard shortcut shown next to the entry. The style flags are described below.

6.3.Layout and containers

C++ Scheme glue Keyword
horizontal_list (a) widget-hlist hlist
vertical_list (a) widget-vlist vlist
glue_widget (hx, vx, w, h) widget-glue glue, ===, >>, …
glue_widget (col, hx, vx, w, h) widget-color color
division_widget (name, w) widget-division division, class
aligned_widget (lhs, rhs) widget-aligned aligned, item
tabs_widget (tabs, bodies) widget-tabs tabs, tab
icon_tabs_widget (us, ts, bs) widget-icon-tabs icon-tabs
responsive_tabs_widget, responsive_icon_tabs_widget widget-responsive-tabs, widget-responsive-icon-tabs responsive-tabs, responsive-icon-tabs
user_canvas_widget (w, style) widget-scrollable scrollable
resize_widget (w, style, …) widget-resize resize
hsplit_widget, vsplit_widget widget-hsplit, widget-vsplit hsplit, vsplit
extend_widget (w, a) widget-extend extend
wrapped_widget (w, quit) run quit on destruction
empty_widget () widget-empty

6.4.Input widgets

C++ Scheme glue Keyword
input_text_widget (cb, type, def, style, width) widget-input input
toggle_widget (cmd, on, style) widget-toggle toggle
setting_toggle_widget widget-setting-toggle setting-toggle
enum_widget (cb, vals, val, style, w) widget-enum enum
setting_enum_widget widget-setting-enum setting-enum
setting_group_widget widget-setting-group setting-group
choice_widget (three overloads) widget-choice, widget-choices, widget-filtered-choice choice, choices, filtered-choice
tree_view_widget (cmd, data, roles) widget-tree-view tree-view
ink_widget (cb) widget-ink ink

6.5.Dynamic widgets

C++ Scheme glue Keyword
refresh_widget (tmwid, kind) widget-refresh refresh
refreshable_widget (prom, kind) widget-refreshable refreshable, cached

A refresh_widget is built from the name of a Scheme widget; a refreshable_widget from a Scheme closure returning a widget. Both rebuild their contents when they receive SLOT_REFRESH with a matching kind. See “The Scheme widget language and its interpreter” for their use.

6.6.Widgets implemented by the kernel

A few constructors are not provided by the ports but by the kernel, in terms of the others: texmacs_output_widget (doc, style) (

Texmacs/Window/tm_button.cpp

, glue widget-texmacs-output) typesets a document into a box and shows it in a box_widget_rep; texmacs_input_widget (doc, style, name) (

Texmacs/Window/tm_window.cpp

, glue widget-texmacs-input) creates a hidden buffer and embeds a complete editor in the widget; the two box_widget functions declared in

Texmacs/tm_frame.hpp

(the second one is glued as widget-box) also return a box_widget_rep, a class of

tm_button.cpp

derived from simple_widget_rep which paints a typeset box.

7.Style flags

Many constructors take an int style, a combination of:

Flag Value Meaning
WIDGET_STYLE_MINI 1 smaller font
WIDGET_STYLE_MONOSPACED 2 monospaced font
WIDGET_STYLE_GREY 4 greyed text
WIDGET_STYLE_PRESSED 8 button shown as pressed
WIDGET_STYLE_INERT 16 no action (disabled)
WIDGET_STYLE_BUTTON 32 render explicitly as a button
WIDGET_STYLE_CENTERED 64 centered text
WIDGET_STYLE_BOLD 128 bold text

The Scheme constants widget-style-mini, …, widget-style-bold of

kernel/gui/gui-markup.scm

have the same values. The additional Scheme constant widget-style-verb (256) is only interpreted on the Scheme side (it suppresses the translation of the entries of an enum). The function get_default_styled_font of

widget.cpp

maps a style to one of the default fonts.

8.The simple_widget_rep contract

Besides the constructors, a port must provide a class simple_widget_rep, from which the kernel derives the editor (editor_rep in

Edit/editor.hpp

) and the box widgets of

tm_button.cpp

. It represents a canvas that the kernel paints itself with a renderer, and which receives raw events. The virtual methods the kernel overrides are listed in a comment at the end of

widget.hpp

:

bool is_editor_widget ();
bool is_embedded_widget ();
void handle_get_size_hint (SI& w, SI& h);
void handle_notify_resize (SI w, SI h);
void handle_keypress (string key, time_t t);
void handle_keyboard_focus (bool new_focus, time_t t);
void handle_mouse (string kind, SI x, SI y, int mods, time_t t, array<double> data);
void handle_set_zoom_factor (double zoom);
void handle_clear (renderer ren, SI x1, SI y1, SI x2, SI y2);
void handle_repaint (renderer ren, SI x1, SI y1, SI x2, SI y2);

Which header defines simple_widget_rep is chosen at compile time:

Edit/editor.hpp

and

Texmacs/Window/tm_button.cpp

include

Macro Header Class
AQUATEXMACS
NS/ns_simple_widget.h
ns_simple_widget_rep
QTTEXMACS without QTWKTEXMACS
Qt/qt_simple_widget.hpp
qt_simple_widget_rep
VUETEXMACS
Vue/vue_widget.hpp
vue_simple_widget_rep
otherwise (X11, SDL, Qtwk)
Widkit/simple_wk_widget.hpp
simple_widget_rep itself

The other headers end with a typedef such as typedef qt_simple_widget_rep simple_widget_rep. Note that the Qtwk port defines QTTEXMACS as well, so that tests of QTTEXMACS alone also select it.

9.System-wide routines

gui.hpp

declares the routines that do not concern a particular widget. The main ones are gui_open, gui_start_loop and gui_close (called from

Texmacs/Texmacs/texmacs.cpp

), gui_interpose, which registers the function that the main loop must call regularly (the server registers texmacs_interpose_handler), needs_update and check_event, which coordinate the typesetter with pending events, gui_refresh (for instance after a change of the interface language), the clipboard functions set_selection, get_selection and clear_selection, and show_help_balloon and show_wait_indicator. The complete list, with the obligations of a port, is given in “Adding new widgets and porting to other toolkits”.

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