std1.0.0-beta-7 SafeC docs

std :: gui::gui_widget

Struct std::Widget

#include <std/gui/gui_widget.h> source

struct Widget {
    int kind;
    int x;
    int y;
    int w;
    int h;
    int prefW;
    int prefH;
    int stretch;
    int alignment;
    struct WidgetStyle style;
    struct String text;
    int visible;
    int enabled;
    int checked;
    double sliderValue;
    double sliderMin;
    double sliderMax;
    unsigned long caret;
    int hover;
    int pressed;
    int focused;
    ?fn void(&Widget, ?&Erased) onClick;
    ?&Erased onClickData;
    ?fn void(&Widget, ?&Erased) onChange;
    ?&Erased onChangeData;
    ?fn void(&GuiWindow, &Widget) customDraw;
    ?fn void(&Widget, int, int, int, int) customLayout;
    ?fn int(&Widget, &GuiEvent) customEvent;
    ?&Erased userData;
    struct Vec<&Widget> children;
    ?&Widget parent;
    struct String name;
    struct String tooltip;
    struct String placeholder;
    struct String shortcut;
    int minW;
    int minH;
    int maxW;
    int maxH;
    int marginL;
    int marginT;
    int marginR;
    int marginB;
    int padL;
    int padT;
    int padR;
    int padB;
    int valign;
    int gridRow;
    int gridCol;
    int gridRowSpan;
    int gridColSpan;
    int columns;
    struct Vec<int> colStretch;
    struct Vec<int> rowStretch;
    double step;
    int decimals;
    int selected;
    int orientation;
    struct Vec<struct String> items;
    struct Vec<struct WidgetTreeNode> nodes;
    struct Vec<struct WidgetColumn> tableCols;
    struct Vec<struct String> cells;
    int rowCount;
    float scrollX;
    float scrollY;
    float contentW;
    float contentH;
    unsigned long anchor;
    float textScroll;
    int password;
    int readOnly;
    int wrap;
    int indeterminate;
    double animPhase;
    int open;
    int textAlign;
    const ?&GuiImage image;
    int fit;
    struct GuiColor color;
    float hue;
    float sat;
    float val;
    int dragPart;
    float dragX;
    float dragY;
    double dragValue;
    int hotIndex;
    ?fn void(&Widget, ?&Erased) onActivate;
    ?&Erased onActivateData;
    ?fn void(&Widget, &GuiCanvas, ?&Erased) onDraw;
    ?&Erased onDrawData;
    ?&Widget contextMenu;
    ?&Widget popup;
    ?&Widget owner;
    ?&struct GuiUi uiPtr;
    int result;
    int focusable;
    double clickTime;
    long tag;
}

Fields

§int kindsource

box, computed by layout() each frame (window pixels)

box, computed by layout() each frame (window pixels)

box, computed by layout() each frame (window pixels)

box, computed by layout() each frame (window pixels)

§int prefWsource

sizing hints (all optional — 0/unset means "size to content")

§int prefHsource

sizing hints (all optional — 0/unset means "size to content")

§int stretchsource

0 = natural size; >0 = flex weight along the parent box's main axis

§int alignmentsource

WIDGET_ALIGN_* — this widget's position on the parent's cross axis

§struct WidgetStyle stylesource
§struct String textsource
§int visiblesource
§int enabledsource
§int checkedsource

check box, toggle, checkable menu item

§double sliderValuesource
§double sliderMinsource
§double sliderMaxsource

slider, progress, spinner, splitter (value)

§unsigned long caretsource

text fields: the caret (a byte offset)

§int hoversource
§int pressedsource
§int focusedsource
§?fn void(&Widget, ?&Erased) onClicksource

Callback/hook slots: typed functions over an erased context (see std/erased.h). The context's type is each caller's own — 'children' below holds buttons, sliders, labels, ... with unrelated userData types in one tree, which one generic Widget<T> couldn't — so the generic setters (on_click<T> & co.) store fn(?&T) / ?&T as fn(?&Erased) / ?&Erased, and every call passes the pair back together.

§?&Erased onClickDatasource
§?fn void(&Widget, ?&Erased) onChangesource
§?&Erased onChangeDatasource
§?fn void(&GuiWindow, &Widget) customDrawsource
§?fn void(&Widget, int, int, int, int) customLayoutsource
§?fn int(&Widget, &GuiEvent) customEventsource
§?&Erased userDatasource
§struct Vec<&Widget> childrensource

empty (null) for the root widget

§struct String namesource

find() looks widgets up by it

