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