-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
status_label_helper.hpp
94 lines (78 loc) · 3.01 KB
/
status_label_helper.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Copyright (C) 2010 - 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "gettext.hpp"
#include "gui/core/event/dispatcher.hpp"
#include "gui/widgets/integer_selector.hpp"
#include "gui/widgets/selectable_item.hpp"
#include "gui/widgets/styled_widget.hpp"
#include "utils/functional.hpp"
#include "utils/type_trait_aliases.hpp"
namespace gui2
{
/**
* Default value getter for selectable widgets (like toggle buttons)
*/
template<typename T>
static inline utils::enable_if_t<std::is_base_of<selectable_item, T>::value, std::string>
default_status_value_getter(T& w)
{
return w.get_value_bool() ? _("yes") : _("no");
}
/**
* Default value getter for integer-based widgets (like sliders)
*/
template<typename T>
static inline utils::enable_if_t<std::is_base_of<integer_selector, T>::value, std::string>
default_status_value_getter(T& w)
{
return w.get_value_label();
}
/**
* Creates a bound status label that will reflect the label state of a widget. The initial label
* value is set here, and then again any time the widget is modified. A function is also returned
* that can be called to update the label manually.
*
* This relies on hooking into the NOTIFY_MODIFIED event, so can only be used with widgets that fire
* that event.
*
* @param find_in The containing widget (usually a window or grid) in which to find
* the source and status label widgets.
* @param source_id The ID of the source widget.
* @param value_getter Functor to process the value of the source widget.
* @param label_id The ID of the status label widget.
*
* @returns The callback function used to update the status label's value.
*/
template<typename W>
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 = "")
{
// If no label ID is provided, use the format source ID + '_label'.
const std::string label_id_ = label_id.empty() ? source_id + "_label" : label_id;
// Find the source value widget.
W& source = find_widget<W>(find_in, source_id, false);
// Find the target status label.
styled_widget& label = find_widget<styled_widget>(find_in, label_id_, false);
const auto update_label = [&, value_getter]() {
const std::string value = value_getter(source);
label.set_label(value);
};
// Bind the callback.
connect_signal_notify_modified(source, std::bind(update_label));
// Set initial value.
update_label();
return update_label;
}
} // namespace gui2