§struct String tooltipsource
§struct String placeholdersource

text fields, combo boxes

§struct String shortcutsource

menu items: "Ctrl+S", "Ctrl+Shift+Z", "F5", "Delete"

§int minWsource
§int minHsource

logical; 0 = none

§int maxWsource
§int maxHsource
§int marginLsource
§int marginTsource
§int marginRsource
§int marginBsource
§int padLsource
§int padTsource
§int padRsource
§int padBsource

-1: use style.padding / the theme

§int valignsource

stacks and grid cells: vertical alignment (-1: alignment)

§int gridRowsource
§int gridColsource
§int gridRowSpansource
§int gridColSpansource
§int columnssource

grids: number of columns

§struct Vec<int> colStretchsource

grids: per column / per row stretch weights

§struct Vec<int> rowStretchsource
§double stepsource

slider/spinner increment (0 = continuous for sliders)

§int decimalssource

spinner/slider value display

§int selectedsource

radio, combo, list, tabs, table row, tree node (-1 = none)

§int orientationsource

WIDGET_HORIZONTAL / WIDGET_VERTICAL

§struct Vec<struct String> itemssource

radio, combo, list entries; tab titles

§struct Vec<struct WidgetTreeNode> nodessource

tree view

§struct Vec<struct WidgetColumn> tableColssource
§struct Vec<struct String> cellssource

table: rows x columns, row-major

§int rowCountsource
§float scrollXsource
§float scrollYsource

scroll offsets (pixels)

§float contentWsource
§float contentHsource

scrolled content size (pixels)

§unsigned long anchorsource

text fields: the other end of the selection

§float textScrollsource

single-line text: horizontal scroll

§int passwordsource
§int readOnlysource
§int wrapsource

labels, text areas: word wrap

§int indeterminatesource

progress bar: busy animation

§double animPhasesource
§int opensource

combo / menu popup open

§int textAlignsource

GUI_TEXT_LEFT/CENTER/RIGHT

§const ?&GuiImage imagesource

