1 xtk_iconview
liquibyte edited this page 2026-11-27 16:51:41 -06:00

xtk_iconview.h

A grid of icons with their names under them, to select and open.

What it is for. Showing a folder as a file manager does: each item a large icon with its name under it, row after row, scrolled up and down. An item's icon is the icon theme's for its name's file type (a folder's for a folder, xtk_icon_draw_file()), or a theme icon the program names. Only the rows on screen are drawn, so a folder of many thousands of files costs what one screenful does.

Selecting. An item is its icon and its name, as in Thunar: the space between items is empty space. A click selects an item; Ctrl+click adds or takes it away; Shift+click selects from the last item clicked to this one; a drag on empty space draws a rectangle that selects what it touches (with Ctrl it changes their state, with Shift it adds them). With the keyboard (while the view is focused, xtk_iconview_set_focused()): the arrows, Home, End, Page Up and Page Down move the keyboard's item and select it; with Shift they select from where the selection started; with Ctrl they move without selecting, and Ctrl+Space selects or unselects; Ctrl+A selects all.

Opening. A double click (or, in single-click mode, a click), Return, and a middle click call the activate callback with the item; a right click calls the context callback (the item is selected first; -1 on empty space). The selection callback is called when the user changes the selection.

Making one. xtk_iconview_create(), then xtk_iconview_set_items(); give every event to xtk_iconview_handle_event(). The view never changes its items: the program sets them again (after a folder changed), then the selection, the keyboard's item and the scroll position it wants kept.

Theme keys. [iconview]: background, color (the names), selected-background and selected-color (a selected name: only the name is highlighted), hover-background (kept for themes; the item under the pointer is shown by its lightened icon, as in Thunar), focus-border (the keyboard's item, while focused and not selected: its name's faint highlight), rubber-band (the selecting rectangle) and muted (the text of an empty view). The names are drawn in [main] font-family and font-size.

Example.

staticvoidopened(XtkIconView*view,intindex,intbutton,void*user_data){
printf("open%s\n",xtk_iconview_get_name(view,index));
}
XtkIconView*view=xtk_iconview_create(display,screen,window,&config,0,0,640,480);
constXtkIconViewItemitems[]={{"Documents",NULL,1},{"notes.txt",NULL,0}};
xtk_iconview_set_items(view,items,2);
xtk_iconview_set_activate_callback(view,opened,NULL);
xtk_iconview_set_focused(view,1);
while(running){
XNextEvent(display,&event);
xtk_iconview_handle_event(view,&event);
}
xtk_iconview_destroy(view);

Pictures. A program can show a picture of its own in place of an item's icon: a file manager's thumbnails (xtk_iconview_set_image()). The picture is fitted into the icon's square, its shape kept, and sits on the name. It is kept on the X server at the size it was fitted to; after the icon size changes it is scaled there (never made larger than it was given) until the program sets it again at the new size. xtk_iconview_get_visible() tells which items are on screen, so their pictures can be made first. Setting the items drops the pictures; xtk_iconview_set_items_keeping_images() keeps each item's by its name, for the same folder read again.

Renaming. xtk_iconview_start_editing() puts an entry over an item's name, its name part selected (the program says where the extension starts). While it shows, the view takes every key (give it key presses first): Return calls the "done" callback with XTK_ICONVIEW_EDIT_ACCEPT, Tab and Shift+Tab with XTK_ICONVIEW_EDIT_NEXT and _PREVIOUS; the program renames, then calls xtk_iconview_stop_editing() (or starts editing another item), or does neither to keep the entry for a fix. Escape and any click that is not on the entry end it with no call and nothing renamed (the program gives xtk_iconview_stop_editing() the clicks on its other windows). F2 moves the selection: the name, the whole name, the extension. The "changed" callback comes after each change of the text, to check it as it is typed (xtk_iconview_set_editing_state() colors the border). Setting the items again keeps the editing on the item of the same name. A slow second click on the name of the only selected item can ask for renaming (xtk_iconview_set_slow_click_rename()). Call xtk_iconview_tick() often (every 50 ms): it blinks the caret and makes that request.

