1 xtk_draw
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_draw.h

Drawing shapes for widgets: rounded rectangles, circles, window shapes, smooth lines.

Every widget of the toolkit draws through these functions. Shapes are built as polygons from X11 primitives: fills use XFillPolygon() and outlines use XDrawLines(), in the GC's current color, so a fill and an outline drawn on top of each other line up the same way every time. Rounded corners are drawn with 32 line segments per quarter circle.

The header has several parts:

  • Standalone shapes (xtk_fill_*, xtk_stroke_*): one call draws one shape with a Display, a drawable and a GC. Set the GC's foreground first. xtk_fill_bordered_rect() draws a border and its inside in two fills.
  • A path context (XtkDrawContext): build a path with move, line and arc steps, then stroke or fill it in the context's colors, as in Cairo.
  • Window shapes: clip a window to a rounded rectangle, a circle or a tooltip outline with the X Shape extension, so the corners outside the shape are not part of the window.
  • Anti-aliased shapes (xtk_draw_aa_*): smooth lines and circles drawn with XRender, for a display that has it.
  • Shared-memory images (XtkShmImage): a pixel buffer shared with the X server through MIT-SHM, for large images and gradients.
  • Text helpers: the baseline that centers text, and the underline of a mnemonic letter.

Example. A bordered, rounded box with a line under it:

unsigned long border = xtk_config_color_to_pixel(display, screen, config.button.border);
unsigned long fill = xtk_config_color_to_pixel(display, screen, config.button.background);
xtk_fill_bordered_rect(display, target, gc, 0, 0, 120, 32, 6, 1, border, fill);

XSetForeground(display, gc, border);
xtk_draw_line(display, target, gc, 0, 40, 119, 40);
 * 

Limitations.

  • When radius is more than half the width or height, it is made smaller to fit: half the shorter side.
  • Every fill of a box x, y, w, h covers the pixels x to x + w - 1 and y to y + h - 1, whether it is drawn with the path context, xtk_fill_rect(), xtk_fill_rounded_rect(), a selective fill or a tooltip shape.
  • Coordinates are stored in an XPoint, so they must fit in a short.
#include "xtk_draw.h"

Types: XtkDrawContext, XtkTooltipArrowDirection, XtkShmImage

Constants: XTK_PATH_MAX_POINTS, XTK_ARC_SEGMENTS, XTK_CIRCLE_SEGMENTS

Functions: xtk_draw_init, xtk_draw_set_target, xtk_draw_set_stroke_color, xtk_draw_set_fill_color, xtk_draw_set_line_width, xtk_draw_new_path, xtk_draw_move_to, xtk_draw_line_to, xtk_draw_line, xtk_mnemonic_index, xtk_draw_mnemonic_underline, xtk_draw_arc, xtk_draw_close_path, xtk_draw_stroke, xtk_draw_fill, xtk_draw_fill_stroke, xtk_draw_rounded_rect, xtk_draw_circle, xtk_draw_rect, xtk_draw_fill_rounded_rect, xtk_draw_stroke_rounded_rect, xtk_draw_fill_circle, xtk_draw_stroke_circle, xtk_draw_bordered_rect, xtk_draw_apply_shape, xtk_draw_apply_shape_selective_tb, xtk_draw_apply_circle_shape, xtk_draw_apply_tooltip_shape, xtk_fill_tooltip_shape, xtk_fill_bordered_tooltip_shape, xtk_text_centered_baseline, xtk_build_rounded_rect_path, xtk_build_circle_path, xtk_fill_rounded_rect, xtk_fill_circle, xtk_stroke_arc, xtk_stroke_ellipse, xtk_fill_bordered_rect, xtk_clear_window, xtk_clear_area, xtk_fill_rect, xtk_stroke_rect, xtk_stroke_rounded_rect, xtk_fill_polygon, xtk_draw_polyline, xtk_fill_rounded_rect_selective, xtk_fill_rounded_rect_selective_lr, xtk_draw_aa_available, xtk_draw_aa_line, xtk_draw_aa_fill_circle, xtk_draw_aa_stroke_circle, xtk_shm_available, xtk_shm_image_create, xtk_shm_image_destroy, xtk_shm_image_data, xtk_shm_image_size, xtk_shm_image_put, xtk_shm_image_resize, xtk_draw_gradient_hline, xtk_draw_gradient_vline

