Fix naming conventions.
[bertos.git] / mware / menu.c
index 1349f215b664896140a2127c3a25637ac8fc08d1..9c1149dbded7043611bc52e3276561e81a86599d 100755 (executable)
@@ -1,7 +1,7 @@
 /*!
  * \file
  * <!--
- * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
  * All Rights Reserved.
  * -->
 
 /*#*
  *#* $Log$
+ *#* Revision 1.7  2006/04/27 05:39:24  bernie
+ *#* Enhance text rendering to arbitrary x,y coords.
+ *#*
+ *#* Revision 1.6  2006/04/11 00:07:32  bernie
+ *#* Implemenent MF_SAVESEL flag.
+ *#*
+ *#* Revision 1.5  2006/03/22 09:49:51  bernie
+ *#* Simplifications from project_grl.
+ *#*
  *#* Revision 1.4  2006/03/20 17:48:35  bernie
  *#* Implement support for ROM menus.
  *#*
@@ -72,7 +81,6 @@
  *#*/
 
 #include "menu.h"
-
 #include <gfx/gfx.h>
 #include <gfx/font.h>
 #include <gfx/text.h>
 #include <cfg/compiler.h>
 #include <cfg/debug.h>
 #include <appconfig.h>
+#include <string.h> /* strcpy() */
 
 #if CPU_HARVARD
 #include <avr/pgmspace.h> /* strncpy_P() */
 #endif
 
-#include <string.h> /* strcpy() */
+#if CONFIG_MENU_SMOOTH
+#include <drv/lcd_gfx.h>
+#endif
 
 #if CONFIG_MENU_MENUBAR
 #include "menubar.h"
 #define PTRMSG(x) ((const char *)x)
 #endif
 
-
 /* Temporary fake defines for ABORT stuff... */
 #define abort_top  0
 #define PUSH_ABORT false
 #define DO_ABORT   do {} while(0)
 
 
-#ifdef _DEBUG
 /**
  * Count the items present in a menu.
  */
@@ -130,8 +139,6 @@ static int menu_count(const struct Menu *menu)
 
        return cnt;
 }
-#endif /* _DEBUG */
-
 
 #if CONFIG_MENU_MENUBAR
 