Limitations. No dragging items out; the name is edited on one line, scrolled when it is long; a name too long for its lines is shortened, not shown whole when selected. The rectangle scrolls the view only while the pointer moves.

#include "xtk_iconview.h"

Types: XtkIconView, XtkIconViewEditEnd, XtkIconViewEditCallback, XtkIconViewRenameCallback, XtkIconViewItem, XtkIconViewEliding, XtkIconViewActivateCallback, XtkIconViewSelectionCallback, XtkIconViewContextCallback, XtkIconViewEmblemStyle

Functions: xtk_iconview_create, xtk_iconview_destroy, xtk_iconview_get_window, xtk_iconview_set_position, xtk_iconview_resize, xtk_iconview_get_size, xtk_iconview_show, xtk_iconview_hide, xtk_iconview_set_items, xtk_iconview_get_count, xtk_iconview_get_name, xtk_iconview_set_items_keeping_images, xtk_iconview_set_empty_text, xtk_iconview_set_icon_size, xtk_iconview_get_icon_size, xtk_iconview_set_label_lines, xtk_iconview_set_eliding, xtk_iconview_set_single_click, xtk_iconview_set_image, xtk_iconview_set_emblem, xtk_iconview_set_emblem_style, xtk_iconview_has_emblem, xtk_iconview_has_image, xtk_iconview_get_image_size, xtk_iconview_get_visible, xtk_iconview_set_clipboard, xtk_iconview_set_input_method, xtk_iconview_start_editing, xtk_iconview_stop_editing, xtk_iconview_is_editing, xtk_iconview_get_editing_index, xtk_iconview_get_editing_window, xtk_iconview_editing_menu_shows, xtk_iconview_set_editing_state, xtk_iconview_set_edit_callbacks, xtk_iconview_set_slow_click_rename, xtk_iconview_tick, xtk_iconview_is_selected, xtk_iconview_set_selected, xtk_iconview_select_all, xtk_iconview_select_none, xtk_iconview_invert_selection, xtk_iconview_get_selected_count, xtk_iconview_get_cursor, xtk_iconview_set_cursor, xtk_iconview_index_at, xtk_iconview_get_scroll, xtk_iconview_set_scroll, xtk_iconview_scroll_to, xtk_iconview_set_focused, xtk_iconview_get_focused, xtk_iconview_set_activate_callback, xtk_iconview_set_selection_callback, xtk_iconview_set_context_callback, xtk_iconview_handle_event, xtk_iconview_draw, xtk_iconview_set_theme, xtk_iconview_config_init_defaults, xtk_iconview_config_parse_styling, xtk_iconview_config_write_styling

Types

XtkIconView

typedef struct XtkIconView XtkIconView;

XtkIconViewEditEnd

Why the editing callback is called.

Name Value Description
XTK_ICONVIEW_EDIT_ACCEPT Return: rename to the text.
XTK_ICONVIEW_EDIT_NEXT Tab: rename, then edit the next item.
XTK_ICONVIEW_EDIT_PREVIOUS Shift+Tab: rename, then edit the previous item.
XTK_ICONVIEW_EDIT_CHANGED The text changed (the "changed" callback)

XtkIconViewEditCallback

typedef void (*XtkIconViewEditCallback)(XtkIconView *view, int index, const char *text, XtkIconViewEditEnd end, void *user_data);

Called while an item's name is edited.

XtkIconViewRenameCallback

typedef void (*XtkIconViewRenameCallback)(XtkIconView *view, int index, void *user_data);

Called when a slow second click asks to rename an item: the program calls xtk_iconview_start_editing().

XtkIconViewItem

One item given to xtk_iconview_set_items(); the view copies it.

Field Type Description
name const char * The name shown under the icon; its file type gives the icon.
icon const char * A theme icon's name to draw instead ("folder-documents"), or NULL.
is_folder int 1 for a folder: a folder's icon

XtkIconViewEliding

How a name too long for its lines is shortened.

Name Value Description
XTK_ICONVIEW_ELIDE_END 0 "a very long na...txt": the end cut, the extension kept
XTK_ICONVIEW_ELIDE_MIDDLE "a very...name.txt": the middle cut
XTK_ICONVIEW_ELIDE_NONE Never cut: the whole name, on as many lines as it takes (the line limit is not used); each row as tall as its longest name.

