GNU TeXmacsUser manualReferenceAboutEnglish
Windows

A window is a top level TeXmacs window with a menu bar, icon bars, side and bottom tool areas, a canvas and a footer. The canvas shows one view, whose editor draws the document and decides what the menus, toolbars and footer contain. This page describes the class tm_window_rep, how windows are created and closed, how their menus and toolbars are built and cached, and the other kinds of windows which the server manages: dialogs, embedded TeXmacs editors and the “alternative” windows which Scheme builds from widgets. All of it is written against the abstract widget interface of

Graphics/Gui/widget.hpp

and

message.hpp

, so it is independent of the toolkit; how the Qt port implements the corresponding widgets is described in the Qt implementation, the other ports (Vue, Cocoa, and X11, SDL and Qtwk with Widkit) in the ports, and the Scheme side of menus and dialogs in widgets from Scheme.

The TeXmacs widget built by texmacs_widget (mask, quit) has the following parts; the numbers are the which arguments used to address the bars (see menus and toolbars below):

+-------------------------------------------------------------+
| header: main menu bar (-1)                                  |
| icon bars: main (0), mode (1), focus (2), user (3)          |
+----------+--------------------------------+-----------------+
| left     | canvas: the editor of the view  | side tools (10) |
| tools    | (scrollable)                   |                 |
| (11)     |                            |                 |
+----------+--------------------------------+-----------------+
| bottom tools (20), extra tools (21)                          |
| footer: messages, context, interactive prompts              |
+-------------------------------------------------------------+

Three other kinds of top level windows exist besides these document windows, and it is easy to confuse them:

Dialogs of the server

File choosers and popup questions opened by tm_frame_rep::dialogue_start; there is a single slot for them (dialogs).

Alternative windows

Windows built by Scheme from widgets (top-window, dialogue-window, tooltips), identified by small integers (alternative windows).

Embedded editors

Not top level windows themselves, but complete editors inside a widget, each with its own buffer, view and an anonymous tm_window_rep (embedded widgets).

Only document windows have an identifier, appear in windows_list (window-list) and can be current.

1.The class tm_window_rep

class tm_window_rep
(a TeXmacs window)

Declared in

Texmacs/tm_window.hpp

, implemented in

Texmacs/Window/tm_window.cpp

. Its public fields are:

widget win

The top level window widget.

widget wid

The TeXmacs widget inside it, with menus, icon bars, side and bottom tools, the canvas and the footer. All the methods of the class operate on wid through the generic widget messages (set_main_menu, set_zoom_factor, set_left_footer, …).

url id

The identifier tmfs://window/n, or url_none () for the window of an embedded widget.

hashmap<tree,tree> props

Arbitrary properties, set and read from Scheme with window-set-property and window-get-property.

int serial

A serial number, unique for the session, returned by window-get-serial.

double zoomf

The zoom factor, multiplied by retina_zoom.

Its protected fields are the menu caches menu_current and menu_cache, the state of an interactive prompt in the footer (text_ptr and call_back), and the current title cur_title.

There are two constructors:

tm_window_rep (widget wid, tree geom)

For ordinary windows: wraps wid in a top level window of the given geometry (texmacs_window_widget), allocates an identifier and takes the default zoom factor of the server.

tm_window_rep (tree doc, command quit)

For embedded widgets: builds a TeXmacs widget without any bars, uses it as both win and wid, does not allocate an identifier, and takes the zoom factor from the document if it has one.

The destructor releases the identifier. The methods are described below.

2.Window identifiers and the window table

Windows are owned by the static table tm_window_table of

Texmacs/Data/new_window.cpp

, which maps window identifiers to tm_window_rep pointers. The identifiers are URLs tmfs://window/n, allocated by create_window_id (from the constructor of tm_window_rep) and released by destroy_window_id (from its destructor); the counter is never decremented, so identifiers are not reused during a session.

Conversions

concrete_window (url) is a lookup in tm_window_table; abstract_window returns the field id of the window.

Enumeration

windows_list returns the identifiers of all document windows, in order of creation. get_nr_windows (windows-number) is something else: the number of top level windows as counted by the GUI back-end (nr_windows). Under Qt it is maintained by qt_window_widget_rep for all its non “fake” windows, which includes dialogs and alternative windows, and likewise by ns_window_widget_rep in the Cocoa port (

ns_widget.mm

); the Widkit ports count their window_reps (

x_window.cpp

,

sdl_window.cpp

,

qtwk_window.cpp

), and Vue its SDL and virtual windows (

vue_gui.cpp

), popups included. In no case is it the length of windows_list.

