4 * Copyright 2003, 2004, 2006 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.7 2006/04/27 05:39:24 bernie
20 *#* Enhance text rendering to arbitrary x,y coords.
22 *#* Revision 1.6 2006/04/11 00:07:32 bernie
23 *#* Implemenent MF_SAVESEL flag.
25 *#* Revision 1.5 2006/03/22 09:49:51 bernie
26 *#* Simplifications from project_grl.
28 *#* Revision 1.4 2006/03/20 17:48:35 bernie
29 *#* Implement support for ROM menus.
31 *#* Revision 1.3 2006/02/20 14:34:32 bernie
32 *#* Include appconfig.h before using its definitions.
34 *#* Revision 1.2 2006/02/15 09:10:51 bernie
35 *#* Make title bold; Fix height when we have no menubar.
37 *#* Revision 1.1 2006/02/10 12:29:36 bernie
40 *#* Revision 1.48 2005/11/27 23:02:55 bernie
41 *#* Move graphics modules from mware/ to gfx/.
43 *#* Revision 1.47 2005/11/16 18:10:19 bernie
44 *#* Move top-level headers to cfg/ as in DevLib.
46 *#* Revision 1.46 2005/02/17 03:49:21 bernie
47 *#* Update to new PGM api.
49 *#* Revision 1.45 2005/02/11 19:11:32 aleph
50 *#* Move menu_displaymsg() in new displaymsg module
52 *#* Revision 1.44 2005/01/21 20:05:57 aleph
53 *#* Fix build warning with debug off
55 *#* Revision 1.43 2005/01/13 16:56:36 aleph
56 *#* Fix progmem includes.
58 *#* Revision 1.42 2004/10/31 11:02:15 aleph
59 *#* Rename functions with correct codying conventions; Simplify version display
61 *#* Revision 1.41 2004/10/15 17:34:33 customer_pw
62 *#* Fix menuitem max length
64 *#* Revision 1.40 2004/10/06 12:55:08 customer_pw
65 *#* Declare unused (if !_DEBUG) menu_count()
67 *#* Revision 1.39 2004/10/01 14:04:59 customer_pw
68 *#* Add accessor functions for menu flags
70 *#* Revision 1.38 2004/09/27 12:05:46 customer_pw
71 *#* Use sel label for toggle menus and remove it
73 *#* Revision 1.37 2004/09/27 10:05:33 customer_pw
74 *#* Menu cosmetic fixes
76 *#* Revision 1.36 2004/09/14 22:18:03 bernie
77 *#* Adapt to stricter casting rules.
79 *#* Revision 1.35 2004/09/12 17:56:03 aleph
80 *#* Include debug.h instead of drv/kdebug.h
88 #include <cfg/compiler.h>
89 #include <cfg/debug.h>
90 #include <appconfig.h>
91 #include <string.h> /* strcpy() */
94 #include <avr/pgmspace.h> /* strncpy_P() */
97 #if CONFIG_MENU_SMOOTH
98 #include <drv/lcd_gfx.h>
101 #if CONFIG_MENU_MENUBAR
105 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
108 #define PTRMSG(x) ((const char *)x)
111 /* Temporary fake defines for ABORT stuff... */
113 #define PUSH_ABORT false
114 #define POP_ABORT do {} while(0)
115 #define DO_ABORT do {} while(0)
119 * Count the items present in a menu.
121 static int menu_count(const struct Menu *menu)
125 for (cnt = 0; /*NOP*/; ++cnt)
127 const MenuItem *item = &menu->items[cnt];
130 if (menu->flags & MF_ROMITEMS)
132 memcpy_P(&ram_item, item, sizeof(ram_item));
136 if (!(item->label || item->hook))
143 #if CONFIG_MENU_MENUBAR
146 * Update the menu bar according to the selected item
149 static void menu_update_menubar(
150 const struct Menu *menu,
156 if (menu->flags & MF_ROMITEMS)
158 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
159 item_flags = pgm_read_int(&menu->items[selected].flags);
163 item_flags = menu->items[selected].flags;
165 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
167 if (item_flags & MIF_DISABLED)
168 newlabel = (const_iptr_t)LABEL_EMPTY;
169 else if (item_flags & MIF_TOGGLE)
170 newlabel = (const_iptr_t)LABEL_SEL;
171 else if (item_flags & MIF_CHECKIT)
173 newlabel = (item_flags & MIF_CHECKED) ?
174 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
177 mb->labels[3] = newlabel;
180 #endif /* CONFIG_MENU_MENUBAR */
184 * Show a menu on the display.
186 static void menu_layout(
187 const struct Menu *menu,
193 const char * PROGMEM title = PTRMSG(menu->title);
194 Bitmap *bm = menu->bitmap;
200 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
201 ypos += bm->font->height;
204 #if CONFIG_MENU_SMOOTH
205 static coord_t yoffset = 0;
206 static int old_first_item = 0;
207 static mtime_t old_time = 0; //UNUSED
209 coord_t old_ymin = bm->cr.ymin;
212 bm->cr.xmin, bm->cr.ymin + ypos,
213 bm->cr.xmax, bm->cr.ymax);
215 if (old_first_item != first_item)
217 speed = ABS(old_first_item - first_item) * 3;
219 if (old_first_item > first_item)
222 if (yoffset > bm->font->height)
231 if (yoffset < -bm->font->height)
237 first_item = old_first_item;
242 for (cnt = 0; cnt < items_per_page; ++cnt)
244 const MenuItem *item = &menu->items[first_item + cnt];
247 if (menu->flags & MF_ROMITEMS)
249 memcpy_P(&ram_item, item, sizeof(ram_item));
254 /* Check for end of menu */
255 if (!(item->label || item->hook))
258 /* Only print visible items */
259 if (!(item->flags & MIF_HIDDEN))
268 (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
269 (item->flags & MIF_RAMLABEL) ? PSTR("%s%S") : PSTR("%S%S"),
271 (item->flags & MIF_TOGGLE) ?
272 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
273 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
275 ypos += bm->font->height;
279 #if CONFIG_MENU_SMOOTH
280 /* Restore old cliprect */
282 bm->cr.xmin, old_ymin,
283 bm->cr.xmax, bm->cr.ymax);
285 lcd_blitBitmap(&lcd_bitmap);
291 * Handle menu item selection
293 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
295 /* Exclude other items */
297 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
300 menu->items[i].flags &= ~MIF_CHECKED;
303 if (item->flags & MIF_DISABLED)
306 /* Handle checkable items */
307 if (item->flags & MIF_TOGGLE)
308 item->flags ^= MIF_CHECKED;
309 else if (item->flags & MIF_CHECKIT)
310 item->flags |= MIF_CHECKED;
312 /* Handle items with callback hooks */
315 /* Push a jmp buffer to abort the operation with the STOP key */
318 item->hook(item->userdata);
326 * Return the next visible item (rolls back to the first item)
328 static int menu_next_visible_item(const struct Menu *menu, int index)
330 int total = menu_count(menu);
335 if (++index >= total)
339 if (menu->flags & MF_ROMITEMS)
341 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
342 item_flags = pgm_read_int(&menu->items[index].flags);
346 item_flags = menu->items[index].flags;
348 while (item_flags & MIF_HIDDEN);
355 * Return the previous visible item (rolls back to the last item)
357 static int menu_prev_visible_item(const struct Menu *menu, int index)
359 int total = menu_count(menu);
368 if (menu->flags & MF_ROMITEMS)
370 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
371 item_flags = pgm_read_int(&menu->items[index].flags);
375 item_flags = menu->items[index].flags;
377 while (item_flags & MIF_HIDDEN);
384 * Handle a menu and invoke hook functions for the selected menu items.
386 iptr_t menu_handle(const struct Menu *menu)
388 uint8_t items_per_page;
389 uint8_t first_item, selected;
391 #if CONFIG_MENU_MENUBAR
393 const_iptr_t labels[] =
395 (const_iptr_t)LABEL_BACK,
396 (const_iptr_t)LABEL_UPARROW,
397 (const_iptr_t)LABEL_DOWNARROW,
402 * Initialize menu bar
404 if (menu->flags & MF_TOPLEVEL)
405 labels[0] = (const_iptr_t)LABEL_EMPTY;
407 mbar_init(&mb, menu->bitmap, labels, countof(labels));
408 #endif /* CONFIG_MENU_MENUBAR */
412 (menu->bitmap->height / menu->bitmap->font->height)
413 #if CONFIG_MENU_MENUBAR
414 - 1 /* menu bar labels */
416 - (menu->title ? 1 : 0);
418 /* Selected item should be a visible entry */
419 first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
422 text_clear(menu->bitmap);
429 * Keep selected item visible
431 while (selected < first_item)
432 first_item = menu_prev_visible_item(menu, first_item);
433 while (selected >= first_item + items_per_page)
434 first_item = menu_next_visible_item(menu, first_item);
436 menu_layout(menu, first_item, items_per_page, selected);
438 #if CONFIG_MENU_MENUBAR
439 menu_update_menubar(menu, &mb, selected);
442 #if CONFIG_MENU_SMOOTH
450 struct MenuItem *item = &(menu->items[selected]);
453 if (menu->flags & MF_ROMITEMS)
455 memcpy_P(&ram_item, item, sizeof(ram_item));
459 menu_doselect(menu, item);
461 /* Return userdata as result */
462 if (!menu->flags & MF_STICKY)
464 /* Store currently selected item before leaving. */
465 if (menu->flags & MF_SAVESEL)
466 CONST_CAST(struct Menu *, menu)->selected = selected;
467 return item->userdata;
471 text_clear(menu->bitmap);
475 selected = menu_prev_visible_item(menu, selected);
477 else if (key & K_DOWN)
479 selected = menu_next_visible_item(menu, selected);
481 else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
483 /* Store currently selected item before leaving. */
484 if (menu->flags & MF_SAVESEL)
485 CONST_CAST(struct Menu *, menu)->selected = selected;
493 * Set flags on a menuitem.
495 * \param menu Menu owner of the item to change.
496 * \param idx Index of the menu item.
497 * \param flags Bit mask of the flags to set.
501 int menu_setFlags(struct Menu *menu, int idx, int flags)
503 ASSERT(idx < menu_count(menu));
504 ASSERT(!(menu->flags & MF_ROMITEMS));
506 int old = menu->items[idx].flags;
507 menu->items[idx].flags |= flags;
513 * Clear flags on a menuitem.
515 * \param menu Menu owner of the item to change.
516 * \param idx Index of the menu item.
517 * \param flags Bit mask of the flags to clear.
521 int menu_clearFlags(struct Menu *menu, int idx, int flags)
523 ASSERT(idx < menu_count(menu));
524 ASSERT(!(menu->flags & MF_ROMITEMS));
526 int old = menu->items[idx].flags;
527 menu->items[idx].flags &= ~flags;