#include /** * Parse a set of items composing the menu and * return a pointer to the equivalent MENU. * * \param menu a set of pair of strings ( name, descr ) * \param x left position * \param y top position * \param w window width (contents will be w-2) * \param h window height (contents will be h-2) * \param cp_fg color pair number to be used as selected item * \param cp_bg color pair number to be used as normal item * \param cp_di color pair number to be used as disabled item * * \return the resulting MENU or NULL on error. */ menu_t *create_menu( item_str_t *menu, int x, int y, int w, int h, int cp_fg, int cp_bg, int cp_di ) { ITEM **items; ITEM *item; menu_t *m; item_str_t *itr; int n=0; if ( ! menu ) return NULL; for ( itr = menu; itr->name != NULL; itr++ ) n++; n++; /* null pointer terminated */ items = (ITEM **) malloc( n * sizeof(ITEM *) ); items[ n - 1 ] = NULL; n = 0; itr = menu; for ( itr = menu, n = 0; itr->name != NULL; itr++, n++ ) { items[ n ] = new_item( itr->name, itr->description ); set_item_userptr( items[ n ], itr->func ); } m = (menu_t *) malloc( sizeof( menu_t ) ); m->menu = new_menu( items ); m->x = x; m->y = y; m->w = w; m->h = h; m->win = newwin( h, w, y, x ); keypad( m->win, 1 ); set_menu_win( m->menu, m->win ); set_menu_sub( m->menu, derwin( m->win, h-2, w-2, 1, 1 ) ); set_menu_fore( m->menu, COLOR_PAIR( cp_fg ) ); set_menu_back( m->menu, COLOR_PAIR( cp_bg ) ); set_menu_grey( m->menu, COLOR_PAIR( cp_di ) ); wcolor_set( m->win, COLOR_PAIR( cp_bg ), NULL ); wbkgdset( m->win, ' ' | COLOR_PAIR( cp_bg ) ); wclear( m->win ); box( m->win, 0, 0 ); set_menu_format( m->menu, h-2, 1 ); return m; } /** * Destroy menu and its items * * \param menu the menu to be destroyed */ void destroy_menu( menu_t *menu ) { ITEM **items; int n = 0; if ( ( ! menu ) || ( ! menu->menu ) ) return; if ( ! ( items = menu_items( menu->menu ) ) ) return; n = item_count( menu->menu ); if ( menu->win ) delwin( menu->win ); menu->win = NULL; free_menu( menu->menu ); for ( ; n > 0; n--, items++ ) { free_item( *items ); } free( menu ); } /* author: Gustavo Sverzut Barbieri (http://www.gustavobarbieri.com.br) */