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 Push button
|
---|
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/pbutton.h>
|
---|
47 | #include "../private/pbutton.h"
|
---|
48 | #include "../private/resource.h"
|
---|
49 |
|
---|
50 | /** Caption movement when button is pressed down */
|
---|
51 | enum {
|
---|
52 | ui_pb_press_dx = 1,
|
---|
53 | ui_pb_press_dy = 1
|
---|
54 | };
|
---|
55 |
|
---|
56 | static ui_evclaim_t ui_pbutton_ctl_pos_event(void *, pos_event_t *);
|
---|
57 |
|
---|
58 | /** Push button control ops */
|
---|
59 | ui_control_ops_t ui_pbutton_ops = {
|
---|
60 | .pos_event = ui_pbutton_ctl_pos_event
|
---|
61 | };
|
---|
62 |
|
---|
63 | /** Create new push button.
|
---|
64 | *
|
---|
65 | * @param resource UI resource
|
---|
66 | * @param caption Caption
|
---|
67 | * @param rpbutton Place to store pointer to new push button
|
---|
68 | * @return EOK on success, ENOMEM if out of memory
|
---|
69 | */
|
---|
70 | errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
|
---|
71 | ui_pbutton_t **rpbutton)
|
---|
72 | {
|
---|
73 | ui_pbutton_t *pbutton;
|
---|
74 | errno_t rc;
|
---|
75 |
|
---|
76 | pbutton = calloc(1, sizeof(ui_pbutton_t));
|
---|
77 | if (pbutton == NULL)
|
---|
78 | return ENOMEM;
|
---|
79 |
|
---|
80 | rc = ui_control_new(&ui_pbutton_ops, (void *) pbutton,
|
---|
81 | &pbutton->control);
|
---|
82 | if (rc != EOK) {
|
---|
83 | free(pbutton);
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 |
|
---|
87 | pbutton->caption = str_dup(caption);
|
---|
88 | if (pbutton->caption == NULL) {
|
---|
89 | ui_control_delete(pbutton->control);
|
---|
90 | free(pbutton);
|
---|
91 | return ENOMEM;
|
---|
92 | }
|
---|
93 |
|
---|
94 | pbutton->res = resource;
|
---|
95 | *rpbutton = pbutton;
|
---|
96 | return EOK;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Destroy push button.
|
---|
100 | *
|
---|
101 | * @param pbutton Push button or @c NULL
|
---|
102 | */
|
---|
103 | void ui_pbutton_destroy(ui_pbutton_t *pbutton)
|
---|
104 | {
|
---|
105 | if (pbutton == NULL)
|
---|
106 | return;
|
---|
107 |
|
---|
108 | ui_control_delete(pbutton->control);
|
---|
109 | free(pbutton);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Get base control from push button.
|
---|
113 | *
|
---|
114 | * @param pbutton Push button
|
---|
115 | * @return Control
|
---|
116 | */
|
---|
117 | ui_control_t *ui_pbutton_ctl(ui_pbutton_t *pbutton)
|
---|
118 | {
|
---|
119 | return pbutton->control;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** Set push button callbacks.
|
---|
123 | *
|
---|
124 | * @param pbutton Push button
|
---|
125 | * @param cb Push button callbacks
|
---|
126 | * @param arg Callback argument
|
---|
127 | */
|
---|
128 | void ui_pbutton_set_cb(ui_pbutton_t *pbutton, ui_pbutton_cb_t *cb, void *arg)
|
---|
129 | {
|
---|
130 | pbutton->cb = cb;
|
---|
131 | pbutton->arg = arg;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Set button rectangle.
|
---|
135 | *
|
---|
136 | * @param pbutton Button
|
---|
137 | * @param rect New button rectangle
|
---|
138 | */
|
---|
139 | void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
|
---|
140 | {
|
---|
141 | pbutton->rect = *rect;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /** Set default flag.
|
---|
145 | *
|
---|
146 | * Default button is the one activated by Enter key, it is marked
|
---|
147 | * by thicker frame.
|
---|
148 | *
|
---|
149 | * @param pbutton Button
|
---|
150 | * @param isdefault @c true iff button is default
|
---|
151 | */
|
---|
152 | void ui_pbutton_set_default(ui_pbutton_t *pbutton, bool isdefault)
|
---|
153 | {
|
---|
154 | pbutton->isdefault = isdefault;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Paint outer button frame.
|
---|
158 | *
|
---|
159 | * @param pbutton Push button
|
---|
160 | * @return EOK on success or an error code
|
---|
161 | */
|
---|
162 | static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton)
|
---|
163 | {
|
---|
164 | gfx_rect_t rect;
|
---|
165 | gfx_coord_t thickness;
|
---|
166 | errno_t rc;
|
---|
167 |
|
---|
168 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
169 |
|
---|
170 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_frame_color);
|
---|
171 | if (rc != EOK)
|
---|
172 | goto error;
|
---|
173 |
|
---|
174 | rect.p0.x = pbutton->rect.p0.x + 1;
|
---|
175 | rect.p0.y = pbutton->rect.p0.y;
|
---|
176 | rect.p1.x = pbutton->rect.p1.x - 1;
|
---|
177 | rect.p1.y = pbutton->rect.p0.y + thickness;
|
---|
178 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
179 | if (rc != EOK)
|
---|
180 | goto error;
|
---|
181 |
|
---|
182 | rect.p0.x = pbutton->rect.p0.x + 1;
|
---|
183 | rect.p0.y = pbutton->rect.p1.y - thickness;
|
---|
184 | rect.p1.x = pbutton->rect.p1.x - 1;
|
---|
185 | rect.p1.y = pbutton->rect.p1.y;
|
---|
186 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
187 | if (rc != EOK)
|
---|
188 | goto error;
|
---|
189 |
|
---|
190 | rect.p0.x = pbutton->rect.p0.x;
|
---|
191 | rect.p0.y = pbutton->rect.p0.y + 1;
|
---|
192 | rect.p1.x = pbutton->rect.p0.x + thickness;
|
---|
193 | rect.p1.y = pbutton->rect.p1.y - 1;
|
---|
194 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
195 | if (rc != EOK)
|
---|
196 | goto error;
|
---|
197 |
|
---|
198 | rect.p0.x = pbutton->rect.p1.x - thickness;
|
---|
199 | rect.p0.y = pbutton->rect.p0.y + 1;
|
---|
200 | rect.p1.x = pbutton->rect.p1.x;
|
---|
201 | rect.p1.y = pbutton->rect.p1.y - 1;
|
---|
202 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
203 | if (rc != EOK)
|
---|
204 | goto error;
|
---|
205 |
|
---|
206 | return EOK;
|
---|
207 | error:
|
---|
208 | return rc;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /** Paint outset button bevel.
|
---|
212 | *
|
---|
213 | * @param pbutton Push button
|
---|
214 | * @return EOK on success or an error code
|
---|
215 | */
|
---|
216 | static errno_t ui_pbutton_paint_outset(ui_pbutton_t *pbutton,
|
---|
217 | gfx_rect_t *rect)
|
---|
218 | {
|
---|
219 | return ui_paint_bevel(pbutton->res->gc, rect,
|
---|
220 | pbutton->res->btn_highlight_color,
|
---|
221 | pbutton->res->btn_shadow_color, 2, NULL);
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** Paint inset button bevel.
|
---|
225 | *
|
---|
226 | * @param pbutton Push button
|
---|
227 | * @return EOK on success or an error code
|
---|
228 | */
|
---|
229 | static errno_t ui_pbutton_paint_inset(ui_pbutton_t *pbutton,
|
---|
230 | gfx_rect_t *rect)
|
---|
231 | {
|
---|
232 | return ui_paint_bevel(pbutton->res->gc, rect,
|
---|
233 | pbutton->res->btn_shadow_color,
|
---|
234 | pbutton->res->btn_face_color, 2, NULL);
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** Paint push button.
|
---|
238 | *
|
---|
239 | * @param pbutton Push button
|
---|
240 | * @return EOK on success or an error code
|
---|
241 | */
|
---|
242 | errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
|
---|
243 | {
|
---|
244 | gfx_coord2_t pos;
|
---|
245 | gfx_text_fmt_t fmt;
|
---|
246 | gfx_rect_t rect;
|
---|
247 | gfx_coord_t thickness;
|
---|
248 | bool depressed;
|
---|
249 | errno_t rc;
|
---|
250 |
|
---|
251 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
252 | depressed = pbutton->held && pbutton->inside;
|
---|
253 |
|
---|
254 | rect.p0.x = pbutton->rect.p0.x + thickness;
|
---|
255 | rect.p0.y = pbutton->rect.p0.y + thickness;
|
---|
256 | rect.p1.x = pbutton->rect.p1.x - thickness;
|
---|
257 | rect.p1.y = pbutton->rect.p1.y - thickness;
|
---|
258 |
|
---|
259 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
|
---|
260 | if (rc != EOK)
|
---|
261 | goto error;
|
---|
262 |
|
---|
263 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
264 | if (rc != EOK)
|
---|
265 | goto error;
|
---|
266 |
|
---|
267 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_text_color);
|
---|
268 | if (rc != EOK)
|
---|
269 | goto error;
|
---|
270 |
|
---|
271 | /* Center of button rectangle */
|
---|
272 | pos.x = (rect.p0.x + rect.p1.x) / 2;
|
---|
273 | pos.y = (rect.p0.y + rect.p1.y) / 2;
|
---|
274 |
|
---|
275 | if (depressed) {
|
---|
276 | pos.x += ui_pb_press_dx;
|
---|
277 | pos.y += ui_pb_press_dy;
|
---|
278 | }
|
---|
279 |
|
---|
280 | gfx_text_fmt_init(&fmt);
|
---|
281 | fmt.halign = gfx_halign_center;
|
---|
282 | fmt.valign = gfx_valign_center;
|
---|
283 |
|
---|
284 | rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
|
---|
285 | if (rc != EOK)
|
---|
286 | goto error;
|
---|
287 |
|
---|
288 | rc = ui_pbutton_paint_frame(pbutton);
|
---|
289 | if (rc != EOK)
|
---|
290 | goto error;
|
---|
291 |
|
---|
292 | if (depressed) {
|
---|
293 | rc = ui_pbutton_paint_inset(pbutton, &rect);
|
---|
294 | if (rc != EOK)
|
---|
295 | goto error;
|
---|
296 | } else {
|
---|
297 | rc = ui_pbutton_paint_outset(pbutton, &rect);
|
---|
298 | if (rc != EOK)
|
---|
299 | goto error;
|
---|
300 | }
|
---|
301 |
|
---|
302 | return EOK;
|
---|
303 | error:
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /** Press down button.
|
---|
308 | *
|
---|
309 | * @param pbutton Push button
|
---|
310 | */
|
---|
311 | void ui_pbutton_press(ui_pbutton_t *pbutton)
|
---|
312 | {
|
---|
313 | if (pbutton->held)
|
---|
314 | return;
|
---|
315 |
|
---|
316 | pbutton->inside = true;
|
---|
317 | pbutton->held = true;
|
---|
318 | (void) ui_pbutton_paint(pbutton);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /** Release button.
|
---|
322 | *
|
---|
323 | * @param pbutton Push button
|
---|
324 | */
|
---|
325 | void ui_pbutton_release(ui_pbutton_t *pbutton)
|
---|
326 | {
|
---|
327 | if (!pbutton->held)
|
---|
328 | return;
|
---|
329 |
|
---|
330 | pbutton->held = false;
|
---|
331 |
|
---|
332 | if (pbutton->inside) {
|
---|
333 | (void) ui_pbutton_paint(pbutton);
|
---|
334 | ui_pbutton_clicked(pbutton);
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | /** Pointer entered button.
|
---|
339 | *
|
---|
340 | * @param pbutton Push button
|
---|
341 | */
|
---|
342 | void ui_pbutton_enter(ui_pbutton_t *pbutton)
|
---|
343 | {
|
---|
344 | if (pbutton->inside)
|
---|
345 | return;
|
---|
346 |
|
---|
347 | pbutton->inside = true;
|
---|
348 | if (pbutton->held)
|
---|
349 | (void) ui_pbutton_paint(pbutton);
|
---|
350 | }
|
---|
351 |
|
---|
352 | /** Pointer left button.
|
---|
353 | *
|
---|
354 | * @param pbutton Push button
|
---|
355 | */
|
---|
356 | void ui_pbutton_leave(ui_pbutton_t *pbutton)
|
---|
357 | {
|
---|
358 | if (!pbutton->inside)
|
---|
359 | return;
|
---|
360 |
|
---|
361 | pbutton->inside = false;
|
---|
362 | if (pbutton->held)
|
---|
363 | (void) ui_pbutton_paint(pbutton);
|
---|
364 | }
|
---|
365 |
|
---|
366 | /** Button was clicked.
|
---|
367 | *
|
---|
368 | * @param pbutton Push button
|
---|
369 | */
|
---|
370 | void ui_pbutton_clicked(ui_pbutton_t *pbutton)
|
---|
371 | {
|
---|
372 | if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
|
---|
373 | pbutton->cb->clicked(pbutton, pbutton->arg);
|
---|
374 | }
|
---|
375 |
|
---|
376 | /** Handle push button position event.
|
---|
377 | *
|
---|
378 | * @param pbutton Push button
|
---|
379 | * @param pos_event Position event
|
---|
380 | * @return @c ui_claimed iff the event is claimed
|
---|
381 | */
|
---|
382 | ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
|
---|
383 | {
|
---|
384 | gfx_coord2_t pos;
|
---|
385 | bool inside;
|
---|
386 |
|
---|
387 | pos.x = event->hpos;
|
---|
388 | pos.y = event->vpos;
|
---|
389 |
|
---|
390 | inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
|
---|
391 |
|
---|
392 | switch (event->type) {
|
---|
393 | case POS_PRESS:
|
---|
394 | if (inside) {
|
---|
395 | ui_pbutton_press(pbutton);
|
---|
396 | return ui_claimed;
|
---|
397 | }
|
---|
398 | break;
|
---|
399 | case POS_RELEASE:
|
---|
400 | if (pbutton->held) {
|
---|
401 | ui_pbutton_release(pbutton);
|
---|
402 | return ui_claimed;
|
---|
403 | }
|
---|
404 | break;
|
---|
405 | case POS_UPDATE:
|
---|
406 | if (inside && !pbutton->inside) {
|
---|
407 | ui_pbutton_enter(pbutton);
|
---|
408 | return ui_claimed;
|
---|
409 | } else if (!inside && pbutton->inside) {
|
---|
410 | ui_pbutton_leave(pbutton);
|
---|
411 | }
|
---|
412 | break;
|
---|
413 | }
|
---|
414 |
|
---|
415 | return ui_unclaimed;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /** Handle push button control position event.
|
---|
419 | *
|
---|
420 | * @param arg Argument (ui_pbutton_t *)
|
---|
421 | * @param pos_event Position event
|
---|
422 | * @return @c ui_claimed iff the event is claimed
|
---|
423 | */
|
---|
424 | ui_evclaim_t ui_pbutton_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
425 | {
|
---|
426 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
---|
427 |
|
---|
428 | return ui_pbutton_pos_event(pbutton, event);
|
---|
429 | }
|
---|
430 |
|
---|
431 | /** @}
|
---|
432 | */
|
---|