GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_widget.h
1//SPDX-License-Identifier: LGPL-2.0-or-later
2
3/*
4
5 Copyright (c) 2014-2024 Cyril Hrubis <metan@ucw.cz>
6
7 */
8
9#ifndef GP_WIDGET_H
10#define GP_WIDGET_H
11
12#include <stdlib.h>
13#include <stdint.h>
14#include <inttypes.h>
15#include <stdbool.h>
16#include <utils/gp_types.h>
17#include <widgets/gp_common.h>
18#include <widgets/gp_widget_types.h>
19
20struct gp_widget {
31 unsigned int type;
39 unsigned int widget_class;
46 gp_widget *parent;
53 int (*on_event)(gp_widget_event *);
54
55 /*
56 * User provided pointer to arbitrary data; useful for event handlers
57 * and other user defined functions. The library will not access or
58 * modify the memory pointed to by this.
59 */
60 void *priv;
61
62 /*
63 * Relative offset to the parent widget.
64 */
65 unsigned int x, y;
66
67 /*
68 * Current widget size.
69 */
70 unsigned int w, h;
71
72 /*
73 * Cached widget minimal size.
74 */
75 unsigned int min_w, min_h;
76
77 unsigned int align:16;
78 /*
79 * If set widget will not shrink, i.e. will be resized only when the
80 * new minimal size is bigger than the previous one.
81 *
82 * By default this is set to 0.
83 */
84 unsigned int no_shrink:1;
85 unsigned int no_resize:1;
86 /*
87 * If set the widget_ops_render() is called next time layout is repainted.
88 */
89 unsigned int redraw:1;
90 /*
91 * If set there is a child widget to be repainted, the widget_ops_render()
92 * function is called but the widget itself shouldn't be repainted.
93 */
94 unsigned int redraw_child:1;
95 /*
96 * Redraw whole subtree, i.e. all children and their children, etc.
97 */
98 unsigned int redraw_children:1;
99 unsigned int focused:1;
100
101 /* Set by resize w and h functions cleared after event was emitted */
102 unsigned int resized:1;
103
104 /*
105 * If set the widget cannot get focused and get events even if
106 * ops->event is implemented.
107 */
108 unsigned int no_events:1;
109
110 /*
111 * If set widget is 'grayed out' and no events are processed.
112 */
113 unsigned int disabled:1;
114
115 uint32_t event_mask;
116
117 union {
118 struct gp_widget_grid *grid;
119 struct gp_widget_tabs *tabs;
120
121 struct gp_widget_bool *b;
122 struct gp_widget_bool *button;
123 struct gp_widget_bool *checkbox;
124
125 struct gp_widget_label *label;
126
127 struct gp_widget_int *i;
128 struct gp_widget_pbar *pbar;
129 struct gp_widget_int *spin;
130 struct gp_widget_int *slider;
131
132 struct gp_widget_tbox *tbox;
133
134 struct gp_widget_choice *choice;
135
136 struct gp_widget_table *tbl;
137
138 struct gp_widget_pixmap *pixmap;
139
140 struct gp_widget_stock *stock;
141
142 struct gp_widget_scroll_area *scroll;
143
144 struct gp_widget_frame *frame;
145
146 struct gp_widget_markup *markup;
147
148 struct gp_widget_switch *switch_;
149
150 struct gp_widget_overlay *overlay;
151
152 struct gp_widget_log *log;
153
154 struct gp_widget_graph *graph;
155
156 void *payload;
157 };
158 char buf[];
159};
160
161enum gp_widget_type {
162 GP_WIDGET_GRID,
163 GP_WIDGET_TABS,
164 GP_WIDGET_BUTTON,
165 GP_WIDGET_CHECKBOX,
166 GP_WIDGET_LABEL,
167 GP_WIDGET_PROGRESSBAR,
168 GP_WIDGET_SPINNER,
169 GP_WIDGET_SLIDER,
170 GP_WIDGET_TBOX,
171 GP_WIDGET_RADIOBUTTON,
172 GP_WIDGET_SPINBUTTON,
173 GP_WIDGET_TABLE,
174 GP_WIDGET_PIXMAP,
175 GP_WIDGET_STOCK,
176 GP_WIDGET_SCROLL_AREA,
177 GP_WIDGET_FRAME,
178 GP_WIDGET_MARKUP,
179 GP_WIDGET_SWITCH,
180 GP_WIDGET_OVERLAY,
181 GP_WIDGET_LOG,
182 GP_WIDGET_GRAPH,
183 GP_WIDGET_MAX,
184};
185
186enum gp_widget_class {
187 GP_WIDGET_CLASS_NONE = 0,
188 GP_WIDGET_CLASS_BOOL,
189 GP_WIDGET_CLASS_INT,
190 GP_WIDGET_CLASS_CHOICE,
191 GP_WIDGET_CLASS_MAX,
192};
193
197const char *gp_widget_class_name(enum gp_widget_class widget_class);
198
199enum gp_widget_alignment {
201 GP_HCENTER_WEAK = 0x00,
202 GP_HCENTER = 0x01,
203 GP_LEFT = 0x02,
204 GP_RIGHT = 0x03,
205 GP_HFILL = 0x08,
207 GP_VCENTER_WEAK = 0x00,
208 GP_VCENTER = 0x10,
209 GP_TOP = 0x20,
210 GP_BOTTOM = 0x30,
211 GP_VFILL = 0x80,
212};
213
214#define GP_FILL (GP_VFILL | GP_HFILL)
215
216#define GP_HALIGN_MASK 0x0f
217#define GP_VALIGN_MASK 0xf0
218
228gp_widget *gp_widget_new(enum gp_widget_type type,
229 enum gp_widget_class widget_class,
230 size_t payload_size);
231
232#define GP_WIDGET_CLASS_ASSERT(self, wclass, ret) do { \
233 if (!self) { \
234 GP_BUG("NULL widget!"); \
235 return ret; \
236 } \
237 if (self->widget_class != wclass) { \
238 GP_BUG("Invalid widget (%p) class %u != %u", \
239 self, self->widget_class, wclass); \
240 return ret; \
241 } \
242 } while (0)
243
244#define GP_WIDGET_ASSERT(self, wtype, ret) do { \
245 if (!self) {\
246 GP_BUG("NULL widget!"); \
247 return ret; \
248 } else if (self->type != wtype) {\
249 GP_BUG("Invalid widget type %s != %s", \
250 gp_widget_type_id(self), gp_widget_type_name(wtype)); \
251 return ret; \
252 } \
253 } while (0)
254
267void gp_widget_free(gp_widget *self);
268
275void gp_widget_set_parent(gp_widget *self, gp_widget *parent);
276
285int gp_widget_focus_set(gp_widget *self);
286
294void gp_widget_disable(gp_widget *self);
295
303void gp_widget_enable(gp_widget *self);
304
313void gp_widget_disable_set(gp_widget *self, bool disable);
314
325static inline bool gp_widget_disabled(gp_widget *self)
326{
327 return self->disabled;
328}
329
340void gp_widget_on_event_set(gp_widget *self,
341 int (*on_event)(gp_widget_event *), void *priv);
342
344
345#endif /* GP_WIDGET_H */
Widget event handling.
Event structure passed to widget event handler.
Common header for types.