The Battle for Wesnoth  1.19.5+dev
status_label_helper.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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 #pragma once
16 
17 #include "gettext.hpp"
22 
23 #include <functional>
24 
25 namespace gui2
26 {
27 template<typename T>
29 {
30  // Menu Buttons
31  if constexpr(std::is_same_v<menu_button, T>) {
32  return w.get_value_string();
33  }
34 
35  // Selectable widgets (like toggle buttons)
36  if constexpr(std::is_base_of_v<selectable_item, T>) {
37  return w.get_value_bool() ? _("yes") : _("no");
38  }
39 
40  // Sliders
41  if constexpr(std::is_same_v<slider, T>) {
42  return w.get_value_label();
43  }
44 
45  return w.get_label();
46 }
47 
48 /**
49  * Creates a bound status label that will reflect the label state of a widget. The initial label
50  * value is set here, and then again any time the widget is modified. A function is also returned
51  * that can be called to update the label manually.
52  *
53  * This relies on hooking into the NOTIFY_MODIFIED event, so can only be used with widgets that fire
54  * that event.
55  *
56  * @param find_in The containing widget (usually a window or grid) in which to find
57  * the source and status label widgets.
58  * @param source_id The ID of the source widget.
59  * @param value_getter Functor to process the value of the source widget.
60  * @param label_id The ID of the status label widget.
61  *
62  * @returns The callback function used to update the status label's value.
63  */
64 template<typename W>
65 std::function<void()> bind_status_label(
66  widget* find_in,
67  const std::string& source_id,
68  const std::function<t_string(const W&)> value_getter = default_status_value_getter<W>,
69  const std::string& label_id = "")
70 {
71  // If no label ID is provided, use the format source ID + '_label'.
72  const std::string label_id_ = label_id.empty() ? source_id + "_label" : label_id;
73 
74  // Find the source value widget.
75  W& source = find_in->find_widget<W>(source_id);
76 
77  // Find the target status label.
78  styled_widget& label = find_in->find_widget<styled_widget>(label_id_);
79 
80  const auto update_label = [&, value_getter]() {
81  label.set_label(value_getter(source));
82  };
83 
84  // Bind the callback.
85  connect_signal_notify_modified(source, std::bind(update_label));
86 
87  // Set initial value.
88  update_label();
89 
90  return update_label;
91 }
92 
93 } // namespace gui2
virtual void set_label(const t_string &text)
Base class for all widgets.
Definition: widget.hpp:55
NOT_DANGLING T * find_widget(const std::string &id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition: widget.hpp:742
int w
static std::string _(const char *str)
Definition: gettext.hpp:93
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
Generic file dialog.
t_string default_status_value_getter(const T &w)
std::function< void()> bind_status_label(widget *find_in, const std::string &source_id, const std::function< t_string(const W &)> value_getter=default_status_value_getter< W >, const std::string &label_id="")
Creates a bound status label that will reflect the label state of a widget.