menu: re-enable harvard specific optimizationn that was commented out, but seems...
[bertos.git] / bertos / gui / menu.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \version $Id$
35  *
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  * \author Stefano Fedrigo <aleph@develer.com>
38  *
39  * \brief General pourpose menu handling functions
40  */
41
42 #include "menu.h"
43
44 #include "cfg/cfg_gfx.h"
45 #include <cfg/compiler.h>
46 #include <cfg/debug.h>
47
48 #include <gfx/gfx.h>
49 #include <gfx/font.h>
50 #include <gfx/text.h>
51
52 #include <drv/kbd.h>
53
54 #include <string.h> /* strcpy() */
55
56 #if CPU_HARVARD
57 #include <avr/pgmspace.h> /* strncpy_P() */
58 #endif
59
60 #if CONFIG_MENU_SMOOTH
61 #include <drv/lcd_gfx.h>
62 #endif
63
64 #if (CONFIG_MENU_TIMEOUT != 0)
65 #include <drv/timer.h>
66 #endif
67
68 #if CONFIG_MENU_MENUBAR
69 #include "menubar.h"
70 #endif
71
72 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
73 #include "msg.h"
74 #else
75 #define PTRMSG(x) ((const char *)x)
76 #endif
77
78
79 /* Temporary fake defines for ABORT stuff... */
80 #define abort_top  0
81 #define PUSH_ABORT false
82 #define POP_ABORT  do {} while(0)
83 #define DO_ABORT   do {} while(0)
84
85
86 /**
87  * Return the total number of items in in a menu.
88  */
89 static int menu_count(const struct Menu *menu)
90 {
91         int cnt = 0;
92
93         for (cnt = 0; /*NOP*/; ++cnt)
94         {
95                 const MenuItem *item = &menu->items[cnt];
96 #if CPU_HARVARD
97                 MenuItem ram_item;
98                 if (menu->flags & MF_ROMITEMS)
99                 {
100                         memcpy_P(&ram_item, item, sizeof(ram_item));
101                         item = &ram_item;
102                 }
103 #endif
104                 if (!(item->label || item->hook))
105                         break;
106         }
107
108         return cnt;
109 }
110
111 #if CONFIG_MENU_MENUBAR
112
113 /**
114  * Update the menu bar according to the selected item and redraw it.
115  */
116 static void menu_update_menubar(
117                 const struct Menu *menu,
118                 struct MenuBar *mb,
119                 int selected)
120 {
121         int item_flags;
122 #if CPU_HARVARD
123         if (menu->flags & MF_ROMITEMS)
124         {
125                 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
126                 item_flags = pgm_read_int(&menu->items[selected].flags);
127         }
128         else
129 #endif
130                 item_flags = menu->items[selected].flags;
131
132         const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
133
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)
139         {
140                 newlabel = (item_flags & MIF_CHECKED) ?
141                         (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
142         }
143
144         mb->labels[3] = newlabel;
145         mbar_draw(mb);
146 }
147 #endif /* CONFIG_MENU_MENUBAR */
148
149
150 static void menu_defaultRenderHook(struct Bitmap *bm, int ypos, bool selected, const struct MenuItem *item)
151 {
152         if (item->flags & MIF_CHECKIT)
153         {
154                 gfx_rectClear(bm, 0, ypos,
155                                 bm->font->height, ypos + bm->font->height);
156
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)
161                 {
162                         gfx_line(bm,
163                                         3, ypos + 3,
164                                         bm->font->height - 3, ypos + bm->font->height - 3);
165                         gfx_line(bm,
166                                         bm->font->height - 3, ypos + 3,
167                                         3, ypos + bm->font->height - 3);
168                 }
169         }
170
171 #if CPU_HARVARD
172         ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
173 #else
174         text_xyprintf
175 #endif
176         (
177                 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
178                 selected ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
179                 PTRMSG(item->label)
180         );
181 }
182
183 /**
184  * Show a menu on the display.
185  */
186 static void menu_layout(
187                 const struct Menu *menu,
188                 int first_item,
189                 int selected,
190                 bool redraw)
191 {
192         coord_t ypos;
193         int i;
194         const char * PROGMEM title = PTRMSG(menu->title);
195         Bitmap *bm = menu->bitmap;
196
197         ypos = bm->cr.ymin;
198
199 #if 1
200         if (redraw)
201         {
202                 /* Clear screen */
203                 text_clear(menu->bitmap);
204         }
205 #endif
206
207         if (title)
208         {
209                 if (redraw)
210                         text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
211                 ypos += bm->font->height;
212         }
213
214 #if CONFIG_MENU_SMOOTH
215         static coord_t yoffset = 0;
216         static int old_first_item = 0;
217         static int speed;
218         coord_t old_ymin = bm->cr.ymin;
219
220         /* Clip drawing inside menu items area */
221         gfx_setClipRect(bm,
222                 bm->cr.xmin, bm->cr.ymin + ypos,
223                 bm->cr.xmax, bm->cr.ymax);
224
225         if (old_first_item != first_item)
226         {
227                 /* Speed proportional to distance */
228                 speed = ABS(old_first_item - first_item) * 3;
229
230                 if (old_first_item > first_item)
231                 {
232                         yoffset += speed;
233                         if (yoffset > bm->font->height)
234                         {
235                                         yoffset = 0;
236                                         --old_first_item;
237                         }
238                 }
239                 else
240                 {
241                         yoffset -= speed;
242                         if (yoffset < -bm->font->height)
243                         {
244                                         yoffset = 0;
245                                         ++old_first_item;
246                         }
247                 }
248                 first_item = MIN(old_first_item, menu_count(menu));
249
250                 ypos += yoffset;
251                 redraw = true;
252         }
253 #endif /* CONFIG_MENU_SMOOTH */
254
255         if (redraw) for (i = first_item; /**/; ++i)
256         {
257                 const MenuItem *item = &menu->items[i];
258 #if CPU_HARVARD
259                 MenuItem ram_item;
260                 if (menu->flags & MF_ROMITEMS)
261                 {
262                         memcpy_P(&ram_item, item, sizeof(ram_item));
263                         item = &ram_item;
264                 }
265 #endif /* CPU_HARVARD */
266
267                 /* Check for end of room */
268                 if (ypos > bm->cr.ymax)
269                         break;
270
271                 /* Check for end of menu */
272                 if (!(item->label || item->hook))
273                         break;
274
275                 /* Only print visible items */
276                 if (!(item->flags & MIF_HIDDEN))
277                 {
278                         /* Check if a special render function is supplied, otherwise use defaults */
279                         RenderHook renderhook = (item->flags & MIF_RENDERHOOK) ? (RenderHook)item->label : menu_defaultRenderHook;
280
281                         /* Render menuitem */
282                         renderhook(menu->bitmap, ypos++, (i == selected), item);
283
284                         ypos += bm->font->height;
285                 }
286         }
287
288 #if CONFIG_MENU_SMOOTH
289         if (redraw)
290         {
291                 /* Clear rest of area */
292                 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
293
294                 lcd_blitBitmap(&lcd_bitmap);
295         }
296
297         /* Restore old cliprect */
298         gfx_setClipRect(bm,
299                         bm->cr.xmin, old_ymin,
300                         bm->cr.xmax, bm->cr.ymax);
301
302 #endif /* CONFIG_MENU_SMOOTH */
303 }
304
305
306 /**
307  * Handle menu item selection
308  */
309 static iptr_t menu_doselect(const struct Menu *menu, struct MenuItem *item)
310 {
311         iptr_t result = 0;
312
313         /* Exclude other items */
314         int mask, i;
315         for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
316         {
317                 if (mask & 1)
318                         menu->items[i].flags &= ~MIF_CHECKED;
319         }
320
321         if (item->flags & MIF_DISABLED)
322                 return MENU_DISABLED;
323
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;
329
330         /* Handle items with callback hooks */
331         if (item->hook)
332         {
333                 /* Push a jmp buffer to abort the operation with the STOP/CANCEL key */
334                 if (!PUSH_ABORT)
335                 {
336                         result = item->hook(item->userdata);
337                         POP_ABORT;
338                 }
339         }
340         else
341                 result = item->userdata;
342
343         return result;
344 }
345
346
347 /**
348  * Return the next visible item (rolls back to the first item)
349  */
350 static int menu_next_visible_item(const struct Menu *menu, int index)
351 {
352         int total = menu_count(menu);
353         int item_flags;
354
355         do
356         {
357                 if (++index >= total)
358                    index = 0;
359
360 #if CPU_HARVARD
361                 if (menu->flags & MF_ROMITEMS)
362                 {
363                         ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
364                         item_flags = pgm_read_int(&menu->items[index].flags);
365                 }
366                 else
367 #endif
368                         item_flags = menu->items[index].flags;
369         }
370         while (item_flags & MIF_HIDDEN);
371
372         return index;
373 }
374
375
376 /**
377  * Return the previous visible item (rolls back to the last item)
378  */
379 static int menu_prev_visible_item(const struct Menu *menu, int index)
380 {
381         int total = menu_count(menu);
382         int item_flags;
383
384         do
385         {
386                 if (--index < 0)
387                         index = total - 1;
388
389 #if CPU_HARVARD
390                 if (menu->flags & MF_ROMITEMS)
391                 {
392                         ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
393                         item_flags = pgm_read_int(&menu->items[index].flags);
394                 }
395                 else
396 #endif
397                         item_flags = menu->items[index].flags;
398         }
399         while (item_flags & MIF_HIDDEN);
400
401         return index;
402 }
403
404
405 /**
406  * Handle a menu and invoke hook functions for the selected menu items.
407  */
408 iptr_t menu_handle(const struct Menu *menu)
409 {
410         uint8_t items_per_page;
411         uint8_t first_item = 0;
412         uint8_t selected;
413         iptr_t result = 0;
414         bool redraw = true;
415
416 #if (CONFIG_MENU_TIMEOUT != 0)
417         ticks_t now, menu_idle_time = timer_clock();
418 #endif
419
420 #if CONFIG_MENU_MENUBAR
421         struct MenuBar mb;
422         const_iptr_t labels[] =
423         {
424                 (const_iptr_t)LABEL_BACK,
425                 (const_iptr_t)LABEL_UPARROW,
426                 (const_iptr_t)LABEL_DOWNARROW,
427                 (const_iptr_t)0
428         };
429
430         /*
431          * Initialize menu bar
432          */
433         if (menu->flags & MF_TOPLEVEL)
434                 labels[0] = (const_iptr_t)LABEL_EMPTY;
435
436         mbar_init(&mb, menu->bitmap, labels, countof(labels));
437 #endif /* CONFIG_MENU_MENUBAR */
438
439
440         items_per_page =
441                 (menu->bitmap->height / menu->bitmap->font->height)
442 #if CONFIG_MENU_MENUBAR
443                 - 1 /* menu bar labels */
444 #endif
445                 - (menu->title ? 1 : 0);
446
447         /* Selected item should be a visible entry */
448         //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
449         selected = menu->selected;
450         first_item = 0;
451
452         for(;;)
453         {
454                 keymask_t key;
455
456                 /*
457                  * Keep selected item visible
458                  */
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);
463
464                 menu_layout(menu, first_item, selected, redraw);
465                 redraw = false;
466
467                 #if CONFIG_MENU_MENUBAR
468                         menu_update_menubar(menu, &mb, selected);
469                 #endif
470
471                 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
472                         key = kbd_peek();
473                 #else
474                         key = kbd_get();
475                 #endif
476
477                 #if (CONFIG_MENU_TIMEOUT != 0)
478                         /* Reset idle timer on key press. */
479                         now = timer_clock();
480                         if (key)
481                                 menu_idle_time = now;
482                 #endif
483
484                 if (key & K_OK)
485                 {
486                         struct MenuItem *item = &(menu->items[selected]);
487 #if CPU_HARVARD
488                         MenuItem ram_item;
489                         if (menu->flags & MF_ROMITEMS)
490                         {
491                                 memcpy_P(&ram_item, item, sizeof(ram_item));
492                                 item = &ram_item;
493                         }
494 #endif
495                         result = menu_doselect(menu, item);
496                         redraw = true;
497
498                         /* Return immediately */
499                         if (!(menu->flags & MF_STICKY))
500                                 break;
501
502                         #if (CONFIG_MENU_TIMEOUT != 0)
503                                 /* Chain timeout */
504                                 if ((result == MENU_TIMEOUT) && !(menu->flags & MF_TOPLEVEL))
505                                         break;
506
507                                 /* Reset timeout */
508                                 menu_idle_time = timer_clock();
509                         #endif
510                 }
511                 else if (key & K_UP)
512                 {
513                         selected = menu_prev_visible_item(menu, selected);
514                         redraw = true;
515                 }
516                 else if (key & K_DOWN)
517                 {
518                         selected = menu_next_visible_item(menu, selected);
519                         redraw = true;
520                 }
521                 else if (!(menu->flags & MF_TOPLEVEL))
522                 {
523                         if (key & K_CANCEL)
524                         {
525                                 result = MENU_CANCEL;
526                                 break;
527                         }
528
529                         #if CONFIG_MENU_TIMEOUT != 0
530                                 if (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
531                                 {
532                                         result = MENU_TIMEOUT;
533                                         break;
534                                 }
535                         #endif
536                 }
537         }
538
539         /* Store currently selected item before leaving. */
540         if (menu->flags & MF_SAVESEL)
541                 CONST_CAST(struct Menu *, menu)->selected = selected;
542
543         return result;
544 }
545
546
547 /**
548  * Set flags on a menuitem.
549  *
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.
553  *
554  * \return Old flags.
555  */
556 int menu_setFlags(struct Menu *menu, int idx, int flags)
557 {
558         ASSERT(idx < menu_count(menu));
559         ASSERT(!(menu->flags & MF_ROMITEMS));
560
561         int old = menu->items[idx].flags;
562         menu->items[idx].flags |= flags;
563         return old;
564 }
565
566
567 /**
568  * Clear flags on a menuitem.
569  *
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.
573  *
574  * \return Old flags.
575  */
576 int menu_clearFlags(struct Menu *menu, int idx, int flags)
577 {
578         ASSERT(idx < menu_count(menu));
579         ASSERT(!(menu->flags & MF_ROMITEMS));
580
581         int old = menu->items[idx].flags;
582         menu->items[idx].flags &= ~flags;
583         return old;
584 }