The Battle for Wesnoth  1.15.12+dev
language_selection.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
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 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
20 #include "gui/widgets/listbox.hpp"
21 #include "gui/widgets/settings.hpp"
22 #include "gui/widgets/window.hpp"
23 #include "language.hpp"
24 #include "preferences/general.hpp"
25 
26 namespace gui2::dialogs
27 {
28 
29 /**
30  * @todo show we also reset the translations and is the tips of day call
31  * really needed?
32  */
33 
34 REGISTER_DIALOG(language_selection)
35 
37 {
38  listbox& list = find_widget<listbox>(&window, "language_list", false);
39  window.keyboard_capture(&list);
40 
41  const std::vector<language_def>& languages = get_languages();
42  const language_def& current_language = get_language();
43  for(const auto & lang : languages)
44  {
45  std::map<std::string, string_map> data;
46 
47  data["language"]["label"] = lang.language;
48 
49  list.add_row(data);
50  if(lang == current_language) {
51  list.select_last_row();
52  }
53  }
54 }
55 
57 {
58  if(get_retval() == retval::OK) {
59  const int res = find_widget<listbox>(&window, "language_list", false)
60  .get_selected_row();
61 
62  assert(res != -1);
63 
64  const std::vector<language_def>& languages = get_languages();
65  ::set_language(languages[res]);
66  preferences::set_language(languages[res].localename);
67  }
68 }
69 
70 } // namespace dialogs
This file contains the window object, this object is a top level container which has the event manage...
void set_language(const std::string &language, const std::vector< std::string > *)
Definition: gettext.cpp:447
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list&#39;s last row.
Definition: listbox.hpp:194
The listbox class.
Definition: listbox.hpp:42
This file contains the settings handling of the widget library.
language_list get_languages(bool all)
Return a list of available translations.
Definition: language.cpp:116
This shows the dialog to select the language to use.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
const language_def & get_language()
Definition: language.cpp:316
grid & add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:67
void set_language(const std::string &s)
Definition: general.cpp:494
int get_retval() const
Returns the cached window exit code.
Dialog was closed with the OK button.
Definition: retval.hpp:34
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64