The Battle for Wesnoth  1.15.1+dev
status_label_helper.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include "gettext.hpp"
21 
22 #include "utils/functional.hpp"
23 
24 namespace gui2
25 {
26 /**
27  * Default value getter for selectable widgets (like toggle buttons)
28  */
29 template<typename T>
30 static inline std::enable_if_t<std::is_base_of<selectable_item, T>::value, std::string>
32 {
33  return w.get_value_bool() ? _("yes") : _("no");
34 }
35 
36 /**
37  * Default value getter for integer-based widgets (like sliders)
38  */
39 template<typename T>
40 static inline std::enable_if_t<std::is_base_of<integer_selector, T>::value, std::string>
42 {
43  return w.get_value_label();
44 }
45 
46 /**
47  * Creates a bound status label that will reflect the label state of a widget. The initial label
48  * value is set here, and then again any time the widget is modified. A function is also returned
49  * that can be called to update the label manually.
50  *
51  * This relies on hooking into the NOTIFY_MODIFIED event, so can only be used with widgets that fire
52  * that event.
53  *
54  * @param find_in The containing widget (usually a window or grid) in which to find
55  * the source and status label widgets.
56  * @param source_id The ID of the source widget.
57  * @param value_getter Functor to process the value of the source widget.
58  * @param label_id The ID of the status label widget.
59  *
60  * @returns The callback function used to update the status label's value.
61  */
62 template<typename W>
63 std::function<void()> bind_status_label(
64  widget* find_in,
65  const std::string& source_id,
66  const std::function<std::string(W&)> value_getter = default_status_value_getter<W>,
67  const std::string& label_id = "")
68 {
69  // If no label ID is provided, use the format source ID + '_label'.
70  const std::string label_id_ = label_id.empty() ? source_id + "_label" : label_id;
71 
72  // Find the source value widget.
73  W& source = find_widget<W>(find_in, source_id, false);
74 
75  // Find the target status label.
76  styled_widget& label = find_widget<styled_widget>(find_in, label_id_, false);
77 
78  const auto update_label = [&, value_getter]() {
79  const std::string value = value_getter(source);
80 
81  label.set_label(value);
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
std::function< void()> bind_status_label(widget *find_in, const std::string &source_id, const std::function< std::string(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.
Base class for all widgets.
Definition: widget.hpp:47
Label showing a text.
Definition: label.hpp:32
Generic file dialog.
Definition: field-fwd.hpp:22
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:248
int w
Base class for all visible items.
static std::enable_if_t< std::is_base_of< selectable_item, T >::value, std::string > default_status_value_getter(T &w)
Default value getter for selectable widgets (like toggle buttons)