Simplifications from Grillo.
[bertos.git] / mware / menu.c
index 2aa23abd873a6a31c3d8941b1c1c8537fde5e5e3..8fdbcb136b4276214db6b257db369f6b408e9b68 100755 (executable)
 
 /*#*
  *#* $Log$
+ *#* 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.
+ *#*
  *#* Revision 1.3  2006/02/20 14:34:32  bernie
  *#* Include appconfig.h before using its definitions.
  *#*
@@ -69,7 +75,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_MENUBAR
 #include "menubar.h"
 #endif
 #define DO_ABORT   do {} while(0)
 
 
-#ifdef _DEBUG
-/*!
+/**
  * Count the items present in a menu.
  */
-static int UNUSED_FUNC menu_count(const struct Menu *menu)
+static int menu_count(const struct Menu *menu)
 {
        int cnt = 0;
-       struct MenuItem *item;
 
-       for (item = menu->items; item->label; ++item)
-               cnt++;
+       for (cnt = 0; /*NOP*/; ++cnt)
+       {
+               const MenuItem *item = &menu->items[cnt];
+#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->label || item->hook))
+                       break;
+       }
 
        return cnt;
 }
-#endif /* _DEBUG */
 
+#if 0 /* UNUSED */
+/**
+ * Compute total number of visible entries, which excludes items
+ * without a label.
+ */
+static int menu_count_visible(const struct Menu *menu)
+{
+       struct MenuItem *item;
+       int visible_entries = 0;
+
+       for (item = menu->items; (item->label || item->hook); ++item)
+       {
+               if (!(item->flags & MIF_HIDDEN))
+                       ++visible_entries;
+       }
+
+       return visible_entries;
+}
+#endif
 
 #if CONFIG_MENU_MENUBAR
 
@@ -130,7 +162,17 @@ static void menu_update_menubar(
                struct MenuBar *mb,
                int selected)
 {
-       int item_flags = menu->items[selected].flags;
+       int item_flags;
+#if CPU_HARVARD
+       if (menu->flags & MF_ROMITEMS)
+       {
+               ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
+               item_flags = pgm_read_int(&menu->items[selected].flags);
+       }
+       else
+#endif
+               item_flags = menu->items[selected].flags;
+
        const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
 
        if (item_flags & MIF_DISABLED)
@@ -149,8 +191,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,
@@ -158,20 +200,26 @@ static void menu_layout(
                int items_per_page,
                int selected)
 {
-       const MenuItem *item;
        int ypos, cnt;
        const char * PROGMEM title = PTRMSG(menu->title);
 
        ypos = menu->startrow;
 
        if (title)
-               text_xprintf(menu->bitmap, ypos++, 0, STYLEF_BOLD | TEXT_FILL, title);
+               text_xprintf(menu->bitmap, ypos++, 0, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
 
-       for (
-               cnt = 0, item = &menu->items[first_item];
-               cnt < items_per_page;
-               ++cnt, ++item)
+       for (cnt = 0; cnt < items_per_page; ++cnt)
        {
+               const MenuItem *item = &menu->items[first_item + cnt];
+#if CPU_HARVARD
+               MenuItem ram_item;
+               if (menu->flags & MF_ROMITEMS)
+               {
+                       memcpy_P(&ram_item, item, sizeof(ram_item));
+                       item = &ram_item;
+               }
+#endif
+
                /* Check for end of menu */
                if (!(item->label || item->hook))
                        break;
@@ -187,7 +235,7 @@ static void menu_layout(
                        (
                                menu->bitmap, ypos++, 0,
                                (first_item + cnt == selected) ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
-                               (item->flags & MIF_RAMLABEL) ? PSTR("%s%s") : PSTR("%S%s"),
+                               (item->flags & MIF_RAMLABEL) ? PSTR("%s%S") : PSTR("%S%S"),
                                PTRMSG(item->label),
                                (item->flags & MIF_TOGGLE) ?
                                        ( (item->flags & MIF_CHECKED) ? PSTR(":ON") : PSTR(":OFF") )
@@ -233,33 +281,59 @@ static void menu_doselect(const struct Menu *menu, struct MenuItem *item)
 }
 
 
-/*!
- * Return the previous visible item (rolls back to the last 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
        {
                if (++index >= total)
                   index = 0;
+
+#if CPU_HARVARD
+               if (menu->flags & MF_ROMITEMS)
+               {
+                       ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
+                       item_flags = pgm_read_int(&menu->items[index].flags);
+               }
+               else
+#endif
+                       item_flags = menu->items[index].flags;
        }
-       while (menu->items[index].flags & MIF_HIDDEN);
+       while (item_flags & MIF_HIDDEN);
 
        return index;
 }
 
 
-/*!
- * Return the next visible item (rolls back to the first item)
+/**
+ * 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
        {
                if (--index < 0)
                        index = total - 1;
+
+#if CPU_HARVARD
+               if (menu->flags & MF_ROMITEMS)
+               {
+                       ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
+                       item_flags = pgm_read_int(&menu->items[index].flags);
+               }
+               else
+#endif
+                       item_flags = menu->items[index].flags;
        }
-       while (menu->items[index].flags & MIF_HIDDEN);
+       while (item_flags & MIF_HIDDEN);
 
        return index;
 }
@@ -270,7 +344,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
@@ -293,24 +367,6 @@ 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.
-        */
-       {
-               struct MenuItem *item;
-
-               entries = 0;
-               visible_entries = 0;
-
-               for (item = menu->items; (item->label || item->hook); ++item)
-               {
-                       ++entries;
-                       if (!(item->flags & MIF_HIDDEN))
-                               ++visible_entries;
-               }
-       }
-
        items_per_page =
                (menu->bitmap->height / menu->bitmap->font->height)
                - menu->startrow
@@ -320,7 +376,7 @@ iptr_t menu_handle(const struct Menu *menu)
                - (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, -1);
 
        for(;;)
        {
@@ -330,23 +386,31 @@ 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
 
                key = kbd_get();
 
                if (key & K_OK)
                {
                        struct MenuItem *item = &(menu->items[selected]);
+#if CPU_HARVARD
+                       MenuItem ram_item;
+                       if (menu->flags & MF_ROMITEMS)
+                       {
+                               memcpy_P(&ram_item, item, sizeof(ram_item));
+                               item = &ram_item;
+                       }
+#endif
                        menu_doselect(menu, item);
 
                        /* Return userdata as result */
@@ -355,11 +419,11 @@ iptr_t menu_handle(const struct Menu *menu)
                }
                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))
                {
@@ -381,6 +445,7 @@ iptr_t menu_handle(const struct Menu *menu)
 int menu_setFlags(struct Menu *menu, int idx, int flags)
 {
        ASSERT(idx < menu_count(menu));
+       ASSERT(!(menu->flags & MF_ROMITEMS));
 
        int old = menu->items[idx].flags;
        menu->items[idx].flags |= flags;
@@ -400,6 +465,7 @@ int menu_setFlags(struct Menu *menu, int idx, int flags)
 int menu_clearFlags(struct Menu *menu, int idx, int flags)
 {
        ASSERT(idx < menu_count(menu));
+       ASSERT(!(menu->flags & MF_ROMITEMS));
 
        int old = menu->items[idx].flags;
        menu->items[idx].flags &= ~flags;