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

xtk_menu.h

A menu bar: a row of titles, each opening a drop-down menu.

An XtkMenuBar is a strip 24 pixels high with menu titles from left to right ("File", "Edit", ...). A click on a title opens its menu below it, an XtkPopupMenu (xtk_popup_menu.h). While a menu is open, moving the pointer over another title opens that one, and Left and Right move to the neighboring menu. Alt with the first letter of a title (underlined) opens that menu from anywhere. On the right, the bar can show a small "pill" with a short message (xtk_menubar_set_rail_label()).

Creating a menu bar. The program describes its menus as an array of XtkMenuBarMenu (a title and the item labels, "---" for a separator) and passes it to xtk_menubar_create() with a MenuBlock for the colors and font (normally &config.menubar) and the geometry (normally from config.menubar_widget). Titles and labels are copied. The bar is mapped at once. All items start enabled. After that, each menu's items are changed through the helpers here (xtk_menubar_set_item_enabled() and so on) or through the menu itself (xtk_menubar_get_menu()): check marks, labels, icons, keyboard hints, flyout submenus. The program frees the bar and its menus with xtk_menubar_destroy().

Events. The program passes every XEvent to xtk_menubar_handle_event() (all KeyPress events too, for the Alt keys) and calls xtk_menubar_update() from a regular timer tick, for flyouts. The return value tells what happened:

  • XTK_MENUBAR_ACTION(menu, item) (0 or more): item item of menu menu was chosen, and the menu has closed.
  • XTK_MENUBAR_CONSUMED: Escape closed the open menu; the key is used up.
  • -1: nothing for the program to do. A click or key outside the bar and its menus also gives -1 after closing the open menu: the program goes on to handle that event as usual.

Items chosen inside a flyout are not reported through the return value; give them callbacks (see xtk_popup_menu_set_item_submenu()).

