GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_widget_ops.h
Go to the documentation of this file.
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
17#ifndef GP_WIDGET_OPS_H
18#define GP_WIDGET_OPS_H
19
20#include <input/gp_event.h>
21#include <utils/gp_bbox.h>
22#include <utils/gp_json.h>
23#include <widgets/gp_widget.h>
25
45
46struct json_object;
47
48typedef struct gp_offset {
49 gp_coord x;
50 gp_coord y;
51} gp_offset;
52
62 GP_WIDGET_REDRAW_CHILDREN = 0x02,
83};
84
104 void (*free)(gp_widget *self);
105
116 int (*event)(gp_widget *self, const gp_widget_render_ctx *ctx, gp_event *ev);
117
125 void (*render)(gp_widget *self, const gp_offset *offset,
126 const gp_widget_render_ctx *ctx, int flags);
127
137 int (*focus)(gp_widget *self, int focus_dir);
138
139 /*
140 * Moves focus to widget on x, y coordinates.
141 */
142 int (*focus_xy)(gp_widget *self, const gp_widget_render_ctx *ctx,
143 unsigned int x, unsigned int y);
144
153 int (*focus_child)(gp_widget *self, gp_widget *child);
154
165 unsigned int (*min_w)(gp_widget *self, const gp_widget_render_ctx *ctx);
176 unsigned int (*min_h)(gp_widget *self, const gp_widget_render_ctx *ctx);
177
187 void (*distribute_w)(gp_widget *self,
188 const gp_widget_render_ctx *ctx,
189 int new_wh);
190
191 void (*distribute_h)(gp_widget *self,
192 const gp_widget_render_ctx *ctx,
193 int new_wh);
194
201 void (*for_each_child)(gp_widget *self, void (*func)(gp_widget *child));
202
212 gp_widget *(*from_json)(gp_json_reader *json, gp_json_val *val, gp_widget_json_ctx *ctx);
213
217 const char *id;
218};
219
220const struct gp_widget_ops *gp_widget_ops(gp_widget *self);
221
222const struct gp_widget_ops *gp_widget_ops_by_id(const char *id);
223
224const char *gp_widget_type_id(gp_widget *self);
225
226const char *gp_widget_type_name(enum gp_widget_type type);
227
228unsigned int gp_widget_min_w(gp_widget *self, const gp_widget_render_ctx *ctx);
229
230unsigned int gp_widget_min_h(gp_widget *self, const gp_widget_render_ctx *ctx);
231
232unsigned int gp_widget_align(gp_widget *self);
233
234int gp_widget_input_event(gp_widget *self, const gp_widget_render_ctx *ctx, gp_event *ev);
235
236void gp_widget_ops_render(gp_widget *self, const gp_offset *offset,
237 const gp_widget_render_ctx *ctx, int flags);
238
249
250/*
251 * Send event to a widget with a cursor offset.
252 *
253 * Same as gp_widget_ops_event() but with offset to the absolute coordinates is
254 * applied so that the event is relative to the cursor coordinates.
255 *
256 * @self A widget to send the event to.
257 * @ctx A render context.
258 * @ev An input event.
259 * @off_x X cursor offset.
260 * @off_y Y cursor offset.
261 *
262 * @return Zero if event wasn't handled, non-zero otherwise.
263 */
264int gp_widget_ops_event_offset(gp_widget *self, const gp_widget_render_ctx *ctx,
265 gp_event *ev, gp_size off_x, gp_size off_y);
266
275int gp_widget_ops_render_focus(gp_widget *self, int focus_dir);
276
288 unsigned int x, unsigned int y);
289
300
301void gp_widget_ops_distribute_w(gp_widget *self, const gp_widget_render_ctx *ctx, unsigned int w, int new_wh);
302void gp_widget_ops_distribute_h(gp_widget *self, const gp_widget_render_ctx *ctx, unsigned int h, int new_wh);
303
315void gp_widget_ops_for_each_child(gp_widget *self, void (*func)(gp_widget *child));
316
324static inline void gp_widget_ops_blit(const gp_widget_render_ctx *ctx,
325 gp_coord x, gp_coord y,
326 gp_size w, gp_size h)
327{
328 if (!ctx->flip)
329 return;
330
331 if (gp_bbox_empty(*ctx->flip))
332 *ctx->flip = gp_bbox_pack(x, y, w, h);
333 else
334 *ctx->flip = gp_bbox_merge(*ctx->flip, gp_bbox_pack(x, y, w, h));
335}
336
345static inline int gp_widget_should_redraw(gp_widget *self, int flags)
346{
347 return self->redraw || (flags & GP_WIDGET_REDRAW);
348}
349
350static inline int gp_widget_is_disabled(gp_widget *self, int flags)
351{
352 return self->disabled || (flags & GP_WIDGET_DISABLED);
353}
354
368 unsigned int w, unsigned int h, int new_wh);
369
378
388
398
406void gp_widget_render(gp_widget *self, const gp_widget_render_ctx *ctx, int new_wh);
407
408#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:74
static int gp_bbox_empty(gp_bbox box)
Returns true if bounding box is empty.
Definition gp_bbox.h:39
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:54
An (input) event layer.
Includes all JSON headers.
A widget implementation base.
gp_widget_type
Widget types.
Definition gp_widget.h:197
void gp_widget_render(gp_widget *self, const gp_widget_render_ctx *ctx, int new_wh)
Resizes and redraws changed widgets.
void gp_widget_redraw(gp_widget *self)
Requests widget repaint.
gp_widget_focus_flag
Flags to move focus around.
@ GP_FOCUS_OUT
Defocus any focused widget in the layout.
@ GP_FOCUS_LEFT
Moves focus left.
@ GP_FOCUS_DOWN
Moves focus down.
@ GP_FOCUS_IN
Focus first widget in the layout.
@ GP_FOCUS_PREV
Focus previous widget.
@ GP_FOCUS_NEXT
Focus next widget.
@ GP_FOCUS_RIGHT
Moves focus right.
@ GP_FOCUS_UP
Moves focus up.
void gp_widget_redraw_children(gp_widget *self)
Redraw all child widgets.
void gp_widget_ops_for_each_child(gp_widget *self, void(*func)(gp_widget *child))
Calls a callback on each child widget.
int gp_widget_ops_render_focus(gp_widget *self, int focus_dir)
Moves focus, if possible, in the direction requested by the focus_dir.
static void gp_widget_ops_blit(const gp_widget_render_ctx *ctx, gp_coord x, gp_coord y, gp_size w, gp_size h)
Marks an area to be blit on the screen from a buffer.
void gp_widget_resize(gp_widget *self)
Requests widget resize.
gp_widget_render_flags
Widget rendering flags.
@ GP_WIDGET_REDRAW
Widget needs to be repainted.
@ GP_WIDGET_COLOR_SCHEME
A color scheme has changed.
@ GP_WIDGET_DISABLED
Widget is disabled.
@ GP_WIDGET_RESIZE
Layout needs to be resized.
void gp_widget_calc_size(gp_widget *layout, const gp_widget_render_ctx *ctx, unsigned int w, unsigned int h, int new_wh)
Calculates layout size recursively.
static int gp_widget_should_redraw(gp_widget *self, int flags)
Returns true if widget should be repainted.
int gp_widget_ops_event(gp_widget *self, const gp_widget_render_ctx *ctx, gp_event *ev)
Send an event to a widget.
int gp_widget_ops_render_focus_xy(gp_widget *self, const gp_widget_render_ctx *ctx, unsigned int x, unsigned int y)
Tries to focus a widget on a given coordinates in a layout.
int gp_widget_ops_focus_widget(gp_widget *self)
Moves focus to a particular widget.
Widget rendering.
An input event.
Definition gp_event.h:153
A JSON parser internal state.
A parsed JSON key value pair.
A context to propagate values top down and bottom up.
Callbacks that implements a widget.
void(* distribute_w)(gp_widget *self, const gp_widget_render_ctx *ctx, int new_wh)
Recursively distributes widgets in a widget container.
void(* render)(gp_widget *self, const gp_offset *offset, const gp_widget_render_ctx *ctx, int flags)
Renders (changes in) widget layout.
int(* event)(gp_widget *self, const gp_widget_render_ctx *ctx, gp_event *ev)
Widget input event handler.
unsigned int(* min_w)(gp_widget *self, const gp_widget_render_ctx *ctx)
Calculates a minimal widget width.
void(* for_each_child)(gp_widget *self, void(*func)(gp_widget *child))
A callback to iterate over all widget children.
const char * id
A widget id, e.g. "checkbox".
void(* free)(gp_widget *self)
Frees any additional memory a widget has allocated.
int(* focus_child)(gp_widget *self, gp_widget *child)
Sets/moves focus to a child specific child widget.
unsigned int(* min_h)(gp_widget *self, const gp_widget_render_ctx *ctx)
Calculates a minimal widget height.
int(* focus)(gp_widget *self, int focus_dir)
Moves focus.
Global widget (rendering) context.
A widget base.
Definition gp_widget.h:28
unsigned int disabled
Internal disabled flag.
Definition gp_widget.h:142
unsigned int redraw
Internal widget repaint flag.
Definition gp_widget.h:106