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

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

Push button - first light

Introduced ui_resource_t class to hold common UI resources such as font,
colors, etc. We don't want every button to load its own copy of the font.

  • Property mode set to 100644
File size: 2.9 KB
Line 
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;
61 errno_t rc;
62
63 resource = calloc(1, sizeof(ui_resource_t));
64 if (resource == NULL)
65 return ENOMEM;
66
67 rc = gfx_typeface_open(gc, ui_typeface_path, &tface);
68 if (rc != EOK)
69 goto error;
70
71 finfo = gfx_typeface_first_font(tface);
72 if (finfo == NULL) {
73 rc = EIO;
74 goto error;
75 }
76
77 rc = gfx_font_open(finfo, &font);
78 if (rc != EOK)
79 goto error;
80
81 resource->gc = gc;
82 resource->tface = tface;
83 resource->font = font;
84 *rresource = resource;
85 return EOK;
86error:
87 if (tface != NULL)
88 gfx_typeface_destroy(tface);
89 free(resource);
90 return rc;
91}
92
93/** Destroy UI resource.
94 *
95 * @param resource Push button or @c NULL
96 */
97void ui_resource_destroy(ui_resource_t *resource)
98{
99 if (resource == NULL)
100 return;
101
102 gfx_font_close(resource->font);
103 gfx_typeface_destroy(resource->tface);
104 free(resource);
105}
106
107/** @}
108 */
Note: See TracBrowser for help on using the repository browser.