2 xtk_filedialog
liquibyte edited this page 2026-11-27 16:51:41 -06:00

xtk_filedialog.h

A dialog window for choosing a file to open or save, or a folder.

The file dialog is a top-level window, 760 by 500 pixels, centered over the program's window. It has three modes (DialogMode): Open (one or more existing files), Save (a file name, which may be new) and Open Folder (a folder). It holds:

  • a sidebar of places (Home, Desktop, Documents, Downloads, Music, Pictures, Videos, File System) and of the user's favorites;
  • a path bar of the current folder, whose parts can be clicked to go up; a click beside them, or Ctrl+L, turns it into a field to type a path;
  • the list of the folder's subfolders and files, with Name, Size, Type and Modified columns that can be sorted, resized and hidden;
  • in Save mode, a "Name:" field for the file name;
  • a filter list (shown once the program adds filters), New Folder, Cancel and OK buttons, and a status bar.

Making one. xtk_filedialog_create() takes the program's window (the dialog is transient for it and centered over it), the mode, the title, the XtkConfig and an XtkClipboard. The dialog keeps the XtkConfig pointer, which must stay valid. Add filters with xtk_filedialog_add_filter(), then show the dialog with xtk_filedialog_show() or xtk_filedialog_show_path(). A dialog can be shown, hidden and shown again; free it with xtk_filedialog_destroy().

Events. While the dialog is shown, pass every X event to xtk_filedialog_handle_event(). It returns 2 when the dialog has closed: then result is 1 if the user accepted and 0 if they cancelled, and xtk_filedialog_get_selected_file() gives the choice. Call xtk_filedialog_update_blink() on the timer that blinks the program's other text cursors.

  • Mouse: a click selects a row; a double-click opens a folder or accepts a file. In Open mode, Ctrl+click adds or removes a file from the selection and Shift+click selects a range. A click on a column header sorts by it (again to reverse); dragging the line between two headers resizes a column. A right click on the list opens a menu: Show Hidden Files, the Size, Type and Modified columns, and Add to Favorites (the current folder); a right click on a favorite offers Remove from Favorites. The wheel scrolls three rows.
  • Keys: Up, Down, Home, End, Page Up and Page Down move the selection; Return accepts the selected file, or enters the selected folder (in Open Folder mode it accepts the selected folder; a double click enters it); Escape cancels; Ctrl+H shows or hides hidden files; Ctrl+L types a path (Return goes there, or in Open mode accepts a file typed as a path: a full one, one from ~, or one in the folder shown; Escape gives the path bar back); Ctrl+Shift+N is New Folder. In Save mode, while the Name field has the focus, only Return, Escape and Ctrl+L act on the dialog.
  • OK with nothing chosen (no file selected in Open mode, no name in Save mode) keeps the dialog open and says so in the status bar. A folder that cannot be opened leaves the list and the path bar as they were, and the status bar says why.
  • New Folder shows a name field in place of the path bar, with Create and Cancel. Create (or Return) makes the folder and enters it; Cancel (or Escape) gives the path bar back. If the name is empty or invalid, or the folder cannot be made, the status bar says why and the field stays.
  • In Save mode, a name typed in the Name field may also be a path ("~/a.txt", "../b.txt"), or a folder, which the dialog then enters. Accepting a name that exists as a file first asks Overwrite or Cancel in a small menu.

