diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-21 15:06:52 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-21 15:07:38 +0300 |
commit | d773b499e595a43b9b1ae449262dcf13cabf2d02 (patch) | |
tree | b1baeb12025a04f8316636b5d0ab18e30ceedb2c /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.h | 131 |
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 | |||
18 | iDeclareType(Widget) | ||
19 | iBeginDeclareClass(Widget) | ||
20 | iBool (*processEvent) (iWidget *, const SDL_Event *); | ||
21 | void (*draw) (const iWidget *); | ||
22 | iEndDeclareClass(Widget) | ||
23 | |||
24 | enum 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 | |||
51 | enum iWidgetAddPos { | ||
52 | back_WidgetAddPos, | ||
53 | front_WidgetAddPos, | ||
54 | }; | ||
55 | |||
56 | enum iWidgetFocusDir { | ||
57 | forward_WidgetFocusDir, | ||
58 | backward_WidgetFocusDir, | ||
59 | }; | ||
60 | |||
61 | struct 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 | |||
72 | iDeclareObjectConstruction(Widget) | ||
73 | |||
74 | iLocalDef 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 | |||
82 | iLocalDef 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 | |||
90 | void destroy_Widget (iWidget *); /* widget removed and deleted later */ | ||
91 | void destroyPending_Widget(void); | ||
92 | |||
93 | const iString *id_Widget (const iWidget *); | ||
94 | int flags_Widget (const iWidget *); | ||
95 | iRect bounds_Widget (const iWidget *); | ||
96 | iInt2 localCoord_Widget (const iWidget *, iInt2 coord); | ||
97 | iBool contains_Widget (const iWidget *, iInt2 coord); | ||
98 | iAny * findChild_Widget (const iWidget *, const char *id); | ||
99 | iAny * findFocusable_Widget(const iWidget *startFrom, enum iWidgetFocusDir focusDir); | ||
100 | size_t childCount_Widget (const iWidget *); | ||
101 | void draw_Widget (const iWidget *); | ||
102 | |||
103 | iBool isVisible_Widget (const iWidget *); | ||
104 | iBool isDisabled_Widget (const iWidget *); | ||
105 | iBool isFocused_Widget (const iWidget *); | ||
106 | iBool isHover_Widget (const iWidget *); | ||
107 | iBool isSelected_Widget (const iWidget *); | ||
108 | iBool isCommand_Widget (const iWidget *d, const SDL_Event *ev, const char *cmd); | ||
109 | iBool hasParent_Widget (const iWidget *d, const iWidget *someParent); | ||
110 | void setId_Widget (iWidget *, const char *id); | ||
111 | void setFlags_Widget (iWidget *, int flags, iBool set); | ||
112 | void setPos_Widget (iWidget *, iInt2 pos); | ||
113 | void setSize_Widget (iWidget *, iInt2 size); | ||
114 | void setBackgroundColor_Widget (iWidget *, int bgColor); | ||
115 | void setCommandHandler_Widget (iWidget *, iBool (*handler)(iWidget *, const char *)); | ||
116 | iAny * addChild_Widget (iWidget *, iAnyObject *child); /* holds a ref */ | ||
117 | iAny * addChildPos_Widget (iWidget *, iAnyObject *child, enum iWidgetAddPos addPos); | ||
118 | iAny * addChildFlags_Widget(iWidget *, iAnyObject *child, int childFlags); /* holds a ref */ | ||
119 | iAny * removeChild_Widget (iWidget *, iAnyObject *child); /* returns a ref */ | ||
120 | iAny * child_Widget (iWidget *, size_t index); /* O(n) */ | ||
121 | void arrange_Widget (iWidget *); | ||
122 | iBool dispatchEvent_Widget(iWidget *, const SDL_Event *); | ||
123 | iBool processEvent_Widget (iWidget *, const SDL_Event *); | ||
124 | void postCommand_Widget (const iWidget *, const char *cmd, ...); | ||
125 | |||
126 | void setFocus_Widget (iWidget *); | ||
127 | iWidget *focus_Widget (void); | ||
128 | iWidget *hover_Widget (void); | ||
129 | void unhover_Widget (void); | ||
130 | void setMouseGrab_Widget (iWidget *); | ||
131 | iWidget *mouseGrab_Widget (void); | ||