aboutsummaryrefslogtreecommitdiffstats
path: root/src/gwin/gwin_radio.c
blob: c3d1564a9386af8b166716368571a48a360cd487 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.io/license.html
 */

/**
 * @file    src/gwin/gwin_radio.c
 * @brief   GWIN sub-system radio button code
 */

#include "../../gfx.h"

#if GFX_USE_GWIN && GWIN_NEED_RADIO

#include "gwin_class.h"

#define GRADIO_TAB_CNR			8		// Diagonal corner on active tab
#define GRADIO_TOP_FADE			50		// (GRADIO_TOP_FADE/255)% fade to white for top of tab/button
#define GRADIO_BOTTOM_FADE		25		// (GRADIO_BOTTOM_FADE/255)% fade to black for bottom of tab/button
#define GRADIO_OUTLINE_FADE		128		// (GRADIO_OUTLINE_FADE/255)% fade to background for active tab edge

// Send the button event
static void SendRadioEvent(GWidgetObject *gw) {
	GSourceListener	*	psl;
	GEvent *			pe;
	#define pbe			((GEventGWinRadio *)pe)

	// Trigger a GWIN Button Event
	psl = 0;
	while ((psl = geventGetSourceListener(GWIDGET_SOURCE, psl))) {
		if (!(pe = geventGetEventBuffer(psl)))
			continue;
		pbe->type = GEVENT_GWIN_RADIO;
		pbe->gwin = (GHandle)gw;
		pbe->group = ((GRadioObject *)gw)->group;
		#if GWIN_WIDGET_TAGS
			pbe->tag = gw->tag;
		#endif
		geventSendEvent(psl);
	}

	#undef pbe
}

#if GINPUT_NEED_MOUSE
	// A mouse down has occurred over the button
	static void RadioMouseDown(GWidgetObject *gw, gCoord x, gCoord y) {
		(void) x; (void) y;

		gwinRadioPress((GHandle)gw);
	}
#endif

#if GINPUT_NEED_KEYBOARD || GWIN_NEED_KEYBOARD
	static void RadioKeyboard(GWidgetObject* gw, GEventKeyboard* pke)
	{
		// Only react on KEYDOWN events. Ignore KEYUP events.
		if ((pke->keystate & GKEYSTATE_KEYUP))
			return;

		// ENTER and SPACE keys to check/uncheck the checkbox
		if (pke->c[0] == GKEY_ENTER || pke->c[0] == GKEY_SPACE) {
			gwinRadioPress((GHandle)gw);
		}
	}
#endif

#if GINPUT_NEED_TOGGLE
	// A toggle on has occurred
	static void RadioToggleOn(GWidgetObject *gw, gU16 role) {
		(void) role;

		gwinRadioPress((GHandle)gw);
	}

	static void RadioToggleAssign(GWidgetObject *gw, gU16 role, gU16 instance) {
		(void) role;
		((GRadioObject *)gw)->toggle = instance;
	}

	static gU16 RadioToggleGet(GWidgetObject *gw, gU16 role) {
		(void) role;
		return ((GRadioObject *)gw)->toggle;
	}
#endif

// The radio button VMT table
static const gwidgetVMT radioVMT = {
	{
		"Radio",				// The classname
		sizeof(GRadioObject),	// The object size
		_gwidgetDestroy,		// The destroy routine
		_gwidgetRedraw,			// The redraw routine
		0,						// The after-clear routine
	},
	gwinRadioDraw_Radio,		// The default drawing routine
	#if GINPUT_NEED_MOUSE
		{
			RadioMouseDown,			// Process mouse down events
			0,						// Process mouse up events (NOT USED)
			0,						// Process mouse move events (NOT USED)
		},
	#endif
	#if GINPUT_NEED_KEYBOARD || GWIN_NEED_KEYBOARD
		{
			RadioKeyboard			// Process keyboard events
		},
	#endif
	#if GINPUT_NEED_TOGGLE
		{
			1,						// 1 toggle role
			RadioToggleAssign,		// Assign Toggles
			RadioToggleGet,			// Get Toggles
			0,						// Process toggle off events (NOT USED)
			RadioToggleOn,				// Process toggle on events
		},
	#endif
	#if GINPUT_NEED_DIAL
		{
			0,						// No dial roles
			0,						// Assign Dials (NOT USED)
			0,						// Get Dials (NOT USED)
			0,						// Process dial move events (NOT USED)
		},
	#endif
};

