GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_widget_ops.h
1//SPDX-License-Identifier: LGPL-2.0-or-later
2
3/*
4
5 Copyright (c) 2014-2020 Cyril Hrubis <metan@ucw.cz>
6
7 */
8
9#ifndef GP_WIDGET_OPS_H
10#define GP_WIDGET_OPS_H
11
12#include <input/gp_event.h>
13#include <utils/gp_bbox.h>
14#include <utils/gp_json.h>
15#include <widgets/gp_widget.h>
16#include <widgets/gp_widget_render.h>
17
18enum gp_widget_focus_flag {
19 GP_FOCUS_OUT,
20 GP_FOCUS_IN,
21 GP_FOCUS_LEFT,
22 GP_FOCUS_RIGHT,
23 GP_FOCUS_UP,
24 GP_FOCUS_DOWN,
25 GP_FOCUS_NEXT,
26 GP_FOCUS_PREV,
27};
28
29struct json_object;
30
31typedef struct gp_offset {
32 gp_coord x;
33 gp_coord y;
34} gp_offset;
35
36enum gp_widget_render_flags {
37 GP_WIDGET_REDRAW = 0x01,
38 GP_WIDGET_REDRAW_CHILDREN = 0x02,
40 GP_WIDGET_COLOR_SCHEME = 0x04,
42 GP_WIDGET_RESIZE = 0x08,
44 GP_WIDGET_DISABLED = 0x10,
45};
46
47struct gp_widget_ops {
55 void (*free)(gp_widget *self);
56
62 int (*event)(gp_widget *self, const gp_widget_render_ctx *ctx, gp_event *ev);
63
71 void (*render)(gp_widget *self, const gp_offset *offset,
72 const gp_widget_render_ctx *ctx, int flags);
73
74 /*
75 * Moves to focused widget.
76 */
77 int (*focus)(gp_widget *self, int focus_dir);
78
79 /*
80 * Moves focus to widget on x, y coordinates.
81 */
82 int (*focus_xy)(gp_widget *self, const gp_widget_render_ctx *ctx,
83 unsigned int x, unsigned int y);
84
85 /*
86 * @brief Sets/moves focus to a child specific child widget.
87 *
88 * @self A container widget.
89 * @child A child widget to be focused.
90 *
91 * @return Non-zero if widget was focused successfuly.
92 */
93 int (*focus_child)(gp_widget *self, gp_widget *child);
94
95 /*
96 * Called once to calculate minimal widget sizes.
97 */
98 unsigned int (*min_w)(gp_widget *self, const gp_widget_render_ctx *ctx);
99 unsigned int (*min_h)(gp_widget *self, const gp_widget_render_ctx *ctx);
100
110 void (*distribute_w)(gp_widget *self,
111 const gp_widget_render_ctx *ctx,
112 int new_wh);
113
114 void (*distribute_h)(gp_widget *self,
115 const gp_widget_render_ctx *ctx,
116 int new_wh);
117
124 void (*for_each_child)(gp_widget *self, void (*func)(gp_widget *child));
125
126 /*
127 * json_object -> widget converter.
128 */
129 gp_widget *(*from_json)(gp_json_reader *json, gp_json_val *val, gp_widget_json_ctx *ctx);
130
131 /* id used for JSON loader */
132 const char *id;
133};
134
135const struct gp_widget_ops *gp_widget_ops(gp_widget *self);
136
137const struct gp_widget_ops *gp_widget_ops_by_id(const char *id);
138
139const char *gp_widget_type_id(gp_widget *self);
140
141const char *gp_widget_type_name(enum gp_widget_type type);
142
143unsigned int gp_widget_min_w(gp_widget *self, const gp_widget_render_ctx *ctx);
144
145unsigned int gp_widget_min_h(gp_widget *self, const gp_widget_render_ctx *ctx);
146
147unsigned int gp_widget_align(gp_widget *self);
148
149int gp_widget_input_event(gp_widget *self, const gp_widget_render_ctx *ctx, gp_event *ev);
150
151void gp_widget_ops_render(gp_widget *self, const gp_offset *offset,
152 const gp_widget_render_ctx *ctx, int flags);
153
154/*
155 * Send event to a widget.
156 *
157 * @self A widget to send the event to.
158 * @ctx A render context.
159 * @ev An input event.
160 *
161 * @return Zero if event wasn't handled, non-zero otherwise.
162 */
163int gp_widget_ops_event(gp_widget *self, const gp_widget_render_ctx *ctx, gp_event *ev);
164
165/*
166 * Send event to a widget with a cursor offset.
167 *
168 * Same as gp_widget_ops_event() but with offset to the absolute coordinates is
169 * applied so that the event is relative to the cursor coordinates.
170 *
171 * @self A widget to send the event to.
172 * @ctx A render context.
173 * @ev An input event.
174 * @off_x X cursor offset.
175 * @off_y Y cursor offset.
176 *
177 * @return Zero if event wasn't handled, non-zero otherwise.
178 */
179int gp_widget_ops_event_offset(gp_widget *self, const gp_widget_render_ctx *ctx,
180 gp_event *ev, gp_size off_x, gp_size off_y);
181
190int gp_widget_ops_render_focus(gp_widget *self, int focus_dir);
191
202int gp_widget_ops_render_focus_xy(gp_widget *self, const gp_widget_render_ctx *ctx,
203 unsigned int x, unsigned int y);
204
214int gp_widget_ops_focus_widget(gp_widget *self);
215
216void gp_widget_ops_distribute_w(gp_widget *self, const gp_widget_render_ctx *ctx, unsigned int w, int new_wh);
217void gp_widget_ops_distribute_h(gp_widget *self, const gp_widget_render_ctx *ctx, unsigned int h, int new_wh);
218
230void gp_widget_ops_for_each_child(gp_widget *self, void (*func)(gp_widget *child));
231
239static inline void gp_widget_ops_blit(const gp_widget_render_ctx *ctx,
240 gp_coord x, gp_coord y,
241 gp_size w, gp_size h)
242{
243 if (!ctx->flip)
244 return;
245
246 if (gp_bbox_empty(*ctx->flip))
247 *ctx->flip = gp_bbox_pack(x, y, w, h);
248 else
249 *ctx->flip = gp_bbox_merge(*ctx->flip, gp_bbox_pack(x, y, w, h));
250}
251
260static inline int gp_widget_should_redraw(gp_widget *self, int flags)
261{
262 return self->redraw || (flags & GP_WIDGET_REDRAW);
263}
264
265static inline int gp_widget_is_disabled(gp_widget *self, int flags)
266{
267 return self->disabled || (flags & GP_WIDGET_DISABLED);
268}
269
282void gp_widget_calc_size(gp_widget *layout, const gp_widget_render_ctx *ctx,
283 unsigned int w, unsigned int h, int new_wh);
284
292void gp_widget_redraw(gp_widget *self);
293
302void gp_widget_resize(gp_widget *self);
303
312void gp_widget_redraw_children(gp_widget *self);
313
314/*
315 * Resizes and redraws changed widgets.
316 */
317void gp_widget_render(gp_widget *self, const gp_widget_render_ctx *ctx, int new_wh);
318
319#endif /* GP_WIDGET_OPS_H */
int gp_coord
Integer type for coordinates i.e. x, y, ...
Definition gp_types.h:19
unsigned int gp_size
Integer type for sizes i.e. w, h, ...
Definition gp_types.h:24
A bounding box implementation.
static gp_bbox gp_bbox_merge(gp_bbox box1, gp_bbox box2)
Merges two bounding boxes.
Definition gp_bbox.h:70
static int gp_bbox_empty(gp_bbox box)
Returns true if bounding box is empty.
Definition gp_bbox.h:35
static gp_bbox gp_bbox_pack(gp_coord x, gp_coord y, gp_coord w, gp_coord h)
Creates a bounding box from coordinates and size.
Definition gp_bbox.h:50
Includes all JSON headers.
A JSON parser internal state.
A parsed JSON key value pair.
A context to propagate values top down and bottom up.