source: mainline/uspace/lib/gui/button.c@ 41408d94

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

Curb the proliferation of libdraw headers

libdraw provides a lot of ambiguously named headers, which makes it
confusing. This change merges the subdirectories into single headers,
and moves all headers into draw subdirectory, so that it's obvious
at a glance what library the header belongs to.

Compare:

#include <path.h>
#include <source.h>
#include <font/bitmap_backend.h>
#include <font/pcf.h>

vs.

#include <draw/path.h>
#include <draw/source.h>
#include <draw/font.h>

  • Property mode set to 100644
File size: 5.9 KB
Line 
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 <stdlib.h>
38#include <draw/drawctx.h>
39#include <draw/surface.h>
40#include <draw/font.h>
41#include <errno.h>
42#include "common.h"
43#include "window.h"
44#include "button.h"
45
46static pixel_t color_highlight = PIXEL(255, 255, 255, 255);
47static pixel_t color_shadow = PIXEL(255, 85, 85, 85);
48
49static void paint_internal(widget_t *widget)
50{
51 button_t *btn = (button_t *) widget;
52
53 surface_t *surface = window_claim(btn->widget.window);
54 if (!surface)
55 window_yield(btn->widget.window);
56
57 source_t source;
58 source_init(&source);
59
60 drawctx_t drawctx;
61 drawctx_init(&drawctx, surface);
62
63 drawctx_set_compose(&drawctx, compose_over);
64 drawctx_set_source(&drawctx, &btn->background);
65 drawctx_transfer(&drawctx, widget->hpos, widget->vpos,
66 widget->width, widget->height);
67
68 if ((widget->width >= 8) && (widget->height >= 8)) {
69 drawctx_set_source(&drawctx, &source);
70 draw_bevel(&drawctx, &source, widget->hpos + 3, widget->vpos + 3,
71 widget->width - 6, widget->height - 6, color_highlight,
72 color_shadow);
73
74 drawctx_set_source(&drawctx, &btn->foreground);
75 drawctx_transfer(&drawctx, widget->hpos + 4, widget->vpos + 4,
76 widget->width - 8, widget->height - 8);
77 }
78
79 sysarg_t cpt_width;
80 sysarg_t cpt_height;
81 font_get_box(btn->font, btn->caption, &cpt_width, &cpt_height);
82
83 if ((widget->width >= cpt_width) && (widget->height >= cpt_height)) {
84 sysarg_t x = ((widget->width - cpt_width) / 2) + widget->hpos;
85 sysarg_t y = ((widget->height - cpt_height) / 2) + widget->vpos;
86
87 drawctx_set_source(&drawctx, &btn->text);
88 drawctx_set_font(&drawctx, btn->font);
89
90 if (btn->caption)
91 drawctx_print(&drawctx, btn->caption, x, y);
92 }
93
94 window_yield(btn->widget.window);
95}
96
97void deinit_button(button_t *btn)
98{
99 widget_deinit(&btn->widget);
100 free(btn->caption);
101 font_release(btn->font);
102}
103
104static void button_destroy(widget_t *widget)
105{
106 button_t *btn = (button_t *) widget;
107
108 deinit_button(btn);
109 free(btn);
110}
111
112static void button_reconfigure(widget_t *widget)
113{
114 /* no-op */
115}
116
117static void button_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
118 sysarg_t width, sysarg_t height)
119{
120 widget_modify(widget, hpos, vpos, width, height);
121 paint_internal(widget);
122}
123
124static void button_repaint(widget_t *widget)
125{
126 paint_internal(widget);
127 window_damage(widget->window);
128}
129
130static void button_handle_keyboard_event(widget_t *widget, kbd_event_t event)
131{
132 button_t *btn = (button_t *) widget;
133
134 if (event.key == KC_ENTER && event.type == KEY_PRESS)
135 sig_send(&btn->clicked, NULL);
136}
137
138static void button_handle_position_event(widget_t *widget, pos_event_t event)
139{
140 button_t *btn = (button_t *) widget;
141 widget->window->focus = widget;
142
143 // TODO make the click logic more robust (mouse grabbing, mouse moves)
144 if (event.btn_num == 1) {
145 if (event.type == POS_RELEASE)
146 sig_send(&btn->clicked, NULL);
147 }
148}
149
150bool init_button(button_t *btn, widget_t *parent, const void *data,
151 const char *caption, uint16_t points, pixel_t background,
152 pixel_t foreground, pixel_t text)
153{
154 widget_init(&btn->widget, parent, data);
155
156 btn->widget.destroy = button_destroy;
157 btn->widget.reconfigure = button_reconfigure;
158 btn->widget.rearrange = button_rearrange;
159 btn->widget.repaint = button_repaint;
160 btn->widget.handle_keyboard_event = button_handle_keyboard_event;
161 btn->widget.handle_position_event = button_handle_position_event;
162
163 source_init(&btn->background);
164 source_set_color(&btn->background, background);
165
166 source_init(&btn->foreground);
167 source_set_color(&btn->foreground, foreground);
168
169 source_init(&btn->text);
170 source_set_color(&btn->text, text);
171
172 if (caption == NULL)
173 btn->caption = NULL;
174 else
175 btn->caption = str_dup(caption);
176
177 errno_t rc = embedded_font_create(&btn->font, points);
178 if (rc != EOK) {
179 free(btn->caption);
180 btn->caption = NULL;
181 return false;
182 }
183
184 sysarg_t cpt_width;
185 sysarg_t cpt_height;
186 font_get_box(btn->font, btn->caption, &cpt_width, &cpt_height);
187 btn->widget.width_min = cpt_width + 10;
188 btn->widget.height_min = cpt_height + 10;
189 btn->widget.width_ideal = cpt_width + 30;
190 btn->widget.height_ideal = cpt_height + 10;
191
192 return true;
193}
194
195button_t *create_button(widget_t *parent, const void *data, const char *caption,
196 uint16_t points, pixel_t background, pixel_t foreground, pixel_t text)
197{
198 button_t *btn = (button_t *) malloc(sizeof(button_t));
199 if (!btn)
200 return NULL;
201
202 if (init_button(btn, parent, data, caption, points, background, foreground,
203 text))
204 return btn;
205
206 free(btn);
207 return NULL;
208}
209
210/** @}
211 */
Note: See TracBrowser for help on using the repository browser.