Theme keys. The dialog has no section of its own; it takes its look from the program's XtkConfig:

  • [main] background (the window; the list, path bar, sidebar and status bar are drawn a little lighter or darker), color (the text, made lighter or darker when it is hard to read; folder names are drawn in a fixed blue), font-family and font-size (the list's font), selection and selection-text (the selected row, the hovered and current places).
  • [button] and config.button_widget for the buttons; the button border color also draws the dialog's lines.
  • [label] for the field labels, [combobox] for the filter and choice lists, [menu] for the right-click and overwrite menus, and [entry-text] for the name fields.

Kept choices. With xtk_filedialog_set_state_file(), the favorites, the hidden-files switch, the columns shown, their widths and the sort are kept in a file across showings and runs.

Example.

XtkFileDialog *dialog = xtk_filedialog_create(display, screen, window, DIALOG_OPEN,
                                              "Open File", &config, clipboard);
const char *c_files[] = { "c", "h" };
xtk_filedialog_add_filter(dialog, "C files", c_files, 2);
xtk_filedialog_show(dialog, NULL);

XEvent event;
for (;;) {
    XNextEvent(display, &event);
    if (xtk_filedialog_handle_event(dialog, &event) == 2) break;
}
if (dialog->result == 1) {
    char *path = xtk_filedialog_get_selected_file(dialog);
    if (path) { printf("open %s\n", path); free(path); }
}
xtk_filedialog_destroy(dialog);
 * 

Limitations.

  • The dialog has a fixed size; it does not lay itself out again when the window manager resizes it.
  • A NULL XtkConfig gives the built-in defaults (the dialog makes and keeps its own); xtk_filedialog_apply_theme() can give it a real one later.
#include "xtk_filedialog.h"

Types: XtkFileDialogFilter, DialogMode, FileEntry, XtkWindowWrapper, XtkFileDialog

Constants: XTK_FILEDIALOG_PATH_MAX, XTK_FILEDIALOG_MAX_FILTERS, XTK_FILEDIALOG_MAX_PATTERNS, XTK_FILEDIALOG_PATTERN_LENGTH, XTK_FILEDIALOG_MAX_FAVORITES, XTK_FILEDIALOG_MAX_MULTI_SELECT

Functions: xtk_filedialog_create, xtk_filedialog_destroy, xtk_filedialog_set_state_file, xtk_filedialog_add_filter, xtk_filedialog_set_name_label, xtk_filedialog_add_choice, xtk_filedialog_clear_filters, xtk_filedialog_add_filter_patterns, xtk_filedialog_set_active_filter, xtk_filedialog_get_active_filter, xtk_filedialog_show, xtk_filedialog_show_path, xtk_filedialog_hide, xtk_filedialog_apply_theme, xtk_filedialog_handle_event, xtk_filedialog_update_blink, xtk_filedialog_get_selected_file, xtk_filedialog_get_selected_count, xtk_filedialog_get_selected_at

Types

XtkFileDialogFilter

A named file-type filter, such as {"Images", {"png", "jpg", "jpeg"}, 3}.

Made by xtk_filedialog_add_filter() or xtk_filedialog_add_filter_patterns(). Every dialog also has an "All Files" entry ahead of these.

Field Type Description
label[48] char The name shown in the filter list.
extensions[XTK_FILEDIALOG_MAX_PATTERNS][XTK_FILEDIALOG_PATTERN_LENGTH] char Extensions without the dot, or globs; a whole-name pattern from xtk_filedialog_add_filter_patterns() is stored after a byte 0x01.
extension_count int The number of entries in extensions.

DialogMode

What a file dialog chooses.

Name Value Description
DIALOG_OPEN One or more existing files.
DIALOG_SAVE A file name to save to; the file need not exist.
DIALOG_OPEN_FOLDER A folder. The list and the browsing work as in Open mode, but OK accepts a folder: the subfolder selected in the list, or else the folder being browsed. Files are listed but cannot be chosen.

FileEntry

One row of the file list.

Field Type Description
name[256] char The file's name.
is_directory int 1 for a folder.
size off_t The size in bytes; 0 for a folder.
modified_time time_t The time of the last change.
extension[16] char The extension in lower case; "" for a folder or a name without one.

XtkWindowWrapper

A plain window and its display, as a child of an XtkGrid (internal).

Field Type Description
display Display * The display connection.
window Window The window.

XtkFileDialog

A file dialog. Made by xtk_filedialog_create().

The fields are the dialog's own state and are public only so that the program can read result after the dialog closes. Use the functions below for everything else; do not change the fields.

Field Type Description
display Display * The display connection.
screen int The screen number.
parent Window The window the dialog is transient for.
dialog_window Window The dialog's top-level window.
mode DialogMode Open, Save or Open Folder.
current_path[XTK_FILEDIALOG_PATH_MAX] char The folder being browsed.
selected_file[XTK_FILEDIALOG_PATH_MAX] char The chosen name, or a path typed in the name field (full, from ~ or relative).
title[128] char The window title.
file_entries FileEntry * The rows of the list.
file_count int The number of rows.
selected_index int The selected row, or -1 for none.
scroll_offset int The first row shown.
multi_selected_names[XTK_FILEDIALOG_MAX_MULTI_SELECT][256] char Open mode: the names of the selected files.
multi_selected_count int The number of names in multi_selected_names.
multi_select_anchor_index int The row a Shift+click range starts from, or -1.
sort_column int The sort column: 0 Name, 1 Size, 2 Type, 3 Modified.
sort_ascending int 1 for ascending, 0 for descending.
filter_extensions[XTK_FILEDIALOG_MAX_PATTERNS][XTK_FILEDIALOG_PATTERN_LENGTH] char The active filter's patterns; none means all files.
filter_count int The number of entries in filter_extensions.
grid XtkGrid * The layout of the dialog.
pathbar XtkPathBar * The path bar (breadcrumbs, or the path field on Ctrl+L).
file_list_window Window The file list.
filename_entry XtkEntry * Save mode: the Name field.
name_label struct XtkLabel * The Name field's label.
folder_label struct XtkLabel * The New Folder field's label.
ok_button XtkButton * OK.
cancel_button XtkButton * Cancel.
filter_combo XtkComboBox * The filter list.
choice_label struct XtkLabel * The label of xtk_filedialog_add_choice(), or NULL.
choice_combo XtkComboBox * The list of xtk_filedialog_add_choice(), or NULL.
button_row int The grid row of the buttons.
list_wrapper XtkWindowWrapper The file list as a grid child.
font XftFont * The font of the list, path bar, sidebar and status bar.
gc GC The dialog's graphics context.
width int The window width.
height int The window height.
visible int 1 while the dialog is shown.
result int After the dialog closes: 1 if the user accepted, 0 if they cancelled.
last_click_time unsigned long The time of the last click.
last_click_index int The row of the last click.
scrollbar_drag int 1 while the thumb is dragged.
scrollbar_drag_start_y int The pointer's y when the drag began.
scrollbar_drag_start_offset int scroll_offset when the drag began.
scrollbar_hover int 1 while the pointer is over the scrollbar.
sidebar_window Window The sidebar of places.
sidebar_hover int The sidebar item under the pointer, or -1.
sidebar_paths[8][256] char The paths of the fixed places.
favorite_paths[XTK_FILEDIALOG_MAX_FAVORITES][XTK_FILEDIALOG_PATH_MAX] char The user's favorite folders.
favorite_count int The number of favorites.
sidebar_context_menu XtkPopupMenu * The sidebar's right-click menu.
sidebar_context_menu_items[1] XtkPopupMenuItem Its items.
sidebar_context_target int The favorite the sidebar menu was opened on.
show_hidden int 1 to list names that start with a dot.
column_size_visible int 1 to show the Size column (the Name column always shows).
column_type_visible int 1 to show the Type column.
column_modified_time_visible int 1 to show the Modified column.
context_menu XtkPopupMenu * The file list's right-click menu.
context_menu_items[7] XtkPopupMenuItem Its items.
overwrite_confirm_menu XtkPopupMenu * Save mode: the Overwrite or Cancel menu.
overwrite_confirm_items[2] XtkPopupMenuItem Its items.
path_accepted int 1 when a file's path typed in Open's path field accepted the dialog.
creating_folder int 1 while the New Folder field shows.
new_folder_button XtkButton * New Folder.
folder_entry XtkEntry * The new folder's name field.
folder_create_button XtkButton * Create.
folder_cancel_button XtkButton * Cancel (of New Folder).
status_message[320] char A message shown in the status bar instead of the selected name; cleared each time the folder is listed.
filters[XTK_FILEDIALOG_MAX_FILTERS] XtkFileDialogFilter The filters added.
filter_definition_count int The number of filters.
active_filter int The filter in use: -1 for "All Files", else an index into filters.
column_modified_time_width int The Modified column's width.
column_type_width int The Type column's width.
column_size_width int The Size column's width.
column_drag_active int 1 while a column line is dragged.
column_drag_column int The column being resized: 1 Size, 2 Type, 3 Modified.
column_drag_start_x int The pointer's x when the drag began.
column_drag_start_width int The column's width when the drag began.
column_near_separator int 1 while the pointer is over a column line.
column_resize_cursor Cursor The pointer shape over a column line.
surface XtkSurface The paint target of dialog_window.
config const XtkConfig * The configuration the dialog draws from (not owned).
owned_config XtkConfig * The default config made when create was given none (owned), or NULL.
state_file[XTK_FILEDIALOG_PATH_MAX] char The file the choices are kept in; "" for none.

Constants

XTK_FILEDIALOG_PATH_MAX

#define XTK_FILEDIALOG_PATH_MAX 4096

The size of the dialog's path buffers, the NUL included (Linux's PATH_MAX).

