4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
36 * \author Bernardo Innocenti <bernie@develer.com>
37 * \author Stefano Fedrigo <aleph@develer.com>
39 * \brief General pourpose menu handling functions
44 *#* Revision 1.8 2006/09/13 13:58:32 bernie
45 *#* Add RenderHook support.
47 *#* Revision 1.7 2006/08/01 12:22:46 bernie
48 *#* Mention DevLib license.
50 *#* Revision 1.6 2006/07/19 12:56:27 bernie
51 *#* Convert to new Doxygen style.
53 *#* Revision 1.5 2006/06/03 13:58:01 bernie
54 *#* Fix recursive timeout and add exit status information.
56 *#* Revision 1.4 2006/06/02 12:26:18 bernie
57 *#* Draw graphical checkmarks.
59 *#* Revision 1.3 2006/05/28 15:03:31 bernie
60 *#* Avoid unnecessary rendering.
62 *#* Revision 1.2 2006/05/25 23:34:38 bernie
63 *#* Implement menu timeouts.
65 *#* Revision 1.1 2006/05/15 07:20:54 bernie
66 *#* Move menu to gui/.
68 *#* Revision 1.7 2006/04/27 05:39:24 bernie
69 *#* Enhance text rendering to arbitrary x,y coords.
71 *#* Revision 1.6 2006/04/11 00:07:32 bernie
72 *#* Implemenent MF_SAVESEL flag.
74 *#* Revision 1.5 2006/03/22 09:49:51 bernie
75 *#* Simplifications from project_grl.
77 *#* Revision 1.4 2006/03/20 17:48:35 bernie
78 *#* Implement support for ROM menus.
80 *#* Revision 1.3 2006/02/20 14:34:32 bernie
81 *#* Include appconfig.h before using its definitions.
83 *#* Revision 1.2 2006/02/15 09:10:51 bernie
84 *#* Make title bold; Fix height when we have no menubar.
93 #include <cfg/compiler.h>
94 #include <cfg/debug.h>
95 #include <appconfig.h>
96 #include <string.h> /* strcpy() */
99 #include <avr/pgmspace.h> /* strncpy_P() */
102 #if CONFIG_MENU_SMOOTH
103 #include <drv/lcd_gfx.h>
106 #if (CONFIG_MENU_TIMEOUT != 0)
107 #include <drv/timer.h>
110 #if CONFIG_MENU_MENUBAR
114 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
117 #define PTRMSG(x) ((const char *)x)
121 /* Temporary fake defines for ABORT stuff... */
123 #define PUSH_ABORT false
124 #define POP_ABORT do {} while(0)
125 #define DO_ABORT do {} while(0)
129 * Return the total number of items in in a menu.
131 static int menu_count(const struct Menu *menu)
135 for (cnt = 0; /*NOP*/; ++cnt)
137 const MenuItem *item = &menu->items[cnt];
140 if (menu->flags & MF_ROMITEMS)
142 memcpy_P(&ram_item, item, sizeof(ram_item));
146 if (!(item->label || item->hook))
153 #if CONFIG_MENU_MENUBAR
156 * Update the menu bar according to the selected item and redraw it.
158 static void menu_update_menubar(
159 const struct Menu *menu,
165 if (menu->flags & MF_ROMITEMS)
167 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
168 item_flags = pgm_read_int(&menu->items[selected].flags);
172 item_flags = menu->items[selected].flags;
174 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
176 if (item_flags & MIF_DISABLED)
177 newlabel = (const_iptr_t)LABEL_EMPTY;
178 else if (item_flags & MIF_TOGGLE)
179 newlabel = (const_iptr_t)LABEL_SEL;
180 else if (item_flags & MIF_CHECKIT)
182 newlabel = (item_flags & MIF_CHECKED) ?
183 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
186 mb->labels[3] = newlabel;
189 #endif /* CONFIG_MENU_MENUBAR */
192 static void menu_defaultRenderHook(struct Bitmap *bm, int ypos, bool selected, const struct MenuItem *item)
194 if (item->flags & MIF_CHECKIT)
196 gfx_rectClear(bm, 0, ypos,
197 bm->font->height, ypos + bm->font->height);
199 if (item->flags & MIF_TOGGLE)
200 gfx_rectDraw(bm, 2, ypos + 2,
201 bm->font->height - 2, ypos + bm->font->height - 2);
202 if (item->flags & MIF_CHECKED)
206 bm->font->height - 3, ypos + bm->font->height - 3);
208 bm->font->height - 3, ypos + 3,
209 3, ypos + bm->font->height - 3);
214 ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
219 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
220 selected ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
226 * Show a menu on the display.
228 static void menu_layout(
229 const struct Menu *menu,
236 const char * PROGMEM title = PTRMSG(menu->title);
237 Bitmap *bm = menu->bitmap;
245 text_clear(menu->bitmap);
252 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
253 ypos += bm->font->height;
256 #if CONFIG_MENU_SMOOTH
257 static coord_t yoffset = 0;
258 static int old_first_item = 0;
260 coord_t old_ymin = bm->cr.ymin;
262 /* Clip drawing inside menu items area */
264 bm->cr.xmin, bm->cr.ymin + ypos,
265 bm->cr.xmax, bm->cr.ymax);
267 if (old_first_item != first_item)
269 /* Speed proportional to distance */
270 speed = ABS(old_first_item - first_item) * 3;
272 if (old_first_item > first_item)
275 if (yoffset > bm->font->height)
284 if (yoffset < -bm->font->height)
290 first_item = MIN(old_first_item, menu_count(menu));
295 #endif /* CONFIG_MENU_SMOOTH */
297 if (redraw) for (i = first_item; /**/; ++i)
299 const MenuItem *item = &menu->items[i];
302 if (menu->flags & MF_ROMITEMS)
304 memcpy_P(&ram_item, item, sizeof(ram_item));
307 #endif /* CPU_HARVARD */
309 /* Check for end of room */
310 if (ypos > bm->cr.ymax)
313 /* Check for end of menu */
314 if (!(item->label || item->hook))
317 /* Only print visible items */
318 if (!(item->flags & MIF_HIDDEN))
320 /* Check if a special render function is supplied, otherwise use defaults */
321 RenderHook renderhook = (item->flags & MIF_RENDERHOOK) ? (RenderHook)item->label : menu_defaultRenderHook;
323 /* Render menuitem */
324 renderhook(menu->bitmap, ypos++, (i == selected), item);
326 ypos += bm->font->height;
330 #if CONFIG_MENU_SMOOTH
333 /* Clear rest of area */
334 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
336 lcd_blitBitmap(&lcd_bitmap);
339 /* Restore old cliprect */
341 bm->cr.xmin, old_ymin,
342 bm->cr.xmax, bm->cr.ymax);
344 #endif /* CONFIG_MENU_SMOOTH */
349 * Handle menu item selection
351 static iptr_t menu_doselect(const struct Menu *menu, struct MenuItem *item)
355 /* Exclude other items */
357 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
360 menu->items[i].flags &= ~MIF_CHECKED;
363 if (item->flags & MIF_DISABLED)
364 return MENU_DISABLED;
366 /* Handle checkable items */
367 if (item->flags & MIF_TOGGLE)
368 item->flags ^= MIF_CHECKED;
369 else if (item->flags & MIF_CHECKIT)
370 item->flags |= MIF_CHECKED;
372 /* Handle items with callback hooks */
375 /* Push a jmp buffer to abort the operation with the STOP/CANCEL key */
378 result = item->hook(item->userdata);
383 result = item->userdata;
390 * Return the next visible item (rolls back to the first item)
392 static int menu_next_visible_item(const struct Menu *menu, int index)
394 int total = menu_count(menu);
399 if (++index >= total)
403 if (menu->flags & MF_ROMITEMS)
405 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
406 item_flags = pgm_read_int(&menu->items[index].flags);
410 item_flags = menu->items[index].flags;
412 while (item_flags & MIF_HIDDEN);
419 * Return the previous visible item (rolls back to the last item)
421 static int menu_prev_visible_item(const struct Menu *menu, int index)
423 int total = menu_count(menu);
432 if (menu->flags & MF_ROMITEMS)
434 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
435 item_flags = pgm_read_int(&menu->items[index].flags);
439 item_flags = menu->items[index].flags;
441 while (item_flags & MIF_HIDDEN);
448 * Handle a menu and invoke hook functions for the selected menu items.
450 iptr_t menu_handle(const struct Menu *menu)
452 uint8_t items_per_page;
453 uint8_t first_item = 0;
458 #if (CONFIG_MENU_TIMEOUT != 0)
459 ticks_t now, menu_idle_time = timer_clock();
462 #if CONFIG_MENU_MENUBAR
464 const_iptr_t labels[] =
466 (const_iptr_t)LABEL_BACK,
467 (const_iptr_t)LABEL_UPARROW,
468 (const_iptr_t)LABEL_DOWNARROW,
473 * Initialize menu bar
475 if (menu->flags & MF_TOPLEVEL)
476 labels[0] = (const_iptr_t)LABEL_EMPTY;
478 mbar_init(&mb, menu->bitmap, labels, countof(labels));
479 #endif /* CONFIG_MENU_MENUBAR */
483 (menu->bitmap->height / menu->bitmap->font->height)
484 #if CONFIG_MENU_MENUBAR
485 - 1 /* menu bar labels */
487 - (menu->title ? 1 : 0);
489 /* Selected item should be a visible entry */
490 //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
491 selected = menu->selected;
499 * Keep selected item visible
501 while (selected < first_item)
502 first_item = menu_prev_visible_item(menu, first_item);
503 while (selected >= first_item + items_per_page)
504 first_item = menu_next_visible_item(menu, first_item);
506 menu_layout(menu, first_item, selected, redraw);
509 #if CONFIG_MENU_MENUBAR
510 menu_update_menubar(menu, &mb, selected);
513 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
519 #if (CONFIG_MENU_TIMEOUT != 0)
520 /* Reset idle timer on key press. */
523 menu_idle_time = now;
528 struct MenuItem *item = &(menu->items[selected]);
531 if (menu->flags & MF_ROMITEMS)
533 memcpy_P(&ram_item, item, sizeof(ram_item));
537 result = menu_doselect(menu, item);
540 /* Return immediately */
541 if (!(menu->flags & MF_STICKY))
544 #if (CONFIG_MENU_TIMEOUT != 0)
546 if ((result == MENU_TIMEOUT) && !(menu->flags & MF_TOPLEVEL))
550 menu_idle_time = timer_clock();
555 selected = menu_prev_visible_item(menu, selected);
558 else if (key & K_DOWN)
560 selected = menu_next_visible_item(menu, selected);
563 else if (!(menu->flags & MF_TOPLEVEL))
567 result = MENU_CANCEL;
571 #if CONFIG_MENU_TIMEOUT != 0
572 if (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
574 result = MENU_TIMEOUT;
581 /* Store currently selected item before leaving. */
582 if (menu->flags & MF_SAVESEL)
583 CONST_CAST(struct Menu *, menu)->selected = selected;
590 * Set flags on a menuitem.
592 * \param menu Menu owner of the item to change.
593 * \param idx Index of the menu item.
594 * \param flags Bit mask of the flags to set.
598 int menu_setFlags(struct Menu *menu, int idx, int flags)
600 ASSERT(idx < menu_count(menu));
601 ASSERT(!(menu->flags & MF_ROMITEMS));
603 int old = menu->items[idx].flags;
604 menu->items[idx].flags |= flags;
610 * Clear flags on a menuitem.
612 * \param menu Menu owner of the item to change.
613 * \param idx Index of the menu item.
614 * \param flags Bit mask of the flags to clear.
618 int menu_clearFlags(struct Menu *menu, int idx, int flags)
620 ASSERT(idx < menu_count(menu));
621 ASSERT(!(menu->flags & MF_ROMITEMS));
623 int old = menu->items[idx].flags;
624 menu->items[idx].flags &= ~flags;