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