Current window

has_current_window, get_current_window (returns the empty URL if there is none) and concrete_window (). There is no stored current window: it is always the window of the current view (see the current view).

Queries

buffer_to_windows, window_to_buffer and window_to_view, which searches the view history for the view attached to the window.

3.Creating windows

A new window is created by new_window (bool map_flag, tree geom) (not declared in any header). It

  1. builds the TeXmacs widget with texmacs_widget (mask, quit), where the bits of mask are computed from the preferences header (1), main icon bar (2), mode dependent icons (4), focus dependent icons (8), user provided icons (16), status bar (32), bottom tools (256) and extra tools (512), so that only the bars enabled in the preferences are initially visible;

  2. creates the tm_window_rep, which wraps the widget in a top level window with texmacs_window_widget (see geometry of top level windows) and allocates an identifier;

  3. registers the window in tm_window_table and maps it.

The quit command passed to the widget is a kill_window_command_rep, which is called when the user clicks on the close box of the window. It does not close anything itself: it schedules the Scheme command (safely-kill-window id) with exec_delayed, so that the user can be asked for confirmation. (It holds a pointer to an URL which is filled in only once the identifier is known.)

A new window is empty; it shows nothing until a view is attached to it. The exported routines therefore always combine the two steps:

open_window (geom)

Creates a new scratch buffer and shows it in a new window (open-window).

new_buffer_in_new_window (name, doc, geom)

Creates the buffer from doc if it does not exist, opens a new window and shows a passive view on the buffer in it, with the focus (open-buffer-in-window; this is how load-buffer implements :new-window).

clone_window ()

Opens a new window with a passive view on the current buffer (clone-window); a new view is created if all existing views are displayed in windows, so that the two windows show two views on the same document.

create_buffer ()

Creates a new scratch buffer in the current window (new-buffer).

The user commands new-document and new-document* choose between open-window and new-buffer according to the buffer management preference: with the value separate (the default on macOS and Windows, tested by the mode predicate window-per-buffer? of

kernel/texmacs/tm-modes.scm

) each document gets its own window; with shared (the default elsewhere) documents replace each other in the same window.

4.Closing windows

At the C++ level, windows are destroyed by the file local delete_window, which detaches the view of the window (the view is kept, so that the “modified” status of its buffer remains available), unmaps the window, removes it from the table, destroys the window widget and deletes the tm_window_rep. It is called by

kill_window (win)

(kill-window) Makes the most recent view which is shown in another window current and deletes the window. If there is no other window, the program quits, unless it acts as a server for remote clients (number_of_servers ()), in which case the window is deleted anyway. The buffer of the window is not closed.

kill_current_window_and_buffer ()

Quits if there is only one buffer; otherwise closes the current window, and also removes its buffer if no other window shows it.

The user command is safely-kill-window in

texmacs/texmacs/tm-server.scm

, called from the close box (with the window as argument) and, through close-document, from the File menu (without argument):

  1. If the current buffer is an embedded buffer and no window was given, it deletes the alternative windows which contain it (see embedded widgets).

  2. If windows-number is at most 1, it calls safely-quit-TeXmacs, which asks for confirmation if some non-auxiliary buffer is modified and then quits.

  3. Otherwise it asks for confirmation if the buffer of the window is modified, then calls kill-window and, after an idle delay of 100 milliseconds, buffer-close on that buffer.

So closing a window also closes its buffer, even when the buffer is still shown in another window: kill_buffer then gives that other window a view on another buffer. Since windows-number counts all toolkit windows, an open dialog or tool window can make the second test fail for the last document window; kill_window then quits anyway (without the confirmation of safely-quit-TeXmacs, but only after the confirmation about the buffer of the window).

The entries of the File menu call close-document and close-document*, which choose between safely-kill-window and safely-kill-buffer according to the buffer management preference, in the same way as new-document above.

5.Geometry of top level windows

texmacs_window_widget (wid, geom) (

tm_window.cpp

) wraps the TeXmacs widget in a top level window and decides its size and position:

  1. The default size and position come from the -geometry command line option (geometry_w, geometry_h, geometry_x, geometry_y; by default 800\(\times\)600 at the origin; a size of exactly 800\(\times\)600 is multiplied by retina_zoom on high resolution screens, and the ports which do not define QTTEXMACS, that is all but Qt and Qtwk, add room for the side tools). Negative coordinates count from the right or bottom edge of the screen.

  2. An explicit geometry geom (a tuple of width and height, from open-window-geometry) overrides the default size, and disables the saved size and position.

  3. Otherwise, the size and position saved for the window name are used.

