Table of Contents
- xtk_tray.h
- Types
- Constants
- Functions
- xtk_tray_create
- xtk_tray_create_from_pixmap
- xtk_tray_destroy
- xtk_tray_set_menu
- xtk_tray_menu_is_open
- xtk_tray_handle_event
- xtk_tray_is_available
- xtk_tray_get_window
- xtk_tray_set_theme
- xtk_tray_set_tooltip
- xtk_tray_update
- xtk_tray_get_tooltip
- xtk_tray_show_notification
- xtk_tray_cancel_notification
- xtk_tray_config_init_defaults
- xtk_tray_config_parse_styling
- xtk_tray_config_write_styling
xtk_tray.h
An icon in the desktop's system tray, with a tooltip, a context menu and notifications.
A tray icon puts a small picture of the program in the system tray of the desktop panel (the freedesktop.org system tray protocol, through XEMBED). A program uses it to stay reachable while its main window is hidden: a left click on the icon usually shows or hides the window.
A right click opens a context menu with fixed items: "Pick Color", a window item, "Copy as Hex", a separator and "Exit". The window item reads "Minimize" while the program's main window is shown and "Maximize" while it is hidden (or "Show Window" when no main window was given). The icon can also show a tooltip when the pointer rests on it, and ask the tray to show a short notification balloon.
Making it. xtk_tray_is_available() tells whether a tray is running. xtk_tray_create() makes the icon from XPM data; xtk_tray_create_from_pixmap() takes a Pixmap the program already has (for example from xtk_image_load_pixmap()). Both need the menu style, a TrayMenuBlock (usually &config.tray_menu), and the program's main window, whose visibility names the window item. They map the icon window and ask the tray to dock it. When no tray is running, the icon window waits, unmapped, and docks as soon as a tray starts. When the tray goes away (the panel is restarted, say), the icon is hidden and docks again in the new tray. For this, the tray selects StructureNotifyMask on the root window and on the tray's window, and xtk_tray_handle_event() must get the events of every window. The program owns the tray and frees it with xtk_tray_destroy().
Events. Pass each XEvent to xtk_tray_handle_event(), also events of the open context menu. It returns what the user did:
- 0: nothing for the program to do.
- 1: a left click on the icon (usually: show or hide the main window).
- 2: "Pick Color" was chosen.
- 3: the window item ("Minimize", "Maximize" or "Show Window") was chosen.
- 4: "Copy as Hex" was chosen.
- 5: "Exit" was chosen.
The program decides what each one does; the tray does nothing to the main window itself. Call xtk_tray_update() once per pass of the main loop, so the tooltip appears after its delay.
Theme keys. Section [tray-menu], for the context menu:
font-family(orfont): the font family. DefaultDejaVu Sans.font-size: the font size in points. Default 14.color: the item text. Default#B5965C.background: the menu's fill. Default#363026.hover-background: the fill of the item under the pointer. Default#454038.hover-color: the text of the item under the pointer. Default: unset, which means thecolorvalue.border: the menu's border. Default#8A7D4F.border-width: the border width in pixels. Default 1.border-radius: the corner radius in pixels. Default 4.
The defaults are those xtk_config_init_defaults() sets; a theme applied with xtk_theme_apply() replaces the colors. The tooltip uses the [tooltip] section once xtk_tray_set_theme() has been called.
Example.
#include "icon.xpm" // static const char *icon_xpm[] = { ... };
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);
XtkTray *tray = NULL;
if (xtk_tray_is_available(display, screen))
tray = xtk_tray_create(display, screen, icon_xpm, &config.tray_menu, window);
xtk_tray_set_theme(tray, &config);
xtk_tray_set_tooltip(tray, "My program");
XEvent event;
while (running) {
while (XPending(display)) {
XNextEvent(display, &event);
switch (xtk_tray_handle_event(tray, &event)) {
case 1: case 3: toggle_main_window(); continue;
case 5: running = 0; continue;
default: break;
}
// ... the program's other windows
}
xtk_tray_update(tray);
// ... wait a little (for example with select() on the X connection)
}
xtk_tray_destroy(tray);
*
Limitations.
- The context menu has the fixed items, or the program's own (
xtk_tray_set_menu()), set as a whole: one item cannot be added, removed or renamed alone. - The tooltip text keeps at most 1023 bytes. A line wider than the screen wraps.
- Not every tray shows notifications, and there is no way to know whether one was shown.
#include "xtk_tray.h"
Types: XtkTray
Constants: XTK_TRAY_MENU_MAX, XTK_TRAY_OWN_ITEM
Functions: xtk_tray_create, xtk_tray_create_from_pixmap, xtk_tray_destroy, xtk_tray_set_menu, xtk_tray_menu_is_open, xtk_tray_handle_event, xtk_tray_is_available, xtk_tray_get_window, xtk_tray_set_theme, xtk_tray_set_tooltip, xtk_tray_update, xtk_tray_get_tooltip, xtk_tray_show_notification, xtk_tray_cancel_notification, xtk_tray_config_init_defaults, xtk_tray_config_parse_styling, xtk_tray_config_write_styling
Types
XtkTray
typedef struct XtkTray XtkTray;
A system-tray icon (freedesktop.org protocol); opaque, made by xtk_tray_create().
Constants
XTK_TRAY_MENU_MAX
#define XTK_TRAY_MENU_MAX 16
the most items of a program's own tray menu
XTK_TRAY_OWN_ITEM
#define XTK_TRAY_OWN_ITEM 100
xtk_tray_handle_event() gives this plus an own item's index when it is chosen
Functions
xtk_tray_create
XtkTray *xtk_tray_create(
Display *display,
int screen,
const char **icon_xpm,
const void *menu_theme,
Window main_window);
Create a tray icon from XPM data and dock it in the system tray.
The call waits about 25 milliseconds between mapping the icon window and sending the dock request, because trays need that time.
| Parameter | Description |
|---|---|
display |
The X display |
screen |
The screen number |
icon_xpm |
The icon as XPM data (the array an .xpm file declares); transparent pixels in it stay transparent |
menu_theme |
The context menu's style: a pointer to a TrayMenuBlock (usually &config.tray_menu), copied. NULL gives a plain dark style (sans 12, white text). |
main_window |
The program's main window, or 0. The window item of the context menu is named after whether it is shown. |
Returns: The new tray, or NULL if display is NULL, memory runs out or the XPM data cannot be made into a pixmap (an error is printed to stderr then)
xtk_tray_create_from_pixmap
XtkTray *xtk_tray_create_from_pixmap(
Display *display,
int screen,
Pixmap icon_pixmap,
int icon_width,
int icon_height,
const void *menu_theme,
Window main_window);
Create a tray icon from a Pixmap and dock it in the system tray.
It works as xtk_tray_create() does, for a program that already has its icon as a Pixmap (for example a PNG loaded with xtk_image_load_pixmap()). The icon is drawn opaque: there is no mask.
| Parameter | Description |
|---|---|
display |
The X display; must not be NULL |
screen |
The screen number |
icon_pixmap |
The icon. It must have the default depth of the screen. The tray takes it over and frees it in xtk_tray_destroy(), and also when the tray cannot be made after the arguments were accepted; do not free or reuse it. |
icon_width |
The pixmap's width in pixels |
icon_height |
The pixmap's height in pixels |
menu_theme |
The context menu's style, as for xtk_tray_create() |
main_window |
The program's main window, or 0, as for xtk_tray_create() |
Returns: The new tray, or NULL if display is NULL, icon_pixmap is None, a size is 0 or less (the pixmap is then freed), or memory runs out
xtk_tray_destroy
void xtk_tray_destroy(XtkTray *tray);
Destroy the tray icon: its window, pixmaps, context menu and tooltip.
The tray removes the icon when its window is destroyed.
| Parameter | Description |
|---|---|
tray |
The tray (NULL does nothing) |
xtk_tray_set_menu
void xtk_tray_set_menu(
XtkTray *tray,
const char *const *labels,
int count,
int window_item);
Give the context menu the program's own items, in place of the fixed ones.
Each label is an item; "" or "---" is a separator. When one of them is chosen, xtk_tray_handle_event() returns XTK_TRAY_OWN_ITEM plus its index (and never 2 to 5). The window item, if any, is named by the main window's state each time the menu opens: "Hide Window" while it is shown, "Show Window" while it is not.
| Parameter | Description |
|---|---|
tray |
The tray |
labels |
The items' labels (copied), or NULL for the fixed menu again |
count |
How many (at most XTK_TRAY_MENU_MAX) |
window_item |
The index of the window item, or -1 for none |
xtk_tray_menu_is_open
int xtk_tray_menu_is_open(const XtkTray *tray);
Tell whether the context menu is open now.
A program that gives the menu new items as its state changes (xtk_tray_set_menu()) waits while it is open, so that the items do not change under the pointer.
| Parameter | Description |
|---|---|
tray |
The tray |
Returns: 1 while the menu shows, 0 if not or tray is NULL
xtk_tray_handle_event
int xtk_tray_handle_event(XtkTray *tray, XEvent *event);
Give an X event to the tray icon and its context menu.
Any event can be passed. A left button press on the icon closes the context menu and returns 1. A right button release opens the menu above the icon. Expose redraws the icon; pointer entry and exit start and stop the tooltip. The tooltip's own events and a tray starting or going away are handled here too and give 0.
The return values are:
- 0: nothing for the program to do (also for events on other windows)
- 1: a left click on the icon
- 2: "Pick Color" was chosen
- 3: the window item was chosen
- 4: "Copy as Hex" was chosen
- 5: "Exit" was chosen
| Parameter | Description |
|---|---|
tray |
The tray |
event |
The event |
Returns: One of the values above; 0 also if tray is NULL
xtk_tray_is_available
int xtk_tray_is_available(Display *display, int screen);
Tell whether a system tray is running on a screen.
| Parameter | Description |
|---|---|
display |
The X display |
screen |
The screen number |
Returns: 1 if a tray owns the screen's tray selection, 0 if not or display is NULL
xtk_tray_get_window
Window xtk_tray_get_window(XtkTray *tray);
Get the icon's X window.
| Parameter | Description |
|---|---|
tray |
The tray |
Returns: The icon window, or None if tray is NULL
xtk_tray_set_theme
void xtk_tray_set_theme(XtkTray *tray, const XtkConfig *theme);
Take new styles from a configuration: the context menu from its tray_menu block and the tooltip from its tooltip block.
An open context menu is closed first. Call it after a new theme is applied to the configuration.
| Parameter | Description |
|---|---|
tray |
The tray (NULL does nothing) |
theme |
The configuration (NULL does nothing) |
xtk_tray_set_tooltip
void xtk_tray_set_tooltip(XtkTray *tray, const char *text);
Set or remove the tooltip shown when the pointer rests on the icon.
A tooltip on the screen at the time shows the new text at once. It is placed beside the icon where it fits, and kept inside the screen: it moves along the screen's edge rather than going off it, and a line wider than the screen wraps.
| Parameter | Description |
|---|---|
tray |
The tray (NULL does nothing) |
text |
The tooltip text, copied (at most 1023 bytes; \n starts a new line); NULL or "" for no tooltip |
xtk_tray_update
void xtk_tray_update(XtkTray *tray);
Do the tray's regular work: show the tooltip once its delay is up.
Call it once per pass of the main loop, also when no event came.
| Parameter | Description |
|---|---|
tray |
The tray (NULL does nothing) |
xtk_tray_get_tooltip
struct XtkTooltip *xtk_tray_get_tooltip(const XtkTray *tray);
Get the tray's tooltip widget.
xtk_tray_update() already updates it; this is for a program that wants to handle it itself, for example to give it other colors.
| Parameter | Description |
|---|---|
tray |
The tray |
Returns: The tooltip, or NULL if tray is NULL or the tooltip could not be made
xtk_tray_show_notification
unsigned long xtk_tray_show_notification(XtkTray *tray, const char *text, int timeout_ms);
Ask the tray to show a notification balloon at the icon.
This uses the optional message part of the system tray protocol. Not every tray supports it, and there is no answer when a tray ignores it.
| Parameter | Description |
|---|---|
tray |
The tray |
text |
The message (UTF-8) |
timeout_ms |
How long to show it, in milliseconds; the tray may use its own time instead. 0 or less leaves it to the tray. |
Returns: A message id other than 0 (for xtk_tray_cancel_notification()), or 0 if tray or text is NULL
xtk_tray_cancel_notification
void xtk_tray_cancel_notification(XtkTray *tray, unsigned long message_id);
Ask the tray to remove a notification that is still showing.
| Parameter | Description |
|---|---|
tray |
The tray (NULL does nothing) |
message_id |
An id that xtk_tray_show_notification() returned |
xtk_tray_config_init_defaults
void xtk_tray_config_init_defaults(TrayMenuBlock *tray_menu_config);
Set a TrayMenuBlock to the default [tray-menu] values (listed above).
xtk_config_init_defaults() already calls it for config.tray_menu.
| Parameter | Description |
|---|---|
tray_menu_config |
The block to set (NULL does nothing) |
xtk_tray_config_parse_styling
int xtk_tray_config_parse_styling(
TrayMenuBlock *tray_menu_config,
const char *key,
const char *value);
Set one [tray-menu] key of a TrayMenuBlock from its text value.
The config file reader calls it for each key of the section.
| Parameter | Description |
|---|---|
tray_menu_config |
The block to change |
key |
The key, for example "hover-background" |
value |
The value as written in the file |
Returns: 1 if the key is a [tray-menu] key, 0 if not or an argument is NULL
xtk_tray_config_write_styling
int xtk_tray_config_write_styling(FILE *file, const TrayMenuBlock *tray_menu_config);
Write a TrayMenuBlock as a [tray-menu] section, with every key.
An unset hover-color is written as the color value.
| Parameter | Description |
|---|---|
file |
The file to write to |
tray_menu_config |
The block to write |
Returns: 1 on success, 0 if an argument 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