XTK_FILEDIALOG_MAX_FILTERS

#define XTK_FILEDIALOG_MAX_FILTERS 128

The most filters a dialog can have, besides "All Files".

XTK_FILEDIALOG_MAX_PATTERNS

#define XTK_FILEDIALOG_MAX_PATTERNS 64

The most patterns (extensions or names) one filter can have.

XTK_FILEDIALOG_PATTERN_LENGTH

#define XTK_FILEDIALOG_PATTERN_LENGTH 40

The size of one pattern, the NUL included.

XTK_FILEDIALOG_MAX_FAVORITES

#define XTK_FILEDIALOG_MAX_FAVORITES 8

The most favorites the sidebar holds.

XTK_FILEDIALOG_MAX_MULTI_SELECT

#define XTK_FILEDIALOG_MAX_MULTI_SELECT 256

The most files an Open dialog can have selected at once.

Functions

xtk_filedialog_create

XtkFileDialog *xtk_filedialog_create(
    Display *display,
    int screen,
    Window parent,
    DialogMode mode,
    const char *title,
    const XtkConfig *config,
    XtkClipboard *clipboard);

Creates a file dialog, hidden.

The dialog starts in the program's current folder (or $HOME, or "/").

Parameter Description
display The X display.
screen The screen number.
parent The program's window; the dialog is transient for it and is centered over it when shown.
mode DIALOG_OPEN, DIALOG_SAVE or DIALOG_OPEN_FOLDER.
title The window title, or NULL for "Open File".
config The configuration the dialog draws from; the pointer is kept. NULL gives the built-in defaults (xtk_config_init_defaults()), in a config the dialog makes and frees itself.
clipboard The clipboard for copy and paste in the dialog's name fields, or NULL for none.

