comparison libui/ui/button.c @ 148:c577c15df07f

misc: split libraries, closes #2496
author David Demelier <markand@malikania.fr>
date Thu, 15 Oct 2020 10:32:18 +0200
parents libcore/core/button.c@789b23e01f52
children b19d076856d2
comparison
equal deleted inserted replaced
147:b386d25832c8 148:c577c15df07f
1 /*
2 * button.c -- GUI button
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20
21 #include <core/event.h>
22 #include <core/maths.h>
23
24 #include "button.h"
25 #include "theme.h"
26
27 static bool
28 is_boxed(const struct button *button, const struct event_click *click)
29 {
30 assert(button);
31 assert(click);
32 assert(click->type == EVENT_CLICKDOWN || click->type == EVENT_CLICKUP);
33
34 return maths_is_boxed(button->x, button->y, button->w, button->h,
35 click->x, click->y);
36 }
37
38 void
39 button_handle(struct button *button, const union event *ev)
40 {
41 assert(button);
42 assert(ev);
43
44 switch (ev->type) {
45 case EVENT_CLICKDOWN:
46 if (is_boxed(button, &ev->click))
47 button->state = BUTTON_STATE_PRESSED;
48 break;
49 case EVENT_CLICKUP:
50 /*
51 * If the button was pressed, indicate that the button is
52 * finally activated. This let the user to move the cursor
53 * outside the button to "forget" the press.
54 */
55 if (!is_boxed(button, &ev->click))
56 button->state = BUTTON_STATE_NONE;
57 else if (button->state == BUTTON_STATE_PRESSED)
58 button->state = BUTTON_STATE_ACTIVATED;
59 break;
60 default:
61 break;
62 }
63 }
64
65 void
66 button_reset(struct button *button)
67 {
68 assert(button);
69
70 button->state = BUTTON_STATE_NONE;
71 }
72
73 void
74 button_draw(struct button *button)
75 {
76 assert(button);
77
78 theme_draw_button(button->theme, button);
79 }