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.3 2006/02/20 14:34:32 bernie
20 *#* Include appconfig.h before using its definitions.
22 *#* Revision 1.2 2006/02/15 09:10:51 bernie
23 *#* Make title bold; Fix height when we have no menubar.
25 *#* Revision 1.1 2006/02/10 12:29:36 bernie
28 *#* Revision 1.48 2005/11/27 23:02:55 bernie
29 *#* Move graphics modules from mware/ to gfx/.
31 *#* Revision 1.47 2005/11/16 18:10:19 bernie
32 *#* Move top-level headers to cfg/ as in DevLib.
34 *#* Revision 1.46 2005/02/17 03:49:21 bernie
35 *#* Update to new PGM api.
37 *#* Revision 1.45 2005/02/11 19:11:32 aleph
38 *#* Move menu_displaymsg() in new displaymsg module
40 *#* Revision 1.44 2005/01/21 20:05:57 aleph
41 *#* Fix build warning with debug off
43 *#* Revision 1.43 2005/01/13 16:56:36 aleph
44 *#* Fix progmem includes.
46 *#* Revision 1.42 2004/10/31 11:02:15 aleph
47 *#* Rename functions with correct codying conventions; Simplify version display
49 *#* Revision 1.41 2004/10/15 17:34:33 customer_pw
50 *#* Fix menuitem max length
52 *#* Revision 1.40 2004/10/06 12:55:08 customer_pw
53 *#* Declare unused (if !_DEBUG) menu_count()
55 *#* Revision 1.39 2004/10/01 14:04:59 customer_pw
56 *#* Add accessor functions for menu flags
58 *#* Revision 1.38 2004/09/27 12:05:46 customer_pw
59 *#* Use sel label for toggle menus and remove it
61 *#* Revision 1.37 2004/09/27 10:05:33 customer_pw
62 *#* Menu cosmetic fixes
64 *#* Revision 1.36 2004/09/14 22:18:03 bernie
65 *#* Adapt to stricter casting rules.
67 *#* Revision 1.35 2004/09/12 17:56:03 aleph
68 *#* Include debug.h instead of drv/kdebug.h
77 #include <cfg/compiler.h>
78 #include <cfg/debug.h>
79 #include <appconfig.h>
82 #include <avr/pgmspace.h> /* strncpy_P() */
85 #include <string.h> /* strcpy() */
87 #if CONFIG_MENU_MENUBAR
91 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
94 #define PTRMSG(x) ((const char *)x)
98 /* Temporary fake defines for ABORT stuff... */
100 #define PUSH_ABORT false
101 #define POP_ABORT do {} while(0)
102 #define DO_ABORT do {} while(0)
107 * Count the items present in a menu.
109 static int UNUSED_FUNC menu_count(const struct Menu *menu)
112 struct MenuItem *item;
114 for (item = menu->items; item->label; ++item)
122 #if CONFIG_MENU_MENUBAR
125 * Update the menu bar according to the selected item
128 static void menu_update_menubar(
129 const struct Menu *menu,
133 int item_flags = menu->items[selected].flags;
134 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
136 if (item_flags & MIF_DISABLED)
137 newlabel = (const_iptr_t)LABEL_EMPTY;
138 else if (item_flags & MIF_TOGGLE)
139 newlabel = (const_iptr_t)LABEL_SEL;
140 else if (item_flags & MIF_CHECKIT)
142 newlabel = (item_flags & MIF_CHECKED) ?
143 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
146 mb->labels[3] = newlabel;
149 #endif /* CONFIG_MENU_MENUBAR */
153 * Show a menu on the LCD display.
155 static void menu_layout(
156 const struct Menu *menu,
161 const MenuItem *item;
163 const char * PROGMEM title = PTRMSG(menu->title);
165 ypos = menu->startrow;
168 text_xprintf(menu->bitmap, ypos++, 0, STYLEF_BOLD | TEXT_FILL, title);
171 cnt = 0, item = &menu->items[first_item];
172 cnt < items_per_page;
175 /* Check for end of menu */
176 if (!(item->label || item->hook))
179 /* Only print visible items */
180 if (!(item->flags & MIF_HIDDEN))
188 menu->bitmap, ypos++, 0,
189 (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
190 (item->flags & MIF_RAMLABEL) ? PSTR("%s%s") : PSTR("%S%s"),
192 (item->flags & MIF_TOGGLE) ?
193 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
194 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
202 * Handle menu item selection
204 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
206 /* Exclude other items */
208 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
211 menu->items[i].flags &= ~MIF_CHECKED;
214 if (item->flags & MIF_DISABLED)
217 /* Handle checkable items */
218 if (item->flags & MIF_TOGGLE)
219 item->flags ^= MIF_CHECKED;
220 else if (item->flags & MIF_CHECKIT)
221 item->flags |= MIF_CHECKED;
223 /* Handle items with callback hooks */
226 /* Push a jmp buffer to abort the operation with the STOP key */
229 item->hook(item->userdata);
237 * Return the previous visible item (rolls back to the last item)
239 static int menu_next_visible_item(const struct Menu *menu, int index, int total)
243 if (++index >= total)
246 while (menu->items[index].flags & MIF_HIDDEN);
253 * Return the next visible item (rolls back to the first item)
255 static int menu_prev_visible_item(const struct Menu *menu, int index, int total)
262 while (menu->items[index].flags & MIF_HIDDEN);
269 * Handle a menu and invoke hook functions for the selected menu items.
271 iptr_t menu_handle(const struct Menu *menu)
273 uint8_t entries, visible_entries, items_per_page;
274 uint8_t first_item, selected;
276 #if CONFIG_MENU_MENUBAR
278 const_iptr_t labels[] =
280 (const_iptr_t)LABEL_BACK,
281 (const_iptr_t)LABEL_UPARROW,
282 (const_iptr_t)LABEL_DOWNARROW,
287 * Initialize menu bar
289 if (menu->flags & MF_TOPLEVEL)
290 labels[0] = (const_iptr_t)LABEL_EMPTY;
292 mbar_init(&mb, menu->bitmap, labels, countof(labels));
293 #endif /* CONFIG_MENU_MENUBAR */
296 /* Compute total number of items in menu (entries) and
297 * the number of visible entries, which excludes items
301 struct MenuItem *item;
306 for (item = menu->items; (item->label || item->hook); ++item)
309 if (!(item->flags & MIF_HIDDEN))
315 (menu->bitmap->height / menu->bitmap->font->height)
317 #if CONFIG_MENU_MENUBAR
318 - 1 /* menu bar labels */
320 - (menu->title ? 1 : 0);
322 /* Selected item should be a visible entry */
323 first_item = selected = menu_next_visible_item(menu, -1, entries);
330 * Keep selected item visible
332 while (selected < first_item)
333 first_item = menu_prev_visible_item(menu, first_item, entries);
334 while (selected >= first_item + items_per_page)
335 first_item = menu_next_visible_item(menu, first_item, entries);
338 text_clear(menu->bitmap);
339 menu_layout(menu, first_item, items_per_page, selected);
341 #if CONFIG_MENU_MENUBAR
342 menu_update_menubar(menu, &mb, selected);
349 struct MenuItem *item = &(menu->items[selected]);
350 menu_doselect(menu, item);
352 /* Return userdata as result */
353 if (!menu->flags & MF_STICKY)
354 return item->userdata;
358 selected = menu_prev_visible_item(menu, selected, entries);
360 else if (key & K_DOWN)
362 selected = menu_next_visible_item(menu, selected, entries);
364 else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
373 * Set flags on a menuitem.
375 * \param menu Menu owner of the item to change.
376 * \param idx Index of the menu item.
377 * \param flags Bit mask of the flags to set.
381 int menu_setFlags(struct Menu *menu, int idx, int flags)
383 ASSERT(idx < menu_count(menu));
385 int old = menu->items[idx].flags;
386 menu->items[idx].flags |= flags;
392 * Clear flags on a menuitem.
394 * \param menu Menu owner of the item to change.
395 * \param idx Index of the menu item.
396 * \param flags Bit mask of the flags to clear.
400 int menu_clearFlags(struct Menu *menu, int idx, int flags)
402 ASSERT(idx < menu_count(menu));
404 int old = menu->items[idx].flags;
405 menu->items[idx].flags &= ~flags;