XtkIconViewActivateCallback

typedef void (*XtkIconViewActivateCallback)(XtkIconView *view, int index, int button, void *user_data);

Called when an item is opened.

XtkIconViewSelectionCallback

typedef void (*XtkIconViewSelectionCallback)(XtkIconView *view, void *user_data);

Called after the user changed the selection.

XtkIconViewContextCallback

typedef void (*XtkIconViewContextCallback)(XtkIconView *view, int index, int x_root, int y_root, void *user_data);

Called on a right click.

XtkIconViewEmblemStyle

How an emblem is drawn.

Name Value Description
XTK_ICONVIEW_EMBLEM_AS_IS 0 The picture as it is.
XTK_ICONVIEW_EMBLEM_EMBOSSED Stamped into the icon, as the themes draw Documents or Music: the picture's shape (its alpha, or for a picture with no transparency its dark parts) in a darker shade of the icon's own color where it goes, with a lighter edge below it.

Functions

xtk_iconview_create

XtkIconView *xtk_iconview_create(
    Display *display,
    int screen,
    Window parent,
    const XtkConfig *config,
    int x,
    int y,
    int width,
    int height);

Make an icon view, mapped and empty.

Parameter Description
config The theme; kept, not copied (the program keeps it alive)

Returns: The view, or NULL if display or config is NULL or memory runs out

xtk_iconview_destroy

void xtk_iconview_destroy(XtkIconView *view);

Destroy the view, its items and its window. NULL does nothing.

xtk_iconview_get_window

Window xtk_iconview_get_window(const XtkIconView *view);

The view's window, or None.

xtk_iconview_set_position

void xtk_iconview_set_position(XtkIconView *view, int x, int y);

Move the view; a hidden view stays hidden.

xtk_iconview_resize

void xtk_iconview_resize(XtkIconView *view, int width, int height);

Resize the view; the items flow into as many columns as fit.

xtk_iconview_get_size

void xtk_iconview_get_size(const XtkIconView *view, int *width, int *height);

xtk_iconview_show

void xtk_iconview_show(XtkIconView *view);

xtk_iconview_hide

void xtk_iconview_hide(XtkIconView *view);

xtk_iconview_set_items

void xtk_iconview_set_items(XtkIconView *view, const XtkIconViewItem *items, int count);

Show these items, in this order (copied); nothing selected, no keyboard item, scrolled to the top.

xtk_iconview_get_count

int xtk_iconview_get_count(const XtkIconView *view);

xtk_iconview_get_name

const char *xtk_iconview_get_name(const XtkIconView *view, int index);

An item's name, or "" for an index that is not an item.

xtk_iconview_set_items_keeping_images

void xtk_iconview_set_items_keeping_images(
    XtkIconView *view,
    const XtkIconViewItem *items,
    int count);

Set the items as xtk_iconview_set_items() does, but an item with a name the old items had keeps its picture.

For the same set of things given again (a folder read again after it changed), so the pictures do not go and come back. The program sets a new picture for an item whose thing changed.

xtk_iconview_set_empty_text

void xtk_iconview_set_empty_text(XtkIconView *view, const char *text);

A line shown in the middle of an empty view ("This folder is empty"); NULL for none.

xtk_iconview_set_icon_size

void xtk_iconview_set_icon_size(XtkIconView *view, int size);

The icons' size in pixels (16 to 512); the cells follow.

xtk_iconview_get_icon_size

int xtk_iconview_get_icon_size(const XtkIconView *view);

xtk_iconview_set_label_lines

void xtk_iconview_set_label_lines(XtkIconView *view, int lines);

How many lines a name may take under its icon (1 to 5; 0: 5). Not used while names are never shortened (XTK_ICONVIEW_ELIDE_NONE).

xtk_iconview_set_eliding

void xtk_iconview_set_eliding(XtkIconView *view, XtkIconViewEliding eliding);

xtk_iconview_set_single_click

void xtk_iconview_set_single_click(XtkIconView *view, int single_click);

1: a click opens an item and the pointer's item is underlined; 0 (the default): a double click opens it.

xtk_iconview_set_image