@@ -173,8 +180,8 @@ static void menu_update_menubar(
 #endif /* CONFIG_MENU_MENUBAR */
 
 
-/*!
- * Show a menu on the LCD display.
+/**
+ * Show a menu on the display.
  */
 static void menu_layout(
                const struct Menu *menu,
@@ -184,11 +191,53 @@ static void menu_layout(
 {
        int ypos, cnt;
        const char * PROGMEM title = PTRMSG(menu->title);
+       Bitmap *bm = menu->bitmap;
 
-       ypos = menu->startrow;
+       ypos = bm->cr.ymin;
 
        if (title)
-               text_xprintf(menu->bitmap, ypos++, 0, STYLEF_BOLD | TEXT_FILL, title);
+       {
+               text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
+               ypos += bm->font->height;
+       }
+
+#if CONFIG_MENU_SMOOTH
+       static coord_t yoffset = 0;
+       static int old_first_item = 0;
+       static mtime_t old_time = 0; //UNUSED
+       static int speed;
+       coord_t old_ymin = bm->cr.ymin;
+
+       gfx_setClipRect(bm,
+               bm->cr.xmin, bm->cr.ymin + ypos,
+               bm->cr.xmax, bm->cr.ymax);
+
+       if (old_first_item != first_item)
+       {
+               speed = ABS(old_first_item - first_item) * 3;
+
+               if (old_first_item > first_item)
+               {
+                       yoffset += speed;
+                       if (yoffset > bm->font->height)
+                       {
+                                       yoffset = 0;
+                                       --old_first_item;
+                       }
+               }
+               else
+               {
+                       yoffset -= speed;
+                       if (yoffset < -bm->font->height)
+                       {
+                                       yoffset = 0;
+                                       ++old_first_item;
+                       }
+               }
+               first_item = old_first_item;
+       }
+       ypos += yoffset;
+#endif
 
        for (cnt = 0; cnt < items_per_page; ++cnt)
        {
@@ -210,12 +259,12 @@ static void menu_layout(
                if (!(item->flags & MIF_HIDDEN))
                {
 #if CPU_HARVARD
-                       text_xprintf_P
+                       text_xyprintf_P
 #else
-                       text_xprintf
+                       text_xyprintf
 #endif
                        (
-                               menu->bitmap, ypos++, 0,
+                               bm, 0, ypos,
                                (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
                                (item->flags & MIF_RAMLABEL) ? PSTR("%s%S") : PSTR("%S%S"),
                                PTRMSG(item->label),
@@ -223,8 +272,18 @@ static void menu_layout(
                                        ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
                                        : ( (item->flags & MIF_CHECKED) ? PSTR("\04") : PSTR("") )
                        );
+                       ypos += bm->font->height;
                }
        }
+
+#if CONFIG_MENU_SMOOTH
+       /* Restore old cliprect */
+       gfx_setClipRect(bm,
+                       bm->cr.xmin, old_ymin,
+                       bm->cr.xmax, bm->cr.ymax);
+
+       lcd_blitBitmap(&lcd_bitmap);
+#endif
 }
 
 
@@ -263,11 +322,12 @@ static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
 }
 
 
-/*!
+/**
  * Return the next visible item (rolls back to the first item)
  */
-static int menu_next_visible_item(const struct Menu *menu, int index, int total)
+static int menu_next_visible_item(const struct Menu *menu, int index)
 {
+       int total = menu_count(menu);
        int item_flags;
 
        do
@@ -291,11 +351,12 @@ static int menu_next_visible_item(const struct Menu *menu, int index, int total)
 }
 
 
-/*!
+/**
  * Return the previous visible item (rolls back to the last item)
  */
-static int menu_prev_visible_item(const struct Menu *menu, int index, int total)
+static int menu_prev_visible_item(const struct Menu *menu, int index)
 {
+       int total = menu_count(menu);
        int item_flags;
 
        do
@@ -324,7 +385,7 @@ static int menu_prev_visible_item(const struct Menu *menu, int index, int total)
  */
 iptr_t menu_handle(const struct Menu *menu)
 {
-       uint8_t entries, visible_entries, items_per_page;
+       uint8_t items_per_page;
        uint8_t first_item, selected;
 
 #if CONFIG_MENU_MENUBAR
@@ -347,40 +408,18 @@ iptr_t menu_handle(const struct Menu *menu)
 #endif /* CONFIG_MENU_MENUBAR */
 
 
-       /*
-        * Compute total number of items in menu (entries) and
-        * the number of visible entries, which excludes items
-        * without a label.
-        */
-       for (entries = 0, visible_entries = 0; /*NOP*/; ++entries)
-       {
-               const MenuItem *item = &menu->items[entries];
-#if CPU_HARVARD
-               MenuItem ram_item;
-               if (menu->flags & MF_ROMITEMS)
-               {
-                       memcpy_P(&ram_item, item, sizeof(ram_item));
-                       item = &ram_item;
-               }
-#endif
-
-               if (!(item->flags & MIF_HIDDEN))
-                       ++visible_entries;
-
-               if (!(item->label || item->hook))
-                       break;
-       }
-
        items_per_page =
                (menu->bitmap->height / menu->bitmap->font->height)
-               - menu->startrow
 #if CONFIG_MENU_MENUBAR
                - 1 /* menu bar labels */
 #endif
                - (menu->title ? 1 : 0);
 
        /* Selected item should be a visible entry */
-       first_item = selected = menu_next_visible_item(menu, -1, entries);
+       first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
+
+       /* Clear screen */
+       text_clear(menu->bitmap);
 
        for(;;)
        {
@@ -390,19 +429,21 @@ iptr_t menu_handle(const struct Menu *menu)
                 * Keep selected item visible
                 */
                while (selected < first_item)
-                       first_item = menu_prev_visible_item(menu, first_item, entries);
+                       first_item = menu_prev_visible_item(menu, first_item);
                while (selected >= first_item + items_per_page)
-                       first_item = menu_next_visible_item(menu, first_item, entries);
+                       first_item = menu_next_visible_item(menu, first_item);
 
-               /* Clear screen */
-               text_clear(menu->bitmap);
                menu_layout(menu, first_item, items_per_page, selected);
 
-#if CONFIG_MENU_MENUBAR
-               menu_update_menubar(menu, &mb, selected);
-#endif
+               #if CONFIG_MENU_MENUBAR
+                       menu_update_menubar(menu, &mb, selected);
+               #endif
 
+#if CONFIG_MENU_SMOOTH
+               key = kbd_peek();
+#else
                key = kbd_get();
+#endif
 
                if (key & K_OK)
                {
@@ -419,18 +460,29 @@ iptr_t menu_handle(const struct Menu *menu)
 
                        /* Return userdata as result */
                        if (!menu->flags & MF_STICKY)
+                       {
+                               /* Store currently selected item before leaving. */
+                               if (menu->flags & MF_SAVESEL)
+                                       CONST_CAST(struct Menu *, menu)->selected = selected;
                                return item->userdata;
+                       }
+
+                       /* Clear screen */
+                       text_clear(menu->bitmap);
                }
                else if (key & K_UP)
                {
-                       selected = menu_prev_visible_item(menu, selected, entries);
+                       selected = menu_prev_visible_item(menu, selected);
                }
                else if (key & K_DOWN)
                {
-                       selected = menu_next_visible_item(menu, selected, entries);
+                       selected = menu_next_visible_item(menu, selected);
                }
                else if (key & K_CANCEL && !(menu->flags & MF_TOPLEVEL))
                {
+                       /* Store currently selected item before leaving. */
+                       if (menu->flags & MF_SAVESEL)
+                               CONST_CAST(struct Menu *, menu)->selected = selected;
                        return 0;
                }
        }