/
label.cpp
213 lines (162 loc) · 4.56 KB
/
label.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Copyright (C) 2008 - 2015 by Mark de Wever <koraq@xs4all.nl>
Part of 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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/widgets/label.hpp"
#include "gui/auxiliary/log.hpp"
#include "gui/auxiliary/widget_definition/label.hpp"
#include "gui/auxiliary/window_builder/label.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/widgets/detail/register.tpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "desktop/clipboard.hpp"
#include "desktop/open.hpp"
#include "gettext.hpp"
#include <boost/bind.hpp>
#include <string>
#include <sstream>
namespace gui2
{
REGISTER_WIDGET(label)
tlabel::tlabel()
: tcontrol(COUNT)
, state_(ENABLED)
, can_wrap_(false)
, characters_per_line_(0)
, link_aware_(false)
, link_color_("#ffff00")
{
connect_signal<event::LEFT_BUTTON_CLICK>(boost::bind(&tlabel::signal_handler_left_button_click, this, _2, _3));
connect_signal<event::RIGHT_BUTTON_CLICK>(boost::bind(&tlabel::signal_handler_right_button_click, this, _2, _3));
}
bool tlabel::can_wrap() const
{
return can_wrap_ || characters_per_line_ != 0;
}
unsigned tlabel::get_characters_per_line() const
{
return characters_per_line_;
}
bool tlabel::get_link_aware() const
{
return link_aware_;
}
std::string tlabel::get_link_color() const
{
return link_color_;
}
void tlabel::set_active(const bool active)
{
if(get_active() != active) {
set_state(active ? ENABLED : DISABLED);
}
}
bool tlabel::get_active() const
{
return state_ != DISABLED;
}
unsigned tlabel::get_state() const
{
return state_;
}
bool tlabel::disable_click_dismiss() const
{
return false;
}
void tlabel::set_characters_per_line(const unsigned characters_per_line)
{
characters_per_line_ = characters_per_line;
}
void tlabel::set_link_aware(bool link_aware)
{
if(link_aware == link_aware_) {
return;
}
link_aware_ = link_aware;
update_canvas();
set_is_dirty(true);
}
void tlabel::set_link_color(const std::string & color)
{
if(color == link_color_) {
return;
}
link_color_ = color;
update_canvas();
set_is_dirty(true);
}
void tlabel::set_state(const tstate state)
{
if(state != state_) {
state_ = state;
set_is_dirty(true);
}
}
const std::string& tlabel::get_control_type() const
{
static const std::string type = "label";
return type;
}
void tlabel::load_config_extra()
{
assert(config());
boost::intrusive_ptr<const tlabel_definition::tresolution>
conf = boost::dynamic_pointer_cast<const tlabel_definition::tresolution>(
config());
assert(conf);
set_link_aware(conf->link_aware);
set_link_color(conf->link_color);
}
void tlabel::signal_handler_left_button_click(const event::tevent /* event */, bool & handled)
{
DBG_GUI_E << "label click" << std::endl;
if (!get_link_aware()) {
return; // without marking event as "handled".
}
if (!desktop::open_object_is_supported()) {
gui2::show_message(get_window()->video(), "", _("Opening links is not supported, contact your packager"), gui2::tmessage::auto_close);
handled = true;
return;
}
tpoint mouse = get_mouse_position();
mouse.x -= get_x();
mouse.y -= get_y();
std::string link = get_label_link(mouse);
if (link.length() == 0) {
return ; // without marking event as "handled"
}
DBG_GUI_E << "Clicked Link:\"" << link << "\"\n";
const int res = gui2::show_message(get_window()->video(), _("Confirm"), _("Do you want to open this link?") + std::string("\n\n") + link, gui2::tmessage::yes_no_buttons);
if(res == gui2::twindow::OK) {
desktop::open_object(link);
}
handled = true;
}
void tlabel::signal_handler_right_button_click(const event::tevent /* event */, bool & handled)
{
DBG_GUI_E << "label right click" << std::endl;
if (!get_link_aware()) {
return ; // without marking event as "handled".
}
tpoint mouse = get_mouse_position();
mouse.x -= get_x();
mouse.y -= get_y();
std::string link = get_label_link(mouse);
if (link.length() == 0) {
return ; // without marking event as "handled"
}
DBG_GUI_E << "Right Clicked Link:\"" << link << "\"\n";
desktop::clipboard::copy_to_clipboard(link, false);
gui2::show_message(get_window()->video(), "", _("Copied link!"), gui2::tmessage::auto_close);
handled = true;
}
} // namespace gui2