Types

XtkDrawContext

A drawing context: a target, a path being built, and the colors and line width it is drawn with.

Usually a local variable, filled in with xtk_draw_init(). It holds no server resources and needs no freeing.

Field Type Description
display Display * The display connection.
screen int The screen number.
target Drawable The drawable paths are drawn on.
gc GC The GC drawing uses; strokes and fills change its foreground and line attributes.
path[XTK_PATH_MAX_POINTS] XPoint The current path's points.
path_count int The number of points in path.
stroke_color unsigned long The pixel value outlines are drawn in.
fill_color unsigned long The pixel value fills are drawn in.
line_width int The outline width in pixels.

XtkTooltipArrowDirection

Which edge a tooltip's arrow sticks out of.

The arrow points at the tooltip's target, so it is on the side facing it.

Name Value Description
XTK_TOOLTIP_ARROW_LEFT 0 On the left edge, pointing left (the tooltip is to the right of its target)
XTK_TOOLTIP_ARROW_RIGHT 1 On the right edge, pointing right (the tooltip is to the left of its target)
XTK_TOOLTIP_ARROW_UP 2 On the top edge, pointing up (the tooltip is below its target)
XTK_TOOLTIP_ARROW_DOWN 3 On the bottom edge, pointing down (the tooltip is above its target)

XtkShmImage

typedef struct XtkShmImage XtkShmImage;

A pixel buffer shared with the X server (MIT-SHM); opaque.

The program writes pixels straight into the buffer and xtk_shm_image_put() shows them, without sending the pixel data through the X connection. It only works when the program and the server are on the same machine.

Constants

XTK_PATH_MAX_POINTS

#define XTK_PATH_MAX_POINTS 256

The most points a path holds; a rounded rectangle needs about 140.

XTK_ARC_SEGMENTS

#define XTK_ARC_SEGMENTS 32

Line segments per quarter circle (the corner of a rounded rectangle).

XTK_CIRCLE_SEGMENTS

#define XTK_CIRCLE_SEGMENTS 64

Line segments per full circle.

Functions

xtk_draw_init

void xtk_draw_init(
    XtkDrawContext *context,
    Display *display,
    int screen,
    Drawable target,
    GC gc);

Set up a drawing context for a drawable.

The path starts empty, the line width is 1, the stroke color is the screen's black pixel and the fill color its white pixel.

Parameter Description
context The context to fill in
display The X connection
screen The screen number
target The window, pixmap or back buffer to draw on
gc The GC to draw with

xtk_draw_set_target

void xtk_draw_set_target(XtkDrawContext *context, Drawable target);

Change the drawable a context draws on, for example to a back buffer.

Parameter Description
context The drawing context
target The new drawable

xtk_draw_set_stroke_color

void xtk_draw_set_stroke_color(XtkDrawContext *context, unsigned long color);

Set the color outlines are drawn in.

Parameter Description
context The drawing context
color The pixel value, for example from xtk_config_color_to_pixel()

xtk_draw_set_fill_color

void xtk_draw_set_fill_color(XtkDrawContext *context, unsigned long color);

Set the color fills are drawn in.

Parameter Description
context The drawing context
color The pixel value

xtk_draw_set_line_width

void xtk_draw_set_line_width(XtkDrawContext *context, int width);

Set the width of outlines.

Parameter Description
context The drawing context
width The line width in pixels; values below 1 become 1

xtk_draw_new_path

void xtk_draw_new_path(XtkDrawContext *context);

Empty the current path.

Parameter Description
context The drawing context

xtk_draw_move_to

void xtk_draw_move_to(XtkDrawContext *context, int x, int y);

Add a point to the path, where it starts.

A path holds one run of points; this adds a point exactly as xtk_draw_line_to() does, so call xtk_draw_new_path() first to start a new shape. Points past XTK_PATH_MAX_POINTS are dropped.

Parameter Description
context The drawing context
x The X coordinate
y The Y coordinate

xtk_draw_line_to

void xtk_draw_line_to(XtkDrawContext *context, int x, int y);

Add a line from the last point to a new point.

Parameter Description
context The drawing context
x The X coordinate of the end point
y The Y coordinate of the end point

xtk_draw_line

void xtk_draw_line(
    Display *display,
    Drawable drawable,
    GC gc,
    int x1,
    int y1,
    int x2,
    int y2);

Draw a straight line at once, in the GC's color and line width (XDrawLine()).

Parameter Description
display The X connection
drawable Where to draw
gc The GC (its color and line width are used)
x1 The start's X
y1 The start's Y
x2 The end's X
y2 The end's Y

xtk_mnemonic_index

int xtk_mnemonic_index(const char *text, int length, char letter);

Where a mnemonic letter is in a label's text.

Case is ignored. The letter chosen is the first one that starts a word; else the first one after the first word; else the first one at all.

Parameter Description
text The text (UTF-8)
length Its length in bytes
letter The mnemonic letter, an ASCII character

Returns: The byte index of the letter in text, or -1 when letter is 0, text is NULL or the letter is not in text

xtk_draw_mnemonic_underline

void xtk_draw_mnemonic_underline(
    Display *display,
    XftDraw *draw,
    const XftColor *color,
    XftFont *font,
    const char *text,
    int length,
    char letter,
    int x,
    int baseline);

Underline a label's mnemonic letter.

Draws the mark under the letter xtk_mnemonic_index() finds, for text that has already been drawn at x on baseline. The line is 2 pixels below the baseline, 1 pixel thick (2 for a font 28 pixels high or more). Draws nothing when letter is 0 or not in text.

Parameter Description
display The X connection
draw The XftDraw the text was drawn with
color The text's color
font The text's font
text The text (UTF-8)
length Its length in bytes
letter The mnemonic letter
x Where the text starts
baseline The text's baseline

xtk_draw_arc

void xtk_draw_arc(
    XtkDrawContext *context,
    int cx,
    int cy,
    int radius,
    double start_angle,
    double end_angle);

Add an arc to the path.

Adds the points of a circular arc from start_angle to end_angle, with up to 32 segments. Angles grow clockwise on screen (Y points down), with 0 at 3 o'clock.

Parameter Description
context The drawing context
cx The center's X
cy The center's Y
radius The radius
start_angle The start angle in radians
end_angle The end angle in radians

xtk_draw_close_path

void xtk_draw_close_path(XtkDrawContext *context);

Close the path: add a line back to its first point.

Parameter Description
context The drawing context

xtk_draw_stroke

void xtk_draw_stroke(XtkDrawContext *context);

Draw the path's outline in the stroke color and line width.

Only the outline is drawn, with round caps and joins; the inside is left as it is, so it is safe after the content has been drawn. Needs at least two points.

Parameter Description
context The drawing context

xtk_draw_fill

void xtk_draw_fill(XtkDrawContext *context);

Fill the path's inside in the fill color.

Needs at least three points. The path may be concave. The path's own points are pixels, and the fill covers them too (it ends with a one-pixel outline in the fill color), so a filled rectangle covers x to x + w - 1, as xtk_fill_rect() does. It sets the GC's line width to 0.

Parameter Description
context The drawing context

xtk_draw_fill_stroke

void xtk_draw_fill_stroke(XtkDrawContext *context);

Fill the path, then draw its outline: the usual way to draw a shape with a border.

Parameter Description
context The drawing context

xtk_draw_rounded_rect

void xtk_draw_rounded_rect(
    XtkDrawContext *context,
    int x,
    int y,
    int w,
    int h,
    int radius);

Make the path a rounded rectangle.

Empties the path first. The rectangle covers x to x + w - 1 and y to y + h - 1. With radius 0 or less the corners are square; a radius more than half the width or height is made half the shorter side.

Parameter Description
context The drawing context
x The left edge
y The top edge
w The width
h The height
radius The corner radius

xtk_draw_circle

void xtk_draw_circle(XtkDrawContext *context, int cx, int cy, int radius);

Make the path a circle, with 64 segments.

Empties the path first.

Parameter Description
context The drawing context
cx The center's X
cy The center's Y
radius The radius

xtk_draw_rect

void xtk_draw_rect(XtkDrawContext *context, int x, int y, int w, int h);

Make the path a rectangle with square corners.

Empties the path first. The rectangle covers x to x + w - 1 and y to y + h - 1.

Parameter Description
context The drawing context
x The left edge
y The top edge
w The width
h The height

xtk_draw_fill_rounded_rect

void xtk_draw_fill_rounded_rect(
    XtkDrawContext *context,
    int x,
    int y,
    int w,
    int h,
    int radius);

Fill a rounded rectangle in the fill color (replaces the path).

Parameter Description
context The drawing context
x The left edge
y The top edge
w The width
h The height
radius The corner radius

xtk_draw_stroke_rounded_rect

void xtk_draw_stroke_rounded_rect(
    XtkDrawContext *context,
    int x,
    int y,
    int w,
    int h,
    int radius);

Draw a rounded rectangle's outline in the stroke color (replaces the path).

Parameter Description
context The drawing context
x The left edge
y The top edge
w The width
h The height
radius The corner radius

xtk_draw_fill_circle

void xtk_draw_fill_circle(XtkDrawContext *context, int cx, int cy, int radius);

Fill a circle in the fill color (replaces the path).

Parameter Description
context The drawing context
cx The center's X
cy The center's Y
radius The radius

xtk_draw_stroke_circle

void xtk_draw_stroke_circle(XtkDrawContext *context, int cx, int cy, int radius);

Draw a circle's outline in the stroke color (replaces the path).

Parameter Description
context The drawing context
cx The center's X
cy The center's Y
radius The radius

xtk_draw_bordered_rect

void xtk_draw_bordered_rect(
    XtkDrawContext *context,
    int x,
    int y,
    int w,
    int h,
    int radius,
    int border_width,
    unsigned long border_color,
    unsigned long fill_color);

Draw a rounded rectangle with a border, in two fills.

Fills the whole shape in border_color, then the inside, inset by border_width on each side, in fill_color, with a corner radius smaller by border_width. A border drawn this way has no gaps at the corners, which an outline of that width can have. The context's fill color is left at fill_color (or border_color when there is no inside to fill).

Parameter Description
context The drawing context
x The left edge
y The top edge
w The width
h The height
radius The outer corner radius
border_width The border's width; 0 for none
border_color The border's pixel value
fill_color The inside's pixel value

xtk_draw_apply_shape

void xtk_draw_apply_shape(Display *display, Window window, int w, int h, int radius);

Clip a window to a rounded rectangle.

Uses the X Shape extension: the corners outside the shape are not part of the window, so what is behind it shows there. Call it again when the window is resized.

Parameter Description
display The X connection
window The window to shape
w The window's width
h The window's height
radius The corner radius; 0 or less (or an empty size) removes the shape, so the window is a plain rectangle again

xtk_draw_apply_shape_selective_tb

void xtk_draw_apply_shape_selective_tb(
    Display *display,
    Window window,
    int w,
    int h,
    int radius,
    int round_top,
    int round_bottom);

Clip a window to a rectangle rounded only at the top, only at the bottom, or both.

The window-shape partner of xtk_fill_rounded_rect_selective(), for a window whose drawing is rounded only on some edges (for example one that joins another window along a square edge), so its real shape matches what is drawn in it. When radius is more than half the width or height, the shape is a plain rectangle.

Parameter Description
display The X connection
window The window to shape
w The window's width
h The window's height
radius The corner radius; 0 or less removes the shape
round_top Nonzero to round the top-left and top-right corners
round_bottom Nonzero to round the bottom-left and bottom-right corners (when neither is rounded, the shape is removed)

xtk_draw_apply_circle_shape

void xtk_draw_apply_circle_shape(
    Display *display,
    Window window,
    int w,
    int h,
    int cx,
    int cy,
    int radius);

Clip a window to a circle.

Does nothing when radius is 0 or less; it does not remove an earlier shape.

Parameter Description
display The X connection
window The window to shape
w The window's width in pixels
h The window's height in pixels
cx The circle's center X, relative to the window
cy The circle's center Y, relative to the window
radius The circle's radius

xtk_draw_apply_tooltip_shape

void xtk_draw_apply_tooltip_shape(
    Display *display,
    Window window,
    int w,
    int h,
    int radius,
    int arrow_size,
    int arrow_direction);

Clip a window to a tooltip outline: a rounded rectangle with an arrow.

The arrow is a triangle in the middle of one edge, as deep as arrow_size and twice as wide. With arrow_size 0 or less, this is xtk_draw_apply_shape().

Parameter Description
display The X connection
window The window to shape
w The whole width, including the arrow when it is on the left or right
h The whole height, including the arrow when it is on the top or bottom
radius The corner radius of the body
arrow_size How far the arrow sticks out, in pixels; 0 for no arrow
arrow_direction Which edge the arrow is on, an XtkTooltipArrowDirection

xtk_fill_tooltip_shape

void xtk_fill_tooltip_shape(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h,
    int radius,
    int arrow_size,
    int arrow_direction);

Fill a tooltip outline (a rounded rectangle with an arrow) in the GC's color.

With arrow_size 0 or less, this is xtk_fill_rounded_rect().

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge of the whole shape
y The top edge of the whole shape
w The whole width, including the arrow when it is on the left or right
h The whole height, including the arrow when it is on the top or bottom
radius The corner radius of the body
arrow_size How far the arrow sticks out; 0 for no arrow
arrow_direction Which edge the arrow is on, an XtkTooltipArrowDirection

xtk_fill_bordered_tooltip_shape

void xtk_fill_bordered_tooltip_shape(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h,
    int radius,
    int arrow_size,
    int arrow_direction,
    int border_width,
    unsigned long border_color,
    unsigned long fill_color);

Fill a tooltip outline with a border, in two fills.

Fills the whole outline in border_color, then the outline inset by border_width in fill_color, as xtk_fill_bordered_rect() does for a rectangle. The GC's foreground is changed.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge of the whole shape
y The top edge of the whole shape
w The whole width, including the arrow when it is on the left or right
h The whole height, including the arrow when it is on the top or bottom
radius The corner radius of the body
arrow_size How far the arrow sticks out; 0 for no arrow
arrow_direction Which edge the arrow is on, an XtkTooltipArrowDirection
border_width The border's width; 0 or less fills the whole shape in fill_color
border_color The border's pixel value
fill_color The inside's pixel value

xtk_text_centered_baseline

int xtk_text_centered_baseline(int container_height, const XftFont *font);

The baseline that centers one line of text vertically in a box.

Computed as (container_height + ascent - descent) / 2, so the middle of the font's ascent and descent is in the middle of the box.

Parameter Description
container_height The box's height
font The font of the text

Returns: The baseline's Y, from the top of the box; container_height / 2 when font is NULL

xtk_build_rounded_rect_path

int xtk_build_rounded_rect_path(XPoint *points, int x, int y, int w, int h, int radius);

Put the outline of a rounded rectangle into a point array.

The outline runs along x, x + w, y and y + h, so that XFillPolygon() fills x to x + w - 1 and y to y + h - 1, as xtk_fill_rect() does. With radius 0 or less it has four points (square corners); a radius more than half the width or height is made half the shorter side. The outline is not closed: the last point is not the first.

Parameter Description
points The array that receives the points; it must have room for XTK_PATH_MAX_POINTS
x The left edge
y The top edge
w The width
h The height
radius The corner radius

Returns: The number of points written (4, or 133 with rounded corners); 0 if points is NULL

xtk_build_circle_path

int xtk_build_circle_path(XPoint *points, int cx, int cy, int radius);

Put the outline of a circle into a point array.

The outline is not closed: the last point is not the first.

Parameter Description
points The array that receives the points; it must have room for XTK_CIRCLE_SEGMENTS + 1
cx The center's X
cy The center's Y
radius The radius

Returns: The number of points written (XTK_CIRCLE_SEGMENTS); 0 if points is NULL

xtk_fill_rounded_rect

void xtk_fill_rounded_rect(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h,
    int radius);

Fill a rounded rectangle in the GC's color.

The rectangle covers x to x + w - 1 and y to y + h - 1, as xtk_fill_rect() does. With radius 0 or less the corners are square; a radius more than half the width or height is made half the shorter side.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge
y The top edge
w The width
h The height
radius The corner radius

xtk_fill_circle

void xtk_fill_circle(
    Display *display,
    Drawable drawable,
    GC gc,
    int cx,
    int cy,
    int radius);

Fill a circle in the GC's color.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
cx The center's X
cy The center's Y
radius The radius

xtk_stroke_arc

