Programming Macaw OS

Apps

Programs with buttons, text boxes and lists use ui, the toolkit every Macaw OS program gets. To start one, type newapp myapp.lua in Terminal. There are three examples to read in /home/examples: greeter.lua, todo.lua and widgets.lua, which shows every widget there is.

How it works

You don't make widgets once and keep them. Instead, _draw calls them in order each frame, between ui.begin() and ui.finish(). Each widget draws itself and tells you what the person did with it. Your program keeps the values:

WINDOW = { w = 260, h = 180, title = "Settings" }

local name, loud, volume = "", false, 5

function _draw()
  ui.begin()
  ui.heading("Settings")
  name = ui.textbox("name", name, { placeholder = "your name" })
  loud = ui.checkbox("Loud", loud)
  volume = ui.slider("Volume", volume, 0, 10, 1)
  ui.row(2)
  if ui.button("Save") then print("saved", name) end
  if ui.button("Cancel") then close() end
  ui.finish()
end

ui.begin() clears the window to the paper colour. Use ui.begin({ bg = false }) if you don't want that. Widgets stack down the window from the top. Tab moves between them, and Enter or Space presses the one with the focus.

Widgets

CallWhat it does
ui.label(text, [colour])Some text
ui.heading(text)A heading
ui.separator()A line across the window
ui.button(label, [{ w = }])A button. Returns true in the frame it's clicked
ui.checkbox(label, value)A tick box. Returns the new value
ui.radio(label, selected)One of a set of choices. Returns true when it's clicked
ui.slider(label, v, min, max, [step])A slider. Returns the new value
ui.textbox(id, text, [opts])A box to type in. Returns the new text, and whether Enter was pressed. opts can have placeholder, password and w
ui.listbox(id, items, selected, [rows])A list. Returns the number of the selected item, and whether it was double-clicked
ui.dropdown(id, items, selected)A drop-down menu. Returns the number of the selected item
ui.tabs(id, names, current)Tabs along the top. Returns the current tab
ui.progress(fraction, [label])A progress bar, from 0 to 1
ui.swatches(colour, [{ all = true }])Colours to pick from. With all it shows all 256
ui.image(picture, [scale])A picture. Returns true when it's clicked

Text boxes, lists, drop-downs and tabs take an id. This is a name you choose, different for each one, so the widget can remember things between frames, such as where the cursor is or how far the list is scrolled.

A password box ({ password = true }) has a key button. It saves the password in Nestchain, or fills it in from there.

Layout

CallWhat it does
ui.row(n, [weights])Puts the next n widgets side by side. With weights such as { 2, 1 }, the first is twice as wide as the second
ui.space([pixels])Leaves a gap
ui.group(title) ... ui.endgroup()Draws a box with a title around the widgets in between
ui.at(x, y, w)Puts the next widget at exactly this place
ui.disabled = trueGreys out the widgets that follow, until you set it back to false

Set MENUS and the menu bar shows your menus while your window is in front:

MENUS = {
  { "File", {
    { "New", new_file, "n" },              -- label, what it does, Ctrl+key
    { "Save", save, "s" },
    "-",                                   -- a dividing line
    { "Close", close, "w" },
  } },
  { "View", {
    { "Grid", toggle_grid, checked = show_grid },
    { "Zoom In", zoom_in, "=", enabled = zoom < 4 },
  } },
}

MENUS can also be a function that returns the menus. Then checked and enabled are worked out again each time a menu opens.

Right-click menus

Define CONTEXT_MENU(x, y) to give your window a right-click menu. It's called with the place that was clicked and returns the items:

function CONTEXT_MENU(x, y)
  return { { "Paint here", function() paint(x, y) end }, "-", { "Clear", clear_all } }
end

To open a menu from your own code, call popupmenu(items, [x, y]). Without x and y it opens at the pointer.

Dialogs

Dialogs don't pause your program. Instead they call a function when the person answers.

CallWhat it does
alert(message)Shows a message with an OK button
confirm(question, fn)Asks a question with OK and Cancel buttons, and calls fn() if they click OK
askstring(prompt, default, fn)Asks for some text, then calls fn(text)
openfile(fn, [opts])Shows the Open dialog, then calls fn(path). opts can have dir and ext = { "pxl", "png" }
savefile(fn, [opts])Shows the Save dialog, then calls fn(path). opts can have dir and name
pickcolour(fn, [hex], [title])Shows the colour picker, then calls fn("#rrggbb")
openfile(function(path)
  picture = loadimage(path)
end, { dir = "/home", ext = { "pxl" } })

Drag and drop

CallWhat it does
function _drop(paths, x, y)Called when files are dropped on your window, at x, y: dragged from Files, or from the computer (those are put in /home/Imports first)
pickfiles()Asks the person to pick files from their computer (the website only). They come to your _drop(paths, -1, -1) once they're in. Returns false where it can't
canpickfiles()true where pickfiles works (the website)
startdrag(paths)Starts dragging files out of your window, to Files, the Trash, the desktop or another program
putondesktop(path)Puts an alias of a file, folder, program or game on the desktop, and returns the alias's file in /home/Desktop (or nil and why)

The rest of the desktop

CallWhat it does
open(path, ...)Opens a file the way double-clicking it would. For a program, the extra arguments reach it as ARGS
close()Closes your window
settitle(s)Changes your window's title
fullscreen(on)Makes your window fill the screen, or puts it back
focused()Returns whether your window is in front
setclipboard(s) / getclipboard()Puts text on the clipboard and gets it back. Every program shares it, and so does the computer underneath
getpref(key) / setpref(key, value)Reads and changes system preferences. For example, getpref("highlight") is the highlight colour