Programming Macaw OS

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.

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.

ui_space

void ui_space(int pixels)

Leaves a gap before the next widget.

ui_group

void ui_group(const char *title)

Starts a box with a title round the widgets that follow, until ui_endgroup.

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.

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.

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.

ui_heading

void ui_heading(const char *s)

Shows a heading, with a line under it.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

ui_swatches

int ui_swatches(int *colour, int all)

Shows squares of colour to pick one from.

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.

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.

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.

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.

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.

confirm

void confirm(const char *question, void (*fn)(void))

Asks a question with OK and Cancel buttons.

askstring

void askstring(const char *prompt, const char *initial, void (*fn)(const char *text))

Asks the person to type some text.

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.

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.

pickcolour

void pickcolour(void (*fn)(const char *hex, int rgb), const char *hex, const char *title)

Shows the colour picker, for picking any 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.

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.

putondesktop

int putondesktop(const char *path, char *buf, int size)

Puts an alias of a file, folder, program or game on the desktop.

Returns the length of the alias's path, or -1 if it didn't work.