GNU TeXmacsUser manualReferenceAboutEnglish
Windows, the main TeXmacs widget and the flow of events

1.Window widgets

A window is represented by a widget as well. Any widget w can be put into a window with

widget plain_window_widget (widget w, string name, command quit= command ());
widget popup_window_widget (widget w, string name);
widget tooltip_window_widget (widget w, string name);
void   destroy_window_widget (widget w);

The result is a window widget, which understands the window-related slots: set_visibility maps or unmaps it, set_name changes its title, set_position and set_size move and resize it, get_identifier returns a non-zero identifier once the window exists. The string name is not the title but a unique identifier, under which the geometry of the window is remembered between sessions (see notify_window_move, notify_window_resize, get_preferred_position and get_preferred_size in

Texmacs/Window/tm_window.cpp

). The command quit is executed when the user closes the window.

The abstract class window_rep of

window.hpp

, with its constructors plain_window and popup_window, is a lower level interface used only by ports which build on Widkit (it is implemented in

Plugins/X11/x_window.cpp

,

Plugins/SDL/sdl_window.cpp

and

Plugins/Qtwk/qtwk_window.cpp

). Kernel code never uses it directly.

The window widgets of the other ports are qt_window_widget_rep (Qt), ns_window_widget_rep (Cocoa, an NSWindow) and vue_plain_window_widget_rep (Vue, an SDL window, or a virtual window drawn inside a host window in the single window mode of TEXMACS_VUE_SINGLE_WINDOW and when running headless). In Vue, plain_window_widget does not wrap the file chooser and the forms of inputs_list_widget: these open their own (system or TeXmacs) dialog.

2.The TeXmacs windows

A TeXmacs editing window is an instance of tm_window_rep (

Texmacs/tm_window.hpp

), which holds two widgets: the window widget win (the result of plain_window_widget) and, inside it, the main TeXmacs widget wid, created by

widget texmacs_widget (int mask, command quit);

The main widget is implemented by each port (by qt_tm_widget_rep in Qt, ns_tm_widget_rep in Cocoa, vue_texmacs_widget_rep in Vue and the Widkit class texmacs_widget_rep of

Widkit/Misc/texmacs_widget.cpp

in the other ports) and contains the menu bar, the four icon bars, the left and right side tools, the canvas, the bottom and extra tools and the footer. The bits of mask say which parts are initially visible:

Bit Preference Part
1 header menu and icon bars as a whole
2 main icon bar main icons
4 mode dependent icons mode icons
8 focus dependent icons focus icons
16 user provided icons user icons
32 status bar footer
64, 128 (currently not set) right and left side tools
256 bottom tools bottom tools
512 extra tools extra tools

A mask of zero, used for embedded editors, asks the port for a stripped-down main widget without any bars (in Qt a qt_tm_embedded_widget_rep, in Cocoa an ns_tm_embedded_widget_rep; Vue uses the same class with all bars hidden).

The main widget understands a large set of slots, sent by the methods of tm_window_rep: SLOT_SCROLLABLE (the canvas, whose contents is the editor of the view shown in the window), SLOT_MAIN_MENU, SLOT_MAIN_ICONS, …, SLOT_SIDE_TOOLS, SLOT_BOTTOM_TOOLS, the visibility slots of the bars, SLOT_LEFT_FOOTER and SLOT_RIGHT_FOOTER, the slots of the interactive prompt, the zoom factor, the extents and the scroll position of the canvas, and SLOT_FULL_SCREEN. The editor itself is a simple_widget_rep; once it is installed as the canvas, it keeps a pointer cvw to the main widget in order to send messages back to it (get_canvas, set_extents, send_keyboard_focus_on, …).

How windows are created and closed, how views are attached to them, how the menus and icon bars are built from Scheme and cached (tm_window_rep::get_menu_widget), and how the footer is used for interactive input is described in windows, in the chapter on the server. The present chapter is only concerned with the widgets involved.

3.Dialogs

3.1.Dialogs built by the kernel

Texmacs/Window/tm_dialogue.cpp

builds two kinds of dialogs directly in C++: file choosers (tm_frame_rep::choose_file, glue cpp-choose-file), from a file_chooser_widget initialized with set_directory and set_file, and the forms which ask for the arguments of an interactive command (tm_frame_rep::interactive, glue tm-interactive), from an inputs_list_widget whose fields are configured with set_input_type, set_string_input and add_input_proposal and read with get_form_field. dialogue_start wraps the dialog into a plain_window_widget, centers it on the current window and shows it. The details, including when the footer is used instead of a dialog, are in dialogs and interactive commands.

3.2.Windows created from Scheme

Most dialogs are nowadays written in Scheme and displayed in auxiliary windows managed by a few functions of

tm_window.cpp

(the glue names are in parentheses):

int window_handle () (alt-window-handle)

void window_create (int win, widget wid, string name, command quit) (alt-window-create-quit)

void window_create_plain (int win, widget wid, string name) (alt-window-create-plain)

void window_create_popup (int win, widget wid, string name) (alt-window-create-popup)

void window_create_tooltip (int win, widget wid, string name) (alt-window-create-tooltip)
(auxiliary windows)

window_handle returns a fresh integer handle. The creation functions wrap wid with the corresponding window constructor and store the result in the table window_table. Further functions (alt-window-show, alt-window-hide, alt-window-get-size, alt-window-set-position, …) send the usual window slots to the stored window widget.

window_delete (alt-window-delete) removes the window from the table, sends SLOT_DESTROY to it (so that embedded editors can clean up, see wrapped_widget below) and calls destroy_window_widget. The Scheme functions top-window, dialogue-window and interactive-window are built on top of these primitives.

