µGFX  2.9
version 2.9
gwin_list.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_list.h
10  * @brief GWIN list widget header file
11  *
12  * @defgroup List List
13  * @ingroup Widgets
14  *
15  * @brief List Widget. Used to display lists of items.
16  *
17  * @details Provides advanced features such as multi-selection, smooth scrolling and item icons.
18  *
19  * @pre GFX_USE_GDISP must be set to GFXON in your gfxconf.h
20  * @pre GFX_USE_GWIN must be set to GFXON in your gfxconf.h
21  * @pre GDISP_NEED_TEXT must be set to GFXON in your gfxconf.h
22  * @pre GWIN_NEED_LIST must be set to GFXON in your gfxconf.h
23  * @pre The font you want to use must be enabled in your gfxconf.h
24  *
25  * @{
26  */
27 
28 #ifndef _GWIN_LIST_H
29 #define _GWIN_LIST_H
30 
31 // This file is included within "src/gwin/gwin_widget.h"
32 
33 /**
34  * @brief The event type for a list event
35  */
36 #define GEVENT_GWIN_LIST (GEVENT_GWIN_CTRL_FIRST+4)
37 
38 /**
39  * @brief A list event
40  */
41 typedef struct GEventGWinList {
42  GEventType type; // The type of this event (GEVENT_GWIN_LIST)
43  GHandle gwin; // The list
44  #if GWIN_WIDGET_TAGS
45  WidgetTag tag; // The list tag
46  #endif
47  int item; // The item that has been selected (or unselected in a multi-select listbox)
49 
50 // A list window
51 typedef struct GListObject {
52  GWidgetObject w;
53 
54  #if GINPUT_NEED_MOUSE
55  gCoord start_mouse_x;
56  gCoord start_mouse_y;
57  gCoord last_mouse_y;
58  #endif
59  #if GINPUT_NEED_TOGGLE
60  gU16 t_up;
61  gU16 t_dn;
62  #endif
63 
64  int cnt; // Number of items currently in the list (quicker than counting each time)
65  int top; // Viewing offset in pixels from the top of the list
66  gfxQueueASync list_head; // The list of items
67 } GListObject;
68 
69 /**
70  * @brief Enum to change the behaviour of the scroll bar
71  *
72  * @note Used with @p gwinListSetScroll()
73  * @note @p scrollAlways always show the scrollbar
74  * @note @p scrollAuto show the scrollbar when there are more items on the list then fit on the screen
75  * @note @p scrollSmooth enable touch screen smooth scrolling
76  */
77 typedef enum scroll_t { scrollAlways, scrollAuto, scrollSmooth } scroll_t;
78 
79 /**
80  * @brief The internal list object flags
81  * @note Used only for writing a custom draw routine.
82  * @{
83  */
84 #define GLIST_FLG_MULTISELECT 0x01
85 #define GLIST_FLG_HASIMAGES 0x02
86 #define GLIST_FLG_SCROLLALWAYS 0x04
87 #define GLIST_FLG_SCROLLSMOOTH 0x08
88 #define GLIST_FLG_ENABLERENDER 0x10
89 /** @} */
90 
91 /**
92  * @brief The internal list item structure
93  * @note Used only for writing a custom draw routine.
94  */
95 typedef struct ListItem {
96  gfxQueueASyncItem q_item; // This must be the first member in the struct
97 
98  gU16 flags;
99  #define GLIST_FLG_SELECTED 0x0001
100  gU16 param; // A parameter the user can specify himself
101  const char* text;
102  #if GWIN_NEED_LIST_IMAGES
103  gImage* pimg;
104  #endif
106 
107 /**
108  * @brief Create a list widget
109  *
110  * @note The drawing color and the background color get set to the current defaults. If you haven't called
111  * @p gwinSetDefaultColor() or @p gwinSetDefaultBgColor() then these are GFX_BLACK and GFX_WHITE.
112  * @note The font gets set to the current default font. If you haven't called @p gwinSetDefaultFont() then
113  * there is no default font and text drawing operations will not display anything.
114  * @note A list remembers its normal drawing state. If there is a window manager then it is automatically
115  * redrawn if the window is moved or its visibility state is changed.
116  * @note The list contains no elements after creation.
117  * @note A slider supports mouse, toggle. Note: toggle only works correctly for single-select lists.
118  * @note When assigning a toggle, only one toggle is supported per role. If you try to assign more than
119  * one toggle to a role, it will forget the previous toggle. Two roles are supported:
120  * Role 0 = toggle for down, role 1 = toggle for up
121  *
122  * @param[in] g The GDisplay to display this window on
123  * @param[in] widget The GListObject structure to initialize. If this is NULL, the structure is dynamically allocated.
124  * @param[in] pInit The initialization parameters to use
125  * @param[in] multiselect If gTrue the list is multi-select instead of single-select.
126  *
127  * @return NULL if there is no resulting drawing area, otherwise a window handle.
128  *
129  * @api
130  */
131 GHandle gwinGListCreate(GDisplay *g, GListObject *widget, GWidgetInit *pInit, gBool multiselect);
132 #define gwinListCreate(w, pInit, m) gwinGListCreate(GDISP, w, pInit, m)
133 
134 /**
135  * @brief Enable or disable the rendering of the list
136  *
137  * @details Usually the list is being re-rendered when an item is added to the list. This can cause
138  * flickering and performance issues when many items are added at once. This can be prevented
139  * by temporarely disabling the render using this function.
140  *
141  * @param[in] gh The widget handle (must be a list handle)
142  * @param[in] ena gTrue or gFalse
143  *
144  * @api
145  */
146 void gwinListEnableRender(GHandle gh, gBool ena);
147 
148 /**
149  * @brief Change the behaviour of the scroll bar
150  *
151  * @note Current possible values: @p scrollAlways, @p scrollAuto and @p scrollSmooth
152  *
153  * @param[in] gh The widget handle (must be a list handle)
154  * @param[in] flag The behaviour to be set
155  *
156  * @api
157  */
159 
160 /**
161  * @brief Add an item to the list
162  *
163  * @note The ID you get returned is not static. If items get removed from the list, the list items get
164  * reordered.
165  *
166  * @param[in] gh The widget handle (must be a list handle)
167  * @param[in] text The string which shall be displayed in the list afterwards
168  * @param[in] useAlloc If set to gTrue, the string will be dynamically allocated. A static buffer must be passed otherwise
169  *
170  * @return The current ID of the item. The ID might change if you remove items from the middle of the list
171  *
172  * @api
173  */
174 int gwinListAddItem(GHandle gh, const char* text, gBool useAlloc);
175 
176 /**
177  * @brief Set the custom parameter of an item with a given ID
178  *
179  * @param[in] gh The widget handle (must be a list handle)
180  * @param[in] item The item ID
181  * @param[in] text The text to replace the existing text
182  * @param[in] useAlloc If set to gTrue, the string will be dynamically allocated. A static buffer must be passed otherwise
183  *
184  * @api
185  */
186 void gwinListItemSetText(GHandle gh, int item, const char* text, gBool useAlloc);
187 
188 /**
189  * @brief Get the name behind an item with a given ID
190  *
191  * @param[in] gh The widget handle (must be a list handle)
192  * @param[in] item The item ID
193  *
194  * @return The string of the list item or NULL on error
195  *
196  * @api
197  */
198 const char* gwinListItemGetText(GHandle gh, int item);
199 
200 /**
201  * @brief Get the ID of an item with a given name
202  *
203  * @param[in] gh The widget handle (must be a list handle)
204  * @param[in] text The item name
205  *
206  * @return The id of the list item or -1 on error
207  *
208  * @api
209  */
210 int gwinListFindText(GHandle gh, const char* text);
211 
212 /**
213  * @brief Set the custom parameter of an item with a given ID
214  *
215  * @param[in] gh The widget handle (must be a list handle)
216  * @param[in] item The item ID
217  * @param[in] param The parameter to be set
218  *
219  * @api
220  */
221 void gwinListItemSetParam(GHandle gh, int item, gU16 param);
222 
223 /**
224  * @brief Get the custom parameter of an item with a given ID
225  *
226  * @param[in] gh The widget handle (must be a list handle)
227  * @param[in] item The item ID
228  *
229  * @return The parameter
230  *
231  * @api
232  */
233 gU16 gwinListItemGetParam(GHandle gh, int item);
234 
235 /**
236  * @brief Delete all the items of the list
237  *
238  * @param[in] gh The widget handle (must be a list handle)
239  *
240  * @api
241  */
243 
244 /**
245  * @brief Delete an item from the list
246  *
247  * @param[in] gh The widget handle (must be a list handle)
248  * @param[in] item The item ID
249  *
250  * @api
251  */
252 void gwinListItemDelete(GHandle gh, int item);
253 
254 /**
255  * @brief Get the amount of items within the list
256  *
257  * @param[in] gh The widget handle (must be a list handle)
258  *
259  * @return The amount of items in the list
260  *
261  * @api
262  */
264 
265 /**
266  * @brief Check if an item with a given ID is selected
267  *
268  * @param[in] gh The widget handle (must be a list handle)
269  * @param[in] item The item ID
270  *
271  * @return gTrue if the item is selected, gFalse otherwise
272  *
273  * @api
274  */
275 gBool gwinListItemIsSelected(GHandle gh, int item);
276 
277 /**
278  * @brief Get the ID of the selected item
279  *
280  * @param[in] gh The widget handle (must be a list handle)
281  *
282  * @return The ID of the selected list item for a single-select list.
283  *
284  * @note It always returns -1 (nothing selected) for a multi-select list.
285  * Instead use @p gwinListItemIsSelected() to get the selection status
286  * for a multi-select list.
287  *
288  * @api
289  */
291 
292 /**
293  * @brief Get the text of the selected item
294  *
295  * @param[in] gh The widget handle (must be a list handle)
296  *
297  * @return The test of the selected list item for a single-select list.
298  *
299  * @note It always returns NULL (nothing selected) for a multi-select list.
300  *
301  * @api
302  */
304 
305 /**
306  * @brief Set whether a specific item is selected or not
307  *
308  * @param[in] gh The widget handle (must be a list handle)
309  * @param[in] item The item ID
310  * @param[in] doSelect gTrue to select the item or gFalse to deselect the item
311  *
312  * @note Changing the selection using this api call will NOT send the list selection
313  * change event.
314  * @note With a single select list selecting an item with this call will deselect
315  * any existing selected item. De-selecting an item with this call will not
316  * cause a new item to be automatically selected.
317  * @note De-selecting an item that is not selected will not effect any items that
318  * are selected, even in single-select mode.
319  * @api
320  */
321 void gwinListSetSelected(GHandle gh, int item, gBool doSelect);
322 
323 /**
324  * @brief Scroll the list so the specified item is in view
325  *
326  * @param[in] gh The widget handle (must be a list handle)
327  * @param[in] item The item ID
328  *
329  * @note This will typically scroll the selected item to the top of the list
330  * unless the item is in the last page of list items.
331  *
332  * @api
333  */
334 void gwinListViewItem(GHandle gh, int item);
335 
336 #if GWIN_NEED_LIST_IMAGES || defined(__DOXYGEN__)
337  /**
338  * @brief Set the image for a list item
339  *
340  * @pre GWIN_NEED_LIST_IMAGES must be set to true in your gfxconf.h
341  *
342  * @param[in] gh The widget handle (must be a list handle)
343  * @param[in] item The item ID
344  * @param[in] pimg The image to be displayed or NULL to turn off the image for this list item.
345  *
346  * @note The image should be up to 4 x (the font height) and a width of (the font height).
347  * The 1st (top) part of the image is displayed for a selected item.
348  * The 2nd part of the image is displayed for an unselected item.
349  * The 3rd part of the image is displayed for a disabled selected item.
350  * The 4th part of the image is displayed for a disabled unselected item.
351  * If the image is less than 4 times the font height then the image use is collapsed
352  * into the available height. For example, an image that equals the font height will use
353  * the same image for all four states.
354  * @note The image is only displayed while it is open. It is up to the application to open
355  * the image.
356  * @note The same image can be used on more than one list item.
357  * @note Images are aligned with the top (not the baseline) of the list item.
358  * @note When any item in the list has an image attached, space is allocated to display
359  * the images even if the image is closed or has been removed by calling @p gwinListItemSetImage()
360  * with a NULL image or by calling @p gwinListItemDelete(). The only way to turn-off the image area
361  * for this list is to call gwinListDeleteAll().
362  *
363  */
364  void gwinListItemSetImage(GHandle gh, int item, gImage *pimg);
365 #endif
366 
367 /**
368  * @defgroup Renderings_List Renderings
369  *
370  * @brief Built-in rendering functions for the list widget.
371  *
372  * @details These function may be passed to @p gwinSetCustomDraw() to get different list drawing styles.
373  *
374  * @note In your custom list drawing function you may optionally call these
375  * standard functions and then draw your extra details on top.
376  * @note These custom drawing routines don't have to worry about setting clipping as the framework
377  * sets clipping to the object window prior to calling these routines.
378  *
379  * @{
380  */
381 
382 /**
383  * @brief The default rendering function for the list widget
384  *
385  * @param[in] gw The widget object (must be a list object)
386  * @param[in] param A parameter passed in from the user. Ignored by this function.
387  *
388  * @api
389  */
390 void gwinListDefaultDraw(GWidgetObject* gw, void* param);
391 
392 /** @} */
393 
394 #endif // _GWIN_LIST_H
395 /** @} */
396 
gI16 gCoord
The type for a coordinate or length on the screen.
Definition: gdisp.h:39
struct ListItem ListItem
The internal list item structure.
struct GEventGWinList GEventGWinList
A list event.
const char * gwinListItemGetText(GHandle gh, int item)
Get the name behind an item with a given ID.
gU16 gwinListItemGetParam(GHandle gh, int item)
Get the custom parameter of an item with a given ID.
int gwinListGetSelected(GHandle gh)
Get the ID of the selected item.
void gwinListItemSetParam(GHandle gh, int item, gU16 param)
Set the custom parameter of an item with a given ID.
const char * gwinListGetSelectedText(GHandle gh)
Get the text of the selected item.
scroll_t
Enum to change the behaviour of the scroll bar.
Definition: gwin_list.h:77
void gwinListItemSetText(GHandle gh, int item, const char *text, gBool useAlloc)
Set the custom parameter of an item with a given ID.
void gwinListViewItem(GHandle gh, int item)
Scroll the list so the specified item is in view.
void gwinListSetSelected(GHandle gh, int item, gBool doSelect)
Set whether a specific item is selected or not.
int gwinListFindText(GHandle gh, const char *text)
Get the ID of an item with a given name.
gBool gwinListItemIsSelected(GHandle gh, int item)
Check if an item with a given ID is selected.
int gwinListAddItem(GHandle gh, const char *text, gBool useAlloc)
Add an item to the list.
GHandle gwinGListCreate(GDisplay *g, GListObject *widget, GWidgetInit *pInit, gBool multiselect)
Create a list widget.
void gwinListDeleteAll(GHandle gh)
Delete all the items of the list.
void gwinListItemSetImage(GHandle gh, int item, gImage *pimg)
Set the image for a list item.
int gwinListItemCount(GHandle gh)
Get the amount of items within the list.
void gwinListSetScroll(GHandle gh, scroll_t flag)
Change the behaviour of the scroll bar.
void gwinListItemDelete(GHandle gh, int item)
Delete an item from the list.
void gwinListEnableRender(GHandle gh, gBool ena)
Enable or disable the rendering of the list.
void gwinListDefaultDraw(GWidgetObject *gw, void *param)
The default rendering function for the list widget.
gU16 WidgetTag
Defines a the type of a tag on a widget.
Definition: gwin_widget.h:81
A list event.
Definition: gwin_list.h:41
The structure to initialise a widget.
Definition: gwin_widget.h:97
The GWIN Widget structure.
Definition: gwin_widget.h:118
A window object structure.
Definition: gwin.h:40
The internal list item structure.
Definition: gwin_list.h:95
The structure for an image.
Definition: gdisp_image.h:59
A queue.
Definition: gqueue.h:54
A queue item.
Definition: gqueue.h:40