GUIslice  0.16.0
Embedded GUI in C
XTogglebtn.h
Go to the documentation of this file.
1 #ifndef _GUISLICE_EX_XTOGGLEBTN_H_
2 #define _GUISLICE_EX_XTOGGLEBTN_H_
3 
4 #include "GUIslice.h"
5 
6 
7 // =======================================================================
8 // GUIslice library extension: Toggle button control
9 // - Paul Conti
10 // - Toggle button with Android and iOS like styles
11 // - Based on Calvin Hass' Checkbox control
12 // - https://www.impulseadventure.com/elec/guislice-gui.html
13 // - https://github.com/ImpulseAdventure/GUIslice
14 // =======================================================================
15 //
16 // The MIT License
17 //
18 // Copyright 2016-2020 Calvin Hass and Paul Conti
19 //
20 // Permission is hereby granted, free of charge, to any person obtaining a copy
21 // of this software and associated documentation files (the "Software"), to deal
22 // in the Software without restriction, including without limitation the rights
23 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24 // copies of the Software, and to permit persons to whom the Software is
25 // furnished to do so, subject to the following conditions:
26 //
27 // The above copyright notice and this permission notice shall be included in
28 // all copies or substantial portions of the Software.
29 //
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 // THE SOFTWARE.
37 //
38 // =======================================================================
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif // __cplusplus
44 
45 // ============================================================================
46 // Extended Element: Togglebtn
47 // - Togglebtn
48 // Acts much like a checkbox but with styles that are similar to iOS and
49 // Android slider buttons.
50 // ============================================================================
51 
52 // Define unique identifier for extended element type
53 // - Select any number above GSLC_TYPE_BASE_EXTEND
54 #define GSLC_TYPEX_TOGGLEBTN GSLC_TYPE_BASE_EXTEND + 40
55 
56 // Extended element data structures
57 // - These data structures are maintained in the gslc_tsElem
58 // structure via the pXData pointer
59 
61 typedef struct {
62  bool bOn;
63  int16_t nMyPageId;
64  gslc_tsColor colThumb;
68  bool bCircular;
71 
72 
90 gslc_tsElemRef* gslc_ElemXTogglebtnCreate(gslc_tsGui* pGui,int16_t nElemId,int16_t nPage,
91  gslc_tsXTogglebtn* pXData,gslc_tsRect rElem,
92  gslc_tsColor colThumb,gslc_tsColor colOnState,gslc_tsColor colOffState,
93  bool bCircular,bool bChecked,GSLC_CB_TOUCH cbTouch);
94 
95 
105 
115 void gslc_ElemXTogglebtnSetState(gslc_tsGui* pGui,gslc_tsElemRef* pElemRef,bool bOn);
116 
126 
137 bool gslc_ElemXTogglebtnDraw(void* pvGui,void* pvElemRef,gslc_teRedrawType eRedraw);
138 
151 bool gslc_ElemXTogglebtnTouch(void* pvGui,void* pvElemRef,gslc_teTouch eTouch,int16_t nRelX,int16_t nRelY);
152 
162 
163 // ============================================================================
164 // ------------------------------------------------------------------------
165 // Read-only element macros
166 // ------------------------------------------------------------------------
167 
168 // Macro initializers for Read-Only Elements in Flash/PROGMEM
169 //
170 
171 
192 
193 #if (GSLC_USE_PROGMEM)
194 
195 #define gslc_ElemXTogglebtnCreate_P(pGui,nElemId,nPage,nX,nY,nW,nH,colThumb_,colOnState_,colOffState_,bCircular_,bChecked_,cbTouch) \
196  static const uint8_t nFeatures##nElemId = GSLC_ELEM_FEA_VALID | \
197  GSLC_ELEM_FEA_GLOW_EN | GSLC_ELEM_FEA_CLICK_EN | GSLC_ELEM_FEA_FILL_EN; \
198  static gslc_tsXTogglebtn sTogglebtn##nElemId; \
199  sTogglebtn##nElemId.bOn = bChecked_; \
200  sTogglebtn##nElemId.nMyPageId = nPage; \
201  sTogglebtn##nElemId.colThumb = colThumb_; \
202  sTogglebtn##nElemId.colOnState = colOnState_; \
203  sTogglebtn##nElemId.colOffState = colOffState_; \
204  sTogglebtn##nElemId.bCircular = bCircular_; \
205  sTogglebtn##nElemId.pfunctUser = cbTouch; \
206  static const gslc_tsElem sElem##nElemId PROGMEM = { \
207  nElemId, \
208  nFeatures##nElemId, \
209  GSLC_TYPEX_TOGGLEBTN, \
210  (gslc_tsRect){nX,nY,nW,nH}, \
211  0, \
212  GSLC_COL_GRAY,GSLC_COL_BLACK,GSLC_COL_WHITE,GSLC_COL_BLACK, \
213  (gslc_tsImgRef){NULL,NULL,GSLC_IMGREF_NONE,NULL}, \
214  (gslc_tsImgRef){NULL,NULL,GSLC_IMGREF_NONE,NULL}, \
215  NULL, \
216  NULL, \
217  0, \
218  (gslc_teTxtFlags)(GSLC_TXT_DEFAULT), \
219  GSLC_COL_WHITE, \
220  GSLC_COL_WHITE, \
221  GSLC_ALIGN_MID_MID, \
222  0, \
223  0, \
224  NULL, \
225  (void*)(&sTogglebtn##nElemId), \
226  NULL, \
227  &gslc_ElemXTogglebtnDraw, \
228  &gslc_ElemXTogglebtnTouch, \
229  NULL, \
230  }; \
231  gslc_ElemAdd(pGui,nPage,(gslc_tsElem*)&sElem##nElemId, \
232  (gslc_teElemRefFlags)(GSLC_ELEMREF_SRC_PROG | GSLC_ELEMREF_VISIBLE | GSLC_ELEMREF_REDRAW_FULL));
233 
234 
235 #else
236 
237 #define gslc_ElemXTogglebtnCreate_P(pGui,nElemId,nPage,nX,nY,nW,nH,colThumb_,colOnState_,colOffState_,bCircular_,bChecked_,cbTouch) \
238  static const uint8_t nFeatures##nElemId = GSLC_ELEM_FEA_VALID | \
239  GSLC_ELEM_FEA_GLOW_EN | GSLC_ELEM_FEA_CLICK_EN | GSLC_ELEM_FEA_FILL_EN; \
240  static gslc_tsXTogglebtn sTogglebtn##nElemId; \
241  sTogglebtn##nElemId.bOn = bChecked_; \
242  sTogglebtn##nElemId.nMyPageId = nPage; \
243  sTogglebtn##nElemId.colThumb = colThumb_; \
244  sTogglebtn##nElemId.colOnState = colOnState_; \
245  sTogglebtn##nElemId.colOffState = colOffState_; \
246  sTogglebtn##nElemId.bCircular = bCircular_; \
247  sTogglebtn##nElemId.pfunctUser = cbTouch; \
248  static const gslc_tsElem sElem##nElemId = { \
249  nElemId, \
250  nFeatures##nElemId, \
251  GSLC_TYPEX_TOGGLEBTN, \
252  (gslc_tsRect){nX,nY,nW,nH}, \
253  0, \
254  GSLC_COL_GRAY,GSLC_COL_BLACK,GSLC_COL_WHITE,GSLC_COL_BLACK, \
255  (gslc_tsImgRef){NULL,NULL,GSLC_IMGREF_NONE,NULL}, \
256  (gslc_tsImgRef){NULL,NULL,GSLC_IMGREF_NONE,NULL}, \
257  NULL, \
258  NULL, \
259  0, \
260  (gslc_teTxtFlags)(GSLC_TXT_DEFAULT), \
261  GSLC_COL_WHITE, \
262  GSLC_COL_WHITE, \
263  GSLC_ALIGN_MID_MID, \
264  0, \
265  0, \
266  NULL, \
267  (void*)(&sTogglebtn##nElemId), \
268  NULL, \
269  &gslc_ElemXTogglebtnDraw, \
270  &gslc_ElemXTogglebtnTouch, \
271  NULL, \
272  }; \
273  gslc_ElemAdd(pGui,nPage,(gslc_tsElem*)&sElem##nElemId, \
274  (gslc_teElemRefFlags)(GSLC_ELEMREF_SRC_PROG | GSLC_ELEMREF_VISIBLE | GSLC_ELEMREF_REDRAW_FULL));
275 
276 #endif
277 
278 
279 #ifdef __cplusplus
280 }
281 #endif // __cplusplus
282 #endif // _GUISLICE_EX_XTOGGLEBTN_H_
bool gslc_ElemXTogglebtnDraw(void *pvGui, void *pvElemRef, gslc_teRedrawType eRedraw)
Draw a Togglebtn element on the screen.
Definition: XTogglebtn.c:332
bool bOn
Indicates if button is ON or OFF.
Definition: XTogglebtn.h:62
gslc_tsElemRef * gslc_ElemXTogglebtnFindSelected(gslc_tsGui *pGui, int16_t nGroupId)
Find the togglebtn within a group that has been selected.
Definition: XTogglebtn.c:411
gslc_tsElemRef * gslc_ElemXTogglebtnCreate(gslc_tsGui *pGui, int16_t nElemId, int16_t nPage, gslc_tsXTogglebtn *pXData, gslc_tsRect rElem, gslc_tsColor colThumb, gslc_tsColor colOnState, gslc_tsColor colOffState, bool bCircular, bool bChecked, GSLC_CB_TOUCH cbTouch)
Create a Togglebtn button Element.
Definition: XTogglebtn.c:79
gslc_tsColor colOffState
Color of button in OFF state.
Definition: XTogglebtn.h:67
gslc_teRedrawType
Redraw types.
Definition: GUIslice.h:370
GSLC_CB_TOUCH pfunctUser
User's Callback event to say element has changed.
Definition: XTogglebtn.h:69
Extended data for Togglebtn element.
Definition: XTogglebtn.h:61
Rectangular region. Defines X,Y corner coordinates plus dimensions.
Definition: GUIslice.h:519
bool gslc_ElemXTogglebtnTouch(void *pvGui, void *pvElemRef, gslc_teTouch eTouch, int16_t nRelX, int16_t nRelY)
Handle touch events to Togglebtn element.
Definition: XTogglebtn.c:371
bool(* GSLC_CB_TOUCH)(void *pvGui, void *pvElemRef, gslc_teTouch eTouch, int16_t nX, int16_t nY)
Callback function for element touch tracking.
Definition: GUIslice.h:502
int16_t nMyPageId
We need to track our page in case of grouping elements on a non current layer, like base layer...
Definition: XTogglebtn.h:63
Element reference structure.
Definition: GUIslice.h:578
bool bCircular
Style of the toggle button circular or rectangular.
Definition: XTogglebtn.h:68
void gslc_ElemXTogglebtnSetState(gslc_tsGui *pGui, gslc_tsElemRef *pElemRef, bool bOn)
Set a Togglebtn element's current state.
Definition: XTogglebtn.c:162
gslc_teTouch
Processed event from input raw events and actions.
Definition: GUIslice.h:305
bool gslc_ElemXTogglebtnGetState(gslc_tsGui *pGui, gslc_tsElemRef *pElemRef)
Get a Togglebtn element's current state.
Definition: XTogglebtn.c:132
void gslc_ElemXTogglebtnToggleState(gslc_tsGui *pGui, gslc_tsElemRef *pElemRef)
Toggle a Togglebtn element's current state.
Definition: XTogglebtn.c:241
gslc_tsColor colOnState
Color of button in ON state.
Definition: XTogglebtn.h:66
GUI structure.
Definition: GUIslice.h:716
Color structure. Defines RGB triplet.
Definition: GUIslice.h:534