Table of Contents
- xtk_combobox.h
- Types
- Functions
- xtk_combobox_create
- xtk_combobox_destroy
- xtk_combobox_show
- xtk_combobox_hide
- xtk_combobox_set_item_enabled
- xtk_combobox_get_item_enabled
- xtk_combobox_set_enabled
- xtk_combobox_get_enabled
- xtk_combobox_set_items
- xtk_combobox_set_selected_index
- xtk_combobox_get_selected_index
- xtk_combobox_set_callback
- xtk_combobox_set_item_icons
- xtk_combobox_set_item_fonts
- xtk_combobox_handle_event
- xtk_combobox_set_focused
- xtk_combobox_get_focused
- xtk_combobox_get_focus_window
- xtk_combobox_draw
- xtk_combobox_get_window
- xtk_combobox_set_position
- xtk_combobox_set_width
- xtk_combobox_resize
- xtk_combobox_get_size
- xtk_combobox_autosize_width
- xtk_combobox_set_theme
- xtk_combobox_config_init_defaults
- xtk_combobox_config_parse_styling
- xtk_combobox_config_write_styling
- xtk_combobox_widget_config_init_defaults
- xtk_combobox_widget_config_parse
- xtk_combobox_widget_config_write
- xtk_combobox_get_hover
- xtk_combobox_keep_open
- xtk_combobox_get_dropdown_window
xtk_combobox.h
A drop-down list for choosing one item out of several.
An XtkComboBox shows the selected item in a box with a down arrow. A click opens a list of all items below the box (or above it, when there is more room there); choosing a row selects it and closes the list. Items can be disabled one by one, and can have a small icon (drawn by the program) and, in the open list, a font of their own (for a font picker, say).
Creating a combo box. xtk_combobox_create() takes the whole XtkConfig, of which it copies config.combobox (colors and font) and the border width, radius and padding in config.combobox_widget, a position and a size (normally config.combobox_widget.width and .height). Then the program gives it its items with xtk_combobox_set_items(), which copies the labels and makes the box as wide as the widest item. The window is not mapped until xtk_combobox_set_position() or xtk_combobox_show(). The program frees the combo box with xtk_combobox_destroy().
Events. The program passes every XEvent to xtk_combobox_handle_event(), also the events of the open list's window (an override-redirect child of the root window) and of the parent window. While the list is open, the combo box grabs the pointer and the keyboard; a release outside the rows, the pointer leaving the list, a second click on the box, Escape or a real loss of focus closes it. In the open list, the wheel scrolls it when it is longer than the screen allows.
With the keyboard focus (a click on the box gives it the X input focus):
- List closed: Up or Left selects the item before, Down or Right the item after; typing a letter selects the next item starting with it (leading spaces are skipped); Space or Return opens the list.
- List open: Up, Down, Home and End move the highlight, typing a letter moves it to the next item starting with it, Return chooses the highlighted item, Escape closes the list without choosing.
Disabled items are skipped by the keys and cannot be clicked.
The callback (xtk_combobox_set_callback()) runs when the user chooses an item: a click on a row, Return in the open list, or a key that changes the selection while the list is closed. A click on the row that was already selected runs it too. It does not run for xtk_combobox_set_selected_index() or xtk_combobox_set_items().
Theme keys. [combobox]:
background(default#262626): the box.hover-background(#333333): the box while the pointer is over it.active-background(#405980): the box while it is pressed.border(#808080): the frame of the box and of the list.hover-border(thebordercolor): the frame while the pointer is over the box.focus-border(the toolkit's focus color): the frame while the box has the focus or is pressed.color(#F2F2F2): the text and the arrow.dropdown-background(#262626): the open list.dropdown-hover-background(#405980): the highlighted row of the list (the selected row while nothing is highlighted).dropdown-hover-color(#F2F2F2): the text of the highlighted row, and of the box while the pointer is over it.font-family(orfont,DejaVu Sans) andfont-size(12).highlightandshade: an optional bevel on the box (see xtk_paint.h).
The five background keys also accept a CSS gradient.
[combobox-widget]:
width(230) andheight(24): the size a program normally gives the box.border-width(1),border-radius(4) andpadding(4, between the frame and the text).
Example.
static void on_unit(XtkComboBox *combobox, int index, const char *label, void *user_data) {
(void)combobox;
(void)user_data;
printf("Unit %d: %s\n", index, 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 char *units[] = { "Pixels", "Points", "Millimetres" };
XtkComboBox *unit = xtk_combobox_create(display, DefaultScreen(display), parent, &config,
0, 0, config.combobox_widget.width, config.combobox_widget.height);
xtk_combobox_set_items(unit, units, 3);
xtk_combobox_set_selected_index(unit, 1);
xtk_combobox_set_callback(unit, on_unit, NULL);
xtk_combobox_set_position(unit, 10, 10); // maps it
XEvent event;
while (running) {
XNextEvent(display, &event);
xtk_combobox_handle_event(unit, &event);
}
xtk_combobox_destroy(unit);
*
Limitations.
xtk_combobox_set_theme()changes colors and font only; the border width, radius and padding stay as they were at creation.
#include "xtk_combobox.h"
Types: XtkComboBox, XtkComboBoxCallback, XtkComboBoxIconDrawCallback
Functions: xtk_combobox_create, xtk_combobox_destroy, xtk_combobox_show, xtk_combobox_hide, xtk_combobox_set_item_enabled, xtk_combobox_get_item_enabled, xtk_combobox_set_enabled, xtk_combobox_get_enabled, xtk_combobox_set_items, xtk_combobox_set_selected_index, xtk_combobox_get_selected_index, xtk_combobox_set_callback, xtk_combobox_set_item_icons, xtk_combobox_set_item_fonts, xtk_combobox_handle_event, xtk_combobox_set_focused, xtk_combobox_get_focused, xtk_combobox_get_focus_window, xtk_combobox_draw, xtk_combobox_get_window, xtk_combobox_set_position, xtk_combobox_set_width, xtk_combobox_resize, xtk_combobox_get_size, xtk_combobox_autosize_width, xtk_combobox_set_theme, xtk_combobox_config_init_defaults, xtk_combobox_config_parse_styling, xtk_combobox_config_write_styling, xtk_combobox_widget_config_init_defaults, xtk_combobox_widget_config_parse, xtk_combobox_widget_config_write, xtk_combobox_get_hover, xtk_combobox_keep_open, xtk_combobox_get_dropdown_window
Types
XtkComboBox
typedef struct XtkComboBox XtkComboBox;
A drop-down list. The structure is private; make one with xtk_combobox_create().
XtkComboBoxCallback
typedef void (*XtkComboBoxCallback)(XtkComboBox *combobox, int index, const char *label, void *user_data);
Called when the user chooses an item.
It runs after the list has closed and the box is redrawn, so it may destroy the combo box.
XtkComboBoxIconDrawCallback
typedef void (*XtkComboBoxIconDrawCallback)(Display *display, Drawable drawable, GC gc, int index, int x, int y, int size, unsigned long pixel, void *user_data);
Draws the icon of one item, in the box and in the open list.
See xtk_combobox_set_item_icons().
Functions
xtk_combobox_create
XtkComboBox *xtk_combobox_create(
Display *display,
int screen,
Window parent,
const XtkConfig *theme,
int x,
int y,
int width,
int height);
Create a combo box with no items.
Its window is not mapped until xtk_combobox_set_position() or xtk_combobox_show().
| Parameter | Description |
|---|---|
display |
The X display. |
screen |
The screen number. |
parent |
The window the combo box sits in. |
theme |
The configuration; its combobox block is copied, and the border width, radius and padding are read from its combobox_widget block. |
x |
The x position in the parent, in pixels. |
y |
The y position in the parent, in pixels. |
width |
The width in pixels; 160 if 0 or less. xtk_combobox_set_items() changes it to fit the items. |
height |
The height in pixels; 24 if 0 or less. |
Returns: The new combo box, or NULL if display or theme is NULL or memory runs out.
xtk_combobox_destroy
void xtk_combobox_destroy(XtkComboBox *combobox);
Destroy a combo box: its windows, items, font and memory.
Fonts given with xtk_combobox_set_item_fonts() are not closed.
| Parameter | Description |
|---|---|
combobox |
The combo box, or NULL (then nothing happens). |
xtk_combobox_show
void xtk_combobox_show(XtkComboBox *combobox);
Show the combo box: map its window.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
xtk_combobox_hide
void xtk_combobox_hide(XtkComboBox *combobox);
Hide the combo box (and its open list): unmap its windows.
It stays unmapped when xtk_combobox_set_position() moves it, until xtk_combobox_show().
| Parameter | Description |
|---|---|
combobox |
The combo box. |
xtk_combobox_set_item_enabled
void xtk_combobox_set_item_enabled(XtkComboBox *combobox, int index, int enabled);
Enable or disable one item.
A disabled item is drawn grey in the open list and cannot be chosen by a click, a key or typing, as with xtk_popup_menu_set_item_enabled(). xtk_combobox_set_selected_index() can still select it. All items start enabled, and xtk_combobox_set_items() enables them all again. Redraws the open list.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
index |
The item's index; an index outside the list is ignored. |
enabled |
Nonzero to enable, 0 to disable. |
xtk_combobox_get_item_enabled
int xtk_combobox_get_item_enabled(const XtkComboBox *combobox, int index);
Whether an item exists and is enabled.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
index |
The item's index. |
Returns: 1 if the item exists and is enabled, 0 otherwise.
xtk_combobox_set_enabled
void xtk_combobox_set_enabled(XtkComboBox *combobox, int enabled);
Enable or disable the whole combo box, and redraw.
A disabled combo box is drawn in muted colors (the box 85% background and 15% color, without a visible frame, the text 40% color), closes its list and ignores the mouse and keyboard. Combo boxes start enabled.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
enabled |
1 to enable, 0 to disable. |
xtk_combobox_get_enabled
int xtk_combobox_get_enabled(const XtkComboBox *combobox);
Whether the combo box is enabled.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
Returns: Nonzero if enabled, 0 if disabled or if combobox is NULL.
xtk_combobox_set_items
void xtk_combobox_set_items(XtkComboBox *combobox, const char **labels, int count);
Replace the items, and redraw.
The labels are copied (a NULL label becomes ""). The selection stays at the same index if the new list is long enough, else it goes to the first item. The width is fitted to the widest item (see xtk_combobox_autosize_width()), all items are enabled again, and fonts from xtk_combobox_set_item_fonts() are dropped. The callback does not run.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
labels |
The labels, or NULL for no items. |
count |
The number of labels; 0 or less for no items (then nothing is selected). |
xtk_combobox_set_selected_index
void xtk_combobox_set_selected_index(XtkComboBox *combobox, int index);
Select an item, and redraw.
The callback does not run. A disabled item can be selected this way.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
index |
The item's index, clamped to the list. Nothing happens if the list is empty. |
xtk_combobox_get_selected_index
int xtk_combobox_get_selected_index(const XtkComboBox *combobox);
The selected item's index.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
Returns: The index, or -1 if the list is empty or combobox is NULL.
xtk_combobox_set_callback
void xtk_combobox_set_callback(
XtkComboBox *combobox,
XtkComboBoxCallback callback,
void *user_data);
Set or remove the callback that runs when the user chooses an item.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
callback |
The callback, or NULL to remove it. |
user_data |
A pointer passed to the callback unchanged. |
xtk_combobox_set_item_icons
void xtk_combobox_set_item_icons(
XtkComboBox *combobox,
XtkComboBoxIconDrawCallback draw_function,
void *user_data);
Draw an icon before each label, in the box and in the open list, and redraw.
The icon is a square as high as a row minus the padding. Call xtk_combobox_autosize_width() afterwards if the box should grow for it.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
draw_function |
The function that draws one item's icon, or NULL for labels only. |
user_data |
A pointer passed to draw_function unchanged. |
xtk_combobox_set_item_fonts
void xtk_combobox_set_item_fonts(XtkComboBox *combobox, XftFont **fonts);
Give each row of the open list a font of its own, and redraw.
For example, a font picker can show each family name in its own font. The box itself always uses the combo box's font. The combo box copies the array of pointers, but never opens or closes the fonts: they stay the program's and must stay open while they are in use. xtk_combobox_set_items() drops the fonts, so call this again after each new list.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
fonts |
An array with one font per current item (an entry may be NULL for the usual font), or NULL for the usual font in every row. |
xtk_combobox_handle_event
int xtk_combobox_handle_event(XtkComboBox *combobox, XEvent *event);
Handle an X event.
Pass it the events of the box, of the open list and of the parent window. A choice is reported through the callback, not through the return value. A disabled combo box handles only Expose, EnterNotify and LeaveNotify.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
event |
The event. |
Returns: 1 if the combo box used the event, 0 if it was not for it.
xtk_combobox_set_focused
void xtk_combobox_set_focused(XtkComboBox *combobox, int focused);
Give the combo box keyboard focus, or take it away.
For a program that moves focus with Tab. With focused nonzero the combo box takes the X input focus (if its window is viewable) and draws its frame in focus-border; the keys described above then work. With 0 it clears its focus look and closes an open list, as a real FocusOut does; the X focus moves when another widget takes it.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
focused |
Nonzero to take the focus, 0 to clear it. |
xtk_combobox_get_focused
int xtk_combobox_get_focused(const XtkComboBox *combobox);
Whether the combo box shows keyboard focus.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
Returns: 1 if focused, 0 if not or if combobox is NULL.
xtk_combobox_get_focus_window
Window xtk_combobox_get_focus_window(const XtkComboBox *combobox);
The window that takes the X input focus for this combo box.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
Returns: The box's window, or None if combobox is NULL.
xtk_combobox_draw
void xtk_combobox_draw(XtkComboBox *combobox);
Redraw the box now (not the open list).
| Parameter | Description |
|---|---|
combobox |
The combo box, or NULL. |
xtk_combobox_get_window
Window xtk_combobox_get_window(const XtkComboBox *combobox);
The box's X window.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
Returns: The window, or None if combobox is NULL.
xtk_combobox_set_position
void xtk_combobox_set_position(XtkComboBox *combobox, int x, int y);
Move the combo box within its parent.
The first call also maps it, unless it is hidden with xtk_combobox_hide().
| Parameter | Description |
|---|---|
combobox |
The combo box. |
x |
The new x position in the parent, in pixels. |
y |
The new y position in the parent, in pixels. |
xtk_combobox_set_width
void xtk_combobox_set_width(XtkComboBox *combobox, int width);
Change the width and keep the height.
The box is redrawn.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
width |
The new width in pixels; 0 or less is ignored. |
xtk_combobox_resize
void xtk_combobox_resize(XtkComboBox *combobox, int width, int height);
Change the size, and redraw.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
width |
The new width in pixels; 0 or less is ignored (with the height). |
height |
The new height in pixels; 0 or less is ignored (with the width). |
xtk_combobox_get_size
void xtk_combobox_get_size(const XtkComboBox *combobox, int *w, int *h);
The box's size.
| Parameter | Description |
|---|---|
combobox |
The combo box. If it is NULL, both outputs are set to 0. |
w |
Receives the width in pixels; may be NULL. |
h |
Receives the height in pixels; may be NULL. |
xtk_combobox_autosize_width
void xtk_combobox_autosize_width(XtkComboBox *combobox);
Make the box as wide as the widest item needs.
The width covers the text, the frame and padding, the arrow and, with icons, an icon; at least 24 pixels. Nothing happens without a font.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
xtk_combobox_set_theme
void xtk_combobox_set_theme(XtkComboBox *combobox, const ComboBoxBlock *style);
Give the combo box new colors and a new font, and redraw (the box and an open list).
Call it after the theme changes. The border width, radius and padding stay.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
style |
The new colors and font, normally &config.combobox; copied. NULL does nothing. |
xtk_combobox_config_init_defaults
void xtk_combobox_config_init_defaults(ComboBoxBlock *combobox_config);
Fill a ComboBoxBlock with the default [combobox] colors and font.
xtk_config_init_defaults() already calls it for config.combobox.
| Parameter | Description |
|---|---|
combobox_config |
The block to fill; NULL does nothing. |
xtk_combobox_config_parse_styling
int xtk_combobox_config_parse_styling(
ComboBoxBlock *combobox_config,
const char *key,
const char *value);
Set one [combobox] key in a ComboBoxBlock.
| Parameter | Description |
|---|---|
combobox_config |
The block to change. |
key |
The key name, such as active-background. |
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_combobox_config_write_styling
int xtk_combobox_config_write_styling(FILE *file, const ComboBoxBlock *combobox_config);
Write a ComboBoxBlock as a [combobox] section.
It writes every key that xtk_combobox_config_parse_styling() reads.
| Parameter | Description |
|---|---|
file |
The file to write to. |
combobox_config |
The block to write. |
Returns: 0 on success, -1 if an argument is NULL.
xtk_combobox_widget_config_init_defaults
void xtk_combobox_widget_config_init_defaults(XtkConfig *config);
Set config.combobox_widget to the default [combobox-widget] values.
| Parameter | Description |
|---|---|
config |
The configuration; NULL does nothing. |
xtk_combobox_widget_config_parse
int xtk_combobox_widget_config_parse(
XtkConfig *config,
const char *key,
const char *value);
Set one [combobox-widget] key in config.combobox_widget.
| Parameter | Description |
|---|---|
config |
The configuration. |
key |
The key name: width, height, 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 it is unknown or an argument is NULL.
xtk_combobox_widget_config_write
int xtk_combobox_widget_config_write(FILE *file, const XtkConfig *config);
Write config.combobox_widget as a [combobox-widget] section.
| Parameter | Description |
|---|---|
file |
The file to write to. |
config |
The configuration. |
Returns: 0 on success, -1 if an argument is NULL.
xtk_combobox_get_hover
int xtk_combobox_get_hover(const XtkComboBox *combobox);
The highlighted row of the open list.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
Returns: The highlighted item's index, or -1 if none is highlighted, the list is closed, or combobox is NULL.
xtk_combobox_keep_open
void xtk_combobox_keep_open(XtkComboBox *combobox, int keep);
Open the list and keep it open, or close it again.
For showing the open list without a pointer. A list kept open takes no grab, and nothing else closes it: not the pointer leaving, a click elsewhere or a lost focus. Choosing a row selects it and runs the callback, and the list stays open.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
keep |
1 to open the list and keep it open (nothing happens if there are no items); 0 to close a list kept open. |
xtk_combobox_get_dropdown_window
Window xtk_combobox_get_dropdown_window(const XtkComboBox *combobox);
The open list's window.
| Parameter | Description |
|---|---|
combobox |
The combo box. |
Returns: The list's window while it is open; None while it is closed or if combobox is NULL.
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats