xtk_surface.h
Where a widget paints one window: a back buffer, a pixmap or the window itself.
An XtkSurface is where a widget paints one of its windows. It holds:
- the DBE back buffer of the window (see xtk_dbe.h), when the server has DBE and the allocation worked;
- or, when asked for with
XTK_SURFACE_PIXMAP_FALLBACK, an offscreen pixmap used when there is no back buffer, and the GC that copies it to the window; - the XftDraw bound to whichever of these is the real target, when asked for with
XTK_SURFACE_XFT.
It does not own the window, the widget's GC, fonts or colors: the widget makes and frees those and does all of its own drawing with xtk_draw.h, Xlib or Xft. The surface only decides where the drawing goes and when its buffers are made again. When there is no back buffer and no pixmap, the widget paints the window directly.
Life cycle.
xtk_surface_init()afterXCreateWindow(). The struct need not be zeroed first.xtk_surface_begin()at the start of every paint. It gives the drawable to draw into and makes the XftDraw again (Xft keeps a picture that is no longer valid after a back buffer changes size).xtk_surface_end()at the end of every paint. It swaps the back buffer, or copies the pixmap, onto the window; with neither it does nothing.xtk_surface_resized()after the widget's ownXResizeWindow(). It makes the pixmap again at the new size and binds the XftDraw again. A back buffer is left alone: the server resizes it with the window.xtk_surface_release()beforeXDestroyWindow(). It frees everything the surface holds. It is safe to call twice, and on a zeroed struct.
xtk_surface_reallocate() and xtk_surface_reallocate_all() are for recovery only (see below), never for each paint or resize.
An XtkSurface is a plain struct that the widget keeps inside its own struct; a widget with several painted windows keeps one per window. Between init and release the toolkit keeps a pointer to it (for xtk_surface_reallocate_all()), so the struct must not move in memory in that time: keep it in a heap-allocated widget struct, not in an array that is reallocated or in a local variable.
Example.
typedef struct { Window window; GC gc; XtkSurface surface; int w, h; } MyWidget;
MyWidget *widget = calloc(1, sizeof(*widget));
widget->w = 200;
widget->h = 100;
widget->window = XCreateSimpleWindow(display, parent, 0, 0, 200, 100, 0, 0, 0);
widget->gc = XCreateGC(display, widget->window, 0, NULL);
xtk_surface_init(&widget->surface, display, screen, widget->window, widget->w, widget->h,
XTK_SURFACE_XFT | XTK_SURFACE_PIXMAP_FALLBACK);
// On Expose:
Drawable target = xtk_surface_begin(&widget->surface);
XSetForeground(display, widget->gc, background_pixel);
XFillRectangle(display, target, widget->gc, 0, 0, widget->w, widget->h);
XftDrawStringUtf8(widget->surface.xft_draw, &text_color, font, 8, 20,
(const FcChar8 *)"Hello", 5);
xtk_surface_end(&widget->surface);
// After the widget resizes its window:
xtk_surface_resized(&widget->surface, widget->w, widget->h);
// Before XDestroyWindow():
xtk_surface_release(&widget->surface);
*
#include "xtk_surface.h"
Types: XTK_SURFACE_XFT and the other values, XtkSurface
Functions: xtk_surface_init, xtk_surface_begin, xtk_surface_end, xtk_surface_resized, xtk_surface_reallocate, xtk_surface_reallocate_all, xtk_surface_release, xtk_surface_drawable, xtk_surface_is_buffered
Types
XTK_SURFACE_XFT and the other values
Flags for xtk_surface_init(); combine them with |.
| Name | Value | Description |
|---|---|---|
XTK_SURFACE_XFT |
1 << 0 |
Keep an XftDraw bound to the paint target (in the xft_draw field) |
XTK_SURFACE_NO_DBE |
1 << 1 |
Never allocate a back buffer |
XTK_SURFACE_PIXMAP_FALLBACK |
1 << 2 |
When there is no back buffer, paint an offscreen pixmap and copy it to the window |
XtkSurface
A window's paint target: a DBE back buffer, an offscreen pixmap, or the window itself.
| Field | Type | Description |
|---|---|---|
display |
Display * |
The display connection; NULL before init and after release. |
screen |
int |
The screen number. |
window |
Window |
The widget's window (not owned by the surface); None after release |
width |
int |
The window's width, as the widget last gave it |
height |
int |
The window's height, as the widget last gave it |
back_buffer |
XdbeBackBuffer |
The back buffer, or None when DBE is not in use |
pixmap |
Pixmap |
The offscreen pixmap, or None when the pixmap fallback is not in use |
copy_gc |
GC |
The GC that copies the pixmap to the window (pixmap mode only), or NULL |
xft_draw |
XftDraw * |
The XftDraw bound to the paint target; NULL without XTK_SURFACE_XFT |
flags |
int |
The XTK_SURFACE_* flags given to xtk_surface_init() |
Functions
xtk_surface_init
void xtk_surface_init(
XtkSurface *surface,
Display *display,
int screen,
Window window,
int width,
int height,
int flags);
Attach a surface to a newly created window.
Tries to allocate a back buffer (unless XTK_SURFACE_NO_DBE is given), then, if that fails and XTK_SURFACE_PIXMAP_FALLBACK is given, a pixmap the size of the window. With neither, the widget paints the window directly. With XTK_SURFACE_XFT, the XftDraw exists as soon as this returns, so text can be measured or drawn before the first xtk_surface_begin().
The struct need not be zeroed before the first call: only a surface that is live (initialized and not released) is released first, so calling it again on a live surface frees the old buffers, and a new struct's old contents are never read. When display is NULL or window is None, the fields are filled in but nothing is allocated.
| Parameter | Description |
|---|---|
surface |
The surface to fill in, usually a field of the widget's struct |
display |
The display connection |
screen |
The screen number |
window |
The window to paint; the surface never destroys it |
width |
The window's width: the size of the fallback pixmap and of the Expose that xtk_surface_reallocate_all() sends |
height |
The window's height, used in the same way |
flags |
A combination of XTK_SURFACE_XFT, XTK_SURFACE_NO_DBE and XTK_SURFACE_PIXMAP_FALLBACK, or 0 |
xtk_surface_begin
Drawable xtk_surface_begin(XtkSurface *surface);
Start a paint.
Makes the XftDraw again (with XTK_SURFACE_XFT) and returns the drawable to paint into. Draw everything between this call and xtk_surface_end().
| Parameter | Description |
|---|---|
surface |
The surface |
Returns: The back buffer or pixmap when the surface has one, else the window; None if the surface was never initialized or has been released
xtk_surface_end
void xtk_surface_end(XtkSurface *surface);
Finish a paint: show what was drawn.
Swaps the back buffer onto the window, or copies the pixmap onto it. When the widget painted the window directly, it does nothing. The request is not flushed.
| Parameter | Description |
|---|---|
surface |
The surface |
xtk_surface_resized
void xtk_surface_resized(XtkSurface *surface, int width, int height);
Tell the surface that the window's size changed.
Call it after the widget's own XResizeWindow() or XMoveResizeWindow(). A back buffer follows the window's size on the server, so this only makes the fallback pixmap again at the new size and binds the XftDraw again. It is cheap enough for every step of an interactive resize.
| Parameter | Description |
|---|---|
surface |
The surface |
width |
The window's new width |
height |
The window's new height |
xtk_surface_reallocate
void xtk_surface_reallocate(XtkSurface *surface);
Free the back buffer (or pixmap) and allocate it again.
For recovery, not for routine use: for a program that has seen a back buffer stop showing on screen while its handle is still valid (this has been seen after a dialog managed by the window manager is mapped or unmapped next to the window). It sends a few requests to the server but does not wait for an answer.
| Parameter | Description |
|---|---|
surface |
The surface |
xtk_surface_reallocate_all
void xtk_surface_reallocate_all(Display *display);
Reallocate every live surface on a display, and ask each window to repaint.
The same recovery as xtk_surface_reallocate(), for a whole program at once: call it once after a dialog managed by the window manager closes, or after the main window is mapped again, instead of reallocating each widget by hand. Each window is then sent an Expose event that covers it (nothing is cleared first, so the old picture stays until the widget repaints). A surface is live from xtk_surface_init() until xtk_surface_release(). The X errors caused by a window that has already been destroyed are caught and ignored. It costs one round trip to the server in all.
| Parameter | Description |
|---|---|
display |
The display whose surfaces are reallocated |
xtk_surface_release
void xtk_surface_release(XtkSurface *surface);
Free the XftDraw, the back buffer or pixmap, and the copy GC.
Call it before XDestroyWindow(). Afterwards display is NULL and window is None. Safe to call twice, and on a zeroed struct.
| Parameter | Description |
|---|---|
surface |
The surface |
xtk_surface_drawable
Drawable xtk_surface_drawable(const XtkSurface *surface);
The current paint target, without making the XftDraw again.
| Parameter | Description |
|---|---|
surface |
The surface |
Returns: The back buffer, the pixmap or the window, whichever paints go to; None if surface is NULL or released
xtk_surface_is_buffered
int xtk_surface_is_buffered(const XtkSurface *surface);
Whether painting goes through a DBE back buffer.
| Parameter | Description |
|---|---|
surface |
The surface |
Returns: 1 if the surface has a back buffer, 0 otherwise (also when it uses the pixmap fallback, or surface 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