Table of Contents
- xtk_toolbar.h
- Types
- Functions
- xtk_toolbar_create
- xtk_toolbar_destroy
- xtk_toolbar_show
- xtk_toolbar_add_button
- xtk_toolbar_add_dropdown
- xtk_toolbar_handle_event
- xtk_toolbar_resize
- xtk_toolbar_get_window
- xtk_toolbar_get_height
- xtk_toolbar_force_redraw
- xtk_toolbar_reallocate_dbe
- xtk_toolbar_update_tooltips
- xtk_toolbar_set_theme
- xtk_toolbar_get_hovered
xtk_toolbar.h
A row of icon buttons for a program's most used actions.
A toolbar is a strip of small buttons that show an icon and no text, for actions a user wants with one click instead of a trip through a menu. Each button shows an icon from xtk_icon.h (by its key, see xtk_icon_draw()) and can have a tooltip. There are two kinds of button:
- A plain button (
xtk_toolbar_add_button()) calls its callback when it is clicked. - A dropdown button (
xtk_toolbar_add_dropdown()) shows a small chevron next to its icon and opens a popup menu just below itself when it is clicked, for an action that needs a choice (for example "New": a new what?).
Buttons are placed from left to right in the order they are added: 4 pixels from the left edge, 4 pixels apart, centered in the toolbar's height. A plain button is 28 by 24 pixels and a dropdown button 38 by 24. A toolbar holds at most 16 buttons. For the formatting bar of a rich-text editor, see xtk_richtext_toolbar.h instead.
Making it. Call xtk_toolbar_create() with the parent window, the whole XtkConfig and the toolbar's box, add the buttons, then call xtk_toolbar_show() to map the toolbar and its buttons. The toolbar keeps the config pointer and reads it again for each button added, so the config must stay valid as long as the toolbar does. The program owns the toolbar and frees it with xtk_toolbar_destroy(), which also destroys the buttons, the dropdown menus and the window.
Events. Pass each XEvent to xtk_toolbar_handle_event(). Each button is an XtkButton and behaves as one: it shows hover and press states, and a click calls the button's callback from inside xtk_toolbar_handle_event(). A click on a dropdown button opens its menu; choosing an item calls that item's own callback (see xtk_popup_menu.h). Call xtk_toolbar_update_tooltips() once per pass of the main loop: tooltips, hover state and submenus of the dropdown menus need a regular tick, not only events.
Theme keys. The toolbar has no section of its own. It takes its colors from other widgets' sections:
[menubar]background: the toolbar's strip, behind and between the buttons. The whole[menubar]section also styles the dropdown menus.[button]: every key styles the buttons (see xtk_button.h); itscolorkey is also the color of the icons and chevrons.[tooltip]: the buttons' tooltips (see xtk_tooltip.h).
Example.
static void on_save(XtkButton *button, void *user_data) {
(void)button; (void)user_data;
// save the document
}
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);
XtkToolbar *toolbar = xtk_toolbar_create(display, screen, window, &config,
0, 0, window_width, 32);
xtk_toolbar_add_button(toolbar, "device-floppy", "Save", on_save, NULL);
xtk_toolbar_show(toolbar);
XEvent event;
while (running) {
while (XPending(display)) {
XNextEvent(display, &event);
if (xtk_toolbar_handle_event(toolbar, &event)) continue;
// ... the program's other windows
}
xtk_toolbar_update_tooltips(toolbar);
// ... wait a little (for example with select() on the X connection)
}
xtk_toolbar_destroy(toolbar);
*
Limitations.
- Buttons do not wrap or scroll: a button past the right edge is cut off.
- The height is fixed when the toolbar is made;
xtk_toolbar_resize()changes only the width. - Buttons cannot be removed, except by destroying the toolbar.
#include "xtk_toolbar.h"
Types: XtkToolbar
Functions: xtk_toolbar_create, xtk_toolbar_destroy, xtk_toolbar_show, xtk_toolbar_add_button, xtk_toolbar_add_dropdown, xtk_toolbar_handle_event, xtk_toolbar_resize, xtk_toolbar_get_window, xtk_toolbar_get_height, xtk_toolbar_force_redraw, xtk_toolbar_reallocate_dbe, xtk_toolbar_update_tooltips, xtk_toolbar_set_theme, xtk_toolbar_get_hovered
Types
XtkToolbar
typedef struct XtkToolbar XtkToolbar;
A row of icon buttons; opaque, made by xtk_toolbar_create().
Functions
xtk_toolbar_create
XtkToolbar *xtk_toolbar_create(
Display *display,
int screen,
Window parent,
const XtkConfig *theme,
int x,
int y,
int width,
int height);
Create an empty toolbar. Its window is not mapped yet; call xtk_toolbar_show() when the buttons are added.
| Parameter | Description |
|---|---|
display |
The X display |
screen |
The screen number |
parent |
The parent window |
theme |
The configuration: it reads the menubar, button and tooltip blocks. The toolbar keeps this pointer, so it must stay valid as long as the toolbar does. |
x, y |
The position within the parent, in pixels |
width |
The width in pixels |
height |
The height in pixels (more than 24, so the buttons fit) |
Returns: The new toolbar, or NULL if display or theme is NULL or memory runs out
xtk_toolbar_destroy
void xtk_toolbar_destroy(XtkToolbar *toolbar);
Destroy the toolbar, its buttons, their dropdown menus and its window.
| Parameter | Description |
|---|---|
toolbar |
The toolbar (NULL does nothing) |
xtk_toolbar_show
void xtk_toolbar_show(XtkToolbar *toolbar);
Map the toolbar's window and every button added so far.
xtk_toolbar_create() does not map anything; the toolbar is drawn on the first Expose after this call.
| Parameter | Description |
|---|---|
toolbar |
The toolbar (NULL does nothing) |
xtk_toolbar_add_button
int xtk_toolbar_add_button(
XtkToolbar *toolbar,
const char *icon_key,
const char *tooltip,
XtkButtonClickedCallback clicked,
void *user_data);
Add a plain icon button at the right of the last button.
| Parameter | Description |
|---|---|
toolbar |
The toolbar |
icon_key |
The key of an icon in xtk_icon.h (see xtk_icon_draw()), for example "device-floppy"; copied (at most 63 bytes) |
tooltip |
The tooltip text, or NULL or "" for none; a button without a label needs one more than most |
clicked |
Called when the button is clicked (on the button release, or Space or Enter while it has focus), or NULL |
user_data |
Passed to clicked |
Returns: 1 on success; 0 if toolbar or icon_key is NULL, the toolbar already has 16 buttons, or the button cannot be made
xtk_toolbar_add_dropdown
int xtk_toolbar_add_dropdown(
XtkToolbar *toolbar,
const char *icon_key,
const char *tooltip,
const XtkPopupMenuItem *items,
int item_count);
Add a dropdown button at the right of the last button: an icon and a chevron that open a popup menu just below the button when it is clicked.
The menu is styled from the config's menubar block.
| Parameter | Description |
|---|---|
toolbar |
The toolbar |
icon_key |
The key of an icon in xtk_icon.h (see xtk_icon_draw()); copied (at most 63 bytes) |
tooltip |
The tooltip text, or NULL or "" for none |
items |
The menu's items. They are copied, so the array need not stay valid after the call. Each item's callback is called when the user chooses it, as in any XtkPopupMenu. |
item_count |
The number of items (at least 1) |
Returns: 1 on success; 0 if the button or its menu cannot be made (see xtk_toolbar_add_button() for the reasons). If only the menu fails, the button is taken away again, so nothing is added.
xtk_toolbar_handle_event
int xtk_toolbar_handle_event(XtkToolbar *toolbar, XEvent *event);
Give an X event to the toolbar.
Any event can be passed; events for windows that are not the toolbar's are left alone. Button callbacks and menu item callbacks are called from inside this call.
| Parameter | Description |
|---|---|
toolbar |
The toolbar |
event |
The event |
Returns: 1 if the event was for the toolbar's strip, one of its buttons or an open dropdown menu (which holds the pointer and the keyboard, so every pointer and key event is its own); 0 otherwise
xtk_toolbar_resize
void xtk_toolbar_resize(XtkToolbar *toolbar, int width);
Change the toolbar's width and redraw its strip.
The position and the height stay as they were made.
| Parameter | Description |
|---|---|
toolbar |
The toolbar (NULL does nothing) |
width |
The new width in pixels; values below 1 become 1 |
xtk_toolbar_get_window
Window xtk_toolbar_get_window(const XtkToolbar *toolbar);
Get the toolbar's X window.
| Parameter | Description |
|---|---|
toolbar |
The toolbar |
Returns: The window, or None if toolbar is NULL
xtk_toolbar_get_height
int xtk_toolbar_get_height(const XtkToolbar *toolbar);
Get the toolbar's height.
| Parameter | Description |
|---|---|
toolbar |
The toolbar |
Returns: The height in pixels, or 0 if toolbar is NULL
xtk_toolbar_force_redraw
void xtk_toolbar_force_redraw(XtkToolbar *toolbar);
Redraw the toolbar's strip and every button at once, without waiting for Expose events.
Call it during a long operation that blocks the event loop, so the toolbar stays drawn. Handling queued Expose events of the toolbar's own window is not enough there, because each button is a window of its own.
| Parameter | Description |
|---|---|
toolbar |
The toolbar (NULL does nothing) |
xtk_toolbar_reallocate_dbe
void xtk_toolbar_reallocate_dbe(XtkToolbar *toolbar);
Make new back buffers for the toolbar's strip and every button, then redraw them all.
Some X servers leave a back buffer stale when a window manager's dialog opens or closes, while the buffer still looks valid; a plain redraw then draws into that stale buffer. Call this right after such a dialog opens and again right after it closes; it is not needed for ordinary redraws (use xtk_toolbar_force_redraw() for those).
| Parameter | Description |
|---|---|
toolbar |
The toolbar (NULL does nothing) |
xtk_toolbar_update_tooltips
void xtk_toolbar_update_tooltips(XtkToolbar *toolbar);
Do the toolbar's regular work: show or hide button tooltips, update the buttons' hover state, and open submenus of an open dropdown menu.
Call it once per pass of the main loop, also when no event came; the tooltip and submenu delays depend on it.
| Parameter | Description |
|---|---|
toolbar |
The toolbar (NULL does nothing) |
xtk_toolbar_set_theme
void xtk_toolbar_set_theme(XtkToolbar *toolbar, const XtkConfig *theme);
Take new colors from a configuration and redraw: the strip, every button and its tooltip, and every dropdown menu.
Call it after a new theme is applied to the configuration. xtk_toolbar_create() reads the colors only once.
| Parameter | Description |
|---|---|
toolbar |
The toolbar (NULL does nothing) |
theme |
The configuration (NULL does nothing). The toolbar keeps this pointer in place of the old one, as xtk_toolbar_create() does. |
xtk_toolbar_get_hovered
int xtk_toolbar_get_hovered(const XtkToolbar *toolbar);
Get the button under the pointer.
Buttons are counted from 0 in the order they were added.
| Parameter | Description |
|---|---|
toolbar |
The toolbar |
Returns: The index of the hovered button, or -1 if no button is hovered or toolbar 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