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