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.3 2006/05/28 15:03:31 bernie
20 *#* Avoid unnecessary rendering.
22 *#* Revision 1.2 2006/05/25 23:34:38 bernie
23 *#* Implement menu timeouts.
25 *#* Revision 1.1 2006/05/15 07:20:54 bernie
26 *#* Move menu to gui/.
28 *#* Revision 1.7 2006/04/27 05:39:24 bernie
29 *#* Enhance text rendering to arbitrary x,y coords.
31 *#* Revision 1.6 2006/04/11 00:07:32 bernie
32 *#* Implemenent MF_SAVESEL flag.
34 *#* Revision 1.5 2006/03/22 09:49:51 bernie
35 *#* Simplifications from project_grl.
37 *#* Revision 1.4 2006/03/20 17:48:35 bernie
38 *#* Implement support for ROM menus.
40 *#* Revision 1.3 2006/02/20 14:34:32 bernie
41 *#* Include appconfig.h before using its definitions.
43 *#* Revision 1.2 2006/02/15 09:10:51 bernie
44 *#* Make title bold; Fix height when we have no menubar.
52 #include <cfg/compiler.h>
53 #include <cfg/debug.h>
54 #include <appconfig.h>
55 #include <string.h> /* strcpy() */
58 #include <avr/pgmspace.h> /* strncpy_P() */
61 #if CONFIG_MENU_SMOOTH
62 #include <drv/lcd_gfx.h>
65 #if (CONFIG_MENU_TIMEOUT != 0)
66 #include <drv/timer.h>
69 #if CONFIG_MENU_MENUBAR
73 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
76 #define PTRMSG(x) ((const char *)x)
79 /* Temporary fake defines for ABORT stuff... */
81 #define PUSH_ABORT false
82 #define POP_ABORT do {} while(0)
83 #define DO_ABORT do {} while(0)
87 * Return the total number of items in in a menu.
89 static int menu_count(const struct Menu *menu)
93 for (cnt = 0; /*NOP*/; ++cnt)
95 const MenuItem *item = &menu->items[cnt];
98 if (menu->flags & MF_ROMITEMS)
100 memcpy_P(&ram_item, item, sizeof(ram_item));
104 if (!(item->label || item->hook))
111 #if CONFIG_MENU_MENUBAR
114 * Update the menu bar according to the selected item
117 static void menu_update_menubar(
118 const struct Menu *menu,
124 if (menu->flags & MF_ROMITEMS)
126 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
127 item_flags = pgm_read_int(&menu->items[selected].flags);
131 item_flags = menu->items[selected].flags;
133 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
135 if (item_flags & MIF_DISABLED)
136 newlabel = (const_iptr_t)LABEL_EMPTY;
137 else if (item_flags & MIF_TOGGLE)
138 newlabel = (const_iptr_t)LABEL_SEL;
139 else if (item_flags & MIF_CHECKIT)
141 newlabel = (item_flags & MIF_CHECKED) ?
142 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
145 mb->labels[3] = newlabel;
148 #endif /* CONFIG_MENU_MENUBAR */
152 * Show a menu on the display.
154 static void menu_layout(
155 const struct Menu *menu,
162 const char * PROGMEM title = PTRMSG(menu->title);
163 Bitmap *bm = menu->bitmap;
171 text_clear(menu->bitmap);
178 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
179 ypos += bm->font->height;
182 #if CONFIG_MENU_SMOOTH
183 static coord_t yoffset = 0;
184 static int old_first_item = 0;
186 coord_t old_ymin = bm->cr.ymin;
188 /* Clip drawing inside menu items area */
190 bm->cr.xmin, bm->cr.ymin + ypos,
191 bm->cr.xmax, bm->cr.ymax);
193 if (old_first_item != first_item)
195 /* Speed proportional to distance */
196 speed = ABS(old_first_item - first_item) * 3;
198 if (old_first_item > first_item)
201 if (yoffset > bm->font->height)
210 if (yoffset < -bm->font->height)
216 first_item = MIN(old_first_item, menu_count(menu));
221 #endif /* CONFIG_MENU_SMOOTH */
223 if (redraw) for (i = first_item; /**/; ++i)
225 const MenuItem *item = &menu->items[i];
228 if (menu->flags & MF_ROMITEMS)
230 memcpy_P(&ram_item, item, sizeof(ram_item));
235 /* Check for end of room */
236 if (ypos > bm->cr.ymax)
239 /* Check for end of menu */
240 if (!(item->label || item->hook))
243 /* Only print visible items */
244 if (!(item->flags & MIF_HIDDEN))
253 (i == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
254 (item->flags & MIF_RAMLABEL) ? PSTR("%s%S") : PSTR("%S%S"),
256 (item->flags & MIF_TOGGLE) ?
257 ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
258 : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
260 ypos += bm->font->height;
264 #if CONFIG_MENU_SMOOTH
267 /* Clear rest of area */
268 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
270 lcd_blitBitmap(&lcd_bitmap);
273 /* Restore old cliprect */
275 bm->cr.xmin, old_ymin,
276 bm->cr.xmax, bm->cr.ymax);
282 * Handle menu item selection
284 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
286 /* Exclude other items */
288 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
291 menu->items[i].flags &= ~MIF_CHECKED;
294 if (item->flags & MIF_DISABLED)
297 /* Handle checkable items */
298 if (item->flags & MIF_TOGGLE)
299 item->flags ^= MIF_CHECKED;
300 else if (item->flags & MIF_CHECKIT)
301 item->flags |= MIF_CHECKED;
303 /* Handle items with callback hooks */
306 /* Push a jmp buffer to abort the operation with the STOP key */
309 item->hook(item->userdata);
317 * Return the next visible item (rolls back to the first item)
319 static int menu_next_visible_item(const struct Menu *menu, int index)
321 int total = menu_count(menu);
326 if (++index >= total)
330 if (menu->flags & MF_ROMITEMS)
332 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
333 item_flags = pgm_read_int(&menu->items[index].flags);
337 item_flags = menu->items[index].flags;
339 while (item_flags & MIF_HIDDEN);
346 * Return the previous visible item (rolls back to the last item)
348 static int menu_prev_visible_item(const struct Menu *menu, int index)
350 int total = menu_count(menu);
359 if (menu->flags & MF_ROMITEMS)
361 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
362 item_flags = pgm_read_int(&menu->items[index].flags);
366 item_flags = menu->items[index].flags;
368 while (item_flags & MIF_HIDDEN);
375 * Handle a menu and invoke hook functions for the selected menu items.
377 iptr_t menu_handle(const struct Menu *menu)
379 uint8_t items_per_page;
380 uint8_t first_item = 0;
384 #if (CONFIG_MENU_TIMEOUT != 0)
385 ticks_t now, menu_idle_time = timer_clock();
388 #if CONFIG_MENU_MENUBAR
390 const_iptr_t labels[] =
392 (const_iptr_t)LABEL_BACK,
393 (const_iptr_t)LABEL_UPARROW,
394 (const_iptr_t)LABEL_DOWNARROW,
399 * Initialize menu bar
401 if (menu->flags & MF_TOPLEVEL)
402 labels[0] = (const_iptr_t)LABEL_EMPTY;
404 mbar_init(&mb, menu->bitmap, labels, countof(labels));
405 #endif /* CONFIG_MENU_MENUBAR */
409 (menu->bitmap->height / menu->bitmap->font->height)
410 #if CONFIG_MENU_MENUBAR
411 - 1 /* menu bar labels */
413 - (menu->title ? 1 : 0);
415 /* Selected item should be a visible entry */
416 //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
417 selected = menu->selected;
425 * Keep selected item visible
427 while (selected < first_item)
428 first_item = menu_prev_visible_item(menu, first_item);
429 while (selected >= first_item + items_per_page)
430 first_item = menu_next_visible_item(menu, first_item);
432 menu_layout(menu, first_item, selected, redraw);
435 #if CONFIG_MENU_MENUBAR
436 menu_update_menubar(menu, &mb, selected);
439 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
445 #if (CONFIG_MENU_TIMEOUT != 0)
446 /* Reset idle timer on key press. */
449 menu_idle_time = now;
454 struct MenuItem *item = &(menu->items[selected]);
457 if (menu->flags & MF_ROMITEMS)
459 memcpy_P(&ram_item, item, sizeof(ram_item));
463 menu_doselect(menu, item);
466 /* Return userdata as result */
467 if (!menu->flags & MF_STICKY)
469 /* Store currently selected item before leaving. */
470 if (menu->flags & MF_SAVESEL)
471 CONST_CAST(struct Menu *, menu)->selected = selected;
472 return item->userdata;
477 selected = menu_prev_visible_item(menu, selected);
480 else if (key & K_DOWN)
482 selected = menu_next_visible_item(menu, selected);
485 else if (((key & K_CANCEL)
486 #if CONFIG_MENU_TIMEOUT != 0
487 || (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
489 ) && !(menu->flags & MF_TOPLEVEL)
492 /* Store currently selected item before leaving. */
493 if (menu->flags & MF_SAVESEL)
494 CONST_CAST(struct Menu *, menu)->selected = selected;
502 * Set flags on a menuitem.
504 * \param menu Menu owner of the item to change.
505 * \param idx Index of the menu item.
506 * \param flags Bit mask of the flags to set.
510 int menu_setFlags(struct Menu *menu, int idx, int flags)
512 ASSERT(idx < menu_count(menu));
513 ASSERT(!(menu->flags & MF_ROMITEMS));
515 int old = menu->items[idx].flags;
516 menu->items[idx].flags |= flags;
522 * Clear flags on a menuitem.
524 * \param menu Menu owner of the item to change.
525 * \param idx Index of the menu item.
526 * \param flags Bit mask of the flags to clear.
530 int menu_clearFlags(struct Menu *menu, int idx, int flags)
532 ASSERT(idx < menu_count(menu));
533 ASSERT(!(menu->flags & MF_ROMITEMS));
535 int old = menu->items[idx].flags;
536 menu->items[idx].flags &= ~flags;