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, 2010 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
34 * \author Bernie Innocenti <bernie@codewiz.org>
35 * \author Stefano Fedrigo <aleph@develer.com>
37 * \brief General pourpose menu handling functions
42 #include "cfg/cfg_menu.h"
43 #include "cfg/cfg_arch.h"
45 #include <cfg/compiler.h>
46 #include <cfg/debug.h>
54 #include <string.h> /* strcpy() */
57 #include <avr/pgmspace.h> /* strncpy_P() */
60 #if (CONFIG_MENU_TIMEOUT != 0)
61 #include <drv/timer.h>
64 #if CONFIG_MENU_MENUBAR
68 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
71 #define PTRMSG(x) ((const char *)x)
75 /* Temporary fake defines for ABORT stuff... */
77 #define PUSH_ABORT false
78 #define POP_ABORT do {} while(0)
79 #define DO_ABORT do {} while(0)
83 * Return the total number of items in in a menu.
85 static int menu_count(const struct Menu *menu)
89 for (cnt = 0; /*NOP*/; ++cnt)
91 const MenuItem *item = &menu->items[cnt];
94 if (menu->flags & MF_ROMITEMS)
96 memcpy_P(&ram_item, item, sizeof(ram_item));
100 if (!(item->label || item->hook))
107 #if CONFIG_MENU_MENUBAR
110 * Update the menu bar according to the selected item and redraw it.
112 static void menu_update_menubar(
113 const struct Menu *menu,
119 if (menu->flags & MF_ROMITEMS)
121 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
122 item_flags = pgm_read_int(&menu->items[selected].flags);
126 item_flags = menu->items[selected].flags;
128 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
130 if (item_flags & MIF_DISABLED)
131 newlabel = (const_iptr_t)LABEL_EMPTY;
132 else if (item_flags & MIF_TOGGLE)
133 newlabel = (const_iptr_t)LABEL_SEL;
134 else if (item_flags & MIF_CHECKIT)
136 newlabel = (item_flags & MIF_CHECKED) ?
137 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
140 mb->labels[3] = newlabel;
143 #endif /* CONFIG_MENU_MENUBAR */
146 static void menu_defaultRenderHook(struct Bitmap *bm, int ypos, bool selected, const struct MenuItem *item)
148 if (item->flags & MIF_CHECKIT)
150 gfx_rectClear(bm, 0, ypos,
151 bm->font->height, ypos + bm->font->height);
153 if (item->flags & MIF_TOGGLE)
154 gfx_rectDraw(bm, 2, ypos + 2,
155 bm->font->height - 2, ypos + bm->font->height - 2);
156 if (item->flags & MIF_CHECKED)
160 bm->font->height - 3, ypos + bm->font->height - 3);
162 bm->font->height - 3, ypos + 3,
163 3, ypos + bm->font->height - 3);
168 ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
173 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
174 selected ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
180 * Show a menu on the display.
182 static void menu_layout(
183 const struct Menu *menu,
190 const char * PROGMEM title = PTRMSG(menu->title);
191 Bitmap *bm = menu->bitmap;
198 text_clear(menu->bitmap);
204 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
205 ypos += bm->font->height;
208 #if CONFIG_MENU_SMOOTH
209 static coord_t yoffset = 0;
210 static int old_first_item = 0;
212 coord_t old_ymin = bm->cr.ymin;
214 /* Clip drawing inside menu items area */
216 bm->cr.xmin, bm->cr.ymin + ypos,
217 bm->cr.xmax, bm->cr.ymax);
219 if (old_first_item != first_item)
221 /* Speed proportional to distance */
222 speed = ABS(old_first_item - first_item) * 3;
224 if (old_first_item > first_item)
227 if (yoffset > bm->font->height)
236 if (yoffset < -bm->font->height)
242 first_item = MIN(old_first_item, menu_count(menu));
247 #endif /* CONFIG_MENU_SMOOTH */
249 if (redraw) for (i = first_item; /**/; ++i)
251 const MenuItem *item = &menu->items[i];
254 if (menu->flags & MF_ROMITEMS)
256 memcpy_P(&ram_item, item, sizeof(ram_item));
259 #endif /* CPU_HARVARD */
261 /* Check for end of room */
262 if (ypos > bm->cr.ymax)
265 /* Check for end of menu */
266 if (!(item->label || item->hook))
269 /* Only print visible items */
270 if (!(item->flags & MIF_HIDDEN))
272 #warning __FILTER_NEXT_WARNING__
273 RenderHook renderhook = (item->flags & MIF_RENDERHOOK) ? (RenderHook)item->label : menu_defaultRenderHook;
275 /* Render menuitem */
276 renderhook(menu->bitmap, ypos++, (i == selected), item);
278 ypos += bm->font->height;
282 #if CONFIG_MENU_SMOOTH
285 /* Clear rest of area */
286 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
288 menu->lcd_blitBitmap(bm);
291 /* Restore old cliprect */
293 bm->cr.xmin, old_ymin,
294 bm->cr.xmax, bm->cr.ymax);
296 #endif /* CONFIG_MENU_SMOOTH */
301 * Handle menu item selection
303 static iptr_t menu_doselect(const struct Menu *menu, struct MenuItem *item)
307 /* Exclude other items */
309 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
312 menu->items[i].flags &= ~MIF_CHECKED;
315 if (item->flags & MIF_DISABLED)
316 return MENU_DISABLED;
318 /* Handle checkable items */
319 if (item->flags & MIF_TOGGLE)
320 item->flags ^= MIF_CHECKED;
321 else if (item->flags & MIF_CHECKIT)
322 item->flags |= MIF_CHECKED;
324 /* Handle items with callback hooks */
327 /* Push a jmp buffer to abort the operation with the STOP/CANCEL key */
330 result = item->hook(item->userdata);
335 result = item->userdata;
342 * Return the next visible item (rolls back to the first item)
344 static int menu_next_visible_item(const struct Menu *menu, int index)
346 int total = menu_count(menu);
351 if (++index >= total)
355 if (menu->flags & MF_ROMITEMS)
357 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
358 item_flags = pgm_read_int(&menu->items[index].flags);
362 item_flags = menu->items[index].flags;
364 while (item_flags & MIF_HIDDEN);
371 * Return the previous visible item (rolls back to the last item)
373 static int menu_prev_visible_item(const struct Menu *menu, int index)
375 int total = menu_count(menu);
384 if (menu->flags & MF_ROMITEMS)
386 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
387 item_flags = pgm_read_int(&menu->items[index].flags);
391 item_flags = menu->items[index].flags;
393 while (item_flags & MIF_HIDDEN);
400 * Handle a menu and invoke hook functions for the selected menu items.
402 iptr_t menu_handle(const struct Menu *menu)
404 uint8_t items_per_page;
405 uint8_t first_item = 0;
410 #if (CONFIG_MENU_TIMEOUT != 0)
411 ticks_t now, menu_idle_time = timer_clock();
414 #if CONFIG_MENU_MENUBAR
416 const_iptr_t labels[] =
418 (const_iptr_t)LABEL_BACK,
419 (const_iptr_t)LABEL_UPARROW,
420 (const_iptr_t)LABEL_DOWNARROW,
425 * Initialize menu bar
427 if (menu->flags & MF_TOPLEVEL)
428 labels[0] = (const_iptr_t)LABEL_EMPTY;
430 mbar_init(&mb, menu->bitmap, labels, countof(labels));
431 #endif /* CONFIG_MENU_MENUBAR */
435 (menu->bitmap->height / menu->bitmap->font->height - 1)
436 #if CONFIG_MENU_MENUBAR
437 - 1 /* menu bar labels */
439 - (menu->title ? 1 : 0);
441 /* Selected item should be a visible entry */
442 //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
443 selected = menu->selected;
451 * Keep selected item visible
453 while (selected < first_item)
454 first_item = menu_prev_visible_item(menu, first_item);
455 while (selected >= first_item + items_per_page)
456 first_item = menu_next_visible_item(menu, first_item);
458 menu_layout(menu, first_item, selected, redraw);
461 #if CONFIG_MENU_MENUBAR
462 menu_update_menubar(menu, &mb, selected);
465 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
471 #if (CONFIG_MENU_TIMEOUT != 0)
472 /* Reset idle timer on key press. */
475 menu_idle_time = now;
480 struct MenuItem *item = &(menu->items[selected]);
483 if (menu->flags & MF_ROMITEMS)
485 memcpy_P(&ram_item, item, sizeof(ram_item));
489 result = menu_doselect(menu, item);
492 /* Return immediately */
493 if (!(menu->flags & MF_STICKY))
496 #if (CONFIG_MENU_TIMEOUT != 0)
498 if ((result == MENU_TIMEOUT) && !(menu->flags & MF_TOPLEVEL))
502 menu_idle_time = timer_clock();
507 selected = menu_prev_visible_item(menu, selected);
510 else if (key & K_DOWN)
512 selected = menu_next_visible_item(menu, selected);
515 else if (!(menu->flags & MF_TOPLEVEL))
519 result = MENU_CANCEL;
523 #if CONFIG_MENU_TIMEOUT != 0
524 if (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
526 result = MENU_TIMEOUT;
533 /* Store currently selected item before leaving. */
534 if (menu->flags & MF_SAVESEL)
535 #warning __FILTER_NEXT_WARNING__
536 CONST_CAST(struct Menu *, menu)->selected = selected;
543 * Set flags on a menuitem.
545 * \param menu Menu owner of the item to change.
546 * \param idx Index of the menu item.
547 * \param flags Bit mask of the flags to set.
551 int menu_setFlags(struct Menu *menu, int idx, int flags)
553 ASSERT(idx < menu_count(menu));
554 ASSERT(!(menu->flags & MF_ROMITEMS));
556 int old = menu->items[idx].flags;
557 menu->items[idx].flags |= flags;
563 * Clear flags on a menuitem.
565 * \param menu Menu owner of the item to change.
566 * \param idx Index of the menu item.
567 * \param flags Bit mask of the flags to clear.
571 int menu_clearFlags(struct Menu *menu, int idx, int flags)
573 ASSERT(idx < menu_count(menu));
574 ASSERT(!(menu->flags & MF_ROMITEMS));
576 int old = menu->items[idx].flags;
577 menu->items[idx].flags &= ~flags;