aboutsummaryrefslogtreecommitdiffstats
path: root/src/gwin/checkbox.c
blob: 4e9908969d0cddcbd023562ce3e49e5db0b91565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * This file is subject to the terms of the GFX License, v1.0. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://chibios-gfx.com/license.html
 */

/**
 * @file    src/gwin/checkbox.c
 * @brief   GWIN sub-system button code.
 *
 * @defgroup Checkbox Checkbox
 * @ingroup GWIN
 *
 * @{
 */

#include "gfx.h"

#if (GFX_USE_GWIN && GWIN_NEED_CHECKBOX) || defined(__DOXYGEN__)

static const GCheckboxDrawStyle GCheckboxDefaultStyleSelected = {
	Green
};

static const GCheckboxDrawStyle GCheckboxDefaultStyleUnselected = {
	Red
};

/* process an event callback */
static void gwinCheckboxCallback(void *param, GEvent *pe) {
	GSourceListener *psl;
	#define gh		((GHandle)param)
	#define gbw		((GCheckboxObject *)param)
	#define gsh		((GSourceHandle)param)
	#define pme		((GEventMouse *)pe)
	#define pte		((GEventTouch *)pe)
	#define pxe		((GEventToggle *)pe)
	#define pbe		((GEventGWinCheckbox *)pe)

	/* check if checkbox is disabled */
	if (!gh->enabled)
		return;

	switch (pe->type) {
	#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
		case GEVENT_MOUSE:
		case GEVENT_TOUCH:

			// Ignore anything other than the primary mouse button going up or down
			if (!((pme->current_buttons ^ pme->last_buttons) & GINPUT_MOUSE_BTN_LEFT))
				return;

			if ((pme->current_buttons & GINPUT_MOUSE_BTN_LEFT)
					&& pme->x >= gbw->gwin.x && pme->x < gbw->gwin.x + gbw->gwin.width
					&& pme->y >= gbw->gwin.y && pme->y < gbw->gwin.y + gbw->gwin.height) {

				gbw->state = !gbw->state;

				gwinCheckboxDraw((GHandle)param);
			}
	#endif /* GFX_USE_GINPUT && GINPUT_NEED_MOUSE */

		default:
			return;
	}

	#undef gh
	#undef pbe
	#undef pme
	#undef pte
	#undef pxe
	#undef gsh
	#undef gbw
}

GHandle gwinCheckboxCreate(GCheckboxObject *gb, coord_t x, coord_t y, coord_t width, coord_t height) {
	if (!(gb = (GCheckboxObject *)_gwinInit((GWindowObject *)gb, x, y, width, height, sizeof(GCheckboxObject))))
		return 0;

	gb->gwin.type = GW_CHECKBOX;
	gb->fn = 0;
	gb->param = 0;
	gb->state = GCHBX_UNSET;

	gwinCheckboxSetStyle(&gb->gwin, GCHBX_NORMAL, &GCheckboxDefaultStyleSelected, &GCheckboxDefaultStyleUnselected);
	geventListenerInit(&gb->listener);
	geventRegisterCallback(&gb->listener, gwinCheckboxCallback, gb);

	// checkboxes are enabled by default
	gb->gwin.enabled = TRUE;

	return (GHandle)gb;
}

void gwinCheckboxSetStyle(GHandle gh, GCheckboxShape shape, const GCheckboxDrawStyle *pSelected, const GCheckboxDrawStyle *pUnselected) {
	#define gcw		((GCheckboxObject *)gh)

	if (gh->type != GW_CHECKBOX)
		return;

	switch (shape) {
		case GCHBX_NORMAL:
			gcw->fn = gwinCheckboxDraw_Normal;
			break;

		default:
			gcw->fn = gwinCheckboxDraw_Normal;
			break;
	}

	if (pSelected) {
		gcw->set.color = pSelected->color;
	}

	if (pUnselected) {
		gcw->unset.color = pSelected->color;
	}

	#undef gcw
}

void gwinCheckboxSetEnabled(GHandle gh, bool_t enabled) {
	if (gh->type != GW_CHECKBOX)
		return;

	gh->enabled = enabled;
}

void gwinCheckboxDraw(GHandle gh) {
	#define gcw		((GCheckboxObject *)gh)

	if (gh->type != GW_CHECKBOX)
		return;

	#if GDISP_NEED_CLIP
		gdispSetClip(gh->x, gh->y, gh->width, gh->height);
	#endif

	gcw->fn(gh,
            gcw->gwin.enabled,
            gcw->state,
            gcw->param);

	#undef gcw
}

void gwinCheckboxDraw_Normal(GHandle gh, bool_t enabled, bool_t state, void* param) {
	(void) enabled;
	(void) param;

	if (state)
		gdispClear(Green);
	else
		gdispClear(Red);
}

#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
	bool_t gwinCheckboxAttachMouse(GHandle gh, uint16_t instance) {
		GSourceHandle gsh;

		if (gh->type != GW_CHECKBOX || !(gsh = ginputGetMouse(instance)))
			return FALSE;

		return geventAttachSource(&((GCheckboxObject *)gh)->listener, gsh, GLISTEN_MOUSEMETA);
	}
#endif

#endif /* (GFX_USE_GWIN && GWIN_NEED_CHECKBOX) */
/** @} */