source: mainline/uspace/lib/ui/src/checkbox.c@ fe40b67

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

Client-side UI rendering

It is possible to turn on and off and if turned on, one can also
enable or disable window double buffering (currently both options
are build-time).

  • Property mode set to 100644
File size: 8.6 KB
Line 
1/*
2 * Copyright (c) 2021 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 Check box
34 */
35
36#include <errno.h>
37#include <gfx/color.h>
38#include <gfx/context.h>
39#include <gfx/render.h>
40#include <gfx/text.h>
41#include <io/pos_event.h>
42#include <stdlib.h>
43#include <str.h>
44#include <ui/control.h>
45#include <ui/paint.h>
46#include <ui/checkbox.h>
47#include "../private/checkbox.h"
48#include "../private/resource.h"
49
50enum {
51 checkbox_box_w = 16,
52 checkbox_box_h = 16,
53 checkbox_label_margin = 8,
54};
55
56static void ui_checkbox_ctl_destroy(void *);
57static errno_t ui_checkbox_ctl_paint(void *);
58static ui_evclaim_t ui_checkbox_ctl_pos_event(void *, pos_event_t *);
59
60/** Check box control ops */
61ui_control_ops_t ui_checkbox_ops = {
62 .destroy = ui_checkbox_ctl_destroy,
63 .paint = ui_checkbox_ctl_paint,
64 .pos_event = ui_checkbox_ctl_pos_event
65};
66
67/** Create new check box.
68 *
69 * @param resource UI resource
70 * @param caption Caption
71 * @param rcheckbox Place to store pointer to new check box
72 * @return EOK on success, ENOMEM if out of memory
73 */
74errno_t ui_checkbox_create(ui_resource_t *resource, const char *caption,
75 ui_checkbox_t **rcheckbox)
76{
77 ui_checkbox_t *checkbox;
78 errno_t rc;
79
80 checkbox = calloc(1, sizeof(ui_checkbox_t));
81 if (checkbox == NULL)
82 return ENOMEM;
83
84 rc = ui_control_new(&ui_checkbox_ops, (void *) checkbox,
85 &checkbox->control);
86 if (rc != EOK) {
87 free(checkbox);
88 return rc;
89 }
90
91 checkbox->caption = str_dup(caption);
92 if (checkbox->caption == NULL) {
93 ui_control_delete(checkbox->control);
94 free(checkbox);
95 return ENOMEM;
96 }
97
98 checkbox->res = resource;
99 *rcheckbox = checkbox;
100 return EOK;
101}
102
103/** Destroy check box.
104 *
105 * @param checkbox Check box or @c NULL
106 */
107void ui_checkbox_destroy(ui_checkbox_t *checkbox)
108{
109 if (checkbox == NULL)
110 return;
111
112 ui_control_delete(checkbox->control);
113 free(checkbox);
114}
115
116/** Get base control from check box.
117 *
118 * @param checkbox Check box
119 * @return Control
120 */
121ui_control_t *ui_checkbox_ctl(ui_checkbox_t *checkbox)
122{
123 return checkbox->control;
124}
125
126/** Set check box callbacks.
127 *
128 * @param checkbox Check box
129 * @param cb Check box callbacks
130 * @param arg Callback argument
131 */
132void ui_checkbox_set_cb(ui_checkbox_t *checkbox, ui_checkbox_cb_t *cb, void *arg)
133{
134 checkbox->cb = cb;
135 checkbox->arg = arg;
136}
137
138/** Set button rectangle.
139 *
140 * @param checkbox Button
141 * @param rect New button rectangle
142 */
143void ui_checkbox_set_rect(ui_checkbox_t *checkbox, gfx_rect_t *rect)
144{
145 checkbox->rect = *rect;
146}
147
148/** Paint check box.
149 *
150 * @param checkbox Check box
151 * @return EOK on success or an error code
152 */
153errno_t ui_checkbox_paint(ui_checkbox_t *checkbox)
154{
155 gfx_coord2_t pos;
156 gfx_text_fmt_t fmt;
157 gfx_rect_t box_rect;
158 gfx_rect_t box_inside;
159 gfx_coord2_t box_center;
160 bool depressed;
161 errno_t rc;
162
163 box_rect.p0 = checkbox->rect.p0;
164 box_rect.p1.x = box_rect.p0.x + checkbox_box_w;
165 box_rect.p1.y = box_rect.p0.y + checkbox_box_h;
166
167 /* Paint checkbox frame */
168
169 rc = ui_paint_inset_frame(checkbox->res, &box_rect, &box_inside);
170 if (rc != EOK)
171 goto error;
172
173 /* Paint checkbox interior */
174
175 depressed = checkbox->held && checkbox->inside;
176
177 rc = gfx_set_color(checkbox->res->gc, depressed ?
178 checkbox->res->entry_act_bg_color :
179 checkbox->res->entry_bg_color);
180 if (rc != EOK)
181 goto error;
182
183 rc = gfx_fill_rect(checkbox->res->gc, &box_inside);
184 if (rc != EOK)
185 goto error;
186
187 /* Paint cross mark */
188
189 if (checkbox->checked) {
190 box_center.x = (box_inside.p0.x + box_inside.p1.x) / 2;
191 box_center.y = (box_inside.p0.y + box_inside.p1.y) / 2;
192
193 gfx_text_fmt_init(&fmt);
194 fmt.halign = gfx_halign_center;
195 fmt.valign = gfx_valign_center;
196
197 rc = gfx_set_color(checkbox->res->gc,
198 checkbox->res->entry_fg_color);
199 if (rc != EOK)
200 goto error;
201
202 rc = gfx_puttext(checkbox->res->font, &box_center, &fmt, "X");
203 if (rc != EOK)
204 goto error;
205 }
206
207 /* Paint checkbox label */
208
209 rc = gfx_set_color(checkbox->res->gc, checkbox->res->wnd_text_color);
210 if (rc != EOK)
211 goto error;
212
213 /* Label position */
214 pos.x = box_rect.p1.x + checkbox_label_margin;
215 pos.y = (box_rect.p0.y + box_rect.p1.y) / 2;
216
217 gfx_text_fmt_init(&fmt);
218 fmt.halign = gfx_halign_left;
219 fmt.valign = gfx_valign_center;
220
221 rc = gfx_puttext(checkbox->res->font, &pos, &fmt, checkbox->caption);
222 if (rc != EOK)
223 goto error;
224
225 rc = gfx_update(checkbox->res->gc);
226 if (rc != EOK)
227 goto error;
228
229 return EOK;
230error:
231 return rc;
232}
233
234/** Press down button.
235 *
236 * @param checkbox Check box
237 */
238void ui_checkbox_press(ui_checkbox_t *checkbox)
239{
240 if (checkbox->held)
241 return;
242
243 checkbox->inside = true;
244 checkbox->held = true;
245 (void) ui_checkbox_paint(checkbox);
246}
247
248/** Release button.
249 *
250 * @param checkbox Check box
251 */
252void ui_checkbox_release(ui_checkbox_t *checkbox)
253{
254 if (!checkbox->held)
255 return;
256
257 checkbox->held = false;
258
259 if (checkbox->inside) {
260 /* Toggle check box state */
261 checkbox->checked = !checkbox->checked;
262
263 /* Repaint and notify */
264 (void) ui_checkbox_paint(checkbox);
265 ui_checkbox_switched(checkbox);
266 }
267}
268
269/** Pointer entered button.
270 *
271 * @param checkbox Check box
272 */
273void ui_checkbox_enter(ui_checkbox_t *checkbox)
274{
275 if (checkbox->inside)
276 return;
277
278 checkbox->inside = true;
279 if (checkbox->held)
280 (void) ui_checkbox_paint(checkbox);
281}
282
283/** Pointer left button.
284 *
285 * @param checkbox Check box
286 */
287void ui_checkbox_leave(ui_checkbox_t *checkbox)
288{
289 if (!checkbox->inside)
290 return;
291
292 checkbox->inside = false;
293 if (checkbox->held)
294 (void) ui_checkbox_paint(checkbox);
295}
296
297/** Button was switched.
298 *
299 * @param checkbox Check box
300 */
301void ui_checkbox_switched(ui_checkbox_t *checkbox)
302{
303 if (checkbox->cb != NULL && checkbox->cb->switched != NULL) {
304 checkbox->cb->switched(checkbox, checkbox->arg,
305 checkbox->checked);
306 }
307}
308
309/** Handle check box position event.
310 *
311 * @param checkbox Check box
312 * @param pos_event Position event
313 * @return @c ui_claimed iff the event is claimed
314 */
315ui_evclaim_t ui_checkbox_pos_event(ui_checkbox_t *checkbox, pos_event_t *event)
316{
317 gfx_coord2_t pos;
318 bool inside;
319
320 pos.x = event->hpos;
321 pos.y = event->vpos;
322
323 inside = gfx_pix_inside_rect(&pos, &checkbox->rect);
324
325 switch (event->type) {
326 case POS_PRESS:
327 if (inside) {
328 ui_checkbox_press(checkbox);
329 return ui_claimed;
330 }
331 break;
332 case POS_RELEASE:
333 if (checkbox->held) {
334 ui_checkbox_release(checkbox);
335 return ui_claimed;
336 }
337 break;
338 case POS_UPDATE:
339 if (inside && !checkbox->inside) {
340 ui_checkbox_enter(checkbox);
341 return ui_claimed;
342 } else if (!inside && checkbox->inside) {
343 ui_checkbox_leave(checkbox);
344 }
345 break;
346 }
347
348 return ui_unclaimed;
349}
350
351/** Destroy check box control.
352 *
353 * @param arg Argument (ui_checkbox_t *)
354 */
355void ui_checkbox_ctl_destroy(void *arg)
356{
357 ui_checkbox_t *checkbox = (ui_checkbox_t *) arg;
358
359 ui_checkbox_destroy(checkbox);
360}
361
362/** Paint check box control.
363 *
364 * @param arg Argument (ui_checkbox_t *)
365 * @return EOK on success or an error code
366 */
367errno_t ui_checkbox_ctl_paint(void *arg)
368{
369 ui_checkbox_t *checkbox = (ui_checkbox_t *) arg;
370
371 return ui_checkbox_paint(checkbox);
372}
373
374/** Handle check box control position event.
375 *
376 * @param arg Argument (ui_checkbox_t *)
377 * @param pos_event Position event
378 * @return @c ui_claimed iff the event is claimed
379 */
380ui_evclaim_t ui_checkbox_ctl_pos_event(void *arg, pos_event_t *event)
381{
382 ui_checkbox_t *checkbox = (ui_checkbox_t *) arg;
383
384 return ui_checkbox_pos_event(checkbox, event);
385}
386
387/** @}
388 */
Note: See TracBrowser for help on using the repository browser.