1
0
Fork 0
forked from len0rd/rockbox

Added support for nested menus

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@707 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Björn Stenberg 2002-05-26 17:03:17 +00:00
parent a057e5cce9
commit 2ac057241a
2 changed files with 60 additions and 41 deletions

View file

@ -16,48 +16,66 @@
* KIND, either express or implied. * KIND, either express or implied.
* *
****************************************************************************/ ****************************************************************************/
#include <stdbool.h>
#include "lcd.h" #include "lcd.h"
#include "menu.h" #include "menu.h"
#include "button.h" #include "button.h"
#include "kernel.h" #include "kernel.h"
#include "debug.h" #include "debug.h"
/* Global values for menuing */ struct menu {
static int menu_top; int menu_top;
static int menu_bottom; int menu_bottom;
static int cursor; int cursor;
static struct menu_items* items; struct menu_items* items;
static int itemcount; int itemcount;
};
#define MAX_MENUS 4
static struct menu menus[MAX_MENUS];
static bool inuse[MAX_MENUS] = { false };
/* /*
* Move the cursor to a particular id, * Move the cursor to a particular id,
* target: where you want it to be * target: where you want it to be
*/ */
static void put_cursor(int target) static void put_cursor(int m, int target)
{ {
lcd_puts(0, cursor, " "); lcd_puts(0, menus[m].cursor, " ");
cursor = target; menus[m].cursor = target;
lcd_puts(0, cursor, "-"); lcd_puts(0, menus[m].cursor, "-");
} }
/* We call the function pointer related to the current cursor position */ int menu_init(struct menu_items* mitems, int count)
static void execute_menu_item(void)
{ {
/* call the proper function for this line */ int i;
items[cursor].function();
for ( i=0; i<MAX_MENUS; i++ ) {
if ( !inuse[i] ) {
inuse[i] = true;
break;
}
}
if ( i == MAX_MENUS ) {
DEBUGF("Out of menus!\n");
return -1;
}
menus[i].items = mitems;
menus[i].itemcount = count;
menus[i].menu_top = 0;
menus[i].menu_bottom = count-1;
menus[i].cursor = 0;
return i;
} }
void menu_init(struct menu_items* mitems, int count) void menu_exit(int m)
{ {
items = mitems; inuse[m] = false;
itemcount = count;
menu_top = items[0].id;
menu_bottom = count-1;
cursor = menu_top;
} }
static void menu_draw(void) static void menu_draw(int m)
{ {
int i = 0; int i = 0;
@ -66,23 +84,23 @@ static void menu_draw(void)
lcd_setmargins(0,0); lcd_setmargins(0,0);
lcd_setfont(0); lcd_setfont(0);
#endif #endif
for (i = 0; i < itemcount; i++) { for (i = 0; i < menus[m].itemcount; i++) {
lcd_puts(1, i, items[i].desc); lcd_puts(1, i, menus[m].items[i].desc);
if (i < menu_top) if (i < menus[m].menu_top)
menu_top = i; menus[m].menu_top = i;
if (i > menu_bottom) if (i > menus[m].menu_bottom)
menu_bottom = i; menus[m].menu_bottom = i;
} }
lcd_puts(0, cursor, "-"); lcd_puts(0, menus[m].cursor, "-");
lcd_update(); lcd_update();
} }
void menu_run(void) void menu_run(int m)
{ {
int key; int key;
menu_draw(); menu_draw(m);
while(1) { while(1) {
key = button_get(); key = button_get();
@ -97,12 +115,12 @@ void menu_run(void)
#else #else
case BUTTON_LEFT: case BUTTON_LEFT:
#endif #endif
if (cursor == menu_top) { if (menus[m].cursor == menus[m].menu_top) {
/* wrap around to menu bottom */ /* wrap around to menu bottom */
put_cursor(menu_bottom); put_cursor(m, menus[m].menu_bottom);
} else { } else {
/* move up */ /* move up */
put_cursor(cursor-1); put_cursor(m, menus[m].cursor-1);
} }
break; break;
@ -111,12 +129,12 @@ void menu_run(void)
#else #else
case BUTTON_RIGHT: case BUTTON_RIGHT:
#endif #endif
if (cursor == menu_bottom) { if (menus[m].cursor == menus[m].menu_bottom) {
/* wrap around to menu top */ /* wrap around to menu top */
put_cursor(menu_top); put_cursor(m, menus[m].menu_top);
} else { } else {
/* move down */ /* move down */
put_cursor(cursor+1); put_cursor(m, menus[m].cursor+1);
} }
break; break;
@ -127,10 +145,10 @@ void menu_run(void)
/* Erase current display state */ /* Erase current display state */
lcd_clear_display(); lcd_clear_display();
execute_menu_item(); menus[m].items[menus[m].cursor].function();
/* Return to previous display state */ /* Return to previous display state */
menu_draw(); menu_draw(m);
break; break;
#ifdef HAVE_RECORDER_KEYPAD #ifdef HAVE_RECORDER_KEYPAD

View file

@ -26,7 +26,8 @@ struct menu_items {
void (*function) (void); void (*function) (void);
}; };
void menu_init(struct menu_items* items, int count); int menu_init(struct menu_items* items, int count);
void menu_run(void); void menu_exit(int menu);
void menu_run(int menu);
#endif /* End __MENU_H__ */ #endif /* End __MENU_H__ */