GHandle gwinGRadioCreate(GDisplay *g, GRadioObject *gw, const GWidgetInit *pInit, gU16 group) {
	if (!(gw = (GRadioObject *)_gwidgetCreate(g, &gw->w, pInit, &radioVMT)))
		return 0;

	#if GINPUT_NEED_TOGGLE
		gw->toggle = GWIDGET_NO_INSTANCE;
	#endif
	gw->group = group;
	gwinSetVisible((GHandle)gw, pInit->g.show);
	return (GHandle)gw;
}

void gwinRadioPress(GHandle gh) {
	GHandle		gx;

	if (gh->vmt != (gwinVMT *)&radioVMT || (gh->flags & GRADIO_FLG_PRESSED))
		return;

	if ((gx = gwinRadioGetActive(((GRadioObject *)gh)->group))) {
		gx->flags &= ~GRADIO_FLG_PRESSED;
		_gwinUpdate(gx);
	}
	gh->flags |= GRADIO_FLG_PRESSED;
	_gwinUpdate(gh);
	SendRadioEvent((GWidgetObject *)gh);
}

gBool gwinRadioIsPressed(GHandle gh) {
	if (gh->vmt != (gwinVMT *)&radioVMT)
		return gFalse;

	return (gh->flags & GRADIO_FLG_PRESSED) ? gTrue : gFalse;
}

GHandle gwinRadioGetActive(gU16 group) {
	GHandle		gh;

	for(gh = gwinGetNextWindow(0); gh; gh = gwinGetNextWindow(gh)) {
		if (gh->vmt == (gwinVMT *)&radioVMT && ((GRadioObject *)gh)->group == group && (gh->flags & GRADIO_FLG_PRESSED))
			return gh;
	}
	return 0;
}

/*----------------------------------------------------------
 * Custom Draw Routines
 *----------------------------------------------------------*/

static const GColorSet *getDrawColors(GWidgetObject *gw) {
	if (!(gw->g.flags & GWIN_FLG_SYSENABLED))	return &gw->pstyle->disabled;
	if ((gw->g.flags & GRADIO_FLG_PRESSED))	return &gw->pstyle->pressed;
	return &gw->pstyle->enabled;
}

void gwinRadioDraw_Radio(GWidgetObject *gw, void *param) {
	#define gcw			((GRadioObject *)gw)
	gCoord				ld, df;
	const GColorSet *	pcol;
	(void)				param;

	if (gw->g.vmt != (gwinVMT *)&radioVMT) return;
	pcol = getDrawColors(gw);

	ld = gw->g.width < gw->g.height ? gw->g.width : gw->g.height;

	#if GDISP_NEED_CIRCLE
		df = (ld-1)/2;
		gdispGFillArea(gw->g.display, gw->g.x, gw->g.y, ld, ld, gw->pstyle->background);
		gdispGDrawCircle(gw->g.display, gw->g.x+df, gw->g.y+df, df, pcol->edge);

		if (gw->g.flags & GRADIO_FLG_PRESSED)
			gdispGFillCircle(gw->g.display, gw->g.x+df, gw->g.y+df, df <= 2 ? 1 : (df-2), pcol->fill);
	#else
		gdispGFillArea(gw->g.display, gw->g.x+1, gw->g.y+1, ld, ld-2, gw->pstyle->background);
		gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, ld, ld, pcol->edge);

		df = ld < 4 ? 1 : 2;
		if (gw->g.flags & GRADIO_FLG_PRESSED)
			gdispGFillArea(gw->g.display, gw->g.x+df, gw->g.y+df, ld-2*df, ld-2*df, pcol->fill);
	#endif

	_gwidgetDrawFocusCircle(gw, df);

	gdispGFillStringBox(gw->g.display, gw->g.x+ld+1, gw->g.y, gw->g.width-ld-1, gw->g.height, gw->text, gw->g.font, pcol->text, gw->pstyle->background, gJustifyLeft);
	#undef gcw
}

