/* Free a menu and all its items */
Menu::~Menu()
{
- if(num_items != 0 && item != NULL)
+ if(item.size() != 0)
{
- for(int i = 0; i < num_items; ++i)
+ for(unsigned int i = 0; i < item.size(); ++i)
{
free(item[i].text);
free(item[i].input);
string_list_free(item[i].list);
}
- free(item);
}
}
pos_y = screen->h/2;
last_menu = 0;
arrange_left = 0;
- num_items = 0;
active_item = 0;
last_menu = 0;
- item = NULL;
effect.init(false);
}
void
Menu::additem(MenuItem* pmenu_item)
{
- ++num_items;
- item = (MenuItem*)realloc(item, sizeof(MenuItem) * num_items);
- memcpy(&item[num_items-1], pmenu_item, sizeof(MenuItem));
- free(pmenu_item);
+ item.push_back(*pmenu_item);
+ delete pmenu_item;
}
/* Process actions done on the menu */
void
Menu::action()
{
- if(num_items != 0 && item != NULL)
+ if(item.size() != 0)
{
switch(menuaction)
{
if (active_item > 0)
--active_item;
else
- active_item = num_items-1;
+ active_item = int(item.size())-1;
break;
case MENU_ACTION_DOWN:
- if(active_item < num_items-1)
+ if(active_item < int(item.size())-1)
++active_item;
else
active_item = 0;
if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
menuaction = MENU_ACTION_DOWN;
- if(num_items > 1)
+ if(item.size() > 1)
action();
}
}
int
Menu::check()
{
- if(num_items != 0 && item != NULL)
+ if (item.size() != 0)
{
if((item[active_item].kind == MN_ACTION
|| item[active_item].kind == MN_TEXTFIELD
/* The width of the menu has to be more than the width of the text
with the most characters */
int menu_width = 0;
- for(int i = 0; i < num_items; ++i)
+ for(unsigned int i = 0; i < item.size(); ++i)
{
int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
if( w > menu_width )
int Menu::height()
{
- return ((num_items) * 24);
+ return item.size() * 24;
}
/* Draw the current menu. */
/* Draw a transparent background */
fillrect(pos_x - menu_width/2,
- pos_y - 24*num_items/2 - 10,
+ pos_y - 24*item.size()/2 - 10,
menu_width,menu_height + 20,
150,180,200,125);
- for(int i = 0; i < num_items; ++i)
+ for(unsigned int i = 0; i < item.size(); ++i)
{
draw_item(i, menu_width, menu_height);
}