Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[supertux.git] / src / gui / menu.cpp
index 052a54d..0045fbc 100644 (file)
@@ -1,7 +1,7 @@
 //  $Id$
 //
 //  SuperTux
-//  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
 //  This program is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU General Public License
@@ -16,6 +16,7 @@
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
 #include <config.h>
 
 #include <sys/types.h>
@@ -30,6 +31,7 @@
 #include <stdexcept>
 
 #include "menu.hpp"
+#include "mainloop.hpp"
 #include "video/screen.hpp"
 #include "video/drawing_context.hpp"
 #include "gettext.hpp"
@@ -37,7 +39,6 @@
 #include "main.hpp"
 #include "resources.hpp"
 #include "control/joystickkeyboardcontroller.hpp"
-#include "exceptions.hpp"
 
 static const int MENU_REPEAT_INITIAL = 400;
 static const int MENU_REPEAT_RATE = 200;
@@ -68,12 +69,13 @@ bool confirm_dialog(Surface *background, std::string text)
 
   DrawingContext context;
 
+  // TODO make this a screen and not another mainloop...
   while(true)
     {
       SDL_Event event;
       while (SDL_PollEvent(&event)) {
         if(event.type == SDL_QUIT)
-          throw graceful_shutdown();
+          main_loop->quit();
         main_controller->process_event(event);
         dialog->event(event);
       }
@@ -487,8 +489,8 @@ Menu::menu_action(MenuItem* )
 void
 Menu::draw_item(DrawingContext& context, int index)
 {
-  int menu_height = get_height();
-  int menu_width = get_width();  
+  float menu_height = get_height();
+  float menu_width = get_width();  
 
   MenuItem& pitem = *(items[index]);
 
@@ -666,29 +668,33 @@ Menu::draw_item(DrawingContext& context, int index)
     }
 }
 
-int Menu::get_width() const
+float Menu::get_width() const
+{
+  /* The width of the menu has to be more than the width of the text
+     with the most characters */
+  float menu_width = 0;
+  for(unsigned int i = 0; i < items.size(); ++i)
   {
-    /* The width of the menu has to be more than the width of the text
-       with the most characters */
-    int menu_width = 0;
-    for(unsigned int i = 0; i < items.size(); ++i)
-      {
-        int w = items[i]->text.size() + items[i]->input.size() + 1;
-        if(w > menu_width)
-          {
-            menu_width = w;
-            if( items[i]->kind == MN_TOGGLE)
-              menu_width += 2;
-          }
-      }
-
-    return (menu_width * 16 + 24);
+    Font* font = default_font;
+    if(items[i]->kind == MN_LABEL)
+      font = label_font;
+
+    float w = font->get_text_width(items[i]->text) +
+        label_font->get_text_width(items[i]->input) + 16;
+    if(items[i]->kind == MN_TOGGLE)
+      w += 32;
+    
+    if(w > menu_width)
+      menu_width = w;
   }
+  
+  return menu_width + 24;
+}
 
-int Menu::get_height() const
-  {
-    return items.size() * 24;
-  }
+float Menu::get_height() const
+{
+  return items.size() * 24;
+}
 
 /* Draw the current menu. */
 void
@@ -698,8 +704,8 @@ Menu::draw(DrawingContext& context)
     MouseCursor::current()->draw(context);
   }
   
-  int menu_height = get_height();
-  int menu_width = get_width();  
+  float menu_height = get_height();
+  float menu_width = get_width();  
 
   /* Draw a transparent background */
   context.draw_filled_rect(