Returns: The new dialog, or NULL when display is NULL or memory runs out.

xtk_filedialog_destroy

void xtk_filedialog_destroy(XtkFileDialog *filedialog);

Destroys a file dialog and its windows.

Parameter Description
filedialog The dialog. NULL is allowed and does nothing.

xtk_filedialog_set_state_file

void xtk_filedialog_set_state_file(XtkFileDialog *filedialog, const char *path);

Keeps the user's choices in the dialog in a file.

The kept choices are the favorites, the hidden-files switch, which columns show, their widths and the sort. The file is read now and each time the dialog is shown (if it exists), and written each time the dialog is hidden or freed while shown, and at once when hidden files are switched on or off (Ctrl+H or the list's menu), so that choice is kept even if the program ends with the dialog open. Each write is whole or not at all (a temporary file is renamed over it). Every dialog given the same file shares these choices, across runs of the program.

Parameter Description
filedialog The dialog.
path The file (its folder must exist), or NULL to keep nothing.

xtk_filedialog_add_filter

void xtk_filedialog_add_filter(
    XtkFileDialog *filedialog,
    const char *label,
    const char **extensions,
    int extension_count);

Adds a named filter of file extensions, and makes it the one in use.

The filter appears in the dialog's filter list, after "All Files". The list is shown once there is at least one filter. Filters can be added before or while the dialog is shown; a list that is shown is filtered again only when the folder is listed again (xtk_filedialog_set_active_filter() does it at once).

Parameter Description
filedialog The dialog.
label The name shown in the list, such as "Images", or NULL or "" for one made from the first extension ("PNG Files").
extensions The extensions, without the dot ({"html", "htm"}). An entry that holds *, ? or [ is a glob matched against the whole file name instead ("*.tar.gz"). Case is ignored. At most XTK_FILEDIALOG_MAX_PATTERNS are used, each cut at 39 bytes.
extension_count The number of extensions; 0 or less does nothing.

xtk_filedialog_set_name_label

void xtk_filedialog_set_name_label(XtkFileDialog *filedialog, const char *text);

Sets the text of the label beside Save mode's name field.

The default is "Name:"; for example "Theme name:" when the name is not a file's.

Parameter Description
filedialog The dialog (in Save mode; otherwise nothing happens).
text The label's text.

xtk_filedialog_add_choice

XtkComboBox *xtk_filedialog_add_choice(
    XtkFileDialog *filedialog,
    const char *label,
    const char **items,
    int count,
    int selected);

Adds a labeled list of options above the buttons.

Use it for an extra option of the dialog, such as an editor's "Set encoding:" in its Open dialog. It can be added once per dialog, before the dialog is shown; the buttons move one row down.

Parameter Description
filedialog The dialog.
label The text left of the list, such as "Set encoding:".
items The list's items; they are copied.
count The number of items.
selected The item selected at first (0 if out of range).

Returns: The list, owned by the dialog: read its selection with xtk_combobox_get_selected_index() before xtk_filedialog_destroy(), and disable items with xtk_combobox_set_item_enabled(). NULL when a choice was already added, items is NULL, count is 0 or less, or the list cannot be made.

xtk_filedialog_clear_filters

void xtk_filedialog_clear_filters(XtkFileDialog *filedialog);

Removes every filter, leaving only "All Files".

Parameter Description
filedialog The dialog.

xtk_filedialog_add_filter_patterns

void xtk_filedialog_add_filter_patterns(
    XtkFileDialog *filedialog,
    const char *label,
    const char **patterns,
    int pattern_count);

Adds a named filter of whole-name patterns, and makes it the one in use.

Each pattern is a glob matched against the whole file name, ignoring case ("*.c", "Makefile", "*.desktop"), for file types known by their name as well as their extension.

Parameter Description
filedialog The dialog.
label The name shown in the filter list, or NULL or "" for the first pattern itself ("*.desktop").
patterns The patterns; NULL or empty ones are skipped. At most XTK_FILEDIALOG_MAX_PATTERNS are used, each cut at 38 bytes.
pattern_count The number of patterns.

xtk_filedialog_set_active_filter

void xtk_filedialog_set_active_filter(XtkFileDialog *filedialog, int index);

Makes a filter the one in use, as if the user chose it.

A shown dialog lists the folder again with the filter.

Parameter Description
filedialog The dialog.
index The filter's index, in the order the filters were added; -1, or an index out of range, for "All Files".

xtk_filedialog_get_active_filter

int xtk_filedialog_get_active_filter(const XtkFileDialog *filedialog);

Gives the filter in use.

Parameter Description
filedialog The dialog, or NULL.

Returns: The filter's index, in the order the filters were added, or -1 for "All Files" (and when filedialog is NULL).

xtk_filedialog_show

void xtk_filedialog_show(XtkFileDialog *filedialog, const char *initial_path);

Shows the dialog, centered over the program's window.

The selection and result are cleared, the kept choices are read again (see xtk_filedialog_set_state_file()), and the folder is listed. The keyboard focus goes to the Name field in Save mode, else to the dialog.

Parameter Description
filedialog The dialog.
initial_path The folder to show, or NULL or "" for the folder shown last (at first, the program's current folder).

xtk_filedialog_show_path

void xtk_filedialog_show_path(XtkFileDialog *filedialog, const char *file_path);

Shows the dialog with a file already selected.

Use it for a "Choose..." button that already has a file chosen, so that the dialog opens on it.

Parameter Description
filedialog The dialog.
file_path The full path of a file. Its folder is shown; if the file is in the list, it is selected and scrolled into view. NULL or "" is the same as xtk_filedialog_show() with NULL.

xtk_filedialog_hide

void xtk_filedialog_hide(XtkFileDialog *filedialog);

Hides the dialog, and writes the kept choices.

The dialog hides itself when the user accepts or cancels; call this only to close it from the program.

Parameter Description
filedialog The dialog.

xtk_filedialog_apply_theme

void xtk_filedialog_apply_theme(XtkFileDialog *filedialog, const XtkConfig *config);

Gives the dialog a new configuration, and redraws it if it is shown.

Parameter Description
filedialog The dialog.
config The new configuration. NULL does nothing. The pointer is kept.

xtk_filedialog_handle_event

int xtk_filedialog_handle_event(XtkFileDialog *filedialog, XEvent *event);

Handles one X event.

Pass every event while the dialog is shown. See the top of this page for what the mouse and the keys do.

Parameter Description
filedialog The dialog.
event The event.

Returns: 0 if the event was not for the dialog, or the dialog is not shown; 1 if the dialog used the event and is still open; 2 if the dialog has just closed (OK, Cancel, Escape, a double-click or Return on a file, Return on a file path typed after Ctrl+L, or Overwrite). Then read result (1 accepted, 0 cancelled) and xtk_filedialog_get_selected_file().

void xtk_filedialog_update_blink(XtkFileDialog *filedialog);

Blinks the text cursor of the dialog's name fields.

Call it on the same timer that calls xtk_entry_update_blink() for the program's own fields.

Parameter Description
filedialog The dialog. NULL, or a hidden dialog, does nothing.

xtk_filedialog_get_selected_file

char *xtk_filedialog_get_selected_file(const XtkFileDialog *filedialog);

Gives the full path of the chosen file or folder.

Read it after xtk_filedialog_handle_event() returned 2 and result is 1; once the dialog has closed without accepting, it gives NULL. In Open mode with several files selected, use xtk_filedialog_get_selected_at() for all of them.

Parameter Description
filedialog The dialog, or NULL.

Returns: A new string, which the caller frees with free(): the selected file in the folder being browsed, or the path typed (a typed full path, or one from ~, is used as it is). In Open Folder mode, the selected subfolder, or else the folder being browsed. NULL when nothing is selected (except in Open Folder mode), when the dialog closed without accepting, when filedialog is NULL, or when memory runs out.

xtk_filedialog_get_selected_count

int xtk_filedialog_get_selected_count(const XtkFileDialog *filedialog);

Gives the number of files selected in Open mode.

Parameter Description
filedialog The dialog, or NULL.

Returns: The number of selected files (1 after a plain click on a file); 0 in the other modes, when nothing is selected, or when filedialog is NULL.

xtk_filedialog_get_selected_at

char *xtk_filedialog_get_selected_at(const XtkFileDialog *filedialog, int index);

Gives the full path of one of the files selected in Open mode.

Parameter Description
filedialog The dialog, or NULL.
index Which file, from 0 to xtk_filedialog_get_selected_count() - 1, in the order they were selected (a Shift+click range in list order).

Returns: A new string, which the caller frees with free(), or NULL when index is out of range, the dialog closed without accepting, or memory runs out.