Mercurial > molko
comparison libmlk-ui/ui/button.c @ 243:71b3b7036de7
misc: lot of cleanups,
- prefix libraries with libmlk,
- move assets from source directories closes #2520,
- prefix header guards closes #2519
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 28 Nov 2020 22:37:30 +0100 |
parents | libui/ui/button.c@76afe639fd72 |
children | 196264679079 |
comparison
equal
deleted
inserted
replaced
242:4c24604efcab | 243:71b3b7036de7 |
---|---|
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 #include <string.h> | |
21 | |
22 #include <core/action.h> | |
23 #include <core/event.h> | |
24 #include <core/maths.h> | |
25 #include <core/painter.h> | |
26 #include <core/trace.h> | |
27 | |
28 #include "align.h" | |
29 #include "button.h" | |
30 #include "label.h" | |
31 #include "theme.h" | |
32 #include "ui_p.h" | |
33 | |
34 static bool | |
35 is_boxed(const struct button *button, const struct event_click *click) | |
36 { | |
37 assert(button); | |
38 assert(click); | |
39 assert(click->type == EVENT_CLICKDOWN || click->type == EVENT_CLICKUP); | |
40 | |
41 return maths_is_boxed(button->x, button->y, button->w, button->h, | |
42 click->x, click->y); | |
43 } | |
44 | |
45 static void | |
46 handle(struct action *act, const union event *ev) | |
47 { | |
48 button_handle(act->data, ev); | |
49 } | |
50 | |
51 static void | |
52 draw(struct action *act) | |
53 { | |
54 button_draw(act->data); | |
55 } | |
56 | |
57 void | |
58 button_draw_default(const struct theme *t, const struct button *button) | |
59 { | |
60 assert(t); | |
61 assert(button); | |
62 | |
63 (void)t; | |
64 | |
65 struct label label = { | |
66 .text = button->text, | |
67 }; | |
68 unsigned int lw, lh; | |
69 | |
70 label_query(&label, &lw, &lh); | |
71 | |
72 if (lw > button->w) | |
73 tracef(_("button width is too small for text: %u < %u"), button->w, lw); | |
74 if (lh > button->h) | |
75 tracef(_("button height is too small for text: %u < %u"), button->h, lh); | |
76 | |
77 align(ALIGN_CENTER, &label.x, &label.y, lw, lh, | |
78 button->x, button->y, button->w, button->h); | |
79 | |
80 painter_set_color(0x577277ff); | |
81 painter_draw_rectangle(button->x, button->y, button->w, button->h); | |
82 | |
83 label_draw(&label); | |
84 } | |
85 | |
86 void | |
87 button_handle(struct button *button, const union event *ev) | |
88 { | |
89 assert(button); | |
90 assert(ev); | |
91 | |
92 switch (ev->type) { | |
93 case EVENT_CLICKDOWN: | |
94 if (is_boxed(button, &ev->click)) | |
95 button->state = BUTTON_STATE_PRESSED; | |
96 break; | |
97 case EVENT_CLICKUP: | |
98 /* | |
99 * If the button was pressed, indicate that the button is | |
100 * finally activated. This let the user to move the cursor | |
101 * outside the button to "forget" the press. | |
102 */ | |
103 if (!is_boxed(button, &ev->click)) | |
104 button->state = BUTTON_STATE_NONE; | |
105 else if (button->state == BUTTON_STATE_PRESSED) | |
106 button->state = BUTTON_STATE_ACTIVATED; | |
107 break; | |
108 default: | |
109 break; | |
110 } | |
111 } | |
112 | |
113 void | |
114 button_reset(struct button *button) | |
115 { | |
116 assert(button); | |
117 | |
118 button->state = BUTTON_STATE_NONE; | |
119 } | |
120 | |
121 void | |
122 button_draw(const struct button *button) | |
123 { | |
124 assert(button); | |
125 | |
126 theme_draw_button(button->theme, button); | |
127 } | |
128 | |
129 void | |
130 button_action(struct button *button, struct action *act) | |
131 { | |
132 assert(button); | |
133 assert(act); | |
134 | |
135 memset(act, 0, sizeof (*act)); | |
136 act->data = button; | |
137 act->handle = handle; | |
138 act->draw = draw; | |
139 } |