Convert to new Doxygen style.
[bertos.git] / gui / menu.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
6  * All Rights Reserved.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Stefano Fedrigo <aleph@develer.com>
13  *
14  * \brief General pourpose menu handling functions
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.6  2006/07/19 12:56:27  bernie
20  *#* Convert to new Doxygen style.
21  *#*
22  *#* Revision 1.5  2006/06/03 13:58:01  bernie
23  *#* Fix recursive timeout and add exit status information.
24  *#*
25  *#* Revision 1.4  2006/06/02 12:26:18  bernie
26  *#* Draw graphical checkmarks.
27  *#*
28  *#* Revision 1.3  2006/05/28 15:03:31  bernie
29  *#* Avoid unnecessary rendering.
30  *#*
31  *#* Revision 1.2  2006/05/25 23:34:38  bernie
32  *#* Implement menu timeouts.
33  *#*
34  *#* Revision 1.1  2006/05/15 07:20:54  bernie
35  *#* Move menu to gui/.
36  *#*
37  *#* Revision 1.7  2006/04/27 05:39:24  bernie
38  *#* Enhance text rendering to arbitrary x,y coords.
39  *#*
40  *#* Revision 1.6  2006/04/11 00:07:32  bernie
41  *#* Implemenent MF_SAVESEL flag.
42  *#*
43  *#* Revision 1.5  2006/03/22 09:49:51  bernie
44  *#* Simplifications from project_grl.
45  *#*
46  *#* Revision 1.4  2006/03/20 17:48:35  bernie
47  *#* Implement support for ROM menus.
48  *#*
49  *#* Revision 1.3  2006/02/20 14:34:32  bernie
50  *#* Include appconfig.h before using its definitions.
51  *#*
52  *#* Revision 1.2  2006/02/15 09:10:51  bernie
53  *#* Make title bold; Fix height when we have no menubar.
54  *#*/
55
56 #include "menu.h"
57 #include <gfx/gfx.h>
58 #include <gfx/font.h>
59 #include <gfx/text.h>
60 #include <drv/kbd.h>
61 #include <cfg/compiler.h>
62 #include <cfg/debug.h>
63 #include <appconfig.h>
64 #include <string.h> /* strcpy() */
65
66 #if CPU_HARVARD
67 #include <avr/pgmspace.h> /* strncpy_P() */
68 #endif
69
70 #if CONFIG_MENU_SMOOTH
71 #include <drv/lcd_gfx.h>
72 #endif
73
74 #if (CONFIG_MENU_TIMEOUT != 0)
75 #include <drv/timer.h>
76 #endif
77
78 #if CONFIG_MENU_MENUBAR
79 #include "menubar.h"
80 #endif
81
82 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
83 #include "msg.h"
84 #else
85 #define PTRMSG(x) ((const char *)x)
86 #endif
87
88 /* Temporary fake defines for ABORT stuff... */
89 #define abort_top  0
90 #define PUSH_ABORT false
91 #define POP_ABORT  do {} while(0)
92 #define DO_ABORT   do {} while(0)
93
94
95 /**
96  * Return the total number of items in in a menu.
97  */
98 static int menu_count(const struct Menu *menu)
99 {
100         int cnt = 0;
101
102         for (cnt = 0; /*NOP*/; ++cnt)
103         {
104                 const MenuItem *item = &menu->items[cnt];
105 #if CPU_HARVARD
106                 MenuItem ram_item;
107                 if (menu->flags & MF_ROMITEMS)
108                 {
109                         memcpy_P(&ram_item, item, sizeof(ram_item));
110                         item = &ram_item;
111                 }
112 #endif
113                 if (!(item->label || item->hook))
114                         break;
115         }
116
117         return cnt;
118 }
119
120 #if CONFIG_MENU_MENUBAR
121
122 /**
123  * Update the menu bar according to the selected item and redraw it.
124  */
125 static void menu_update_menubar(
126                 const struct Menu *menu,
127                 struct MenuBar *mb,
128                 int selected)
129 {
130         int item_flags;
131 #if CPU_HARVARD
132         if (menu->flags & MF_ROMITEMS)
133         {
134                 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
135                 item_flags = pgm_read_int(&menu->items[selected].flags);
136         }
137         else
138 #endif
139                 item_flags = menu->items[selected].flags;
140
141         const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
142
143         if (item_flags & MIF_DISABLED)
144                 newlabel = (const_iptr_t)LABEL_EMPTY;
145         else if (item_flags & MIF_TOGGLE)
146                 newlabel = (const_iptr_t)LABEL_SEL;
147         else if (item_flags & MIF_CHECKIT)
148         {
149                 newlabel = (item_flags & MIF_CHECKED) ?
150                         (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
151         }
152
153         mb->labels[3] = newlabel;
154         mbar_draw(mb);
155 }
156 #endif /* CONFIG_MENU_MENUBAR */
157
158
159 /**
160  * Show a menu on the display.
161  */
162 static void menu_layout(
163                 const struct Menu *menu,
164                 int first_item,
165                 int selected,
166                 bool redraw)
167 {
168         coord_t ypos;
169         int i;
170         const char * PROGMEM title = PTRMSG(menu->title);
171         Bitmap *bm = menu->bitmap;
172
173         ypos = bm->cr.ymin;
174
175 #if 0
176         if (redraw)
177         {
178                 /* Clear screen */
179                 text_clear(menu->bitmap);
180         }
181 #endif
182
183         if (title)
184         {
185                 if (redraw)
186                         text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
187                 ypos += bm->font->height;
188         }
189
190 #if CONFIG_MENU_SMOOTH
191         static coord_t yoffset = 0;
192         static int old_first_item = 0;
193         static int speed;
194         coord_t old_ymin = bm->cr.ymin;
195
196         /* Clip drawing inside menu items area */
197         gfx_setClipRect(bm,
198                 bm->cr.xmin, bm->cr.ymin + ypos,
199                 bm->cr.xmax, bm->cr.ymax);
200
201         if (old_first_item != first_item)
202         {
203                 /* Speed proportional to distance */
204                 speed = ABS(old_first_item - first_item) * 3;
205
206                 if (old_first_item > first_item)
207                 {
208                         yoffset += speed;
209                         if (yoffset > bm->font->height)
210                         {
211                                         yoffset = 0;
212                                         --old_first_item;
213                         }
214                 }
215                 else
216                 {
217                         yoffset -= speed;
218                         if (yoffset < -bm->font->height)
219                         {
220                                         yoffset = 0;
221                                         ++old_first_item;
222                         }
223                 }
224                 first_item = MIN(old_first_item, menu_count(menu));
225
226                 ypos += yoffset;
227                 redraw = true;
228         }
229 #endif /* CONFIG_MENU_SMOOTH */
230
231         if (redraw) for (i = first_item; /**/; ++i)
232         {
233                 const MenuItem *item = &menu->items[i];
234 #if CPU_HARVARD
235                 MenuItem ram_item;
236                 if (menu->flags & MF_ROMITEMS)
237                 {
238                         memcpy_P(&ram_item, item, sizeof(ram_item));
239                         item = &ram_item;
240                 }
241 #endif /* CPU_HARVARD */
242
243                 /* Check for end of room */
244                 if (ypos > bm->cr.ymax)
245                         break;
246
247                 /* Check for end of menu */
248                 if (!(item->label || item->hook))
249                         break;
250
251                 /* Only print visible items */
252                 if (!(item->flags & MIF_HIDDEN))
253                 {
254                         if (item->flags & MIF_CHECKIT)
255                         {
256                                 gfx_rectClear(bm, 0, ypos,
257                                                 bm->font->height, ypos + bm->font->height);
258
259                                 if (item->flags & MIF_TOGGLE)
260                                         gfx_rectDraw(bm, 2, ypos + 2,
261                                                         bm->font->height - 2, ypos + bm->font->height - 2);
262                                 if (item->flags & MIF_CHECKED)
263                                 {
264                                         gfx_line(bm,
265                                                         3, ypos + 3,
266                                                         bm->font->height - 3, ypos + bm->font->height - 3);
267                                         gfx_line(bm,
268                                                         bm->font->height - 3, ypos + 3,
269                                                         3, ypos + bm->font->height - 3);
270                                 }
271                         }
272
273 #if CPU_HARVARD
274                         ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
275 #else
276                         text_xyprintf
277 #endif
278                         (
279                                 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
280                                 (i == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
281                                 PTRMSG(item->label)
282                         );
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 }