Table of Contents
- xtk_label.h
- Types
- Functions
- xtk_label_create
- xtk_label_destroy
- xtk_label_set_text
- xtk_label_set_theme
- xtk_label_set_position
- xtk_label_resize
- xtk_label_set_geometry
- xtk_label_set_transparent
- xtk_label_set_background_key
- xtk_label_set_wrap
- xtk_label_set_ellipsize
- xtk_label_set_auto_width
- xtk_label_get_text_width
- xtk_label_wrapped_height
- xtk_label_get_window
- xtk_label_get_size
- xtk_label_draw
- xtk_label_show
- xtk_label_hide
- xtk_label_set_enabled
- xtk_label_set_underline
- xtk_label_get_enabled
- xtk_label_handle_expose
- xtk_label_is_using_dbe
- xtk_label_set_cursor
- xtk_label_set_copyable
- xtk_label_handle_event
xtk_label.h
A line of text, with an optional border, that the user cannot edit.
A label shows text: a caption beside a field, a status line, a heading. It can be as wide as its text or have a fixed width, and with a fixed width it can wrap the text over several lines or cut it short with "…". It can be dimmed (disabled), underline the letter of a dialog's Alt+letter key, blend with the window it sits on (transparent), and copy its text to the clipboard when it is clicked.
Making one. xtk_label_create() takes the parent window, the position, a width (0 to fit the text), the geometry, the text and a LabelBlock with the font and colors, usually &config.label. The geometry usually comes from config.label_widget (padding, border width, border radius, and whether a border is drawn). The LabelBlock and the text are copied. The height is always worked out from the font: the font's height, plus the padding and the border on both sides, plus 2 pixels, and at least 22 pixels plus the border. The window is not mapped at first: the first call of xtk_label_set_position() maps it (unless xtk_label_hide() was called), as does xtk_label_show(). Free the label with xtk_label_destroy().
Events. Pass every X event to xtk_label_handle_event() (or only the Expose events to xtk_label_handle_expose()). The label draws itself on the last Expose of a series. xtk_label_set_text() and xtk_label_set_theme() send the label an Expose of their own, so the new text shows when the event loop passes that event on; the calls that change only how the text is drawn (xtk_label_set_wrap(), xtk_label_set_enabled() and the like) redraw at once. A copyable label (xtk_label_set_copyable()) shows a hand pointer and copies its text to the clipboard when the left button is released over it.
Theme keys. [label] (LabelBlock, config.label):
font-family(orfont): the font (default "DejaVu Sans").font-size: the font size in pixels (default 12).color: the text (default F2F2F2). A disabled label draws its text 40% of the way from background to color.background: the fill (default #2E2E2E). It can be a gradient.border: the border, when one is drawn (default fully transparent).padding,border-width,border-radius(pixels, defaults 4, 0, 0) andborder-enabled(trueorfalse, default false): the geometry a label takes whenxtk_label_create()is given -1 for that value.
The geometry of config.label_widget (padding 4, border width 0, border radius 0, no border) is set by xtk_config_init_defaults() and has no keys in the config file; a program can pass it to xtk_label_create().
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);
xtk_theme_free(theme);
XtkLabel *label = xtk_label_create(display, screen, window, 10, 10, 0,
config.label_widget.padding, config.label_widget.border_width,
config.label_widget.border_radius, config.label_widget.border_enabled,
"Name:", &config.label);
xtk_label_set_position(label, 10, 10); // maps the window
XEvent event;
for (;;) {
XNextEvent(display, &event);
xtk_label_handle_event(label, &event);
}
*
Limitations.
- A wrapped label shows at most 32 lines, does not grow to fit them, and shows no Alt+letter underline. A word wider than the label is not broken.
- Ellipsizing works on at most about 500 bytes of text.
- The height cannot be made smaller than one line of text.
#include "xtk_label.h"
Types: XtkLabel
Functions: xtk_label_create, xtk_label_destroy, xtk_label_set_text, xtk_label_set_theme, xtk_label_set_position, xtk_label_resize, xtk_label_set_geometry, xtk_label_set_transparent, xtk_label_set_background_key, xtk_label_set_wrap, xtk_label_set_ellipsize, xtk_label_set_auto_width, xtk_label_get_text_width, xtk_label_wrapped_height, xtk_label_get_window, xtk_label_get_size, xtk_label_draw, xtk_label_show, xtk_label_hide, xtk_label_set_enabled, xtk_label_set_underline, xtk_label_get_enabled, xtk_label_handle_expose, xtk_label_is_using_dbe, xtk_label_set_cursor, xtk_label_set_copyable, xtk_label_handle_event
Types
XtkLabel
typedef struct XtkLabel XtkLabel;
A text label (opaque). Made by xtk_label_create().
Functions
xtk_label_create
XtkLabel *xtk_label_create(
Display *display,
int screen,
Window parent,
int x,
int y,
int width,
int padding,
int border_width,
int border_radius,
int border_enabled,
const char *text,
const LabelBlock *theme);
Creates a label.
The window is created but not mapped; xtk_label_set_position() or xtk_label_show() maps it.
| Parameter | Description |
|---|---|
display |
The X display. |
screen |
The screen number. |
parent |
The parent window. |
x |
The x position in the parent. |
y |
The y position in the parent. |
width |
The width in pixels, or 0 to make the label as wide as its text (and to keep it so when the text or font changes). |
padding |
The space between the border and the text, in pixels; -1 for the theme's padding. |
border_width |
The width of the border in pixels; -1 for the theme's border-width. |
border_radius |
The radius of the corners in pixels; the window is shaped to match. -1 for the theme's border-radius. |
border_enabled |
1 to draw the border, 0 for none, -1 for the theme's border-enabled. Without a border, border_width takes no room. |
text |
The text, or NULL for none. It is copied. |
theme |
The font and colors, usually &config.label. It is copied. |
Returns: The new label, or NULL when display or theme is NULL or the window, font or drawing surface cannot be made.
xtk_label_destroy
void xtk_label_destroy(XtkLabel *label);
Destroys a label and its window.
| Parameter | Description |
|---|---|
label |
The label. NULL is allowed and does nothing. |
xtk_label_set_text
void xtk_label_set_text(XtkLabel *label, const char *text);
Changes the text.
A label made with width 0 (or set to it with xtk_label_set_auto_width()) is resized to fit the new text. The label is drawn again when the Expose this call sends reaches xtk_label_handle_event().
| Parameter | Description |
|---|---|
label |
The label. |
text |
The new text, or NULL for none. It is copied. |
xtk_label_set_theme
void xtk_label_set_theme(XtkLabel *label, const LabelBlock *theme);
Changes the font and colors.
The block is copied. An auto-width label is resized to fit its text in the new font, and the height follows the new font. The label is drawn again on the next Expose, which this call sends when it does not resize.
| Parameter | Description |
|---|---|
label |
The label. |
theme |
The new font and colors. NULL does nothing. |
xtk_label_set_position
void xtk_label_set_position(XtkLabel *label, int x, int y);
Moves the label, and maps it the first time.
The first call maps the window, unless xtk_label_hide() was called before.
| Parameter | Description |
|---|---|
label |
The label. |
x |
The new x position in the parent. |
y |
The new y position in the parent. |
xtk_label_resize
void xtk_label_resize(XtkLabel *label, int width, int height);
Resizes the label.
The size is never made smaller than the text needs: the width is at least that of the text on one line (except while wrapping is on, see xtk_label_set_wrap()), and the height at least one line. The X server's Expose after the resize draws the label again.
| Parameter | Description |
|---|---|
label |
The label. |
width |
The new width in pixels, or 0 for the width of the text. |
height |
The new height in pixels, or 0 for the height of one line. |
xtk_label_set_geometry
void xtk_label_set_geometry(
XtkLabel *label,
int padding,
int border_width,
int border_radius,
int border_enabled);
Changes the padding and border.
When something changed, the height (and, for an auto-width label, the width) is worked out again, as at creation. The label is drawn again on the next Expose.
| Parameter | Description |
|---|---|
label |
The label. |
padding |
The space between the border and the text, in pixels. |
border_width |
The width of the border in pixels. |
border_radius |
The radius of the corners in pixels. |
border_enabled |
1 to draw the border, 0 for none. |
xtk_label_set_transparent
void xtk_label_set_transparent(XtkLabel *label, int transparent);
Makes the label show the window behind it instead of its background.
A transparent label draws no background or border, only its text, over whatever its parent window shows there. Use it for plain captions that should blend with any container. Keep a label opaque (the default) when its own background matters, as in a colored status chip or a table header. The label is drawn again at once.
| Parameter | Description |
|---|---|
label |
The label. |
transparent |
1 for transparent, 0 for the background color of the theme (the default). |
xtk_label_set_background_key
void xtk_label_set_background_key(XtkLabel *label, const char *section, const char *key);
Sets the theme key the background's gradient is taken from.
When the theme gives that key a gradient (see xtk_paint.h), the label fills its background with it. Use it for a label that wears another widget's colors, such as a table header with the [datagrid] key header-background. The label is drawn again at once.
| Parameter | Description |
|---|---|
label |
The label. |
section |
The section, without brackets ("label" by default). |
key |
The key ("background" by default). |
xtk_label_set_wrap
void xtk_label_set_wrap(XtkLabel *label, int wrap);
Turns word wrapping on or off.
With wrapping on, text wider than the label is broken at spaces over several lines, drawn from the top. This needs a fixed width: it does nothing on an auto-width label. The label does not grow to fit the lines; size it with xtk_label_wrapped_height() and xtk_label_resize(). Lines that do not fit are not drawn. The label is drawn again at once.
| Parameter | Description |
|---|---|
label |
The label. |
wrap |
1 to wrap, 0 for a single line. |
xtk_label_set_ellipsize
void xtk_label_set_ellipsize(XtkLabel *label, int ellipsize);
Turns ellipsizing on or off.
With ellipsizing on, a single line that is wider than the label is cut and ends in "…". It needs a fixed width, as wrapping does, and it is not used while wrapping is on. The label is drawn again at once.
| Parameter | Description |
|---|---|
label |
The label. |
ellipsize |
1 to ellipsize, 0 to let the text run past the edge. |
xtk_label_set_auto_width
void xtk_label_set_auto_width(XtkLabel *label, int auto_width);
Turns fit-to-text width on or off.
This is what a width of 0 at creation turns on. The change takes effect on the next text, theme or geometry change (xtk_label_set_geometry()).
| Parameter | Description |
|---|---|
label |
The label. |
auto_width |
1 to make the label as wide as its text; 0 to keep the width it is given (which wrapping and ellipsizing need). |
xtk_label_get_text_width
int xtk_label_get_text_width(const XtkLabel *label);
Gives the width the text needs on one line.
It includes the padding and the border: it is the width an auto-width label gives itself.
| Parameter | Description |
|---|---|
label |
The label, or NULL. |
Returns: The width in pixels, or 0 when label is NULL.
xtk_label_wrapped_height
int xtk_label_wrapped_height(XtkLabel *label, int width);
Gives the height the text needs when it is wrapped at a width.
It wraps the text the way xtk_label_set_wrap() draws it. To show every line of a wrapping label, resize it to this height at that width.
| Parameter | Description |
|---|---|
label |
The label, or NULL. |
width |
The width of the label to wrap at, in pixels. |
Returns: The height in pixels (the lines plus the padding), or 0 when label is NULL.
xtk_label_get_window
Window xtk_label_get_window(const XtkLabel *label);
Gives the label's window.
| Parameter | Description |
|---|---|
label |
The label, or NULL. |
Returns: The window, or None when label is NULL.
xtk_label_get_size
void xtk_label_get_size(const XtkLabel *label, int *w, int *h);
Gives the label's size.
| Parameter | Description |
|---|---|
label |
The label, or NULL (which gives 0 by 0). |
w |
Receives the width in pixels, or NULL. |
h |
Receives the height in pixels, or NULL. |
xtk_label_draw
void xtk_label_draw(XtkLabel *label);
Draws the label now.
The label normally draws on Expose. Call this when it must show before the first Expose arrives, for example after making many widgets at once.
| Parameter | Description |
|---|---|
label |
The label. NULL does nothing. |
xtk_label_show
void xtk_label_show(XtkLabel *label);
Maps the label's window.
| Parameter | Description |
|---|---|
label |
The label. |
xtk_label_hide
void xtk_label_hide(XtkLabel *label);
Unmaps the label's window.
A hidden label stays hidden through xtk_label_set_position() until xtk_label_show() is called.
| Parameter | Description |
|---|---|
label |
The label. |
xtk_label_set_enabled
void xtk_label_set_enabled(XtkLabel *label, int enabled);
Enables or disables the label.
A disabled label draws its text dimmed and does not copy on click (see xtk_label_set_copyable()). The label is drawn again at once.
| Parameter | Description |
|---|---|
label |
The label. |
enabled |
1 to enable it, 0 to disable it. |
xtk_label_set_underline
void xtk_label_set_underline(XtkLabel *label, char letter);
Underlines the letter of a dialog's Alt+letter key in the text.
Use it on the label that names a field, to show the key that moves to the field. It only draws the mark: the dialog handles the key. It is not drawn on wrapped text. The label is drawn again at once.
| Parameter | Description |
|---|---|
label |
The label. |
letter |
The letter, in either case, or 0 for none. |
xtk_label_get_enabled
int xtk_label_get_enabled(const XtkLabel *label);
Tells whether the label is enabled.
| Parameter | Description |
|---|---|
label |
The label, or NULL. |
Returns: 1 if it is enabled (the default), 0 if it is disabled or label is NULL.
xtk_label_handle_expose
int xtk_label_handle_expose(XtkLabel *label, const XExposeEvent *expose_event);
Handles an Expose event.
The label draws itself when the event is the last of a series (its count is 0).
| Parameter | Description |
|---|---|
label |
The label. |
expose_event |
The event, or NULL to draw the label now. |
Returns: 1 if the event was for the label's window (or expose_event was NULL), 0 if it was for another window or label is NULL.
xtk_label_is_using_dbe
int xtk_label_is_using_dbe(const XtkLabel *label);
Tells whether the label draws through an Xdbe back buffer.
| Parameter | Description |
|---|---|
label |
The label, or NULL. |
Returns: 1 if it uses an Xdbe back buffer, 0 if it uses a pixmap, draws straight to its window (a transparent label), or label is NULL.
xtk_label_set_cursor
void xtk_label_set_cursor(XtkLabel *label, unsigned int cursor_shape);
Sets the pointer shape over the label.
| Parameter | Description |
|---|---|
label |
The label. |
cursor_shape |
A shape from <X11/cursorfont.h>, such as XC_hand2. |
xtk_label_set_copyable
void xtk_label_set_copyable(XtkLabel *label, int copyable, void *clipboard_context);
Makes a click on the label copy its text to the clipboard.
When it is on, the label shows a hand pointer, and releasing the left button over it puts its text on the CLIPBOARD selection. Turning it off restores the default pointer.
| Parameter | Description |
|---|---|
label |
The label. |
copyable |
1 to copy on click, 0 not to. |
clipboard_context |
The XtkClipboard to copy through (see xtk_clipboard.h). |
xtk_label_handle_event
int xtk_label_handle_event(XtkLabel *label, XEvent *event);
Handles one X event.
It handles Expose (as xtk_label_handle_expose() does) and, for an enabled copyable label, the left button.
| Parameter | Description |
|---|---|
label |
The label. |
event |
The event. |
Returns: 1 if the label used the event, 0 if not (an event for another window, or one the label has no use for).
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