GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_widget_size_units.h
1//SPDX-License-Identifier: LGPL-2.0-or-later
2
3/*
4
5 Copyright (c) 2014-2021 Cyril Hrubis <metan@ucw.cz>
6
7 */
8
9#ifndef GP_WIDGET_SIZE_UNITS_H
10#define GP_WIDGET_SIZE_UNITS_H
11
12#include <widgets/gp_widget_types.h>
13#include <widgets/gp_widget_render.h>
14
15/*
16 * There are two fundamental constants that are used for widget size
17 * computation. These are padding and text ascent. For example button widget
18 * minimal height consists of ascent and two paddings one on the top and one
19 * for the bottom.
20 *
21 * This structure describes a size as a sum of three different values these are
22 * pixels, padding and ascent. The later two are relative to a render context
23 * that defines the text ascent and padding.
24 */
25struct gp_widget_size {
26 uint16_t px;
27 uint8_t pad;
28 uint8_t asc;
29};
30
39static inline unsigned int gp_widget_size_units_get(gp_widget_size *size,
40 const gp_widget_render_ctx *ctx)
41{
42 return size->px + ctx->padd * size->pad +
43 gp_text_ascent(ctx->font) * size->asc;
44}
45
54int gp_widget_size_units_parse(const char *size, gp_widget_size *ret);
55
56/*
57 * Could be used to construct function parameters on the fly such as:
58 *
59 * w = gp_pixmap_create(GP_WIDGET_SIZE(100, 0, 0),
60 * GP_WIDGET_SIZE(100, 0, 0),
61 * callback, NULL);
62 */
63#define GP_WIDGET_SIZE(px_val, pad_val, asc_val) \
64 ((gp_widget_size){.px = px_val, .pad = pad_val, .asc = asc_val})
65
66/*
67 * Special value used in widget constructors. When size is set to this value
68 * the min size is set to a default for a given widget type.
69 */
70#define GP_WIDGET_SIZE_DEFAULT ((gp_widget_size){0xffff, 0xff, 0xff})
71
72#define GP_WIDGET_SIZE_EQ(a, b) (((a).px == (b).px) && \
73 ((a).pad == (b).pad) && \
74 ((a).asc == (b).asc))
75
76#endif /* GP_WIDGET_SIZE_UNITS_H */