89d889307263e02c9475b35b79b83314f4c9a04a
[bertos.git] / menu.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
6  * All Rights Reserved.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Stefano Fedrigo <aleph@develer.com>
13  *
14  * \brief General pourpose menu handling functions
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.2  2006/02/15 09:10:51  bernie
20  *#* Make title bold; Fix height when we have no menubar.
21  *#*
22  *#* Revision 1.1  2006/02/10 12:29:36  bernie
23  *#* Add menu system.
24  *#*
25  *#* Revision 1.48  2005/11/27 23:02:55  bernie
26  *#* Move graphics modules from mware/ to gfx/.
27  *#*
28  *#* Revision 1.47  2005/11/16 18:10:19  bernie
29  *#* Move top-level headers to cfg/ as in DevLib.
30  *#*
31  *#* Revision 1.46  2005/02/17 03:49:21  bernie
32  *#* Update to new PGM api.
33  *#*
34  *#* Revision 1.45  2005/02/11 19:11:32  aleph
35  *#* Move menu_displaymsg() in new displaymsg module
36  *#*
37  *#* Revision 1.44  2005/01/21 20:05:57  aleph
38  *#* Fix build warning with debug off
39  *#*
40  *#* Revision 1.43  2005/01/13 16:56:36  aleph
41  *#* Fix progmem includes.
42  *#*
43  *#* Revision 1.42  2004/10/31 11:02:15  aleph
44  *#* Rename functions with correct codying conventions; Simplify version display
45  *#*
46  *#* Revision 1.41  2004/10/15 17:34:33  customer_pw
47  *#* Fix menuitem max length
48  *#*
49  *#* Revision 1.40  2004/10/06 12:55:08  customer_pw
50  *#* Declare unused (if !_DEBUG) menu_count()
51  *#*
52  *#* Revision 1.39  2004/10/01 14:04:59  customer_pw
53  *#* Add accessor functions for menu flags
54  *#*
55  *#* Revision 1.38  2004/09/27 12:05:46  customer_pw
56  *#* Use sel label for toggle menus and remove it
57  *#*
58  *#* Revision 1.37  2004/09/27 10:05:33  customer_pw
59  *#* Menu cosmetic fixes
60  *#*
61  *#* Revision 1.36  2004/09/14 22:18:03  bernie
62  *#* Adapt to stricter casting rules.
63  *#*
64  *#* Revision 1.35  2004/09/12 17:56:03  aleph
65  *#* Include debug.h instead of drv/kdebug.h
66  *#*/
67
68 #include "menu.h"
69
70 #if CONFIG_MENU_MENUBAR
71 #include "menubar.h"
72 #endif
73
74 #include <gfx/gfx.h>
75 #include <gfx/font.h>
76 #include <gfx/text.h>
77 #include <drv/kbd.h>
78 #include <cfg/compiler.h>
79 #include <cfg/debug.h>
80
81 #if CPU_HARVARD
82 #include <avr/pgmspace.h> /* strncpy_P() */
83 #endif
84
85 #include <string.h> /* strcpy() */
86
87 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
88 #include "msg.h"
89 #else
90 #define PTRMSG(x) ((const char *)x)
91 #endif
92
93
94 /* Temporary fake defines for ABORT stuff... */
95 #define abort_top  0
96 #define PUSH_ABORT false
97 #define POP_ABORT  do {} while(0)
98 #define DO_ABORT   do {} while(0)
99
100
101 #ifdef _DEBUG
102 /*!
103  * Count the items present in a menu.
104  */
105 static int UNUSED_FUNC menu_count(const struct Menu *menu)
106 {
107         int cnt = 0;
108         struct MenuItem *item;
109
110         for (item = menu->items; item->label; ++item)
111                 cnt++;
112
113         return cnt;
114 }
115 #endif /* _DEBUG */
116
117
118 #if CONFIG_MENU_MENUBAR
119
120 /*!
121  * Update the menu bar according to the selected item
122  * and redraw it.
123  */
124 static void menu_update_menubar(
125                 const struct Menu *menu,
126                 struct MenuBar *mb,
127                 int selected)
128 {
129         int item_flags = menu->items[selected].flags;
130         const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
131
132         if (item_flags & MIF_DISABLED)
133                 newlabel = (const_iptr_t)LABEL_EMPTY;
134         else if (item_flags & MIF_TOGGLE)
135                 newlabel = (const_iptr_t)LABEL_SEL;
136         else if (item_flags & MIF_CHECKIT)
137         {
138                 newlabel = (item_flags & MIF_CHECKED) ?
139                         (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
140         }
141
142         mb->labels[3] = newlabel;
143         mbar_draw(mb);
144 }
145 #endif /* CONFIG_MENU_MENUBAR */
146
147
148 /*!
149  * Show a menu on the LCD display.
150  */
151 static void menu_layout(
152                 const struct Menu *menu,
153                 int first_item,
154                 int items_per_page,
155                 int selected)
156 {
157         const MenuItem *item;
158         int ypos, cnt;
159         const char * PROGMEM title = PTRMSG(menu->title);
160
161         ypos = menu->startrow;
162
163         if (title)
164                 text_xprintf(menu->bitmap, ypos++, 0, STYLEF_BOLD | TEXT_FILL, title);
165
166         for (
167                 cnt = 0, item = &menu->items[first_item];
168                 cnt < items_per_page;
169                 ++cnt, ++item)
170         {
171                 /* Check for end of menu */
172                 if (!(item->label || item->hook))
173                         break;
174
175                 /* Only print visible items */
176                 if (!(item->flags & MIF_HIDDEN))
177                 {
178 #if CPU_HARVARD
179                         text_xprintf_P
180 #else
181                         text_xprintf
182 #endif
183                         (
184                                 menu->bitmap, ypos++, 0,
185                                 (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
186                                 (item->flags & MIF_RAMLABEL) ? PSTR("%s%s") : PSTR("%S%s"),
187                                 PTRMSG(item->label),
188                                 (item->flags & MIF_TOGGLE) ?
189                                         ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
190                                         : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
191                         );
192                 }
193         }
194 }
195
196
197 /*!
198  * Handle menu item selection
199  */
200 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
201 {
202         /* Exclude other items */
203         int mask, i;
204         for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
205         {
206                 if (mask & 1)
207                         menu->items[i].flags &= ~MIF_CHECKED;
208         }
209
210         if (item->flags & MIF_DISABLED)
211                 return;
212
213         /* Handle checkable items */
214         if (item->flags & MIF_TOGGLE)
215                 item->flags ^= MIF_CHECKED;
216         else if (item->flags & MIF_CHECKIT)
217                 item->flags |= MIF_CHECKED;
218
219         /* Handle items with callback hooks */
220         if (item->hook)
221         {
222                 /* Push a jmp buffer to abort the operation with the STOP key */
223                 if (!PUSH_ABORT)
224                 {
225                         item->hook(item->userdata);
226                         POP_ABORT;
227                 }
228         }
229 }
230
231
232 /*!
233  * Return the previous visible item (rolls back to the last item)
234  */
235 static int menu_next_visible_item(const struct Menu *menu, int index, int total)
236 {
237         do
238         {
239                 if (++index >= total)
240                    index = 0;
241         }
242         while (menu->items[index].flags & MIF_HIDDEN);
243
244         return index;
245 }
246
247
248 /*!
249  * Return the next visible item (rolls back to the first item)
250  */
251 static int menu_prev_visible_item(const struct Menu *menu, int index, int total)
252 {
253         do
254         {
255                 if (--index < 0)
256                         index = total - 1;
257         }
258         while (menu->items[index].flags & MIF_HIDDEN);
259
260         return index;
261 }
262
263
264 /*!
265  * Handle a menu and invoke hook functions for the selected menu items.
266  */
267 iptr_t menu_handle(const struct Menu *menu)
268 {
269         uint8_t entries, visible_entries, items_per_page;
270         uint8_t first_item, selected;
271
272 #if CONFIG_MENU_MENUBAR
273         struct MenuBar mb;
274         const_iptr_t labels[] =
275         {
276                 (const_iptr_t)LABEL_BACK,
277                 (const_iptr_t)LABEL_UPARROW,
278                 (const_iptr_t)LABEL_DOWNARROW,
279                 (const_iptr_t)0
280         };
281
282         /*
283          * Initialize menu bar
284          */
285         if (menu->flags & MF_TOPLEVEL)
286                 labels[0] = (const_iptr_t)LABEL_EMPTY;
287
288         mbar_init(&mb, menu->bitmap, labels, countof(labels));
289 #endif /* CONFIG_MENU_MENUBAR */
290
291
292         /* Compute total number of items in menu (entries) and
293          * the number of visible entries, which excludes items
294          * without a label.
295          */
296         {
297                 struct MenuItem *item;
298
299                 entries = 0;
300                 visible_entries = 0;
301
302                 for (item = menu->items; (item->label || item->hook); ++item)
303                 {
304                         ++entries;
305                         if (!(item->flags & MIF_HIDDEN))
306                                 ++visible_entries;
307                 }
308         }
309
310         items_per_page =
311                 (menu->bitmap->height / menu->bitmap->font->height)
312                 - menu->startrow
313 #if CONFIG_MENU_MENUBAR
314                 - 1 /* menu bar labels */
315 #endif
316                 - (menu->title ? 1 : 0);
317
318         /* Selected item should be a visible entry */
319         first_item = selected = menu_next_visible_item(menu, -1, entries);
320
321         for(;;)
322         {
323                 keymask_t key;
324
325                 /*
326                  * Keep selected item visible
327                  */
328                 while (selected < first_item)
329                         first_item = menu_prev_visible_item(menu, first_item, entries);
330                 while (selected >= first_item + items_per_page)
331                         first_item = menu_next_visible_item(menu, first_item, entries);
332
333                 /* Clear screen */
334                 text_clear(menu->bitmap);
335                 menu_layout(menu, first_item, items_per_page, selected);
336
337 #if CONFIG_MENU_MENUBAR
338                 menu_update_menubar(menu, &mb, selected);
339 #endif
340
341                 key = kbd_get();
342
343                 if (key & K_OK)
344                 {
345                         struct MenuItem *item = &(menu->items[selected]);
346                         menu_doselect(menu, item);
347
348                         /* Return userdata as result */
349                         if (!menu->flags & MF_STICKY)
350                                 return item->userdata;
351                 }
352                 else if (key & K_UP)
353                 {
354                         selected = menu_prev_visible_item(menu, selected, entries);
355                 }
356                 else if (key & K_DOWN)
357                 {
358                         selected = menu_next_visible_item(menu, selected, entries);
359                 }
360                 else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
361                 {
362                         return 0;
363                 }
364         }
365 }
366
367
368 /*!
369  * Set flags on a menuitem.
370  *
371  * \param menu  Menu owner of the item to change.
372  * \param idx   Index of the menu item.
373  * \param flags Bit mask of the flags to set.
374  *
375  * \return Old flags.
376  */
377 int menu_setFlags(struct Menu *menu, int idx, int flags)
378 {
379         ASSERT(idx < menu_count(menu));
380
381         int old = menu->items[idx].flags;
382         menu->items[idx].flags |= flags;
383         return old;
384 }
385
386
387 /*!
388  * Clear flags on a menuitem.
389  *
390  * \param menu  Menu owner of the item to change.
391  * \param idx   Index of the menu item.
392  * \param flags Bit mask of the flags to clear.
393  *
394  * \return Old flags.
395  */
396 int menu_clearFlags(struct Menu *menu, int idx, int flags)
397 {
398         ASSERT(idx < menu_count(menu));
399
400         int old = menu->items[idx].flags;
401         menu->items[idx].flags &= ~flags;
402         return old;
403 }