The Battle for Wesnoth  1.15.1+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
27 {
28 namespace dialogs
29 {
30 
31 /*WIKI
32  * @page = GUIWindowDefinitionWML
33  * @order = 2_language_selection
34  *
35  * == Language selection ==
36  *
37  * This shows the dialog to select the language to use. When the dialog is
38  * closed with the OK button it also updates the selected language in the
39  * preferences.
40  *
41  * @begin{table}{dialog_widgets}
42  *
43  * language_list & & listbox & m &
44  * This listbox contains the list with available languages. $
45  *
46  * - & & styled_widget & o &
47  * Show the name of the language in the current row. $
48  *
49  * @end{table}
50  */
51 
52 /**
53  * @todo show we also reset the translations and is the tips of day call
54  * really needed?
55  */
56 
57 REGISTER_DIALOG(language_selection)
58 
60 {
61  listbox& list = find_widget<listbox>(&window, "language_list", false);
62  window.keyboard_capture(&list);
63 
64  const std::vector<language_def>& languages = get_languages();
65  const language_def& current_language = get_language();
66  for(const auto & lang : languages)
67  {
68  std::map<std::string, string_map> data;
69 
70  data["language"]["label"] = lang.language;
71 
72  list.add_row(data);
73  if(lang == current_language) {
74  list.select_last_row();
75  }
76  }
77 }
78 
80 {
81  if(get_retval() == retval::OK) {
82  const int res = find_widget<listbox>(&window, "language_list", false)
83  .get_selected_row();
84 
85  assert(res != -1);
86 
87  const std::vector<language_def>& languages = get_languages();
88  ::set_language(languages[res]);
89  preferences::set_language(languages[res].localename);
90  }
91 }
92 
93 } // namespace dialogs
94 } // namespace gui2
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:449
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list&#39;s last row.
Definition: listbox.hpp:192
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
This file contains the settings handling of the widget library.
virtual void post_show(window &window) override
Inherited from modal_dialog.
Various uncategorised dialogs.
const language_def & get_language()
Definition: language.cpp:261
language_list get_languages()
Definition: language.cpp:115
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:66
void set_language(const std::string &s)
Definition: general.cpp:480
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:63