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