1 | /*
|
---|
2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libui
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file UI resources
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <gfx/color.h>
|
---|
38 | #include <gfx/context.h>
|
---|
39 | #include <gfx/font.h>
|
---|
40 | #include <gfx/render.h>
|
---|
41 | #include <gfx/typeface.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <str.h>
|
---|
44 | #include <ui/resource.h>
|
---|
45 | #include "../private/resource.h"
|
---|
46 |
|
---|
47 | static const char *ui_typeface_path = "/data/font/helena.tpf";
|
---|
48 |
|
---|
49 | /** Create new UI resource.
|
---|
50 | *
|
---|
51 | * @param gc Graphic context
|
---|
52 | * @param textmode @c true if running in text mode
|
---|
53 | * @param rresource Place to store pointer to new UI resource
|
---|
54 | * @return EOK on success, ENOMEM if out of memory
|
---|
55 | */
|
---|
56 | errno_t ui_resource_create(gfx_context_t *gc, bool textmode,
|
---|
57 | ui_resource_t **rresource)
|
---|
58 | {
|
---|
59 | ui_resource_t *resource;
|
---|
60 | gfx_typeface_t *tface = NULL;
|
---|
61 | gfx_font_t *font = NULL;
|
---|
62 | gfx_font_info_t *finfo;
|
---|
63 | gfx_color_t *btn_frame_color = NULL;
|
---|
64 | gfx_color_t *btn_face_color = NULL;
|
---|
65 | gfx_color_t *btn_text_color = NULL;
|
---|
66 | gfx_color_t *btn_highlight_color = NULL;
|
---|
67 | gfx_color_t *btn_shadow_color = NULL;
|
---|
68 | gfx_color_t *wnd_face_color = NULL;
|
---|
69 | gfx_color_t *wnd_text_color = NULL;
|
---|
70 | gfx_color_t *wnd_sel_text_color = NULL;
|
---|
71 | gfx_color_t *wnd_sel_text_bg_color = NULL;
|
---|
72 | gfx_color_t *wnd_frame_hi_color = NULL;
|
---|
73 | gfx_color_t *wnd_frame_sh_color = NULL;
|
---|
74 | gfx_color_t *wnd_highlight_color = NULL;
|
---|
75 | gfx_color_t *wnd_shadow_color = NULL;
|
---|
76 | gfx_color_t *tbar_act_bg_color = NULL;
|
---|
77 | gfx_color_t *tbar_inact_bg_color = NULL;
|
---|
78 | gfx_color_t *tbar_act_text_color = NULL;
|
---|
79 | gfx_color_t *tbar_inact_text_color = NULL;
|
---|
80 | gfx_color_t *entry_fg_color = NULL;
|
---|
81 | gfx_color_t *entry_bg_color = NULL;
|
---|
82 | gfx_color_t *entry_act_bg_color = NULL;
|
---|
83 | errno_t rc;
|
---|
84 |
|
---|
85 | resource = calloc(1, sizeof(ui_resource_t));
|
---|
86 | if (resource == NULL)
|
---|
87 | return ENOMEM;
|
---|
88 |
|
---|
89 | if (textmode) {
|
---|
90 | /* Create dummy font for text mode */
|
---|
91 | rc = gfx_typeface_create(gc, &tface);
|
---|
92 | if (rc != EOK)
|
---|
93 | goto error;
|
---|
94 |
|
---|
95 | rc = gfx_font_create_textmode(tface, &font);
|
---|
96 | if (rc != EOK)
|
---|
97 | goto error;
|
---|
98 | } else {
|
---|
99 | rc = gfx_typeface_open(gc, ui_typeface_path, &tface);
|
---|
100 | if (rc != EOK)
|
---|
101 | goto error;
|
---|
102 |
|
---|
103 | finfo = gfx_typeface_first_font(tface);
|
---|
104 | if (finfo == NULL) {
|
---|
105 | rc = EIO;
|
---|
106 | goto error;
|
---|
107 | }
|
---|
108 |
|
---|
109 | rc = gfx_font_open(finfo, &font);
|
---|
110 | if (rc != EOK)
|
---|
111 | goto error;
|
---|
112 | }
|
---|
113 |
|
---|
114 | rc = gfx_color_new_rgb_i16(0, 0, 0, &btn_frame_color);
|
---|
115 | if (rc != EOK)
|
---|
116 | goto error;
|
---|
117 |
|
---|
118 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &btn_face_color);
|
---|
119 | if (rc != EOK)
|
---|
120 | goto error;
|
---|
121 |
|
---|
122 | rc = gfx_color_new_rgb_i16(0, 0, 0, &btn_text_color);
|
---|
123 | if (rc != EOK)
|
---|
124 | goto error;
|
---|
125 |
|
---|
126 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff,
|
---|
127 | &btn_highlight_color);
|
---|
128 | if (rc != EOK)
|
---|
129 | goto error;
|
---|
130 |
|
---|
131 | rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &btn_shadow_color);
|
---|
132 | if (rc != EOK)
|
---|
133 | goto error;
|
---|
134 |
|
---|
135 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &wnd_face_color);
|
---|
136 | if (rc != EOK)
|
---|
137 | goto error;
|
---|
138 |
|
---|
139 | rc = gfx_color_new_rgb_i16(0, 0, 0, &wnd_text_color);
|
---|
140 | if (rc != EOK)
|
---|
141 | goto error;
|
---|
142 |
|
---|
143 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &wnd_sel_text_color);
|
---|
144 | if (rc != EOK)
|
---|
145 | goto error;
|
---|
146 |
|
---|
147 | rc = gfx_color_new_rgb_i16(0x5858, 0x6a6a, 0xc4c4,
|
---|
148 | &wnd_sel_text_bg_color);
|
---|
149 | if (rc != EOK)
|
---|
150 | goto error;
|
---|
151 |
|
---|
152 | rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &wnd_frame_hi_color);
|
---|
153 | if (rc != EOK)
|
---|
154 | goto error;
|
---|
155 |
|
---|
156 | rc = gfx_color_new_rgb_i16(0x4444, 0x4444, 0x4444, &wnd_frame_sh_color);
|
---|
157 | if (rc != EOK)
|
---|
158 | goto error;
|
---|
159 |
|
---|
160 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff,
|
---|
161 | &wnd_highlight_color);
|
---|
162 | if (rc != EOK)
|
---|
163 | goto error;
|
---|
164 |
|
---|
165 | rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &wnd_shadow_color);
|
---|
166 | if (rc != EOK)
|
---|
167 | goto error;
|
---|
168 |
|
---|
169 | rc = gfx_color_new_rgb_i16(0x5858, 0x6a6a, 0xc4c4, &tbar_act_bg_color);
|
---|
170 | if (rc != EOK)
|
---|
171 | goto error;
|
---|
172 |
|
---|
173 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff,
|
---|
174 | &tbar_act_text_color);
|
---|
175 | if (rc != EOK)
|
---|
176 | goto error;
|
---|
177 |
|
---|
178 | rc = gfx_color_new_rgb_i16(0xdddd, 0xdddd, 0xdddd,
|
---|
179 | &tbar_inact_bg_color);
|
---|
180 | if (rc != EOK)
|
---|
181 | goto error;
|
---|
182 |
|
---|
183 | rc = gfx_color_new_rgb_i16(0x5858, 0x5858, 0x5858,
|
---|
184 | &tbar_inact_text_color);
|
---|
185 | if (rc != EOK)
|
---|
186 | goto error;
|
---|
187 |
|
---|
188 | rc = gfx_color_new_rgb_i16(0, 0, 0, &entry_fg_color);
|
---|
189 | if (rc != EOK)
|
---|
190 | goto error;
|
---|
191 |
|
---|
192 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &entry_bg_color);
|
---|
193 | if (rc != EOK)
|
---|
194 | goto error;
|
---|
195 |
|
---|
196 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &entry_act_bg_color);
|
---|
197 | if (rc != EOK)
|
---|
198 | goto error;
|
---|
199 |
|
---|
200 | resource->gc = gc;
|
---|
201 | resource->tface = tface;
|
---|
202 | resource->font = font;
|
---|
203 | resource->textmode = textmode;
|
---|
204 |
|
---|
205 | resource->btn_frame_color = btn_frame_color;
|
---|
206 | resource->btn_face_color = btn_face_color;
|
---|
207 | resource->btn_text_color = btn_text_color;
|
---|
208 | resource->btn_highlight_color = btn_highlight_color;
|
---|
209 | resource->btn_shadow_color = btn_shadow_color;
|
---|
210 |
|
---|
211 | resource->wnd_face_color = wnd_face_color;
|
---|
212 | resource->wnd_text_color = wnd_text_color;
|
---|
213 | resource->wnd_sel_text_color = wnd_sel_text_color;
|
---|
214 | resource->wnd_sel_text_bg_color = wnd_sel_text_bg_color;
|
---|
215 | resource->wnd_frame_hi_color = wnd_frame_hi_color;
|
---|
216 | resource->wnd_frame_sh_color = wnd_frame_sh_color;
|
---|
217 | resource->wnd_highlight_color = wnd_highlight_color;
|
---|
218 | resource->wnd_shadow_color = wnd_shadow_color;
|
---|
219 |
|
---|
220 | resource->tbar_act_bg_color = tbar_act_bg_color;
|
---|
221 | resource->tbar_act_text_color = tbar_act_text_color;
|
---|
222 | resource->tbar_inact_bg_color = tbar_inact_bg_color;
|
---|
223 | resource->tbar_inact_text_color = tbar_inact_text_color;
|
---|
224 |
|
---|
225 | resource->entry_fg_color = entry_fg_color;
|
---|
226 | resource->entry_bg_color = entry_bg_color;
|
---|
227 | resource->entry_act_bg_color = entry_act_bg_color;
|
---|
228 |
|
---|
229 | *rresource = resource;
|
---|
230 | return EOK;
|
---|
231 | error:
|
---|
232 | if (btn_frame_color != NULL)
|
---|
233 | gfx_color_delete(btn_frame_color);
|
---|
234 | if (btn_face_color != NULL)
|
---|
235 | gfx_color_delete(btn_face_color);
|
---|
236 | if (btn_text_color != NULL)
|
---|
237 | gfx_color_delete(btn_text_color);
|
---|
238 | if (btn_highlight_color != NULL)
|
---|
239 | gfx_color_delete(btn_highlight_color);
|
---|
240 | if (btn_shadow_color != NULL)
|
---|
241 | gfx_color_delete(btn_shadow_color);
|
---|
242 |
|
---|
243 | if (wnd_face_color != NULL)
|
---|
244 | gfx_color_delete(wnd_face_color);
|
---|
245 | if (wnd_text_color != NULL)
|
---|
246 | gfx_color_delete(wnd_text_color);
|
---|
247 | if (wnd_sel_text_color != NULL)
|
---|
248 | gfx_color_delete(wnd_sel_text_color);
|
---|
249 | if (wnd_sel_text_bg_color != NULL)
|
---|
250 | gfx_color_delete(wnd_sel_text_bg_color);
|
---|
251 | if (wnd_frame_hi_color != NULL)
|
---|
252 | gfx_color_delete(wnd_frame_hi_color);
|
---|
253 | if (wnd_frame_sh_color != NULL)
|
---|
254 | gfx_color_delete(wnd_frame_sh_color);
|
---|
255 | if (wnd_highlight_color != NULL)
|
---|
256 | gfx_color_delete(wnd_highlight_color);
|
---|
257 | if (wnd_shadow_color != NULL)
|
---|
258 | gfx_color_delete(wnd_shadow_color);
|
---|
259 |
|
---|
260 | if (tbar_act_bg_color != NULL)
|
---|
261 | gfx_color_delete(tbar_act_bg_color);
|
---|
262 | if (tbar_act_text_color != NULL)
|
---|
263 | gfx_color_delete(tbar_act_text_color);
|
---|
264 | if (tbar_inact_bg_color != NULL)
|
---|
265 | gfx_color_delete(tbar_inact_bg_color);
|
---|
266 | if (tbar_inact_text_color != NULL)
|
---|
267 | gfx_color_delete(tbar_inact_text_color);
|
---|
268 |
|
---|
269 | if (entry_fg_color != NULL)
|
---|
270 | gfx_color_delete(entry_fg_color);
|
---|
271 | if (entry_bg_color != NULL)
|
---|
272 | gfx_color_delete(entry_bg_color);
|
---|
273 | if (entry_act_bg_color != NULL)
|
---|
274 | gfx_color_delete(entry_act_bg_color);
|
---|
275 |
|
---|
276 | if (tface != NULL)
|
---|
277 | gfx_typeface_destroy(tface);
|
---|
278 | free(resource);
|
---|
279 | return rc;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /** Destroy UI resource.
|
---|
283 | *
|
---|
284 | * @param resource UI resource or @c NULL
|
---|
285 | */
|
---|
286 | void ui_resource_destroy(ui_resource_t *resource)
|
---|
287 | {
|
---|
288 | if (resource == NULL)
|
---|
289 | return;
|
---|
290 |
|
---|
291 | gfx_color_delete(resource->btn_frame_color);
|
---|
292 | gfx_color_delete(resource->btn_face_color);
|
---|
293 | gfx_color_delete(resource->btn_text_color);
|
---|
294 | gfx_color_delete(resource->btn_highlight_color);
|
---|
295 | gfx_color_delete(resource->btn_shadow_color);
|
---|
296 |
|
---|
297 | gfx_color_delete(resource->wnd_face_color);
|
---|
298 | gfx_color_delete(resource->wnd_text_color);
|
---|
299 | gfx_color_delete(resource->wnd_sel_text_color);
|
---|
300 | gfx_color_delete(resource->wnd_sel_text_bg_color);
|
---|
301 | gfx_color_delete(resource->wnd_frame_hi_color);
|
---|
302 | gfx_color_delete(resource->wnd_frame_sh_color);
|
---|
303 | gfx_color_delete(resource->wnd_highlight_color);
|
---|
304 | gfx_color_delete(resource->wnd_shadow_color);
|
---|
305 |
|
---|
306 | gfx_color_delete(resource->tbar_act_bg_color);
|
---|
307 | gfx_color_delete(resource->tbar_act_text_color);
|
---|
308 | gfx_color_delete(resource->tbar_inact_bg_color);
|
---|
309 | gfx_color_delete(resource->tbar_inact_text_color);
|
---|
310 |
|
---|
311 | gfx_color_delete(resource->entry_fg_color);
|
---|
312 | gfx_color_delete(resource->entry_bg_color);
|
---|
313 | gfx_color_delete(resource->entry_act_bg_color);
|
---|
314 |
|
---|
315 | gfx_font_close(resource->font);
|
---|
316 | gfx_typeface_destroy(resource->tface);
|
---|
317 | free(resource);
|
---|
318 | }
|
---|
319 |
|
---|
320 | /** Set UI resource expose callback.
|
---|
321 | *
|
---|
322 | * @param resource Resource
|
---|
323 | * @param cb Callback
|
---|
324 | * @param arg Callback argument
|
---|
325 | */
|
---|
326 | void ui_resource_set_expose_cb(ui_resource_t *resource,
|
---|
327 | ui_expose_cb_t cb, void *arg)
|
---|
328 | {
|
---|
329 | resource->expose_cb = cb;
|
---|
330 | resource->expose_arg = arg;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /** Force UI repaint after an area has been exposed.
|
---|
334 | *
|
---|
335 | * This is called when a popup disappears, which could have exposed some
|
---|
336 | * other UI elements. It causes complete repaint of the UI.
|
---|
337 | *
|
---|
338 | * NOTE Ideally we could specify the exposed rectangle and then limit
|
---|
339 | * the repaint to just that. That would, however, require means of
|
---|
340 | * actually clipping the repaint operation.
|
---|
341 | */
|
---|
342 | void ui_resource_expose(ui_resource_t *resource)
|
---|
343 | {
|
---|
344 | if (resource->expose_cb != NULL)
|
---|
345 | resource->expose_cb(resource->expose_arg);
|
---|
346 | }
|
---|
347 |
|
---|
348 | /** @}
|
---|
349 | */
|
---|