Move some more globals to supertux/globals.hpp
[supertux.git] / src / supertux / menu / language_menu.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobas Glaesser <tobi.web@gmx.de>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "supertux/menu/language_menu.hpp"
19
20 extern "C" {
21 #include <findlocale.h>
22 }
23 #include "gui/menu_item.hpp"
24 #include "gui/menu_manager.hpp"
25 #include "supertux/gameconfig.hpp"
26 #include "supertux/globals.hpp"
27
28 enum {
29   MNID_LANGUAGE_AUTO_DETECT = 0,
30   MNID_LANGUAGE_ENGLISH     = 1,
31   MNID_LANGUAGE_NEXT        = 10
32 };
33
34 LanguageMenu::LanguageMenu() 
35 {
36   add_label(_("Language"));
37   add_hl();
38   add_entry(MNID_LANGUAGE_AUTO_DETECT, _("<auto-detect>"));
39   add_entry(MNID_LANGUAGE_ENGLISH, "English");
40
41   int mnid = MNID_LANGUAGE_NEXT;
42   std::set<tinygettext::Language> languages = dictionary_manager.get_languages();
43   for (std::set<tinygettext::Language>::iterator i = languages.begin(); i != languages.end(); i++) 
44   {
45     add_entry(mnid++, i->get_name());
46   }
47
48   add_hl();
49   add_back(_("Back"));
50 }
51
52 void
53 LanguageMenu::menu_action(MenuItem* item) 
54 {
55   if (item->id == MNID_LANGUAGE_AUTO_DETECT) // auto detect
56   {
57     FL_Locale *locale;
58     FL_FindLocale(&locale, FL_MESSAGES);
59     tinygettext::Language language = tinygettext::Language::from_spec(locale->lang, locale->country, locale->variant);
60     FL_FreeLocale(&locale);
61
62     dictionary_manager.set_language(language);
63     g_config->locale = language.str();
64     g_config->save();
65     MenuManager::pop_current();
66   }
67   else if (item->id == MNID_LANGUAGE_ENGLISH) // english
68   {
69     g_config->locale = "en";
70     dictionary_manager.set_language(tinygettext::Language::from_name(g_config->locale));
71     g_config->save();
72     MenuManager::pop_current();
73   }
74   else
75   {
76     int mnid = MNID_LANGUAGE_NEXT;
77     std::set<tinygettext::Language> languages = dictionary_manager.get_languages();
78
79     for (std::set<tinygettext::Language>::iterator i = languages.begin(); i != languages.end(); i++) 
80     {
81       if (item->id == mnid++) 
82       {
83         g_config->locale = i->str();
84         dictionary_manager.set_language(*i);
85         g_config->save();
86         MenuManager::pop_current();
87         break;
88       }
89     }
90   }
91 }
92
93 /* EOF */