µGFX  2.9
version 2.9
gwin_widget.h
Go to the documentation of this file.
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.io/license.html
6  */
7 
8 /**
9  * @file src/gwin/gwin_widget.h
10  * @brief GWIN Widgets header file.
11  *
12  * @defgroup Widget Widget
13  * @ingroup Widgets
14  *
15  * @brief The basic widget implementation (base class).
16  *
17  * @details A widget is a Window that supports interacting with the user
18  * via an input device such as a mouse or toggle buttons. It is the
19  * base class for widgets such as buttons and sliders.
20  *
21  * @pre GFX_USE_GWIN and GWIN_NEED_WIDGET must be set to GFXON in your gfxconf.h
22  * @{
23  */
24 
25 #ifndef _GWIDGET_H
26 #define _GWIDGET_H
27 
28 /* This file is included within "src/gwin/gwin.h" */
29 
30 // Forward definition
31 struct GWidgetObject;
32 
33 /**
34  * @brief The GColorSet structure
35  * @{
36  */
37 typedef struct GColorSet {
38  gColor text; /**< The text color */
39  gColor edge; /**< The edge color */
40  gColor fill; /**< The fill color */
41  gColor progress; /**< The color of progress bars */
43 /** @} */
44 
45 /**
46  * @brief The GWidgetStyle structure
47  * @details A GWidgetStyle is a set of colors that together form a "style".
48  * These colors should not be confused with the GWindow foreground
49  * and background colors which are used for drawing operations.
50  * @{
51  */
52 typedef struct GWidgetStyle {
53  gColor background; /**< The window background color */
54  gColor focus; /**< The color when a widget is focused */
55  GColorSet enabled; /**< The colors when enabled */
56  GColorSet disabled; /**< The colors when disabled */
57  GColorSet pressed; /**< The colors when pressed */
59 /** @} */
60 
61 /**
62  * @brief We define a couple of GWidgetStyle's that you can use in your
63  * application. The Black style is the default style if you don't
64  * specify one.
65  * @note BlackWidgetStyle means that it is designed for a Black background.
66  * Similarly WhiteWidgetStyle is designed for a White background.
67  * @{
68  */
69 extern const GWidgetStyle BlackWidgetStyle;
70 extern const GWidgetStyle WhiteWidgetStyle;
71 /** @} */
72 
73 /**
74  * @brief Defines a custom drawing function for a widget
75  */
76 typedef void (*CustomWidgetDrawFunction)(struct GWidgetObject *gw, void *param);
77 
78 /**
79  * @brief Defines a the type of a tag on a widget
80  */
81 typedef gU16 WidgetTag;
82 
83 /**
84  * @brief The structure to initialise a widget.
85  *
86  * @note Some widgets may have extra parameters.
87  * @note If you create this structure on the stack, you should always memset
88  * it to all zero's first in case a future version of the software
89  * add's extra fields. Alternatively you can use @p gwinWidgetClearInit()
90  * to clear it.
91  * @note The text element must be static string (not stack allocated). If you want to use
92  * a dynamic string (eg a stack allocated string) use NULL for this member and then call
93  * @p gwinSetText() with useAlloc set to gTrue.
94  *
95  * @{
96  */
97 typedef struct GWidgetInit {
98  GWindowInit g; /**< The GWIN initializer */
99  const char * text; /**< The initial text */
100  CustomWidgetDrawFunction customDraw; /**< A custom draw function - use NULL for the standard */
101  void * customParam; /**< A parameter for the custom draw function (default = NULL) */
102  const GWidgetStyle * customStyle; /**< A custom style to use - use NULL for the default style */
103  #if GWIN_WIDGET_TAGS || defined(__DOXYGEN__)
104  WidgetTag tag; /**< The tag to associate with the widget */
105  #endif
107 /** @} */
108 
109 /**
110  * @brief The GWIN Widget structure
111  * @note A widget is a GWIN window that accepts user input.
112  * It also has a number of other properties such as its ability
113  * to redraw itself (a widget maintains drawing state).
114  * @note Do not access the members directly. Treat it as a black-box and use the method functions.
115  *
116  * @{
117  */
118 typedef struct GWidgetObject {
119  GWindowObject g; /**< This is still a GWIN */
120  const char * text; /**< The widget text */
121  CustomWidgetDrawFunction fnDraw; /**< The current draw function */
122  void * fnParam; /**< A parameter for the current draw function */
123  const GWidgetStyle * pstyle; /**< The current widget style colors */
124  #if GWIN_WIDGET_TAGS || defined(__DOXYGEN__)
125  WidgetTag tag; /**< The widget tag */
126  #endif
128 /** @} */
129 
130 /**
131  * A comment/rant on the above structure:
132  * We would really like the GWindowObject member to be anonymous. While this is
133  * allowed under the C11, C99, GNU and various other standards which have been
134  * around forever - compiler support often requires special flags e.g
135  * gcc requires the -fms-extensions flag (no wonder the language and compilers have
136  * not really progressed in 30 years). As portability is a key requirement
137  * we unfortunately won't use this useful feature in case we get a compiler that
138  * won't support it even with special flags.
139  */
140 
141 /**
142  * @brief A Generic GWIN Event
143  * @note All gwin windows when sending events will either use this structure or a
144  * structure that is 100% compatible except that it may also have extra fields.
145  * @note There are currently no GEventGWin listening flags - use 0 as the flags to @p gwinAttachListener()
146  *
147  * @{
148  */
149 typedef struct GEventGWin {
150  GEventType type; /**< The type of this event */
151  GHandle gwin; /**< The gwin window handle */
152  #if GWIN_NEED_WIDGET && GWIN_WIDGET_TAGS
153  WidgetTag tag; /**< The tag (if applicable) */
154  #endif
156 /** @} */
157 
158 /**
159  * @brief The list of predefined GWIN events.
160  * @note The definition of an event type does not mean it is always sent. For example,
161  * close events are sent by Frame windows but by little else. They are normally
162  * only sent if there is a specific reason that the event should be sent.
163  * @{
164  */
165 #define GEVENT_GWIN_OPEN (GEVENT_GWIN_FIRST+0x00)
166 #define GEVENT_GWIN_CLOSE (GEVENT_GWIN_FIRST+0x01)
167 #define GEVENT_GWIN_RESIZE (GEVENT_GWIN_FIRST+0x02)
168 #define GEVENT_GWIN_CTRL_FIRST (GEVENT_GWIN_FIRST+0x40)
169 /** @} */
170 
171 /**
172  * @brief Clear a GWidgetInit structure to all zero's
173  * @note This function is provided just to prevent problems
174  * on operating systems where using memset() causes issues
175  * in the users application.
176  *
177  * @param[in] pwi The GWidgetInit structure to clear
178  *
179  * @api
180  */
182 
183 /**
184  * @brief Set the default style for widgets created hereafter.
185  *
186  * @param[in] pstyle The default style. Passing NULL uses the system compiled style.
187  * @param[in] updateAll If gTrue then all existing widgets that are using the current default style
188  * will be updated to use this new style. Widgets that have custom styles different
189  * from the default style will not be updated.
190  *
191  * @note The style must be allocated statically (not on the stack) as only the pointer is stored.
192  *
193  * @api
194  */
195 void gwinSetDefaultStyle(const GWidgetStyle *pstyle, gBool updateAll);
196 
197 /**
198  * @brief Get the current default style.
199  *
200  * @return The current default style.
201  *
202  * @api
203  */
205 
206 /**
207  * @brief Set the text of a widget.
208  *
209  * @param[in] gh The widget handle
210  * @param[in] text The text to set. This must be a constant string unless useAlloc is set.
211  * @param[in] useAlloc If gTrue the string specified will be copied into dynamically allocated memory.
212  *
213  * @note The widget is automatically redrawn
214  * @note Non-widgets will ignore this call.
215  *
216  * @api
217  */
218 void gwinSetText(GHandle gh, const char *text, gBool useAlloc);
219 
220 /**
221  * @brief Get the text of a widget.
222  * @return The widget text or NULL if it isn't a widget
223  *
224  * @param[in] gh The widget handle
225  *
226  * @api
227  */
228 const char *gwinGetText(GHandle gh);
229 
230 #if (GFX_USE_GFILE && GFILE_NEED_PRINTG && GFILE_NEED_STRINGS) || defined(__DOXYGEN__)
231  /**
232  * @brief Set the text of a widget using a printf style format.
233  * @pre GFX_USE_GFILE, GFILE_NEED_PRINTG and GFILE_NEED_STRINGS must all be GFXON
234  *
235  * @param[in] gh The widget handle
236  * @param[in] fmt The format string using a printf/g syntax. See @p vsnprintg()
237  * @param[in] ... The printg paramters.
238  *
239  * @note The widget is automatically redrawn
240  * @note Non-widgets will ignore this call.
241  * @note The memory for the text is always allocated by this function.
242  *
243  * @api
244  */
245  void gwinPrintg(GHandle gh, const char * fmt,...);
246 #endif
247 
248 /**
249  * @brief Check whether a handles is a widget handle or not
250  *
251  * @param[in] gh The handle to check.
252  *
253  * @return gTrue if the passed handle is a widget handle. gFalse otherwise.
254  *
255  * @api
256  */
258 
259 #if GWIN_WIDGET_TAGS || defined(__DOXYGEN__)
260  /**
261  * @brief Set the tag of a widget.
262  *
263  * @param[in] gh The widget handle
264  * @param[in] tag The tag to set.
265  *
266  * @note Non-widgets will ignore this call.
267  *
268  * @pre Requires GWIN_WIDGET_TAGS to be GFXON
269  *
270  * @api
271  */
272  void gwinSetTag(GHandle gh, WidgetTag tag);
273 
274  /**
275  * @brief Get the tag of a widget.
276  * @return The widget tag value (or 0 if it is not a widget)
277  *
278  * @param[in] gh The widget handle
279  *
280  * @pre Requires GWIN_WIDGET_TAGS to be GFXON
281  *
282  * @api
283  */
285 #endif
286 
287 /**
288  * @brief Set the style of a widget.
289  *
290  * @param[in] gh The widget handle
291  * @param[in] pstyle The style to set. This must be a static structure (not allocated on a transient stack).
292  * Use NULL to reset to the default style.
293  *
294  * @note The widget is automatically redrawn
295  * @note Non-widgets will ignore this call.
296  *
297  * @api
298  */
299 void gwinSetStyle(GHandle gh, const GWidgetStyle *pstyle);
300 
301 /**
302  * @brief Get the style of a widget.
303  * @return The widget style or NULL if it isn't a widget
304  *
305  * @param[in] gh The widget handle
306  *
307  * @api
308  */
310 
311 /**
312  * @brief Set the routine to perform a custom widget drawing.
313  *
314  * @param[in] gh The widget handle
315  * @param[in] fn The function to use to draw the widget
316  * @param[in] param A parameter to pass to the widget drawing function
317  *
318  * @note The widget is not automatically redrawn. Call @p gwinDraw() to redraw the widget.
319  * @note Non-widgets will ignore this call.
320  *
321  * @api
322  */
324 
325 /**
326  * @brief Attach a Listener to listen for widget events
327  * @return gTrue on success
328  *
329  * @param[in] pl The listener
330  *
331  * @api
332  */
333 gBool gwinAttachListener(GListener *pl);
334 
335 #if (GFX_USE_GINPUT && GINPUT_NEED_MOUSE) || defined(__DOXYGEN__)
336  gBool DEPRECATED("This call can now be removed. Attaching the mouse to GWIN is now automatic.") gwinAttachMouse(gU16 instance);
337 #endif
338 
339 #if (GFX_USE_GINPUT && GINPUT_NEED_TOGGLE) || defined(__DOXYGEN__)
340  /**
341  * @brief Attach a toggle to a widget
342  * @return gTrue on success
343  *
344  * @param[in] gh The widget handle
345  * @param[in] role The function the toggle will perform for the widget
346  * @param[in] instance The toggle instance
347  *
348  * @note See the documentation on the specific widget to see the possible
349  * values for the role parameter. If it is out of range, this function
350  * will return gFalse
351  *
352  * @api
353  */
354  gBool gwinAttachToggle(GHandle gh, gU16 role, gU16 instance);
355  /**
356  * @brief Detach a toggle from a widget
357  * @return gTrue on success
358  *
359  * @param[in] gh The widget handle
360  * @param[in] role The function the toggle will perform for the widget
361  *
362  * @note See the documentation on the specific widget to see the possible
363  * values for the role parameter. If it is out of range, this function
364  * will return gFalse
365  *
366  * @api
367  */
368  gBool gwinDetachToggle(GHandle gh, gU16 role);
369 #endif
370 
371 #if (GFX_USE_GINPUT && GINPUT_NEED_DIAL) || defined(__DOXYGEN__)
372  /**
373  * @brief Attach a toggle to a widget
374  * @return gTrue on success
375  *
376  * @param[in] gh The widget handle
377  * @param[in] role The function the dial will perform for the widget
378  * @param[in] instance The dial instance
379  *
380  * @note See the documentation on the specific widget to see the possible
381  * values for the role parameter. If it is out of range, this function
382  * will return gFalse
383  *
384  * @api
385  */
386  gBool gwinAttachDial(GHandle gh, gU16 role, gU16 instance);
387 #endif
388 
389 #if (GFX_USE_GINPUT && GINPUT_NEED_KEYBOARD) || GWIN_NEED_KEYBOARD || defined(__DOXYGEN__)
390  /**
391  * @brief Set the keyboard focus to a specific window
392  * @return Returns gTrue if the focus could be set to that window
393  *
394  * @param[in] gh The window
395  *
396  * @note Passing NULL will remove the focus from any window.
397  * @note Only visible enabled widgets are capable of getting the focus.
398  *
399  * @api
400  */
402 
403  /**
404  * @brief Get the widget that is currently in focus
405  *
406  * @details The widget that is currently in focus is the widget that
407  * receives mouse and keyboard events.
408  *
409  * @return The handle of the widget that is currently in focus. May be NULL.
410  *
411  * @api
412  */
414 #else
415  #define gwinGetFocus() (0)
416  #define gwinSetFocus(gh) (gFalse)
417 #endif
418 
419 /* Include extra widget types */
420 #if GWIN_NEED_BUTTON || defined(__DOXYGEN__)
421  #include "gwin_button.h"
422 #endif
423 
424 #if GWIN_NEED_SLIDER || defined(__DOXYGEN__)
425  #include "gwin_slider.h"
426 #endif
427 
428 #if GWIN_NEED_CHECKBOX || defined(__DOXYGEN__)
429  #include "gwin_checkbox.h"
430 #endif
431 
432 #if GWIN_NEED_RADIO || defined(__DOXYGEN__)
433  #include "gwin_radio.h"
434 #endif
435 
436 #if GWIN_NEED_LABEL || defined(__DOXYGEN__)
437  #include "gwin_label.h"
438 #endif
439 
440 #if GWIN_NEED_LIST || defined(__DOXYGEN__)
441  #include "gwin_list.h"
442 #endif
443 
444 #if GWIN_NEED_PROGRESSBAR || defined(__DOXYGEN__)
445  #include "gwin_progressbar.h"
446 #endif
447 
448 #if GWIN_NEED_KEYBOARD || defined(__DOXYGEN__)
449  #include "gwin_keyboard.h"
450 #endif
451 
452 #if GWIN_NEED_TEXTEDIT || defined(__DOXYGEN__)
453  #include "gwin_textedit.h"
454 #endif
455 
456 #endif /* _GWIDGET_H */
457 /** @} */
COLOR_TYPE gColor
The color type definition.
Definition: gdisp_colors.h:437
#define DEPRECATED(msg)
Mark a function as deprecated.
struct GColorSet GColorSet
The GColorSet structure.
gBool gwinDetachToggle(GHandle gh, gU16 role)
Detach a toggle from a widget.
void gwinSetText(GHandle gh, const char *text, gBool useAlloc)
Set the text of a widget.
const char * gwinGetText(GHandle gh)
Get the text of a widget.
struct GEventGWin GEventGWin
A Generic GWIN Event.
void(* CustomWidgetDrawFunction)(struct GWidgetObject *gw, void *param)
Defines a custom drawing function for a widget.
Definition: gwin_widget.h:76
const GWidgetStyle * gwinGetStyle(GHandle gh)
Get the style of a widget.
gBool gwinAttachToggle(GHandle gh, gU16 role, gU16 instance)
Attach a toggle to a widget.
gBool gwinAttachListener(GListener *pl)
Attach a Listener to listen for widget events.
void gwinSetCustomDraw(GHandle gh, CustomWidgetDrawFunction fn, void *param)
Set the routine to perform a custom widget drawing.
void gwinPrintg(GHandle gh, const char *fmt,...)
Set the text of a widget using a printf style format.
void gwinSetStyle(GHandle gh, const GWidgetStyle *pstyle)
Set the style of a widget.
const GWidgetStyle * gwinGetDefaultStyle(void)
Get the current default style.
struct GWidgetObject GWidgetObject
The GWIN Widget structure.
struct GWidgetStyle GWidgetStyle
The GWidgetStyle structure.
gBool gwinIsWidget(GHandle gh)
Check whether a handles is a widget handle or not.
GHandle gwinGetFocus(void)
Get the widget that is currently in focus.
struct GWidgetInit GWidgetInit
The structure to initialise a widget.
gU16 WidgetTag
Defines a the type of a tag on a widget.
Definition: gwin_widget.h:81
gBool gwinAttachDial(GHandle gh, gU16 role, gU16 instance)
Attach a toggle to a widget.
void gwinWidgetClearInit(GWidgetInit *pwi)
Clear a GWidgetInit structure to all zero's.
gBool gwinSetFocus(GHandle gh)
Set the keyboard focus to a specific window.
void gwinSetDefaultStyle(const GWidgetStyle *pstyle, gBool updateAll)
Set the default style for widgets created hereafter.
const GWidgetStyle BlackWidgetStyle
We define a couple of GWidgetStyle's that you can use in your application. The Black style is the def...
void gwinSetTag(GHandle gh, WidgetTag tag)
Set the tag of a widget.
WidgetTag gwinGetTag(GHandle gh)
Get the tag of a widget.
GWIN Graphic window subsystem header file.
GWIN Graphic window subsystem header file.
GWIN Graphic window subsystem header file.
GWIN label widget header file.
GWIN list widget header file.
GWIN Graphic window subsystem header file.
GWIN Graphic window subsystem header file.
GWIN Graphic window subsystem header file.
GWIN textedit widget header file.
The GColorSet structure.
Definition: gwin_widget.h:37
gColor fill
Definition: gwin_widget.h:40
gColor text
Definition: gwin_widget.h:38
gColor progress
Definition: gwin_widget.h:41
gColor edge
Definition: gwin_widget.h:39
A Generic GWIN Event.
Definition: gwin_widget.h:149
GEventType type
Definition: gwin_widget.h:150
GHandle gwin
Definition: gwin_widget.h:151
The structure to initialise a widget.
Definition: gwin_widget.h:97
GWindowInit g
Definition: gwin_widget.h:98
WidgetTag tag
Definition: gwin_widget.h:104
const char * text
Definition: gwin_widget.h:99
CustomWidgetDrawFunction customDraw
Definition: gwin_widget.h:100
void * customParam
Definition: gwin_widget.h:101
const GWidgetStyle * customStyle
Definition: gwin_widget.h:102
The GWIN Widget structure.
Definition: gwin_widget.h:118
GWindowObject g
Definition: gwin_widget.h:119
CustomWidgetDrawFunction fnDraw
Definition: gwin_widget.h:121
void * fnParam
Definition: gwin_widget.h:122
const GWidgetStyle * pstyle
Definition: gwin_widget.h:123
WidgetTag tag
Definition: gwin_widget.h:125
const char * text
Definition: gwin_widget.h:120
The GWidgetStyle structure.
Definition: gwin_widget.h:52
gColor background
Definition: gwin_widget.h:53
GColorSet disabled
Definition: gwin_widget.h:56
GColorSet enabled
Definition: gwin_widget.h:55
gColor focus
Definition: gwin_widget.h:54
GColorSet pressed
Definition: gwin_widget.h:57
The structure to initialise a GWIN.
Definition: gwin.h:75
A window object structure.
Definition: gwin.h:40