Table of Contents
- xtk_swatch.h
- Types
- Constants
- Functions
- xtk_swatch_create
- xtk_swatch_destroy
- xtk_swatch_get_window
- xtk_swatch_is_attached
- xtk_swatch_handle_event
- xtk_swatch_set_color
- xtk_swatch_set_color_noflush
- xtk_swatch_set_drag_anchor_corner
- xtk_swatch_get_color
- xtk_swatch_set_background
- xtk_swatch_set_position
- xtk_swatch_get_size
- xtk_swatch_set_border
- xtk_swatch_set_theme
- xtk_swatch_resize
- xtk_swatch_set_drag_callback
- xtk_swatch_set_draw_callback
- xtk_swatch_redraw
- xtk_swatch_set_alpha
- xtk_swatch_set_alpha_noflush
- xtk_swatch_begin_drag
- xtk_swatch_set_border_mode
- xtk_swatch_config_init_defaults
- xtk_swatch_config_parse_styling
- xtk_swatch_config_write_styling
- xtk_swatch_widget_config_init_defaults
- xtk_swatch_widget_config_parse
- xtk_swatch_widget_config_write
xtk_swatch.h
A color well that shows one color and can be dragged to drop it somewhere.
A swatch is a small child window filled with one color, with rounded corners and a border whose color is chosen to stand out from both the swatch color and the background behind it. A color picker uses it to show the current color, and a palette uses rows of them. The user can drag a swatch with the first mouse button and drop it anywhere on the screen; the program learns where through a drag callback and decides what the drop means (for example, set the color of the widget under the pointer).
Making one. xtk_swatch_create() makes the window as a child of parent, with the given size. The window is not shown until the first xtk_swatch_set_position(). The program takes the size from config.swatch_widget.width and .height and calls xtk_swatch_set_theme() with the config, which applies the border width, radius, mode and color (without it the border is 1 pixel wide with a radius of 4). Then it sets the color with xtk_swatch_set_color() and, for the border's contrast test, the color behind the swatch with xtk_swatch_set_background(). The swatch owns its window; xtk_swatch_destroy() destroys it.
Drawing. The swatch draws itself (through a double buffer when the DBE extension is there) on Expose and whenever a setter changes how it looks. Its window background is kept at the swatch color so that the server fills newly exposed parts at once. When the alpha set with xtk_swatch_set_alpha() is below 255, the inside is a checkerboard blended with the color. xtk_swatch_set_draw_callback() replaces all of this with the program's own drawing.
Border color. While the swatch is in its parent, the border is:
- black or white, whichever stands out from the background, when the swatch color is close to the background color (their RGB distance is less than 0.25);
- else a color made from the swatch color by the border mode: its complement, its inverse, black or white for contrast, or a triadic neighbor. The mode is the one set with
xtk_swatch_set_border_mode()orxtk_swatch_set_theme(), or else the shared mode ofxtk_config_get_border_mode()(complementary unless the program callsxtk_config_set_border_mode(), or a config or theme file sets[swatch]border-mode).
While the swatch is dragged, the border is the [swatch] border color (given with xtk_swatch_set_theme(); #897C4F by default).
Events. Pass every event to xtk_swatch_handle_event(). It returns 1 for an event it used: an Expose of its window, a press or release of the first button on its window, and pointer motion while a drag is under way. A press starts a drag: the window moves to the root window under the pointer, the pointer is hidden and grabbed, and the drag callback is called with each motion. On release the window goes back to its place in the parent, the grab ends, and the drag callback is called with is_drop set to 1 and the pointer's root position. A press and release without motion is a drop at the press position, so a program that wants a plain click can test whether the drop is inside the swatch. The swatch takes no keyboard focus.
Theme keys. The swatch's settings live in two sections, applied with xtk_swatch_set_theme().
[swatch]:border(a color; default #897C4F;xtk_theme_apply()sets it from the theme's border role),border-mode(complementary,inverse,contrastortriadic; default: the shared mode, which iscomplementary; stored inconfig.swatch_border_mode, and made the shared mode when a file sets it),border-widthandborder-radius(the same fields as in[swatch-widget]).[swatch-widget]:width(default 74),height(default 74),border-width(default 1) andborder-radius(default 4), in pixels.[sizes]also takesswatch-widthandswatch-heightfor the same width and height.
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);
XtkSwatch *swatch = xtk_swatch_create(display, window,
config.swatch_widget.width, config.swatch_widget.height);
xtk_swatch_set_theme(swatch, &config);
xtk_swatch_set_background(swatch, config.main.background);
xtk_swatch_set_color(swatch, xtk_config_parse_color("#3A7BD5"));
xtk_swatch_set_position(swatch, 10, 10);
for (;;) {
XEvent event;
XNextEvent(display, &event);
if (xtk_swatch_handle_event(swatch, &event, window)) continue;
// ... other widgets
}
xtk_swatch_destroy(swatch);
xtk_theme_free(theme);
*
Limitations.
- Rounded corners need the X Shape extension.
#include "xtk_swatch.h"
Types: XtkSwatch, XtkSwatchDragCallback, XtkSwatchDrawCallback
Constants: XTK_SWATCH_WIDTH, XTK_SWATCH_HEIGHT
Functions: xtk_swatch_create, xtk_swatch_destroy, xtk_swatch_get_window, xtk_swatch_is_attached, xtk_swatch_handle_event, xtk_swatch_set_color, xtk_swatch_set_color_noflush, xtk_swatch_set_drag_anchor_corner, xtk_swatch_get_color, xtk_swatch_set_background, xtk_swatch_set_position, xtk_swatch_get_size, xtk_swatch_set_border, xtk_swatch_set_theme, xtk_swatch_resize, xtk_swatch_set_drag_callback, xtk_swatch_set_draw_callback, xtk_swatch_redraw, xtk_swatch_set_alpha, xtk_swatch_set_alpha_noflush, xtk_swatch_begin_drag, xtk_swatch_set_border_mode, xtk_swatch_config_init_defaults, xtk_swatch_config_parse_styling, xtk_swatch_config_write_styling, xtk_swatch_widget_config_init_defaults, xtk_swatch_widget_config_parse, xtk_swatch_widget_config_write
Types
XtkSwatch
typedef struct XtkSwatch XtkSwatch;
A color swatch widget (opaque).
Make one with xtk_swatch_create() and free it with xtk_swatch_destroy().
XtkSwatchDragCallback
typedef void (*XtkSwatchDragCallback)(XtkSwatch *swatch, int rx, int ry, int is_drop, void *user_data);
A function the swatch calls while it is dragged and when it is dropped.
XtkSwatchDrawCallback
typedef void (*XtkSwatchDrawCallback)(XtkSwatch *swatch, Display *dpy, Drawable target, GC gc, int w, int h, void *user_data);
A function that draws the swatch in place of its normal drawing.
It is called on every redraw. It must cover the whole w x h area; the swatch shows the result (and swaps the double buffer) after it returns.
Constants
XTK_SWATCH_WIDTH
#define XTK_SWATCH_WIDTH 74
The default width of a swatch in pixels, the same as the [swatch-widget] default.
XTK_SWATCH_HEIGHT
#define XTK_SWATCH_HEIGHT 74
The default height of a swatch in pixels, the same as the [swatch-widget] default.
Functions
xtk_swatch_create
XtkSwatch *xtk_swatch_create(Display *display, Window parent, int width, int height);
Creates a swatch as a child window of parent.
The window is made off screen and is not mapped; the first xtk_swatch_set_position() moves and maps it. The color starts as transparent black (all zeros), the alpha at 255, the border 1 pixel wide with a radius of 4. The swatch uses the display's default screen.
| Parameter | Description |
|---|---|
display |
The display connection. |
parent |
The window the swatch sits in. A drop puts it back here. |
width |
The width in pixels. |
height |
The height in pixels. |
Returns: The new swatch, which the caller frees with xtk_swatch_destroy(); NULL when display is NULL, memory runs out or the window cannot be made.
xtk_swatch_destroy
void xtk_swatch_destroy(XtkSwatch *swatch);
Destroys a swatch's window and frees the swatch.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
xtk_swatch_get_window
Window xtk_swatch_get_window(XtkSwatch *swatch);
Gives the swatch's X window.
| Parameter | Description |
|---|---|
swatch |
The swatch. |
Returns: The window; None when swatch is NULL.
xtk_swatch_is_attached
int xtk_swatch_is_attached(const XtkSwatch *swatch);
Tells if the swatch is in its parent window, or on the root window while it is dragged.
This reads the swatch's own state, so it is much cheaper than asking the server with XQueryTree().
| Parameter | Description |
|---|---|
swatch |
The swatch. |
Returns: 1 when the swatch is in its parent; 0 while it is dragged, or when swatch is NULL.
xtk_swatch_handle_event
int xtk_swatch_handle_event(XtkSwatch *swatch, const XEvent *event, Window main_window);
Gives an event to the swatch.
See the file description for what each event does.
| Parameter | Description |
|---|---|
swatch |
The swatch. |
event |
The event. |
main_window |
Not used; pass the program's main window or None. |
Returns: 1 when the swatch used the event: an Expose of its window, a press or release of the first button on its window, or pointer motion during a drag. 0 for every other event, and when swatch or event is NULL.
xtk_swatch_set_color
void xtk_swatch_set_color(XtkSwatch *swatch, ConfigColor color);
Sets the swatch's color, redraws it and flushes the display.
The color's alpha is not drawn; use xtk_swatch_set_alpha() to show transparency.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
color |
The color. xtk_swatch_get_color() gives it back unchanged. |
xtk_swatch_set_color_noflush
void xtk_swatch_set_color_noflush(XtkSwatch *swatch, ConfigColor color);
Sets the swatch's color and redraws it, without flushing the display.
Use it to change many swatches at once, for example a whole palette row, and call XFlush() one time after the loop.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
color |
The color. |
xtk_swatch_set_drag_anchor_corner
void xtk_swatch_set_drag_anchor_corner(XtkSwatch *swatch, int corner);
Chooses where the pointer holds a dragged swatch.
By default the pointer holds the swatch at its center. With corner set, the swatch hangs below and to the right of the pointer, so that a large swatch does not hide the place it is dropped on.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
corner |
Nonzero to hold the swatch by its top left corner, 0 by its center. |
xtk_swatch_get_color
ConfigColor xtk_swatch_get_color(const XtkSwatch *swatch);
Gives the swatch's color.
| Parameter | Description |
|---|---|
swatch |
The swatch. |
Returns: The color last given to xtk_swatch_set_color(); all zeros when swatch is NULL or no color was set.
xtk_swatch_set_background
void xtk_swatch_set_background(XtkSwatch *swatch, ConfigColor background);
Sets the color behind the swatch, for choosing the border color.
When the swatch color is close to this color, the border becomes black or white so that the swatch stays visible. The swatch is redrawn and the display flushed.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
background |
The color of the parent window behind the swatch. |
xtk_swatch_set_position
void xtk_swatch_set_position(XtkSwatch *swatch, int x, int y);
Moves the swatch in its parent and maps it.
Nothing happens during a drag, or when the position is the same as before.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
x |
The new X position in the parent. |
y |
The new Y position in the parent. |
xtk_swatch_get_size
void xtk_swatch_get_size(const XtkSwatch *swatch, int *width, int *height);
Gives the swatch's size.
| Parameter | Description |
|---|---|
swatch |
The swatch. When it is NULL, both sizes are set to 0. |
width |
Receives the width in pixels; may be NULL. |
height |
Receives the height in pixels; may be NULL. |
xtk_swatch_set_border
void xtk_swatch_set_border(XtkSwatch *swatch, int border_width, int border_radius);
Sets the border width and the corner radius, and redraws.
A width of 0 or less draws no border: the whole swatch is the color. A radius of 0 or less gives square corners. The window's shape is updated for the new radius and the display is flushed.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
border_width |
The border width in pixels. |
border_radius |
The corner radius in pixels. |
xtk_swatch_set_theme
void xtk_swatch_set_theme(XtkSwatch *swatch, const XtkConfig *config);
Applies a config's swatch settings, and redraws.
The border width and radius of [swatch-widget], and the border color (used while the swatch is dragged) and border-mode of [swatch] (as this swatch's own mode, as xtk_swatch_set_border_mode() sets it).
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
config |
The configuration. NULL does nothing. |
xtk_swatch_resize
void xtk_swatch_resize(XtkSwatch *swatch, int width, int height);
Changes the swatch's size and redraws it.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
width |
The new width in pixels; 0 or less does nothing. |
height |
The new height in pixels; 0 or less does nothing. |
xtk_swatch_set_drag_callback
void xtk_swatch_set_drag_callback(
XtkSwatch *swatch,
XtkSwatchDragCallback callback,
void *user_data);
Sets the function that is called during a drag and on the drop.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
callback |
The function, or NULL for none. |
user_data |
A pointer passed to the function. |
xtk_swatch_set_draw_callback
void xtk_swatch_set_draw_callback(
XtkSwatch *swatch,
XtkSwatchDrawCallback callback,
void *user_data);
Sets a function that draws the swatch in place of the color and border.
The new function is used from the next redraw on; call xtk_swatch_redraw() to draw at once.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
callback |
The function, or NULL for the normal drawing. |
user_data |
A pointer passed to the function. |
xtk_swatch_redraw
void xtk_swatch_redraw(XtkSwatch *swatch);
Redraws the swatch at once and flushes the display.
Use it after changing data that a draw callback reads.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
xtk_swatch_set_alpha
void xtk_swatch_set_alpha(XtkSwatch *swatch, unsigned int alpha);
Sets the swatch's alpha, redraws it and flushes the display.
Below 255, the inside of the swatch is a checkerboard of light and dark gray squares blended with the color, so the user sees the transparency.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
alpha |
0 (transparent) to 255 (opaque, the default); larger values are taken as 255. |
xtk_swatch_set_alpha_noflush
void xtk_swatch_set_alpha_noflush(XtkSwatch *swatch, unsigned int alpha);
Sets the swatch's alpha and redraws it, without flushing the display.
See xtk_swatch_set_color_noflush() for when to use it.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
alpha |
0 (transparent) to 255 (opaque); larger values are taken as 255. |
xtk_swatch_begin_drag
void xtk_swatch_begin_drag(XtkSwatch *swatch);
Starts a drag of the swatch from the pointer's position now.
Call it when a button press on the parent window falls on the swatch, for example when a larger widget draws over the swatch's area. The swatch moves to the root window and grabs the pointer, as if the user had pressed on it, and the drag callback is called once with is_drop 0. When the grab fails the swatch stays where it is. Nothing happens during a drag.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
xtk_swatch_set_border_mode
void xtk_swatch_set_border_mode(XtkSwatch *swatch, BorderMode mode);
Sets the border mode for this swatch only.
Without this call the swatch uses the shared mode of xtk_config_get_border_mode(). With it, this swatch always uses the given mode, for example to show several modes side by side. The swatch is redrawn and the display flushed.
| Parameter | Description |
|---|---|
swatch |
The swatch. NULL does nothing. |
mode |
How the border color is made from the swatch color. |
xtk_swatch_config_init_defaults
void xtk_swatch_config_init_defaults(XtkConfig *config);
Sets the defaults of the [swatch] section in a config.
It sets config->swatch.border to #897C4F and config->swatch_border_mode to the shared mode of xtk_config_get_border_mode() (complementary unless the program changed it). xtk_config_init_defaults() calls it.
| Parameter | Description |
|---|---|
config |
The config. NULL does nothing. |
xtk_swatch_config_parse_styling
int xtk_swatch_config_parse_styling(
XtkConfig *config,
const char *key,
const char *value);
Reads one key of the [swatch] section into a config.
The keys are border, border-mode, border-width and border-radius. An unknown border-mode value gives contrast. A border-mode also becomes the shared mode (xtk_config_set_border_mode()).
| Parameter | Description |
|---|---|
config |
The config to change. |
key |
The key. |
value |
The value as text. |
Returns: 1 when the key was known and stored; 0 when the key is not one of this section or an argument is NULL (the convention of every section handler, see xtk_config_registry.h).
xtk_swatch_config_write_styling
int xtk_swatch_config_write_styling(FILE *file, const XtkConfig *config);
Writes the [swatch] section of a config to a file.
| Parameter | Description |
|---|---|
file |
The open file to write to. |
config |
The config. |
Returns: 0 when it was written; -1 when an argument is NULL.
xtk_swatch_widget_config_init_defaults
void xtk_swatch_widget_config_init_defaults(XtkConfig *config);
Sets the defaults of the [swatch-widget] section in a config.
Width and height 74, border width 1 and border radius 4, in pixels.
| Parameter | Description |
|---|---|
config |
The config. NULL does nothing. |
xtk_swatch_widget_config_parse
int xtk_swatch_widget_config_parse(XtkConfig *config, const char *key, const char *value);
Reads one key of the [swatch-widget] section into a config.
The keys are width, height, border-width and border-radius, all whole numbers of pixels.
| Parameter | Description |
|---|---|
config |
The config to change. |
key |
The key. |
value |
The value as text. |
Returns: 1 when the key was known and stored; 0 when the key is not one of this section or an argument is NULL (the convention of every section handler, see xtk_config_registry.h).
xtk_swatch_widget_config_write
int xtk_swatch_widget_config_write(FILE *file, const XtkConfig *config);
Writes the [swatch-widget] section of a config to a file.
| Parameter | Description |
|---|---|
file |
The open file to write to. |
config |
The config. |
Returns: 0 when it was written; -1 when 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