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

xtk_icongrid.h

A search box over a scrolling grid of icons, for picking one.

An icon grid lets the user pick an icon from the catalog of xtk_icon.h (the SVG icons in the icon folder; see xtk_icon_catalog_count()). At the top is a search box (an xtk_entry); below it is a grid of the icons whose key or tags contain the search text, with a scrollbar on the right when they do not all fit. Only the rows in view are drawn, so a catalog of thousands of icons costs no more to draw than a few.

It is a plain widget, not a dialog: the program puts it in a dialog of its own, with its own title and buttons, and reads the result with xtk_icongrid_get_selected_key(). The grid shows no name for the icons; a program that wants one shows xtk_icongrid_get_hovered_key() or the selected key where it likes, for example in its dialog's title.

Making one. xtk_icongrid_create() takes the parent window, the XtkConfig to read its colors from, an XtkClipboard for the search box, the box the whole widget fills in the parent, and the size of the icons. The search box takes the top 26 pixels of the box; the grid starts 14 pixels below it and fills the rest. The colors are copied, so the XtkConfig need not outlive the call. Call xtk_icongrid_show() to map the windows, and xtk_icongrid_destroy() to free it.

Events. Pass every X event to xtk_icongrid_handle_event(). Events for the search box go to it; when its text changes the grid is filtered again and scrolled to the top. On the grid:

  • Moving the pointer over an icon gives it the hover color.
  • A left click on an icon selects it. There is no callback: read the selection with xtk_icongrid_get_selected_key() when the program needs it (for example when its OK button is pressed).
  • The wheel scrolls by 40 pixels. On the scrollbar, the thumb can be dragged, and a click above or below it scrolls by a page.

Theme keys. The grid reads background, foreground (the icons), selected-background and hover-background of [treeview], and scrollbar-track and scrollbar-thumb of [toolwindow] (the thumb is drawn a little lighter while it is hovered or dragged). The search box is an xtk_entry of the kind ENTRY_TEXT and reads [entry-text]. The grid has no section of its own.

Example.

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

XtkIconGrid *grid = xtk_icongrid_create(display, screen, dialog_window, &config, clipboard,
                                        10, 10, 400, 300, 24);
xtk_icongrid_set_selected_key(grid, "folder");
xtk_icongrid_show(grid);

XEvent event;
for (;;) {
    XNextEvent(display, &event);
    xtk_icongrid_handle_event(grid, &event);
    // When the dialog's OK button is pressed:
    //     const char *key = xtk_icongrid_get_selected_key(grid);
}
 * 

Limitations.

  • There is no keyboard navigation in the grid, and a double-click does nothing more than a click.
  • The widget cannot be moved or resized after it is created.
  • The search box holds at most 64 characters.
#include "xtk_icongrid.h"

Types: XtkIconGrid

Functions: xtk_icongrid_create, xtk_icongrid_destroy, xtk_icongrid_show, xtk_icongrid_get_window, xtk_icongrid_set_selected_key, xtk_icongrid_get_selected_key, xtk_icongrid_get_hovered_key, xtk_icongrid_handle_event, xtk_icongrid_set_theme

Types

XtkIconGrid

typedef struct XtkIconGrid XtkIconGrid;

A searchable grid of icons (opaque). Made by xtk_icongrid_create().

Functions

xtk_icongrid_create

XtkIconGrid *xtk_icongrid_create(
    Display *display,
    int screen,
    Window parent,
    const XtkConfig *theme,
    XtkClipboard *clipboard,
    int x,
    int y,
    int width,
    int height,
    int cell_size);

Creates an icon grid with its search box.

The windows are not mapped; call xtk_icongrid_show(). At first the search box is empty, every catalog icon is shown, and nothing is selected.

Parameter Description
display The X display.
screen The screen number.
parent The parent window of both the search box and the grid.
theme The configuration the colors are read from (see the theme keys at the top of this page). It must not be NULL. It is not kept.
clipboard The clipboard the search box uses for copy and paste.
x The x position of the widget in parent.
y The y position of the widget in parent.
width The width of the widget in pixels.
height The height of the widget in pixels, the search box included. It must be more than 40.
cell_size The size of each icon in pixels; each cell is 12 pixels larger. A value of 0 or less means 16.

Returns: The new icon grid, or NULL when display is NULL or memory runs out.

xtk_icongrid_destroy

void xtk_icongrid_destroy(XtkIconGrid *icongrid);

Destroys the icon grid, its search box and its windows.

Parameter Description
icongrid The icon grid. NULL is allowed and does nothing.

xtk_icongrid_show

void xtk_icongrid_show(XtkIconGrid *icongrid);

Maps the search box and the grid window.

Parameter Description
icongrid The icon grid.

xtk_icongrid_get_window

Window xtk_icongrid_get_window(const XtkIconGrid *icongrid);

Gives the grid's window.

This is the window of the icons and the scrollbar; the search box has a window of its own. Events for both go through xtk_icongrid_handle_event().

Parameter Description
icongrid The icon grid, or NULL.

Returns: The grid window, or None when icongrid is NULL.

xtk_icongrid_set_selected_key

void xtk_icongrid_set_selected_key(XtkIconGrid *icongrid, const char *key);

Selects an icon by its key and scrolls it into view.

Use it to show the current icon when the grid opens.

Parameter Description
icongrid The icon grid.
key A catalog key (see xtk_icon_catalog_key()). When it is NULL, not in the catalog, or not among the icons the search shows, nothing changes.

xtk_icongrid_get_selected_key

const char *xtk_icongrid_get_selected_key(const XtkIconGrid *icongrid);

Gives the key of the selected icon.

Parameter Description
icongrid The icon grid, or NULL.

Returns: The key, or NULL when nothing is selected. The string belongs to the icon catalog and stays valid while the program runs; do not free it.

xtk_icongrid_get_hovered_key

const char *xtk_icongrid_get_hovered_key(const XtkIconGrid *icongrid);

Gives the key of the icon under the pointer.

Parameter Description
icongrid The icon grid, or NULL.

Returns: The key, or NULL when the pointer is over no icon. The string belongs to the icon catalog and stays valid while the program runs; do not free it.

xtk_icongrid_handle_event

int xtk_icongrid_handle_event(XtkIconGrid *icongrid, XEvent *event);

Handles one X event.

Pass every event of the program's event loop; events for other windows are ignored.

Parameter Description
icongrid The icon grid.
event The event.

Returns: 1 if the search box or the grid used the event, 0 if not (an event for another window, or one the grid has no use for).

xtk_icongrid_set_theme

void xtk_icongrid_set_theme(XtkIconGrid *icongrid, const XtkConfig *theme);

Reads the colors again from a configuration, and redraws.

Call it after a theme change. The search box gets the new theme too.

Parameter Description
icongrid The icon grid.
theme The configuration with the new colors. NULL does nothing.