1 xtk_focus
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_focus.h

A focus manager: Ctrl+Tab and Ctrl+Shift+Tab move keyboard focus between widgets.

The widgets of the toolkit have no common base type, and none of them includes this header. A program that wants Ctrl+Tab to move keyboard focus between the widgets of a window makes a focus manager and registers each widget with it, in Tab order. A program that does not want this never uses this header.

Why Ctrl+Tab. Plain Tab is left to the widgets: a text view inserts a tab character on Tab, and the other widgets ignore it. Ctrl+Tab always moves focus, from every widget, with no exceptions.

Registering. The manager does not know widget types. For each widget the program gives a pointer to the widget, its window, and small adapter functions: one that gives or takes focus (required) and one that says whether the widget is enabled (optional). Registration order is Tab order. A widget whose real focus can be on one of several child windows (a data grid's cells) is registered with xtk_focus_manager_register_composite() and an extra adapter that says whether a window belongs to it.

Moving focus. The manager never remembers which widget has focus. On each Ctrl+Tab it asks the server (XGetInputFocus()) and looks for the registered widget that owns the focus window, so it stays right when focus moved by a mouse click. The xtk widgets take focus on their own button press, so clicks need nothing from the manager. Ctrl+Tab skips a widget that is disabled, and one whose window is not viewable: a widget on a hidden notebook page, or one that was hidden, is skipped. One manager can therefore hold the widgets of every page of a notebook.

Hints. Optionally, a short text can be attached to each widget with xtk_focus_manager_set_hint(), and one callback set with xtk_focus_manager_set_hint_callback(). When focus moves onto a widget, the callback gets its text, for example to show "Space: toggle" in a status bar. The manager does nothing else with the text.

Events. In the event loop, after each widget's own *_handle_event() has had the event, pass it to xtk_focus_manager_handle_event(). It returns 1 when the event was Ctrl+Tab or Ctrl+Shift+Tab.

Teardown. Call xtk_focus_manager_unregister() before a registered widget is destroyed: Ctrl+Tab on a freed widget is a use after free. Call xtk_focus_manager_destroy() when the window closes.

Example.

static void button_set_focused(void *widget, int focused) {
    xtk_button_set_focused((XtkButton *)widget, focused);
}
static int button_is_enabled(const void *widget) {
    return xtk_button_get_enabled((const XtkButton *)widget);
}

XtkFocusManager *focus = xtk_focus_manager_create(display);
xtk_focus_manager_register(focus, ok_button, button_set_focused, button_is_enabled,
                           xtk_button_get_focus_window(ok_button));
xtk_focus_manager_register(focus, cancel_button, button_set_focused, button_is_enabled,
                           xtk_button_get_focus_window(cancel_button));
xtk_focus_manager_focus_first(focus);

// In the event loop:
xtk_button_handle_event(ok_button, &event);
xtk_button_handle_event(cancel_button, &event);
xtk_focus_manager_handle_event(focus, &event);

// At the end:
xtk_focus_manager_unregister(focus, ok_button);
xtk_focus_manager_unregister(focus, cancel_button);
xtk_focus_manager_destroy(focus);
 * 

Note: One manager holds at most XTK_FOCUS_MANAGER_MAX_WIDGETS widgets; more are ignored. Each Ctrl+Tab costs a round trip to the server for the focus, and one for each widget it checks for being viewable. A registered window that has been destroyed causes an X error (BadWindow), which is why widgets must be unregistered first.

#include "xtk_focus.h"

Types: XtkFocusSetFocusedCallback, XtkFocusIsEnabledCallback, XtkFocusOwnsWindowCallback, XtkFocusHintCallback, XtkFocusManager

Constants: XTK_FOCUS_MANAGER_MAX_WIDGETS

Functions: xtk_focus_manager_create, xtk_focus_manager_destroy, xtk_focus_manager_register, xtk_focus_manager_register_composite, xtk_focus_manager_unregister, xtk_focus_manager_set_hint, xtk_focus_manager_set_hint_callback, xtk_focus_manager_handle_event, xtk_focus_manager_advance, xtk_focus_manager_owns_window, xtk_focus_manager_focus_first

Types

XtkFocusSetFocusedCallback

typedef void (*XtkFocusSetFocusedCallback)(void *widget, int focused);

Adapter that gives keyboard focus to one widget, or takes it away.

Usually it calls the widget's own *_set_focused() function.

XtkFocusIsEnabledCallback

typedef int (*XtkFocusIsEnabledCallback)(const void *widget);

Adapter that says whether one widget is enabled.

May be NULL at registration for a widget type that cannot be disabled; the widget is then always enabled.

XtkFocusOwnsWindowCallback

typedef int (*XtkFocusOwnsWindowCallback)(void *widget, Window window);

Adapter that says whether a window belongs to a composite widget.

Only for xtk_focus_manager_register_composite(), for a widget whose real focus can be on any of several child windows (for example the text views of a data grid's cells). A widget with one window does not need it.

XtkFocusHintCallback

typedef void (*XtkFocusHintCallback)(const char *hint, void *user_data);

Callback called when the manager moves focus onto a widget.

Called after Ctrl+Tab, Ctrl+Shift+Tab, xtk_focus_manager_advance() or xtk_focus_manager_focus_first() has given focus to a widget. Set it with xtk_focus_manager_set_hint_callback(); a program with nowhere to show hints does not set it.

XtkFocusManager

typedef struct XtkFocusManager XtkFocusManager;

A focus order over a set of widgets; opaque, made by xtk_focus_manager_create().

Constants

XTK_FOCUS_MANAGER_MAX_WIDGETS

#define XTK_FOCUS_MANAGER_MAX_WIDGETS 128

The most widgets one focus manager can hold.

Functions

xtk_focus_manager_create

XtkFocusManager *xtk_focus_manager_create(Display *display);

Make a focus manager for one display connection.

Parameter Description
display The X connection; the manager asks it which window has focus

Returns: The new manager, or NULL if display is NULL or memory ran out

xtk_focus_manager_destroy

void xtk_focus_manager_destroy(XtkFocusManager *manager);

Free a focus manager.

The registered widgets are not touched. Does nothing when manager is NULL.

Parameter Description
manager The manager to free, or NULL

xtk_focus_manager_register

void xtk_focus_manager_register(
    XtkFocusManager *manager,
    void *widget,
    XtkFocusSetFocusedCallback set_focused,
    XtkFocusIsEnabledCallback is_enabled,
    Window window);

Add a widget at the end of the focus order.

Registration order is Tab order; Ctrl+Shift+Tab walks it backward. Does nothing if manager, widget or set_focused is NULL, window is None, or the manager already holds XTK_FOCUS_MANAGER_MAX_WIDGETS widgets.

Parameter Description
manager The focus manager
widget The widget; it is passed back to the adapters unchanged
set_focused Required adapter that gives or takes focus
is_enabled Optional adapter that says whether the widget is enabled; NULL means always enabled
window The widget's window, as its *_get_focus_window() function gives it. The manager compares it with the focus window and checks that it is viewable.

xtk_focus_manager_register_composite

void xtk_focus_manager_register_composite(
    XtkFocusManager *manager,
    void *widget,
    XtkFocusSetFocusedCallback set_focused,
    XtkFocusIsEnabledCallback is_enabled,
    XtkFocusOwnsWindowCallback owns_window,
    Window window);

Add a composite widget, whose focus can be on any of several child windows.

The same as xtk_focus_manager_register(), but the manager asks owns_window whether the focus window belongs to the widget, instead of comparing it with window. Passing NULL for owns_window makes it the same as xtk_focus_manager_register().

Parameter Description
manager The focus manager
widget The widget; it is passed back to the adapters unchanged
set_focused Required adapter that gives or takes focus
is_enabled Optional adapter that says whether the widget is enabled; NULL means always enabled
owns_window Adapter that says whether a window belongs to the widget
window Still required: the widget's outer window, which the manager checks for being viewable

xtk_focus_manager_unregister

void xtk_focus_manager_unregister(XtkFocusManager *manager, void *widget);

Remove a widget from the focus order.

Call it before the widget is destroyed. The widgets after it move up one place. Does nothing if the widget is not registered.

Parameter Description
manager The focus manager
widget The pointer given at registration

xtk_focus_manager_set_hint

void xtk_focus_manager_set_hint(XtkFocusManager *manager, void *widget, const char *hint);

Attach a hint text to a registered widget, or remove it.

The text is given to the hint callback when focus moves onto the widget. Does nothing if the widget is not registered.

Parameter Description
manager The focus manager
widget The pointer given at registration
hint The text, for example "Space: toggle", or NULL to remove the hint. It is not copied: it must stay valid while it is attached (a string literal is the usual choice).

xtk_focus_manager_set_hint_callback

void xtk_focus_manager_set_hint_callback(
    XtkFocusManager *manager,
    XtkFocusHintCallback callback,
    void *user_data);

Set the one callback called when focus moves onto a registered widget.

Everything else in the manager works the same with or without it.

Parameter Description
manager The focus manager
callback The callback, or NULL to stop the calls
user_data Passed to every call of the callback

xtk_focus_manager_handle_event

int xtk_focus_manager_handle_event(XtkFocusManager *manager, const XEvent *event);

Move focus on Ctrl+Tab (forward) or Ctrl+Shift+Tab (backward).

Pass every event, after the widgets have had it. Only a KeyPress of Tab (or ISO_Left_Tab) with Ctrl held is used; plain Tab is never used. The move itself is xtk_focus_manager_advance().

Parameter Description
manager The focus manager
event The X event from the program's event loop

Returns: 1 if the event was Ctrl+Tab or Ctrl+Shift+Tab and the manager holds two widgets or more (focus moved, or no other widget could take it); 0 otherwise, and the program can use the event for something else

xtk_focus_manager_advance

int xtk_focus_manager_advance(XtkFocusManager *manager, int backward);

Move focus to the next (or previous) enabled, viewable widget.

The same move Ctrl+Tab makes, for a program that moves focus on a key of its own, for example plain Tab inside a panel where no widget uses Tab. Such a program usually keeps a second manager for that panel, and checks with xtk_focus_manager_owns_window() that focus is already in it. The walk starts at the widget that has focus (or, when none has, before the first widget, or after the last one going backward) and wraps around at the ends. The widget that had focus is told it lost it, the new one is given focus, and the hint callback is called.

Parameter Description
manager The focus manager
backward Nonzero to move to the previous widget instead of the next

Returns: 1 if the manager holds two widgets or more (focus moved, or no widget was enabled and viewable); 0 if it holds fewer, or manager is NULL

xtk_focus_manager_owns_window

int xtk_focus_manager_owns_window(const XtkFocusManager *manager, Window window);

Whether a window belongs to a registered widget that is enabled and viewable.

Use it before reacting to a key of the program's own (see xtk_focus_manager_advance()), so that the key only moves focus when focus is already in this manager's widgets, and not when a widget elsewhere uses the same key.

Parameter Description
manager The focus manager
window A window to test, usually the xkey.window of a KeyPress event

Returns: 1 if window belongs to a registered widget that is enabled and viewable (through owns_window for a composite widget, by comparison otherwise), 0 otherwise

xtk_focus_manager_focus_first

void xtk_focus_manager_focus_first(XtkFocusManager *manager);

Give focus to the first enabled, viewable widget.

Call it once after the window's widgets are made and registered, so that one has focus from the start. The hint callback is called. Does nothing if no widget is registered, or none is enabled and viewable.

Parameter Description
manager The focus manager