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

xtk_panel.h

A scrolling color-map list: each row maps an original color to a palette color.

A panel shows a list of rows under a header that reads "Color Map (n)" with the column titles "Original" and "Palette". Each row (an XtkPanelEntry) shows a swatch of an original color on the left, an arrow, and a swatch of the palette color it maps to on the right, each with its hex value (or the palette color's name) below it. A row with no palette color shows "none". A locked palette color has an amber bar under its swatch. A program uses it to show how a set of colors (for example those found in a document) maps onto a palette, and lets the user change that map.

The user can:

  • scroll the list with the mouse wheel or the thin scrollbar at the right;
  • drag a swatch onto another row to swap the two rows (the reorder callback is called);
  • drag a swatch out of the panel to drop its color somewhere else (the color-drag callback is called, if one is set);
  • right-click a palette swatch to lock or unlock that palette color (the lock callback is called);
  • click the label under a palette swatch to edit the color as hex text in place (the edit callback is called when the edit is committed).

Making it. Call xtk_panel_create() with the parent window, the whole XtkConfig and the box. The panel maps its window. Give it the palette with xtk_panel_set_palette() and then the rows with xtk_panel_set_entries(); it copies both. Set the callbacks the program needs, and give it an XtkClipboard with xtk_panel_set_clipboard() so the editor can cut, copy and paste. The program owns the panel and frees it with xtk_panel_destroy().

Events. Pass each XEvent to xtk_panel_handle_event(); it also takes the events of the scrollbar, the swatches and the editor's context menu (xtk_panel_owns_window() tells which windows are the panel's). Call xtk_panel_update_blink() once per pass of the main loop so the editor's caret blinks.

