a87676e011687e75a08b86f8f62ba5e406d4f5b7
[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
46     int mnid = 10;    
47     std::set<std::string> languages = dictionary_manager.get_languages();
48     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
49       std::string locale_name = *i;
50       TinyGetText::LanguageDef ldef = TinyGetText::get_language_def(locale_name);
51       std::string locale_fullname = locale_name;
52       if (std::string(ldef.code) == locale_name) {
53         locale_fullname = ldef.name;
54       }
55       add_entry(mnid++, locale_fullname);
56     } 
57
58     add_hl();
59     add_back(_("Back"));
60   }
61
62   virtual void menu_action(MenuItem* item) {
63     if (item->id == 0) {
64       config->locale = "";
65       dictionary_manager.set_language(config->locale);
66       config->save();
67       Menu::set_current(0);
68     }
69     int mnid = 10;    
70     std::set<std::string> languages = dictionary_manager.get_languages();
71     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
72       std::string locale_name = *i;
73       if (item->id == mnid++) {
74         config->locale = locale_name;
75         dictionary_manager.set_language(config->locale);
76         config->save();
77         Menu::set_current(0);
78       }
79     }
80   }
81 };
82
83
84 class OptionsMenu : public Menu
85 {
86 public:
87   OptionsMenu();
88   virtual ~OptionsMenu();
89
90   virtual void menu_action(MenuItem* item);
91
92 protected:
93   std::auto_ptr<LanguageMenu> language_menu;
94   
95 };
96
97 OptionsMenu::OptionsMenu()
98 {
99   language_menu.reset(new LanguageMenu());
100
101   add_label(_("Options"));
102   add_hl();
103   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), config->use_fullscreen);
104   add_submenu(_("Language"), language_menu.get());
105   if (sound_manager->is_audio_enabled()) {
106     add_toggle(MNID_SOUND, _("Sound"), config->sound_enabled);
107     add_toggle(MNID_MUSIC, _("Music"), config->music_enabled);
108   } else {
109     add_deactive(MNID_SOUND, _("Sound (disabled)"));
110     add_deactive(MNID_SOUND, _("Music (disabled)"));
111   }
112   add_submenu(_("Setup Keyboard"), main_controller->get_key_options_menu());
113   add_submenu(_("Setup Joystick"),main_controller->get_joystick_options_menu());
114   add_hl();
115   add_back(_("Back"));
116 }
117
118 OptionsMenu::~OptionsMenu()
119 {
120 }
121
122 void
123 OptionsMenu::menu_action(MenuItem* item)
124 {
125   switch (item->id) {
126     case MNID_FULLSCREEN:
127       if(config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) {
128         config->use_fullscreen = !config->use_fullscreen;
129         init_video();
130         config->save();
131       }
132       break;
133     case MNID_SOUND:
134       if(config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) {
135         config->sound_enabled = !config->sound_enabled;
136         sound_manager->enable_sound(config->sound_enabled);
137         config->save();
138       }
139       break;
140     case MNID_MUSIC:
141       if(config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) {
142         config->music_enabled = !config->music_enabled;
143         sound_manager->enable_music(config->music_enabled);
144         config->save();
145       }
146       break;
147     default:
148       break;
149   }
150 }
151
152 Menu* get_options_menu()
153 {
154   //if(options_menu == NULL)
155   options_menu = new OptionsMenu();
156
157   return options_menu;
158 }
159
160 void free_options_menu()
161 {
162   delete options_menu;
163   options_menu = NULL;
164 }