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/compiler.h>
46 #include <cfg/debug.h>
54 #include <string.h> /* strcpy() */
57 #include <avr/pgmspace.h> /* strncpy_P() */
60 #if CONFIG_MENU_SMOOTH
61 #include <drv/lcd_gfx.h>
64 #if (CONFIG_MENU_TIMEOUT != 0)
65 #include <drv/timer.h>
68 #if CONFIG_MENU_MENUBAR
72 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
75 #define PTRMSG(x) ((const char *)x)
79 /* Temporary fake defines for ABORT stuff... */
81 #define PUSH_ABORT false
82 #define POP_ABORT do {} while(0)
83 #define DO_ABORT do {} while(0)
87 * Return the total number of items in in a menu.
89 static int menu_count(const struct Menu *menu)
93 for (cnt = 0; /*NOP*/; ++cnt)
95 const MenuItem *item = &menu->items[cnt];
98 if (menu->flags & MF_ROMITEMS)
100 memcpy_P(&ram_item, item, sizeof(ram_item));
104 if (!(item->label || item->hook))
111 #if CONFIG_MENU_MENUBAR
114 * Update the menu bar according to the selected item and redraw it.
116 static void menu_update_menubar(
117 const struct Menu *menu,
123 if (menu->flags & MF_ROMITEMS)
125 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
126 item_flags = pgm_read_int(&menu->items[selected].flags);
130 item_flags = menu->items[selected].flags;
132 const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
134 if (item_flags & MIF_DISABLED)
135 newlabel = (const_iptr_t)LABEL_EMPTY;
136 else if (item_flags & MIF_TOGGLE)
137 newlabel = (const_iptr_t)LABEL_SEL;
138 else if (item_flags & MIF_CHECKIT)
140 newlabel = (item_flags & MIF_CHECKED) ?
141 (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
144 mb->labels[3] = newlabel;
147 #endif /* CONFIG_MENU_MENUBAR */
150 static void menu_defaultRenderHook(struct Bitmap *bm, int ypos, bool selected, const struct MenuItem *item)
152 if (item->flags & MIF_CHECKIT)
154 gfx_rectClear(bm, 0, ypos,
155 bm->font->height, ypos + bm->font->height);
157 if (item->flags & MIF_TOGGLE)
158 gfx_rectDraw(bm, 2, ypos + 2,
159 bm->font->height - 2, ypos + bm->font->height - 2);
160 if (item->flags & MIF_CHECKED)
164 bm->font->height - 3, ypos + bm->font->height - 3);
166 bm->font->height - 3, ypos + 3,
167 3, ypos + bm->font->height - 3);
172 ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
177 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
178 selected ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
184 * Show a menu on the display.
186 static void menu_layout(
187 const struct Menu *menu,
194 const char * PROGMEM title = PTRMSG(menu->title);
195 Bitmap *bm = menu->bitmap;
203 text_clear(menu->bitmap);
210 text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
211 ypos += bm->font->height;
214 #if CONFIG_MENU_SMOOTH
215 static coord_t yoffset = 0;
216 static int old_first_item = 0;
218 coord_t old_ymin = bm->cr.ymin;
220 /* Clip drawing inside menu items area */
222 bm->cr.xmin, bm->cr.ymin + ypos,
223 bm->cr.xmax, bm->cr.ymax);
225 if (old_first_item != first_item)
227 /* Speed proportional to distance */
228 speed = ABS(old_first_item - first_item) * 3;
230 if (old_first_item > first_item)
233 if (yoffset > bm->font->height)
242 if (yoffset < -bm->font->height)
248 first_item = MIN(old_first_item, menu_count(menu));
253 #endif /* CONFIG_MENU_SMOOTH */
255 if (redraw) for (i = first_item; /**/; ++i)
257 const MenuItem *item = &menu->items[i];
260 if (menu->flags & MF_ROMITEMS)
262 memcpy_P(&ram_item, item, sizeof(ram_item));
265 #endif /* CPU_HARVARD */
267 /* Check for end of room */
268 if (ypos > bm->cr.ymax)
271 /* Check for end of menu */
272 if (!(item->label || item->hook))
275 /* Only print visible items */
276 if (!(item->flags & MIF_HIDDEN))
278 /* Check if a special render function is supplied, otherwise use defaults */
279 RenderHook renderhook = (item->flags & MIF_RENDERHOOK) ? (RenderHook)item->label : menu_defaultRenderHook;
281 /* Render menuitem */
282 renderhook(menu->bitmap, ypos++, (i == selected), item);
284 ypos += bm->font->height;
288 #if CONFIG_MENU_SMOOTH
291 /* Clear rest of area */
292 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
294 lcd_blitBitmap(&lcd_bitmap);
297 /* Restore old cliprect */
299 bm->cr.xmin, old_ymin,
300 bm->cr.xmax, bm->cr.ymax);
302 #endif /* CONFIG_MENU_SMOOTH */
307 * Handle menu item selection
309 static iptr_t menu_doselect(const struct Menu *menu, struct MenuItem *item)
313 /* Exclude other items */
315 for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
318 menu->items[i].flags &= ~MIF_CHECKED;
321 if (item->flags & MIF_DISABLED)
322 return MENU_DISABLED;
324 /* Handle checkable items */
325 if (item->flags & MIF_TOGGLE)
326 item->flags ^= MIF_CHECKED;
327 else if (item->flags & MIF_CHECKIT)
328 item->flags |= MIF_CHECKED;
330 /* Handle items with callback hooks */
333 /* Push a jmp buffer to abort the operation with the STOP/CANCEL key */
336 result = item->hook(item->userdata);
341 result = item->userdata;
348 * Return the next visible item (rolls back to the first item)
350 static int menu_next_visible_item(const struct Menu *menu, int index)
352 int total = menu_count(menu);
357 if (++index >= total)
361 if (menu->flags & MF_ROMITEMS)
363 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
364 item_flags = pgm_read_int(&menu->items[index].flags);
368 item_flags = menu->items[index].flags;
370 while (item_flags & MIF_HIDDEN);
377 * Return the previous visible item (rolls back to the last item)
379 static int menu_prev_visible_item(const struct Menu *menu, int index)
381 int total = menu_count(menu);
390 if (menu->flags & MF_ROMITEMS)
392 ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
393 item_flags = pgm_read_int(&menu->items[index].flags);
397 item_flags = menu->items[index].flags;
399 while (item_flags & MIF_HIDDEN);
406 * Handle a menu and invoke hook functions for the selected menu items.
408 iptr_t menu_handle(const struct Menu *menu)
410 uint8_t items_per_page;
411 uint8_t first_item = 0;
416 #if (CONFIG_MENU_TIMEOUT != 0)
417 ticks_t now, menu_idle_time = timer_clock();
420 #if CONFIG_MENU_MENUBAR
422 const_iptr_t labels[] =
424 (const_iptr_t)LABEL_BACK,
425 (const_iptr_t)LABEL_UPARROW,
426 (const_iptr_t)LABEL_DOWNARROW,
431 * Initialize menu bar
433 if (menu->flags & MF_TOPLEVEL)
434 labels[0] = (const_iptr_t)LABEL_EMPTY;
436 mbar_init(&mb, menu->bitmap, labels, countof(labels));
437 #endif /* CONFIG_MENU_MENUBAR */
441 (menu->bitmap->height / menu->bitmap->font->height)
442 #if CONFIG_MENU_MENUBAR
443 - 1 /* menu bar labels */
445 - (menu->title ? 1 : 0);
447 /* Selected item should be a visible entry */
448 //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
449 selected = menu->selected;
457 * Keep selected item visible
459 while (selected < first_item)
460 first_item = menu_prev_visible_item(menu, first_item);
461 while (selected >= first_item + items_per_page)
462 first_item = menu_next_visible_item(menu, first_item);
464 menu_layout(menu, first_item, selected, redraw);
467 #if CONFIG_MENU_MENUBAR
468 menu_update_menubar(menu, &mb, selected);
471 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
477 #if (CONFIG_MENU_TIMEOUT != 0)
478 /* Reset idle timer on key press. */
481 menu_idle_time = now;
486 struct MenuItem *item = &(menu->items[selected]);
489 if (menu->flags & MF_ROMITEMS)
491 memcpy_P(&ram_item, item, sizeof(ram_item));
495 result = menu_doselect(menu, item);
498 /* Return immediately */
499 if (!(menu->flags & MF_STICKY))
502 #if (CONFIG_MENU_TIMEOUT != 0)
504 if ((result == MENU_TIMEOUT) && !(menu->flags & MF_TOPLEVEL))
508 menu_idle_time = timer_clock();
513 selected = menu_prev_visible_item(menu, selected);
516 else if (key & K_DOWN)
518 selected = menu_next_visible_item(menu, selected);
521 else if (!(menu->flags & MF_TOPLEVEL))
525 result = MENU_CANCEL;
529 #if CONFIG_MENU_TIMEOUT != 0
530 if (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
532 result = MENU_TIMEOUT;
539 /* Store currently selected item before leaving. */
540 if (menu->flags & MF_SAVESEL)
541 CONST_CAST(struct Menu *, menu)->selected = selected;
548 * Set flags on a menuitem.
550 * \param menu Menu owner of the item to change.
551 * \param idx Index of the menu item.
552 * \param flags Bit mask of the flags to set.
556 int menu_setFlags(struct Menu *menu, int idx, int flags)
558 ASSERT(idx < menu_count(menu));
559 ASSERT(!(menu->flags & MF_ROMITEMS));
561 int old = menu->items[idx].flags;
562 menu->items[idx].flags |= flags;
568 * Clear flags on a menuitem.
570 * \param menu Menu owner of the item to change.
571 * \param idx Index of the menu item.
572 * \param flags Bit mask of the flags to clear.
576 int menu_clearFlags(struct Menu *menu, int idx, int flags)
578 ASSERT(idx < menu_count(menu));
579 ASSERT(!(menu->flags & MF_ROMITEMS));
581 int old = menu->items[idx].flags;
582 menu->items[idx].flags &= ~flags;