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