Window names are made unique by unique_window_name: the first TeXmacs window is called TeXmacs, the next ones TeXmacs:2, TeXmacs:3, …; the names are released by notify_window_destroy. When the user moves or resizes a window, the toolkit calls notify_window_move and notify_window_resize, which store the new values in the user preferences abscissa name, ordinate name, width name and height name (in pixels; under Qt and Qtwk, a resize which keeps the width and changes the height by at most 80 pixels is ignored, presumably to absorb toolbars appearing and disappearing). Popup windows are never recorded. Since the names are per session numbers, the second window of a session gets the geometry that the second window of the previous session had. Under Qt, plain_window_widget and QTMPlainWindow also save and restore the geometry of every other non-popup top level window (dialogs, alternative windows) under its title (

Plugins/Qt/qt_widget.cpp

,

Plugins/Qt/QTMWindow.cpp

).

6.Menus and toolbars

Menus and toolbars are given as a string which holds a Scheme menu expression, such as "(horizontal (link texmacs-menu))"; it is quoted and evaluated to obtain the menu. get_menu_widget (which, menu, w) turns it into a widget:

  1. It temporarily sets the_drd to the DRD of the view shown in the window, since menu entries may depend on the document.

  2. It expands the menu with the Scheme function menu-expand, which evaluates all dynamic parts and yields a static description.

  3. If the expansion is in the cache menu_cache: if it is also the one currently installed in the slot which (menu_current), nothing needs to be done and the function returns false; otherwise, for the menu bar and the icon bars (slots below 10), the cached widget is reused.

  4. Otherwise, the widget is built with the Scheme function make-menu-widget (or make-menu-widget* with a size for the side tools) and cached if menu caching is enabled and either the slot is a tool area or cache-menu? accepts the expansion.

Two consequences are worth knowing. First, the “unchanged” test only works for expansions which are in the cache: a menu which cache-menu? rejects (any menu containing an input entry) is rebuilt every time, even if it did not change, and menu-expand is run on every call in any case. Second, for the tool areas (slots 10 and above) a cached widget is never reused for a different slot content; the cache entry only serves the “unchanged” test.

The slots are numbered as follows: \(- 1\) for the main menu bar, \(0\) to \(3\) for the main, mode, focus and user icon bars, \(10\) and \(11\) for the right and left side tools, and \(20\) and \(21\) for the bottom and extra tools. The public methods menu_main, menu_icons, side_tools and bottom_tools first force the lazy loading of all Scheme modules ((lazy-initialize-force)), then call get_menu_widget and, if it returns true, install the widget in the TeXmacs widget. refresh empties the cache. The set_…_flag and get_…_flag methods show, hide and query the corresponding bars.

These methods are normally not called directly: the menus of a window are installed by its editor. When the editor is resumed (edit_interface_rep::resume, called by attach_view, switch_to_window and var_focus_on_buffer), it calls

SERVER (menu_main ("(horizontal (link texmacs-menu))"));
SERVER (menu_icons (0, "(horizontal (link texmacs-main-icons))"));
SERVER (menu_icons (1, "(horizontal (link texmacs-mode-icons))"));
SERVER (menu_icons (2, "(horizontal (link texmacs-focus-icons))"));
SERVER (menu_icons (3, "(horizontal (link texmacs-extra-icons))"));

and similarly side_tools (1, …), side_tools (0, …), bottom_tools (0, …) and bottom_tools (1, …) with the dynamic menus texmacs-left-tools, texmacs-side-tools, texmacs-bottom-tools and texmacs-extra-tools, which receive the window as an argument. update_menus, called from apply_changes when the document changed and the user is idle, does the same. The SERVER macro makes the editor current during each call, because the server routines act on the current window (see working in the context of another view). For cacheable menus, the comparison with menu_current makes these repeated calls cheap when nothing has changed.

7.Other methods of tm_window_rep