The hex editor takes the keyboard focus when it opens. In it:

  • hex digits and # are typed (at most 10 characters); a commit takes #RGB or #RRGGBB (the # may be left out);
  • Left, Right, Home and End move the caret, with Shift to select; Backspace and Delete delete; a double click selects all;
  • Ctrl+A selects all, Ctrl+C copies, Ctrl+X cuts, Ctrl+V pastes (only hex digits and # are kept), Ctrl+Z undoes and Ctrl+Y redoes;
  • a right click opens a menu with Cut, Copy, Paste, Select All, Undo and Redo;
  • Enter, a click elsewhere or the loss of the focus commits the edit; Escape closes the editor without a change.

Each palette color has its own undo history, which lasts while the panel lives. A commit sets the palette color (keeping its alpha) only if the text is a hex color; the edit callback is then called.

Theme keys. The panel has no section of its own. It uses:

  • [main] background: the panel's background, and the scrollbar's track. [main] selection and selection-text: selected text in the editor, and the frame around the row a dragged swatch will drop on.
  • [label] font-family and font-size: the font (the size less 2 points). [label] color: the arrows, mixed with the background for the scrollbar thumb. [label] border: the lines between the rows and under the header.
  • [button] background: the palette swatch of a row with no palette color.
  • [entry-hex] background, color and focus-border: the hex editor's box, text, and its border and caret.
  • [menu]: the editor's context menu.
  • [slider]: the scrollbar, in part.

Some colors are fixed: the hex labels are gray (#999999), the title is light gray (#CCCCCC), the column titles are dark gray (#777777) and the lock bar is amber (#FFA500).

Example.

static void on_edit(XtkPanel *panel, int palette_index, ConfigColor color, void *user_data) {
    (void)panel; (void)user_data;
    // store color as palette color palette_index
}

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

XtkPanel *panel = xtk_panel_create(display, screen, window, &config, 0, 0, 220, 400);
XtkPanelColor palette[2] = {
    { {0.8, 0.1, 0.1, 1.0}, "", 0 },
    { {0.1, 0.2, 0.8, 1.0}, "Blue", 0 },
};
XtkPanelEntry entries[2] = {
    { {0.9, 0.2, 0.2, 1.0}, 0 },    // maps to palette color 0
    { {0.5, 0.5, 0.5, 1.0}, -1 },   // no palette color
};
xtk_panel_set_palette(panel, palette, 2);
xtk_panel_set_entries(panel, entries, 2);
xtk_panel_set_edit_callback(panel, on_edit, NULL);

XEvent event;
while (running) {
    while (XPending(display)) {
        XNextEvent(display, &event);
        if (xtk_panel_handle_event(panel, &event)) continue;
        // ... the program's other windows
    }
    xtk_panel_update_blink(panel);
    // ... wait a little (for example with select() on the X connection)
}
xtk_panel_destroy(panel);
 * 

Limitations.

  • The panel holds at most XTK_PANEL_MAX_ENTRIES rows and palette colors.
  • The header text, the column titles and the layout (rows 62 pixels high, swatches 28 pixels) are fixed.
  • The palette given to xtk_panel_set_palette() sets the locks: a program keeps the user's locks by recording them from the lock callback.
  • The panel does not report the order of the rows; a program that needs it keeps it from the reorder callback.
#include "xtk_panel.h"

Types: XtkPanelEntry, XtkPanelColor, XtkPanel, XtkPanelReorderCallback, XtkPanelColorDragCallback, XtkPanelEditCallback, XtkPanelLockCallback

Constants: XTK_PANEL_MAX_ENTRIES

Functions: xtk_panel_create, xtk_panel_destroy, xtk_panel_get_window, xtk_panel_owns_window, xtk_panel_set_entries, xtk_panel_set_palette, xtk_panel_resize, xtk_panel_set_theme, xtk_panel_paint, xtk_panel_handle_event, xtk_panel_set_reorder_callback, xtk_panel_set_color_drag_callback, xtk_panel_set_edit_callback, xtk_panel_set_lock_callback, xtk_panel_get_locked, xtk_panel_update_blink, xtk_panel_set_clipboard

Types

XtkPanelEntry

One row of the panel: an original color and the palette color it maps to.

Field Type Description
original ConfigColor The original color; its alpha is ignored (the swatch is drawn opaque)
palette_index int The index of its palette color (see xtk_panel_set_palette()), or -1 for none.

XtkPanelColor

One palette color: its color, name and lock.

Field Type Description
color ConfigColor The color; an alpha below 1.0 is drawn over a checkerboard.
label[32] char The name shown under its swatch (at most 12 characters are shown), or "" to show the hex value.
locked int 1 if the color is locked (an amber bar under its swatch); what a lock means is the program's choice

XtkPanel

typedef struct XtkPanel XtkPanel;

A color-map panel; opaque, made by xtk_panel_create().

XtkPanelReorderCallback

typedef void (*XtkPanelReorderCallback)(XtkPanel *panel, int from_di, int to_di, void *user_data);

Called after the user drags a swatch onto another row, which swaps the two rows.

The indexes are display positions (0 is the top row), not indexes into the entries array: the panel keeps the entries in their order and changes only the order it shows them in. It is not called when a swatch is dropped on its own row or outside the panel.

XtkPanelColorDragCallback

typedef void (*XtkPanelColorDragCallback)(XtkPanel *panel, ConfigColor color, int root_x, int root_y, int active, void *user_data);

Called while the user drags a swatch's color out of the panel, and when the user drops it.

Once a dragged swatch has left the panel, the function is called on every motion with active 1, and once on the release with active 0. The program uses it to show where the color will go and to drop it there. The color is the palette color for a palette swatch (the original color if the row has none), or the original color, opaque, for an original swatch.

XtkPanelEditCallback

typedef void (*XtkPanelEditCallback)(XtkPanel *panel, int palette_index, ConfigColor color, void *user_data);

Called when the user commits a new hex color in the editor under a palette swatch.

The commit comes from Enter, a click elsewhere or the loss of the focus. When it is called, the editor is closed and the panel's copy of the palette already has the new color.

XtkPanelLockCallback

typedef void (*XtkPanelLockCallback)(XtkPanel *panel, int palette_index, int locked, void *user_data);

Called when the user locks or unlocks a palette color with a right click on its swatch.

The panel's copy of the palette already has the new lock. What a lock means is the program's choice; it records the lock in its own palette, so that the next xtk_panel_set_palette() keeps it.

Constants

XTK_PANEL_MAX_ENTRIES

#define XTK_PANEL_MAX_ENTRIES 256

The most rows, and the most palette colors, a panel holds; more are ignored.

Functions

xtk_panel_create

XtkPanel *xtk_panel_create(
    Display *display,
    int screen,
    Window parent,
    XtkConfig *config,
    int x,
    int y,
    int width,
    int height);

Create an empty panel and map its window.

Parameter Description
display The X display
screen The screen number
parent The parent window
config The configuration (see the theme keys above); NULL makes no panel. The panel keeps the pointer, so it must stay valid as long as the panel does.
x The X position in the parent, in pixels
y The Y position in the parent, in pixels
width The width in pixels; must be at least 1
height The height in pixels; must be at least 1

Returns: The new panel, or NULL if display or config is NULL, memory runs out or the window cannot be made

xtk_panel_destroy

void xtk_panel_destroy(XtkPanel *panel);

Destroy the panel, its swatches, scrollbar, menu and window.

Parameter Description
panel The panel (NULL does nothing)

xtk_panel_get_window

Window xtk_panel_get_window(const XtkPanel *panel);

Get the panel's X window.

Parameter Description
panel The panel

Returns: The window, or None if panel is NULL

xtk_panel_owns_window

int xtk_panel_owns_window(const XtkPanel *panel, Window window);

Tell whether a window is one of the panel's: its own window, its scrollbar or one of its swatches.

The editor's context menu is not counted.

Parameter Description
panel The panel
window The window

Returns: 1 if the window is the panel's, 0 if not or panel is NULL

xtk_panel_set_entries

void xtk_panel_set_entries(XtkPanel *panel, const XtkPanelEntry *entries, int count);

Replace all rows, and redraw.

The rows are copied and shown in the order of the array (any order the user made by dragging is forgotten), and the list scrolls back to the top. Set the palette first, so the palette swatches get their colors.

Parameter Description
panel The panel (NULL does nothing)
entries The rows; may be NULL if count is 0
count The number of rows; at most XTK_PANEL_MAX_ENTRIES are kept

xtk_panel_set_palette

void xtk_panel_set_palette(XtkPanel *panel, const XtkPanelColor *palette, int count);

Replace the palette colors, and redraw.

The colors are copied, their locks too; the rows keep their order. Call it again whenever the program's palette changes, including after an edit or a lock (see xtk_panel_set_lock_callback()).

Parameter Description
panel The panel (NULL does nothing)
palette The palette colors, indexed by XtkPanelEntry::palette_index; may be NULL if count is 0
count The number of colors; at most XTK_PANEL_MAX_ENTRIES are kept

xtk_panel_resize

void xtk_panel_resize(XtkPanel *panel, int width, int height);

Resize the panel, move its scrollbar to the new right edge, and redraw.

Parameter Description
panel The panel (NULL does nothing)
width The new width in pixels; values below 1 become 1
height The new height in pixels; values below 1 become 1

xtk_panel_set_theme

void xtk_panel_set_theme(XtkPanel *panel, XtkConfig *config);

Use a configuration's new colors, and redraw.

Call it after a new theme is applied to the configuration. The font (from [label]) is opened again and the context menu takes the new [menu] style.

Parameter Description
panel The panel (NULL does nothing)
config The configuration (NULL does nothing). The panel keeps this pointer in place of the old one, so it must stay valid.

xtk_panel_paint

void xtk_panel_paint(XtkPanel *panel);

Redraw the panel now.

Parameter Description
panel The panel (NULL does nothing)

xtk_panel_handle_event

int xtk_panel_handle_event(XtkPanel *panel, XEvent *event);

Give an X event to the panel.

Any event can be passed. Callbacks are called from inside this call. While the hex editor is open, every key press is the editor's.

Parameter Description
panel The panel
event The event

Returns: 1 if the panel used the event (scrollbar, swatches, editor, menu, wheel, or the last Expose of its window); 0 otherwise, also for focus changes and if panel is NULL

xtk_panel_set_reorder_callback

void xtk_panel_set_reorder_callback(
    XtkPanel *panel,
    XtkPanelReorderCallback callback,
    void *user_data);

Set the function called after the user swaps two rows by dragging.

Parameter Description
panel The panel (NULL does nothing)
callback The function, or NULL for none
user_data A pointer passed to the function

xtk_panel_set_color_drag_callback

void xtk_panel_set_color_drag_callback(
    XtkPanel *panel,
    XtkPanelColorDragCallback callback,
    void *user_data);

Set the function called while a swatch's color is dragged out of the panel.

Without it, dragging out of the panel does nothing.

Parameter Description
panel The panel (NULL does nothing)
callback The function, or NULL for none
user_data A pointer passed to the function

xtk_panel_set_edit_callback

void xtk_panel_set_edit_callback(
    XtkPanel *panel,
    XtkPanelEditCallback callback,
    void *user_data);

Set the function called when the user commits an edit of a palette color.

Note: The function is called while the panel is still handling an event: it may change the panel (for example call xtk_panel_set_palette()), but it must not destroy it.

Parameter Description
panel The panel (NULL does nothing)
callback The function, or NULL for none
user_data A pointer passed to the function

xtk_panel_set_lock_callback

void xtk_panel_set_lock_callback(
    XtkPanel *panel,
    XtkPanelLockCallback callback,
    void *user_data);

Set the function called when the user locks or unlocks a palette color.

Note: It is called last while the panel handles the event: it may change the panel (for example call xtk_panel_set_palette()), but it must not destroy it.

Parameter Description
panel The panel (NULL does nothing)
callback The function, or NULL for none
user_data A pointer passed to the function

xtk_panel_get_locked

int xtk_panel_get_locked(const XtkPanel *panel, int palette_index);

Tell whether a palette color is locked in the panel.

Parameter Description
panel The panel
palette_index The index of the palette color

Returns: 1 if it is locked, 0 if not, if the index is out of range or if panel is NULL

void xtk_panel_update_blink(XtkPanel *panel);

Make the editor's caret blink.

Call it once per pass of the main loop. It redraws only while the hex editor is open, about every half second (530 milliseconds).

Parameter Description
panel The panel (NULL does nothing)

xtk_panel_set_clipboard

void xtk_panel_set_clipboard(XtkPanel *panel, XtkClipboard *clipboard);

Give the panel a clipboard for cut, copy and paste in the editor.

Without one, those do nothing (Paste is disabled in the menu).

Parameter Description
panel The panel (NULL does nothing)
clipboard The clipboard (see xtk_clipboard.h), or NULL. The program keeps it and must keep it valid as long as the panel uses it.