-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg.h
288 lines (249 loc) · 8.32 KB
/
pg.h
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
/*
PG embedded graphic library
Copyright (c) 2009 jkmnt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef __PG_H__
#define __PG_H__
/** Public */
#include "pg_config.h"
#include "pg_tagitems.h"
#include "pg_attributes.h"
#include "pg_utf8.h"
#include "pg_primitives.h"
#include "pg_theme.h"
typedef struct pg_obj_t pg_obj_t;
typedef pg_obj_t *pg_obj_p;
typedef void (* pg_hook_t)( pg_obj_t *obj);
typedef void (* pg_updhook_t)( pg_obj_t *obj, int Pos);
extern pg_obj_t *const pg_screen; // User-accessed root
/**
* Create PG object
*
* @param type - object type (pg_objs_e)
* @param parent - parent object or pg_screen
* @param left - left coordinate relative to parent left
* @param top - top coordinate relative to parent top
* @param right - right coordinate relative to parent left (if > 0) or right ( <= 0)
* @param bot - bottom coordinate relative to parent top (if > 0) or bottom ( <= 0)
* @param tl - NULL-terminated taglist of attribute/value pairs to be applied at creation
*
* @return pg_obj_t* dynamically allocated PG object or NULL if memory allocation failed
*/
pg_obj_t *pg_create( enum pg_objs_e type, pg_obj_t *parent, int left, int top, int right, int bot, pg_ctaglist_p tl);
/**
* Set attribute of PG object
*
* @param obj - PG object
* @param id - attribute id
* @param val - attribute value
*/
void pg_set(pg_obj_t *obj, enum pg_attrs_e id, pg_val_t val);
/**
* Set multiple attributes of PG object.
* Attributes are applied in order of appearance
*
* @param obj - PG object
* @param tl - NULL-terminated taglist of attribute/value pairs
*/
void pg_set_via_taglist( pg_obj_t *obj, pg_ctaglist_p tl);
/**
* Kill PG object.
* It is ok to pass NULL pointer. Pass pg_screen is to kill all user-created objects
*
* @param obj - PG object
*/
void pg_kill( pg_obj_t *obj);
/**
* Render screen
*
* @return int - 0 if no render was executed (no screen update is require)
*/
int pg_render( void);
/**
* Get child of object by index
*
* @param obj - object to search child of
* @param num - child index
*
* @return pg_obj_t* - PG object or NULL
*/
pg_obj_t *pg_get_child( const pg_obj_t *obj, int num);
/**
* Get parent of object
*
* @param me - object to search parent of
*
* @return pg_obj_t* - PG object or NULL
*/
pg_obj_t *pg_get_parent( const pg_obj_t *obj);
/**
* Get next brother of PG object
*
* @param obj - object to search brother of
*
* @return pg_obj_t* - brother of object or NULL
*/
pg_obj_t *pg_get_next_brother( const pg_obj_t *obj);
/**
* Request update of object at the next pass of render
*
* @param obj - PG object
*/
void pg_force_update( pg_obj_t *obj);
/**
* Push object down
*
* @param obj - PG object
*/
void pg_lower( pg_obj_t *obj);
/**
* Pull object up
*
* @param obj - PG object
*/
void pg_lift( pg_obj_t *obj);
/**
* Pull object to the foreground
*
* @param obj - PG object
*/
void pg_tofront( pg_obj_t *obj);
/**
* Push object to the background
*
* @param obj - PG object
*/
void pg_toback( pg_obj_t *obj);
/**
* Move object by a specified amount.
* Movement is limited by parent object
*
* NOTE: may fail if coordinates are out of screen bounds
*
* @param obj - PG object
* @param dx - delta X
* @param dy - delta Y
*/
void pg_move( pg_obj_t *obj, int dx, int dy);
/**
* Move object to a new coordinates relative to parent box
* Movement is limited by parent object
*
* NOTE: object may be resized or behave strange if corner cases.
* Use with care
*
* @param obj - PG object
* @param x0 - new left
* @param y0 - new top
*/
void pg_moveto( pg_obj_t *obj, uint x0, uint y0);
/**
* Execute a method of object
*
* @param obj - PG object
* @param id - method id
* @param val - method arguments
*/
void pg_act( pg_obj_t* obj, enum pg_acts_e id, pg_val_t val);
/**
* Get a value of PG object's attribute
*
* @param obj - PG object
* @param id - attribute
* @param val - value output
*
* @return int - 0 if get failed
*/
int pg_get( pg_obj_t *obj, pg_id_t id, pg_val_t *val);
/**
* Get current dirty rectangle (right top point is included, closed ranges)
*
* @return const pg_fast_area_t* dirty rectange or NULL if everything up to date
*/
const pg_fast_area_t *pg_get_dirty_rect(void);
/**
* PG animate clock. Call periodically to advance animation of bitmaps
*/
void pg_animate(void);
void pg_print_tree( void);
// Kind-of va-arg set and create
#define pg_create_via_tags( type, parent, l, t, r, b, ...) \
pg_create((type), (parent), (l), (t), (r), (b), (const pg_tag_t *) (pg_item_t []){__VA_ARGS__, PG_TAG_END, 0})
#define pg_set_via_tags(obj, ...) \
pg_set_via_taglist((obj), (const pg_tag_t *) (pg_item_t []){__VA_ARGS__, PG_TAG_END, 0})
#ifdef __PG_C__
/** Private */
enum base_flags_e
{
PG_F_INITED = 1U << 0,
PG_F_HIDDEN = 1U << 1,
PG_F_HL = 1U << 2,
PG_F_OFF = 1U << 3,
PG_F_PROPAGATE_DECO = 1U << 4,
_PG_F_USER = 1U << 5,
};
/** Description of drawable object*/
struct pg_obj_t
{
pg_area_t area;
enum pg_objs_e type; // Class id
uint32_t flags; // Some flags
pg_theme_p theme; // Pointer to current theme
pg_obj_t *parent; // Pointer to parent. 0 if nonattached
pg_obj_t *first_child; // Pointer to first child. 0 if no childs
pg_obj_t *next_bro; // Pointer to next brother. 0 if no younger brothers exist
#if PG_USE_PDHOOK
pg_hook_t postdraw_hook; // Hook to be executed immidiately after render. May be used for
// custom draw, prining debug, posting event etc.
#endif
#if PG_USE_USERDATA
uint32_t usr_data; // Space to store some user-defined info
#endif
};
typedef struct
{
pg_obj_t *(*create)( void); // Create function (allocate memory and set some parameters)
int (*set)( pg_obj_t *, pg_id_t, pg_val_t); // Set parameters
int (*get)( pg_obj_t *, pg_id_t, pg_val_t *); // Get parameters
void (*act)( pg_obj_t *, pg_id_t, pg_val_t); // Do some action
void (*init)( pg_obj_t *); // Init - called after creation and doing optional sets
void (*rend)( pg_obj_t *); // Renderer
void (*del)( pg_obj_t *); // Free memory
} obj_handler_t;
typedef struct
{
pg_obj_t *root; // Root for tree iterator
pg_obj_t *curr; // Current object for tree iterator
} tree_iterator_t;
static inline int is_inited( const pg_obj_t *Me) { return Me->flags & PG_F_INITED; }
static inline const pg_deco_t *sel_deco( const pg_obj_t *me)
{
switch ( me->flags & (PG_F_OFF | PG_F_HL))
{
default:
case 0: return &me->theme->norm;
case PG_F_HL: return &me->theme->hl;
case PG_F_OFF: return &me->theme->off;
case PG_F_HL | PG_F_OFF: return &me->theme->hloff;
}
}
int pg_base_set( pg_obj_t *obj, pg_id_t id, pg_val_t val);
int pg_base_get( pg_obj_t *obj, pg_id_t id, pg_val_t *val);
void pg_base_act( pg_obj_t* me, pg_id_t id, pg_val_t val);
#endif
#endif