C reference: buttons, lists and dialogs
#include <macawui.h> for the toolkit of buttons, sliders, text boxes and lists, and for the dialogs. The widgets are immediate mode: you don't make them once and keep them, but call them every frame from your _draw function, between ui_begin and ui_finish. Each one draws itself and returns 1 in the frame it's clicked or changed, and the values it changes live in your program. Apps explains the toolkit, and C says how C programs work.
The frame and layout
ui_begin
void ui_begin(void)
Starts this frame's widgets. It clears the window to the paper colour, and widgets then stack down from the top left, 8 pixels in.
It takes nothing.
ui_beginex
void ui_beginex(int x, int y, int w, int bg)
Starts this frame's widgets, like ui_begin, but you choose where they go and what's behind them.
x: the left margin in pixels. -1 means 8.y: the top margin in pixels. -1 means 8.w: how wide the widgets are, in pixels. 0 is the window's width less the margins.bg: a colour number to clear the window to,UI_DEFAULTfor the paper colour, orUI_KEEPto keep what's drawn.
ui_finish
void ui_finish(void)
Ends this frame's widgets. It draws an open drop-down's list on top of everything, and moves the keyboard's focus when Tab was pressed.
It takes nothing.
ui_row
void ui_row(int n, const double *weights)
Puts the next few widgets side by side on one line, sharing its width.
n: how many widgets go on the line, up to 64.weights: an array ofndoubles for their widths, like{ 2, 1 }to make the first twice as wide.NULLmakes them all as wide.
ui_space
void ui_space(int pixels)
Leaves a gap before the next widget.
pixels: how tall the gap is, in pixels. 0 means 8.
ui_group
void ui_group(const char *title)
Starts a box with a title round the widgets that follow, until ui_endgroup.
title: the text at the top of the box.NULLfor no title.
ui_endgroup
void ui_endgroup(void)
Ends the box ui_group started, and draws it round the widgets inside.
It takes nothing.
ui_at
void ui_at(int x, int y, int w)
Puts the next widget at exactly this place, instead of under the last one.
x: its left edge, in pixels from the window's left.y: its top edge, in pixels from the window's top.w: its width in pixels. 0 gives it the width it would usually have.
ui_disabled
int ui_disabled(int on)
Greys out the widgets after it, and they stop answering the mouse and keys. It stays that way, into the next frames too, until you switch it off.
on: 1 to grey them out, 0 to go back to normal.
Returns what it was before: 1 if widgets were greyed out, 0 if not.
ui_nexty
int ui_nexty(void)
Tells you where the next widget would go, so you can draw something of your own under a form.
It takes nothing.
Returns the next widget's top, in pixels from the window's top, or -1 outside ui_begin and ui_finish.
Widgets
ui_label
void ui_label(const char *s, int colour)
Shows some text, wrapped onto more lines if it's too long for the width.
s: the text.colour: a colour number for it. -1 for the usual colour.
ui_heading
void ui_heading(const char *s)
Shows a heading, with a line under it.
s: the heading's text.
ui_separator
void ui_separator(void)
Draws a line across, to divide one part of a form from the next.
It takes nothing.
ui_button
int ui_button(const char *label, int w)
Shows a button. Enter or Space presses it too, while it has the keyboard's focus.
label: the text on the button.w: its width in pixels. 0 fits the label (or fills its share of aui_row).
Returns 1 in the frame it's clicked, 0 otherwise.
ui_checkbox
int ui_checkbox(const char *label, int *on)
Shows a tick box. A click ticks it or takes the tick away.
label: the text beside the box.on: the address of anintof yours: 1 ticked, 0 not. A click changes it.
Returns 1 in the frame it's changed, 0 otherwise.
ui_radio
int ui_radio(const char *label, int *choice, int value)
Shows one choice of a set, where only one can be picked. Give every choice of the set the same choice.
label: the text beside it.choice: the address of anintof yours, which the set shares. A click sets it tovalue.value: this choice's number. It shows as picked while*choiceisvalue.
Returns 1 in the frame it's clicked, 0 otherwise.
ui_slider
int ui_slider(const char *label, double *v, double min, double max, double step)
Shows a slider for picking a number between two others. The arrow keys move it too.
label: the text to its left.NULLor""for none.v: the address of adoubleof yours with the value. Dragging changes it.min: the value at the left end.max: the value at the right end.step: the slider moves in steps of this size frommin. 0 lets it take any value.
Returns 1 in the frame *v changes, 0 otherwise.
ui_textbox
int ui_textbox(const char *id, char *buf, int size)
Shows a box to type text in. Ctrl+C, Ctrl+X and Ctrl+V copy, cut and paste.
id: a name you choose, different for each box, so it remembers where its cursor is.buf: your buffer with the text, which typing changes.size: how many bytesbufholds, with its 0 at the end. Typing that won't fit isn't taken.
Returns 1 in the frame Enter is pressed in it, 0 otherwise.
ui_textboxex
int ui_textboxex(const char *id, char *buf, int size, const char *placeholder, int w, int password)
Shows a box to type text in, like ui_textbox, with a few more choices.
id: a name you choose, different for each box.buf: your buffer with the text, which typing changes.size: how many bytesbufholds, with its 0 at the end. Typing that won't fit isn't taken.placeholder: grey text shown while the box is empty, like"your name".NULLfor none.w: its width in pixels. 0 for the full width.password: 0 for plain text,UI_PASSWORDfor stars and a key button for Nestchain,UI_PASSWORD_NOKEYfor stars only.
Returns 1 in the frame Enter is pressed in it, 0 otherwise.
ui_listbox
int ui_listbox(const char *id, const char **items, int n, int *selected, int rows)
Shows a list to pick from. It scrolls with the mouse wheel, and the up and down keys move through it.
id: a name you choose, different for each list, so it remembers how far it's scrolled.items: an array ofnstrings, the items from the top. ANULLone shows as empty.n: how many items there are.selected: the address of anintof yours: the picked item, counting from 0, or -1 for none.rows: how many rows show at once. 0 means 5.
Returns 1 in the frame an item is double-clicked, or Enter is pressed on it, 0 otherwise.
ui_dropdown
int ui_dropdown(const char *id, const char **items, int n, int *selected, int w)
Shows a drop-down menu: a box with the picked item, which opens a list when it's clicked.
id: a name you choose, different for each one.items: an array ofnstrings, the choices. ANULLone shows as empty.n: how many items there are.selected: the address of anintof yours: the picked item, counting from 0, or -1 for none.w: its width in pixels. 0 for the full width.
Returns 1 in the frame another item is picked, 0 otherwise.
ui_tabs
int ui_tabs(const char *id, const char **names, int n, int *current)
Shows a row of tabs. You draw the widgets for the tab that's showing after it.
id: a name you choose, different for each set of tabs.names: an array ofnstrings, the tabs' names from the left.n: how many tabs there are.current: the address of anintof yours: the tab showing, counting from 0. A click changes it.
Returns 1 in the frame another tab is clicked, 0 otherwise.
ui_progress
void ui_progress(double fraction, const char *label)
Shows a progress bar, for how far through something you are.
fraction: how full the bar is, from 0 (empty) to 1 (full).label: text in the middle of the bar, like"50%".NULLfor none.
ui_swatches
int ui_swatches(int *colour, int all)
Shows squares of colour to pick one from.
colour: the address of anintof yours: the picked colour number. A click changes it.all: 0 for the 32 named colours, 1 for all 256.
Returns 1 in the frame another colour is picked, 0 otherwise.
ui_image
int ui_image(int picture, int scale)
Shows a picture among the widgets.
picture: a picture number fromnewpictureorloadpicture.scale: how many times bigger to draw it, from 1 to 16. 0 means 1.
Returns 1 in the frame it's clicked, 0 otherwise.
The keyboard's focus, and where widgets are
ui_focus
int ui_focus(char *buf, int size)
Tells you which widget has the keyboard's focus, so your own key handling can leave it alone.
buf: a buffer for the widget's id, such as a text box's id. It gets""when none has it.size: how many bytesbufholds.
Returns the id's length, or 0 if no widget has the focus.
ui_setfocus
void ui_setfocus(const char *id)
Gives the keyboard's focus to a widget, such as a text box you want the person to type in first.
id: the widget's id.NULLtakes the focus away from every widget.
ui_typing
int ui_typing(void)
Tells you whether a text box is taking the keys. While it is, Ctrl+C, X, V and A are the text box's, not your menus'.
It takes nothing.
Returns 1 if a text box has the focus, 0 if not.
ui_rect
int ui_rect(const char *id, int *x, int *y, int *w, int *h)
Tells you where a widget was last drawn, in the window.
id: the widget's id, as you gave it to a text box, list, drop-down or tabs.x: pass the address of anintfor its left edge, in pixels.NULLskips it.y: pass the address of anintfor its top edge, in pixels.NULLskips it.w: pass the address of anintfor its width, in pixels.NULLskips it.h: pass the address of anintfor its height, in pixels.NULLskips it.
Returns 1, or 0 if no widget with that id has been drawn.
Dialogs
Dialogs don't pause your program. Each calls a function of yours when the person answers, and doesn't call it if they cancel. Text it's given lasts only for that call, so copy it to keep it.
alert
void alert(const char *message)
Shows a message with an OK button.
message: the text to show.
confirm
void confirm(const char *question, void (*fn)(void))
Asks a question with OK and Cancel buttons.
question: the text of the question.fn: your function, called with nothing if they click OK.NULLonly shows the question.
askstring
void askstring(const char *prompt, const char *initial, void (*fn)(const char *text))
Asks the person to type some text.
prompt: the question above the box.initial: the text in the box to start with.NULLfor an empty box.fn: your function, called with the text they typed when they click OK.
openfile
void openfile(void (*fn)(const char *path), const char *dir, const char **exts, int n)
Shows the Open dialog, for the person to pick a file.
fn: your function, called with the whole path of the file they picked.dir: the folder it starts in, from your program's folder unless it starts with/.NULLfor/home.exts: an array ofnfile endings it can open, without the dot, like{ "pxl", "png" }.NULLfor any file.n: how many endings are inexts. 0 for any file.
savefile
void savefile(void (*fn)(const char *path), const char *dir, const char *name)
Shows the Save dialog, for the person to choose a name and a folder. It asks first if the file is already there.
fn: your function, called with the whole path to save to.dir: the folder it starts in, from your program's folder unless it starts with/.NULLfor/home.name: the file name it suggests.NULLfor "untitled".
pickcolour
void pickcolour(void (*fn)(const char *hex, int rgb), const char *hex, const char *title)
Shows the colour picker, for picking any colour.
fn: your function, called with the colour as"#rrggbb"text and as the number0xRRGGBB.hex: the colour it starts on, like"#ff8800".NULLfor white.title: the picker window's title.NULLfor "Pick a Colour".
Drag and drop
ondrop
void ondrop(void (*fn)(const char **paths, int n, int x, int y))
Sets a function to call when files are dropped on your window, from Files or the computer. It takes the place of a _drop function.
fn: your function. It gets an array ofnwhole paths, and where they were dropped in the window, in pixels (-1, -1 for files frompickfiles).NULLstops it.
canpickfiles
int canpickfiles(void)
Tells you whether pickfiles works here. It only works on the website.
It takes nothing.
Returns 1 if it works, 0 if not.
pickfiles
int pickfiles(void)
Asks the person to pick files from their computer. Once they're copied in, they come to your ondrop function (or _drop), at -1, -1.
It takes nothing.
Returns 1 if it asked, or 0 where it can't (anywhere but the website).
startdrag
void startdrag(const char **paths, int n, const char *label)
Starts dragging files out of your window, to Files, the Trash, the desktop or another program. Call it while the mouse button is down: they're dropped where it's let go.
paths: an array ofnpaths, each from your program's folder unless it starts with/.n: how many paths there are.label: the text shown by the pointer while dragging.NULLfor the first file's name.
putondesktop
int putondesktop(const char *path, char *buf, int size)
Puts an alias of a file, folder, program or game on the desktop.
path: what to make an alias of, from your program's folder unless it starts with/.buf: a buffer for the alias's own path, in/home/Desktop, or for why it didn't work.size: how many bytesbufholds.
Returns the length of the alias's path, or -1 if it didn't work.