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.5 2006/03/22 09:49:51 bernie
20 *#* Simplifications from project_grl.
22 *#* Revision 1.4 2006/03/20 17:48:35 bernie
23 *#* Implement support for ROM menus.
25 *#* Revision 1.3 2006/02/20 14:34:32 bernie
26 *#* Include appconfig.h before using its definitions.
28 *#* Revision 1.2 2006/02/15 09:10:51 bernie
29 *#* Make title bold; Fix height when we have no menubar.
31 *#* Revision 1.1 2006/02/10 12:29:36 bernie
34 *#* Revision 1.48 2005/11/27 23:02:55 bernie
35 *#* Move graphics modules from mware/ to gfx/.
37 *#* Revision 1.47 2005/11/16 18:10:19 bernie
38 *#* Move top-level headers to cfg/ as in DevLib.
40 *#* Revision 1.46 2005/02/17 03:49:21 bernie
41 *#* Update to new PGM api.
43 *#* Revision 1.45 2005/02/11 19:11:32 aleph
44 *#* Move menu_displaymsg() in new displaymsg module
46 *#* Revision 1.44 2005/01/21 20:05:57 aleph
47 *#* Fix build warning with debug off
49 *#* Revision 1.43 2005/01/13 16:56:36 aleph
50 *#* Fix progmem includes.
52 *#* Revision 1.42 2004/10/31 11:02:15 aleph
53 *#* Rename functions with correct codying conventions; Simplify version display
55 *#* Revision 1.41 2004/10/15 17:34:33 customer_pw
56 *#* Fix menuitem max length
58 *#* Revision 1.40 2004/10/06 12:55:08 customer_pw
59 *#* Declare unused (if !_DEBUG) menu_count()
61 *#* Revision 1.39 2004/10/01 14:04:59 customer_pw
62 *#* Add accessor functions for menu flags
64 *#* Revision 1.38 2004/09/27 12:05:46 customer_pw
65 *#* Use sel label for toggle menus and remove it
67 *#* Revision 1.37 2004/09/27 10:05:33 customer_pw
68 *#* Menu cosmetic fixes
70 *#* Revision 1.36 2004/09/14 22:18:03 bernie
71 *#* Adapt to stricter casting rules.
73 *#* Revision 1.35 2004/09/12 17:56:03 aleph
74 *#* Include debug.h instead of drv/kdebug.h
82 #include <cfg/compiler.h>
83 #include <cfg/debug.h>
84 #include <appconfig.h>
85 #include <string.h> /* strcpy() */
88 #include <avr/pgmspace.h> /* strncpy_P() */
91 #if CONFIG_MENU_MENUBAR
95 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
98 #define PTRMSG(x) ((const char *)x)
102 /* Temporary fake defines for ABORT stuff... */
104 #define PUSH_ABORT false
105 #define POP_ABORT do {} while(0)
106 #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 * Compute total number of visible entries, which excludes items
139 static int menu_count_visible(const struct Menu *menu)
141 struct MenuItem *item;
142 int visible_entries = 0;
144 for (item = menu->items; (item->label || item->hook); ++item)
146 if (!(item->flags & MIF_HIDDEN))
150 return visible_entries;
154 #if CONFIG_MENU_MENUBAR
157 * Update the menu bar according to the selected item
160 static void menu_update_menubar(
161 const struct Menu *menu,
167 if (menu->flags & MF_ROMITEMS)
169 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
170 item_flags = pgm_read_int(&menu->items[selected].flags);
174 item_flags = menu->items[selected].flags;
176 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
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)
184 newlabel = (item_flags & MIF_CHECKED) ?
185 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
188 mb->labels[3] = newlabel;
191 #endif /* CONFIG_MENU_MENUBAR */
195 * Show a menu on the display.
197 static void menu_layout(
198 const struct Menu *menu,
204 const char * PROGMEM title = PTRMSG(menu->title);
206 ypos = menu->startrow;
209 text_xprintf(menu->bitmap, ypos++, 0, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
211 for (cnt = 0; cnt < items_per_page; ++cnt)
213 const MenuItem *item = &menu->items[first_item + cnt];
216 if (menu->flags & MF_ROMITEMS)
218 memcpy_P(&ram_item, item, sizeof(ram_item));
223 /* Check for end of menu */
224 if (!(item->label || item->hook))
227 /* Only print visible items */
228 if (!(item->flags & MIF_HIDDEN))
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"),
240 (item->flags & MIF_TOGGLE) ?
241 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
242 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
250 * Handle menu item selection
252 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
254 /* Exclude other items */
256 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
259 menu->items[i].flags &= ~MIF_CHECKED;
262 if (item->flags & MIF_DISABLED)
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;
271 /* Handle items with callback hooks */
274 /* Push a jmp buffer to abort the operation with the STOP key */
277 item->hook(item->userdata);
285 * Return the next visible item (rolls back to the first item)
287 static int menu_next_visible_item(const struct Menu *menu, int index)
289 int total = menu_count(menu);
294 if (++index >= total)
298 if (menu->flags & MF_ROMITEMS)
300 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
301 item_flags = pgm_read_int(&menu->items[index].flags);
305 item_flags = menu->items[index].flags;
307 while (item_flags & MIF_HIDDEN);
314 * Return the previous visible item (rolls back to the last item)
316 static int menu_prev_visible_item(const struct Menu *menu, int index)
318 int total = menu_count(menu);
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 * Handle a menu and invoke hook functions for the selected menu items.
345 iptr_t menu_handle(const struct Menu *menu)
347 uint8_t items_per_page;
348 uint8_t first_item, selected;
350 #if CONFIG_MENU_MENUBAR
352 const_iptr_t labels[] =
354 (const_iptr_t)LABEL_BACK,
355 (const_iptr_t)LABEL_UPARROW,
356 (const_iptr_t)LABEL_DOWNARROW,
361 * Initialize menu bar
363 if (menu->flags & MF_TOPLEVEL)
364 labels[0] = (const_iptr_t)LABEL_EMPTY;
366 mbar_init(&mb, menu->bitmap, labels, countof(labels));
367 #endif /* CONFIG_MENU_MENUBAR */
371 (menu->bitmap->height / menu->bitmap->font->height)
373 #if CONFIG_MENU_MENUBAR
374 - 1 /* menu bar labels */
376 - (menu->title ? 1 : 0);
378 /* Selected item should be a visible entry */
379 first_item = selected = menu_next_visible_item(menu, -1);
386 * Keep selected item visible
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);
394 text_clear(menu->bitmap);
395 menu_layout(menu, first_item, items_per_page, selected);
397 #if CONFIG_MENU_MENUBAR
398 menu_update_menubar(menu, &mb, selected);
405 struct MenuItem *item = &(menu->items[selected]);
408 if (menu->flags & MF_ROMITEMS)
410 memcpy_P(&ram_item, item, sizeof(ram_item));
414 menu_doselect(menu, item);
416 /* Return userdata as result */
417 if (!menu->flags & MF_STICKY)
418 return item->userdata;
422 selected = menu_prev_visible_item(menu, selected);
424 else if (key & K_DOWN)
426 selected = menu_next_visible_item(menu, selected);
428 else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
437 * Set flags on a menuitem.
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.
445 int menu_setFlags(struct Menu *menu, int idx, int flags)
447 ASSERT(idx < menu_count(menu));
448 ASSERT(!(menu->flags & MF_ROMITEMS));
450 int old = menu->items[idx].flags;
451 menu->items[idx].flags |= flags;
457 * Clear flags on a menuitem.
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.
465 int menu_clearFlags(struct Menu *menu, int idx, int flags)
467 ASSERT(idx < menu_count(menu));
468 ASSERT(!(menu->flags & MF_ROMITEMS));
470 int old = menu->items[idx].flags;
471 menu->items[idx].flags &= ~flags;