From 5f893be2a354bd569f5fcdf9dda12d9333de16ea Mon Sep 17 00:00:00 2001 From: Jonathan Gordon Date: Sat, 20 Oct 2007 12:32:55 +0000 Subject: add a list API for simple lists which dont need lots of code to run. Example uses in debug_menu.c This API works best if most of the text is static, or not many actions need to acted on. (of course, any list could use this) git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15221 a1c6a512-1295-4272-9138-f99709370657 --- apps/gui/list.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ apps/gui/list.h | 42 +++++++++++++++++++++++ 2 files changed, 145 insertions(+) (limited to 'apps/gui') diff --git a/apps/gui/list.c b/apps/gui/list.c index 41bde5802b..52c1e4a3b9 100644 --- a/apps/gui/list.c +++ b/apps/gui/list.c @@ -1028,3 +1028,106 @@ bool gui_synclist_do_button(struct gui_synclist * lists, } return false; } + +/* Simple use list implementation */ +static int simplelist_line_count = 0; +static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH]; +/* set the amount of lines shown in the list */ +void simplelist_set_line_count(int lines) +{ + if (lines < 0) + lines = 0; + else if (lines > SIMPLELIST_MAX_LINES) + lines = SIMPLELIST_MAX_LINES; + simplelist_line_count = 0; +} +/* get the current amount of lines shown */ +int simplelist_get_line_count(void) +{ + return simplelist_line_count; +} +/* add/edit a line in the list. + if line_number > number of lines shown it adds the line, else it edits the line */ +void simplelist_addline(int line_number, const char *fmt, ...) +{ + va_list ap; + + if (line_number > simplelist_line_count) + { + if (simplelist_line_count < SIMPLELIST_MAX_LINES) + line_number = simplelist_line_count++; + else + return; + } + va_start(ap, fmt); + vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap); + va_end(ap); +} + +static char* simplelist_static_getname(int item, void * data, char *buffer) +{ + (void)data; (void)buffer; + return simplelist_text[item]; +} +bool simplelist_show_list(struct simplelist_info *info) +{ + struct gui_synclist lists; + int action, old_line_count = simplelist_line_count; + char* (*getname)(int item, void * data, char *buffer); + if (info->get_name) + getname = info->get_name; + else + getname = simplelist_static_getname; + gui_synclist_init(&lists, getname, info->callback_data, + info->scroll_all, info->selection_size); + if (info->title) + gui_synclist_set_title(&lists, info->title, NOICON); + if (info->get_icon) + gui_synclist_set_icon_callback(&lists, info->get_icon); + + gui_synclist_hide_selection_marker(&lists, info->hide_selection); + + if (info->action_callback) + info->action_callback(ACTION_REDRAW, &lists); + + if (info->get_name == NULL) + gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size); + else + gui_synclist_set_nb_items(&lists, info->count*info->selection_size); + + gui_synclist_draw(&lists); + + while(1) + { + gui_syncstatusbar_draw(&statusbars, true); + action = get_action(CONTEXT_STD, HZ/5); + if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD)) + continue; + if (info->action_callback) + { + action = info->action_callback(action, &lists); + if (info->get_name == NULL) + gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size); + } + if (action == ACTION_STD_CANCEL) + break; + else if ((action == ACTION_REDRAW) || (old_line_count == simplelist_line_count)) + { + if (info->get_name == NULL) + gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size); + gui_synclist_draw(&lists); + } + else if(default_event_handler(action) == SYS_USB_CONNECTED) + return true; + } + return false; +} + + + + + + + + + diff --git a/apps/gui/list.h b/apps/gui/list.h index 9aaa18ed08..2de67f5219 100644 --- a/apps/gui/list.h +++ b/apps/gui/list.h @@ -223,5 +223,47 @@ extern void gui_synclist_hide_selection_marker(struct gui_synclist *lists, extern bool gui_synclist_do_button(struct gui_synclist * lists, unsigned *action, enum list_wrap); + +/** Simplelist implementation. + USe this if you dont need to reimplement the list code, + and just need to show a list + **/ + +struct simplelist_info { + char *title; /* title to show on the list */ + int count; /* number of items in the list, each item is selection_size high */ + char selection_size; /* list selection size, usually 1 */ + bool hide_selection; + bool scroll_all; + int (*action_callback)(int action, struct gui_synclist *lists); /* can be NULL */ + /* action_callback notes: + action == the action pressed by the user + _after_ gui_synclist_do_button returns. + lists == the lists sturct so the callack can get selection and count etc. */ + list_get_icon *get_icon; /* can be NULL */ + list_get_name *get_name; /* NULL if you're using simplelist_addline() */ + void *callback_data; /* data for callbacks */ +}; + +#define SIMPLELIST_MAX_LINES 32 +#define SIMPLELIST_MAX_LINELENGTH 32 + +/** The next three functions are used if the text is mostly static. + These should be called in the action callback for the list. + **/ +/* set the amount of lines shown in the list + Only needed if simplelist_info.get_name == NULL */ +void simplelist_set_line_count(int lines); +/* get the current amount of lines shown */ +int simplelist_get_line_count(void); +/* add/edit a line in the list. + if line_number > number of lines shown it adds the line, else it edits the line */ +#define SIMPLELIST_ADD_LINE (SIMPLELIST_MAX_LINES+1) +void simplelist_addline(int line_number, const char *fmt, ...); + +/* show a list. + if list->action_callback != NULL it is called with the action ACTION_REDRAW + before the list is dislplayed for the first time */ +bool simplelist_show_list(struct simplelist_info *info); #endif /* _GUI_LIST_H_ */ -- cgit v1.2.3