Mercurial > molko
comparison src/core/theme.c @ 115:3bd0d3a39e30
core: implement checkbox, closes #2486
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 12 Jul 2020 09:44:27 +0200 |
parents | 5e38f88cb9ab |
children | 0a6683615c73 |
comparison
equal
deleted
inserted
replaced
114:bf7500aea454 | 115:3bd0d3a39e30 |
---|---|
18 | 18 |
19 #include <assert.h> | 19 #include <assert.h> |
20 #include <stddef.h> | 20 #include <stddef.h> |
21 | 21 |
22 #include "button.h" | 22 #include "button.h" |
23 #include "checkbox.h" | |
23 #include "font.h" | 24 #include "font.h" |
24 #include "frame.h" | 25 #include "frame.h" |
25 #include "label.h" | 26 #include "label.h" |
26 #include "maths.h" | 27 #include "maths.h" |
27 #include "painter.h" | 28 #include "painter.h" |
32 | 33 |
33 #include "assets/fonts/ComicNeue-Regular.h" | 34 #include "assets/fonts/ComicNeue-Regular.h" |
34 | 35 |
35 #define THEME(t) (t ? t : &default_theme) | 36 #define THEME(t) (t ? t : &default_theme) |
36 | 37 |
38 #define CHECKBOX_W 16 | |
39 #define CHECKBOX_H 16 | |
40 #define CHECKBOX_RAD 6 | |
41 | |
37 static struct font default_font; | 42 static struct font default_font; |
38 | 43 |
39 static void | 44 static void |
40 box(int x, int y, unsigned int w, unsigned int h) | 45 box(int x, int y, unsigned int w, unsigned int h) |
41 { | 46 { |
45 painter_draw_line(x, y, x + w, y); | 50 painter_draw_line(x, y, x + w, y); |
46 painter_draw_line(x, y + h, x + w, y + h); | 51 painter_draw_line(x, y + h, x + w, y + h); |
47 painter_draw_line(x, y, x, y + h); | 52 painter_draw_line(x, y, x, y + h); |
48 painter_draw_line(x + w, y, x + w, y + h); | 53 painter_draw_line(x + w, y, x + w, y + h); |
49 } | 54 } |
55 | |
50 | 56 |
51 static void | 57 static void |
52 draw_frame(struct theme *t, const struct frame *frame) | 58 draw_frame(struct theme *t, const struct frame *frame) |
53 { | 59 { |
54 if (frame->style == FRAME_STYLE_BOX) | 60 if (frame->style == FRAME_STYLE_BOX) |
118 label_draw(&label); | 124 label_draw(&label); |
119 | 125 |
120 box(button->x, button->y, button->w, button->h); | 126 box(button->x, button->y, button->w, button->h); |
121 } | 127 } |
122 | 128 |
129 static void | |
130 draw_checkbox(struct theme *t, const struct checkbox *cb) | |
131 { | |
132 box(cb->x, cb->y, CHECKBOX_W, CHECKBOX_H); | |
133 | |
134 if (cb->checked) | |
135 painter_draw_rectangle(cb->x + 5, cb->y + 5, CHECKBOX_W - 9, CHECKBOX_H - 9); | |
136 | |
137 if (cb->label) { | |
138 const unsigned int w = cb->w - (t->padding * 2) - CHECKBOX_W; | |
139 const int x = cb->x + (t->padding * 2) + CHECKBOX_W; | |
140 | |
141 struct label label = { | |
142 .text = cb->label, | |
143 .flags = LABEL_NO_HCENTER, | |
144 .x = x, | |
145 .y = cb->y, | |
146 .w = w, | |
147 .h = cb->h | |
148 }; | |
149 | |
150 draw_label(t, &label); | |
151 } | |
152 } | |
153 | |
123 /* Default theme. */ | 154 /* Default theme. */ |
124 static struct theme default_theme = { | 155 static struct theme default_theme = { |
125 .colors = { | 156 .colors = { |
126 [THEME_COLOR_NORMAL] = 0xffffffff, | 157 [THEME_COLOR_NORMAL] = 0xffffffff, |
127 [THEME_COLOR_SELECTED] = 0x006554ff, | 158 [THEME_COLOR_SELECTED] = 0x006554ff, |
128 [THEME_COLOR_SHADOW] = 0x000000ff | 159 [THEME_COLOR_SHADOW] = 0x000000ff |
129 }, | 160 }, |
161 .padding = 10, | |
130 .draw_frame = draw_frame, | 162 .draw_frame = draw_frame, |
131 .draw_label = draw_label, | 163 .draw_label = draw_label, |
132 .draw_button = draw_button | 164 .draw_button = draw_button, |
165 .draw_checkbox = draw_checkbox | |
133 }; | 166 }; |
134 | 167 |
135 /* Default font catalog. */ | 168 /* Default font catalog. */ |
136 #define FONT(bin, size, index) \ | 169 #define FONT(bin, size, index) \ |
137 { bin, sizeof (bin), size, &default_theme.fonts[index] } | 170 { bin, sizeof (bin), size, &default_theme.fonts[index] } |
172 theme_default(void) | 205 theme_default(void) |
173 { | 206 { |
174 return &default_theme; | 207 return &default_theme; |
175 } | 208 } |
176 | 209 |
210 unsigned int | |
211 theme_padding(const struct theme *t) | |
212 { | |
213 return THEME(t)->padding; | |
214 } | |
215 | |
177 void | 216 void |
178 theme_draw_frame(struct theme *t, const struct frame *frame) | 217 theme_draw_frame(struct theme *t, const struct frame *frame) |
179 { | 218 { |
180 assert(frame); | 219 assert(frame); |
181 | 220 |
194 theme_draw_button(struct theme *t, const struct button *button) | 233 theme_draw_button(struct theme *t, const struct button *button) |
195 { | 234 { |
196 assert(button); | 235 assert(button); |
197 | 236 |
198 THEME(t)->draw_button(THEME(t), button); | 237 THEME(t)->draw_button(THEME(t), button); |
238 } | |
239 | |
240 void | |
241 theme_draw_checkbox(struct theme *t, const struct checkbox *cb) | |
242 { | |
243 assert(cb); | |
244 | |
245 THEME(t)->draw_checkbox(THEME(t), cb); | |
199 } | 246 } |
200 | 247 |
201 void | 248 void |
202 theme_finish(void) | 249 theme_finish(void) |
203 { | 250 { |