aboutsummaryrefslogtreecommitdiffstats
path: root/src/gwin/slider.c
blob: 01df189d403734f1d0c9fe545ab4abd9ad515051 (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
/*
 * 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://chibios-gfx.com/license.html
 */

/**
 * @file    src/gwin/slider.c
 * @brief   GWIN sub-system slider code.
 *
 * @defgroup Slider Slider
 * @ingroup GWIN
 *
 * @{
 */

#include "gfx.h"

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

#include "gwin/internal.h"

#ifndef GWIN_SLIDER_DEAD_BAND
	#define GWIN_SLIDER_DEAD_BAND	5
#endif

#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
	static void trackSliderDraw(GHandle gh, coord_t x, coord_t y);
#endif

static const GSliderDrawStyle GSliderDefaultStyle = {
	HTML2COLOR(0x404040),		// color_edge;
	HTML2COLOR(0x000000),		// color_thumb;
	HTML2COLOR(0x00E000),		// color_active;
	HTML2COLOR(0xE0E0E0),		// color_inactive;
};

// Process an event callback
static void gwinSliderCallback(void *param, GEvent *pe) {
	GSourceListener	*psl;
	#define gh		((GHandle)param)
	#define gsw		((GSliderObject *)param)
	#define gsh		((GSourceHandle)param)
	#define pme		((GEventMouse *)pe)
	#define pde		((GEventDial *)pe)
	#define pse		((GEventGWinSlider *)pe)

	switch (pe->type) {
	#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
		case GEVENT_MOUSE:
		case GEVENT_TOUCH:
			// If not tracking we only only interested in a mouse down over the slider
			if (!gsw->tracking) {
				if ((pme->meta & GMETA_MOUSE_DOWN)
						&& pme->x >= gh->x && pme->x < gh->x + gh->width
						&& pme->y >= gh->y && pme->y < gh->y + gh->height) {
					gsw->tracking = TRUE;
					trackSliderDraw(gh, pme->x-gh->x, pme->y-gh->y);
				}
				return;
			}

			// We are tracking the mouse

			// Test for button up
			if ((pme->meta & GMETA_MOUSE_UP)) {
				gsw->tracking = FALSE;

				#if !GWIN_BUTTON_LAZY_RELEASE
					// Are we over the slider?
					if (pme->x < gh->x || pme->x >= gh->x + gh->width
							|| pme->y < gh->y || pme->y >= gh->y + gh->height) {
						// No - restore the slider
						gwinSliderDraw(gh);
						return;
					}
				#endif

				// Set the new position
				if (gh->width < gh->height)
					gwinSetSliderPosition(gh,
						(uint16_t)((uint32_t)(gh->height-1-pme->y+gh->y-GWIN_SLIDER_DEAD_BAND)*(gsw->max-gsw->min)/(gh->height-2*GWIN_SLIDER_DEAD_BAND) + gsw->min));
				else
					gwinSetSliderPosition(gh,
						(uint16_t)((uint32_t)(pme->x-gh->x-GWIN_SLIDER_DEAD_BAND)*(gsw->max-gsw->min)/(gh->width-2*GWIN_SLIDER_DEAD_BAND) + gsw->min));

				// Update the display
				gwinSliderDraw(gh);

				// Generate the event
				break;
			}

			// If mouse down - track movement
			if ((pme->current_buttons & GINPUT_MOUSE_BTN_LEFT))
				trackSliderDraw(gh, pme->x-gh->x, pme->y-gh->y);

			return;
	#endif
	#if GFX_USE_GINPUT && GINPUT_NEED_DIAL
		case GEVENT_DIAL:
			// Set the new position
			gwinSetSliderPosition(gh, (uint16_t)((uint32_t)pde->value*(gsw->max-gsw->min)/ginputGetDialRange(pde->instance) + gsw->min));

			// Update the display
			gwinSliderDraw(gh);

			// Generate the event
			break;
	#endif

	default:
		return;
	}

	// Trigger a GWIN Slider Event
	psl = 0;
	while ((psl = geventGetSourceListener(gsh, psl))) {
		if (!(pe = geventGetEventBuffer(psl)))
			continue;
		pse->type = GEVENT_GWIN_SLIDER;
		pse->slider = gh;
		pse->position = gsw->pos;
		geventSendEvent(psl);
	}

	#undef pse
	#undef pme
	#undef pxe
	#undef gsh
	#undef gsw
	#undef gh
}

GHandle gwinCreateSlider(GSliderObject *gs, coord_t x, coord_t y, coord_t width, coord_t height) {
	if (!(gs = (GSliderObject *)_gwinInit((GWindowObject *)gs, x, y, width, height, sizeof(GSliderObject))))
		return 0;
	gs->gwin.type = GW_SLIDER;
	gs->fn = gwinSliderDraw_Std;
	gs->param = 0;
	gwinSetSliderStyle(&gs->gwin, &GSliderDefaultStyle);
	gs->min = 0;
	gs->max = 100;
	gs->pos = 0;
	gs->tracking = FALSE;
	geventListenerInit(&gs->listener);
	geventRegisterCallback(&gs->listener, gwinSliderCallback, gs);
	return (GHandle)gs;
}

void gwinSetSliderRange(GHandle gh, int min, int max) {
	#define gsw		((GSliderObject *)gh)

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

	if (min == max)		// prevent divide by 0 errors.
		max++;
	gsw->min = min;
	gsw->max = max;
	gsw->pos = min;
	#undef gsw
}

void gwinSetSliderPosition(GHandle gh, int pos) {
	#define gsw		((GSliderObject *)gh)

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

	if (gsw->min <= gsw->max) {
		if (pos < gsw->min) gsw->pos = gsw->min;
		else if (pos > gsw->max) gsw->pos = gsw->max;
		else gsw->pos = pos;
	} else {
		if (pos > gsw->min) gsw->pos = gsw->min;
		else if (pos < gsw->max) gsw->pos = gsw->max;
		else gsw->pos = pos;
	}
	#undef gsw
}

void gwinSetSliderStyle(GHandle gh, const GSliderDrawStyle *pStyle) {
	#define gsw		((GSliderObject *)gh)

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

	gsw->style.color_edge		= pStyle->color_edge;
	gsw->style.color_thumb	= pStyle->color_thumb;
	gsw->style.color_active	= pStyle->color_active;
	gsw->style.color_inactive	= pStyle->color_inactive;
	#undef gsw
}

#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
	static void trackSliderDraw(GHandle gh, coord_t x, coord_t y) {
		#define gsw		((GSliderObject *)gh)

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

		if (gh->height <= gh->width)
			gsw->fn(gh, FALSE, x, &gsw->style, gsw->param);
		else
			gsw->fn(gh, TRUE, y, &gsw->style, gsw->param);
	
		#undef gbw
	}
#endif

void gwinSliderDraw(GHandle gh) {
	#define gsw		((GSliderObject *)gh)
	
	if (gh->type != GW_SLIDER)
		return;

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

	if (gh->height <= gh->width)
		gsw->fn(gh, FALSE, ((gh->width-1)*(gsw->pos-gsw->min))/(gsw->max-gsw->min), &gsw->style, gsw->param);
	else
		gsw->fn(gh, TRUE, gh->height-1-((gh->height-1)*(gsw->pos-gsw->min))/(gsw->max-gsw->min), &gsw->style, gsw->param);

	#undef gbw
}

void gwinSetSliderCustom(GHandle gh, GSliderDrawFunction fn, void *param) {
	#define gsw		((GSliderObject *)gh)

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

	gsw->fn = fn ? fn : gwinSliderDraw_Std;
	gsw->param = param;

	#undef gsw
}

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

	gh->enabled = enabled;
}

