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.1 2006/02/10 12:29:36 bernie
22 *#* Revision 1.48 2005/11/27 23:02:55 bernie
23 *#* Move graphics modules from mware/ to gfx/.
25 *#* Revision 1.47 2005/11/16 18:10:19 bernie
26 *#* Move top-level headers to cfg/ as in DevLib.
28 *#* Revision 1.46 2005/02/17 03:49:21 bernie
29 *#* Update to new PGM api.
31 *#* Revision 1.45 2005/02/11 19:11:32 aleph
32 *#* Move menu_displaymsg() in new displaymsg module
34 *#* Revision 1.44 2005/01/21 20:05:57 aleph
35 *#* Fix build warning with debug off
37 *#* Revision 1.43 2005/01/13 16:56:36 aleph
38 *#* Fix progmem includes.
40 *#* Revision 1.42 2004/10/31 11:02:15 aleph
41 *#* Rename functions with correct codying conventions; Simplify version display
43 *#* Revision 1.41 2004/10/15 17:34:33 customer_pw
44 *#* Fix menuitem max length
46 *#* Revision 1.40 2004/10/06 12:55:08 customer_pw
47 *#* Declare unused (if !_DEBUG) menu_count()
49 *#* Revision 1.39 2004/10/01 14:04:59 customer_pw
50 *#* Add accessor functions for menu flags
52 *#* Revision 1.38 2004/09/27 12:05:46 customer_pw
53 *#* Use sel label for toggle menus and remove it
55 *#* Revision 1.37 2004/09/27 10:05:33 customer_pw
56 *#* Menu cosmetic fixes
58 *#* Revision 1.36 2004/09/14 22:18:03 bernie
59 *#* Adapt to stricter casting rules.
61 *#* Revision 1.35 2004/09/12 17:56:03 aleph
62 *#* Include debug.h instead of drv/kdebug.h
67 #if CONFIG_MENU_MENUBAR
75 #include <cfg/compiler.h>
76 #include <cfg/debug.h>
79 #include <avr/pgmspace.h> /* strncpy_P() */
82 #include <string.h> /* strcpy() */
84 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
87 #define PTRMSG(x) ((const char *)x)
91 /* Temporary fake defines for ABORT stuff... */
93 #define PUSH_ABORT false
94 #define POP_ABORT do {} while(0)
95 #define DO_ABORT do {} while(0)
100 * Count the items present in a menu.
102 static int UNUSED_FUNC menu_count(const struct Menu *menu)
105 struct MenuItem *item;
107 for (item = menu->items; item->label; ++item)
115 #if CONFIG_MENU_MENUBAR
118 * Update the menu bar according to the selected item
121 static void menu_update_menubar(
122 const struct Menu *menu,
126 int item_flags = menu->items[selected].flags;
127 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
129 if (item_flags & MIF_DISABLED)
130 newlabel = (const_iptr_t)LABEL_EMPTY;
131 else if (item_flags & MIF_TOGGLE)
132 newlabel = (const_iptr_t)LABEL_SEL;
133 else if (item_flags & MIF_CHECKIT)
135 newlabel = (item_flags & MIF_CHECKED) ?
136 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
139 mb->labels[3] = newlabel;
142 #endif /* CONFIG_MENU_MENUBAR */
146 * Show a menu on the LCD display.
148 static void menu_layout(
149 const struct Menu *menu,
154 const MenuItem *item;
156 const char * PROGMEM title = PTRMSG(menu->title);
158 ypos = menu->startrow;
161 text_xprintf(menu->bitmap, ypos++, 0, TEXT_FILL, title);
164 cnt = 0, item = &menu->items[first_item];
165 cnt < items_per_page;
168 /* Check for end of menu */
169 if (!(item->label || item->hook))
172 /* Only print visible items */
173 if (!(item->flags & MIF_HIDDEN))
181 menu->bitmap, ypos++, 0,
182 (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
183 (item->flags & MIF_RAMLABEL) ? PSTR("%s%s") : PSTR("%S%s"),
185 (item->flags & MIF_TOGGLE) ?
186 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
187 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
195 * Handle menu item selection
197 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
199 /* Exclude other items */
201 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
204 menu->items[i].flags &= ~MIF_CHECKED;
207 if (item->flags & MIF_DISABLED)
210 /* Handle checkable items */
211 if (item->flags & MIF_TOGGLE)
212 item->flags ^= MIF_CHECKED;
213 else if (item->flags & MIF_CHECKIT)
214 item->flags |= MIF_CHECKED;
216 /* Handle items with callback hooks */
219 /* Push a jmp buffer to abort the operation with the STOP key */
222 item->hook(item->userdata);
230 * Return the previous visible item (rolls back to the last item)
232 static int menu_next_visible_item(const struct Menu *menu, int index, int total)
236 if (++index >= total)
239 while (menu->items[index].flags & MIF_HIDDEN);
246 * Return the next visible item (rolls back to the first item)
248 static int menu_prev_visible_item(const struct Menu *menu, int index, int total)
255 while (menu->items[index].flags & MIF_HIDDEN);
262 * Handle a menu and invoke hook functions for the selected menu items.
264 iptr_t menu_handle(const struct Menu *menu)
266 uint8_t entries, visible_entries, items_per_page;
267 uint8_t first_item, selected;
269 #if CONFIG_MENU_MENUBAR
271 const_iptr_t labels[] =
273 (const_iptr_t)LABEL_BACK,
274 (const_iptr_t)LABEL_UPARROW,
275 (const_iptr_t)LABEL_DOWNARROW,
280 * Initialize menu bar
282 if (menu->flags & MF_TOPLEVEL)
283 labels[0] = (const_iptr_t)LABEL_EMPTY;
285 mbar_init(&mb, menu->bitmap, labels, countof(labels));
286 #endif /* CONFIG_MENU_MENUBAR */
289 /* Compute total number of items in menu (entries) and
290 * the number of visible entries, which excludes items
294 struct MenuItem *item;
299 for (item = menu->items; (item->label || item->hook); ++item)
302 if (!(item->flags & MIF_HIDDEN))
308 (menu->bitmap->height / menu->bitmap->font->height)
310 - 1 /* menu bar labels */
311 - (menu->title ? 1 : 0);
313 /* Selected item should be a visible entry */
314 first_item = selected = menu_next_visible_item(menu, -1, entries);
321 * Keep selected item visible
323 while (selected < first_item)
324 first_item = menu_prev_visible_item(menu, first_item, entries);
325 while (selected >= first_item + items_per_page)
326 first_item = menu_next_visible_item(menu, first_item, entries);
329 text_clear(menu->bitmap);
330 menu_layout(menu, first_item, items_per_page, selected);
332 #if CONFIG_MENU_MENUBAR
333 menu_update_menubar(menu, &mb, selected);
340 struct MenuItem *item = &(menu->items[selected]);
341 menu_doselect(menu, item);
343 /* Return userdata as result */
344 if (!menu->flags & MF_STICKY)
345 return item->userdata;
349 selected = menu_prev_visible_item(menu, selected, entries);
351 else if (key & K_DOWN)
353 selected = menu_next_visible_item(menu, selected, entries);
355 else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
364 * Set flags on a menuitem.
366 * \param menu Menu owner of the item to change.
367 * \param idx Index of the menu item.
368 * \param flags Bit mask of the flags to set.
372 int menu_setFlags(struct Menu *menu, int idx, int flags)
374 ASSERT(idx < menu_count(menu));
376 int old = menu->items[idx].flags;
377 menu->items[idx].flags |= flags;
383 * Clear flags on a menuitem.
385 * \param menu Menu owner of the item to change.
386 * \param idx Index of the menu item.
387 * \param flags Bit mask of the flags to clear.
391 int menu_clearFlags(struct Menu *menu, int idx, int flags)
393 ASSERT(idx < menu_count(menu));
395 int old = menu->items[idx].flags;
396 menu->items[idx].flags &= ~flags;