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.6 2006/07/19 12:56:27 bernie
20 *#* Convert to new Doxygen style.
22 *#* Revision 1.5 2006/06/03 13:58:01 bernie
23 *#* Fix recursive timeout and add exit status information.
25 *#* Revision 1.4 2006/06/02 12:26:18 bernie
26 *#* Draw graphical checkmarks.
28 *#* Revision 1.3 2006/05/28 15:03:31 bernie
29 *#* Avoid unnecessary rendering.
31 *#* Revision 1.2 2006/05/25 23:34:38 bernie
32 *#* Implement menu timeouts.
34 *#* Revision 1.1 2006/05/15 07:20:54 bernie
35 *#* Move menu to gui/.
37 *#* Revision 1.7 2006/04/27 05:39:24 bernie
38 *#* Enhance text rendering to arbitrary x,y coords.
40 *#* Revision 1.6 2006/04/11 00:07:32 bernie
41 *#* Implemenent MF_SAVESEL flag.
43 *#* Revision 1.5 2006/03/22 09:49:51 bernie
44 *#* Simplifications from project_grl.
46 *#* Revision 1.4 2006/03/20 17:48:35 bernie
47 *#* Implement support for ROM menus.
49 *#* Revision 1.3 2006/02/20 14:34:32 bernie
50 *#* Include appconfig.h before using its definitions.
52 *#* Revision 1.2 2006/02/15 09:10:51 bernie
53 *#* Make title bold; Fix height when we have no menubar.
61 #include <cfg/compiler.h>
62 #include <cfg/debug.h>
63 #include <appconfig.h>
64 #include <string.h> /* strcpy() */
67 #include <avr/pgmspace.h> /* strncpy_P() */
70 #if CONFIG_MENU_SMOOTH
71 #include <drv/lcd_gfx.h>
74 #if (CONFIG_MENU_TIMEOUT != 0)
75 #include <drv/timer.h>
78 #if CONFIG_MENU_MENUBAR
82 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
85 #define PTRMSG(x) ((const char *)x)
88 /* Temporary fake defines for ABORT stuff... */
90 #define PUSH_ABORT false
91 #define POP_ABORT do {} while(0)
92 #define DO_ABORT do {} while(0)
96 * Return the total number of items in in a menu.
98 static int menu_count(const struct Menu *menu)
102 for (cnt = 0; /*NOP*/; ++cnt)
104 const MenuItem *item = &menu->items[cnt];
107 if (menu->flags & MF_ROMITEMS)
109 memcpy_P(&ram_item, item, sizeof(ram_item));
113 if (!(item->label || item->hook))
120 #if CONFIG_MENU_MENUBAR
123 * Update the menu bar according to the selected item and redraw it.
125 static void menu_update_menubar(
126 const struct Menu *menu,
132 if (menu->flags & MF_ROMITEMS)
134 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
135 item_flags = pgm_read_int(&menu->items[selected].flags);
139 item_flags = menu->items[selected].flags;
141 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
143 if (item_flags & MIF_DISABLED)
144 newlabel = (const_iptr_t)LABEL_EMPTY;
145 else if (item_flags & MIF_TOGGLE)
146 newlabel = (const_iptr_t)LABEL_SEL;
147 else if (item_flags & MIF_CHECKIT)
149 newlabel = (item_flags & MIF_CHECKED) ?
150 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
153 mb->labels[3] = newlabel;
156 #endif /* CONFIG_MENU_MENUBAR */
160 * Show a menu on the display.
162 static void menu_layout(
163 const struct Menu *menu,
170 const char * PROGMEM title = PTRMSG(menu->title);
171 Bitmap *bm = menu->bitmap;
179 text_clear(menu->bitmap);
186 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
187 ypos += bm->font->height;
190 #if CONFIG_MENU_SMOOTH
191 static coord_t yoffset = 0;
192 static int old_first_item = 0;
194 coord_t old_ymin = bm->cr.ymin;
196 /* Clip drawing inside menu items area */
198 bm->cr.xmin, bm->cr.ymin + ypos,
199 bm->cr.xmax, bm->cr.ymax);
201 if (old_first_item != first_item)
203 /* Speed proportional to distance */
204 speed = ABS(old_first_item - first_item) * 3;
206 if (old_first_item > first_item)
209 if (yoffset > bm->font->height)
218 if (yoffset < -bm->font->height)
224 first_item = MIN(old_first_item, menu_count(menu));
229 #endif /* CONFIG_MENU_SMOOTH */
231 if (redraw) for (i = first_item; /**/; ++i)
233 const MenuItem *item = &menu->items[i];
236 if (menu->flags & MF_ROMITEMS)
238 memcpy_P(&ram_item, item, sizeof(ram_item));
241 #endif /* CPU_HARVARD */
243 /* Check for end of room */
244 if (ypos > bm->cr.ymax)
247 /* Check for end of menu */
248 if (!(item->label || item->hook))
251 /* Only print visible items */
252 if (!(item->flags & MIF_HIDDEN))
254 if (item->flags & MIF_CHECKIT)
256 gfx_rectClear(bm, 0, ypos,
257 bm->font->height, ypos + bm->font->height);
259 if (item->flags & MIF_TOGGLE)
260 gfx_rectDraw(bm, 2, ypos + 2,
261 bm->font->height - 2, ypos + bm->font->height - 2);
262 if (item->flags & MIF_CHECKED)
266 bm->font->height - 3, ypos + bm->font->height - 3);
268 bm->font->height - 3, ypos + 3,
269 3, ypos + bm->font->height - 3);
274 ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
279 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
280 (i == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
284 ypos += bm->font->height;
288 #if CONFIG_MENU_SMOOTH
291 /* Clear rest of area */
292 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
294 lcd_blitBitmap(&lcd_bitmap);
297 /* Restore old cliprect */
299 bm->cr.xmin, old_ymin,
300 bm->cr.xmax, bm->cr.ymax);
302 #endif /* CONFIG_MENU_SMOOTH */
307 * Handle menu item selection
309 static iptr_t menu_doselect(const struct Menu *menu, struct MenuItem *item)
313 /* Exclude other items */
315 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
318 menu->items[i].flags &= ~MIF_CHECKED;
321 if (item->flags & MIF_DISABLED)
322 return MENU_DISABLED;
324 /* Handle checkable items */
325 if (item->flags & MIF_TOGGLE)
326 item->flags ^= MIF_CHECKED;
327 else if (item->flags & MIF_CHECKIT)
328 item->flags |= MIF_CHECKED;
330 /* Handle items with callback hooks */
333 /* Push a jmp buffer to abort the operation with the STOP/CANCEL key */
336 result = item->hook(item->userdata);
341 result = item->userdata;
348 * Return the next visible item (rolls back to the first item)
350 static int menu_next_visible_item(const struct Menu *menu, int index)
352 int total = menu_count(menu);
357 if (++index >= total)
361 if (menu->flags & MF_ROMITEMS)
363 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
364 item_flags = pgm_read_int(&menu->items[index].flags);
368 item_flags = menu->items[index].flags;
370 while (item_flags & MIF_HIDDEN);
377 * Return the previous visible item (rolls back to the last item)
379 static int menu_prev_visible_item(const struct Menu *menu, int index)
381 int total = menu_count(menu);
390 if (menu->flags & MF_ROMITEMS)
392 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
393 item_flags = pgm_read_int(&menu->items[index].flags);
397 item_flags = menu->items[index].flags;
399 while (item_flags & MIF_HIDDEN);
406 * Handle a menu and invoke hook functions for the selected menu items.
408 iptr_t menu_handle(const struct Menu *menu)
410 uint8_t items_per_page;
411 uint8_t first_item = 0;
416 #if (CONFIG_MENU_TIMEOUT != 0)
417 ticks_t now, menu_idle_time = timer_clock();
420 #if CONFIG_MENU_MENUBAR
422 const_iptr_t labels[] =
424 (const_iptr_t)LABEL_BACK,
425 (const_iptr_t)LABEL_UPARROW,
426 (const_iptr_t)LABEL_DOWNARROW,
431 * Initialize menu bar
433 if (menu->flags & MF_TOPLEVEL)
434 labels[0] = (const_iptr_t)LABEL_EMPTY;
436 mbar_init(&mb, menu->bitmap, labels, countof(labels));
437 #endif /* CONFIG_MENU_MENUBAR */
441 (menu->bitmap->height / menu->bitmap->font->height)
442 #if CONFIG_MENU_MENUBAR
443 - 1 /* menu bar labels */
445 - (menu->title ? 1 : 0);
447 /* Selected item should be a visible entry */
448 //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
449 selected = menu->selected;
457 * Keep selected item visible
459 while (selected < first_item)
460 first_item = menu_prev_visible_item(menu, first_item);
461 while (selected >= first_item + items_per_page)
462 first_item = menu_next_visible_item(menu, first_item);
464 menu_layout(menu, first_item, selected, redraw);
467 #if CONFIG_MENU_MENUBAR
468 menu_update_menubar(menu, &mb, selected);
471 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
477 #if (CONFIG_MENU_TIMEOUT != 0)
478 /* Reset idle timer on key press. */
481 menu_idle_time = now;
486 struct MenuItem *item = &(menu->items[selected]);
489 if (menu->flags & MF_ROMITEMS)
491 memcpy_P(&ram_item, item, sizeof(ram_item));
495 result = menu_doselect(menu, item);
498 /* Return immediately */
499 if (!(menu->flags & MF_STICKY))
502 #if (CONFIG_MENU_TIMEOUT != 0)
504 if ((result == MENU_TIMEOUT) && !(menu->flags & MF_TOPLEVEL))
508 menu_idle_time = timer_clock();
513 selected = menu_prev_visible_item(menu, selected);
516 else if (key & K_DOWN)
518 selected = menu_next_visible_item(menu, selected);
521 else if (!(menu->flags & MF_TOPLEVEL))
525 result = MENU_CANCEL;
529 #if CONFIG_MENU_TIMEOUT != 0
530 if (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
532 result = MENU_TIMEOUT;
539 /* Store currently selected item before leaving. */
540 if (menu->flags & MF_SAVESEL)
541 CONST_CAST(struct Menu *, menu)->selected = selected;
548 * Set flags on a menuitem.
550 * \param menu Menu owner of the item to change.
551 * \param idx Index of the menu item.
552 * \param flags Bit mask of the flags to set.
556 int menu_setFlags(struct Menu *menu, int idx, int flags)
558 ASSERT(idx < menu_count(menu));
559 ASSERT(!(menu->flags & MF_ROMITEMS));
561 int old = menu->items[idx].flags;
562 menu->items[idx].flags |= flags;
568 * Clear flags on a menuitem.
570 * \param menu Menu owner of the item to change.
571 * \param idx Index of the menu item.
572 * \param flags Bit mask of the flags to clear.
576 int menu_clearFlags(struct Menu *menu, int idx, int flags)
578 ASSERT(idx < menu_count(menu));
579 ASSERT(!(menu->flags & MF_ROMITEMS));
581 int old = menu->items[idx].flags;
582 menu->items[idx].flags &= ~flags;