[47728678] | 1 | /*
|
---|
[7020d1f] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[47728678] | 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
|
---|
[9c7dc8e] | 52 | * @param textmode @c true if running in text mode
|
---|
[ba09d06] | 53 | * @param rresource Place to store pointer to new UI resource
|
---|
[47728678] | 54 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 55 | */
|
---|
[9c7dc8e] | 56 | errno_t ui_resource_create(gfx_context_t *gc, bool textmode,
|
---|
| 57 | ui_resource_t **rresource)
|
---|
[47728678] | 58 | {
|
---|
| 59 | ui_resource_t *resource;
|
---|
| 60 | gfx_typeface_t *tface = NULL;
|
---|
| 61 | gfx_font_t *font = NULL;
|
---|
| 62 | gfx_font_info_t *finfo;
|
---|
[de9992c] | 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;
|
---|
[1769693] | 68 | gfx_color_t *wnd_face_color = NULL;
|
---|
| 69 | gfx_color_t *wnd_text_color = NULL;
|
---|
| 70 | gfx_color_t *wnd_frame_hi_color = NULL;
|
---|
| 71 | gfx_color_t *wnd_frame_sh_color = NULL;
|
---|
| 72 | gfx_color_t *wnd_highlight_color = NULL;
|
---|
| 73 | gfx_color_t *wnd_shadow_color = NULL;
|
---|
| 74 | gfx_color_t *tbar_act_bg_color = NULL;
|
---|
| 75 | gfx_color_t *tbar_inact_bg_color = NULL;
|
---|
| 76 | gfx_color_t *tbar_act_text_color = NULL;
|
---|
| 77 | gfx_color_t *tbar_inact_text_color = NULL;
|
---|
[03145ee] | 78 | gfx_color_t *entry_fg_color = NULL;
|
---|
| 79 | gfx_color_t *entry_bg_color = NULL;
|
---|
[d70dc1c4] | 80 | gfx_color_t *entry_act_bg_color = NULL;
|
---|
[47728678] | 81 | errno_t rc;
|
---|
| 82 |
|
---|
| 83 | resource = calloc(1, sizeof(ui_resource_t));
|
---|
| 84 | if (resource == NULL)
|
---|
| 85 | return ENOMEM;
|
---|
| 86 |
|
---|
[9c7dc8e] | 87 | if (textmode) {
|
---|
| 88 | /* Create dummy font for text mode */
|
---|
| 89 | rc = gfx_typeface_create(gc, &tface);
|
---|
| 90 | if (rc != EOK)
|
---|
| 91 | goto error;
|
---|
| 92 |
|
---|
| 93 | rc = gfx_font_create_textmode(tface, &font);
|
---|
| 94 | if (rc != EOK)
|
---|
| 95 | goto error;
|
---|
| 96 | } else {
|
---|
| 97 | rc = gfx_typeface_open(gc, ui_typeface_path, &tface);
|
---|
| 98 | if (rc != EOK)
|
---|
| 99 | goto error;
|
---|
| 100 |
|
---|
| 101 | finfo = gfx_typeface_first_font(tface);
|
---|
| 102 | if (finfo == NULL) {
|
---|
| 103 | rc = EIO;
|
---|
| 104 | goto error;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | rc = gfx_font_open(finfo, &font);
|
---|
| 108 | if (rc != EOK)
|
---|
| 109 | goto error;
|
---|
[47728678] | 110 | }
|
---|
| 111 |
|
---|
[de9992c] | 112 | rc = gfx_color_new_rgb_i16(0, 0, 0, &btn_frame_color);
|
---|
| 113 | if (rc != EOK)
|
---|
| 114 | goto error;
|
---|
| 115 |
|
---|
| 116 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &btn_face_color);
|
---|
| 117 | if (rc != EOK)
|
---|
| 118 | goto error;
|
---|
| 119 |
|
---|
| 120 | rc = gfx_color_new_rgb_i16(0, 0, 0, &btn_text_color);
|
---|
| 121 | if (rc != EOK)
|
---|
| 122 | goto error;
|
---|
| 123 |
|
---|
| 124 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff,
|
---|
| 125 | &btn_highlight_color);
|
---|
| 126 | if (rc != EOK)
|
---|
| 127 | goto error;
|
---|
| 128 |
|
---|
| 129 | rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &btn_shadow_color);
|
---|
| 130 | if (rc != EOK)
|
---|
| 131 | goto error;
|
---|
| 132 |
|
---|
[1769693] | 133 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &wnd_face_color);
|
---|
| 134 | if (rc != EOK)
|
---|
| 135 | goto error;
|
---|
| 136 |
|
---|
| 137 | rc = gfx_color_new_rgb_i16(0, 0, 0, &wnd_text_color);
|
---|
| 138 | if (rc != EOK)
|
---|
| 139 | goto error;
|
---|
| 140 |
|
---|
| 141 | rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &wnd_frame_hi_color);
|
---|
| 142 | if (rc != EOK)
|
---|
| 143 | goto error;
|
---|
| 144 |
|
---|
[7020d1f] | 145 | rc = gfx_color_new_rgb_i16(0x4444, 0x4444, 0x4444, &wnd_frame_sh_color);
|
---|
[1769693] | 146 | if (rc != EOK)
|
---|
| 147 | goto error;
|
---|
| 148 |
|
---|
| 149 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff,
|
---|
| 150 | &wnd_highlight_color);
|
---|
| 151 | if (rc != EOK)
|
---|
| 152 | goto error;
|
---|
| 153 |
|
---|
| 154 | rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &wnd_shadow_color);
|
---|
| 155 | if (rc != EOK)
|
---|
| 156 | goto error;
|
---|
| 157 |
|
---|
| 158 | rc = gfx_color_new_rgb_i16(0x5858, 0x6a6a, 0xc4c4, &tbar_act_bg_color);
|
---|
| 159 | if (rc != EOK)
|
---|
| 160 | goto error;
|
---|
| 161 |
|
---|
| 162 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff,
|
---|
| 163 | &tbar_act_text_color);
|
---|
| 164 | if (rc != EOK)
|
---|
| 165 | goto error;
|
---|
| 166 |
|
---|
| 167 | rc = gfx_color_new_rgb_i16(0xdddd, 0xdddd, 0xdddd,
|
---|
| 168 | &tbar_inact_bg_color);
|
---|
| 169 | if (rc != EOK)
|
---|
| 170 | goto error;
|
---|
| 171 |
|
---|
| 172 | rc = gfx_color_new_rgb_i16(0x5858, 0x5858, 0x5858,
|
---|
| 173 | &tbar_inact_text_color);
|
---|
| 174 | if (rc != EOK)
|
---|
| 175 | goto error;
|
---|
| 176 |
|
---|
[03145ee] | 177 | rc = gfx_color_new_rgb_i16(0, 0, 0, &entry_fg_color);
|
---|
| 178 | if (rc != EOK)
|
---|
| 179 | goto error;
|
---|
| 180 |
|
---|
| 181 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &entry_bg_color);
|
---|
| 182 | if (rc != EOK)
|
---|
| 183 | goto error;
|
---|
| 184 |
|
---|
[d70dc1c4] | 185 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &entry_act_bg_color);
|
---|
| 186 | if (rc != EOK)
|
---|
| 187 | goto error;
|
---|
| 188 |
|
---|
[47728678] | 189 | resource->gc = gc;
|
---|
| 190 | resource->tface = tface;
|
---|
| 191 | resource->font = font;
|
---|
[9c7dc8e] | 192 | resource->textmode = textmode;
|
---|
[1769693] | 193 |
|
---|
[de9992c] | 194 | resource->btn_frame_color = btn_frame_color;
|
---|
| 195 | resource->btn_face_color = btn_face_color;
|
---|
| 196 | resource->btn_text_color = btn_text_color;
|
---|
| 197 | resource->btn_highlight_color = btn_highlight_color;
|
---|
| 198 | resource->btn_shadow_color = btn_shadow_color;
|
---|
[1769693] | 199 |
|
---|
| 200 | resource->wnd_face_color = wnd_face_color;
|
---|
| 201 | resource->wnd_text_color = wnd_text_color;
|
---|
| 202 | resource->wnd_frame_hi_color = wnd_frame_hi_color;
|
---|
| 203 | resource->wnd_frame_sh_color = wnd_frame_sh_color;
|
---|
| 204 | resource->wnd_highlight_color = wnd_highlight_color;
|
---|
| 205 | resource->wnd_shadow_color = wnd_shadow_color;
|
---|
| 206 |
|
---|
| 207 | resource->tbar_act_bg_color = tbar_act_bg_color;
|
---|
| 208 | resource->tbar_act_text_color = tbar_act_text_color;
|
---|
| 209 | resource->tbar_inact_bg_color = tbar_inact_bg_color;
|
---|
| 210 | resource->tbar_inact_text_color = tbar_inact_text_color;
|
---|
| 211 |
|
---|
[03145ee] | 212 | resource->entry_fg_color = entry_fg_color;
|
---|
| 213 | resource->entry_bg_color = entry_bg_color;
|
---|
[d70dc1c4] | 214 | resource->entry_act_bg_color = entry_act_bg_color;
|
---|
[03145ee] | 215 |
|
---|
[47728678] | 216 | *rresource = resource;
|
---|
| 217 | return EOK;
|
---|
| 218 | error:
|
---|
[de9992c] | 219 | if (btn_frame_color != NULL)
|
---|
| 220 | gfx_color_delete(btn_frame_color);
|
---|
| 221 | if (btn_face_color != NULL)
|
---|
| 222 | gfx_color_delete(btn_face_color);
|
---|
| 223 | if (btn_text_color != NULL)
|
---|
| 224 | gfx_color_delete(btn_text_color);
|
---|
| 225 | if (btn_highlight_color != NULL)
|
---|
| 226 | gfx_color_delete(btn_highlight_color);
|
---|
| 227 | if (btn_shadow_color != NULL)
|
---|
| 228 | gfx_color_delete(btn_shadow_color);
|
---|
[1769693] | 229 |
|
---|
| 230 | if (wnd_face_color != NULL)
|
---|
| 231 | gfx_color_delete(wnd_face_color);
|
---|
| 232 | if (wnd_text_color != NULL)
|
---|
| 233 | gfx_color_delete(wnd_text_color);
|
---|
| 234 | if (wnd_frame_hi_color != NULL)
|
---|
| 235 | gfx_color_delete(wnd_frame_hi_color);
|
---|
| 236 | if (wnd_frame_sh_color != NULL)
|
---|
| 237 | gfx_color_delete(wnd_frame_sh_color);
|
---|
| 238 | if (wnd_highlight_color != NULL)
|
---|
| 239 | gfx_color_delete(wnd_highlight_color);
|
---|
| 240 | if (wnd_shadow_color != NULL)
|
---|
| 241 | gfx_color_delete(wnd_shadow_color);
|
---|
| 242 |
|
---|
| 243 | if (tbar_act_bg_color != NULL)
|
---|
| 244 | gfx_color_delete(tbar_act_bg_color);
|
---|
| 245 | if (tbar_act_text_color != NULL)
|
---|
| 246 | gfx_color_delete(tbar_act_text_color);
|
---|
| 247 | if (tbar_inact_bg_color != NULL)
|
---|
| 248 | gfx_color_delete(tbar_inact_bg_color);
|
---|
| 249 | if (tbar_inact_text_color != NULL)
|
---|
| 250 | gfx_color_delete(tbar_inact_text_color);
|
---|
| 251 |
|
---|
[03145ee] | 252 | if (entry_fg_color != NULL)
|
---|
| 253 | gfx_color_delete(entry_fg_color);
|
---|
| 254 | if (entry_bg_color != NULL)
|
---|
| 255 | gfx_color_delete(entry_bg_color);
|
---|
[d70dc1c4] | 256 | if (entry_act_bg_color != NULL)
|
---|
| 257 | gfx_color_delete(entry_act_bg_color);
|
---|
[03145ee] | 258 |
|
---|
[47728678] | 259 | if (tface != NULL)
|
---|
| 260 | gfx_typeface_destroy(tface);
|
---|
| 261 | free(resource);
|
---|
| 262 | return rc;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /** Destroy UI resource.
|
---|
| 266 | *
|
---|
[4ed00d3] | 267 | * @param resource UI resource or @c NULL
|
---|
[47728678] | 268 | */
|
---|
| 269 | void ui_resource_destroy(ui_resource_t *resource)
|
---|
| 270 | {
|
---|
| 271 | if (resource == NULL)
|
---|
| 272 | return;
|
---|
| 273 |
|
---|
[de9992c] | 274 | gfx_color_delete(resource->btn_frame_color);
|
---|
| 275 | gfx_color_delete(resource->btn_face_color);
|
---|
| 276 | gfx_color_delete(resource->btn_text_color);
|
---|
| 277 | gfx_color_delete(resource->btn_highlight_color);
|
---|
| 278 | gfx_color_delete(resource->btn_shadow_color);
|
---|
| 279 |
|
---|
[1769693] | 280 | gfx_color_delete(resource->wnd_face_color);
|
---|
| 281 | gfx_color_delete(resource->wnd_text_color);
|
---|
| 282 | gfx_color_delete(resource->wnd_frame_hi_color);
|
---|
| 283 | gfx_color_delete(resource->wnd_frame_sh_color);
|
---|
| 284 | gfx_color_delete(resource->wnd_highlight_color);
|
---|
| 285 | gfx_color_delete(resource->wnd_shadow_color);
|
---|
| 286 |
|
---|
| 287 | gfx_color_delete(resource->tbar_act_bg_color);
|
---|
| 288 | gfx_color_delete(resource->tbar_act_text_color);
|
---|
| 289 | gfx_color_delete(resource->tbar_inact_bg_color);
|
---|
| 290 | gfx_color_delete(resource->tbar_inact_text_color);
|
---|
| 291 |
|
---|
[03145ee] | 292 | gfx_color_delete(resource->entry_fg_color);
|
---|
| 293 | gfx_color_delete(resource->entry_bg_color);
|
---|
[d70dc1c4] | 294 | gfx_color_delete(resource->entry_act_bg_color);
|
---|
[03145ee] | 295 |
|
---|
[47728678] | 296 | gfx_font_close(resource->font);
|
---|
| 297 | gfx_typeface_destroy(resource->tface);
|
---|
| 298 | free(resource);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /** @}
|
---|
| 302 | */
|
---|