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

xtk_popup_menu.h

A pop-up menu of items: context menus, drop-down menus and flyouts.

An XtkPopupMenu is a list of items shown in its own window on top of everything, at a point on the screen. An item has a label and can be disabled, a separator line, checkable (a check box or radio glyph), carry an icon and a keyboard hint ("Ctrl+S"), or open a flyout submenu to the right when the pointer rests on it. Flyouts can nest to any depth. The menu bar (xtk_menu.h) uses one XtkPopupMenu per title; other widgets use one for their context menu.

Creating a menu. xtk_popup_menu_create() takes a MenuBlock (normally &config.menu) and an array of XtkPopupMenuItem; both are copied. The menu is not shown yet. Extras that the item struct has no field for are set by index afterwards: xtk_popup_menu_set_item_submenu(), xtk_popup_menu_set_item_accelerator(), xtk_popup_menu_set_item_icon(), xtk_popup_menu_set_item_checked(). The program frees the menu with xtk_popup_menu_destroy().

Showing it. xtk_popup_menu_show() opens the menu with its top-left corner at a screen position (moved so that it stays on the screen) and grabs the pointer and the keyboard. The menu closes when an item is chosen, on a click outside it, on Escape, and when it loses the focus. xtk_popup_menu_hide() closes it from code. The window is made at each show and destroyed at each hide.

Events. While the menu is open, the program passes every XEvent to xtk_popup_menu_handle_event(); because of the grab, the pointer and key events all come to the menu's window. The program also calls xtk_popup_menu_update() from a regular timer tick: a flyout opens when the pointer has rested 225 ms on its row. The keys are:

  • Up and Down move the highlight over the enabled items, wrapping around.
  • Return or keypad Enter chooses the highlighted item; on a row with a flyout, it opens the flyout instead, as Right does.
  • Right opens the highlighted row's flyout with its first item highlighted; the keys then work in the flyout. Left or Escape closes the flyout and goes back to the row that opened it.
  • A letter chooses the first enabled item whose label starts with it (the first letter of every label is underlined for this); on a row with a flyout, it opens the flyout.
  • Escape closes the menu.

Choosing an item (Button1 released over it, Return, or its letter) calls its callback and, for the top menu, returns its index. A plain item closes the whole menu (and all flyouts) before its callback runs, so the callback can open a dialog or destroy the menu. A checkable item keeps the menu open: the menu does not change the check mark itself; the callback (or the code acting on the index) calls xtk_popup_menu_set_item_checked() with the new state. A row with a flyout cannot be chosen, with the mouse or the keys; it only opens the flyout. Items chosen inside a flyout are reported only through their callbacks: xtk_popup_menu_handle_event() returns -1 for them.

Theme keys. The menu reads only the MenuBlock it is given; it has no config section of its own. Menus made from &config.menu take their colors from [menu] (also read as [context-menu]); the menu bar's menus use [menubar] (see xtk_menu.h for both sections and their defaults). Of the MenuBlock, the menu uses:

  • background: the menu.
  • border: the frame (when border_width is above 0) and the separator lines, also the glyphs of disabled items.
  • color: the labels. Disabled labels and keyboard hints are drawn half way between color and background.
  • hover-background and hover-color: the highlighted item (an unset, fully transparent hover-color means color).
  • hover-border: a line around the highlighted item, drawn when it is a color other than the item's background.
  • active-background: the row whose flyout is open.
  • font-family and font-size.
  • border-width (1), border-radius (4) and padding (8, the space between the frame and the text).

background, hover-background and active-background accept a CSS gradient in the section named by xtk_popup_menu_set_section().

Example.

static void on_item(XtkPopupMenu *menu, int item_index, const char *item_label, void *user_data) {
    (void)menu;
    (void)user_data;
    printf("Chose %d: %s\n", item_index, item_label);
}

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);

static const XtkPopupMenuItem items[] = {
    { "Cut",   XTK_MENU_ITEM_ENABLED,   on_item, NULL },
    { "Copy",  XTK_MENU_ITEM_ENABLED,   on_item, NULL },
    { "",      XTK_MENU_ITEM_SEPARATOR, NULL,    NULL },
    { "Paste", XTK_MENU_ITEM_ENABLED,   on_item, NULL },
};
XtkPopupMenu *menu = xtk_popup_menu_create(display, DefaultScreen(display), &config.menu, items, 4);
xtk_popup_menu_set_item_accelerator(menu, 0, "Ctrl+X");

