The Battle for Wesnoth  1.19.0-dev
language_selection.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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/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  {
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 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.
int get_retval() const
Returns the cached window exit code.
The listbox class.
Definition: listbox.hpp:43
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list's last row.
Definition: listbox.hpp:189
grid & add_row(const widget_item &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:59
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void keyboard_capture(widget *widget)
Definition: window.cpp:1215
This file contains the window object, this object is a top level container which has the event manage...
const language_def & get_language()
Definition: language.cpp:326
language_list get_languages(bool all)
Return a list of available translations.
Definition: language.cpp:126
REGISTER_DIALOG(tod_new_schedule)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
void set_language(const std::string &s)
Definition: general.cpp:540
void set_language(const std::string &language, const std::vector< std::string > *)
Definition: gettext.cpp:494
std::string_view data
Definition: picture.cpp:194