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.1 2006/05/15 07:20:54 bernie
20 *#* Move menu to gui/.
22 *#* Revision 1.7 2006/04/27 05:39:24 bernie
23 *#* Enhance text rendering to arbitrary x,y coords.
25 *#* Revision 1.6 2006/04/11 00:07:32 bernie
26 *#* Implemenent MF_SAVESEL flag.
28 *#* Revision 1.5 2006/03/22 09:49:51 bernie
29 *#* Simplifications from project_grl.
31 *#* Revision 1.4 2006/03/20 17:48:35 bernie
32 *#* Implement support for ROM menus.
34 *#* Revision 1.3 2006/02/20 14:34:32 bernie
35 *#* Include appconfig.h before using its definitions.
37 *#* Revision 1.2 2006/02/15 09:10:51 bernie
38 *#* Make title bold; Fix height when we have no menubar.
40 *#* Revision 1.1 2006/02/10 12:29:36 bernie
43 *#* Revision 1.48 2005/11/27 23:02:55 bernie
44 *#* Move graphics modules from mware/ to gfx/.
46 *#* Revision 1.47 2005/11/16 18:10:19 bernie
47 *#* Move top-level headers to cfg/ as in DevLib.
49 *#* Revision 1.46 2005/02/17 03:49:21 bernie
50 *#* Update to new PGM api.
52 *#* Revision 1.45 2005/02/11 19:11:32 aleph
53 *#* Move menu_displaymsg() in new displaymsg module
55 *#* Revision 1.44 2005/01/21 20:05:57 aleph
56 *#* Fix build warning with debug off
58 *#* Revision 1.43 2005/01/13 16:56:36 aleph
59 *#* Fix progmem includes.
61 *#* Revision 1.42 2004/10/31 11:02:15 aleph
62 *#* Rename functions with correct codying conventions; Simplify version display
64 *#* Revision 1.41 2004/10/15 17:34:33 customer_pw
65 *#* Fix menuitem max length
67 *#* Revision 1.40 2004/10/06 12:55:08 customer_pw
68 *#* Declare unused (if !_DEBUG) menu_count()
70 *#* Revision 1.39 2004/10/01 14:04:59 customer_pw
71 *#* Add accessor functions for menu flags
73 *#* Revision 1.38 2004/09/27 12:05:46 customer_pw
74 *#* Use sel label for toggle menus and remove it
76 *#* Revision 1.37 2004/09/27 10:05:33 customer_pw
77 *#* Menu cosmetic fixes
79 *#* Revision 1.36 2004/09/14 22:18:03 bernie
80 *#* Adapt to stricter casting rules.
82 *#* Revision 1.35 2004/09/12 17:56:03 aleph
83 *#* Include debug.h instead of drv/kdebug.h
91 #include <cfg/compiler.h>
92 #include <cfg/debug.h>
93 #include <appconfig.h>
94 #include <string.h> /* strcpy() */
97 #include <avr/pgmspace.h> /* strncpy_P() */
100 #if CONFIG_MENU_SMOOTH
101 #include <drv/lcd_gfx.h>
104 #if CONFIG_MENU_MENUBAR
108 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
111 #define PTRMSG(x) ((const char *)x)
114 /* Temporary fake defines for ABORT stuff... */
116 #define PUSH_ABORT false
117 #define POP_ABORT do {} while(0)
118 #define DO_ABORT do {} while(0)
122 * Count the items present in a menu.
124 static int menu_count(const struct Menu *menu)
128 for (cnt = 0; /*NOP*/; ++cnt)
130 const MenuItem *item = &menu->items[cnt];
133 if (menu->flags & MF_ROMITEMS)
135 memcpy_P(&ram_item, item, sizeof(ram_item));
139 if (!(item->label || item->hook))
146 #if CONFIG_MENU_MENUBAR
149 * Update the menu bar according to the selected item
152 static void menu_update_menubar(
153 const struct Menu *menu,
159 if (menu->flags & MF_ROMITEMS)
161 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
162 item_flags = pgm_read_int(&menu->items[selected].flags);
166 item_flags = menu->items[selected].flags;
168 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
170 if (item_flags & MIF_DISABLED)
171 newlabel = (const_iptr_t)LABEL_EMPTY;
172 else if (item_flags & MIF_TOGGLE)
173 newlabel = (const_iptr_t)LABEL_SEL;
174 else if (item_flags & MIF_CHECKIT)
176 newlabel = (item_flags & MIF_CHECKED) ?
177 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
180 mb->labels[3] = newlabel;
183 #endif /* CONFIG_MENU_MENUBAR */
187 * Show a menu on the display.
189 static void menu_layout(
190 const struct Menu *menu,
196 const char * PROGMEM title = PTRMSG(menu->title);
197 Bitmap *bm = menu->bitmap;
203 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
204 ypos += bm->font->height;
207 #if CONFIG_MENU_SMOOTH
208 static coord_t yoffset = 0;
209 static int old_first_item = 0;
210 static mtime_t old_time = 0; //UNUSED
212 coord_t old_ymin = bm->cr.ymin;
215 bm->cr.xmin, bm->cr.ymin + ypos,
216 bm->cr.xmax, bm->cr.ymax);
218 if (old_first_item != first_item)
220 speed = ABS(old_first_item - first_item) * 3;
222 if (old_first_item > first_item)
225 if (yoffset > bm->font->height)
234 if (yoffset < -bm->font->height)
240 first_item = old_first_item;
245 for (cnt = 0; cnt < items_per_page; ++cnt)
247 const MenuItem *item = &menu->items[first_item + cnt];
250 if (menu->flags & MF_ROMITEMS)
252 memcpy_P(&ram_item, item, sizeof(ram_item));
257 /* Check for end of menu */
258 if (!(item->label || item->hook))
261 /* Only print visible items */
262 if (!(item->flags & MIF_HIDDEN))
271 (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
272 (item->flags & MIF_RAMLABEL) ? PSTR("%s%S") : PSTR("%S%S"),
274 (item->flags & MIF_TOGGLE) ?
275 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
276 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
278 ypos += bm->font->height;
282 #if CONFIG_MENU_SMOOTH
283 /* Restore old cliprect */
285 bm->cr.xmin, old_ymin,
286 bm->cr.xmax, bm->cr.ymax);
288 lcd_blitBitmap(&lcd_bitmap);
294 * Handle menu item selection
296 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
298 /* Exclude other items */
300 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
303 menu->items[i].flags &= ~MIF_CHECKED;
306 if (item->flags & MIF_DISABLED)
309 /* Handle checkable items */
310 if (item->flags & MIF_TOGGLE)
311 item->flags ^= MIF_CHECKED;
312 else if (item->flags & MIF_CHECKIT)
313 item->flags |= MIF_CHECKED;
315 /* Handle items with callback hooks */
318 /* Push a jmp buffer to abort the operation with the STOP key */
321 item->hook(item->userdata);
329 * Return the next visible item (rolls back to the first item)
331 static int menu_next_visible_item(const struct Menu *menu, int index)
333 int total = menu_count(menu);
338 if (++index >= total)
342 if (menu->flags & MF_ROMITEMS)
344 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
345 item_flags = pgm_read_int(&menu->items[index].flags);
349 item_flags = menu->items[index].flags;
351 while (item_flags & MIF_HIDDEN);
358 * Return the previous visible item (rolls back to the last item)
360 static int menu_prev_visible_item(const struct Menu *menu, int index)
362 int total = menu_count(menu);
371 if (menu->flags & MF_ROMITEMS)
373 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
374 item_flags = pgm_read_int(&menu->items[index].flags);
378 item_flags = menu->items[index].flags;
380 while (item_flags & MIF_HIDDEN);
387 * Handle a menu and invoke hook functions for the selected menu items.
389 iptr_t menu_handle(const struct Menu *menu)
391 uint8_t items_per_page;
392 uint8_t first_item, selected;
394 #if CONFIG_MENU_MENUBAR
396 const_iptr_t labels[] =
398 (const_iptr_t)LABEL_BACK,
399 (const_iptr_t)LABEL_UPARROW,
400 (const_iptr_t)LABEL_DOWNARROW,
405 * Initialize menu bar
407 if (menu->flags & MF_TOPLEVEL)
408 labels[0] = (const_iptr_t)LABEL_EMPTY;
410 mbar_init(&mb, menu->bitmap, labels, countof(labels));
411 #endif /* CONFIG_MENU_MENUBAR */
415 (menu->bitmap->height / menu->bitmap->font->height)
416 #if CONFIG_MENU_MENUBAR
417 - 1 /* menu bar labels */
419 - (menu->title ? 1 : 0);
421 /* Selected item should be a visible entry */
422 first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
425 text_clear(menu->bitmap);
432 * Keep selected item visible
434 while (selected < first_item)
435 first_item = menu_prev_visible_item(menu, first_item);
436 while (selected >= first_item + items_per_page)
437 first_item = menu_next_visible_item(menu, first_item);
439 menu_layout(menu, first_item, items_per_page, selected);
441 #if CONFIG_MENU_MENUBAR
442 menu_update_menubar(menu, &mb, selected);
445 #if CONFIG_MENU_SMOOTH
453 struct MenuItem *item = &(menu->items[selected]);
456 if (menu->flags & MF_ROMITEMS)
458 memcpy_P(&ram_item, item, sizeof(ram_item));
462 menu_doselect(menu, item);
464 /* Return userdata as result */
465 if (!menu->flags & MF_STICKY)
467 /* Store currently selected item before leaving. */
468 if (menu->flags & MF_SAVESEL)
469 CONST_CAST(struct Menu *, menu)->selected = selected;
470 return item->userdata;
474 text_clear(menu->bitmap);
478 selected = menu_prev_visible_item(menu, selected);
480 else if (key & K_DOWN)
482 selected = menu_next_visible_item(menu, selected);
484 else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
486 /* Store currently selected item before leaving. */
487 if (menu->flags & MF_SAVESEL)
488 CONST_CAST(struct Menu *, menu)->selected = selected;
496 * Set flags on a menuitem.
498 * \param menu Menu owner of the item to change.
499 * \param idx Index of the menu item.
500 * \param flags Bit mask of the flags to set.
504 int menu_setFlags(struct Menu *menu, int idx, int flags)
506 ASSERT(idx < menu_count(menu));
507 ASSERT(!(menu->flags & MF_ROMITEMS));
509 int old = menu->items[idx].flags;
510 menu->items[idx].flags |= flags;
516 * Clear flags on a menuitem.
518 * \param menu Menu owner of the item to change.
519 * \param idx Index of the menu item.
520 * \param flags Bit mask of the flags to clear.
524 int menu_clearFlags(struct Menu *menu, int idx, int flags)
526 ASSERT(idx < menu_count(menu));
527 ASSERT(!(menu->flags & MF_ROMITEMS));
529 int old = menu->items[idx].flags;
530 menu->items[idx].flags &= ~flags;