void xtk_stroke_arc(
    Display *display,
    Drawable drawable,
    GC gc,
    int cx,
    int cy,
    int radius,
    int start_angle,
    int span_angle);

Draw part of a circle's outline in the GC's color and line width.

The arc is drawn as a line through points on it, up to 64 segments for a full circle. The angles use the same units and direction as XDrawArc(), so a call to XDrawArc() can be changed to this without converting them. Draws nothing when radius is 0 or less.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
cx The center's X
cy The center's Y
radius The radius
start_angle The start, in 64ths of a degree, counterclockwise from 3 o'clock
span_angle The sweep from start_angle, in 64ths of a degree; a negative sweep goes clockwise

xtk_stroke_ellipse

void xtk_stroke_ellipse(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h);

Draw an ellipse's outline in the GC's color and line width.

Draws nothing when w or h is 0 or less.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge of the bounding box
y The top edge of the bounding box
w The bounding box's width; the outline spans x to x + w, as XDrawArc()'s does
h The bounding box's height; the outline spans y to y + h

xtk_fill_bordered_rect

void xtk_fill_bordered_rect(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h,
    int radius,
    int border_width,
    unsigned long border_color,
    unsigned long fill_color);

Draw a rounded rectangle with a border, in two fills.

Fills the whole shape in border_color, then the inside, inset by border_width, in fill_color with a corner radius smaller by border_width. With border_width 0 or less, the whole shape is filled in fill_color. The GC's foreground is changed.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge
y The top edge
w The width
h The height
radius The outer corner radius
border_width The border's width
border_color The border's pixel value
fill_color The inside's pixel value

xtk_clear_window

void xtk_clear_window(Display *display, Window window);

Clear a whole window to its background (XClearWindow()).

For a widget that draws without a back buffer. A window whose background is ParentRelative shows its parent's background.

Parameter Description
display The X connection
window The window to clear

xtk_clear_area

void xtk_clear_area(
    Display *display,
    Window window,
    int x,
    int y,
    int w,
    int h,
    int exposures);

Clear part of a window to its background (XClearArea()).

Does nothing when w or h is negative.

Parameter Description
display The X connection
window The window to clear
x The left edge
y The top edge
w The width; 0 reaches the window's right edge
h The height; 0 reaches the window's bottom edge
exposures Nonzero to also send Expose events for the area, so the window's owner repaints it

xtk_fill_rect

void xtk_fill_rect(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h);

Fill a rectangle in the GC's color.

Drawn as a four-point polygon, so it lines up with the other polygon shapes. The rectangle covers x to x + w and y to y + h.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge
y The top edge
w The width
h The height

xtk_stroke_rect

void xtk_stroke_rect(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h);

Draw a rectangle's outline in the GC's color and line width.

The outline passes through x, y and x + w, y + h, so it covers w + 1 by h + 1 pixels, as XDrawRectangle() does.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge
y The top edge
w The width
h The height

xtk_stroke_rounded_rect

void xtk_stroke_rounded_rect(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h,
    int radius);

Draw a rounded rectangle's outline in the GC's color and line width.

With radius 0 or less, this is xtk_stroke_rect().

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge
y The top edge
w The width
h The height
radius The corner radius

xtk_fill_polygon

void xtk_fill_polygon(
    Display *display,
    Drawable drawable,
    GC gc,
    const XPoint *points,
    int point_count);

Fill any polygon in the GC's color.

The polygon may be concave. Draws nothing with fewer than three points.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
points The corners
point_count The number of corners (3 or more)

xtk_draw_polyline

void xtk_draw_polyline(
    Display *display,
    Drawable drawable,
    GC gc,
    const XPoint *points,
    int point_count);

Draw a line through a list of points in the GC's color and line width.

Draws nothing with fewer than two points.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
points The points
point_count The number of points (2 or more)

xtk_fill_rounded_rect_selective

void xtk_fill_rounded_rect_selective(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h,
    int radius,
    int round_top,
    int round_bottom);

Fill a rectangle rounded only at the top, only at the bottom, or both.

For a part that joins another part along a square edge, for example a tab that sits on a panel. With radius 0 or less, or neither end rounded, a plain rectangle is filled; a radius more than half the width or height is made half the shorter side. The shape covers x to x + w - 1 and y to y + h - 1, as xtk_fill_rect() does.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge
y The top edge
w The width
h The height
radius The corner radius
round_top 1 to round the top corners, 0 for square
round_bottom 1 to round the bottom corners, 0 for square

