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 Bernie Innocenti <bernie@codewiz.org>
36 * \author Bernie Innocenti <bernie@codewiz.org>
37 * \author Stefano Fedrigo <aleph@develer.com>
39 * \brief General pourpose menu handling functions
44 #include "cfg/cfg_gfx.h"
45 #include "cfg/cfg_arch.h"
46 #include <cfg/compiler.h>
47 #include <cfg/debug.h>
55 #include <string.h> /* strcpy() */
58 #include <avr/pgmspace.h> /* strncpy_P() */
61 #if CONFIG_MENU_SMOOTH
62 #include <drv/lcd_gfx.h>
65 #if (CONFIG_MENU_TIMEOUT != 0)
66 #include <drv/timer.h>
69 #if CONFIG_MENU_MENUBAR
73 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
76 #define PTRMSG(x) ((const char *)x)
80 /* Temporary fake defines for ABORT stuff... */
82 #define PUSH_ABORT false
83 #define POP_ABORT do {} while(0)
84 #define DO_ABORT do {} while(0)
88 * Return the total number of items in in a menu.
90 static int menu_count(const struct Menu *menu)
94 for (cnt = 0; /*NOP*/; ++cnt)
96 const MenuItem *item = &menu->items[cnt];
99 if (menu->flags & MF_ROMITEMS)
101 memcpy_P(&ram_item, item, sizeof(ram_item));
105 if (!(item->label || item->hook))
112 #if CONFIG_MENU_MENUBAR
115 * Update the menu bar according to the selected item and redraw it.
117 static void menu_update_menubar(
118 const struct Menu *menu,
124 if (menu->flags & MF_ROMITEMS)
126 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
127 item_flags = pgm_read_int(&menu->items[selected].flags);
131 item_flags = menu->items[selected].flags;
133 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
135 if (item_flags & MIF_DISABLED)
136 newlabel = (const_iptr_t)LABEL_EMPTY;
137 else if (item_flags & MIF_TOGGLE)
138 newlabel = (const_iptr_t)LABEL_SEL;
139 else if (item_flags & MIF_CHECKIT)
141 newlabel = (item_flags & MIF_CHECKED) ?
142 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
145 mb->labels[3] = newlabel;
148 #endif /* CONFIG_MENU_MENUBAR */
151 static void menu_defaultRenderHook(struct Bitmap *bm, int ypos, bool selected, const struct MenuItem *item)
153 if (item->flags & MIF_CHECKIT)
155 gfx_rectClear(bm, 0, ypos,
156 bm->font->height, ypos + bm->font->height);
158 if (item->flags & MIF_TOGGLE)
159 gfx_rectDraw(bm, 2, ypos + 2,
160 bm->font->height - 2, ypos + bm->font->height - 2);
161 if (item->flags & MIF_CHECKED)
165 bm->font->height - 3, ypos + bm->font->height - 3);
167 bm->font->height - 3, ypos + 3,
168 3, ypos + bm->font->height - 3);
173 ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
178 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
179 selected ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
185 * Show a menu on the display.
187 static void menu_layout(
188 const struct Menu *menu,
195 const char * PROGMEM title = PTRMSG(menu->title);
196 Bitmap *bm = menu->bitmap;
204 text_clear(menu->bitmap);
211 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
212 ypos += bm->font->height;
215 #if CONFIG_MENU_SMOOTH
216 static coord_t yoffset = 0;
217 static int old_first_item = 0;
219 coord_t old_ymin = bm->cr.ymin;
221 /* Clip drawing inside menu items area */
223 bm->cr.xmin, bm->cr.ymin + ypos,
224 bm->cr.xmax, bm->cr.ymax);
226 if (old_first_item != first_item)
228 /* Speed proportional to distance */
229 speed = ABS(old_first_item - first_item) * 3;
231 if (old_first_item > first_item)
234 if (yoffset > bm->font->height)
243 if (yoffset < -bm->font->height)
249 first_item = MIN(old_first_item, menu_count(menu));
254 #endif /* CONFIG_MENU_SMOOTH */
256 if (redraw) for (i = first_item; /**/; ++i)
258 const MenuItem *item = &menu->items[i];
261 if (menu->flags & MF_ROMITEMS)
263 memcpy_P(&ram_item, item, sizeof(ram_item));
266 #endif /* CPU_HARVARD */
268 /* Check for end of room */
269 if (ypos > bm->cr.ymax)
272 /* Check for end of menu */
273 if (!(item->label || item->hook))
276 /* Only print visible items */
277 if (!(item->flags & MIF_HIDDEN))
279 /* Check if a special render function is supplied, otherwise use defaults */
280 #if (ARCH & ARCH_NIGHTTEST)
281 #warning __FILTER_NEXT_WARNING__
283 RenderHook renderhook = (item->flags & MIF_RENDERHOOK) ? (RenderHook)item->label : menu_defaultRenderHook;
285 /* Render menuitem */
286 renderhook(menu->bitmap, ypos++, (i == selected), item);
288 ypos += bm->font->height;
292 #if CONFIG_MENU_SMOOTH
295 /* Clear rest of area */
296 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
298 lcd_blitBitmap(&lcd_bitmap);
301 /* Restore old cliprect */
303 bm->cr.xmin, old_ymin,
304 bm->cr.xmax, bm->cr.ymax);
306 #endif /* CONFIG_MENU_SMOOTH */
311 * Handle menu item selection
313 static iptr_t menu_doselect(const struct Menu *menu, struct MenuItem *item)
317 /* Exclude other items */
319 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
322 menu->items[i].flags &= ~MIF_CHECKED;
325 if (item->flags & MIF_DISABLED)
326 return MENU_DISABLED;
328 /* Handle checkable items */
329 if (item->flags & MIF_TOGGLE)
330 item->flags ^= MIF_CHECKED;
331 else if (item->flags & MIF_CHECKIT)
332 item->flags |= MIF_CHECKED;
334 /* Handle items with callback hooks */
337 /* Push a jmp buffer to abort the operation with the STOP/CANCEL key */
340 result = item->hook(item->userdata);
345 result = item->userdata;
352 * Return the next visible item (rolls back to the first item)
354 static int menu_next_visible_item(const struct Menu *menu, int index)
356 int total = menu_count(menu);
361 if (++index >= total)
365 if (menu->flags & MF_ROMITEMS)
367 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
368 item_flags = pgm_read_int(&menu->items[index].flags);
372 item_flags = menu->items[index].flags;
374 while (item_flags & MIF_HIDDEN);
381 * Return the previous visible item (rolls back to the last item)
383 static int menu_prev_visible_item(const struct Menu *menu, int index)
385 int total = menu_count(menu);
394 if (menu->flags & MF_ROMITEMS)
396 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
397 item_flags = pgm_read_int(&menu->items[index].flags);
401 item_flags = menu->items[index].flags;
403 while (item_flags & MIF_HIDDEN);
410 * Handle a menu and invoke hook functions for the selected menu items.
412 iptr_t menu_handle(const struct Menu *menu)
414 uint8_t items_per_page;
415 uint8_t first_item = 0;
420 #if (CONFIG_MENU_TIMEOUT != 0)
421 ticks_t now, menu_idle_time = timer_clock();
424 #if CONFIG_MENU_MENUBAR
426 const_iptr_t labels[] =
428 (const_iptr_t)LABEL_BACK,
429 (const_iptr_t)LABEL_UPARROW,
430 (const_iptr_t)LABEL_DOWNARROW,
435 * Initialize menu bar
437 if (menu->flags & MF_TOPLEVEL)
438 labels[0] = (const_iptr_t)LABEL_EMPTY;
440 mbar_init(&mb, menu->bitmap, labels, countof(labels));
441 #endif /* CONFIG_MENU_MENUBAR */
445 (menu->bitmap->height / menu->bitmap->font->height)
446 #if CONFIG_MENU_MENUBAR
447 - 1 /* menu bar labels */
449 - (menu->title ? 1 : 0);
451 /* Selected item should be a visible entry */
452 //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
453 selected = menu->selected;
461 * Keep selected item visible
463 while (selected < first_item)
464 first_item = menu_prev_visible_item(menu, first_item);
465 while (selected >= first_item + items_per_page)
466 first_item = menu_next_visible_item(menu, first_item);
468 menu_layout(menu, first_item, selected, redraw);
471 #if CONFIG_MENU_MENUBAR
472 menu_update_menubar(menu, &mb, selected);
475 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
481 #if (CONFIG_MENU_TIMEOUT != 0)
482 /* Reset idle timer on key press. */
485 menu_idle_time = now;
490 struct MenuItem *item = &(menu->items[selected]);
493 if (menu->flags & MF_ROMITEMS)
495 memcpy_P(&ram_item, item, sizeof(ram_item));
499 result = menu_doselect(menu, item);
502 /* Return immediately */
503 if (!(menu->flags & MF_STICKY))
506 #if (CONFIG_MENU_TIMEOUT != 0)
508 if ((result == MENU_TIMEOUT) && !(menu->flags & MF_TOPLEVEL))
512 menu_idle_time = timer_clock();
517 selected = menu_prev_visible_item(menu, selected);
520 else if (key & K_DOWN)
522 selected = menu_next_visible_item(menu, selected);
525 else if (!(menu->flags & MF_TOPLEVEL))
529 result = MENU_CANCEL;
533 #if CONFIG_MENU_TIMEOUT != 0
534 if (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
536 result = MENU_TIMEOUT;
543 /* Store currently selected item before leaving. */
544 if (menu->flags & MF_SAVESEL)
545 #if (ARCH & ARCH_NIGHTTEST)
546 #warning __FILTER_NEXT_WARNING__
548 CONST_CAST(struct Menu *, menu)->selected = selected;
555 * Set flags on a menuitem.
557 * \param menu Menu owner of the item to change.
558 * \param idx Index of the menu item.
559 * \param flags Bit mask of the flags to set.
563 int menu_setFlags(struct Menu *menu, int idx, int flags)
565 ASSERT(idx < menu_count(menu));
566 ASSERT(!(menu->flags & MF_ROMITEMS));
568 int old = menu->items[idx].flags;
569 menu->items[idx].flags |= flags;
575 * Clear flags on a menuitem.
577 * \param menu Menu owner of the item to change.
578 * \param idx Index of the menu item.
579 * \param flags Bit mask of the flags to clear.
583 int menu_clearFlags(struct Menu *menu, int idx, int flags)
585 ASSERT(idx < menu_count(menu));
586 ASSERT(!(menu->flags & MF_ROMITEMS));
588 int old = menu->items[idx].flags;
589 menu->items[idx].flags &= ~flags;