The official µGFX library repository.

gwin_slider.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file, you can obtain one at:
  4. *
  5. * http://ugfx.org/license.html
  6. */
  7. /**
  8. * @file src/gwin/gwin_slider.c
  9. * @brief GWIN sub-system slider code
  10. */
  11. #include "gfx.h"
  12. #if (GFX_USE_GWIN && GWIN_NEED_SLIDER) || defined(__DOXYGEN__)
  13. #include "gwin_class.h"
  14. #define GSLIDER_FLG_EXTENDED_EVENTS (GWIN_FIRST_CONTROL_FLAG<<0)
  15. #ifndef GWIN_SLIDER_DEAD_BAND
  16. #define GWIN_SLIDER_DEAD_BAND 5
  17. #endif
  18. #ifndef GWIN_SLIDER_TOGGLE_INC
  19. #define GWIN_SLIDER_TOGGLE_INC 20 // How many toggles to go from minimum to maximum
  20. #endif
  21. // Calculate the slider position from the display position
  22. static int CalculatePosFromDPos(GSliderObject *gsw) {
  23. // Set the new position
  24. if (gsw->w.g.width < gsw->w.g.height) {
  25. if (gsw->dpos > gsw->w.g.height-GWIN_SLIDER_DEAD_BAND)
  26. return gsw->min;
  27. if (gsw->dpos < GWIN_SLIDER_DEAD_BAND)
  28. return gsw->max;
  29. return ((int)(gsw->w.g.height-1-gsw->dpos-GWIN_SLIDER_DEAD_BAND))*(gsw->max-gsw->min)/(gsw->w.g.height-2*GWIN_SLIDER_DEAD_BAND) + gsw->min;
  30. }
  31. if (gsw->dpos > gsw->w.g.width-GWIN_SLIDER_DEAD_BAND)
  32. return gsw->max;
  33. if (gsw->dpos < GWIN_SLIDER_DEAD_BAND)
  34. return gsw->min;
  35. return ((int)(gsw->dpos-GWIN_SLIDER_DEAD_BAND))*(gsw->max-gsw->min)/(gsw->w.g.width-2*GWIN_SLIDER_DEAD_BAND) + gsw->min;
  36. }
  37. // Send the slider event
  38. static void SendSliderEvent(GSliderObject *gsw, uint8_t action) {
  39. GSourceListener * psl;
  40. GEvent * pe;
  41. #define pse ((GEventGWinSlider *)pe)
  42. // Does this slider want more than just SET events?
  43. if (action != GSLIDER_EVENT_SET && !(gsw->w.g.flags & GSLIDER_FLG_EXTENDED_EVENTS))
  44. return;
  45. psl = 0;
  46. while ((psl = geventGetSourceListener(GWIDGET_SOURCE, psl))) {
  47. // Work out which action to send.
  48. // This precedence order helps provide some protection against missed events.
  49. // Saving it into srcflags works regardless of if a buffer is available.
  50. if (psl->srcflags < action)
  51. psl->srcflags = action;
  52. // Skip sending if no buffer is available
  53. if (!(pe = geventGetEventBuffer(psl)))
  54. continue;
  55. // Fill in the event
  56. pse->type = GEVENT_GWIN_SLIDER;
  57. pse->gwin = (GHandle)gsw;
  58. pse->action = psl->srcflags;
  59. #if GWIN_WIDGET_TAGS
  60. pse->tag = gsw->w.tag;
  61. #endif
  62. // If it is a cancel or set use the defined position else use the calculated position.
  63. pse->position = pse->action >= GSLIDER_EVENT_CANCEL ? gsw->pos : CalculatePosFromDPos(gsw);
  64. // Cleanup and send.
  65. psl->srcflags = 0;
  66. geventSendEvent(psl);
  67. }
  68. #undef pse
  69. }
  70. // Reset the display position back to the value predicted by the saved slider position
  71. static void ResetDisplayPos(GSliderObject *gsw) {
  72. if (gsw->w.g.width < gsw->w.g.height)
  73. gsw->dpos = gsw->w.g.height-1-((gsw->w.g.height-1)*(gsw->pos-gsw->min))/(gsw->max-gsw->min);
  74. else
  75. gsw->dpos = ((gsw->w.g.width-1)*(gsw->pos-gsw->min))/(gsw->max-gsw->min);
  76. }
  77. #if GINPUT_NEED_MOUSE
  78. // Set the display position from the mouse position
  79. static void SetDisplayPosFromMouse(GSliderObject *gsw, coord_t x, coord_t y) {
  80. if (gsw->w.g.width < gsw->w.g.height)
  81. gsw->dpos = y < 0 ? 0 : (y >= gsw->w.g.height ? gsw->w.g.height-1 : y);
  82. else
  83. gsw->dpos = x < 0 ? 0 : (x >= gsw->w.g.width ? gsw->w.g.width-1 : x);
  84. }
  85. // A mouse up event
  86. static void MouseUp(GWidgetObject *gw, coord_t x, coord_t y) {
  87. #define gsw ((GSliderObject *)gw)
  88. #if !GWIN_BUTTON_LAZY_RELEASE
  89. // Are we over the slider?
  90. if (x < 0 || x >= gsw->w.g.width || y < 0 || y >= gsw->w.g.height) {
  91. // No - restore the slider
  92. ResetDisplayPos(gsw);
  93. _gwinUpdate(&gsw->w.g);
  94. SendSliderEvent(gsw, GSLIDER_EVENT_CANCEL);
  95. return;
  96. }
  97. #endif
  98. // Set the new position
  99. SetDisplayPosFromMouse(gsw, x, y);
  100. gsw->pos = CalculatePosFromDPos(gsw);
  101. // Update the display
  102. ResetDisplayPos(gsw);
  103. _gwinUpdate(&gsw->w.g);
  104. // Generate the event
  105. SendSliderEvent(gsw, GSLIDER_EVENT_SET);
  106. #undef gsw
  107. }
  108. // A mouse down event
  109. static void MouseDown(GWidgetObject *gw, coord_t x, coord_t y) {
  110. #define gsw ((GSliderObject *)gw)
  111. // Determine the display position
  112. SetDisplayPosFromMouse(gsw, x, y);
  113. // Update the display
  114. _gwinUpdate(&gsw->w.g);
  115. // Send the event
  116. SendSliderEvent(gsw, GSLIDER_EVENT_START);
  117. #undef gsw
  118. }
  119. // A mouse move event
  120. static void MouseMove(GWidgetObject *gw, coord_t x, coord_t y) {
  121. #define gsw ((GSliderObject *)gw)
  122. // Determine the display position
  123. SetDisplayPosFromMouse(gsw, x, y);
  124. // Update the display
  125. _gwinUpdate(&gsw->w.g);
  126. // Send the event
  127. SendSliderEvent(gsw, GSLIDER_EVENT_MOVE);
  128. #undef gsw
  129. }
  130. #endif
  131. #if GINPUT_NEED_TOGGLE
  132. // A toggle on has occurred
  133. static void ToggleOn(GWidgetObject *gw, uint16_t role) {
  134. #define gsw ((GSliderObject *)gw)
  135. if (role) {
  136. gwinSliderSetPosition(&gsw->w.g, gsw->pos+(gsw->max-gsw->min)/GWIN_SLIDER_TOGGLE_INC);
  137. SendSliderEvent(gsw, GSLIDER_EVENT_SET);
  138. } else {
  139. gwinSliderSetPosition(&gsw->w.g, gsw->pos-(gsw->max-gsw->min)/GWIN_SLIDER_TOGGLE_INC);
  140. SendSliderEvent(gsw, GSLIDER_EVENT_SET);
  141. }
  142. #undef gsw
  143. }
  144. static void ToggleAssign(GWidgetObject *gw, uint16_t role, uint16_t instance) {
  145. if (role)
  146. ((GSliderObject *)gw)->t_up = instance;
  147. else
  148. ((GSliderObject *)gw)->t_dn = instance;
  149. }
  150. static uint16_t ToggleGet(GWidgetObject *gw, uint16_t role) {
  151. return role ? ((GSliderObject *)gw)->t_up : ((GSliderObject *)gw)->t_dn;
  152. }
  153. #endif
  154. #if GINPUT_NEED_DIAL
  155. // A dial move event
  156. static void DialMove(GWidgetObject *gw, uint16_t role, uint16_t value, uint16_t max) {
  157. #define gsw ((GSliderObject *)gw)
  158. (void) role;
  159. // Set the new position
  160. gsw->pos = (uint16_t)((uint32_t)value*(gsw->max-gsw->min)/max + gsw->min);
  161. ResetDisplayPos(gsw);
  162. _gwinUpdate(&gsw->w.g);
  163. // Generate the event
  164. SendSliderEvent(gsw, GSLIDER_EVENT_SET);
  165. #undef gsw
  166. }
  167. static void DialAssign(GWidgetObject *gw, uint16_t role, uint16_t instance) {
  168. (void) role;
  169. ((GSliderObject *)gw)->dial = instance;
  170. }
  171. static uint16_t DialGet(GWidgetObject *gw, uint16_t role) {
  172. (void) role;
  173. return ((GSliderObject *)gw)->dial;
  174. }
  175. #endif
  176. // The slider VMT table
  177. static const gwidgetVMT sliderVMT = {
  178. {
  179. "Slider", // The classname
  180. sizeof(GSliderObject), // The object size
  181. _gwidgetDestroy, // The destroy routine
  182. _gwidgetRedraw, // The redraw routine
  183. 0, // The after-clear routine
  184. },
  185. gwinSliderDraw_Std, // The default drawing routine
  186. #if GINPUT_NEED_MOUSE
  187. {
  188. MouseDown, // Process mouse down events
  189. MouseUp, // Process mouse up events
  190. MouseMove, // Process mouse move events
  191. },
  192. #endif
  193. #if GINPUT_NEED_TOGGLE
  194. {
  195. 2, // 1 toggle role
  196. ToggleAssign, // Assign Toggles
  197. ToggleGet, // Get Toggles
  198. 0, // Process toggle off events (NOT USED)
  199. ToggleOn, // Process toggle on events
  200. },
  201. #endif
  202. #if GINPUT_NEED_DIAL
  203. {
  204. 1, // 1 dial roles
  205. DialAssign, // Assign Dials
  206. DialGet, // Get Dials
  207. DialMove, // Process dial move events
  208. },
  209. #endif
  210. };
  211. GHandle gwinGSliderCreate(GDisplay *g, GSliderObject *gs, const GWidgetInit *pInit) {
  212. if (!(gs = (GSliderObject *)_gwidgetCreate(g, &gs->w, pInit, &sliderVMT)))
  213. return 0;
  214. #if GINPUT_NEED_TOGGLE
  215. gs->t_dn = GWIDGET_NO_INSTANCE;
  216. gs->t_up = GWIDGET_NO_INSTANCE;
  217. #endif
  218. #if GINPUT_NEED_DIAL
  219. gs->dial = GWIDGET_NO_INSTANCE;
  220. #endif
  221. gs->min = 0;
  222. gs->max = 100;
  223. gs->pos = 0;
  224. ResetDisplayPos(gs);
  225. gwinSetVisible((GHandle)gs, pInit->g.show);
  226. return (GHandle)gs;
  227. }
  228. void gwinSliderSetRange(GHandle gh, int min, int max) {
  229. #define gsw ((GSliderObject *)gh)
  230. if (gh->vmt != (gwinVMT *)&sliderVMT)
  231. return;
  232. if (min == max) // prevent divide by 0 errors.
  233. max++;
  234. gsw->min = min;
  235. gsw->max = max;
  236. gsw->pos = min;
  237. ResetDisplayPos(gsw);
  238. #undef gsw
  239. }
  240. void gwinSliderSetPosition(GHandle gh, int pos) {
  241. #define gsw ((GSliderObject *)gh)
  242. if (gh->vmt != (gwinVMT *)&sliderVMT)
  243. return;
  244. if (gsw->min <= gsw->max) {
  245. if (pos < gsw->min) gsw->pos = gsw->min;
  246. else if (pos > gsw->max) gsw->pos = gsw->max;
  247. else gsw->pos = pos;
  248. } else {
  249. if (pos > gsw->min) gsw->pos = gsw->min;
  250. else if (pos < gsw->max) gsw->pos = gsw->max;
  251. else gsw->pos = pos;
  252. }
  253. ResetDisplayPos(gsw);
  254. _gwinUpdate(gh);
  255. #undef gsw
  256. }
  257. void gwinSliderSendExtendedEvents(GHandle gh, bool_t enabled) {
  258. if (gh->vmt != (gwinVMT *)&sliderVMT)
  259. return;
  260. if (enabled)
  261. gh->flags |= GSLIDER_FLG_EXTENDED_EVENTS;
  262. else
  263. gh->flags &= ~GSLIDER_FLG_EXTENDED_EVENTS;
  264. }
  265. /*----------------------------------------------------------
  266. * Custom Draw Routines
  267. *----------------------------------------------------------*/
  268. void gwinSliderDraw_Std(GWidgetObject *gw, void *param) {
  269. #define gsw ((GSliderObject *)gw)
  270. const GColorSet * pcol;
  271. (void) param;
  272. if (gw->g.vmt != (gwinVMT *)&sliderVMT)
  273. return;
  274. if ((gw->g.flags & GWIN_FLG_SYSENABLED))
  275. pcol = &gw->pstyle->pressed;
  276. else
  277. pcol = &gw->pstyle->disabled;
  278. if (gw->g.width < gw->g.height) { // Vertical slider
  279. if (gsw->dpos != gw->g.height-1)
  280. gdispGFillArea(gw->g.display, gw->g.x, gw->g.y+gsw->dpos, gw->g.width, gw->g.height - gsw->dpos, pcol->progress); // Active Area
  281. if (gsw->dpos != 0)
  282. gdispGFillArea(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gsw->dpos, gw->pstyle->enabled.progress); // Inactive area
  283. gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, pcol->edge); // Edge
  284. gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gsw->dpos, gw->g.x+gw->g.width-1, gw->g.y+gsw->dpos, pcol->edge); // Thumb
  285. if (gsw->dpos >= 2)
  286. gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gsw->dpos-2, gw->g.x+gw->g.width-1, gw->g.y+gsw->dpos-2, pcol->edge); // Thumb
  287. if (gsw->dpos <= gw->g.height-2)
  288. gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gsw->dpos+2, gw->g.x+gw->g.width-1, gw->g.y+gsw->dpos+2, pcol->edge); // Thumb
  289. // Horizontal slider
  290. } else {
  291. if (gsw->dpos != gw->g.width-1)
  292. gdispGFillArea(gw->g.display, gw->g.x+gsw->dpos, gw->g.y, gw->g.width-gsw->dpos, gw->g.height, gw->pstyle->enabled.progress); // Inactive area
  293. if (gsw->dpos != 0)
  294. gdispGFillArea(gw->g.display, gw->g.x, gw->g.y, gsw->dpos, gw->g.height, pcol->progress); // Active Area
  295. gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, pcol->edge); // Edge
  296. gdispGDrawLine(gw->g.display, gw->g.x+gsw->dpos, gw->g.y, gw->g.x+gsw->dpos, gw->g.y+gw->g.height-1, pcol->edge); // Thumb
  297. if (gsw->dpos >= 2)
  298. gdispGDrawLine(gw->g.display, gw->g.x+gsw->dpos-2, gw->g.y, gw->g.x+gsw->dpos-2, gw->g.y+gw->g.height-1, pcol->edge); // Thumb
  299. if (gsw->dpos <= gw->g.width-2)
  300. gdispGDrawLine(gw->g.display, gw->g.x+gsw->dpos+2, gw->g.y, gw->g.x+gsw->dpos+2, gw->g.y+gw->g.height-1, pcol->edge); // Thumb
  301. }
  302. 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, justifyCenter);
  303. #undef gsw
  304. }
  305. #if GDISP_NEED_IMAGE
  306. void gwinSliderDraw_Image(GWidgetObject *gw, void *param) {
  307. #define gsw ((GSliderObject *)gw)
  308. #define gi ((gdispImage *)param)
  309. const GColorSet * pcol;
  310. coord_t z, v;
  311. if (gw->g.vmt != (gwinVMT *)&sliderVMT)
  312. return;
  313. if ((gw->g.flags & GWIN_FLG_SYSENABLED))
  314. pcol = &gw->pstyle->pressed;
  315. else
  316. pcol = &gw->pstyle->disabled;
  317. if (gw->g.width < gw->g.height) { // Vertical slider
  318. if (gsw->dpos != 0) // The unfilled area
  319. gdispGFillArea(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gsw->dpos, gw->pstyle->enabled.progress); // Inactive area
  320. if (gsw->dpos != gw->g.height-1) { // The filled area
  321. for(z=gw->g.height, v=gi->height; z > gsw->dpos;) {
  322. z -= v;
  323. if (z < gsw->dpos) {
  324. v -= gsw->dpos - z;
  325. z = gsw->dpos;
  326. }
  327. gdispGImageDraw(gw->g.display, gi, gw->g.x, gw->g.y+z, gw->g.width, v, 0, gi->height-v);
  328. }
  329. }
  330. gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, pcol->edge); // Edge
  331. gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gsw->dpos, gw->g.x+gw->g.width-1, gw->g.y+gsw->dpos, pcol->edge); // Thumb
  332. // Horizontal slider
  333. } else {
  334. if (gsw->dpos != gw->g.width-1) // The unfilled area
  335. gdispGFillArea(gw->g.display, gw->g.x+gsw->dpos, gw->g.y, gw->g.width-gsw->dpos, gw->g.height, gw->pstyle->enabled.progress); // Inactive area
  336. if (gsw->dpos != 0) { // The filled area
  337. for(z=0, v=gi->width; z < gsw->dpos; z += v) {
  338. if (z+v > gsw->dpos)
  339. v -= z+v - gsw->dpos;
  340. gdispGImageDraw(gw->g.display, gi, gw->g.x+z, gw->g.y, v, gw->g.height, 0, 0);
  341. }
  342. }
  343. gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, pcol->edge); // Edge
  344. gdispGDrawLine(gw->g.display, gw->g.x+gsw->dpos, gw->g.y, gw->g.x+gsw->dpos, gw->g.y+gw->g.height-1, pcol->edge); // Thumb
  345. }
  346. 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, justifyCenter);
  347. #undef gsw
  348. }
  349. #endif /* GDISP_NEED_IMAGE */
  350. #endif /* GFX_USE_GWIN && GWIN_NEED_BUTTON */