Title and status.
set_window_name (only sends the title to the widget if it changed), set_window_url (the file associated with the window), set_modified (the “modified” mark of the title bar), map and unmap.
Canvas.
set_window_zoom_factor and get_window_zoom_factor (the stored factor includes retina_zoom, the returned one does not), get_visible, get_extents, set_extents, set_scrollbars, get_scroll_pos, set_scroll_pos. They forward to the canvas of the TeXmacs widget.
Footer.
get_footer_flag, set_footer_flag, set_left_footer, set_right_footer.

The contents of the footer are decided by the editor (

Edit/Interface/edit_footer.cpp

). set_message (left, right, temp) stores a message and calls notify_change (THE_DECORATIONS); set_footer, called from update_menus, displays the message if there is one, and otherwise computes a description of the context of the cursor (mode, language, font, and the path of enclosing tags) with set_left_footer and set_right_footer. The server routine tm_frame_rep::set_message (Scheme: set-message) forwards to the current editor.

Interactive input in the footer.
A single line prompt can replace the footer:
interactive (name, type, def, s, cmd)

Installs a text widget with the prompt name and an input field of the given type with the proposals def, switches the footer to interactive mode, and remembers where to store the answer (s) and what to call when the user is done (cmd). If the footer is already in interactive mode, the answer is "cancel" at once.

interactive_return ()

Called by the input field (through an ia_command_rep) when the user presses return: stores the answer, leaves interactive mode and calls the callback.

8.Dialogs and interactive commands

Dialog windows.
tm_frame_rep::dialogue_start (name, wid) (
tm_dialogue.cpp

) opens wid in a plain window with the translated title name, centered on the current window. There is a single dialog slot in the server (dialogue_win, dialogue_wid); while it is occupied, further calls are ignored. dialogue_inquire (i, arg) reads the \(i\)-th input field of the dialog (field 0 is the dialog widget itself) and dialogue_end closes it. These are the routines behind the Scheme function dialogue-end.

A dialogue_command_rep is the callback of such a dialog. When the user validates, it reads the fields from last to first, stops (and closes the dialog) if one of them is "#f" (the dialog was cancelled), records the answers with the Scheme function learn-interactive (except for password fields, which are recorded as empty strings) and schedules both (dialogue-end) and the call of the Scheme function with the answers as arguments.

File choosers.
choose_file (fun, title, type, prompt, name) builds a file_chooser_widget with such a callback, presets its directory from name (or uses . for a scratch buffer), and, unless the type is image or empty, also the file name, adapting the suffix to the requested format (for instance when exporting). It shows the chooser with dialogue_start and gives the keyboard focus to its file or directory field. It is exported as cpp-choose-file; Scheme code normally uses the higher level choose-file.
Interactive commands.
interactive (fun, p) asks the user for the arguments of the Scheme function fun; it is the implementation of tm-interactive, which is used by the interactive menu entries. p is a tuple with one entry per argument, each of which is either a quoted prompt or a tuple of a prompt, a type (string, password, a file type, …) and proposals. Depending on the situation, the arguments are asked for in one of two ways:

With no arguments at all, the function is called at once.

Note that the Scheme function interactive does not always reach this code: it calls tm-interactive-hook, which is set to tm-interactive-new in

generic/generic-menu.scm

. When the side tools are enabled (side-tools?, that is, the side tools and developer tool preferences are both on), that function shows an interactive-tool in the bottom tool area instead, and tm_frame_rep::interactive is not used.

9.Embedded TeXmacs widgets

An embedded TeXmacs widget is a complete editor inside a dialog or a side panel, used for instance for input fields with mathematics, for the search and replace panes and for the bibliography and database tools. It is created from Scheme with the texmacs-input widget, which calls texmacs_input_widget (doc, style, name):

  1. enrich_embedded_document turns doc into a complete TeXmacs document with the given style and an initial environment suitable for a small widget: automatic page size, small screen margins, a fixed resolution and zoom factor and the no-zoom flag. The variables of an outer with are moved to the initial environment.

  2. The buffer name is name, or a fresh tmfs://aux/TeXmacs-input-n if none is given. The buffer is created, or its contents are replaced if it exists.

  3. A passive view is obtained and a window is created with the second constructor of tm_window_rep, which has no identifier and is not registered in tm_window_table.

  4. The master of the new buffer is set to the master of the current buffer, so that links and relative file names are resolved as in the surrounding document.

  5. The view is attached by hand: vw->win is set, the editor is installed as the canvas, and ed->mvw is set to the current view.

  6. The result is the TeXmacs widget wrapped with a close_embedded_command_rep, which runs when the widget is destroyed.