3.3.Contextual menus

The contextual menu of the editor shows how a menu can be displayed outside the menu bar. In edit_interface_rep::mouse_adjust (

Edit/Interface/edit_mouse.cpp

):

SERVER (menu_widget ("(vertical (link " * menu * "))", wid));
widget popup_wid= ::popup_widget (wid);
popup_win= ::popup_window_widget (popup_wid, "Popup menu");
…
set_position (popup_win, wx+ ox+ x, wy+ oy+ y);
set_visibility (popup_win, true);
send_keyboard_focus (this);
send_mouse_grab (popup_wid, true);

In the Qt port, popup_widget applied to a vertical_menu yields a native QMenu, and in the Cocoa port a native NSMenu shown by popUpMenuPositioningItem when the mouse grab is sent (ns_menu_rep). Other contents become an undecorated window which disappears when the pointer leaves it (TMPopupPanel in Cocoa). The Vue port has no native menus: the popup is an undecorated SDL window, laid out by Clay and dismissed when the pointer leaves it, and the grab is implicit.

3.4.Embedded TeXmacs widgets

texmacs_input_widget embeds a full editor in a dialog or a side panel: it creates an auxiliary buffer, a passive view on it and an anonymous tm_window_rep whose main widget is built with a mask of zero, installs the editor as the canvas with set_scrollable as for an ordinary window, and returns the main widget wrapped by wrapped_widget, whose command (close_embedded_command) is executed on SLOT_DESTROY and closes the auxiliary buffer. texmacs_output_widget (

tm_button.cpp

) is much lighter: it typesets the document once into a box and returns a box_widget_rep, a read-only simple_widget_rep which paints the box. Both are described in more detail in embedded TeXmacs widgets.

4.Refreshing dynamic widgets

Besides the bars of the main window, which are rebuilt by update_menus, widgets built with refresh, refreshable or cached are updated through the SLOT_REFRESH message. The kernel sends it to all windows in

void
windows_refresh (string kind) {
  if (kind == "auto" && texmacs_time () < refresh_time) return;
  iterator<int> it= iterate (window_table);
  while (it->busy ()) {
    int id= it->next ();
    send_refresh (window_table[id], kind);
    …
  }
  if (kind == "auto") windows_delayed_refresh (1000000000);
}

There are two ways to trigger it:

A refresh widget only recomputes itself if its own kind is "any" or equal to the kind of the message, and, as for the bars of the main window, only rebuilds its contents when the expansion of its Scheme description changed.

Remark 1. windows_refresh iterates over window_table, which only contains the auxiliary windows created with window_create and friends, not the main editing windows. In the Qt port, a window widget which receives SLOT_REFRESH emits a global Qt signal to which all refresh widgets are connected, including those in the side tools of the main windows; hence these are refreshed as well, but only if at least one auxiliary window is open (see “The Qt implementation”). A port which dispatches SLOT_REFRESH only to the subwidgets of the receiving window must keep this in mind. The ports handle it as follows:

5.The flow of events and commands

5.1.Keyboard and mouse events

Events on the document canvas are delivered to the handle_* methods of the simple_widget_rep (that is, to the editor). A port should not call these handlers from inside a toolkit callback, because they may run arbitrary Scheme code and modify the document while the toolkit is in an inconsistent state. The Qt port therefore queues events: for example, QTMWidget::keyPressEvent translates the Qt event into a TeXmacs key name and calls the_gui->process_keypress, which appends an event to a queue and asks for an update. In qt_gui_rep::update, queued events are processed one after the other (handle_keypress, handle_mouse, …), then the interpose handler registered with gui_interpose is run (it lets the editors apply their changes, typeset and update the menus), and finally the invalid regions of all canvases are repainted through handle_repaint.

The Cocoa port copies this design (event_queue and command_queue in

ns_gui.h

, an update cycle driven by an NSTimer). The Vue port has no callbacks at all: its main loop (

vue_gui.cpp

) collects the SDL events into the input state of each window, then lays the windows out; during the layout, the canvas consumes the pending key and mouse event and calls its handle_* methods, while buttons only push their command onto a list. The commands are run after the layout, followed by the interpose handler and the repainting of the canvases. The Widkit ports translate the events of their window system into Widkit events, which travel down the widget tree to the canvas.

5.2.Commands

Commands attached to menu entries and other widgets follow a similar path. Take a menu entry ("New" (new-document)):

  1. the Scheme interpreter wraps the action into a command whose closure calls exec-delayed on (protected-call (lambda () (new-document))) (see make-menu-command in

    kernel/gui/menu-widget.scm

    );

  2. when the user activates the entry, the port calls the command (in Qt, QTMCommand::apply queues it with the_gui->process_command; Cocoa does the same, and Vue appends it to cmd_list, run after the layout of the frame);

  3. the command, run from the main loop, only schedules the action with exec_delayed;

  4. the delayed action is executed by protected_call (

    Scheme/Scheme/object.cpp

    ), which surrounds it with before_menu_action and after_menu_action of the current editor (or cancel_menu_action if an exception occurs): the editor state is archived for undo, and afterwards windows_delayed_refresh (1) requests a refresh of the dynamic widgets;

  5. the resulting document changes set flags in the editor, so that the next apply_changes retypesets the document and, if needed, calls update_menus.

Input widgets use commands with arguments: their call-backs are invoked with cmd (list_object (…)) and the Scheme wrapper menu-protect also defers the actual call with exec-delayed and protected-call.

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