void gwinSliderDraw_Std(GHandle gh, bool_t isVertical, coord_t thumbpos, const GSliderDrawStyle *pstyle, void *param) {
	(void) param;

	if (isVertical) {
		if (thumbpos != gh->height-1)
			gdispFillArea(gh->x, gh->y+thumbpos, gh->width, gh->height - thumbpos, pstyle->color_active);
		if (thumbpos != 0)
			gdispFillArea(gh->x, gh->y, gh->width, thumbpos, pstyle->color_inactive);
		gdispDrawBox(gh->x, gh->y, gh->width, gh->height, pstyle->color_edge);
		gdispDrawLine(gh->x, gh->y+thumbpos, gh->x+gh->width-1, gh->y+thumbpos, pstyle->color_thumb);
		if (thumbpos >= 2)
			gdispDrawLine(gh->x, gh->y+thumbpos-2, gh->x+gh->width-1, gh->y+thumbpos-2, pstyle->color_thumb);
		if (thumbpos <= gh->height-2)
			gdispDrawLine(gh->x, gh->y+thumbpos+2, gh->x+gh->width-1, gh->y+thumbpos+2, pstyle->color_thumb);
	} else {
		if (thumbpos != gh->width-1)
			gdispFillArea(gh->x+thumbpos, gh->y, gh->width-thumbpos, gh->height, pstyle->color_inactive);
		if (thumbpos != 0)
			gdispFillArea(gh->x, gh->y, thumbpos, gh->height, pstyle->color_active);
		gdispDrawBox(gh->x, gh->y, gh->width, gh->height, pstyle->color_edge);
		gdispDrawLine(gh->x+thumbpos, gh->y, gh->x+thumbpos, gh->y+gh->height-1, pstyle->color_thumb);
		if (thumbpos >= 2)
			gdispDrawLine(gh->x+thumbpos-2, gh->y, gh->x+thumbpos-2, gh->y+gh->height-1, pstyle->color_thumb);
		if (thumbpos <= gh->width-2)
			gdispDrawLine(gh->x+thumbpos+2, gh->y, gh->x+thumbpos+2, gh->y+gh->height-1, pstyle->color_thumb);
	}
}

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

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

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

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

		if (gh->type != GW_SLIDER || !(gsh = ginputGetDial(instance)))
			return FALSE;

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

#endif /* GFX_USE_GWIN && GWIN_NEED_BUTTON */
/** @} */