4 * Copyright 2003, 2004 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.4 2006/03/20 17:48:35 bernie
20 *#* Implement support for ROM menus.
22 *#* Revision 1.3 2006/02/20 14:34:32 bernie
23 *#* Include appconfig.h before using its definitions.
25 *#* Revision 1.2 2006/02/15 09:10:51 bernie
26 *#* Make title bold; Fix height when we have no menubar.
28 *#* Revision 1.1 2006/02/10 12:29:36 bernie
31 *#* Revision 1.48 2005/11/27 23:02:55 bernie
32 *#* Move graphics modules from mware/ to gfx/.
34 *#* Revision 1.47 2005/11/16 18:10:19 bernie
35 *#* Move top-level headers to cfg/ as in DevLib.
37 *#* Revision 1.46 2005/02/17 03:49:21 bernie
38 *#* Update to new PGM api.
40 *#* Revision 1.45 2005/02/11 19:11:32 aleph
41 *#* Move menu_displaymsg() in new displaymsg module
43 *#* Revision 1.44 2005/01/21 20:05:57 aleph
44 *#* Fix build warning with debug off
46 *#* Revision 1.43 2005/01/13 16:56:36 aleph
47 *#* Fix progmem includes.
49 *#* Revision 1.42 2004/10/31 11:02:15 aleph
50 *#* Rename functions with correct codying conventions; Simplify version display
52 *#* Revision 1.41 2004/10/15 17:34:33 customer_pw
53 *#* Fix menuitem max length
55 *#* Revision 1.40 2004/10/06 12:55:08 customer_pw
56 *#* Declare unused (if !_DEBUG) menu_count()
58 *#* Revision 1.39 2004/10/01 14:04:59 customer_pw
59 *#* Add accessor functions for menu flags
61 *#* Revision 1.38 2004/09/27 12:05:46 customer_pw
62 *#* Use sel label for toggle menus and remove it
64 *#* Revision 1.37 2004/09/27 10:05:33 customer_pw
65 *#* Menu cosmetic fixes
67 *#* Revision 1.36 2004/09/14 22:18:03 bernie
68 *#* Adapt to stricter casting rules.
70 *#* Revision 1.35 2004/09/12 17:56:03 aleph
71 *#* Include debug.h instead of drv/kdebug.h
80 #include <cfg/compiler.h>
81 #include <cfg/debug.h>
82 #include <appconfig.h>
85 #include <avr/pgmspace.h> /* strncpy_P() */
88 #include <string.h> /* strcpy() */
90 #if CONFIG_MENU_MENUBAR
94 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
97 #define PTRMSG(x) ((const char *)x)
101 /* Temporary fake defines for ABORT stuff... */
103 #define PUSH_ABORT false
104 #define POP_ABORT do {} while(0)
105 #define DO_ABORT do {} while(0)
110 * Count the items present in a menu.
112 static int menu_count(const struct Menu *menu)
116 for (cnt = 0; /*NOP*/; ++cnt)
118 const MenuItem *item = &menu->items[cnt];
121 if (menu->flags & MF_ROMITEMS)
123 memcpy_P(&ram_item, item, sizeof(ram_item));
127 if (!(item->label || item->hook))
136 #if CONFIG_MENU_MENUBAR
139 * Update the menu bar according to the selected item
142 static void menu_update_menubar(
143 const struct Menu *menu,
149 if (menu->flags & MF_ROMITEMS)
151 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
152 item_flags = pgm_read_int(&menu->items[selected].flags);
156 item_flags = menu->items[selected].flags;
158 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
160 if (item_flags & MIF_DISABLED)
161 newlabel = (const_iptr_t)LABEL_EMPTY;
162 else if (item_flags & MIF_TOGGLE)
163 newlabel = (const_iptr_t)LABEL_SEL;
164 else if (item_flags & MIF_CHECKIT)
166 newlabel = (item_flags & MIF_CHECKED) ?
167 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
170 mb->labels[3] = newlabel;
173 #endif /* CONFIG_MENU_MENUBAR */
177 * Show a menu on the LCD display.
179 static void menu_layout(
180 const struct Menu *menu,
186 const char * PROGMEM title = PTRMSG(menu->title);
188 ypos = menu->startrow;
191 text_xprintf(menu->bitmap, ypos++, 0, STYLEF_BOLD | TEXT_FILL, title);
193 for (cnt = 0; cnt < items_per_page; ++cnt)
195 const MenuItem *item = &menu->items[first_item + cnt];
198 if (menu->flags & MF_ROMITEMS)
200 memcpy_P(&ram_item, item, sizeof(ram_item));
205 /* Check for end of menu */
206 if (!(item->label || item->hook))
209 /* Only print visible items */
210 if (!(item->flags & MIF_HIDDEN))
218 menu->bitmap, ypos++, 0,
219 (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
220 (item->flags & MIF_RAMLABEL) ? PSTR("%s%S") : PSTR("%S%S"),
222 (item->flags & MIF_TOGGLE) ?
223 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
224 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
232 * Handle menu item selection
234 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
236 /* Exclude other items */
238 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
241 menu->items[i].flags &= ~MIF_CHECKED;
244 if (item->flags & MIF_DISABLED)
247 /* Handle checkable items */
248 if (item->flags & MIF_TOGGLE)
249 item->flags ^= MIF_CHECKED;
250 else if (item->flags & MIF_CHECKIT)
251 item->flags |= MIF_CHECKED;
253 /* Handle items with callback hooks */
256 /* Push a jmp buffer to abort the operation with the STOP key */
259 item->hook(item->userdata);
267 * Return the next visible item (rolls back to the first item)
269 static int menu_next_visible_item(const struct Menu *menu, int index, int total)
275 if (++index >= total)
279 if (menu->flags & MF_ROMITEMS)
281 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
282 item_flags = pgm_read_int(&menu->items[index].flags);
286 item_flags = menu->items[index].flags;
288 while (item_flags & MIF_HIDDEN);
295 * Return the previous visible item (rolls back to the last item)
297 static int menu_prev_visible_item(const struct Menu *menu, int index, int total)
307 if (menu->flags & MF_ROMITEMS)
309 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
310 item_flags = pgm_read_int(&menu->items[index].flags);
314 item_flags = menu->items[index].flags;
316 while (item_flags & MIF_HIDDEN);
323 * Handle a menu and invoke hook functions for the selected menu items.
325 iptr_t menu_handle(const struct Menu *menu)
327 uint8_t entries, visible_entries, items_per_page;
328 uint8_t first_item, selected;
330 #if CONFIG_MENU_MENUBAR
332 const_iptr_t labels[] =
334 (const_iptr_t)LABEL_BACK,
335 (const_iptr_t)LABEL_UPARROW,
336 (const_iptr_t)LABEL_DOWNARROW,
341 * Initialize menu bar
343 if (menu->flags & MF_TOPLEVEL)
344 labels[0] = (const_iptr_t)LABEL_EMPTY;
346 mbar_init(&mb, menu->bitmap, labels, countof(labels));
347 #endif /* CONFIG_MENU_MENUBAR */
351 * Compute total number of items in menu (entries) and
352 * the number of visible entries, which excludes items
355 for (entries = 0, visible_entries = 0; /*NOP*/; ++entries)
357 const MenuItem *item = &menu->items[entries];
360 if (menu->flags & MF_ROMITEMS)
362 memcpy_P(&ram_item, item, sizeof(ram_item));
367 if (!(item->flags & MIF_HIDDEN))
370 if (!(item->label || item->hook))
375 (menu->bitmap->height / menu->bitmap->font->height)
377 #if CONFIG_MENU_MENUBAR
378 - 1 /* menu bar labels */
380 - (menu->title ? 1 : 0);
382 /* Selected item should be a visible entry */
383 first_item = selected = menu_next_visible_item(menu, -1, entries);
390 * Keep selected item visible
392 while (selected < first_item)
393 first_item = menu_prev_visible_item(menu, first_item, entries);
394 while (selected >= first_item + items_per_page)
395 first_item = menu_next_visible_item(menu, first_item, entries);
398 text_clear(menu->bitmap);
399 menu_layout(menu, first_item, items_per_page, selected);
401 #if CONFIG_MENU_MENUBAR
402 menu_update_menubar(menu, &mb, selected);
409 struct MenuItem *item = &(menu->items[selected]);
412 if (menu->flags & MF_ROMITEMS)
414 memcpy_P(&ram_item, item, sizeof(ram_item));
418 menu_doselect(menu, item);
420 /* Return userdata as result */
421 if (!menu->flags & MF_STICKY)
422 return item->userdata;
426 selected = menu_prev_visible_item(menu, selected, entries);
428 else if (key & K_DOWN)
430 selected = menu_next_visible_item(menu, selected, entries);
432 else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
441 * Set flags on a menuitem.
443 * \param menu Menu owner of the item to change.
444 * \param idx Index of the menu item.
445 * \param flags Bit mask of the flags to set.
449 int menu_setFlags(struct Menu *menu, int idx, int flags)
451 ASSERT(idx < menu_count(menu));
452 ASSERT(!(menu->flags & MF_ROMITEMS));
454 int old = menu->items[idx].flags;
455 menu->items[idx].flags |= flags;
461 * Clear flags on a menuitem.
463 * \param menu Menu owner of the item to change.
464 * \param idx Index of the menu item.
465 * \param flags Bit mask of the flags to clear.
469 int menu_clearFlags(struct Menu *menu, int idx, int flags)
471 ASSERT(idx < menu_count(menu));
472 ASSERT(!(menu->flags & MF_ROMITEMS));
474 int old = menu->items[idx].flags;
475 menu->items[idx].flags &= ~flags;