Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_objx/lv_slider.c
3694 views
1
/*
2
* Copyright (c) 2026 CTCaer
3
*
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms and conditions of the GNU General Public License,
6
* version 2, as published by the Free Software Foundation.
7
*
8
* This program is distributed in the hope it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11
* more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15
*/
16
17
/**
18
* @file lv_slider.c
19
*
20
*/
21
22
/*********************
23
* INCLUDES
24
*********************/
25
#include "lv_slider.h"
26
#if USE_LV_SLIDER != 0
27
28
#include "../lv_core/lv_group.h"
29
#include "../lv_draw/lv_draw.h"
30
#include "../lv_themes/lv_theme.h"
31
#include "../lv_misc/lv_math.h"
32
33
/*********************
34
* DEFINES
35
*********************/
36
#define LV_SLIDER_SIZE_MIN 4 /*hor. pad and ver. pad cannot make the bar or indicator smaller then this [px]*/
37
#define LV_SLIDER_NOT_PRESSED INT16_MIN
38
39
/**********************
40
* TYPEDEFS
41
**********************/
42
43
/**********************
44
* STATIC PROTOTYPES
45
**********************/
46
static bool lv_slider_design(lv_obj_t * slider, const lv_area_t * mask, lv_design_mode_t mode);
47
static lv_res_t lv_slider_signal(lv_obj_t * slider, lv_signal_t sign, void * param);
48
49
/**********************
50
* STATIC VARIABLES
51
**********************/
52
static lv_design_func_t ancestor_design_f;
53
static lv_signal_func_t ancestor_signal;
54
55
/**********************
56
* MACROS
57
**********************/
58
59
/**********************
60
* GLOBAL FUNCTIONS
61
**********************/
62
63
/**
64
* Create a slider objects
65
* @param par pointer to an object, it will be the parent of the new slider
66
* @param copy pointer to a slider object, if not NULL then the new object will be copied from it
67
* @return pointer to the created slider
68
*/
69
lv_obj_t * lv_slider_create(lv_obj_t * par, const lv_obj_t * copy)
70
{
71
LV_LOG_TRACE("slider create started");
72
73
/*Create the ancestor slider*/
74
lv_obj_t * new_slider = lv_bar_create(par, copy);
75
lv_mem_assert(new_slider);
76
if(new_slider == NULL) return NULL;
77
78
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_slider);
79
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_slider);
80
81
/*Allocate the slider type specific extended data*/
82
lv_slider_ext_t * ext = lv_obj_allocate_ext_attr(new_slider, sizeof(lv_slider_ext_t));
83
lv_mem_assert(ext);
84
if(ext == NULL) return NULL;
85
86
/*Initialize the allocated 'ext' */
87
ext->action = NULL;
88
ext->drag_value = LV_SLIDER_NOT_PRESSED;
89
ext->style_knob = &lv_style_pretty;
90
ext->knob_in = 0;
91
92
/*The signal and design functions are not copied so set them here*/
93
lv_obj_set_signal_func(new_slider, lv_slider_signal);
94
lv_obj_set_design_func(new_slider, lv_slider_design);
95
96
/*Init the new slider slider*/
97
if(copy == NULL) {
98
lv_obj_set_click(new_slider, true);
99
lv_obj_set_protect(new_slider, LV_PROTECT_PRESS_LOST);
100
101
/*Set the default styles*/
102
lv_theme_t * th = lv_theme_get_current();
103
if(th) {
104
lv_slider_set_style(new_slider, LV_SLIDER_STYLE_BG, th->slider.bg);
105
lv_slider_set_style(new_slider, LV_SLIDER_STYLE_INDIC, th->slider.indic);
106
lv_slider_set_style(new_slider, LV_SLIDER_STYLE_KNOB, th->slider.knob);
107
} else {
108
lv_slider_set_style(new_slider, LV_SLIDER_STYLE_KNOB, ext->style_knob);
109
}
110
}
111
/*Copy an existing slider*/
112
else {
113
lv_slider_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
114
ext->style_knob = copy_ext->style_knob;
115
ext->action = copy_ext->action;
116
ext->knob_in = copy_ext->knob_in;
117
/*Refresh the style with new signal function*/
118
lv_obj_refresh_style(new_slider);
119
}
120
121
122
LV_LOG_INFO("slider created");
123
124
125
return new_slider;
126
}
127
128
/*=====================
129
* Setter functions
130
*====================*/
131
132
/**
133
* Set a function which will be called when a new value is set on the slider
134
* @param slider pointer to slider object
135
* @param action a callback function
136
*/
137
void lv_slider_set_action(lv_obj_t * slider, lv_action_t action)
138
{
139
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
140
ext->action = action;
141
}
142
143
/**
144
* Set the 'knob in' attribute of a slider
145
* @param slider pointer to slider object
146
* @param in true: the knob is drawn always in the slider;
147
* false: the knob can be out on the edges
148
*/
149
void lv_slider_set_knob_in(lv_obj_t * slider, bool in)
150
{
151
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
152
if(ext->knob_in == in) return;
153
154
ext->knob_in = in == false ? 0 : 1;
155
lv_obj_invalidate(slider);
156
}
157
158
/**
159
* Set a style of a slider
160
* @param slider pointer to a slider object
161
* @param type which style should be set
162
* @param style pointer to a style
163
*/
164
void lv_slider_set_style(lv_obj_t * slider, lv_slider_style_t type, lv_style_t * style)
165
{
166
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
167
168
switch(type) {
169
case LV_SLIDER_STYLE_BG:
170
lv_bar_set_style(slider, LV_BAR_STYLE_BG, style);
171
break;
172
case LV_SLIDER_STYLE_INDIC:
173
lv_bar_set_style(slider, LV_BAR_STYLE_INDIC, style);
174
break;
175
case LV_SLIDER_STYLE_KNOB:
176
ext->style_knob = style;
177
lv_obj_refresh_ext_size(slider);
178
break;
179
}
180
}
181
182
/*=====================
183
* Getter functions
184
*====================*/
185
186
/**
187
* Get the value of a slider
188
* @param slider pointer to a slider object
189
* @return the value of the slider
190
*/
191
int16_t lv_slider_get_value(const lv_obj_t * slider)
192
{
193
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
194
195
if(ext->drag_value != LV_SLIDER_NOT_PRESSED) return ext->drag_value;
196
else return lv_bar_get_value(slider);
197
}
198
199
/**
200
* Get the slider action function
201
* @param slider pointer to slider object
202
* @return the callback function
203
*/
204
lv_action_t lv_slider_get_action(const lv_obj_t * slider)
205
{
206
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
207
return ext->action;
208
}
209
210
/**
211
* Give the slider is being dragged or not
212
* @param slider pointer to a slider object
213
* @return true: drag in progress false: not dragged
214
*/
215
bool lv_slider_is_dragged(const lv_obj_t * slider)
216
{
217
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
218
return ext->drag_value == LV_SLIDER_NOT_PRESSED ? false : true;
219
}
220
221
/**
222
* Get the 'knob in' attribute of a slider
223
* @param slider pointer to slider object
224
* @return true: the knob is drawn always in the slider;
225
* false: the knob can be out on the edges
226
*/
227
bool lv_slider_get_knob_in(const lv_obj_t * slider)
228
{
229
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
230
return ext->knob_in == 0 ? false : true;
231
}
232
233
/**
234
* Get a style of a slider
235
* @param slider pointer to a slider object
236
* @param type which style should be get
237
* @return style pointer to a style
238
*/
239
lv_style_t * lv_slider_get_style(const lv_obj_t * slider, lv_slider_style_t type)
240
{
241
lv_style_t * style = NULL;
242
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
243
244
switch(type) {
245
case LV_SLIDER_STYLE_BG:
246
style = lv_bar_get_style(slider, LV_BAR_STYLE_BG);
247
break;
248
case LV_SLIDER_STYLE_INDIC:
249
style = lv_bar_get_style(slider, LV_BAR_STYLE_INDIC);
250
break;
251
case LV_SLIDER_STYLE_KNOB:
252
style = ext->style_knob;
253
break;
254
default:
255
style = NULL;
256
break;
257
}
258
259
return style;
260
}
261
262
/**********************
263
* STATIC FUNCTIONS
264
**********************/
265
266
267
/**
268
* Handle the drawing related tasks of the sliders
269
* @param slider pointer to an object
270
* @param mask the object will be drawn only in this area
271
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
272
* (return 'true' if yes)
273
* LV_DESIGN_DRAW: draw the object (always return 'true')
274
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
275
* @param return true/false, depends on 'mode'
276
*/
277
static bool lv_slider_design(lv_obj_t * slider, const lv_area_t * mask, lv_design_mode_t mode)
278
{
279
/*Return false if the object is not covers the mask_p area*/
280
if(mode == LV_DESIGN_COVER_CHK) {
281
return false;
282
}
283
/*Draw the object*/
284
else if(mode == LV_DESIGN_DRAW_MAIN) {
285
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
286
287
lv_style_t * style_bg = lv_slider_get_style(slider, LV_SLIDER_STYLE_BG);
288
lv_style_t * style_knob = lv_slider_get_style(slider, LV_SLIDER_STYLE_KNOB);
289
lv_style_t * style_indic = lv_slider_get_style(slider, LV_SLIDER_STYLE_INDIC);
290
291
lv_opa_t opa_scale = lv_obj_get_opa_scale(slider);
292
293
lv_coord_t slider_w = lv_area_get_width(&slider->coords);
294
lv_coord_t slider_h = lv_area_get_height(&slider->coords);
295
296
/*Draw the bar*/
297
lv_area_t area_bg;
298
lv_area_copy(&area_bg, &slider->coords);
299
300
/*Be sure at least LV_SLIDER_SIZE_MIN size will remain*/
301
lv_coord_t pad_ver_bg = style_bg->body.padding.ver;
302
lv_coord_t pad_hor_bg = style_bg->body.padding.hor;
303
if(pad_ver_bg * 2 + LV_SLIDER_SIZE_MIN > lv_area_get_height(&area_bg)) {
304
pad_ver_bg = (lv_area_get_height(&area_bg) - LV_SLIDER_SIZE_MIN) >> 1;
305
}
306
if(pad_hor_bg * 2 + LV_SLIDER_SIZE_MIN > lv_area_get_width(&area_bg)) {
307
pad_hor_bg = (lv_area_get_width(&area_bg) - LV_SLIDER_SIZE_MIN) >> 1;
308
}
309
310
if(ext->knob_in) { /*Enable extra size if the knob is inside */
311
area_bg.x1 += pad_hor_bg;
312
area_bg.x2 -= pad_hor_bg;
313
area_bg.y1 += pad_hor_bg;
314
area_bg.y2 -= pad_hor_bg;
315
} else { /*Let space only in the perpendicular directions*/
316
area_bg.x1 += slider_w < slider_h ? pad_hor_bg : 0; /*Pad only for vertical slider*/
317
area_bg.x2 -= slider_w < slider_h ? pad_hor_bg : 0; /*Pad only for vertical slider*/
318
area_bg.y1 += slider_w > slider_h ? pad_ver_bg : 0; /*Pad only for horizontal slider*/
319
area_bg.y2 -= slider_w > slider_h ? pad_ver_bg : 0; /*Pad only for horizontal slider*/
320
}
321
322
323
#if USE_LV_GROUP == 0
324
lv_draw_rect(&area_bg, mask, style_bg, lv_obj_get_opa_scale(slider));
325
#else
326
/* Draw the borders later if the bar is focused.
327
* At value = 100% the indicator can cover to whole background and the focused style won't be visible*/
328
if(lv_obj_is_focused(slider)) {
329
lv_style_t style_tmp;
330
lv_style_copy(&style_tmp, style_bg);
331
style_tmp.body.border.width = 0;
332
lv_draw_rect(&area_bg, mask, &style_tmp, opa_scale);
333
} else {
334
lv_draw_rect(&area_bg, mask, style_bg, opa_scale);
335
}
336
#endif
337
338
339
/*Draw the indicator*/
340
lv_area_t area_indic;
341
lv_area_copy(&area_indic, &area_bg);
342
343
/*Be sure at least ver pad/hor pad width indicator will remain*/
344
lv_coord_t pad_ver_indic = style_indic->body.padding.ver;
345
lv_coord_t pad_hor_indic = style_indic->body.padding.hor;
346
if(pad_ver_indic * 2 + LV_SLIDER_SIZE_MIN > lv_area_get_height(&area_bg)) {
347
pad_ver_indic = (lv_area_get_height(&area_bg) - LV_SLIDER_SIZE_MIN) >> 1;
348
}
349
if(pad_hor_indic * 2 + LV_SLIDER_SIZE_MIN > lv_area_get_width(&area_bg)) {
350
pad_hor_indic = (lv_area_get_width(&area_bg) - LV_SLIDER_SIZE_MIN) >> 1;
351
}
352
353
area_indic.x1 += pad_hor_indic;
354
area_indic.x2 -= pad_hor_indic;
355
area_indic.y1 += pad_ver_indic;
356
area_indic.y2 -= pad_ver_indic;
357
358
359
lv_coord_t cur_value = lv_slider_get_value(slider);
360
lv_coord_t min_value = lv_slider_get_min_value(slider);
361
lv_coord_t max_value = lv_slider_get_max_value(slider);
362
363
/*If dragged draw to the drag position*/
364
if(ext->drag_value != LV_SLIDER_NOT_PRESSED) cur_value = ext->drag_value;
365
366
if(slider_w >= slider_h) {
367
area_indic.x2 = (int32_t)((int32_t)(lv_area_get_width(&area_indic)) * (cur_value - min_value)) / (max_value - min_value);
368
area_indic.x2 = area_indic.x1 + area_indic.x2 - 1;
369
370
} else {
371
area_indic.y1 = (int32_t)((int32_t)(lv_area_get_height(&area_indic)) * (cur_value - min_value)) / (max_value - min_value);
372
area_indic.y1 = area_indic.y2 - area_indic.y1 + 1;
373
}
374
375
if(cur_value != min_value) lv_draw_rect(&area_indic, mask, style_indic, opa_scale);
376
377
/*Before the knob add the border if required*/
378
#if USE_LV_GROUP
379
/* Draw the borders later if the bar is focused.
380
* At value = 100% the indicator can cover to whole background and the focused style won't be visible*/
381
if(lv_obj_is_focused(slider)) {
382
lv_style_t style_tmp;
383
lv_style_copy(&style_tmp, style_bg);
384
style_tmp.body.empty = 1;
385
style_tmp.body.shadow.width = 0;
386
lv_draw_rect(&area_bg, mask, &style_tmp, opa_scale);
387
}
388
#endif
389
390
/*Draw the knob*/
391
lv_area_t knob_area;
392
lv_area_copy(&knob_area, &slider->coords);
393
394
if(slider_w >= slider_h) {
395
if(ext->knob_in == 0) {
396
knob_area.x1 = area_indic.x2 - slider_h / 2;
397
knob_area.x2 = knob_area.x1 + slider_h - 1;
398
} else {
399
knob_area.x1 = (int32_t)((int32_t)(slider_w - slider_h - 1) * (cur_value - min_value)) / (max_value - min_value);
400
knob_area.x1 += slider->coords.x1;
401
knob_area.x2 = knob_area.x1 + slider_h - 1;
402
}
403
404
knob_area.y1 = slider->coords.y1;
405
knob_area.y2 = slider->coords.y2;
406
} else {
407
if(ext->knob_in == 0) {
408
knob_area.y1 = area_indic.y1 - slider_w / 2;
409
knob_area.y2 = knob_area.y1 + slider_w - 1;
410
} else {
411
knob_area.y2 = (int32_t)((int32_t)(slider_h - slider_w - 1) * (cur_value - min_value)) / (max_value - min_value);
412
knob_area.y2 = slider->coords.y2 - knob_area.y2;
413
knob_area.y1 = knob_area.y2 - slider_w - 1;
414
}
415
knob_area.x1 = slider->coords.x1;
416
knob_area.x2 = slider->coords.x2;
417
418
}
419
lv_draw_rect(&knob_area, mask, style_knob, opa_scale);
420
}
421
/*Post draw when the children are drawn*/
422
else if(mode == LV_DESIGN_DRAW_POST) {
423
424
}
425
426
return true;
427
}
428
429
/**
430
* Signal function of the slider
431
* @param slider pointer to a slider object
432
* @param sign a signal type from lv_signal_t enum
433
* @param param pointer to a signal specific variable
434
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
435
*/
436
static lv_res_t lv_slider_signal(lv_obj_t * slider, lv_signal_t sign, void * param)
437
{
438
lv_res_t res;
439
440
/* Include the ancient signal function */
441
res = ancestor_signal(slider, sign, param);
442
if(res != LV_RES_OK) return res;
443
444
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
445
lv_point_t p;
446
lv_coord_t w = lv_obj_get_width(slider);
447
lv_coord_t h = lv_obj_get_height(slider);
448
449
if(sign == LV_SIGNAL_PRESSED) {
450
ext->drag_value = lv_slider_get_value(slider);
451
} else if(sign == LV_SIGNAL_PRESSING) {
452
lv_indev_get_point(param, &p);
453
int16_t tmp = 0;
454
if(w > h) {
455
lv_coord_t knob_w = h;
456
lv_coord_t offset = (ext->knob_in == 0) ? w : w - knob_w;
457
p.x -= slider->coords.x1 + ((ext->knob_in == 0) ? 0 : knob_w / 2);
458
tmp = ((int32_t)p.x * (ext->bar.max_value - ext->bar.min_value) + offset / 2) / offset;
459
} else {
460
lv_coord_t knob_h = w;
461
lv_coord_t offset = (ext->knob_in == 0) ? h : h - knob_h;
462
p.y -= slider->coords.y1 + ((ext->knob_in == 0) ? 0 : knob_h / 2);
463
tmp = ((int32_t)p.y * (ext->bar.max_value - ext->bar.min_value) + offset / 2) / offset;
464
}
465
tmp += ext->bar.min_value;
466
467
if(tmp < ext->bar.min_value) tmp = ext->bar.min_value;
468
else if(tmp > ext->bar.max_value) tmp = ext->bar.max_value;
469
470
if(tmp != ext->drag_value) {
471
ext->drag_value = tmp;
472
lv_obj_invalidate(slider);
473
if(ext->action != NULL) res = ext->action(slider);
474
}
475
} else if(sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST) {
476
lv_slider_set_value(slider, ext->drag_value);
477
ext->drag_value = LV_SLIDER_NOT_PRESSED;
478
if(ext->action != NULL) res = ext->action(slider);
479
} else if(sign == LV_SIGNAL_CORD_CHG) {
480
/* The knob size depends on slider size.
481
* During the drawing method the ext. size is used by the knob so refresh the ext. size.*/
482
if(lv_obj_get_width(slider) != lv_area_get_width(param) ||
483
lv_obj_get_height(slider) != lv_area_get_height(param)) {
484
slider->signal_func(slider, LV_SIGNAL_REFR_EXT_SIZE, NULL);
485
}
486
} else if(sign == LV_SIGNAL_REFR_EXT_SIZE) {
487
lv_style_t * style = lv_slider_get_style(slider, LV_SLIDER_STYLE_BG);
488
lv_style_t * knob_style = lv_slider_get_style(slider, LV_SLIDER_STYLE_KNOB);
489
lv_coord_t shadow_w = knob_style->body.shadow.width;
490
if(ext->knob_in == 0) {
491
/* The smaller size is the knob diameter*/
492
lv_coord_t x = LV_MATH_MIN(w / 2 + 1 + shadow_w, h / 2 + 1 + shadow_w);
493
if(slider->ext_size < x) slider->ext_size = x;
494
} else {
495
lv_coord_t pad = LV_MATH_MIN(style->body.padding.hor, style->body.padding.ver);
496
if(pad < 0) pad = -pad;
497
if(slider->ext_size < pad) slider->ext_size = pad;
498
499
if(slider->ext_size < shadow_w) slider->ext_size = shadow_w;
500
}
501
} else if(sign == LV_SIGNAL_CONTROLL) {
502
char c = *((char *)param);
503
504
ext->drag_value = LV_SLIDER_NOT_PRESSED;
505
506
#if USE_LV_GROUP
507
lv_group_t * g = lv_obj_get_group(slider);
508
bool editing = lv_group_get_editing(g);
509
lv_hal_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
510
/*Encoders need special handling*/
511
if(indev_type == LV_INDEV_TYPE_ENCODER && c == LV_GROUP_KEY_ENTER) {
512
if(editing) lv_group_set_editing(g, false);
513
}
514
#endif
515
if(c == LV_GROUP_KEY_RIGHT || c == LV_GROUP_KEY_UP) {
516
lv_slider_set_value(slider, lv_slider_get_value(slider) + 1);
517
if(ext->action != NULL) res = ext->action(slider);
518
} else if(c == LV_GROUP_KEY_LEFT || c == LV_GROUP_KEY_DOWN) {
519
lv_slider_set_value(slider, lv_slider_get_value(slider) - 1);
520
if(ext->action != NULL) res = ext->action(slider);
521
}
522
} else if(sign == LV_SIGNAL_GET_EDITABLE) {
523
bool * editable = (bool *)param;
524
*editable = true;
525
} else if(sign == LV_SIGNAL_GET_TYPE) {
526
lv_obj_type_t * buf = param;
527
uint8_t i;
528
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
529
if(buf->type[i] == NULL) break;
530
}
531
buf->type[i] = "lv_slider";
532
}
533
534
return res;
535
}
536
#endif
537
538