The Battle for Wesnoth  1.17.0-dev
language_selection.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
21 #include "gui/widgets/listbox.hpp"
22 #include "gui/widgets/settings.hpp"
23 #include "gui/widgets/window.hpp"
24 #include "language.hpp"
25 #include "preferences/general.hpp"
26 
27 namespace gui2::dialogs
28 {
29 
30 /**
31  * @todo show we also reset the translations and is the tips of day call
32  * really needed?
33  */
34 
35 REGISTER_DIALOG(language_selection)
36 
38 {
39  listbox& list = find_widget<listbox>(&window, "language_list", false);
40  window.keyboard_capture(&list);
41 
42  const std::vector<language_def>& languages = get_languages();
43  const language_def& current_language = get_language();
44  for(const auto & lang : languages)
45  {
46  std::map<std::string, string_map> data;
47 
48  data["language"]["label"] = lang.language;
49 
50  list.add_row(data);
51  if(lang == current_language) {
52  list.select_last_row();
53  }
54  }
55 }
56 
58 {
59  if(get_retval() == retval::OK) {
60  const int res = find_widget<listbox>(&window, "language_list", false)
61  .get_selected_row();
62 
63  assert(res != -1);
64 
65  const std::vector<language_def>& languages = get_languages();
66  ::set_language(languages[res]);
67  preferences::set_language(languages[res].localename);
68  }
69 }
70 
71 } // 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:489
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list&#39;s last row.
Definition: listbox.hpp:195
The listbox class.
Definition: listbox.hpp:43
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:127
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:327
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:68
void set_language(const std::string &s)
Definition: general.cpp:510
int get_retval() const
Returns the cached window exit code.
Dialog was closed with the OK button.
Definition: retval.hpp:35
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65