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.4 2006/06/02 12:26:18 bernie
20 *#* Draw graphical checkmarks.
22 *#* Revision 1.3 2006/05/28 15:03:31 bernie
23 *#* Avoid unnecessary rendering.
25 *#* Revision 1.2 2006/05/25 23:34:38 bernie
26 *#* Implement menu timeouts.
28 *#* Revision 1.1 2006/05/15 07:20:54 bernie
29 *#* Move menu to gui/.
31 *#* Revision 1.7 2006/04/27 05:39:24 bernie
32 *#* Enhance text rendering to arbitrary x,y coords.
34 *#* Revision 1.6 2006/04/11 00:07:32 bernie
35 *#* Implemenent MF_SAVESEL flag.
37 *#* Revision 1.5 2006/03/22 09:49:51 bernie
38 *#* Simplifications from project_grl.
40 *#* Revision 1.4 2006/03/20 17:48:35 bernie
41 *#* Implement support for ROM menus.
43 *#* Revision 1.3 2006/02/20 14:34:32 bernie
44 *#* Include appconfig.h before using its definitions.
46 *#* Revision 1.2 2006/02/15 09:10:51 bernie
47 *#* Make title bold; Fix height when we have no menubar.
55 #include <cfg/compiler.h>
56 #include <cfg/debug.h>
57 #include <appconfig.h>
58 #include <string.h> /* strcpy() */
61 #include <avr/pgmspace.h> /* strncpy_P() */
64 #if CONFIG_MENU_SMOOTH
65 #include <drv/lcd_gfx.h>
68 #if (CONFIG_MENU_TIMEOUT != 0)
69 #include <drv/timer.h>
72 #if CONFIG_MENU_MENUBAR
76 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
79 #define PTRMSG(x) ((const char *)x)
82 /* Temporary fake defines for ABORT stuff... */
84 #define PUSH_ABORT false
85 #define POP_ABORT do {} while(0)
86 #define DO_ABORT do {} while(0)
90 * Return the total number of items in in a menu.
92 static int menu_count(const struct Menu *menu)
96 for (cnt = 0; /*NOP*/; ++cnt)
98 const MenuItem *item = &menu->items[cnt];
101 if (menu->flags & MF_ROMITEMS)
103 memcpy_P(&ram_item, item, sizeof(ram_item));
107 if (!(item->label || item->hook))
114 #if CONFIG_MENU_MENUBAR
117 * Update the menu bar according to the selected item
120 static void menu_update_menubar(
121 const struct Menu *menu,
127 if (menu->flags & MF_ROMITEMS)
129 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
130 item_flags = pgm_read_int(&menu->items[selected].flags);
134 item_flags = menu->items[selected].flags;
136 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
138 if (item_flags & MIF_DISABLED)
139 newlabel = (const_iptr_t)LABEL_EMPTY;
140 else if (item_flags & MIF_TOGGLE)
141 newlabel = (const_iptr_t)LABEL_SEL;
142 else if (item_flags & MIF_CHECKIT)
144 newlabel = (item_flags & MIF_CHECKED) ?
145 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
148 mb->labels[3] = newlabel;
151 #endif /* CONFIG_MENU_MENUBAR */
155 * Show a menu on the display.
157 static void menu_layout(
158 const struct Menu *menu,
165 const char * PROGMEM title = PTRMSG(menu->title);
166 Bitmap *bm = menu->bitmap;
174 text_clear(menu->bitmap);
181 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
182 ypos += bm->font->height;
185 #if CONFIG_MENU_SMOOTH
186 static coord_t yoffset = 0;
187 static int old_first_item = 0;
189 coord_t old_ymin = bm->cr.ymin;
191 /* Clip drawing inside menu items area */
193 bm->cr.xmin, bm->cr.ymin + ypos,
194 bm->cr.xmax, bm->cr.ymax);
196 if (old_first_item != first_item)
198 /* Speed proportional to distance */
199 speed = ABS(old_first_item - first_item) * 3;
201 if (old_first_item > first_item)
204 if (yoffset > bm->font->height)
213 if (yoffset < -bm->font->height)
219 first_item = MIN(old_first_item, menu_count(menu));
224 #endif /* CONFIG_MENU_SMOOTH */
226 if (redraw) for (i = first_item; /**/; ++i)
228 const MenuItem *item = &menu->items[i];
231 if (menu->flags & MF_ROMITEMS)
233 memcpy_P(&ram_item, item, sizeof(ram_item));
238 /* Check for end of room */
239 if (ypos > bm->cr.ymax)
242 /* Check for end of menu */
243 if (!(item->label || item->hook))
246 /* Only print visible items */
247 if (!(item->flags & MIF_HIDDEN))
249 if (item->flags & MIF_CHECKIT)
251 gfx_rectClear(bm, 0, ypos,
252 bm->font->height, ypos + bm->font->height);
254 if (item->flags & MIF_TOGGLE)
255 gfx_rectDraw(bm, 2, ypos + 2,
256 bm->font->height - 2, ypos + bm->font->height - 2);
257 if (item->flags & MIF_CHECKED)
261 bm->font->height - 3, ypos + bm->font->height - 3);
263 bm->font->height - 3, ypos + 3,
264 3, ypos + bm->font->height - 3);
269 ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
274 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
275 (i == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
279 ypos += bm->font->height;
283 #if CONFIG_MENU_SMOOTH
286 /* Clear rest of area */
287 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
289 lcd_blitBitmap(&lcd_bitmap);
292 /* Restore old cliprect */
294 bm->cr.xmin, old_ymin,
295 bm->cr.xmax, bm->cr.ymax);
301 * Handle menu item selection
303 static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
305 /* Exclude other items */
307 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
310 menu->items[i].flags &= ~MIF_CHECKED;
313 if (item->flags & MIF_DISABLED)
316 /* Handle checkable items */
317 if (item->flags & MIF_TOGGLE)
318 item->flags ^= MIF_CHECKED;
319 else if (item->flags & MIF_CHECKIT)
320 item->flags |= MIF_CHECKED;
322 /* Handle items with callback hooks */
325 /* Push a jmp buffer to abort the operation with the STOP key */
328 item->hook(item->userdata);
336 * Return the next visible item (rolls back to the first item)
338 static int menu_next_visible_item(const struct Menu *menu, int index)
340 int total = menu_count(menu);
345 if (++index >= total)
349 if (menu->flags & MF_ROMITEMS)
351 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
352 item_flags = pgm_read_int(&menu->items[index].flags);
356 item_flags = menu->items[index].flags;
358 while (item_flags & MIF_HIDDEN);
365 * Return the previous visible item (rolls back to the last item)
367 static int menu_prev_visible_item(const struct Menu *menu, int index)
369 int total = menu_count(menu);
378 if (menu->flags & MF_ROMITEMS)
380 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
381 item_flags = pgm_read_int(&menu->items[index].flags);
385 item_flags = menu->items[index].flags;
387 while (item_flags & MIF_HIDDEN);
394 * Handle a menu and invoke hook functions for the selected menu items.
396 iptr_t menu_handle(const struct Menu *menu)
398 uint8_t items_per_page;
399 uint8_t first_item = 0;
403 #if (CONFIG_MENU_TIMEOUT != 0)
404 ticks_t now, menu_idle_time = timer_clock();
407 #if CONFIG_MENU_MENUBAR
409 const_iptr_t labels[] =
411 (const_iptr_t)LABEL_BACK,
412 (const_iptr_t)LABEL_UPARROW,
413 (const_iptr_t)LABEL_DOWNARROW,
418 * Initialize menu bar
420 if (menu->flags & MF_TOPLEVEL)
421 labels[0] = (const_iptr_t)LABEL_EMPTY;
423 mbar_init(&mb, menu->bitmap, labels, countof(labels));
424 #endif /* CONFIG_MENU_MENUBAR */
428 (menu->bitmap->height / menu->bitmap->font->height)
429 #if CONFIG_MENU_MENUBAR
430 - 1 /* menu bar labels */
432 - (menu->title ? 1 : 0);
434 /* Selected item should be a visible entry */
435 //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
436 selected = menu->selected;
444 * Keep selected item visible
446 while (selected < first_item)
447 first_item = menu_prev_visible_item(menu, first_item);
448 while (selected >= first_item + items_per_page)
449 first_item = menu_next_visible_item(menu, first_item);
451 menu_layout(menu, first_item, selected, redraw);
454 #if CONFIG_MENU_MENUBAR
455 menu_update_menubar(menu, &mb, selected);
458 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
464 #if (CONFIG_MENU_TIMEOUT != 0)
465 /* Reset idle timer on key press. */
468 menu_idle_time = now;
473 struct MenuItem *item = &(menu->items[selected]);
476 if (menu->flags & MF_ROMITEMS)
478 memcpy_P(&ram_item, item, sizeof(ram_item));
482 menu_doselect(menu, item);
485 /* Return userdata as result */
486 if (!menu->flags & MF_STICKY)
488 /* Store currently selected item before leaving. */
489 if (menu->flags & MF_SAVESEL)
490 CONST_CAST(struct Menu *, menu)->selected = selected;
491 return item->userdata;
496 selected = menu_prev_visible_item(menu, selected);
499 else if (key & K_DOWN)
501 selected = menu_next_visible_item(menu, selected);
504 else if (((key & K_CANCEL)
505 #if CONFIG_MENU_TIMEOUT != 0
506 || (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
508 ) && !(menu->flags & MF_TOPLEVEL)
511 /* Store currently selected item before leaving. */
512 if (menu->flags & MF_SAVESEL)
513 CONST_CAST(struct Menu *, menu)->selected = selected;
521 * Set flags on a menuitem.
523 * \param menu Menu owner of the item to change.
524 * \param idx Index of the menu item.
525 * \param flags Bit mask of the flags to set.
529 int menu_setFlags(struct Menu *menu, int idx, int flags)
531 ASSERT(idx < menu_count(menu));
532 ASSERT(!(menu->flags & MF_ROMITEMS));
534 int old = menu->items[idx].flags;
535 menu->items[idx].flags |= flags;
541 * Clear flags on a menuitem.
543 * \param menu Menu owner of the item to change.
544 * \param idx Index of the menu item.
545 * \param flags Bit mask of the flags to clear.
549 int menu_clearFlags(struct Menu *menu, int idx, int flags)
551 ASSERT(idx < menu_count(menu));
552 ASSERT(!(menu->flags & MF_ROMITEMS));
554 int old = menu->items[idx].flags;
555 menu->items[idx].flags &= ~flags;