4 * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
6 * This file is part of DevLib - See README.devlib for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
12 * \author Stefano Fedrigo <aleph@develer.com>
14 * \brief General pourpose menu handling functions
19 *#* Revision 1.8 2006/09/13 13:58:32 bernie
20 *#* Add RenderHook support.
22 *#* Revision 1.7 2006/08/01 12:22:46 bernie
23 *#* Mention DevLib license.
25 *#* Revision 1.6 2006/07/19 12:56:27 bernie
26 *#* Convert to new Doxygen style.
28 *#* Revision 1.5 2006/06/03 13:58:01 bernie
29 *#* Fix recursive timeout and add exit status information.
31 *#* Revision 1.4 2006/06/02 12:26:18 bernie
32 *#* Draw graphical checkmarks.
34 *#* Revision 1.3 2006/05/28 15:03:31 bernie
35 *#* Avoid unnecessary rendering.
37 *#* Revision 1.2 2006/05/25 23:34:38 bernie
38 *#* Implement menu timeouts.
40 *#* Revision 1.1 2006/05/15 07:20:54 bernie
41 *#* Move menu to gui/.
43 *#* Revision 1.7 2006/04/27 05:39:24 bernie
44 *#* Enhance text rendering to arbitrary x,y coords.
46 *#* Revision 1.6 2006/04/11 00:07:32 bernie
47 *#* Implemenent MF_SAVESEL flag.
49 *#* Revision 1.5 2006/03/22 09:49:51 bernie
50 *#* Simplifications from project_grl.
52 *#* Revision 1.4 2006/03/20 17:48:35 bernie
53 *#* Implement support for ROM menus.
55 *#* Revision 1.3 2006/02/20 14:34:32 bernie
56 *#* Include appconfig.h before using its definitions.
58 *#* Revision 1.2 2006/02/15 09:10:51 bernie
59 *#* Make title bold; Fix height when we have no menubar.
68 #include <cfg/compiler.h>
69 #include <cfg/debug.h>
70 #include <appconfig.h>
71 #include <string.h> /* strcpy() */
74 #include <avr/pgmspace.h> /* strncpy_P() */
77 #if CONFIG_MENU_SMOOTH
78 #include <drv/lcd_gfx.h>
81 #if (CONFIG_MENU_TIMEOUT != 0)
82 #include <drv/timer.h>
85 #if CONFIG_MENU_MENUBAR
89 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
92 #define PTRMSG(x) ((const char *)x)
96 /* Temporary fake defines for ABORT stuff... */
98 #define PUSH_ABORT false
99 #define POP_ABORT do {} while(0)
100 #define DO_ABORT do {} while(0)
104 * Return the total number of items in in a menu.
106 static int menu_count(const struct Menu *menu)
110 for (cnt = 0; /*NOP*/; ++cnt)
112 const MenuItem *item = &menu->items[cnt];
115 if (menu->flags & MF_ROMITEMS)
117 memcpy_P(&ram_item, item, sizeof(ram_item));
121 if (!(item->label || item->hook))
128 #if CONFIG_MENU_MENUBAR
131 * Update the menu bar according to the selected item and redraw it.
133 static void menu_update_menubar(
134 const struct Menu *menu,
140 if (menu->flags & MF_ROMITEMS)
142 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
143 item_flags = pgm_read_int(&menu->items[selected].flags);
147 item_flags = menu->items[selected].flags;
149 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
151 if (item_flags & MIF_DISABLED)
152 newlabel = (const_iptr_t)LABEL_EMPTY;
153 else if (item_flags & MIF_TOGGLE)
154 newlabel = (const_iptr_t)LABEL_SEL;
155 else if (item_flags & MIF_CHECKIT)
157 newlabel = (item_flags & MIF_CHECKED) ?
158 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
161 mb->labels[3] = newlabel;
164 #endif /* CONFIG_MENU_MENUBAR */
167 static void menu_defaultRenderHook(struct Bitmap *bm, int ypos, bool selected, const struct MenuItem *item)
169 if (item->flags & MIF_CHECKIT)
171 gfx_rectClear(bm, 0, ypos,
172 bm->font->height, ypos + bm->font->height);
174 if (item->flags & MIF_TOGGLE)
175 gfx_rectDraw(bm, 2, ypos + 2,
176 bm->font->height - 2, ypos + bm->font->height - 2);
177 if (item->flags & MIF_CHECKED)
181 bm->font->height - 3, ypos + bm->font->height - 3);
183 bm->font->height - 3, ypos + 3,
184 3, ypos + bm->font->height - 3);
189 ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
194 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
195 selected ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
201 * Show a menu on the display.
203 static void menu_layout(
204 const struct Menu *menu,
211 const char * PROGMEM title = PTRMSG(menu->title);
212 Bitmap *bm = menu->bitmap;
220 text_clear(menu->bitmap);
227 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
228 ypos += bm->font->height;
231 #if CONFIG_MENU_SMOOTH
232 static coord_t yoffset = 0;
233 static int old_first_item = 0;
235 coord_t old_ymin = bm->cr.ymin;
237 /* Clip drawing inside menu items area */
239 bm->cr.xmin, bm->cr.ymin + ypos,
240 bm->cr.xmax, bm->cr.ymax);
242 if (old_first_item != first_item)
244 /* Speed proportional to distance */
245 speed = ABS(old_first_item - first_item) * 3;
247 if (old_first_item > first_item)
250 if (yoffset > bm->font->height)
259 if (yoffset < -bm->font->height)
265 first_item = MIN(old_first_item, menu_count(menu));
270 #endif /* CONFIG_MENU_SMOOTH */
272 if (redraw) for (i = first_item; /**/; ++i)
274 const MenuItem *item = &menu->items[i];
277 if (menu->flags & MF_ROMITEMS)
279 memcpy_P(&ram_item, item, sizeof(ram_item));
282 #endif /* CPU_HARVARD */
284 /* Check for end of room */
285 if (ypos > bm->cr.ymax)
288 /* Check for end of menu */
289 if (!(item->label || item->hook))
292 /* Only print visible items */
293 if (!(item->flags & MIF_HIDDEN))
295 /* Check if a special render function is supplied, otherwise use defaults */
296 RenderHook renderhook = (item->flags & MIF_RENDERHOOK) ? (RenderHook)item->label : menu_defaultRenderHook;
298 /* Render menuitem */
299 renderhook(menu->bitmap, ypos++, (i == selected), item);
301 ypos += bm->font->height;
305 #if CONFIG_MENU_SMOOTH
308 /* Clear rest of area */
309 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
311 lcd_blitBitmap(&lcd_bitmap);
314 /* Restore old cliprect */
316 bm->cr.xmin, old_ymin,
317 bm->cr.xmax, bm->cr.ymax);
319 #endif /* CONFIG_MENU_SMOOTH */
324 * Handle menu item selection
326 static iptr_t menu_doselect(const struct Menu *menu, struct MenuItem *item)
330 /* Exclude other items */
332 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
335 menu->items[i].flags &= ~MIF_CHECKED;
338 if (item->flags & MIF_DISABLED)
339 return MENU_DISABLED;
341 /* Handle checkable items */
342 if (item->flags & MIF_TOGGLE)
343 item->flags ^= MIF_CHECKED;
344 else if (item->flags & MIF_CHECKIT)
345 item->flags |= MIF_CHECKED;
347 /* Handle items with callback hooks */
350 /* Push a jmp buffer to abort the operation with the STOP/CANCEL key */
353 result = item->hook(item->userdata);
358 result = item->userdata;
365 * Return the next visible item (rolls back to the first item)
367 static int menu_next_visible_item(const struct Menu *menu, int index)
369 int total = menu_count(menu);
374 if (++index >= total)
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 * Return the previous visible item (rolls back to the last item)
396 static int menu_prev_visible_item(const struct Menu *menu, int index)
398 int total = menu_count(menu);
407 if (menu->flags & MF_ROMITEMS)
409 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
410 item_flags = pgm_read_int(&menu->items[index].flags);
414 item_flags = menu->items[index].flags;
416 while (item_flags & MIF_HIDDEN);
423 * Handle a menu and invoke hook functions for the selected menu items.
425 iptr_t menu_handle(const struct Menu *menu)
427 uint8_t items_per_page;
428 uint8_t first_item = 0;
433 #if (CONFIG_MENU_TIMEOUT != 0)
434 ticks_t now, menu_idle_time = timer_clock();
437 #if CONFIG_MENU_MENUBAR
439 const_iptr_t labels[] =
441 (const_iptr_t)LABEL_BACK,
442 (const_iptr_t)LABEL_UPARROW,
443 (const_iptr_t)LABEL_DOWNARROW,
448 * Initialize menu bar
450 if (menu->flags & MF_TOPLEVEL)
451 labels[0] = (const_iptr_t)LABEL_EMPTY;
453 mbar_init(&mb, menu->bitmap, labels, countof(labels));
454 #endif /* CONFIG_MENU_MENUBAR */
458 (menu->bitmap->height / menu->bitmap->font->height)
459 #if CONFIG_MENU_MENUBAR
460 - 1 /* menu bar labels */
462 - (menu->title ? 1 : 0);
464 /* Selected item should be a visible entry */
465 //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
466 selected = menu->selected;
474 * Keep selected item visible
476 while (selected < first_item)
477 first_item = menu_prev_visible_item(menu, first_item);
478 while (selected >= first_item + items_per_page)
479 first_item = menu_next_visible_item(menu, first_item);
481 menu_layout(menu, first_item, selected, redraw);
484 #if CONFIG_MENU_MENUBAR
485 menu_update_menubar(menu, &mb, selected);
488 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
494 #if (CONFIG_MENU_TIMEOUT != 0)
495 /* Reset idle timer on key press. */
498 menu_idle_time = now;
503 struct MenuItem *item = &(menu->items[selected]);
506 if (menu->flags & MF_ROMITEMS)
508 memcpy_P(&ram_item, item, sizeof(ram_item));
512 result = menu_doselect(menu, item);
515 /* Return immediately */
516 if (!(menu->flags & MF_STICKY))
519 #if (CONFIG_MENU_TIMEOUT != 0)
521 if ((result == MENU_TIMEOUT) && !(menu->flags & MF_TOPLEVEL))
525 menu_idle_time = timer_clock();
530 selected = menu_prev_visible_item(menu, selected);
533 else if (key & K_DOWN)
535 selected = menu_next_visible_item(menu, selected);
538 else if (!(menu->flags & MF_TOPLEVEL))
542 result = MENU_CANCEL;
546 #if CONFIG_MENU_TIMEOUT != 0
547 if (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
549 result = MENU_TIMEOUT;
556 /* Store currently selected item before leaving. */
557 if (menu->flags & MF_SAVESEL)
558 CONST_CAST(struct Menu *, menu)->selected = selected;
565 * Set 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 set.
573 int menu_setFlags(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;
585 * Clear flags on a menuitem.
587 * \param menu Menu owner of the item to change.
588 * \param idx Index of the menu item.
589 * \param flags Bit mask of the flags to clear.
593 int menu_clearFlags(struct Menu *menu, int idx, int flags)
595 ASSERT(idx < menu_count(menu));
596 ASSERT(!(menu->flags & MF_ROMITEMS));
598 int old = menu->items[idx].flags;
599 menu->items[idx].flags &= ~flags;