XEvent event;
while (running) {
    XNextEvent(display, &event);
    if (xtk_popup_menu_is_visible(menu)) {
        xtk_popup_menu_handle_event(menu, &event);   // the callback reports the choice
        continue;
    }
    if (event.type == ButtonPress && event.xbutton.button == Button3)
        xtk_popup_menu_show(menu, event.xbutton.x_root, event.xbutton.y_root);
}
xtk_popup_menu_destroy(menu);
 * 

Limitations.

  • Labels hold 63 bytes, keyboard hints and icon keys 31.
#include "xtk_popup_menu.h"

Types: XtkPopupMenu, XtkMenuItemCallback, XtkPopupMenuItem, XtkPopupMenuSubmenuSpec

Constants: XTK_MENU_ITEM_ENABLED, XTK_MENU_ITEM_SEPARATOR, XTK_MENU_ITEM_CHECKABLE, XTK_MENU_ITEM_CHECKED, XTK_MENU_ITEM_RADIO, XTK_MENU_ITEM_SUBMENU

Functions: xtk_popup_menu_set_item_submenu, xtk_popup_menu_set_item_submenu_icons, xtk_popup_menu_set_item_accelerator, xtk_popup_menu_set_item_icon, xtk_popup_menu_set_item_submenu_tree, xtk_popup_menu_create, xtk_popup_menu_destroy, xtk_popup_menu_set_min_width, xtk_popup_menu_set_section, xtk_popup_menu_show, xtk_popup_menu_hide, xtk_popup_menu_is_visible, xtk_popup_menu_get_window, xtk_popup_menu_get_size, xtk_popup_menu_set_item_enabled, xtk_popup_menu_get_item_flags, xtk_popup_menu_set_item_label, xtk_popup_menu_set_item_checked, xtk_popup_menu_set_item_checkable, xtk_popup_menu_set_item_radio, xtk_popup_menu_set_items, xtk_popup_menu_handle_event, xtk_popup_menu_update, xtk_popup_menu_draw, xtk_popup_menu_set_theme, xtk_popup_menu_set_hover, xtk_popup_menu_keep_submenu, xtk_popup_menu_get_submenu, xtk_popup_menu_get_hover

Types

XtkPopupMenu

typedef struct XtkPopupMenu XtkPopupMenu;

A pop-up menu. The structure is private; make one with xtk_popup_menu_create().

XtkMenuItemCallback

typedef void (*XtkMenuItemCallback)(XtkPopupMenu *menu, int item_index, const char *item_label, void *user_data);

Called when an item is chosen.

For a plain item the menu (and the whole flyout chain) is already hidden, and the callback may destroy it or run a dialog of its own. For a checkable item the menu stays open and is redrawn after the callback, so the callback may change items (its check mark, say) but must not destroy the menu.

XtkPopupMenuItem

One menu item: its label, XTK_MENU_ITEM_* flags and callback.

Field Type Description
label[64] char The label; its first letter chooses the item from the keyboard.
flags int XTK_MENU_ITEM_* flags, ORed together.
callback XtkMenuItemCallback Called when the item is chosen, or NULL.
user_data void * Passed to callback unchanged.

XtkPopupMenuSubmenuSpec

A flyout to attach to one row of a menu, with the flyouts inside it.

Used with xtk_popup_menu_set_item_submenu_tree() to describe nested flyouts (File, Open, Import, CSV) in static data. Each level is attached when the flyout above it opens. All the arrays are kept as pointers only, so they must stay valid as long as the menu exists.

Field Type Description
row int The row, in the items this spec belongs to, that the flyout attaches to.
items const XtkPopupMenuItem * The flyout's items.
item_count int The number of items in items
nested const struct XtkPopupMenuSubmenuSpec * Flyouts to attach to rows of items, or NULL.
nested_count int The number of specs in nested

Constants

XTK_MENU_ITEM_ENABLED

#define XTK_MENU_ITEM_ENABLED (1 << 0)

Item flag: the item can be chosen; without it the item is grey.

XTK_MENU_ITEM_SEPARATOR

#define XTK_MENU_ITEM_SEPARATOR (1 << 1)

Item flag: the item is a separator line, not a choice.

XTK_MENU_ITEM_CHECKABLE

#define XTK_MENU_ITEM_CHECKABLE (1 << 2)

Item flag: the item has a check box (or radio) glyph before its label; choosing it keeps the menu open