int xtk_iconview_set_image(
    XtkIconView *view,
    int index,
    const unsigned char *rgba,
    int width,
    int height);

Show a picture in place of an item's icon (a thumbnail), or the icon again.

The picture is fitted into the icon's square now (scaled down smoothly, never up) and kept on the X server; the pixels are only read. Nothing is drawn: set the pictures that came, then call xtk_iconview_draw() once.

Parameter Description
index The item
rgba The pixels, width * height * 4 bytes (red, green, blue, alpha), row by row; NULL to show the icon again
width The picture's width
height Its height

Returns: 1 when it was set (or taken away); 0 for an index that is not an item, a size 0 or less, no XRender, or no memory

xtk_iconview_set_emblem

int xtk_iconview_set_emblem(
    XtkIconView *view,
    int index,
    const unsigned char *rgba,
    int width,
    int height);

Show a picture on the middle of an item's icon (a folder's emblem), or none.

Drawn over the icon (or the item's picture), centered across and a little below the middle, where a folder's face is, as the icon themes put the symbol of Documents or Music; at most 45% of the icon, its shape kept, never made larger. Embossed (xtk_iconview_set_emblem_style()), it is made once here, from the icon's color: set it again after the icon size or the theme changes. Kept on the X server; nothing is drawn now (call xtk_iconview_draw()); kept by name by xtk_iconview_set_items_keeping_images().

Parameter Description
rgba The pixels, width * height * 4 bytes; NULL takes the emblem away

Returns: 1 when it was set (or taken away); 0 for an index that is not an item, a size 0 or less, or no memory

xtk_iconview_set_emblem_style

void xtk_iconview_set_emblem_style(XtkIconView *view, XtkIconViewEmblemStyle style);

How the emblems set from now on are drawn (set them again after a change); XTK_ICONVIEW_EMBLEM_AS_IS at first.

xtk_iconview_has_emblem

int xtk_iconview_has_emblem(const XtkIconView *view, int index);

1 when the item shows an emblem.

xtk_iconview_has_image

int xtk_iconview_has_image(const XtkIconView *view, int index);

1 when the item shows a picture of the program's.

xtk_iconview_get_image_size

int xtk_iconview_get_image_size(const XtkIconView *view, int index);

The longer side of the item's picture as it was fitted, in pixels; 0 when it has none.

xtk_iconview_get_visible

void xtk_iconview_get_visible(const XtkIconView *view, int *first, int *last);

The items on screen now: whole or partly shown rows.

Parameter Description
first Set to the first (0 when there are none)
last Set to the last (-1 when there are none, so first > last)

xtk_iconview_set_clipboard

void xtk_iconview_set_clipboard(XtkIconView *view, XtkClipboard *clipboard);

The clipboard the editing entry copies and pastes with (the program's; NULL: none).

xtk_iconview_set_input_method

void xtk_iconview_set_input_method(XtkIconView *view, XIM input_method);

The input method the editing entry types through, or NULL.

xtk_iconview_start_editing

int xtk_iconview_start_editing(XtkIconView *view, int index, int name_end);

Edit an item's name where it is shown; one at a time (editing another ends this one).

Parameter Description
index The item
name_end Where its name part ends (the extension's dot): that part is selected first; -1 or the name's length selects it all

Returns: 1, or 0 for an index that is not an item, a hidden view, or no memory

xtk_iconview_stop_editing

void xtk_iconview_stop_editing(XtkIconView *view);

End the editing, nothing renamed.

xtk_iconview_is_editing

int xtk_iconview_is_editing(const XtkIconView *view);

xtk_iconview_get_editing_index

int xtk_iconview_get_editing_index(const XtkIconView *view);

The item being edited, or -1.

xtk_iconview_get_editing_window

Window xtk_iconview_get_editing_window(const XtkIconView *view);

The editing entry's window (None when not editing): a click on another window should end the editing.

xtk_iconview_editing_menu_shows

int xtk_iconview_editing_menu_shows(const XtkIconView *view);

1 while the editing entry's right-click menu shows: it takes every event then.

xtk_iconview_set_editing_state

void xtk_iconview_set_editing_state(XtkIconView *view, int state);

The entry's border: 0 normal, 1 a name that can't be used, 2 a good one.

xtk_iconview_set_edit_callbacks

void xtk_iconview_set_edit_callbacks(
    XtkIconView *view,
    XtkIconViewEditCallback done,
    XtkIconViewEditCallback changed,
    void *user_data);

The callbacks of the editing: done for Return and Tab, changed for each change (either may be NULL).

xtk_iconview_set_slow_click_rename

void xtk_iconview_set_slow_click_rename(
    XtkIconView *view,
    int on,
    XtkIconViewRenameCallback request,
    void *user_data);

1: a slow second click on the only selected item's name calls request (after a double click's time).

xtk_iconview_tick

void xtk_iconview_tick(XtkIconView *view);

Call often (every 50 ms): blinks the editing caret, and asks for a slow click's renaming.

xtk_iconview_is_selected

int xtk_iconview_is_selected(const XtkIconView *view, int index);

xtk_iconview_set_selected

void xtk_iconview_set_selected(XtkIconView *view, int index, int selected);

xtk_iconview_select_all

void xtk_iconview_select_all(XtkIconView *view);

xtk_iconview_select_none

void xtk_iconview_select_none(XtkIconView *view);

xtk_iconview_invert_selection

void xtk_iconview_invert_selection(XtkIconView *view);

xtk_iconview_get_selected_count

int xtk_iconview_get_selected_count(const XtkIconView *view);

xtk_iconview_get_cursor

int xtk_iconview_get_cursor(const XtkIconView *view);

The keyboard's item, or -1.

xtk_iconview_set_cursor

void xtk_iconview_set_cursor(XtkIconView *view, int index);

Make an item the keyboard's (and where Shift selects from), scrolled into view; the selection stays. -1 for none.

xtk_iconview_index_at

int xtk_iconview_index_at(const XtkIconView *view, int x, int y);

The item at a point of the view's window, or -1.

xtk_iconview_get_scroll

int xtk_iconview_get_scroll(const XtkIconView *view);

How far the view is scrolled down, in pixels.

xtk_iconview_set_scroll

void xtk_iconview_set_scroll(XtkIconView *view, int scroll);

xtk_iconview_scroll_to

void xtk_iconview_scroll_to(XtkIconView *view, int index);

Scroll as little as makes an item wholly shown.

xtk_iconview_set_focused

void xtk_iconview_set_focused(XtkIconView *view, int focused);

1: the view takes key presses and frames its keyboard item. A click on it focuses it.

xtk_iconview_get_focused

int xtk_iconview_get_focused(const XtkIconView *view);

xtk_iconview_set_activate_callback

void xtk_iconview_set_activate_callback(
    XtkIconView *view,
    XtkIconViewActivateCallback callback,
    void *user_data);

xtk_iconview_set_selection_callback

void xtk_iconview_set_selection_callback(
    XtkIconView *view,
    XtkIconViewSelectionCallback callback,
    void *user_data);

xtk_iconview_set_context_callback

void xtk_iconview_set_context_callback(
    XtkIconView *view,
    XtkIconViewContextCallback callback,
    void *user_data);

xtk_iconview_handle_event

int xtk_iconview_handle_event(XtkIconView *view, XEvent *event);

Give the view an X event.

Returns: 1 when it took it: an event of its windows, or, while focused, a key it uses. A Ctrl+wheel is not taken, so a program can zoom with it.

xtk_iconview_draw

void xtk_iconview_draw(XtkIconView *view);

Draw the view again.

xtk_iconview_set_theme

void xtk_iconview_set_theme(XtkIconView *view, const XtkConfig *config);

Take a new theme (kept, not copied), and draw in it.

xtk_iconview_config_init_defaults

void xtk_iconview_config_init_defaults(IconViewBlock *iconview_config);

The [iconview] colors a config starts with (a dark look).

xtk_iconview_config_parse_styling

int xtk_iconview_config_parse_styling(
    IconViewBlock *iconview_config,
    const char *key,
    const char *value);

Read one [iconview] key; 1 when it was one.

xtk_iconview_config_write_styling

int xtk_iconview_config_write_styling(FILE *file, const IconViewBlock *iconview_config);

Write the [iconview] section.