Code:
typedef struct list_info_s list_info_t;
struct list_info_s
{
int position;
char name[ 50 ];
list_info_t *next;
list_info_t *prev;
};
struct list_mgr_s
{
int num_lists;
};
int list_get_num_lists( list_mgr_t *mgr )
{
return mgr->num_lists;
}
const char *list_get_name( list_mgr_t *mgr, int position )
{
const list_info_t *list = list_get_list(mgr, position);
if( (list) && (*list->name) ) {
return list->name;
} else {
return 0;
}
}
#define MENU_LIST_MAX_LINES 10
menu_t *menu_new( const char *name )
{
menu_t *menu = malloc( sizeof( menu_t ) );
if( !menu ) {
return 0;
}
menu->numlines = 0;
menu->name = strdup( name );
if( !menu->name ) {
free( menu );
return 0;
}
return menu;
}
struct menu_list_s
{
int numlines;
menu_string_t *lines[ MENU_LIST_MAX_LINES ];
};
void menu_list_set_text( menu_list_t *menul, int line, const char *text )
{
if( line < MENU_LIST_MAX_LINES ) {
menu_string_show_text( menul->lines[ line ], text, 100 );
}
}
void menu_list_set_lines( menu_list_t *menul, int numlines )
{
if( numlines > MENU_LIST_MAX_LINES ) numlines = MENU_LIST_MAX_LINES;
menu->numlines = numlines;
}
void program_menu_list_set_lines( program_menu_t *menu, int numlines )
{
menu_list_set_lines( menu->list, numlines );
}
void program_menu_list_set_text( program_menu_t *menu, int line, const char *text )
{
menu_list_set_text( menu->list, line, text );
}
static void view_menu( commands_t *cmd )
{
int i;
const int max_num_lines = 10;
const int num_lists = list_get_num_lists( cmd->listmgr );
char text[ 50 ];
program_menu_list_set_lines( cmd->menu, num_lists + 1 );
for( i = 0; i < num_lists; i++ ) {
snprintf( text, sizeof (text), "%s", list_get_name( cmd->listmgr, i ) );
program_menu_list_set_text( cmd->menu, i, text );
}
cmd->menusize = num_lists;
}
struct commands_s {
program_menu_t *menu;
list_mgr_t *listmgr;
int menuactive;
int menuposition;
int menusize;
};
void commands_handle( commands_t *cmd, int program_cmd )
{
if( cmd->menuactive && program_is_menu_command( program_cmd ) ) {
switch( program_cmd ) {
case MENU_EXIT:
menu_off( cmd );
break;
case MENU_UP:
cmd->menuposition = (cmd->menuposition + cmd->menusize - 1) % (cmd->menusize);
view_menu( cmd );
break;
case MENU_DOWN:
cmd->menuposition = (cmd->menuposition + 1) % (cmd->menusize);
view_menu( cmd );
break;
case SHOW_MENU:
if( cmd->menu ) {
commands_clear_menu_positions( cmd );
view_menu( cmd );
}
break;
}
return;
}
}
This create entry in list (for example in xml file:
Code:
<name="GREEN" />
<name="RED" />
<name="BLUE" />
<name="BLACK" />
<name="BROWN" /
...............................
I want to create an menu ( view_menu( commands_t *cmd ) ) with maxim 10 line and display all items from xml file by scrolling up/down line by line to view all content of file.
In my API menu is capable to set: number of lines in menu (maxim 10), display text in line, move up/down cursor in menu,
Menu is set to 10 lines max because xml may contain a large number of items.
I my actual code, menu is able to display only first 10 items of xml.