source: mainline/uspace/lib/gui/label.c@ b7fd2a0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7fd2a0 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[6d5e378]1/*
2 * Copyright (c) 2012 Petr Koupy
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 gui
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <str.h>
37#include <drawctx.h>
[38d150e]38#include <stdlib.h>
[6d5e378]39#include <surface.h>
[2cc1ec0]40#include <font/embedded.h>
41#include <errno.h>
[6d5e378]42#include "window.h"
43#include "label.h"
44
[296e124e]45static void paint_internal(widget_t *widget)
[6d5e378]46{
[296e124e]47 label_t *lbl = (label_t *) widget;
[6d5e378]48
49 surface_t *surface = window_claim(lbl->widget.window);
[296e124e]50 if (!surface)
[6d5e378]51 window_yield(lbl->widget.window);
[296e124e]52
[6d5e378]53 drawctx_t drawctx;
54 drawctx_init(&drawctx, surface);
[296e124e]55
[6d5e378]56 drawctx_set_source(&drawctx, &lbl->background);
[296e124e]57 drawctx_transfer(&drawctx, widget->hpos, widget->vpos, widget->width,
58 widget->height);
59
[6d5e378]60 sysarg_t cpt_width;
61 sysarg_t cpt_height;
[2cc1ec0]62 font_get_box(lbl->font, lbl->caption, &cpt_width, &cpt_height);
[296e124e]63
64 if ((widget->width >= cpt_width) && (widget->height >= cpt_height)) {
65 sysarg_t x = ((widget->width - cpt_width) / 2) + widget->hpos;
66 sysarg_t y = ((widget->height - cpt_height) / 2) + widget->vpos;
67
68 drawctx_set_source(&drawctx, &lbl->text);
[2cc1ec0]69 drawctx_set_font(&drawctx, lbl->font);
[296e124e]70
71 if (lbl->caption)
[6d5e378]72 drawctx_print(&drawctx, lbl->caption, x, y);
73 }
[296e124e]74
[6d5e378]75 window_yield(lbl->widget.window);
76}
77
78static void on_rewrite(widget_t *widget, void *data)
79{
80 if (data != NULL) {
81 label_t *lbl = (label_t *) widget;
[296e124e]82
[6d5e378]83 const char *new_caption = (const char *) data;
84 lbl->caption = str_dup(new_caption);
[296e124e]85
[6d5e378]86 sysarg_t cpt_width;
87 sysarg_t cpt_height;
[2cc1ec0]88 font_get_box(lbl->font, lbl->caption, &cpt_width, &cpt_height);
[296e124e]89
[6d5e378]90 lbl->widget.width_min = cpt_width + 4;
91 lbl->widget.height_min = cpt_height + 4;
92 lbl->widget.width_ideal = lbl->widget.width_min;
93 lbl->widget.height_ideal = lbl->widget.height_min;
[296e124e]94
[6d5e378]95 window_refresh(lbl->widget.window);
96 }
97}
98
99void deinit_label(label_t *lbl)
100{
101 widget_deinit(&lbl->widget);
102 free(lbl->caption);
[2cc1ec0]103 font_release(lbl->font);
[6d5e378]104}
105
106static void label_destroy(widget_t *widget)
107{
108 label_t *lbl = (label_t *) widget;
109
[296e124e]110 deinit_label(lbl);
[6d5e378]111 free(lbl);
112}
113
114static void label_reconfigure(widget_t *widget)
115{
116 /* no-op */
117}
118
119static void label_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
120 sysarg_t width, sysarg_t height)
121{
122 widget_modify(widget, hpos, vpos, width, height);
123 paint_internal(widget);
124}
125
126static void label_repaint(widget_t *widget)
127{
128 paint_internal(widget);
129 window_damage(widget->window);
130}
131
132static void label_handle_keyboard_event(widget_t *widget, kbd_event_t event)
133{
134 /* no-op */
135}
136
137static void label_handle_position_event(widget_t *widget, pos_event_t event)
138{
139 /* no-op */
140}
141
[10cb47e]142bool init_label(label_t *lbl, widget_t *parent, const void *data,
143 const char *caption, uint16_t points, pixel_t background, pixel_t text)
[6d5e378]144{
[10cb47e]145 widget_init(&lbl->widget, parent, data);
[296e124e]146
[6d5e378]147 lbl->widget.destroy = label_destroy;
148 lbl->widget.reconfigure = label_reconfigure;
149 lbl->widget.rearrange = label_rearrange;
150 lbl->widget.repaint = label_repaint;
151 lbl->widget.handle_keyboard_event = label_handle_keyboard_event;
152 lbl->widget.handle_position_event = label_handle_position_event;
[296e124e]153
[6d5e378]154 source_init(&lbl->background);
155 source_set_color(&lbl->background, background);
[296e124e]156
157 source_init(&lbl->text);
158 source_set_color(&lbl->text, text);
159
160 if (caption == NULL)
[6d5e378]161 lbl->caption = NULL;
[296e124e]162 else
[6d5e378]163 lbl->caption = str_dup(caption);
[296e124e]164
[b7fd2a0]165 errno_t rc = embedded_font_create(&lbl->font, points);
[2cc1ec0]166 if (rc != EOK) {
167 free(lbl->caption);
168 lbl->caption = NULL;
169 return false;
170 }
[296e124e]171
[6d5e378]172 sysarg_t cpt_width;
173 sysarg_t cpt_height;
[2cc1ec0]174 font_get_box(lbl->font, lbl->caption, &cpt_width, &cpt_height);
[296e124e]175
[6d5e378]176 lbl->widget.width_min = cpt_width + 4;
177 lbl->widget.height_min = cpt_height + 4;
178 lbl->widget.width_ideal = lbl->widget.width_min;
179 lbl->widget.height_ideal = lbl->widget.height_min;
[296e124e]180
[6d5e378]181 lbl->rewrite = on_rewrite;
[296e124e]182
[6d5e378]183 return true;
184}
185
[10cb47e]186label_t *create_label(widget_t *parent, const void *data, const char *caption,
187 uint16_t points, pixel_t background, pixel_t text)
[6d5e378]188{
189 label_t *lbl = (label_t *) malloc(sizeof(label_t));
[296e124e]190 if (!lbl)
[6d5e378]191 return NULL;
[296e124e]192
[10cb47e]193 if (init_label(lbl, parent, data, caption, points, background, text))
[6d5e378]194 return lbl;
[296e124e]195
196 free(lbl);
197 return NULL;
[6d5e378]198}
199
200/** @}
201 */
Note: See TracBrowser for help on using the repository browser.