register_widget_type() can now be called in any order.
[xuni.git] / src / widget / widgets.h
blob15ab8fbe8fba235d8d7e52b892a13796fa01a636
1 /*! \file widgets.h
3 */
5 #ifndef XUNI_GUARD_WIDGETS_H
6 #define XUNI_GUARD_WIDGETS_H
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
12 /*! The type of a widget.
14 The order of these enumerated values should not matter.
16 enum widget_type_t {
17 WIDGET_NONE = -1,
18 WIDGET_BOX,
19 WIDGET_BUTTON,
20 WIDGET_CHECKBOX,
21 WIDGET_COMBOBOX,
22 WIDGET_FONT,
23 WIDGET_IMAGE,
24 WIDGET_IMAGE_TILE,
25 WIDGET_LABEL,
26 WIDGET_LISTBOX,
27 WIDGET_PANEL,
28 WIDGET_SCROLLBAR,
29 WIDGET_TEXTAREA,
30 WIDGET_TEXTBOX,
31 WIDGET_THEME,
32 WIDGET_LAST
34 #if !1
35 /* ideas */
36 WIDGET_PULLDOWN,
37 WIDGET_MENU,
38 WIDGET_TAB,
39 WIDGET_GROUP,
40 WIDGET_RADIOBUTTON
41 #endif
44 enum widget_visibility_t {
45 /*! If unset, the widget cannot be selected -- that is, when sel members
46 are being set, this widget is simply passed over. Parents and children
47 of this widget are unaffected by this flag.
49 WIDGET_VISIBILITY_SELABLE = 1 << 0,
51 /*! If unset, when the widget is selected, its parent widget is considered
52 for selection. That is, the parent could have its sel member set as
53 well. This original widget still gets assigned to the sel->p.widget
54 pointer, however.
56 WIDGET_VISIBILITY_INDEPENDENT = 1 << 1,
58 /*! If unset, when the widget is selected, all of its children are
59 considered for selection (rather than just the last widget painted).
61 This is the inverse of WIDGET_VISIBILITY_INDEPENDENT.
63 WIDGET_VISIBILITY_NOT_COMPOSE = 1 << 2,
65 /*! If unset, the widget is always in "normal" mode; the widget does
66 not respond to hovering or clicking.
68 WIDGET_VISIBILITY_CLICKABLE = 1 << 3,
70 /*! If unset, the widget is completely invisible. */
71 WIDGET_VISIBILITY_VISIBLE = 1 << 4,
73 /*! All visibility types enabled (default). */
74 WIDGET_VISIBILITY_ALL = (1 << 5) - 1
77 typedef size_t widget_id_type_t;
79 /*! A nameid, a way to efficiently refer to a widget.
81 When widgets are created from resource files, their names are in strings.
82 However, referencing widgets by strings is inefficient. By "binding" a
83 number to each widget that needs referencing, much more efficient
84 referencing can be achieved.
86 These numbers are called "wids", short for "widget identifiers". They are
87 also frequently referred to as "nameids", or even just "ids".
89 These wids which are bound to widgets are specific to each panel. That is,
90 each panel can have its own wid 0, for example. wids are usually specified
91 as enums so that they increment automatically. However, wids need not be
92 contiguous. (Though if they are not, memory would be wasted.)
94 widget_nameid_access(), or, for multiple nested nameids,
95 widget_nameid_follow(), can be used to access these numerical widget ids.
97 struct widget_nameid_t {
98 widget_id_type_t id; /* mostly redundant */
99 const char *name; /* mostly redundant */
100 struct widget_t *widget;
103 /*! An array of struct widget_nameid_ts. Used by panels via
104 struct panel_data_t.
106 struct nameid_t {
107 struct widget_nameid_t *widget;
108 size_t widgets;
111 struct xuni_accelerator_key_t {
112 SDL_keysym key;
113 struct widget_t *widget;
116 struct xuni_accelerator_t {
117 struct xuni_accelerator_key_t *key;
118 size_t n;
121 enum widget_event_t {
122 WIDGET_EVENT_FREE,
123 WIDGET_EVENT_UPDATE_TEXT,
124 WIDGET_EVENT_PAINT,
125 WIDGET_EVENT_REPOSITION,
126 WIDGET_EVENT_RESCALE,
127 /*WIDGET_EVENT_ACTIVATE,
128 WIDGET_EVENT_REVERT,
129 WIDGET_EVENT_DEACTIVATE,*/
130 WIDGET_EVENTS
133 typedef void (*widget_event_handler_t)(struct xuni_t *xuni,
134 struct widget_t *widget, enum widget_event_t event);
136 typedef void (*widget_event_func_t)(struct xuni_t *xuni,
137 struct widget_t *widget);
139 struct wtype_t {
140 const char *name;
141 /*size_t type;*/
142 widget_event_handler_t handler;
145 struct wtype_array_t {
146 struct wtype_t *type;
147 size_t types;
150 /* !!! needs alphabetical ordering */
151 enum panel_event_type_t {
152 PANEL_EVENT_NONE = -1,
153 PANEL_EVENT_FREE,
154 PANEL_EVENT_EVENT,
155 PANEL_EVENT_INIT,
156 PANEL_EVENT_PAINT,
157 PANEL_EVENT_START,
158 PANEL_EVENT_CLICK,
159 PANEL_EVENT_DEACTIVATE,
160 PANEL_EVENT_SEL,
161 PANEL_EVENTS
164 struct panel_event_init_t {
165 struct widget_t *panel;
166 struct resource_t *settings;
169 struct panel_event_start_t {
170 int unused;
173 struct panel_event_event_t {
174 panel_type_t *mode;
175 SDL_Event *event;
176 struct widget_t *widget;
179 struct panel_event_sel_t {
180 panel_type_t mode;
181 int xp, yp;
182 int click;
185 struct panel_event_click_t {
186 struct widget_t *widget;
187 panel_type_t *mode;
190 struct panel_event_deactivate_t {
191 struct widget_t *widget;
194 struct panel_event_paint_t {
195 panel_type_t mode;
198 struct panel_event_free_t {
199 int unused;
202 struct panel_data_t;
204 typedef int (*panel_event_func_t)(struct xuni_t *xuni,
205 struct panel_data_t *data);
207 struct panel_event_t {
208 enum panel_event_type_t type;
210 union {
211 struct panel_event_init_t init;
212 struct panel_event_start_t start;
213 struct panel_event_event_t event;
214 struct panel_event_sel_t sel;
215 struct panel_event_click_t click;
216 struct panel_event_deactivate_t deactivate;
217 struct panel_event_paint_t paint;
218 struct panel_event_free_t free;
219 } p;
221 panel_event_func_t handler;
224 #if 0
225 typedef void (*init_func_t)(void *vdata, struct xuni_t *xuni,
226 struct widget_t *panel, struct resource_t *settings);
227 typedef void (*start_func_t)(void *vdata, struct xuni_t *xuni);
228 typedef int (*event_func_t)(void *vdata, struct xuni_t *xuni,
229 panel_type_t *mode, SDL_Event *event);
230 typedef int (*set_widget_sel_func_t)(struct xuni_t *xuni, panel_type_t mode,
231 int xp, int yp, int click, void *vdata);
232 typedef int (*perform_click_func_t)(struct widget_t *widget,
233 panel_type_t *mode, void *vdata, struct xuni_t *xuni);
234 typedef void (*deactivate_func_t)(void *vdata, struct xuni_t *xuni,
235 struct widget_t *widget);
236 typedef void (*paint_func_t)(void *vdata, panel_type_t mode,
237 struct xuni_t *xuni);
238 typedef void (*free_func_t)(void *vdata, struct xuni_t *xuni);
239 #endif
241 struct panel_data_t {
242 void *data;
243 int frameupdate;
245 struct panel_event_t event[PANEL_EVENTS];
247 struct nameid_t *nameid;
249 struct xuni_accelerator_t *accel;
252 /*! Generic GUI widget structure. */
253 struct widget_t {
254 char *name;
255 widget_id_type_t id;
257 struct widget_t *base;
258 size_t type;
260 union {
261 struct box_t *box;
262 struct button_t *button;
263 struct checkbox_t *checkbox;
264 struct combobox_t *combobox;
265 struct font_t *font;
266 struct image_t *image;
267 struct image_tile_t *image_tile;
268 struct label_t *label;
269 struct listbox_t *listbox;
270 struct panel_data_t *panel;
271 struct scrollbar_t *scrollbar;
272 struct textarea_t *textarea;
273 struct textbox_t *textbox;
274 struct theme_data_t *theme;
275 void *p;
276 } p;
278 struct panel_t *compose;
279 struct pos_t *pos;
281 enum widget_visibility_t visibility;
283 struct widget_t *selwidget;
285 int sel;
286 int repaint; /* !!! ... */
289 void delete_widget(struct xuni_t *xuni, struct panel_t *panel, size_t n);
290 void delete_widget_pointer(struct xuni_t *xuni, struct widget_t *widget);
291 void add_widget(struct panel_t *panel, struct widget_t *widget);
292 void add_allocate_widget(struct widget_t *base, char *name);
293 void add_allocate_widget_compose(struct widget_t *base, char *name);
294 struct panel_t *allocate_panel(struct widget_t *base);
295 struct widget_t *allocate_widget(char *name, struct widget_t *base);
296 void init_widget_pos(struct widget_t *widget, double x, double y,
297 double w, double h, enum pos_pack_t pack);
298 void clear_widget_clip(struct xuni_t *xuni, struct widget_t *widget);
299 void add_panel_clip(struct xuni_t *xuni, struct widget_t *widget,
300 int xoff, int yoff, int xclip, int yclip, int wclip, int hclip);
301 void add_widget_clip_nonrec(struct xuni_t *xuni, struct widget_t *widget,
302 int xoff, int yoff, int xclip, int yclip, int wclip, int hclip);
303 void add_widget_clip(struct xuni_t *xuni, struct widget_t *widget,
304 int xoff, int yoff, int xclip, int yclip, int wclip, int hclip);
305 void add_widget_accelerator(struct xuni_t *xuni, struct widget_t *panel,
306 struct widget_t *widget, SDLKey key, SDLMod mod);
307 struct widget_t *last_compose_widget(struct widget_t *widget);
308 struct widget_t *widget_nameid_access(struct widget_t *widget, size_t n);
309 struct widget_t *widget_nameid_follow(struct widget_t *widget, ...);
310 int widget_is_parent(struct widget_t *search, struct widget_t *widget);
311 int widget_name_match(const char *string, const char *buffer, size_t len);
312 struct widget_t *find_widget(struct widget_t *widget, const char *name);
313 void widget_event(struct xuni_t *xuni, struct widget_t *widget,
314 enum widget_event_t event);
315 void widget_compose_event(struct xuni_t *xuni, struct widget_t *widget,
316 enum widget_event_t event);
317 int get_real_pos(double scale, int basereal);
319 void init_wtype(struct xuni_t *xuni, struct wtype_array_t *wtype);
320 void free_wtype(struct wtype_array_t *wtype);
321 void register_widget_type(struct xuni_t *xuni, const char *name, size_t type,
322 widget_event_handler_t handler);
323 void call_widget_event(struct xuni_t *xuni, struct widget_t *widget,
324 enum widget_event_t type);
325 void call_widget_event_func(struct xuni_t *xuni, struct widget_t *widget,
326 enum widget_event_t event, widget_event_func_t function[],
327 size_t functions);
329 int assert_widget_type(struct xuni_t *xuni, struct widget_t *widget,
330 size_t type, const char *file, int line);
332 int get_widget_visibility(struct widget_t *widget,
333 enum widget_visibility_t visibility);
334 void set_widget_visibility(struct widget_t *widget,
335 enum widget_visibility_t visibility, int set);
337 #ifdef __cplusplus
339 #endif
341 /* only because of init_wtype() */
342 #include "box.h"
343 #include "button.h"
344 #include "checkbox.h"
345 #include "combobox.h"
346 #include "font.h"
347 #include "image.h"
348 #include "image_tile.h"
349 #include "label.h"
350 #include "listbox.h"
351 #include "panel.h"
352 #include "scrollbar.h"
353 #include "textarea.h"
354 #include "textbox.h"
355 #include "theme.h"
357 #endif