xtk_fill_rounded_rect_selective_lr

void xtk_fill_rounded_rect_selective_lr(
    Display *display,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h,
    int radius,
    int round_left,
    int round_right);

Fill a rectangle rounded only on the left, only on the right, or both.

The same as xtk_fill_rounded_rect_selective(), for the left and right ends.

Parameter Description
display The X connection
drawable Where to draw
gc The GC to draw with
x The left edge
y The top edge
w The width
h The height
radius The corner radius
round_left 1 to round the left corners, 0 for square
round_right 1 to round the right corners, 0 for square

xtk_draw_aa_available

int xtk_draw_aa_available(Display *display);

Whether the anti-aliased shapes can draw on this display.

When this returns 0, the xtk_draw_aa_* functions draw nothing; draw the shape with the polygon functions instead.

Parameter Description
display The X connection

Returns: 1 if the server has XRender with the A8, ARGB32 and RGB24 formats, 0 otherwise

xtk_draw_aa_line

void xtk_draw_aa_line(
    Display *display,
    int screen,
    Drawable drawable,
    int x1,
    int y1,
    int x2,
    int y2,
    int line_width,
    unsigned long pixel);

Draw an anti-aliased line over a drawable.

The line's edges are smoothed, and its ends are round. Each call makes a small coverage mask and composites the color through it with XRender, so it costs more than a plain line; use it for a few lines, not for many.

Parameter Description
display The X connection
screen The screen number (its default colormap gives the color)
drawable The target; it must have depth 24
x1 The start's X
y1 The start's Y
x2 The end's X
y2 The end's Y
line_width The line's width in pixels; nothing is drawn when it is 0 or less
pixel The line's color, as a pixel value

xtk_draw_aa_fill_circle

void xtk_draw_aa_fill_circle(
    Display *display,
    int screen,
    Drawable drawable,
    int cx,
    int cy,
    int radius,
    unsigned long pixel);

Fill an anti-aliased circle over a drawable.

Note: A circle that reaches past the drawable's left or top edge is drawn moved right or down, so it stays inside.

Parameter Description
display The X connection
screen The screen number (its default colormap gives the color)
drawable The target; it must have depth 24
cx The center's X
cy The center's Y
radius The radius in pixels; nothing is drawn when it is 0 or less
pixel The fill color, as a pixel value

xtk_draw_aa_stroke_circle

void xtk_draw_aa_stroke_circle(
    Display *display,
    int screen,
    Drawable drawable,
    int cx,
    int cy,
    int radius,
    int line_width,
    unsigned long pixel);

Draw an anti-aliased circle outline over a drawable.

Parameter Description
display The X connection
screen The screen number (its default colormap gives the color)
drawable The target; it must have depth 24
cx The center's X
cy The center's Y
radius The radius of the middle of the outline
line_width The outline's width in pixels; nothing is drawn when it is 0 or less
pixel The outline's color, as a pixel value

xtk_shm_available

int xtk_shm_available(Display *display);

Whether the server has the MIT-SHM extension.

Parameter Description
display The X connection

Returns: 1 if it has, 0 otherwise

xtk_shm_image_create

XtkShmImage *xtk_shm_image_create(
    Display *display,
    int screen,
    int width,
    int height,
    int depth);

Make a shared-memory image.

Waits for the server to attach the memory. When it refuses (a display on another machine, say), the error is caught and NULL is returned; the program goes on.

Parameter Description
display The X connection
screen The screen number (its default visual is used)
width The image's width in pixels
height The image's height in pixels
depth The depth, usually 24 (four bytes per pixel)

Returns: The image, which the caller frees with xtk_shm_image_destroy(); or NULL if the size is empty, the server has no MIT-SHM, or the shared memory could not be made

xtk_shm_image_destroy

void xtk_shm_image_destroy(XtkShmImage *image);

Free a shared-memory image.

Detaches the memory from the server (with an XSync()) and from the program. Does nothing when image is NULL.

Parameter Description
image The image to free, or NULL

xtk_shm_image_data

unsigned int *xtk_shm_image_data(XtkShmImage *image);

