The Battle for Wesnoth  1.19.5+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 
20 #include "gui/widgets/listbox.hpp"
22 #include "gui/widgets/window.hpp"
23 #include "game_config.hpp"
24 #include "gettext.hpp"
25 #include "language.hpp"
27 #include "serialization/markup.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 const std::string translations_stats_url = "https://gettext.wesnoth.org/";
38 
39 }
40 
41 REGISTER_DIALOG(language_selection)
42 
44  : modal_dialog(window_id())
45  , langs_(get_languages(true))
46  , complete_langs_(langs_.size())
47 {
48  bool show_all = get_min_translation_percent() == 0;
49 
50  const language_def& current_language = get_language();
51 
52  // Build language list and completion filter
53  for(std::size_t i = 0; i < langs_.size(); ++i) {
54  if(langs_[i] == current_language) {
55  // Always show the initial language regardless of completion. If it's under
56  // threshold then it probably means we should start by showing all languages
57  // too, regardless of what the initial threshold setting is.
58  complete_langs_[i] = true;
59  if(langs_[i].percent < get_min_translation_percent()) {
60  show_all = true;
61  }
62  } else {
63  complete_langs_[i] = langs_[i].percent >= get_min_translation_percent();
64  }
65  }
66 
67  register_bool("show_all", true, show_all);
68  // Markup needs to be enabled for the link to be highlighted
69  register_label("contrib_url", true, translations_wiki_url, true);
70  register_label("stats_url", true, translations_stats_url, true);
71 }
72 
74 {
75  toggle_button& show_all_toggle = find_widget<toggle_button>("show_all");
76  listbox& list = find_widget<listbox>("language_list");
77 
78  if(show_all_toggle.get_value_bool()) {
79  list.set_row_shown(boost::dynamic_bitset<>{langs_.size(), ~0UL});
80  } else {
82  }
83 }
84 
86 {
87  listbox& list = find_widget<listbox>("language_list");
88  keyboard_capture(&list);
89 
90  toggle_button& show_all_toggle = find_widget<toggle_button>("show_all");
91  connect_signal_mouse_left_click(show_all_toggle, std::bind(
93 
94  const language_def& current_language = get_language();
95 
96  for(const auto& lang : langs_) {
98 
99  data["language"]["label"] = lang.language;
100  data["language"]["use_markup"] = "true";
101  data["translated_total"]["label"] = markup::span_color(game_config::red_to_green(lang.percent), lang.percent, "%");
102  data["translated_total"]["use_markup"] = "true";
103 
104  if(game_config::debug && !lang.localename.empty()) {
105  data["language"]["label"] += "\n" + markup::tag("small", markup::tag("tt", lang.localename));
106  }
107 
108  list.add_row(data);
109 
110  if(lang == current_language) {
111  list.select_last_row();
112  }
113  }
114 
115  // The view filter needs to be set after building the list to take the Show
116  // All toggle value + language completion stats into account as needed.
118 }
119 
121 {
122  if(get_retval() == retval::OK) {
123  const int res = find_widget<listbox>("language_list")
124  .get_selected_row();
125 
126  assert(res != -1);
127 
128  ::set_language(langs_[res]);
129  prefs::get().set_locale(langs_[res].localename);
130  }
131 }
132 
133 } // namespace dialogs
virtual void post_show() override
Actions to be taken after the window has been shown.
virtual void pre_show() 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.
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:135
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:58
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
static prefs & get()
std::size_t i
Definition: function.cpp:1028
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:325
language_list get_languages(bool all)
Return a list of available translations.
Definition: language.cpp:126
const bool & debug
Definition: game_config.cpp:94
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:36
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::string tag(const std::string &tag_name, Args &&... contents)
Definition: markup.hpp:45
std::string span_color(const color_t &color, Args &&... data)
Definition: markup.hpp:68
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:178