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.2 2006/02/15 09:10:51 bernie
20 *#* Make title bold; Fix height when we have no menubar.
22 *#* Revision 1.1 2006/02/10 12:29:36 bernie
25 *#* Revision 1.48 2005/11/27 23:02:55 bernie
26 *#* Move graphics modules from mware/ to gfx/.
28 *#* Revision 1.47 2005/11/16 18:10:19 bernie
29 *#* Move top-level headers to cfg/ as in DevLib.
31 *#* Revision 1.46 2005/02/17 03:49:21 bernie
32 *#* Update to new PGM api.
34 *#* Revision 1.45 2005/02/11 19:11:32 aleph
35 *#* Move menu_displaymsg() in new displaymsg module
37 *#* Revision 1.44 2005/01/21 20:05:57 aleph
38 *#* Fix build warning with debug off
40 *#* Revision 1.43 2005/01/13 16:56:36 aleph
41 *#* Fix progmem includes.
43 *#* Revision 1.42 2004/10/31 11:02:15 aleph
44 *#* Rename functions with correct codying conventions; Simplify version display
46 *#* Revision 1.41 2004/10/15 17:34:33 customer_pw
47 *#* Fix menuitem max length
49 *#* Revision 1.40 2004/10/06 12:55:08 customer_pw
50 *#* Declare unused (if !_DEBUG) menu_count()
52 *#* Revision 1.39 2004/10/01 14:04:59 customer_pw
53 *#* Add accessor functions for menu flags
55 *#* Revision 1.38 2004/09/27 12:05:46 customer_pw
56 *#* Use sel label for toggle menus and remove it
58 *#* Revision 1.37 2004/09/27 10:05:33 customer_pw
59 *#* Menu cosmetic fixes
61 *#* Revision 1.36 2004/09/14 22:18:03 bernie
62 *#* Adapt to stricter casting rules.
64 *#* Revision 1.35 2004/09/12 17:56:03 aleph
65 *#* Include debug.h instead of drv/kdebug.h
70 #if CONFIG_MENU_MENUBAR
78 #include <cfg/compiler.h>
79 #include <cfg/debug.h>
82 #include <avr/pgmspace.h> /* strncpy_P() */
85 #include <string.h> /* strcpy() */
87 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
90 #define PTRMSG(x) ((const char *)x)
94 /* Temporary fake defines for ABORT stuff... */
96 #define PUSH_ABORT false
97 #define POP_ABORT do {} while(0)
98 #define DO_ABORT do {} while(0)
103 * Count the items present in a menu.
105 static int UNUSED_FUNC menu_count(const struct Menu *menu)
108 struct MenuItem *item;
110 for (item = menu->items; item->label; ++item)
118 #if CONFIG_MENU_MENUBAR
121 * Update the menu bar according to the selected item
124 static void menu_update_menubar(
125 const struct Menu *menu,
129 int item_flags = menu->items[selected].flags;
130 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
132 if (item_flags & MIF_DISABLED)
133 newlabel = (const_iptr_t)LABEL_EMPTY;
134 else if (item_flags & MIF_TOGGLE)
135 newlabel = (const_iptr_t)LABEL_SEL;
136 else if (item_flags & MIF_CHECKIT)
138 newlabel = (item_flags & MIF_CHECKED) ?
139 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
142 mb->labels[3] = newlabel;
145 #endif /* CONFIG_MENU_MENUBAR */
149 * Show a menu on the LCD display.
151 static void menu_layout(
152 const struct Menu *menu,
157 const MenuItem *item;
159 const char * PROGMEM title = PTRMSG(menu->title);
161 ypos = menu->startrow;
164 text_xprintf(menu->bitmap, ypos++, 0, STYLEF_BOLD | TEXT_FILL, title);
167 cnt = 0, item = &menu->items[first_item];
168 cnt < items_per_page;
171 /* Check for end of menu */
172 if (!(item->label || item->hook))
175 /* Only print visible items */
176 if (!(item->flags & MIF_HIDDEN))
184 menu->bitmap, ypos++, 0,
185 (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
186 (item->flags & MIF_RAMLABEL) ? PSTR("%s%s") : PSTR("%S%s"),
188 (item->flags & MIF_TOGGLE) ?
189 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
190 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
198 * Handle menu item selection
200 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
202 /* Exclude other items */
204 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
207 menu->items[i].flags &= ~MIF_CHECKED;
210 if (item->flags & MIF_DISABLED)
213 /* Handle checkable items */
214 if (item->flags & MIF_TOGGLE)
215 item->flags ^= MIF_CHECKED;
216 else if (item->flags & MIF_CHECKIT)
217 item->flags |= MIF_CHECKED;
219 /* Handle items with callback hooks */
222 /* Push a jmp buffer to abort the operation with the STOP key */
225 item->hook(item->userdata);
233 * Return the previous visible item (rolls back to the last item)
235 static int menu_next_visible_item(const struct Menu *menu, int index, int total)
239 if (++index >= total)
242 while (menu->items[index].flags & MIF_HIDDEN);
249 * Return the next visible item (rolls back to the first item)
251 static int menu_prev_visible_item(const struct Menu *menu, int index, int total)
258 while (menu->items[index].flags & MIF_HIDDEN);
265 * Handle a menu and invoke hook functions for the selected menu items.
267 iptr_t menu_handle(const struct Menu *menu)
269 uint8_t entries, visible_entries, items_per_page;
270 uint8_t first_item, selected;
272 #if CONFIG_MENU_MENUBAR
274 const_iptr_t labels[] =
276 (const_iptr_t)LABEL_BACK,
277 (const_iptr_t)LABEL_UPARROW,
278 (const_iptr_t)LABEL_DOWNARROW,
283 * Initialize menu bar
285 if (menu->flags & MF_TOPLEVEL)
286 labels[0] = (const_iptr_t)LABEL_EMPTY;
288 mbar_init(&mb, menu->bitmap, labels, countof(labels));
289 #endif /* CONFIG_MENU_MENUBAR */
292 /* Compute total number of items in menu (entries) and
293 * the number of visible entries, which excludes items
297 struct MenuItem *item;
302 for (item = menu->items; (item->label || item->hook); ++item)
305 if (!(item->flags & MIF_HIDDEN))
311 (menu->bitmap->height / menu->bitmap->font->height)
313 #if CONFIG_MENU_MENUBAR
314 - 1 /* menu bar labels */
316 - (menu->title ? 1 : 0);
318 /* Selected item should be a visible entry */
319 first_item = selected = menu_next_visible_item(menu, -1, entries);
326 * Keep selected item visible
328 while (selected < first_item)
329 first_item = menu_prev_visible_item(menu, first_item, entries);
330 while (selected >= first_item + items_per_page)
331 first_item = menu_next_visible_item(menu, first_item, entries);
334 text_clear(menu->bitmap);
335 menu_layout(menu, first_item, items_per_page, selected);
337 #if CONFIG_MENU_MENUBAR
338 menu_update_menubar(menu, &mb, selected);
345 struct MenuItem *item = &(menu->items[selected]);
346 menu_doselect(menu, item);
348 /* Return userdata as result */
349 if (!menu->flags & MF_STICKY)
350 return item->userdata;
354 selected = menu_prev_visible_item(menu, selected, entries);
356 else if (key & K_DOWN)
358 selected = menu_next_visible_item(menu, selected, entries);
360 else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
369 * Set flags on a menuitem.
371 * \param menu Menu owner of the item to change.
372 * \param idx Index of the menu item.
373 * \param flags Bit mask of the flags to set.
377 int menu_setFlags(struct Menu *menu, int idx, int flags)
379 ASSERT(idx < menu_count(menu));
381 int old = menu->items[idx].flags;
382 menu->items[idx].flags |= flags;
388 * Clear flags on a menuitem.
390 * \param menu Menu owner of the item to change.
391 * \param idx Index of the menu item.
392 * \param flags Bit mask of the flags to clear.
396 int menu_clearFlags(struct Menu *menu, int idx, int flags)
398 ASSERT(idx < menu_count(menu));
400 int old = menu->items[idx].flags;
401 menu->items[idx].flags &= ~flags;