comparison libmlk-ui/ui/theme.c @ 243:71b3b7036de7

misc: lot of cleanups, - prefix libraries with libmlk, - move assets from source directories closes #2520, - prefix header guards closes #2519
author David Demelier <markand@malikania.fr>
date Sat, 28 Nov 2020 22:37:30 +0100
parents libui/ui/theme.c@4ad7420ab678
children bfde372bf152
comparison
equal deleted inserted replaced
242:4c24604efcab 243:71b3b7036de7
1 /*
2 * theme.c -- abstract theming
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <stddef.h>
21 #include <string.h>
22
23 #include <core/font.h>
24 #include <core/maths.h>
25 #include <core/painter.h>
26 #include <core/panic.h>
27 #include <core/texture.h>
28 #include <core/util.h>
29
30 #include <assets/fonts/opensans-light.h>
31 #include <assets/fonts/opensans-regular.h>
32
33 #include "align.h"
34 #include "button.h"
35 #include "checkbox.h"
36 #include "frame.h"
37 #include "label.h"
38 #include "theme.h"
39
40 #define THEME(t) (t ? t : &default_theme)
41
42 /* Default font catalog. */
43 #define FONT(bin, size, index) \
44 { bin, sizeof (bin), size, &default_theme.fonts[index], {0} }
45
46 /* Default theme. */
47 static struct theme default_theme = {
48 .colors = {
49 [THEME_COLOR_DEBUG] = 0xff0000ff,
50 [THEME_COLOR_NORMAL] = 0xffffffff,
51 [THEME_COLOR_SELECTED] = 0x006554ff,
52 [THEME_COLOR_SHADOW] = 0x000000ff
53 },
54 .padding = 10,
55 .draw_frame = frame_draw_default,
56 .draw_label = label_draw_default,
57 .draw_button = button_draw_default,
58 .draw_checkbox = checkbox_draw_default
59 };
60
61 static struct font_catalog {
62 const unsigned char *data;
63 const size_t datasz;
64 unsigned int size;
65 struct font **dest;
66 struct font font;
67 } default_fonts[] = {
68 FONT(fonts_opensans_light, 12, THEME_FONT_DEBUG),
69 FONT(fonts_opensans_regular, 14, THEME_FONT_INTERFACE)
70 };
71
72 bool
73 theme_init(void)
74 {
75 /* Open all fonts. */
76 for (size_t i = 0; i < NELEM(default_fonts); ++i) {
77 struct font_catalog *fc = &default_fonts[i];
78
79 if (!font_openmem(&fc->font, fc->data, fc->datasz, fc->size))
80 goto failed;
81
82 /* Reference this font into the catalog. */
83 *default_fonts[i].dest = &default_fonts[i].font;
84 }
85
86 return true;
87
88 failed:
89 theme_finish();
90
91 return false;
92 }
93
94 struct theme *
95 theme_default(void)
96 {
97 return &default_theme;
98 }
99
100 unsigned int
101 theme_padding(const struct theme *t)
102 {
103 return THEME(t)->padding;
104 }
105
106 void
107 theme_shallow(struct theme *dst, const struct theme *src)
108 {
109 assert(dst);
110
111 memcpy(dst, src ? src : &default_theme, sizeof (*src));
112 }
113
114 void
115 theme_draw_frame(const struct theme *t, const struct frame *frame)
116 {
117 assert(frame);
118
119 THEME(t)->draw_frame(THEME(t), frame);
120 }
121
122 void
123 theme_draw_label(const struct theme *t, const struct label *label)
124 {
125 assert(label);
126
127 THEME(t)->draw_label(THEME(t), label);
128 }
129
130 void
131 theme_draw_button(const struct theme *t, const struct button *button)
132 {
133 assert(button);
134
135 THEME(t)->draw_button(THEME(t), button);
136 }
137
138 void
139 theme_draw_checkbox(const struct theme *t, const struct checkbox *cb)
140 {
141 assert(cb);
142
143 THEME(t)->draw_checkbox(THEME(t), cb);
144 }
145
146 void
147 theme_finish(void)
148 {
149 for (size_t i = 0; i < NELEM(default_fonts); ++i) {
150 font_finish(&default_fonts[i].font);
151 *default_fonts[i].dest = NULL;
152 }
153 }