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"
23 #include "gui/widgets/window.hpp"
24 #include "game_config.hpp"
25 #include "gettext.hpp"
26 #include "language.hpp"
27 #include "preferences/general.hpp"
28 
29 namespace gui2::dialogs
30 {
31 
32 namespace
33 {
34 
35 const std::string translations_wiki_url = "https://wiki.wesnoth.org/WesnothTranslations";
36 
37 }
38 
39 REGISTER_DIALOG(language_selection)
40 
42  : modal_dialog(window_id())
43  , langs_(get_languages(true))
44  , complete_langs_(langs_.size())
45 {
47 
48  const language_def& current_language = get_language();
49 
50  // Build language list and completion filter
51  for(std::size_t i = 0; i < langs_.size(); ++i) {
52  if(langs_[i] == current_language) {
53  // Always show the initial language regardless of completion. If it's under
54  // threshold then it probably means we should start by showing all languages
55  // too, regardless of what the initial threshold setting is.
56  complete_langs_[i] = true;
57  if(langs_[i].percent < get_min_translation_percent()) {
58  show_all = true;
59  }
60  } else {
61  complete_langs_[i] = langs_[i].percent >= get_min_translation_percent();
62  }
63  }
64 
65  register_bool("show_all", true, show_all);
66  // Markup needs to be enabled for the link to be highlighted
67  register_label("contrib_url", true, translations_wiki_url, true);
68 }
69 
71 {
72  window& window = *get_window();
73 
74  toggle_button& show_all_toggle = find_widget<toggle_button>(&window, "show_all", false);
75  listbox& list = find_widget<listbox>(&window, "language_list", false);
76 
77  if(show_all_toggle.get_value_bool()) {
78  list.set_row_shown(boost::dynamic_bitset<>{langs_.size(), ~0UL});
79  } else {
81  }
82 }
83 
85 {
86  listbox& list = find_widget<listbox>(&window, "language_list", false);
87  window.keyboard_capture(&list);
88 
89  toggle_button& show_all_toggle = find_widget<toggle_button>(&window, "show_all", false);
90  connect_signal_mouse_left_click(show_all_toggle, std::bind(
92 
93  const language_def& current_language = get_language();
94 
95  for(const auto& lang : langs_) {
97 
98  data["language"]["label"] = lang.language;
99  data["translated_total"]["label"] = "<span color='" + game_config::red_to_green(lang.percent).to_hex_string() + "'>" + std::to_string(lang.percent) + "%</span>";
100  data["translated_total"]["use_markup"] = "true";
101 
102  list.add_row(data);
103 
104  if(lang == current_language) {
105  list.select_last_row();
106  }
107  }
108 
109  // The view filter needs to be set after building the list to take the Show
110  // All toggle value + language completion stats into account as needed.
112 }
113 
115 {
116  if(get_retval() == retval::OK) {
117  const int res = find_widget<listbox>(&window, "language_list", false)
118  .get_selected_row();
119 
120  assert(res != -1);
121 
122  ::set_language(langs_[res]);
123  preferences::set_language(langs_[res].localename);
124  }
125 }
126 
127 } // 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.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
const std::vector< language_def > langs_
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
window * get_window()
Returns a pointer to the dialog's window.
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
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:136
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
Class for a toggle button.
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:1221
std::size_t i
Definition: function.cpp:968
This file contains the window object, this object is a top level container which has the event manage...
int get_min_translation_percent()
Definition: language.cpp:143
const language_def & get_language()
Definition: language.cpp:331
language_list get_languages(bool all)
Return a list of available translations.
Definition: language.cpp:126
color_t red_to_green(double val, bool for_text)
Return a color corresponding to the value val red for val=0.0 to green for val=100....
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
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::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
std::string_view data
Definition: picture.cpp:194
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.
Definition: color.cpp:78