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.2 2006/05/25 23:34:38 bernie
20 *#* Implement menu timeouts.
22 *#* Revision 1.1 2006/05/15 07:20:54 bernie
23 *#* Move menu to gui/.
25 *#* Revision 1.7 2006/04/27 05:39:24 bernie
26 *#* Enhance text rendering to arbitrary x,y coords.
28 *#* Revision 1.6 2006/04/11 00:07:32 bernie
29 *#* Implemenent MF_SAVESEL flag.
31 *#* Revision 1.5 2006/03/22 09:49:51 bernie
32 *#* Simplifications from project_grl.
34 *#* Revision 1.4 2006/03/20 17:48:35 bernie
35 *#* Implement support for ROM menus.
37 *#* Revision 1.3 2006/02/20 14:34:32 bernie
38 *#* Include appconfig.h before using its definitions.
40 *#* Revision 1.2 2006/02/15 09:10:51 bernie
41 *#* Make title bold; Fix height when we have no menubar.
49 #include <cfg/compiler.h>
50 #include <cfg/debug.h>
51 #include <appconfig.h>
52 #include <string.h> /* strcpy() */
55 #include <avr/pgmspace.h> /* strncpy_P() */
58 #if CONFIG_MENU_SMOOTH
59 #include <drv/lcd_gfx.h>
62 #if (CONFIG_MENU_TIMEOUT != 0)
63 #include <drv/timer.h>
66 #if CONFIG_MENU_MENUBAR
70 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
73 #define PTRMSG(x) ((const char *)x)
76 /* Temporary fake defines for ABORT stuff... */
78 #define PUSH_ABORT false
79 #define POP_ABORT do {} while(0)
80 #define DO_ABORT do {} while(0)
84 * Count the items present in a menu.
86 static int menu_count(const struct Menu *menu)
90 for (cnt = 0; /*NOP*/; ++cnt)
92 const MenuItem *item = &menu->items[cnt];
95 if (menu->flags & MF_ROMITEMS)
97 memcpy_P(&ram_item, item, sizeof(ram_item));
101 if (!(item->label || item->hook))
108 #if CONFIG_MENU_MENUBAR
111 * Update the menu bar according to the selected item
114 static void menu_update_menubar(
115 const struct Menu *menu,
121 if (menu->flags & MF_ROMITEMS)
123 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
124 item_flags = pgm_read_int(&menu->items[selected].flags);
128 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 display.
151 static void menu_layout(
152 const struct Menu *menu,
158 const char * PROGMEM title = PTRMSG(menu->title);
159 Bitmap *bm = menu->bitmap;
165 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
166 ypos += bm->font->height;
169 #if CONFIG_MENU_SMOOTH
170 static coord_t yoffset = 0;
171 static int old_first_item = 0;
173 coord_t old_ymin = bm->cr.ymin;
176 bm->cr.xmin, bm->cr.ymin + ypos,
177 bm->cr.xmax, MIN(bm->cr.ymax, bm->cr.ymin + ypos + items_per_page * bm->font->height));
179 if (old_first_item != first_item)
181 speed = ABS(old_first_item - first_item) * 3;
183 if (old_first_item > first_item)
186 if (yoffset > bm->font->height)
195 if (yoffset < -bm->font->height)
201 first_item = old_first_item;
207 for (cnt = 0; cnt < items_per_page; ++cnt)
209 const MenuItem *item = &menu->items[first_item + cnt];
212 if (menu->flags & MF_ROMITEMS)
214 memcpy_P(&ram_item, item, sizeof(ram_item));
219 /* Check for end of menu */
220 if (!(item->label || item->hook))
223 /* Only print visible items */
224 if (!(item->flags & MIF_HIDDEN))
233 (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
234 (item->flags & MIF_RAMLABEL) ? PSTR("%s%S") : PSTR("%S%S"),
236 (item->flags & MIF_TOGGLE) ?
237 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
238 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
240 ypos += bm->font->height;
244 #if CONFIG_MENU_SMOOTH
245 /* Restore old cliprect */
247 bm->cr.xmin, old_ymin,
248 bm->cr.xmax, bm->cr.ymax);
250 lcd_blitBitmap(&lcd_bitmap);
256 * Handle menu item selection
258 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
260 /* Exclude other items */
262 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
265 menu->items[i].flags &= ~MIF_CHECKED;
268 if (item->flags & MIF_DISABLED)
271 /* Handle checkable items */
272 if (item->flags & MIF_TOGGLE)
273 item->flags ^= MIF_CHECKED;
274 else if (item->flags & MIF_CHECKIT)
275 item->flags |= MIF_CHECKED;
277 /* Handle items with callback hooks */
280 /* Push a jmp buffer to abort the operation with the STOP key */
283 item->hook(item->userdata);
291 * Return the next visible item (rolls back to the first item)
293 static int menu_next_visible_item(const struct Menu *menu, int index)
295 int total = menu_count(menu);
300 if (++index >= total)
304 if (menu->flags & MF_ROMITEMS)
306 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
307 item_flags = pgm_read_int(&menu->items[index].flags);
311 item_flags = menu->items[index].flags;
313 while (item_flags & MIF_HIDDEN);
320 * Return the previous visible item (rolls back to the last item)
322 static int menu_prev_visible_item(const struct Menu *menu, int index)
324 int total = menu_count(menu);
333 if (menu->flags & MF_ROMITEMS)
335 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
336 item_flags = pgm_read_int(&menu->items[index].flags);
340 item_flags = menu->items[index].flags;
342 while (item_flags & MIF_HIDDEN);
349 * Handle a menu and invoke hook functions for the selected menu items.
351 iptr_t menu_handle(const struct Menu *menu)
353 uint8_t items_per_page;
354 uint8_t first_item, selected;
356 #if (CONFIG_MENU_TIMEOUT != 0)
357 ticks_t now, menu_idle_time = timer_clock();
360 #if CONFIG_MENU_MENUBAR
362 const_iptr_t labels[] =
364 (const_iptr_t)LABEL_BACK,
365 (const_iptr_t)LABEL_UPARROW,
366 (const_iptr_t)LABEL_DOWNARROW,
371 * Initialize menu bar
373 if (menu->flags & MF_TOPLEVEL)
374 labels[0] = (const_iptr_t)LABEL_EMPTY;
376 mbar_init(&mb, menu->bitmap, labels, countof(labels));
377 #endif /* CONFIG_MENU_MENUBAR */
381 (menu->bitmap->height / menu->bitmap->font->height)
382 #if CONFIG_MENU_MENUBAR
383 - 1 /* menu bar labels */
385 - (menu->title ? 1 : 0);
387 /* Selected item should be a visible entry */
388 first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
391 text_clear(menu->bitmap);
398 * Keep selected item visible
400 while (selected < first_item)
401 first_item = menu_prev_visible_item(menu, first_item);
402 while (selected >= first_item + items_per_page)
403 first_item = menu_next_visible_item(menu, first_item);
405 menu_layout(menu, first_item, items_per_page, selected);
407 #if CONFIG_MENU_MENUBAR
408 menu_update_menubar(menu, &mb, selected);
411 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
417 #if (CONFIG_MENU_TIMEOUT != 0)
418 /* Reset idle timer on key press. */
421 menu_idle_time = now;
426 struct MenuItem *item = &(menu->items[selected]);
429 if (menu->flags & MF_ROMITEMS)
431 memcpy_P(&ram_item, item, sizeof(ram_item));
435 menu_doselect(menu, item);
437 /* Return userdata as result */
438 if (!menu->flags & MF_STICKY)
440 /* Store currently selected item before leaving. */
441 if (menu->flags & MF_SAVESEL)
442 CONST_CAST(struct Menu *, menu)->selected = selected;
443 return item->userdata;
447 text_clear(menu->bitmap);
451 selected = menu_prev_visible_item(menu, selected);
453 else if (key & K_DOWN)
455 selected = menu_next_visible_item(menu, selected);
457 else if (((key & K_CANCEL)
458 #if CONFIG_MENU_TIMEOUT != 0
459 || (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
461 ) && !(menu->flags & MF_TOPLEVEL)
464 /* Store currently selected item before leaving. */
465 if (menu->flags & MF_SAVESEL)
466 CONST_CAST(struct Menu *, menu)->selected = selected;
474 * Set flags on a menuitem.
476 * \param menu Menu owner of the item to change.
477 * \param idx Index of the menu item.
478 * \param flags Bit mask of the flags to set.
482 int menu_setFlags(struct Menu *menu, int idx, int flags)
484 ASSERT(idx < menu_count(menu));
485 ASSERT(!(menu->flags & MF_ROMITEMS));
487 int old = menu->items[idx].flags;
488 menu->items[idx].flags |= flags;
494 * Clear flags on a menuitem.
496 * \param menu Menu owner of the item to change.
497 * \param idx Index of the menu item.
498 * \param flags Bit mask of the flags to clear.
502 int menu_clearFlags(struct Menu *menu, int idx, int flags)
504 ASSERT(idx < menu_count(menu));
505 ASSERT(!(menu->flags & MF_ROMITEMS));
507 int old = menu->items[idx].flags;
508 menu->items[idx].flags &= ~flags;