The close command gives the focus back to the window of the master view (or to the first window), removes the buffer and deletes the window. It asserts that the buffer has exactly one view: an embedded buffer must not be shown elsewhere.

Embedded widgets are placed either in an “alternative” window (see below), for instance in dialogs, or in the side tools of a main window, for instance the search and replace tools (

generic/search-widgets.scm

) and the format tools (

generic/format-tools.scm

). To be able to close the alternative windows when their buffer is closed, the constructor of the close command records the handle of the most recently allocated alternative window (last_window_handle) under the buffer name in window_by_name. window_search (name) returns the handles which still exist, is_embedded_buffer tests whether there is one (buffer-embedded?), and safely-kill-buffer and safely-kill-window in

tm-server.scm

use alt-window-search to delete them. This relies on the order of calls in

menu-widget.scm

, where the handle is allocated before the widget is built.

Warning 1. The mechanism is fragile. For an embedded widget in the side tools, no alternative window is allocated, and last_window_handle is whatever handle was allocated last, for an unrelated window. If that window still exists when the embedded buffer is closed, window_search returns it and safely-kill-buffer or safely-kill-window deletes the wrong window.

The Scheme constructors of these widgets (make-texmacs-input, make-texmacs-output) pass the document through attach-resize; inside a resize widget (when global-resize is set), this sets the page medium to papyrus and fixed page dimensions, which take precedence over the automatic page medium mentioned above.

10.Output widgets and box widgets

Texmacs/Window/tm_button.cpp

contains the widgets which only display typeset material:

box_widget_rep

A simple_widget_rep which shows a box, centered and scaled, optionally on a background color. It forwards mouse moves, clicks and releases to the box as "enter", "leave", "click", "drag" and "select" messages, which makes simple interactive boxes possible; box_broadcast (msg) broadcasts a message to the whole box of the box widget which last received a mouse event.

box_widget (b, trans), box_widget (font, s, col, trans, ink)

Widgets for a given box, or for a string in a given font. The second one is exported as widget-box and used for menu entries with special rendering.

texmacs_output_widget (doc, style)

Typesets a document without creating a buffer or an editor, and shows the resulting box. It builds an edit_env by hand (initialize_environment applies the style, the modules and the initial environment), typesets the body with the lazy typesetter at its natural width, and wraps the box. If the document belongs to a project, the references, auxiliary data and attachments of the project are used. It is behind the texmacs-output widget.

tree_extents (doc)

Computes the size of a document in a similar way (but without enrich_embedded_document and without project data), in units of 5 pixels rounded up; exported as tree-extents, which is only used by the old GUI code in

kernel/old-gui/

.

get_texmacs_widget_size (wid)

The size hint of such a widget.

11.Alternative windows

The end of

tm_window.cpp

implements a second, simpler family of top level windows, identified by small integers rather than by URLs. They are used for the top level windows which Scheme builds from widgets (dialogs and tool windows made with top-window, dialogue-window and similar functions of

kernel/gui/menu-widget.scm

, and tooltips) through the alt-window-… glue, which the code describes as “transitional”. Editor windows (open-window) are tm_window_reps, and the dialogs of tm_frame_rep (file choosers, popup questions) use the separate dialog slot.

window_handle ()

Allocates a new handle (and records it in last_window_handle, see above).

window_create (win, wid, name, quit), window_create_plain, window_create_popup, window_create_tooltip

Wrap wid in a top level window of the corresponding kind and store it in the static table window_table.

window_delete (win)

Sends a destroy notification to the widget (so that embedded TeXmacs widgets inside it are closed) and destroys it; a guard prevents recursive deletion of the same window.

window_show, window_hide, window_get_size, window_set_size, window_get_position, window_set_position

The obvious operations, with sizes in pixels.

These windows have no tm_window_rep, no view and no menus of their own. They are not returned by windows_list.

12.Refreshing dynamic widgets

Widgets built from Scheme may contain refresh parts, which are rebuilt when they are sent a refresh message. windows_refresh (kind) sends send_refresh (w, kind) to every alternative window. It is called by the interpose handler with "auto", in which case it does nothing unless a refresh was requested with windows_delayed_refresh (ms) and the delay has elapsed; after an automatic refresh, the next one is postponed indefinitely. Scheme code calls refresh-now with a specific kind to refresh only the widgets of that kind. The mechanism is described in more detail in refreshing dynamic widgets.

13.Pitfalls

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