summaryrefslogtreecommitdiff
path: root/src/ui/widget.h
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-07-21 15:06:52 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-07-21 15:07:38 +0300
commitd773b499e595a43b9b1ae449262dcf13cabf2d02 (patch)
treeb1baeb12025a04f8316636b5d0ab18e30ceedb2c /src/ui/widget.h
Initial commit
Borrowing the app skeleton from Bitwise Harmony.
Diffstat (limited to 'src/ui/widget.h')
-rw-r--r--src/ui/widget.h131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/ui/widget.h b/src/ui/widget.h
new file mode 100644
index 00000000..bf5c22f1
--- /dev/null
+++ b/src/ui/widget.h
@@ -0,0 +1,131 @@
1#pragma once
2
3/* Base class for UI widgets. */
4
5#include "metrics.h"
6
7#include <the_Foundation/object.h>
8#include <the_Foundation/objectlist.h>
9#include <the_Foundation/rect.h>
10#include <the_Foundation/string.h>
11#include <SDL_events.h>
12
13#define iDeclareWidgetClass(className) \
14 iDeclareType(className); \
15 typedef iWidgetClass i##className##Class; \
16 extern i##className##Class Class_##className;
17
18iDeclareType(Widget)
19iBeginDeclareClass(Widget)
20 iBool (*processEvent) (iWidget *, const SDL_Event *);
21 void (*draw) (const iWidget *);
22iEndDeclareClass(Widget)
23
24enum iWidgetFlag {
25 hidden_WidgetFlag = iBit(1),
26 disabled_WidgetFlag = iBit(2),
27 hover_WidgetFlag = iBit(3), /* eligible for mouse hover */
28 selected_WidgetFlag = iBit(4),
29 pressed_WidgetFlag = iBit(5),
30 alignLeft_WidgetFlag = iBit(6),
31 alignRight_WidgetFlag = iBit(7),
32 frameless_WidgetFlag = iBit(8),
33 drawKey_WidgetFlag = iBit(10),
34 focusable_WidgetFlag = iBit(11),
35 keepOnTop_WidgetFlag = iBit(12), /* gets events first; drawn last */
36 arrangeHorizontal_WidgetFlag = iBit(17), /* arrange children horizontally */
37 arrangeVertical_WidgetFlag = iBit(18), /* arrange children vertically */
38 arrangeWidth_WidgetFlag = iBit(19), /* area of children becomes parent size */
39 arrangeHeight_WidgetFlag = iBit(20), /* area of children becomes parent size */
40 arrangeSize_WidgetFlag = arrangeWidth_WidgetFlag | arrangeHeight_WidgetFlag,
41 resizeChildren_WidgetFlag = iBit(21), /* resize children to fill parent size */
42 expand_WidgetFlag = iBit(22),
43 fixedWidth_WidgetFlag = iBit(23),
44 fixedHeight_WidgetFlag = iBit(24),
45 fixedSize_WidgetFlag = fixedWidth_WidgetFlag | fixedHeight_WidgetFlag,
46 resizeChildrenToWidestChild_WidgetFlag = iBit(25),
47 resizeToParentWidth_WidgetFlag = iBit(26),
48 resizeToParentHeight_WidgetFlag = iBit(27),
49};
50
51enum iWidgetAddPos {
52 back_WidgetAddPos,
53 front_WidgetAddPos,
54};
55
56enum iWidgetFocusDir {
57 forward_WidgetFocusDir,
58 backward_WidgetFocusDir,
59};
60
61struct Impl_Widget {
62 iObject object;
63 iString id;
64 int flags;
65 iRect rect;
66 int bgColor;
67 iObjectList *children;
68 iWidget * parent;
69 iBool (*commandHandler)(iWidget *, const char *);
70};
71
72iDeclareObjectConstruction(Widget)
73
74iLocalDef iWidget *as_Widget(iAnyObject *d) {
75 if (d) {
76 iAssertIsObject(d);
77 iAssert(isInstance_Object(d, &Class_Widget));
78 }
79 return (iWidget *) d;
80}
81
82iLocalDef const iWidget *constAs_Widget(const iAnyObject *d) {
83 if (d) {
84 iAssertIsObject(d);
85 iAssert(isInstance_Object(d, &Class_Widget));
86 }
87 return (const iWidget *) d;
88}
89
90void destroy_Widget (iWidget *); /* widget removed and deleted later */
91void destroyPending_Widget(void);
92
93const iString *id_Widget (const iWidget *);
94int flags_Widget (const iWidget *);
95iRect bounds_Widget (const iWidget *);
96iInt2 localCoord_Widget (const iWidget *, iInt2 coord);
97iBool contains_Widget (const iWidget *, iInt2 coord);
98iAny * findChild_Widget (const iWidget *, const char *id);
99iAny * findFocusable_Widget(const iWidget *startFrom, enum iWidgetFocusDir focusDir);
100size_t childCount_Widget (const iWidget *);
101void draw_Widget (const iWidget *);
102
103iBool isVisible_Widget (const iWidget *);
104iBool isDisabled_Widget (const iWidget *);
105iBool isFocused_Widget (const iWidget *);
106iBool isHover_Widget (const iWidget *);
107iBool isSelected_Widget (const iWidget *);
108iBool isCommand_Widget (const iWidget *d, const SDL_Event *ev, const char *cmd);
109iBool hasParent_Widget (const iWidget *d, const iWidget *someParent);
110void setId_Widget (iWidget *, const char *id);
111void setFlags_Widget (iWidget *, int flags, iBool set);
112void setPos_Widget (iWidget *, iInt2 pos);
113void setSize_Widget (iWidget *, iInt2 size);
114void setBackgroundColor_Widget (iWidget *, int bgColor);
115void setCommandHandler_Widget (iWidget *, iBool (*handler)(iWidget *, const char *));
116iAny * addChild_Widget (iWidget *, iAnyObject *child); /* holds a ref */
117iAny * addChildPos_Widget (iWidget *, iAnyObject *child, enum iWidgetAddPos addPos);
118iAny * addChildFlags_Widget(iWidget *, iAnyObject *child, int childFlags); /* holds a ref */
119iAny * removeChild_Widget (iWidget *, iAnyObject *child); /* returns a ref */
120iAny * child_Widget (iWidget *, size_t index); /* O(n) */
121void arrange_Widget (iWidget *);
122iBool dispatchEvent_Widget(iWidget *, const SDL_Event *);
123iBool processEvent_Widget (iWidget *, const SDL_Event *);
124void postCommand_Widget (const iWidget *, const char *cmd, ...);
125
126void setFocus_Widget (iWidget *);
127iWidget *focus_Widget (void);
128iWidget *hover_Widget (void);
129void unhover_Widget (void);
130void setMouseGrab_Widget (iWidget *);
131iWidget *mouseGrab_Widget (void);