Theme keys. [menubar] styles the bar and its menus; [menu] (also read as [context-menu]) is the same set of keys for the pop-up menus of other widgets (a text field's context menu, say), which use config.menu.

  • background: the bar (default #2E2E2E) or the menu ([menu]: #363026).
  • color: the text (#EBEBEB; [menu]: #B5965C).
  • border: the frame of a lit title and of a menu, and separator lines (#1F1F1F; [menu]: #8A7D4F).
  • hover-background: a title or item under the pointer (#4D4D4D; [menu]: #454038).
  • hover-color: the text of a lit title or item (unset: color).
  • active-background: the title whose menu is open, and the item whose flyout is open (the same as hover-background by default).
  • hover-border: a line around a menu's highlighted item, when it is a color other than the item's background (by default it is the same, so no line shows).
  • font-family (or font, DejaVu Sans) and font-size (14).
  • rail-background (#2E4C00) and rail-foreground (#81EE00): the pill on the right of the bar.
  • border-width (1), border-radius (4) and padding (8): the frame, corners and text inset of the menus (the bar's own are in [menubar-widget]).

background, hover-background, active-background and rail-background accept a CSS gradient.

[menubar-widget] holds the bar's geometry:

  • width (278): the bar's width. [sizes] menubar-width sets it too.
  • border-width (1) and border-radius (4): the bar's frame, drawn in [menubar] border, and its corners. The first title starts against the frame. padding (4) is still read and written, but the titles do not move for it.

Example.

enum { MENU_FILE, MENU_EDIT };
static const char *const file_items[] = { "New", "Open...", "---", "Quit" };
static const char *const edit_items[] = { "Undo", "Redo" };
static const XtkMenuBarMenu menus[] = {
    { "File", file_items, 4 },
    { "Edit", edit_items, 2 },
};

XtkConfig config;
xtk_config_init_defaults(&config);
XtkTheme *theme = xtk_theme_new();
if (theme && xtk_theme_builtin("Dark", theme)) xtk_theme_apply(&config, theme);
xtk_theme_free(theme);

XtkMenuBar *bar = xtk_menubar_create(display, window, &config.menubar, 0, 0,
    config.menubar_widget.width, config.menubar_widget.border_width,
    config.menubar_widget.border_radius, config.menubar_widget.padding, menus, 2);
xtk_menubar_set_item_enabled(bar, MENU_EDIT, 1, 0);   // nothing to redo yet

while (running) {
    while (XPending(display)) {
        XEvent event;
        XNextEvent(display, &event);
        const int action = xtk_menubar_handle_event(bar, &event);
        if (action == XTK_MENUBAR_ACTION(MENU_FILE, 3)) running = 0;   // Quit
    }
    xtk_menubar_update(bar);
    usleep(10000);
}
xtk_menubar_destroy(bar);
 * 

Limitations.

  • The bar is always 24 pixels high.
  • A menu holds at most 99 items.
#include "xtk_menu.h"

Types: XtkMenuBar, XtkMenuBarMenu, XtkMenuBarOpenCallback

Constants: XTK_MENUBAR_ITEMS_PER_MENU, XTK_MENUBAR_ACTION, XTK_MENUBAR_CONSUMED

Functions: xtk_menubar_create, xtk_menubar_destroy, xtk_menu_config_init_defaults, xtk_menu_config_parse_styling, xtk_menu_config_write, xtk_menu_config_write_styling, xtk_menubar_widget_config_init_defaults, xtk_menubar_widget_config_parse, xtk_menubar_widget_config_write, xtk_menubar_draw, xtk_menubar_reallocate_dbe, xtk_menubar_set_rail_label, xtk_menubar_hide_all_submenus, xtk_menubar_update, xtk_menubar_handle_event, xtk_menubar_get_window, xtk_menubar_get_menu_count, xtk_menubar_get_titles_width, xtk_menubar_get_menu, xtk_menubar_set_item_enabled, xtk_menubar_set_item_label, xtk_menubar_set_item_icon, xtk_menubar_set_item_checked, xtk_menubar_set_item_radio, xtk_menubar_is_menubar_window, xtk_menubar_set_theme, xtk_menubar_set_position, xtk_menubar_resize, xtk_menubar_get_natural_width, xtk_menubar_get_hover, xtk_menubar_keep_menu, xtk_menubar_get_open_menu, xtk_menubar_set_open_callback

Types

XtkMenuBar

typedef struct XtkMenuBar XtkMenuBar;

A menu bar. The structure is private; make one with xtk_menubar_create().

XtkMenuBarMenu

One menu of the bar: its title and its item labels.

Field Type Description
title const char * The title on the bar (at most 31 bytes); its first letter, with Alt, opens the menu.
items const char *const * The item labels ("---" for a separator, at most 63 bytes each), copied at creation.
item_count int The number of items, at most XTK_MENUBAR_ITEMS_PER_MENU - 1 (more are dropped); 0 leaves the menu off the bar.

XtkMenuBarOpenCallback

typedef void (*XtkMenuBarOpenCallback)(XtkMenuBar *menubar, int menu, void *user_data);

Called just before a menu of the bar opens.

Constants

XTK_MENUBAR_ITEMS_PER_MENU

#define XTK_MENUBAR_ITEMS_PER_MENU 100

One more than the most items a menu can hold; keeps every action code unique.

XTK_MENUBAR_ACTION

#define XTK_MENUBAR_ACTION(menu, item) ((menu) * XTK_MENUBAR_ITEMS_PER_MENU + (item))

The code xtk_menubar_handle_event() returns when item item of menu menu (both counted from 0) is chosen.

XTK_MENUBAR_CONSUMED

#define XTK_MENUBAR_CONSUMED (-2)

Returned by xtk_menubar_handle_event() when the event (Escape) only closed the open menu: the program should not act on it.

Functions

xtk_menubar_create

XtkMenuBar *xtk_menubar_create(
    Display *display,
    Window parent,
    const struct MenuBlock *style,
    int x,
    int y,
    int width,
    int border_width,
    int border_radius,
    int padding,
    const XtkMenuBarMenu *menus,
    int menu_count);

Create a menu bar, mapped.

Parameter Description
display The X display.
parent The window the bar sits in; must not be None.
style The colors and font of the bar and its menus, normally &config.menubar; copied.
x The x position in the parent, in pixels.
y The y position in the parent, in pixels.
width The bar's width in pixels; the height is 24.
border_width The width of the bar's frame, in pixels; 0 for none.
border_radius The corner radius of the bar, in pixels.
padding Kept for older callers; it does not move the titles.
menus The menus, left to right; titles and labels are copied.
menu_count The number of menus; may be 0.

Returns: The new menu bar, or NULL if display or style is NULL, parent is None, menu_count is below 0 (or above 0 with menus NULL), or memory runs out.

xtk_menubar_destroy

void xtk_menubar_destroy(XtkMenuBar *menubar);

Destroy a menu bar and its menus.

Parameter Description
menubar The menu bar, or NULL (then nothing happens).

xtk_menu_config_init_defaults

void xtk_menu_config_init_defaults(struct MenuBlock *block, const char *xtk_menu_type);

Fill a MenuBlock with the default colors and font of [menu] or [menubar].

xtk_config_init_defaults() already calls it for config.menu and config.menubar. It leaves hover_foreground unset (which means color), sets hover_border to the hover background (no line) and the menus' geometry to a 1-pixel frame, a 4-pixel radius and 8 pixels of padding.

Parameter Description
block The block to fill; NULL does nothing.
xtk_menu_type "menubar" for the menu bar's darker grey colors; anything else (or NULL) for the [menu] colors.

xtk_menu_config_parse_styling

int xtk_menu_config_parse_styling(
    struct MenuBlock *menu_config,
    const char *key,
    const char *value);

Set one [menu] or [menubar] key in a MenuBlock.

Parameter Description
menu_config The block to change.
key The key name: color, background, border, hover-background, hover-color, hover-border, active-background, rail-background, rail-foreground, border-width, border-radius, padding, font-family (or font) or font-size.
value The value as written in the file.

Returns: 1 if the key is known and was set; 0 if it is unknown or an argument is NULL.

xtk_menu_config_write

void xtk_menu_config_write(
    FILE *file,
    const struct MenuBlock *block,
    const char *xtk_menu_type);

Write a MenuBlock as a section of the given name.

An unset hover-color is written as the color.

Parameter Description
file The file to write to; NULL does nothing.
block The block to write; NULL does nothing.
xtk_menu_type The section name, such as "menu" or "menubar"; NULL does nothing.

xtk_menu_config_write_styling

int xtk_menu_config_write_styling(FILE *file, const struct MenuBlock *menu_config);

Write a MenuBlock as a [menu] section.

Parameter Description
file The file to write to.
menu_config The block to write.

Returns: 1 on success, 0 if an argument is NULL.

xtk_menubar_widget_config_init_defaults

void xtk_menubar_widget_config_init_defaults(XtkConfig *config);

Set config.menubar_widget to the default [menubar-widget] values.

Parameter Description
config The configuration; NULL does nothing.

xtk_menubar_widget_config_parse

int xtk_menubar_widget_config_parse(
    XtkConfig *config,
    const char *key,
    const char *value);

Set one [menubar-widget] key in config.menubar_widget.

Parameter Description
config The configuration.
key The key name: width, border-width, border-radius or padding.
value The value as written in the file.

Returns: 1 if the key is known and was set, 0 if the key is unknown or an argument is NULL (the convention of every section handler).

xtk_menubar_widget_config_write

int xtk_menubar_widget_config_write(FILE *file, const XtkConfig *config);

Write config.menubar_widget as a [menubar-widget] section.

Parameter Description
file The file to write to.
config The configuration.

Returns: 0 on success, -1 if an argument is NULL.

xtk_menubar_draw

void xtk_menubar_draw(XtkMenuBar *menubar);

Redraw the menu bar now.

Parameter Description
menubar The menu bar, or NULL.

xtk_menubar_reallocate_dbe

void xtk_menubar_reallocate_dbe(XtkMenuBar *menubar);

Make the bar's back buffer again from nothing, and redraw.

Only for repairing a back buffer that stopped showing after window-manager dialogs opened over it; a normal redraw is xtk_menubar_draw().

Parameter Description
menubar The menu bar, or NULL.

xtk_menubar_set_rail_label

void xtk_menubar_set_rail_label(XtkMenuBar *menubar, const char *text);

Show or remove the pill at the right of the bar, and redraw.

The pill shows a short message (such as "Copied") in rail-foreground on rail-background, 8 pixels from the right edge. It is not clickable.

Parameter Description
menubar The menu bar.
text The text, copied (at most 63 bytes), or NULL or "" to remove the pill.

xtk_menubar_hide_all_submenus

void xtk_menubar_hide_all_submenus(XtkMenuBar *menubar);

Close the open menu (also one kept open), and redraw.

Parameter Description
menubar The menu bar, or NULL.

xtk_menubar_update

void xtk_menubar_update(XtkMenuBar *menubar);

Open a flyout of the open menu whose delay has passed.

Call it from a regular timer tick (see xtk_popup_menu_update()). It does nothing while no menu is open.

Parameter Description
menubar The menu bar, or NULL.

xtk_menubar_handle_event

int xtk_menubar_handle_event(XtkMenuBar *menubar, XEvent *event);

Handle an X event for the bar or its open menu.

Pass it every event. See the file description for what it does.

Note: Unlike most xtk handlers, the return value is not 0, 1 or 2: it says which item was chosen.

Parameter Description
menubar The menu bar; NULL returns -1.
event The event.

Returns: XTK_MENUBAR_ACTION(menu, item) when an item is chosen; XTK_MENUBAR_CONSUMED when Escape only closed the open menu; -1 otherwise.

xtk_menubar_get_window

Window xtk_menubar_get_window(const XtkMenuBar *menubar);

The menu bar's X window.

Parameter Description
menubar The menu bar.

Returns: The window, or None if menubar is NULL.

xtk_menubar_get_menu_count

int xtk_menubar_get_menu_count(const XtkMenuBar *menubar);

The number of menus given at creation.

Parameter Description
menubar The menu bar.

Returns: The number of menus (also those with no items), or 0 if menubar is NULL.

xtk_menubar_get_titles_width

int xtk_menubar_get_titles_width(const XtkMenuBar *menubar);

Where the last title ends.

A program can put something of its own after the titles, such as a toolbar in the menu row.

Parameter Description
menubar The menu bar.

Returns: The distance in pixels from the bar's left edge to the right end of the last title, or 0 if there are none or menubar is NULL.

xtk_menubar_get_menu

XtkPopupMenu *xtk_menubar_get_menu(const XtkMenuBar *menubar, int menu);

One menu's XtkPopupMenu, to change its items.

The menu bar owns it: it stays valid until xtk_menubar_destroy(). What is set on its items (enabled, checked, labels, icons, flyouts) stays through xtk_menubar_set_theme().

Parameter Description
menubar The menu bar.
menu The menu's index, 0 for the leftmost.

Returns: The menu, or NULL if menu is out of range or the menu has no items.

xtk_menubar_set_item_enabled

void xtk_menubar_set_item_enabled(XtkMenuBar *menubar, int menu, int item, int enabled);

Enable or disable one item of a menu.

Indexes out of range are ignored.

Parameter Description
menubar The menu bar.
menu The menu's index, 0 for the leftmost.
item The item's index in the menu, 0 for the first.
enabled Nonzero to enable, 0 to disable.

xtk_menubar_set_item_label

void xtk_menubar_set_item_label(
    XtkMenuBar *menubar,
    int menu,
    int item,
    const char *label);

Change the label of one item of a menu.

Parameter Description
menubar The menu bar.
menu The menu's index, 0 for the leftmost.
item The item's index in the menu, 0 for the first.
label The new label, copied (at most 63 bytes).

xtk_menubar_set_item_icon

void xtk_menubar_set_item_icon(
    XtkMenuBar *menubar,
    int menu,
    int item,
    const char *icon_key);

Set or clear the icon drawn before one item of a menu.

Parameter Description
menubar The menu bar.
menu The menu's index, 0 for the leftmost.
item The item's index in the menu, 0 for the first.
icon_key An xtk_icon.h key, or NULL for none.

xtk_menubar_set_item_checked

void xtk_menubar_set_item_checked(XtkMenuBar *menubar, int menu, int item, int checked);

Make one item of a menu checkable and set its check mark.

Choosing it is reported as for any item, and the menu closes; the program calls this again with the new state.

Parameter Description
menubar The menu bar.
menu The menu's index, 0 for the leftmost.
item The item's index in the menu, 0 for the first.
checked Nonzero to show the mark, 0 for an empty box.

xtk_menubar_set_item_radio

void xtk_menubar_set_item_radio(XtkMenuBar *menubar, int menu, int item, int is_radio);

Draw one checkable item of a menu as a radio item or a check item.

Parameter Description
menubar The menu bar.
menu The menu's index, 0 for the leftmost.
item The item's index in the menu, 0 for the first.
is_radio Nonzero for a radio circle, 0 for a check box.

xtk_menubar_is_menubar_window

int xtk_menubar_is_menubar_window(const XtkMenuBar *menubar, Window window);

Whether a window belongs to the menu bar.

Parameter Description
menubar The menu bar.
window The window to test.

Returns: 1 if it is the bar's window or the window of one of its open menus (not of a flyout), 0 otherwise.

xtk_menubar_set_theme

void xtk_menubar_set_theme(XtkMenuBar *menubar, const XtkConfig *theme);

Take a new theme for the bar and its menus, and redraw.

It reads theme->menubar and theme->menubar_widget (the frame, radius and padding); the bar keeps its width. An open menu closes (a menu kept with xtk_menubar_keep_menu() opens again).

Parameter Description
menubar The menu bar.
theme The configuration. NULL does nothing.

xtk_menubar_set_position

void xtk_menubar_set_position(XtkMenuBar *menubar, int x, int y);

Move the menu bar within its parent.

Parameter Description
menubar The menu bar.
x The new x position in the parent, in pixels.
y The new y position in the parent, in pixels.

xtk_menubar_resize

void xtk_menubar_resize(XtkMenuBar *menubar, int width);

Change the bar's width, and redraw.

Parameter Description
menubar The menu bar.
width The new width in pixels; 0 or less is ignored.

xtk_menubar_get_natural_width

int xtk_menubar_get_natural_width(const XtkMenuBar *menubar);

The width the titles, the border and the pill need.

For a bar only as wide as its menus. It changes with the font of the theme.

Parameter Description
menubar The menu bar.

Returns: The width in pixels, or 0 if menubar is NULL.

xtk_menubar_get_hover

int xtk_menubar_get_hover(const XtkMenuBar *menubar);

The title under the pointer.

Parameter Description
menubar The menu bar.

Returns: The menu's index, or -1 if no title is lit or menubar is NULL.

xtk_menubar_keep_menu

void xtk_menubar_keep_menu(XtkMenuBar *menubar, int menu);

Open a menu and keep it open, or close the kept one.

For showing the bar with a menu open, without a pointer. The menu opens without grabbing the pointer or the keyboard. While it is kept, clicks and keys elsewhere leave it open, the pointer over other titles only lights them, and a new theme keeps it open.

Parameter Description
menubar The menu bar.
menu The menu's index, or -1 to close the kept menu.

xtk_menubar_get_open_menu

XtkPopupMenu *xtk_menubar_get_open_menu(const XtkMenuBar *menubar);

The menu that is open.

Parameter Description
menubar The menu bar.

Returns: The open menu (owned by the menu bar), or NULL if none is open.

xtk_menubar_set_open_callback

void xtk_menubar_set_open_callback(
    XtkMenuBar *menubar,
    XtkMenuBarOpenCallback callback,
    void *user_data);

Set or remove the function called just before each menu opens.

It runs before the menu is shown, so it can change that menu's items (a list of recent files read again, say), and the menu opens with them and sizes itself to them. It runs however the menu opens: a click, the Alt key, a move from one open menu to the next, and xtk_menubar_keep_menu().

Parameter Description
menubar The menu bar.
callback The function, or NULL for none.
user_data A pointer passed to the callback unchanged.