comparison src/core/checkbox.c @ 115:3bd0d3a39e30

core: implement checkbox, closes #2486
author David Demelier <markand@malikania.fr>
date Sun, 12 Jul 2020 09:44:27 +0200
parents
children
comparison
equal deleted inserted replaced
114:bf7500aea454 115:3bd0d3a39e30
1 /*
2 * checkbox.c -- GUI checkbox
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 "checkbox.h"
22 #include "theme.h"
23 #include "event.h"
24 #include "maths.h"
25
26 static bool
27 is_boxed(const struct checkbox *cb, const struct event_click *click)
28 {
29 assert(cb);
30 assert(click && click->type == EVENT_CLICKDOWN);
31
32 return maths_is_boxed(cb->x, cb->y, cb->w, cb->h, click->x, click->y);
33 }
34
35 void
36 checkbox_handle(struct checkbox *cb, const union event *ev)
37 {
38 assert(cb);
39 assert(ev);
40
41 switch (ev->type) {
42 case EVENT_CLICKDOWN:
43 if (is_boxed(cb, &ev->click))
44 cb->checked = !cb->checked;
45 break;
46 default:
47 break;
48 }
49 }
50
51 void
52 checkbox_draw(const struct checkbox *cb)
53 {
54 theme_draw_checkbox(cb->theme, cb);
55 }