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.5 2006/06/03 13:58:01 bernie
20 *#* Fix recursive timeout and add exit status information.
22 *#* Revision 1.4 2006/06/02 12:26:18 bernie
23 *#* Draw graphical checkmarks.
25 *#* Revision 1.3 2006/05/28 15:03:31 bernie
26 *#* Avoid unnecessary rendering.
28 *#* Revision 1.2 2006/05/25 23:34:38 bernie
29 *#* Implement menu timeouts.
31 *#* Revision 1.1 2006/05/15 07:20:54 bernie
32 *#* Move menu to gui/.
34 *#* Revision 1.7 2006/04/27 05:39:24 bernie
35 *#* Enhance text rendering to arbitrary x,y coords.
37 *#* Revision 1.6 2006/04/11 00:07:32 bernie
38 *#* Implemenent MF_SAVESEL flag.
40 *#* Revision 1.5 2006/03/22 09:49:51 bernie
41 *#* Simplifications from project_grl.
43 *#* Revision 1.4 2006/03/20 17:48:35 bernie
44 *#* Implement support for ROM menus.
46 *#* Revision 1.3 2006/02/20 14:34:32 bernie
47 *#* Include appconfig.h before using its definitions.
49 *#* Revision 1.2 2006/02/15 09:10:51 bernie
50 *#* Make title bold; Fix height when we have no menubar.
58 #include <cfg/compiler.h>
59 #include <cfg/debug.h>
60 #include <appconfig.h>
61 #include <string.h> /* strcpy() */
64 #include <avr/pgmspace.h> /* strncpy_P() */
67 #if CONFIG_MENU_SMOOTH
68 #include <drv/lcd_gfx.h>
71 #if (CONFIG_MENU_TIMEOUT != 0)
72 #include <drv/timer.h>
75 #if CONFIG_MENU_MENUBAR
79 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
82 #define PTRMSG(x) ((const char *)x)
85 /* Temporary fake defines for ABORT stuff... */
87 #define PUSH_ABORT false
88 #define POP_ABORT do {} while(0)
89 #define DO_ABORT do {} while(0)
93 * Return the total number of items in in a menu.
95 static int menu_count(const struct Menu *menu)
99 for (cnt = 0; /*NOP*/; ++cnt)
101 const MenuItem *item = &menu->items[cnt];
104 if (menu->flags & MF_ROMITEMS)
106 memcpy_P(&ram_item, item, sizeof(ram_item));
110 if (!(item->label || item->hook))
117 #if CONFIG_MENU_MENUBAR
120 * Update the menu bar according to the selected item and redraw it.
122 static void menu_update_menubar(
123 const struct Menu *menu,
129 if (menu->flags & MF_ROMITEMS)
131 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
132 item_flags = pgm_read_int(&menu->items[selected].flags);
136 item_flags = menu->items[selected].flags;
138 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
140 if (item_flags & MIF_DISABLED)
141 newlabel = (const_iptr_t)LABEL_EMPTY;
142 else if (item_flags & MIF_TOGGLE)
143 newlabel = (const_iptr_t)LABEL_SEL;
144 else if (item_flags & MIF_CHECKIT)
146 newlabel = (item_flags & MIF_CHECKED) ?
147 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
150 mb->labels[3] = newlabel;
153 #endif /* CONFIG_MENU_MENUBAR */
157 * Show a menu on the display.
159 static void menu_layout(
160 const struct Menu *menu,
167 const char * PROGMEM title = PTRMSG(menu->title);
168 Bitmap *bm = menu->bitmap;
176 text_clear(menu->bitmap);
183 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
184 ypos += bm->font->height;
187 #if CONFIG_MENU_SMOOTH
188 static coord_t yoffset = 0;
189 static int old_first_item = 0;
191 coord_t old_ymin = bm->cr.ymin;
193 /* Clip drawing inside menu items area */
195 bm->cr.xmin, bm->cr.ymin + ypos,
196 bm->cr.xmax, bm->cr.ymax);
198 if (old_first_item != first_item)
200 /* Speed proportional to distance */
201 speed = ABS(old_first_item - first_item) * 3;
203 if (old_first_item > first_item)
206 if (yoffset > bm->font->height)
215 if (yoffset < -bm->font->height)
221 first_item = MIN(old_first_item, menu_count(menu));
226 #endif /* CONFIG_MENU_SMOOTH */
228 if (redraw) for (i = first_item; /**/; ++i)
230 const MenuItem *item = &menu->items[i];
233 if (menu->flags & MF_ROMITEMS)
235 memcpy_P(&ram_item, item, sizeof(ram_item));
238 #endif /* CPU_HARVARD */
240 /* Check for end of room */
241 if (ypos > bm->cr.ymax)
244 /* Check for end of menu */
245 if (!(item->label || item->hook))
248 /* Only print visible items */
249 if (!(item->flags & MIF_HIDDEN))
251 if (item->flags & MIF_CHECKIT)
253 gfx_rectClear(bm, 0, ypos,
254 bm->font->height, ypos + bm->font->height);
256 if (item->flags & MIF_TOGGLE)
257 gfx_rectDraw(bm, 2, ypos + 2,
258 bm->font->height - 2, ypos + bm->font->height - 2);
259 if (item->flags & MIF_CHECKED)
263 bm->font->height - 3, ypos + bm->font->height - 3);
265 bm->font->height - 3, ypos + 3,
266 3, ypos + bm->font->height - 3);
271 ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
276 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
277 (i == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
281 ypos += bm->font->height;
285 #if CONFIG_MENU_SMOOTH
288 /* Clear rest of area */
289 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
291 lcd_blitBitmap(&lcd_bitmap);
294 /* Restore old cliprect */
296 bm->cr.xmin, old_ymin,
297 bm->cr.xmax, bm->cr.ymax);
299 #endif /* CONFIG_MENU_SMOOTH */
304 * Handle menu item selection
306 static iptr_t menu_doselect(const struct Menu *menu, struct MenuItem *item)
310 /* Exclude other items */
312 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
315 menu->items[i].flags &= ~MIF_CHECKED;
318 if (item->flags & MIF_DISABLED)
319 return MENU_DISABLED;
321 /* Handle checkable items */
322 if (item->flags & MIF_TOGGLE)
323 item->flags ^= MIF_CHECKED;
324 else if (item->flags & MIF_CHECKIT)
325 item->flags |= MIF_CHECKED;
327 /* Handle items with callback hooks */
330 /* Push a jmp buffer to abort the operation with the STOP/CANCEL key */
333 result = item->hook(item->userdata);
338 result = item->userdata;
345 * Return the next visible item (rolls back to the first item)
347 static int menu_next_visible_item(const struct Menu *menu, int index)
349 int total = menu_count(menu);
354 if (++index >= total)
358 if (menu->flags & MF_ROMITEMS)
360 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
361 item_flags = pgm_read_int(&menu->items[index].flags);
365 item_flags = menu->items[index].flags;
367 while (item_flags & MIF_HIDDEN);
374 * Return the previous visible item (rolls back to the last item)
376 static int menu_prev_visible_item(const struct Menu *menu, int index)
378 int total = menu_count(menu);
387 if (menu->flags & MF_ROMITEMS)
389 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
390 item_flags = pgm_read_int(&menu->items[index].flags);
394 item_flags = menu->items[index].flags;
396 while (item_flags & MIF_HIDDEN);
403 * Handle a menu and invoke hook functions for the selected menu items.
405 iptr_t menu_handle(const struct Menu *menu)
407 uint8_t items_per_page;
408 uint8_t first_item = 0;
413 #if (CONFIG_MENU_TIMEOUT != 0)
414 ticks_t now, menu_idle_time = timer_clock();
417 #if CONFIG_MENU_MENUBAR
419 const_iptr_t labels[] =
421 (const_iptr_t)LABEL_BACK,
422 (const_iptr_t)LABEL_UPARROW,
423 (const_iptr_t)LABEL_DOWNARROW,
428 * Initialize menu bar
430 if (menu->flags & MF_TOPLEVEL)
431 labels[0] = (const_iptr_t)LABEL_EMPTY;
433 mbar_init(&mb, menu->bitmap, labels, countof(labels));
434 #endif /* CONFIG_MENU_MENUBAR */
438 (menu->bitmap->height / menu->bitmap->font->height)
439 #if CONFIG_MENU_MENUBAR
440 - 1 /* menu bar labels */
442 - (menu->title ? 1 : 0);
444 /* Selected item should be a visible entry */
445 //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
446 selected = menu->selected;
454 * Keep selected item visible
456 while (selected < first_item)
457 first_item = menu_prev_visible_item(menu, first_item);
458 while (selected >= first_item + items_per_page)
459 first_item = menu_next_visible_item(menu, first_item);
461 menu_layout(menu, first_item, selected, redraw);
464 #if CONFIG_MENU_MENUBAR
465 menu_update_menubar(menu, &mb, selected);
468 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
474 #if (CONFIG_MENU_TIMEOUT != 0)
475 /* Reset idle timer on key press. */
478 menu_idle_time = now;
483 struct MenuItem *item = &(menu->items[selected]);
486 if (menu->flags & MF_ROMITEMS)
488 memcpy_P(&ram_item, item, sizeof(ram_item));
492 result = menu_doselect(menu, item);
495 /* Return immediately */
496 if (!(menu->flags & MF_STICKY))
499 #if (CONFIG_MENU_TIMEOUT != 0)
501 if ((result == MENU_TIMEOUT) && !(menu->flags & MF_TOPLEVEL))
505 menu_idle_time = timer_clock();
510 selected = menu_prev_visible_item(menu, selected);
513 else if (key & K_DOWN)
515 selected = menu_next_visible_item(menu, selected);
518 else if (!(menu->flags & MF_TOPLEVEL))
522 result = MENU_CANCEL;
526 #if CONFIG_MENU_TIMEOUT != 0
527 if (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
529 result = MENU_TIMEOUT;
536 /* Store currently selected item before leaving. */
537 if (menu->flags & MF_SAVESEL)
538 CONST_CAST(struct Menu *, menu)->selected = selected;
545 * Set flags on a menuitem.
547 * \param menu Menu owner of the item to change.
548 * \param idx Index of the menu item.
549 * \param flags Bit mask of the flags to set.
553 int menu_setFlags(struct Menu *menu, int idx, int flags)
555 ASSERT(idx < menu_count(menu));
556 ASSERT(!(menu->flags & MF_ROMITEMS));
558 int old = menu->items[idx].flags;
559 menu->items[idx].flags |= flags;
565 * Clear flags on a menuitem.
567 * \param menu Menu owner of the item to change.
568 * \param idx Index of the menu item.
569 * \param flags Bit mask of the flags to clear.
573 int menu_clearFlags(struct Menu *menu, int idx, int flags)
575 ASSERT(idx < menu_count(menu));
576 ASSERT(!(menu->flags & MF_ROMITEMS));
578 int old = menu->items[idx].flags;
579 menu->items[idx].flags &= ~flags;