restore trunk
[supertux.git] / src / options_menu.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobas Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #include <config.h>
21
22 #include "options_menu.hpp"
23 #include "gui/menu.hpp"
24 #include "audio/sound_manager.hpp"
25 #include "control/joystickkeyboardcontroller.hpp"
26 #include "main.hpp"
27 #include "gettext.hpp"
28 #include "gameconfig.hpp"
29
30 Menu* options_menu   = 0;
31
32 enum OptionsMenuIDs {
33   MNID_FULLSCREEN,
34   MNID_SOUND,
35   MNID_MUSIC
36 };
37
38 class LanguageMenu : public Menu
39 {
40 public:
41   LanguageMenu() {
42     add_label(_("Language"));
43     add_hl();
44     add_entry(0, std::string("(")+_("auto-detect language")+")");
45     add_entry(1, "English");
46
47     int mnid = 10;    
48     std::set<std::string> languages = dictionary_manager.get_languages();
49     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
50       std::string locale_name = *i;
51       TinyGetText::LanguageDef ldef = TinyGetText::get_language_def(locale_name);
52       std::string locale_fullname = locale_name;
53       if (std::string(ldef.code) == locale_name) {
54         locale_fullname = ldef.name;
55       }
56       add_entry(mnid++, locale_fullname);
57     } 
58
59     add_hl();
60     add_back(_("Back"));
61   }
62
63   virtual void menu_action(MenuItem* item) {
64     if (item->id == 0) {
65       config->locale = "";
66       dictionary_manager.set_language(config->locale);
67       config->save();
68       Menu::set_current(0);
69     }
70     else if (item->id == 1) {
71       config->locale = "en";
72       dictionary_manager.set_language(config->locale);
73       config->save();
74       Menu::set_current(0);
75     }
76     int mnid = 10;    
77     std::set<std::string> languages = dictionary_manager.get_languages();
78     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
79       std::string locale_name = *i;
80       if (item->id == mnid++) {
81         config->locale = locale_name;
82         dictionary_manager.set_language(config->locale);
83         config->save();
84         Menu::set_current(0);
85       }
86     }
87   }
88 };
89
90
91 class OptionsMenu : public Menu
92 {
93 public:
94   OptionsMenu();
95   virtual ~OptionsMenu();
96
97   virtual void menu_action(MenuItem* item);
98
99 protected:
100   std::auto_ptr<LanguageMenu> language_menu;
101   
102 };
103
104 OptionsMenu::OptionsMenu()
105 {
106   language_menu.reset(new LanguageMenu());
107
108   add_label(_("Options"));
109   add_hl();
110   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), config->use_fullscreen);
111   add_submenu(_("Language"), language_menu.get());
112   if (sound_manager->is_audio_enabled()) {
113     add_toggle(MNID_SOUND, _("Sound"), config->sound_enabled);
114     add_toggle(MNID_MUSIC, _("Music"), config->music_enabled);
115   } else {
116     add_deactive(MNID_SOUND, _("Sound (disabled)"));
117     add_deactive(MNID_SOUND, _("Music (disabled)"));
118   }
119   add_submenu(_("Setup Keyboard"), main_controller->get_key_options_menu());
120   add_submenu(_("Setup Joystick"),main_controller->get_joystick_options_menu());
121   add_hl();
122   add_back(_("Back"));
123 }
124
125 OptionsMenu::~OptionsMenu()
126 {
127 }
128
129 void
130 OptionsMenu::menu_action(MenuItem* item)
131 {
132   switch (item->id) {
133     case MNID_FULLSCREEN:
134       if(config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) {
135         config->use_fullscreen = !config->use_fullscreen;
136         init_video();
137         config->save();
138       }
139       break;
140     case MNID_SOUND:
141       if(config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) {
142         config->sound_enabled = !config->sound_enabled;
143         sound_manager->enable_sound(config->sound_enabled);
144         config->save();
145       }
146       break;
147     case MNID_MUSIC:
148       if(config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) {
149         config->music_enabled = !config->music_enabled;
150         sound_manager->enable_music(config->music_enabled);
151         config->save();
152       }
153       break;
154     default:
155       break;
156   }
157 }
158
159 Menu* get_options_menu()
160 {
161   //static OptionsMenu menu;
162   options_menu = new OptionsMenu();
163   return options_menu;
164 }
165
166 void free_options_menu()
167 {
168   delete options_menu;
169   options_menu = 0;
170 }