4 * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
11 * \author Bernardo Innocenti <bernie@develer.com>
12 * \author Stefano Fedrigo <aleph@develer.com>
14 * \brief General pourpose menu handling functions
19 *#* Revision 1.6 2006/04/11 00:07:32 bernie
20 *#* Implemenent MF_SAVESEL flag.
22 *#* Revision 1.5 2006/03/22 09:49:51 bernie
23 *#* Simplifications from project_grl.
25 *#* Revision 1.4 2006/03/20 17:48:35 bernie
26 *#* Implement support for ROM menus.
28 *#* Revision 1.3 2006/02/20 14:34:32 bernie
29 *#* Include appconfig.h before using its definitions.
31 *#* Revision 1.2 2006/02/15 09:10:51 bernie
32 *#* Make title bold; Fix height when we have no menubar.
34 *#* Revision 1.1 2006/02/10 12:29:36 bernie
37 *#* Revision 1.48 2005/11/27 23:02:55 bernie
38 *#* Move graphics modules from mware/ to gfx/.
40 *#* Revision 1.47 2005/11/16 18:10:19 bernie
41 *#* Move top-level headers to cfg/ as in DevLib.
43 *#* Revision 1.46 2005/02/17 03:49:21 bernie
44 *#* Update to new PGM api.
46 *#* Revision 1.45 2005/02/11 19:11:32 aleph
47 *#* Move menu_displaymsg() in new displaymsg module
49 *#* Revision 1.44 2005/01/21 20:05:57 aleph
50 *#* Fix build warning with debug off
52 *#* Revision 1.43 2005/01/13 16:56:36 aleph
53 *#* Fix progmem includes.
55 *#* Revision 1.42 2004/10/31 11:02:15 aleph
56 *#* Rename functions with correct codying conventions; Simplify version display
58 *#* Revision 1.41 2004/10/15 17:34:33 customer_pw
59 *#* Fix menuitem max length
61 *#* Revision 1.40 2004/10/06 12:55:08 customer_pw
62 *#* Declare unused (if !_DEBUG) menu_count()
64 *#* Revision 1.39 2004/10/01 14:04:59 customer_pw
65 *#* Add accessor functions for menu flags
67 *#* Revision 1.38 2004/09/27 12:05:46 customer_pw
68 *#* Use sel label for toggle menus and remove it
70 *#* Revision 1.37 2004/09/27 10:05:33 customer_pw
71 *#* Menu cosmetic fixes
73 *#* Revision 1.36 2004/09/14 22:18:03 bernie
74 *#* Adapt to stricter casting rules.
76 *#* Revision 1.35 2004/09/12 17:56:03 aleph
77 *#* Include debug.h instead of drv/kdebug.h
85 #include <cfg/compiler.h>
86 #include <cfg/debug.h>
87 #include <appconfig.h>
88 #include <string.h> /* strcpy() */
91 #include <avr/pgmspace.h> /* strncpy_P() */
94 #if CONFIG_MENU_MENUBAR
98 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
101 #define PTRMSG(x) ((const char *)x)
105 /* Temporary fake defines for ABORT stuff... */
107 #define PUSH_ABORT false
108 #define POP_ABORT do {} while(0)
109 #define DO_ABORT do {} while(0)
113 * Count the items present in a menu.
115 static int menu_count(const struct Menu *menu)
119 for (cnt = 0; /*NOP*/; ++cnt)
121 const MenuItem *item = &menu->items[cnt];
124 if (menu->flags & MF_ROMITEMS)
126 memcpy_P(&ram_item, item, sizeof(ram_item));
130 if (!(item->label || item->hook))
139 * Compute total number of visible entries, which excludes items
142 static int menu_count_visible(const struct Menu *menu)
144 struct MenuItem *item;
145 int visible_entries = 0;
147 for (item = menu->items; (item->label || item->hook); ++item)
149 if (!(item->flags & MIF_HIDDEN))
153 return visible_entries;
157 #if CONFIG_MENU_MENUBAR
160 * Update the menu bar according to the selected item
163 static void menu_update_menubar(
164 const struct Menu *menu,
170 if (menu->flags & MF_ROMITEMS)
172 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
173 item_flags = pgm_read_int(&menu->items[selected].flags);
177 item_flags = menu->items[selected].flags;
179 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
181 if (item_flags & MIF_DISABLED)
182 newlabel = (const_iptr_t)LABEL_EMPTY;
183 else if (item_flags & MIF_TOGGLE)
184 newlabel = (const_iptr_t)LABEL_SEL;
185 else if (item_flags & MIF_CHECKIT)
187 newlabel = (item_flags & MIF_CHECKED) ?
188 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
191 mb->labels[3] = newlabel;
194 #endif /* CONFIG_MENU_MENUBAR */
198 * Show a menu on the display.
200 static void menu_layout(
201 const struct Menu *menu,
207 const char * PROGMEM title = PTRMSG(menu->title);
212 text_xprintf(menu->bitmap, ypos++, 0, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
214 #if CONFIG_MENU_SMOOTH
215 static coord_t yoffset = 0;
216 static int old_first_item = 0;
217 if (old_first_item != first_item)
219 if (old_first_item > first_item)
221 if (++yoffset > menu->bitmap->font->height)
229 if (--yoffset < -menu->bitmap->font->height)
235 first_item = old_first_item;
237 text_offset(menu->bitmap, 0, yoffset);
240 for (cnt = 0; cnt < items_per_page; ++cnt)
242 const MenuItem *item = &menu->items[first_item + cnt];
245 if (menu->flags & MF_ROMITEMS)
247 memcpy_P(&ram_item, item, sizeof(ram_item));
252 /* Check for end of menu */
253 if (!(item->label || item->hook))
256 /* Only print visible items */
257 if (!(item->flags & MIF_HIDDEN))
265 menu->bitmap, ypos++, 0,
266 (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
267 (item->flags & MIF_RAMLABEL) ? PSTR("%s%S") : PSTR("%S%S"),
269 (item->flags & MIF_TOGGLE) ?
270 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
271 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
279 * Handle menu item selection
281 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
283 /* Exclude other items */
285 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
288 menu->items[i].flags &= ~MIF_CHECKED;
291 if (item->flags & MIF_DISABLED)
294 /* Handle checkable items */
295 if (item->flags & MIF_TOGGLE)
296 item->flags ^= MIF_CHECKED;
297 else if (item->flags & MIF_CHECKIT)
298 item->flags |= MIF_CHECKED;
300 /* Handle items with callback hooks */
303 /* Push a jmp buffer to abort the operation with the STOP key */
306 item->hook(item->userdata);
314 * Return the next visible item (rolls back to the first item)
316 static int menu_next_visible_item(const struct Menu *menu, int index)
318 int total = menu_count(menu);
323 if (++index >= total)
327 if (menu->flags & MF_ROMITEMS)
329 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
330 item_flags = pgm_read_int(&menu->items[index].flags);
334 item_flags = menu->items[index].flags;
336 while (item_flags & MIF_HIDDEN);
343 * Return the previous visible item (rolls back to the last item)
345 static int menu_prev_visible_item(const struct Menu *menu, int index)
347 int total = menu_count(menu);
356 if (menu->flags & MF_ROMITEMS)
358 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
359 item_flags = pgm_read_int(&menu->items[index].flags);
363 item_flags = menu->items[index].flags;
365 while (item_flags & MIF_HIDDEN);
372 * Handle a menu and invoke hook functions for the selected menu items.
374 iptr_t menu_handle(const struct Menu *menu)
376 uint8_t items_per_page;
377 uint8_t first_item, selected;
379 #if CONFIG_MENU_MENUBAR
381 const_iptr_t labels[] =
383 (const_iptr_t)LABEL_BACK,
384 (const_iptr_t)LABEL_UPARROW,
385 (const_iptr_t)LABEL_DOWNARROW,
390 * Initialize menu bar
392 if (menu->flags & MF_TOPLEVEL)
393 labels[0] = (const_iptr_t)LABEL_EMPTY;
395 mbar_init(&mb, menu->bitmap, labels, countof(labels));
396 #endif /* CONFIG_MENU_MENUBAR */
400 (menu->bitmap->height / menu->bitmap->font->height)
401 #if CONFIG_MENU_MENUBAR
402 - 1 /* menu bar labels */
404 - (menu->title ? 1 : 0);
406 /* Selected item should be a visible entry */
407 first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
414 * Keep selected item visible
416 while (selected < first_item)
417 first_item = menu_prev_visible_item(menu, first_item);
418 while (selected >= first_item + items_per_page)
419 first_item = menu_next_visible_item(menu, first_item);
422 text_clear(menu->bitmap);
423 menu_layout(menu, first_item, items_per_page, selected);
425 #if CONFIG_MENU_MENUBAR
426 menu_update_menubar(menu, &mb, selected);
429 #if CONFIG_MENU_SMOOTH
437 struct MenuItem *item = &(menu->items[selected]);
440 if (menu->flags & MF_ROMITEMS)
442 memcpy_P(&ram_item, item, sizeof(ram_item));
446 menu_doselect(menu, item);
448 /* Return userdata as result */
449 if (!menu->flags & MF_STICKY)
451 /* Store currently selected item before leaving. */
452 if (menu->flags & MF_SAVESEL)
453 CONST_CAST(struct Menu *, menu)->selected = selected;
454 return item->userdata;
459 selected = menu_prev_visible_item(menu, selected);
461 else if (key & K_DOWN)
463 selected = menu_next_visible_item(menu, selected);
465 else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
467 /* Store currently selected item before leaving. */
468 if (menu->flags & MF_SAVESEL)
469 CONST_CAST(struct Menu *, menu)->selected = selected;
477 * Set flags on a menuitem.
479 * \param menu Menu owner of the item to change.
480 * \param idx Index of the menu item.
481 * \param flags Bit mask of the flags to set.
485 int menu_setFlags(struct Menu *menu, int idx, int flags)
487 ASSERT(idx < menu_count(menu));
488 ASSERT(!(menu->flags & MF_ROMITEMS));
490 int old = menu->items[idx].flags;
491 menu->items[idx].flags |= flags;
497 * Clear flags on a menuitem.
499 * \param menu Menu owner of the item to change.
500 * \param idx Index of the menu item.
501 * \param flags Bit mask of the flags to clear.
505 int menu_clearFlags(struct Menu *menu, int idx, int flags)
507 ASSERT(idx < menu_count(menu));
508 ASSERT(!(menu->flags & MF_ROMITEMS));
510 int old = menu->items[idx].flags;
511 menu->items[idx].flags &= ~flags;