From a4294e4a94189dffb1fdf99c9a60d87d77272926 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 13 Jul 2024 10:52:24 -0700 Subject: Restructure project. --- src/widget/widget.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/widget/widget.c (limited to 'src/widget/widget.c') diff --git a/src/widget/widget.c b/src/widget/widget.c new file mode 100644 index 0000000..ef79ac4 --- /dev/null +++ b/src/widget/widget.c @@ -0,0 +1,93 @@ +#include "widget.h" + +#include + +// ----------------------------------------------------------------------------- +// Widget. + +#define UI_DEL(ppWidget) \ + { \ + assert(ppWidget); \ + void* widget_ = *ppWidget; \ + if (widget_) { \ + free(widget_); \ + *ppWidget = 0; \ + } \ + } + +uiWidgetType uiWidgetGetType(const uiWidget* widget) { + assert(widget); + return widget->type; +} + +void DestroyWidget(uiWidget** ppWidget) { + assert(ppWidget); + + uiWidget* widget = *ppWidget; + if (widget) { + list_foreach_mut(widget->children, child, { DestroyWidget(&child); }); + } + UI_DEL(ppWidget); +} + +void uiWidgetSetParent(uiPtr child_, uiPtr parent_) { + uiWidget* child = child_.widget; + uiWidget* parent = parent_.widget; + + assert(child); + assert(parent); + + list_add(parent->children, child); +} + +// ----------------------------------------------------------------------------- +// Widget pointers. + +uiPtr uiMakeButtonPtr(uiButton* button) { + assert(button); + return (uiPtr){.type = uiTypeButton, .button = button}; +} + +uiPtr uiMakeFramePtr(uiFrame* frame) { + assert(frame); + return (uiPtr){.type = uiTypeFrame, .frame = frame}; +} + +uiPtr uiMakeLabelPtr(uiLabel* label) { + assert(label); + return (uiPtr){.type = uiTypeLabel, .label = label}; +} + +uiPtr uiMakeTablePtr(uiTable* table) { + assert(table); + return (uiPtr){.type = uiTypeTable, .table = table}; +} + +static uiPtr uiMakeWidgetPtr(uiWidget* widget) { + assert(widget); + return (uiPtr){.type = widget->type, .widget = widget}; +} + +uiButton* uiGetButtonPtr(uiPtr ptr) { + assert(ptr.type == uiTypeButton); + assert(ptr.button); + return ptr.button; +} + +uiFrame* uiGetFramePtr(uiPtr ptr) { + assert(ptr.type == uiTypeFrame); + assert(ptr.frame); + return ptr.frame; +} + +uiLabel* uiGetLabelPtr(uiPtr ptr) { + assert(ptr.type == uiTypeLabel); + assert(ptr.label); + return ptr.label; +} + +uiTable* uiGetTablePtr(uiPtr ptr) { + assert(ptr.type == uiTypeTable); + assert(ptr.table); + return ptr.table; +} -- cgit v1.2.3