XTK_MENU_ITEM_CHECKED

#define XTK_MENU_ITEM_CHECKED (1 << 3)

Item flag: the glyph shows checked; only with XTK_MENU_ITEM_CHECKABLE

XTK_MENU_ITEM_RADIO

#define XTK_MENU_ITEM_RADIO (1 << 5)

Item flag: the glyph is a circle instead of a square; only with XTK_MENU_ITEM_CHECKABLE (see xtk_popup_menu_set_item_radio())

XTK_MENU_ITEM_SUBMENU

#define XTK_MENU_ITEM_SUBMENU (1 << 4)

Item flag: the item opens a flyout submenu.

Do not set it by hand: xtk_popup_menu_set_item_submenu() or xtk_popup_menu_set_item_submenu_tree() sets it when it attaches the flyout's items. The row shows a small arrow at its right; resting the pointer on it opens the flyout to the right of the menu (moved to stay on the screen). The item's own callback is not used: the items in the flyout have their own.

Functions

xtk_popup_menu_set_item_submenu

void xtk_popup_menu_set_item_submenu(
    XtkPopupMenu *menu,
    int index,
    const XtkPopupMenuItem *submenu_items,
    int submenu_item_count);

Attach a flyout submenu to an item.

It sets XTK_MENU_ITEM_SUBMENU on the item. The flyout is created (as an XtkPopupMenu with this menu's style) each time it opens, and destroyed when it closes.

Parameter Description
menu The menu; the item must already exist in it.
index The item's index.
submenu_items The flyout's items. Only the pointer is kept, so the array must stay valid as long as menu exists (a static array does).
submenu_item_count The number of items in submenu_items; 0 or less, or a NULL array, does nothing.

xtk_popup_menu_set_item_submenu_icons

void xtk_popup_menu_set_item_submenu_icons(
    XtkPopupMenu *menu,
    int index,
    const char *const *icon_keys);

Give the items of an item's flyout their icons.

A flyout's items exist only while it is open, so their icons are given here as a table that is applied each time the flyout opens.

Parameter Description
menu The menu; the item at index must have a flyout attached.
index The item's index.
icon_keys One xtk_icon.h key per item of the flyout, in the same order (a NULL entry for no icon), or NULL for none. Only the pointer is kept, so the array must stay valid as long as menu exists.

xtk_popup_menu_set_item_accelerator

void xtk_popup_menu_set_item_accelerator(
    XtkPopupMenu *menu,
    int index,
    const char *accelerator_text);

Set or clear an item's keyboard hint, such as "Ctrl+S".

The hint is drawn at the right of the row, in the disabled-label color. It is text only: the program binds the key itself.

Parameter Description
menu The menu; the item must already exist in it.
index The item's index.
accelerator_text The hint, copied (at most 31 bytes), or NULL or "" for none.

xtk_popup_menu_set_item_icon

void xtk_popup_menu_set_item_icon(XtkPopupMenu *menu, int index, const char *icon_key);

Set or clear an item's icon, drawn 16 pixels square before the label in the label's color.

Parameter Description
menu The menu; the item must already exist in it.
index The item's index.
icon_key An xtk_icon.h key (see xtk_icon_draw()), copied (at most 31 bytes), or NULL or "" for none.

xtk_popup_menu_set_item_submenu_tree

void xtk_popup_menu_set_item_submenu_tree(
    XtkPopupMenu *menu,
    int index,
    const XtkPopupMenuItem *submenu_items,
    int submenu_item_count,
    const XtkPopupMenuSubmenuSpec *nested_specs,
    int nested_spec_count);

Attach a flyout to an item, with flyouts inside it to any depth.

Like xtk_popup_menu_set_item_submenu(), and nested_specs describes the flyouts to attach to rows of submenu_items when that flyout opens; each spec can carry its own nested specs. For example, File > Open > Import > CSV is one call on the File menu for the Open row, with one nested spec for the Import row of Open's items.

Parameter Description
menu The menu; the item must already exist in it.
index The item's index.
submenu_items The flyout's items (kept as a pointer, see xtk_popup_menu_set_item_submenu()).
submenu_item_count The number of items in submenu_items.
nested_specs The flyouts to attach inside it, or NULL for none.
nested_spec_count The number of specs in nested_specs.

xtk_popup_menu_create

XtkPopupMenu *xtk_popup_menu_create(
    Display *display,
    int screen,
    const MenuBlock *style,
    const XtkPopupMenuItem *items,
    int item_count);

Create a menu, not shown yet.

Parameter Description
display The X display.
screen The screen number.
style The colors, font, border width and radius, normally &config.menu; copied.
items The items; copied, so the array may be freed afterwards.
item_count The number of items.

Returns: The new menu, or NULL if an argument is NULL, item_count is 0 or less, memory runs out, or the font or a color cannot be had.

xtk_popup_menu_destroy

void xtk_popup_menu_destroy(XtkPopupMenu *menu);

Destroy a menu: hide it (closing its flyouts) and free it.

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

xtk_popup_menu_set_min_width

void xtk_popup_menu_set_min_width(XtkPopupMenu *menu, int min_width);

Set the least width of the menu.

The width is worked out from the items at every show and is never less than this; the menu bar uses it to make a menu at least as wide as its title.

Parameter Description
menu The menu.
min_width The least width in pixels, or 0 for the built-in 120.

xtk_popup_menu_set_section

void xtk_popup_menu_set_section(XtkPopupMenu *menu, const char *section);

Name the theme section whose gradients the menu draws.

If that section gives background, hover-background or active-background a gradient (xtk_paint.h), the menu draws it. Flyouts take the section of their parent.

Parameter Description
menu The menu.
section The section name, such as "menu" (the default), "menubar" or "tray-menu"; at most 23 bytes.

xtk_popup_menu_show

void xtk_popup_menu_show(XtkPopupMenu *menu, int x, int y);

Show the menu at a screen position, and grab the pointer and the keyboard.

Nothing happens if it is already shown. If a mouse button is still held (the menu opened on a press), its release is ignored, so it does not choose the item under the pointer.

Parameter Description
menu The menu.
x The screen x of the menu's top-left corner.
y The screen y of the menu's top-left corner.

xtk_popup_menu_hide

void xtk_popup_menu_hide(XtkPopupMenu *menu);

Hide the menu: close its flyouts, release the grabs it took and destroy its window.

Parameter Description
menu The menu, or NULL.

xtk_popup_menu_is_visible

int xtk_popup_menu_is_visible(const XtkPopupMenu *menu);

Whether the menu is shown.

Parameter Description
menu The menu.

Returns: 1 if shown, 0 if hidden or if menu is NULL.

xtk_popup_menu_get_window

Window xtk_popup_menu_get_window(const XtkPopupMenu *menu);

The menu's window.

Parameter Description
menu The menu.

Returns: The window while the menu is shown, or None.

xtk_popup_menu_get_size

void xtk_popup_menu_get_size(const XtkPopupMenu *menu, int *width, int *height);

The menu's size.

It is worked out at creation and after every change of items or theme, so it is valid before the first show: for placing the menu by its size (above or to the left of a point, say).

Parameter Description
menu The menu; if NULL, both outputs are set to 0.
width Receives the width in pixels; may be NULL.
height Receives the height in pixels; may be NULL.

xtk_popup_menu_set_item_enabled

void xtk_popup_menu_set_item_enabled(XtkPopupMenu *menu, int index, int enabled);

Enable or disable an item, and redraw if shown.

A disabled item is grey and cannot be chosen or highlighted.

Parameter Description
menu The menu.
index The item's index; outside the list, nothing happens.
enabled Nonzero to enable, 0 to disable.

xtk_popup_menu_get_item_flags

int xtk_popup_menu_get_item_flags(const XtkPopupMenu *menu, int index);

An item's flags.

For example, to save the enabled and checked state of the items before replacing the list, and to set it again afterwards.

Parameter Description
menu The menu.
index The item's index.

Returns: The item's XTK_MENU_ITEM_* flags ORed together, or 0 if menu is NULL or index is outside the list.

xtk_popup_menu_set_item_label

void xtk_popup_menu_set_item_label(XtkPopupMenu *menu, int index, const char *label);

Change an item's label, and redraw if shown.

For a label that follows the program's state ("Show Grid" or "Hide Grid") without replacing the whole list.

Parameter Description
menu The menu.
index The item's index.
label The new label, copied (at most 63 bytes); NULL does nothing.

xtk_popup_menu_set_item_checked

void xtk_popup_menu_set_item_checked(XtkPopupMenu *menu, int index, int checked);

Make an item checkable and set its check mark, and redraw if shown.

Choosing a checkable item calls its callback and returns its index as usual and keeps the menu open, but does not change the mark: the program calls this with the new state. xtk_popup_menu_set_item_checkable() makes the item plain again.

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

xtk_popup_menu_set_item_checkable

void xtk_popup_menu_set_item_checkable(XtkPopupMenu *menu, int index, int checkable);

Make an item checkable, or plain again, and redraw if shown.

Parameter Description
menu The menu.
index The item's index.
checkable Nonzero to give it a check box glyph; 0 for a plain item, which also clears its checked and radio flags.

xtk_popup_menu_set_item_radio

void xtk_popup_menu_set_item_radio(XtkPopupMenu *menu, int index, int is_radio);

Draw a checkable item's glyph as a radio circle or a check box square, and redraw if shown.

Only the look changes: the program checks one item of a group and unchecks the others itself. Use it for items that are one choice out of several. It shows only on a checkable item (see xtk_popup_menu_set_item_checked(), in either order).

Parameter Description
menu The menu.
index The item's index.
is_radio Nonzero for a circle, 0 for a square (the default).

xtk_popup_menu_set_items

void xtk_popup_menu_set_items(
    XtkPopupMenu *menu,
    const XtkPopupMenuItem *items,
    int item_count);

Replace all items.

A shown menu is hidden first. The items are copied. Flyouts, keyboard hints and icons set before are dropped.

Parameter Description
menu The menu.
items The new items; NULL does nothing.
item_count The number of items; 0 or less does nothing.

xtk_popup_menu_handle_event

int xtk_popup_menu_handle_event(XtkPopupMenu *menu, XEvent *event);

Handle an X event while the menu is shown.

Pass it every event while xtk_popup_menu_is_visible() is 1. It moves the highlight, opens and closes flyouts, closes the menu on a click outside, Escape, a real FocusOut or an UnmapNotify, and chooses items (see the file description). The item's callback has run before this returns.

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

Parameter Description
menu The menu; NULL or a hidden menu returns -1.
event The event.

Returns: The index of the item of this menu that was chosen, or -1 for every other event, including a choice made in a flyout.

xtk_popup_menu_update

void xtk_popup_menu_update(XtkPopupMenu *menu);

Open a flyout whose delay has passed.

Call it from a regular timer tick on the menu that is shown. When the pointer has rested 225 ms on a row with a flyout, this opens it; a pointer at rest sends no events, so the menu needs this tick. It goes on into open flyouts. It does nothing if menu is NULL or hidden, or nothing is waiting.

Parameter Description
menu The menu.

xtk_popup_menu_draw

void xtk_popup_menu_draw(XtkPopupMenu *menu);

Redraw the menu now, if it is shown.

Parameter Description
menu The menu, or NULL.

xtk_popup_menu_set_theme

void xtk_popup_menu_set_theme(XtkPopupMenu *menu, const MenuBlock *style);

Give the menu new colors, font and border.

A shown menu stays shown, resized for the new style; an open flyout closes (a flyout kept with xtk_popup_menu_keep_submenu() opens again at once). If the new font cannot be opened, the old one stays.

Parameter Description
menu The menu.
style The new style; copied. NULL does nothing.

xtk_popup_menu_set_hover

void xtk_popup_menu_set_hover(XtkPopupMenu *menu, int index);

Show an item highlighted, as if the pointer were on it.

For a program that shows the menu's hover look without a pointer; moving the pointer over the menu highlights as usual.

Parameter Description
menu The menu.
index The item, or -1 for none. A separator or disabled item means none.

xtk_popup_menu_keep_submenu

void xtk_popup_menu_keep_submenu(XtkPopupMenu *menu, int index);

Open an item's flyout and keep it open, or close it.

For a program that shows a menu with its flyout open, without a pointer. While kept, moving the pointer over other items leaves the flyout open, and a new theme opens it again. Hiding the menu ends it.

Parameter Description
menu A shown menu.
index An item with a flyout, or -1 to close the kept flyout.

xtk_popup_menu_get_submenu

XtkPopupMenu *xtk_popup_menu_get_submenu(const XtkPopupMenu *menu);

The open flyout.

Parameter Description
menu The menu.

Returns: The flyout that is open, or NULL if none is. It belongs to menu: never destroy it.

xtk_popup_menu_get_hover

int xtk_popup_menu_get_hover(const XtkPopupMenu *menu);

The highlighted item.

Parameter Description
menu The menu.

Returns: The highlighted item's index, or -1 if none is or menu is NULL.