The image's pixels, to write into directly.

For depth 24 each pixel is one unsigned int in the visual's layout, usually 0x00RRGGBB, row after row. When a put is still being read by the server, this waits for it (XSync()) first, so call it before each new picture is written: writing then cannot tear the picture the server is still reading.

Parameter Description
image The image

Returns: The first pixel, or NULL if image is NULL

xtk_shm_image_size

void xtk_shm_image_size(const XtkShmImage *image, int *width, int *height);

The image's size.

Parameter Description
image The image; when NULL, both sizes are 0
width Receives the width; may be NULL
height Receives the height; may be NULL

xtk_shm_image_put

void xtk_shm_image_put(
    XtkShmImage *image,
    Drawable drawable,
    GC gc,
    int source_x,
    int source_y,
    int destination_x,
    int destination_y,
    int width,
    int height);

Copy part of the image to a drawable (XShmPutImage()).

The part is clipped to the image. The request is not flushed, and the call does not wait for the server to finish reading the memory; the next xtk_shm_image_data() does.

Parameter Description
image The image
drawable Where to copy to
gc The GC for the copy
source_x The part's left edge in the image
source_y The part's top edge in the image
destination_x Where its left edge goes in the drawable
destination_y Where its top edge goes in the drawable
width The part's width
height The part's height

xtk_shm_image_resize

int xtk_shm_image_resize(
    XtkShmImage **image_pointer,
    Display *display,
    int screen,
    int width,
    int height,
    int depth);

Make an image the given size, keeping it when it already is.

When the size or depth differs, the old image is freed and a new one made (its pixels are not kept).

Parameter Description
image_pointer Where the image pointer is kept; *image_pointer may be NULL, and it is set to the new image (NULL on failure)
display The X connection
screen The screen number
width The new width
height The new height
depth The depth

Returns: 1 on success, 0 on failure

xtk_draw_gradient_hline

int xtk_draw_gradient_hline(
    Display *display,
    int screen,
    XtkShmImage **image,
    Drawable target,
    GC gc,
    int destination_x,
    int destination_y,
    int w,
    int h,
    const unsigned char *gradient_r,
    const unsigned char *gradient_g,
    const unsigned char *gradient_b,
    int count);

Draw a gradient from left to right with a shared-memory image.

The colors are spread evenly across the width and mixed linearly between stops: the first stop at the left edge, the last at the right edge. The image in *image is made or resized as needed and kept for the next call; the caller frees it with xtk_shm_image_destroy(). Assumes a 24-bit visual with 0x00RRGGBB pixels.

Parameter Description
display The X connection
screen The screen number
image Where the cached image pointer is kept (*image may be NULL)
target Where to draw the gradient
gc The GC for the copy
destination_x The region's left edge in target
destination_y The region's top edge in target
w The region's width in pixels
h The region's height in pixels
gradient_r The red of each stop, 0 to 255
gradient_g The green of each stop, 0 to 255
gradient_b The blue of each stop, 0 to 255
count The number of stops in each array (at least 2)

Returns: 1 when drawn; 0 on failure (no MIT-SHM, an empty region, fewer than two stops), and the caller should fill flat instead

xtk_draw_gradient_vline

int xtk_draw_gradient_vline(
    Display *display,
    int screen,
    XtkShmImage **image,
    Drawable target,
    GC gc,
    int destination_x,
    int destination_y,
    int w,
    int h,
    const unsigned char *gradient_r,
    const unsigned char *gradient_g,
    const unsigned char *gradient_b,
    int count);

Draw a gradient from bottom to top with a shared-memory image.

The same as xtk_draw_gradient_hline(), upward: the first stop is at the bottom edge and the last at the top edge.

Parameter Description
display The X connection
screen The screen number
image Where the cached image pointer is kept (*image may be NULL)
target Where to draw the gradient
gc The GC for the copy
destination_x The region's left edge in target
destination_y The region's top edge in target
w The region's width in pixels
h The region's height in pixels
gradient_r The red of each stop, 0 to 255
gradient_g The green of each stop, 0 to 255
gradient_b The blue of each stop, 0 to 255
count The number of stops in each array (at least 2)

Returns: 1 when drawn; 0 on failure, and the caller should fill flat instead