image view (the widget doesn't own it)

§int fitsource

WIDGET_FIT_*

§struct GuiColor colorsource

color picker

§float huesource
§float satsource
§float valsource
§int dragPartsource

what the pointer is dragging (internal)

§float dragXsource
§float dragYsource
§double dragValuesource
§int hotIndexsource

hovered item / row / tab / menu entry (internal)

§?fn void(&Widget, ?&Erased) onActivatesource
§?&Erased onActivateDatasource
§?fn void(&Widget, &GuiCanvas, ?&Erased) onDrawsource
§?&Erased onDrawDatasource
§?&Widget contextMenusource

shown on right click

combo: its drop-down list; menu: its open submenu

popups/menus: the widget that opened it

§?&struct GuiUi uiPtrsource

the GuiUi this tree belongs to (root and overlays)

§int resultsource

dialogs: GUI_RESULT_* when closed

§int focusablesource

-1 = by kind, 0 / 1 = forced

§double clickTimesource
§long tagsource

yours

Methods

§static &Widget vstack()source · definition

All return a freshly heap-allocated, non-null widget — a region-less '&Widget' (see README's "Outliving references"), since nothing here cares whether the tree it ends up in is heap/stack/static/arena-owned. A widget is freed with its tree (free() on the root).

§static &Widget hstack()source · definition
§static &Widget grid(int columns)source · definition
§static &Widget stack()source · definition
§static &Widget panel()source · definition
§static &Widget group(const &char title)source · definition
§static &Widget scroll()source · definition
§static &Widget splitter(int orientation)source · definition

Two children (add them with add_child), side by side (WIDGET_HORIZONTAL) or one above the other, with a draggable divider. value() is the first child's share, 0..1.

§static &Widget tabs()source · definition

Pages: add_tab(title, page).

§static &Widget spacer()source · definition
§static &Widget separator()source · definition
§static &Widget label(const &char text)source · definition
§static &Widget button(const &char text)source · definition
§static &Widget checkbox(const &char text, int checked)source · definition
§static &Widget toggle(const &char text, int on)source · definition
§static &Widget radio_group()source · definition

Items with add_item(); selected() / set_selected().

§static &Widget slider(double minValue, double maxValue, double value)source · definition
§static &Widget progress(double fraction)source · definition
§static &Widget spinner(double minValue, double maxValue, double value, double step)source · definition
§static &Widget textinput(const &char placeholder)source · definition
§static &Widget textarea()source · definition
§static &Widget combo()source · definition
§static &Widget list()source · definition
§static &Widget tree()source · definition
§static &Widget table()source · definition
§static &Widget image(const ?&GuiImage img)source · definition
§static &Widget color_picker(struct GuiColor initial)source · definition
§static &Widget statusbar()source · definition
§static &Widget menubar()source · definition
§static &Widget menu(const &char title)source · definition

A menu: inside a menubar, its title is the bar entry; inside another menu, a submenu; on its own, a popup/context menu.

§static &Widget menu_item(const &char text)source · definition
§static &Widget menu_separator()source · definition
§static &Widget dialog(const &char title)source · definition

A modal dialog box with a title bar; add content and buttons as children (it's a column). Show it with GuiUi::show_modal().

§generic<T> static &Widget canvas(fn void(&Widget, &GuiCanvas, ?&T) draw, ?&T userData)source · definition

A WIDGET_CANVAS: draw(widget, canvas, userData) paints it; the canvas is already translated to the widget's top-left and clipped to it, in logical units (scaled for the window).

§generic<T> static &Widget custom( fn void(&GuiWindow, &Widget) draw, fn void(&Widget, int, int, int, int) layout, fn int(&Widget, &GuiEvent) eventFn, ?&T userData, )source · definition

A WIDGET_CUSTOM node drawn and laid out by your own functions (see "Custom widgets" below); userData is the caller's type, stored erased. The parameters are WidgetDrawFn / WidgetLayoutFn / WidgetCustomEventFn (declared after this struct), spelled out: a compiler without forward typedef resolution in struct-body declarations (before SafeC#43) can't match them to the definitions otherwise.

§void add_child(&Widget child)source · definition
§void insert_child(unsigned long index, &Widget child)source · definition

Inserts at 'index' (clamped).

§void remove_child(&Widget child)source · definition

Detaches 'child' (not freed: free() it, or add it elsewhere).

§void clear_children()source · definition

Removes and frees every child.

§unsigned long child_count() constsource · definition
§?&Widget child_at(unsigned long i) constsource · definition
§?&Widget find(const &char name)source · definition

The first widget named 'name' in this subtree (depth first).

§?&GuiUi ui() constsource · definition

The GuiUi this widget's tree is attached to (null before the first render/dispatch or GuiUi::new).

§void invalidate()source · definition

Marks the widget's window for repainting (and relayout).

§&Widget set_bg(unsigned char r, unsigned char g, unsigned char b, unsigned char a)source · definition
§&Widget set_fg(unsigned char r, unsigned char g, unsigned char b, unsigned char a)source · definition
§&Widget set_border( unsigned char r, unsigned char g, unsigned char b, unsigned char a, int width, )source · definition
§&Widget set_padding(int padding)source · definition
§&Widget set_padding4(int left, int top, int right, int bottom)source · definition
§&Widget set_margin(int margin)source · definition
§&Widget set_margin4(int left, int top, int right, int bottom)source · definition
§&Widget set_spacing(int spacing)source · definition
§&Widget set_pref_size(int prefW, int prefH)source · definition
§&Widget set_min_size(int minW, int minH)source · definition
§&Widget set_max_size(int maxW, int maxH)source · definition
§&Widget set_stretch(int stretch)source · definition
§&Widget set_alignment(int alignment)source · definition
§&Widget set_align2(int halign, int valign)source · definition

Both axes for stacks and grid cells (horizontal, vertical).

§&Widget set_font_scale(int scale)source · definition
§&Widget set_font(const ?&GuiFont font)source · definition
§&Widget set_font_size(float size)source · definition

TrueType font size (logical px) and weight for this widget's text.

§&Widget set_bold(int bold)source · definition
§&Widget set_visible(int visible)source · definition
§&Widget set_enabled(int enabled)source · definition
§&Widget set_text(const &char text)source · definition
§&Widget set_name(const &char name)source · definition
§&Widget set_tooltip(const &char text)source · definition
§&Widget set_placeholder(const &char text)source · definition
§&Widget set_text_align(int align)source · definition
§&Widget set_wrap(int wrap)source · definition
§&Widget set_password(int on)source · definition
§&Widget set_read_only(int on)source · definition
§&Widget set_focusable(int on)source · definition
§&Widget set_cell(int row, int col)source · definition

Grid placement (and spans of 1 or more cells).

§&Widget set_span(int rows, int cols)source · definition
§&Widget set_column_stretch(int col, int stretch)source · definition
§&Widget set_row_stretch(int row, int stretch)source · definition
§&Widget set_orientation(int orientation)source · definition
§&Widget set_context_menu(?&Widget menu)source · definition
§&Widget set_tag(long tag)source · definition
§&Widget set_value(double v)source · definition

slider / progress / spinner / splitter; clamped to the range, rounded to 'step' when it's > 0. Setting a value doesn't fire on_change.

§double value() constsource · definition
§&Widget set_range(double minValue, double maxValue)source · definition
§&Widget set_step(double step)source · definition
§&Widget set_decimals(int decimals)source · definition
§&Widget set_indeterminate(int on)source · definition
§&Widget set_checked(int checked)source · definition

check box / toggle / menu item

§int is_checked() constsource · definition
§&Widget add_item(const &char text)source · definition

items of radio groups, combo boxes and list views

§void clear_items()source · definition
§unsigned long item_count() constsource · definition
§const &char item_text(unsigned long i) constsource · definition

"" out of range

§&Widget set_selected(int index)source · definition
§int selected_index() constsource · definition
§&Widget add_tab(const &char title, &Widget page)source · definition

tabs

§int add_node(int parent, const &char text)source · definition

tree views: a node under 'parent' (-1: top level); returns its index

§&Widget set_expanded(int node, int expanded)source · definition
§int node_count() constsource · definition
§const &char node_text(int node) constsource · definition
§&Widget add_column(const &char title, int width)source · definition

table views

§int add_row()source · definition

returns the row index

§&Widget set_cell_text(int row, int col, const &char text)source · definition
§const &char cell_text(int row, int col) constsource · definition
§int row_count() constsource · definition
§void clear_rows()source · definition
§&Widget select(unsigned long start, unsigned long end)source · definition

text fields: the selection (byte offsets, start <= end)

§&Widget select_all()source · definition
§unsigned long selection_start() constsource · definition
§unsigned long selection_end() constsource · definition
§&Widget set_image(const ?&GuiImage img)source · definition

image views

§&Widget set_fit(int fit)source · definition
§&Widget set_color(struct GuiColor c)source · definition

color pickers

§struct GuiColor get_color() constsource · definition
§&Widget set_shortcut(const &char shortcut)source · definition

menu items: the shortcut shown and triggered ("Ctrl+O", "Ctrl+Shift+S", "F2", "Delete"; Ctrl means Command on macOS)

§void close_dialog(int result)source · definition

dialogs: closes it (GuiUi::close_modal) with 'result'

§generic<T> &Widget on_click(fn void(&Widget, ?&T) cb, ?&T userData)source · definition

cb(widget, userData). userData's type is the caller's; the widget stores it erased (see the field comment above).

§generic<T> &Widget on_change(fn void(&Widget, ?&T) cb, ?&T userData)source · definition
§generic<T> &Widget on_activate(fn void(&Widget, ?&T) cb, ?&T userData)source · definition
§generic<T> &Widget on_draw(fn void(&Widget, &GuiCanvas, ?&T) cb, ?&T userData)source · definition
§void layout(int x, int y, int w, int h)source · definition

Positions this widget and its whole subtree within the box (x,y,w,h) — window pixels.

§void render(&GuiWindow win)source · definition

Lays the tree out to the window's size and paints it into 'win'. (A tree that's already laid out to another box — layout() called with a box not at the window's origin — keeps that box.)

§int dispatch_event(&GuiEvent ev)source · definition

Routes one event through the tree rooted here: hover/pressed/focus state, clicks, value changes, text editing, scrolling, popups, and a WIDGET_CUSTOM node's customEvent hook. Returns 1 if some widget consumed the event.

After that, for GUI_EVENT_KEY_DOWN, a default action runs — the same idea as a browser's default DOM action for a key event — unless ev.prevent_default() (see gui.h) was called on this event first (typically from a WIDGET_CUSTOM node's WidgetCustomEventFn, the one hook that gets the raw GuiEvent* and can act before the default fires):

Tab / Shift+Tab   moves focus to the next/previous focusable widget (wraps around)
Enter / Space     activates the focused widget (button click / check box toggle)
Escape            closes a popup or dialog, or clears focus
Left/Right        nudges a focused slider by 5% of its range (or its step)

These are genuinely default actions: they still run even if dispatch already returned 1 for this event (e.g. a focused text input consuming Backspace doesn't block Tab from later moving focus off it).

§void drop()source · definition

Recursively frees this widget and its whole subtree (children, text, style, context menu), including the widget's own allocation. Detach it from its parent first (remove_child) when it has one.

§void free() consumesource · definition

= drop()