Empty menus shouldn't lead to crashs anymore.
[supertux.git] / lib / gui / menu.cpp
index e060e39..d35f6d9 100644 (file)
@@ -457,17 +457,23 @@ Menu::action()
         }
     }
 
-  MenuItem& new_item = item[active_item];
-  if(new_item.kind == MN_DEACTIVE
-      || new_item.kind == MN_LABEL
-      || new_item.kind == MN_HL)
+  if(active_item > 0 && active_item < (int)item.size())
     {
+    // FIXME: wtf?! having a hack to avoid horizontal lines...
+    // Elegant solution would be to check for horizontal lines, right
+    // when it was asked to move menu up and down
+    MenuItem& new_item = item[active_item];
+    if(new_item.kind == MN_DEACTIVE ||
+       new_item.kind == MN_LABEL ||
+       new_item.kind == MN_HL)
+      {
       // Skip the horzontal line item
       if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
         menuaction = MENU_ACTION_DOWN;
 
       if (item.size() > 1)
         action();
+      }
     }
 
   menuaction = MENU_ACTION_NONE;
@@ -509,7 +515,8 @@ Menu::draw_item(DrawingContext& context,
   int text_width  = int(text_font->get_text_width(pitem.text));
   int input_width = int(text_font->get_text_width(pitem.input) + 10);
   int list_width = 0;
-  if(pitem.list.second != 0)
+  std::set<std::string>::iterator tmp = 0;
+  if(pitem.list.second != tmp)
   list_width = int(text_font->get_text_width((*pitem.list.second)));
   
   if (arrange_left)
@@ -763,7 +770,7 @@ Menu::event(SDL_Event& event)
             /* An International Character. */
           }
 
-        if(item[active_item].kind == MN_CONTROLFIELD_KB)
+        if(item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_KB)
           {
             if(key == SDLK_ESCAPE)
               {
@@ -791,7 +798,7 @@ Menu::event(SDL_Event& event)
             menuaction = MENU_ACTION_RIGHT;
             break;
           case SDLK_SPACE:
-            if(item[active_item].kind == MN_TEXTFIELD)
+            if(item.size() > 0 && item[active_item].kind == MN_TEXTFIELD)
               {
                 menuaction = MENU_ACTION_INPUT;
                 mn_input_char = ' ';
@@ -841,7 +848,7 @@ Menu::event(SDL_Event& event)
         }
       break;
     case  SDL_JOYBUTTONDOWN:
-      if (item[active_item].kind == MN_CONTROLFIELD_JS)
+      if (item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_JS)
         {
           // FIXME: This next line does nothing useable, right?
           // *item[active_item].int_p = key;