-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathelement.h
More file actions
386 lines (344 loc) · 10.9 KB
/
element.h
File metadata and controls
386 lines (344 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#ifndef PATCHWORK_ELEMENT_H
#define PATCHWORK_ELEMENT_H 1
#include "cmd.h"
#include "drawable.h"
#include "element_id.h"
#include "font.h"
#include "procedure.h"
#include "rect.h"
#include "theme.h"
#include <stdbool.h>
#include <stdint.h>
#include <sys/proc.h>
#if defined(__cplusplus)
extern "C"
{
#endif
/**
* @brief UI Elements.
* @defgroup libpatchwork_element UI Elements
* @ingroup libpatchwork
*
* A window is made up of a tree of elements, each element is responsible for drawing a part of the window and handling
* events for that part. Elements can have child elements, which are drawn on top of the parent element.
*
* Each element will on creation, copy the current global theme as its own theme, which can then be modified on a
* per-element basis.
*
* @{
*/
/**
* @brief Element flags type.
* @typedef element_flags_t
*
* We make this a uint64_t instead an an enum to give us more flags for the future.
*/
typedef uint64_t element_flags_t;
#define ELEMENT_NONE 0
#define ELEMENT_TOGGLE (1 << 0)
#define ELEMENT_FLAT (1 << 1)
#define ELEMENT_NO_BEZEL (1 << 2)
#define ELEMENT_NO_OUTLINE (1 << 3)
/**
* @brief Opaque element structure.
* @struct element_t
*/
typedef struct element element_t;
/**
* @brief Element text properties structure.
* @struct text_props_t
*
* To avoid code duplication, we implement a text properties structure that can be used by any element that
* needs to render text.
*/
typedef struct
{
align_t xAlign;
align_t yAlign;
font_t* font;
} text_props_t;
/**
* @brief Element image properties structure.
* @struct image_props_t
*
* To avoid code duplication, we implement an image properties structure that can be used by any element that
* needs to render an image.
*/
typedef struct
{
align_t xAlign;
align_t yAlign;
point_t srcOffset;
} image_props_t;
/**
* @brief Allocate and initialize a new element.
*
* Will send a fake `EVENT_LIB_INIT` event to the element after creation, followed by a real `EVENT_LIB_REDRAW` event.
*
* A event being fake just means its sent by directly calling the element procedure, instead of being pushed to the
* display's event queue.
*
* @param parent The parent element.
* @param id The element ID.
* @param rect The elements rectangle relative to its parent.
* @param text The elements text, if the element is for example a button, this will be the button label.
* @param flags The element flags.
* @param procedure The element procedure.
* @param private Pointer to private data for the element.
* @return On success, a pointer to the new element. On failure, `NULL` and `errno` is set.
*/
element_t* element_new(element_t* parent, element_id_t id, const rect_t* rect, const char* text, element_flags_t flags,
procedure_t procedure, void* data);
/**
* @brief Deinitialize and free an element and all its children.
*
* Will send a fake `EVENT_LIB_DEINIT` event to the element before freeing it.
*
* @param elem The element to free.
*/
void element_free(element_t* elem);
/**
* @brief Find a child element by its ID.
*
* Will search recursively through all child elements.
*
* @param elem The element to search from.
* @param id The element ID to search for.
* @return A pointer to the found element, or `NULL` if not found.
*/
element_t* element_find(element_t* elem, element_id_t id);
/**
* @brief Set private data for an element.
*
* @param elem The element.
* @param private Pointer to the private data.
*/
void element_set_private(element_t* elem, void* data);
/**
* @brief Get private data for an element.
*
* @param elem The element.
* @return Pointer to the private data, or `NULL` if none is set.
*/
void* element_get_private(element_t* elem);
/**
* @brief Get the ID of an element.
*
* @param elem The element.
* @return The element ID, or `ELEMENT_ID_NONE` if `elem` is `NULL`.
*/
element_id_t element_get_id(element_t* elem);
/**
* @brief Move an element to a new rectangle in its parent's coordinate space.
*
* Will NOT redraw the element, call `element_redraw()` if needed.
*
* @param elem The element.
* @param rect The new rectangle.
*/
void element_move(element_t* elem, const rect_t* rect);
/**
* @brief Get the rectangle of an element in its parent's coordinate space.
*
* Equivalent to `RECT_INIT_DIM(x, y, width, height)`.
*
* @param elem The element.
* @return The element rectangle, or a zero-area rectangle if `elem` is `NULL`.
*/
rect_t element_get_rect(element_t* elem);
/**
* @brief Get the element's rectangle in local coordinates.
*
* Equivalent to `RECT_INIT_DIM(0, 0, width, height)`.
*
* @param elem The element.
* @return The content rectangle, or a zero-area rectangle if `elem` is `NULL`.
*/
rect_t element_get_content_rect(element_t* elem);
/**
* @brief Get the rectangle of an element in window coordinates.
*
* @param elem The element.
* @return The element rectangle in window coordinates, or a zero-area rectangle if `elem` is `NULL`.
*/
rect_t element_get_window_rect(element_t* elem);
/**
* @brief Get the top-left point of an element in window coordinates.
*
* @param elem The element.
* @return The top-left point in window coordinates, or (0, 0) if `elem` is `NULL`.
*/
point_t element_get_window_point(element_t* elem);
/**
* @brief Convert a rectangle from element coordinates to window coordinates.
*
* @param elem The element.
* @param src The source rectangle in element coordinates.
* @return The rectangle in window coordinates, or a zero-area rectangle if `elem` or `src` is `NULL`.
*/
rect_t element_rect_to_window(element_t* elem, const rect_t* src);
/**
* @brief Convert a point from element coordinates to window coordinates.
*
* @param elem The element.
* @param src The source point in element coordinates.
* @return The point in window coordinates, or (0, 0) if `elem` or `src` is `NULL`.
*/
point_t element_point_to_window(element_t* elem, const point_t* src);
/**
* @brief Convert a rectangle from window coordinates to element coordinates.
*
* @param elem The element.
* @param src The source rectangle in window coordinates.
* @return The rectangle in element coordinates, or a zero-area rectangle if `elem` or `src` is `NULL`.
*/
rect_t element_window_to_rect(element_t* elem, const rect_t* src);
/**
* @brief Convert a point from window coordinates to element coordinates.
*
* @param elem The element.
* @param src The source point in window coordinates.
* @return The point in element coordinates, or (0, 0) if `elem` or `src` is `NULL`.
*/
point_t element_window_to_point(element_t* elem, const point_t* src);
/**
* @brief Get the flags of an element.
*
* @param elem The element.
* @return The element flags, or `ELEMENT_NONE` if `elem` is `NULL`.
*/
element_flags_t element_get_flags(element_t* elem);
/**
* @brief Set the flags of an element.
*
* @param elem The element.
* @param flags The new element flags.
*/
void element_set_flags(element_t* elem, element_flags_t flags);
/**
* @brief Get the text of an element.
*
* @param elem The element.
* @return The element text, or `NULL` if `elem` is `NULL`.
*/
const char* element_get_text(element_t* elem);
/**
* @brief Set the text of an element.
*
* Will NOT redraw the element, call `element_redraw()` if needed.
*
* @param elem The element.
* @param text The new text.
* @return On success, `0`. On failure, `ERR` and `errno` is set.
*/
uint64_t element_set_text(element_t* elem, const char* text);
/**
* @brief Get the text properties of an element.
*
* The returned pointer can be used to modify the text properties.
*
* @param elem The element.
* @return Pointer to the text properties, or `NULL` if `elem` is `NULL`.
*/
text_props_t* element_get_text_props(element_t* elem);
/**
* @brief Get the image of an element.
*
* @param elem The element.
* @return Pointer to the image, or `NULL` if `elem` is `NULL` or has no image.
*/
image_t* element_get_image(element_t* elem);
/**
* @brief Set the image of an element.
*
* Will NOT redraw the element, call `element_redraw()` if needed.
*
* @param elem The element.
* @param image Pointer to the new image or `NULL` to remove the image.
*/
void element_set_image(element_t* elem, image_t* image);
/**
* @brief Get the image properties of an element.
*
* The returned pointer can be used to modify the image properties.
*
* @param elem The element.
* @return Pointer to the image properties, or `NULL` if `elem` is `NULL`.
*/
image_props_t* element_get_image_props(element_t* elem);
/**
* @brief Get the theme of an element.
*
* @param elem The element.
* @return Pointer to the theme, or `NULL` if `elem` is `NULL`.
*/
theme_t* element_get_theme(element_t* elem);
/**
* @brief Begin drawing to an element.
*
* Note that since this will fill the drawable structure with the element's content rectangle, if the element is for
* example moved or resized the drawable will be invalid.
*
* @param elem The element to draw to.
* @param draw Pointer to the drawable structure to initialize.
*/
void element_draw_begin(element_t* elem, drawable_t* draw);
/**
* @brief End drawing to an element.
*
* This will invalidate the area of the element that was drawn to and send redraw events to any child elements that
* overlap the invalid area.
*
* @param elem The element that was drawn to.
* @param draw Pointer to the drawable structure that was used for drawing.
*/
void element_draw_end(element_t* elem, drawable_t* draw);
/**
* @brief Redraw an element.
*
* Will push a `EVENT_LIB_REDRAW` event to the display event queue for the element, meaning the redraw event is not
* processed immediately.
*
* @param elem The element to redraw.
* @param shouldPropagate Whether the redraw event should propagate to child elements.
*/
void element_redraw(element_t* elem, bool shouldPropagate);
/**
* @brief Force an action on an element.
*
* Will push a `EVENT_LIB_FORCE_ACTION` event to the display event queue for the element, meaning the action event is
* not processed immediately.
*
* @param elem The element.
* @param action The action to force.
*/
void element_force_action(element_t* elem, action_type_t action);
/**
* @brief Dispatch an event to an element.
*
* This will call the element's procedure with the given event after some preprocessing.
*
* Most events will also be propagated to child elements by the element's procedure.
*
* @param elem The element.
* @param event The event to dispatch.
* @return The return value of the element's procedure.
*/
uint64_t element_dispatch(element_t* elem, const event_t* event);
/**
* @brief Emit an event to an element.
*
* This function will construct an event and dispatch it to the element.
*
* @param elem The element.
* @param type The event type.
* @param data Pointer to the event data, can be `NULL` if `size` is `0`.
* @param size The size of the event data.
* @return On success, `0`. On failure, `ERR` and `errno` is set.
*/
uint64_t element_emit(element_t* elem, event_type_t type, const void* data, uint64_t size);
/** @} */
#if defined(__cplusplus)
}
#endif
#endif