source: mainline/uspace/lib/ui/src/resource.c@ de9992c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since de9992c was de9992c, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Factor out bevel drawing, store button colors in ui_resource_t

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[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
47static const char *ui_typeface_path = "/data/font/helena.tpf";
48
49/** Create new UI resource.
50 *
51 * @param gc Graphic context
52 * @param rresource Place to store pointer to new push button
53 * @return EOK on success, ENOMEM if out of memory
54 */
55errno_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;
[47728678]66 errno_t rc;
67
68 resource = calloc(1, sizeof(ui_resource_t));
69 if (resource == NULL)
70 return ENOMEM;
71
72 rc = gfx_typeface_open(gc, ui_typeface_path, &tface);
73 if (rc != EOK)
74 goto error;
75
76 finfo = gfx_typeface_first_font(tface);
77 if (finfo == NULL) {
78 rc = EIO;
79 goto error;
80 }
81
82 rc = gfx_font_open(finfo, &font);
83 if (rc != EOK)
84 goto error;
85
[de9992c]86 rc = gfx_color_new_rgb_i16(0, 0, 0, &btn_frame_color);
87 if (rc != EOK)
88 goto error;
89
90 rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &btn_face_color);
91 if (rc != EOK)
92 goto error;
93
94 rc = gfx_color_new_rgb_i16(0, 0, 0, &btn_text_color);
95 if (rc != EOK)
96 goto error;
97
98 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff,
99 &btn_highlight_color);
100 if (rc != EOK)
101 goto error;
102
103 rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &btn_shadow_color);
104 if (rc != EOK)
105 goto error;
106
[47728678]107 resource->gc = gc;
108 resource->tface = tface;
109 resource->font = font;
[de9992c]110 resource->btn_frame_color = btn_frame_color;
111 resource->btn_face_color = btn_face_color;
112 resource->btn_text_color = btn_text_color;
113 resource->btn_highlight_color = btn_highlight_color;
114 resource->btn_shadow_color = btn_shadow_color;
[47728678]115 *rresource = resource;
116 return EOK;
117error:
[de9992c]118 if (btn_frame_color != NULL)
119 gfx_color_delete(btn_frame_color);
120 if (btn_face_color != NULL)
121 gfx_color_delete(btn_face_color);
122 if (btn_text_color != NULL)
123 gfx_color_delete(btn_text_color);
124 if (btn_highlight_color != NULL)
125 gfx_color_delete(btn_highlight_color);
126 if (btn_shadow_color != NULL)
127 gfx_color_delete(btn_shadow_color);
[47728678]128 if (tface != NULL)
129 gfx_typeface_destroy(tface);
130 free(resource);
131 return rc;
132}
133
134/** Destroy UI resource.
135 *
[4ed00d3]136 * @param resource UI resource or @c NULL
[47728678]137 */
138void ui_resource_destroy(ui_resource_t *resource)
139{
140 if (resource == NULL)
141 return;
142
[de9992c]143 gfx_color_delete(resource->btn_frame_color);
144 gfx_color_delete(resource->btn_face_color);
145 gfx_color_delete(resource->btn_text_color);
146 gfx_color_delete(resource->btn_highlight_color);
147 gfx_color_delete(resource->btn_shadow_color);
148
[47728678]149 gfx_font_close(resource->font);
150 gfx_typeface_destroy(resource->tface);
151 free(resource);
152}
153
154/** @}
155 */
Note: See TracBrowser for help on using the repository browser.