#if GWIN_FLAT_STYLING
	void gwinRadioDraw_Button(GWidgetObject *gw, void *param) {
		const GColorSet *	pcol;
		(void)				param;

		if (gw->g.vmt != (gwinVMT *)&radioVMT) return;
		pcol = getDrawColors(gw);

		#if GWIN_NEED_FLASHING
			// Flash only the on state.
			pcol = _gwinGetFlashedColor(gw, pcol, gFalse);
		#endif

		gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width-1, gw->g.height-1, gw->text, gw->g.font, pcol->text, pcol->fill, gJustifyCenter);
		gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, pcol->edge);
		gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gw->g.height-1, gw->g.x+gw->g.width-2, gw->g.y+gw->g.height-1, pcol->edge);

		// Render highlighted border if focused
		_gwidgetDrawFocusRect(gw, 1, 1, gw->g.width-2, gw->g.height-2);
	}
	void gwinRadioDraw_Tab(GWidgetObject *gw, void *param) {
		const GColorSet *	pcol;
		(void)				param;

		if (gw->g.vmt != (gwinVMT *)&radioVMT)	return;
		pcol = getDrawColors(gw);

		#if GWIN_NEED_FLASHING
			// Flash only the on state.
			pcol = _gwinGetFlashedColor(gw, pcol, gFalse);
		#endif

		if ((gw->g.flags & GRADIO_FLG_PRESSED)) {
			gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, pcol->edge);
			gdispGFillStringBox(gw->g.display, gw->g.x+1, gw->g.y+1, gw->g.width-2, gw->g.height-1, gw->text, gw->g.font, pcol->text, pcol->fill, gJustifyCenter);
		} else {
			gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width-1, gw->g.height-1, gw->text, gw->g.font, pcol->text, pcol->fill, gJustifyCenter);
			gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, pcol->edge);
			gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gw->g.height-1, gw->g.x+gw->g.width-2, gw->g.y+gw->g.height-1, pcol->edge);
		}

		// Render highlighted border if focused
		_gwidgetDrawFocusRect(gw, 0, 0, gw->g.width-1, gw->g.height-1);
	}
#else
	void gwinRadioDraw_Button(GWidgetObject *gw, void *param) {
		const GColorSet *	pcol;
		fixed				alpha;
		fixed				dalpha;
		gCoord				i;
		gColor				tcol, bcol;
		(void)				param;

		if (gw->g.vmt != (gwinVMT *)&radioVMT) return;
		pcol = getDrawColors(gw);
	
		/* Fill the box blended from variants of the fill color */
		tcol = gdispBlendColor(GFX_WHITE, pcol->fill, GRADIO_TOP_FADE);
		bcol = gdispBlendColor(GFX_BLACK, pcol->fill, GRADIO_BOTTOM_FADE);
		dalpha = FIXED(255)/gw->g.height;
		for(alpha = 0, i = 0; i < gw->g.height; i++, alpha += dalpha)
			gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+i, gw->g.x+gw->g.width-2, gw->g.y+i, gdispBlendColor(bcol, tcol, NONFIXED(alpha)));

		gdispGDrawStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width-1, gw->g.height-1, gw->text, gw->g.font, pcol->text, gJustifyCenter);
		gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, pcol->edge);
		gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gw->g.height-1, gw->g.x+gw->g.width-2, gw->g.y+gw->g.height-1, pcol->edge);

		// Render highlighted border if focused
		_gwidgetDrawFocusRect(gw, 0, 0, gw->g.width-1, gw->g.height-1);
	}
	void gwinRadioDraw_Tab(GWidgetObject *gw, void *param) {
		const GColorSet *	pcol;
		fixed				alpha;
		fixed				dalpha;
		gCoord				i;
		gColor				tcol, bcol;
		(void)				param;

		if (gw->g.vmt != (gwinVMT *)&radioVMT)	return;
		pcol = getDrawColors(gw);

		if ((gw->g.flags & GRADIO_FLG_PRESSED)) {
			tcol = gdispBlendColor(pcol->edge, gw->pstyle->background, GRADIO_OUTLINE_FADE);
			gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, gw->text, gw->g.font, pcol->text, gw->g.bgcolor, gJustifyCenter);
			gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y, gw->g.x+gw->g.width-(GRADIO_TAB_CNR+1), gw->g.y, tcol);
			gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-(GRADIO_TAB_CNR+1), gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+GRADIO_TAB_CNR, tcol);
			gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y+GRADIO_TAB_CNR, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, tcol);
		} else {
			/* Fill the box blended from variants of the fill color */
			tcol = gdispBlendColor(GFX_WHITE, pcol->fill, GRADIO_TOP_FADE);
			bcol = gdispBlendColor(GFX_BLACK, pcol->fill, GRADIO_BOTTOM_FADE);
			dalpha = FIXED(255)/gw->g.height;
			for(alpha = 0, i = 0; i < gw->g.height; i++, alpha += dalpha)
				gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+i, gw->g.x+gw->g.width-2, gw->g.y+i, gdispBlendColor(bcol, tcol, NONFIXED(alpha)));
			gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, pcol->edge);
			gdispGDrawStringBox(gw->g.display, gw->g.x+1, gw->g.y+1, gw->g.width-2, gw->g.height-2, gw->text, gw->g.font, pcol->text, gJustifyCenter);
		}

		// Render highlighted border if focused
		_gwidgetDrawFocusRect(gw, 0, 0, gw->g.width-1, gw->g.height-1);
	}
#endif

#endif /* GFX_USE_GWIN && GWIN_NEED_BUTTON */