1 |
elmex |
1.1 |
package CFPlus::UI::MessageWindow; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use utf8; |
5 |
|
|
|
6 |
elmex |
1.4 |
use Scalar::Util qw/weaken/; |
7 |
elmex |
1.3 |
use CFPlus::UI::ChatView; |
8 |
elmex |
1.12 |
use Crossfire::Protocol::Constants; |
9 |
elmex |
1.3 |
|
10 |
elmex |
1.1 |
our @ISA = CFPlus::UI::Toplevel::; |
11 |
|
|
|
12 |
elmex |
1.4 |
our %channel_info; |
13 |
|
|
|
14 |
root |
1.23 |
sub clr_def($) { "<span foreground=\"#ffffff\">$_[0]</span>" } |
15 |
|
|
sub clr_act($) { "<span foreground=\"#ffffff\">$_[0]</span>" } |
16 |
|
|
sub clr_hlt($) { "<span foreground=\"#aaaaff\">$_[0]</span>" } |
17 |
|
|
sub clr_hlt2($) { "<span foreground=\"#ff0000\">$_[0]</span>" } |
18 |
elmex |
1.9 |
|
19 |
elmex |
1.1 |
sub new { |
20 |
|
|
my $class = shift; |
21 |
root |
1.2 |
|
22 |
elmex |
1.1 |
my $self = $class->SUPER::new ( |
23 |
|
|
name => "message_window", |
24 |
|
|
title => "Messages", |
25 |
|
|
border_bg => [1, 1, 1, 1], |
26 |
|
|
x => "max", |
27 |
|
|
y => 0, |
28 |
|
|
force_w => $::WIDTH * 0.4, |
29 |
|
|
force_h => $::HEIGHT * 0.5, |
30 |
elmex |
1.3 |
child => (my $nb = CFPlus::UI::Notebook->new (expand => 1)), |
31 |
elmex |
1.1 |
has_close_button => 1 |
32 |
|
|
); |
33 |
root |
1.2 |
|
34 |
elmex |
1.4 |
$self->{nb} = $nb; |
35 |
|
|
$self->{chatviews} = {}; |
36 |
elmex |
1.1 |
|
37 |
elmex |
1.8 |
$nb->connect (page_changed => sub { |
38 |
|
|
my ($nb, $page) = @_; |
39 |
elmex |
1.17 |
$self->set_current_tab ($page); |
40 |
|
|
}); |
41 |
|
|
$nb->connect (c_add => sub { |
42 |
|
|
$self->update_tabs; |
43 |
elmex |
1.8 |
}); |
44 |
|
|
|
45 |
|
|
my $l = $self->{main_log} = |
46 |
|
|
CFPlus::UI::ChatView->new (expand => 1, say_command => ''); |
47 |
root |
1.23 |
|
48 |
elmex |
1.17 |
$l->{_tab_label} = |
49 |
elmex |
1.8 |
$l->{c_tab} = |
50 |
|
|
CFPlus::UI::Button->new ( |
51 |
|
|
markup => "Log", tooltip => "This is the main log of the server." |
52 |
|
|
); |
53 |
|
|
|
54 |
|
|
$nb->add ($l); |
55 |
elmex |
1.17 |
$nb->set_current_page ($l); |
56 |
elmex |
1.8 |
|
57 |
elmex |
1.1 |
$self |
58 |
|
|
} |
59 |
|
|
|
60 |
elmex |
1.3 |
sub add_chat { |
61 |
elmex |
1.4 |
my ($self, $id) = @_; |
62 |
|
|
|
63 |
|
|
my $chatviews = $self->{chatviews}; |
64 |
|
|
my $chaninfo = $self->{channel_info}->{$id}; |
65 |
|
|
my $nb = $self->{nb}; |
66 |
|
|
|
67 |
elmex |
1.6 |
my $cv = $chatviews->{$id} = |
68 |
|
|
CFPlus::UI::ChatView->new ( |
69 |
|
|
expand => 1, |
70 |
|
|
say_command => $chaninfo->{reply}, |
71 |
|
|
entry_tooltip => $chaninfo->{tooltip}, |
72 |
|
|
text_tooltip => "Conversation with $chaninfo->{title}" |
73 |
|
|
); |
74 |
|
|
|
75 |
|
|
my $bb = CFPlus::UI::ButtonBin->new (tooltip => $chaninfo->{tooltip}); |
76 |
elmex |
1.4 |
$cv->{c_tab} = $bb; |
77 |
|
|
|
78 |
|
|
$bb->add (my $vb = CFPlus::UI::Box->new); |
79 |
|
|
$vb->add ( |
80 |
|
|
my $b = CFPlus::UI::Label->new ( |
81 |
elmex |
1.9 |
expand => 1, markup => clr_def ($chaninfo->{title}), valign => 0, align => 0 |
82 |
elmex |
1.4 |
) |
83 |
|
|
); |
84 |
|
|
|
85 |
elmex |
1.8 |
$cv->{_chat_id} = $id; |
86 |
elmex |
1.17 |
$cv->{_tab_label} = $b; |
87 |
|
|
weaken $cv->{_tab_label}; |
88 |
elmex |
1.1 |
|
89 |
elmex |
1.4 |
$vb->add ( |
90 |
|
|
my $b = CFPlus::UI::ImageButton->new ( |
91 |
root |
1.23 |
path => 'x1_close.png', |
92 |
|
|
scale => 0.3, |
93 |
elmex |
1.4 |
) |
94 |
|
|
); |
95 |
|
|
$b->connect (activate => sub { |
96 |
|
|
my $b = shift; |
97 |
elmex |
1.1 |
|
98 |
elmex |
1.3 |
my @chld = $nb->pages; |
99 |
|
|
my $cur = pop @chld; |
100 |
elmex |
1.4 |
while (@chld && $cur != $cv) { |
101 |
elmex |
1.3 |
$cur = pop @chld; |
102 |
|
|
} |
103 |
|
|
$cur = pop @chld; |
104 |
elmex |
1.4 |
$nb->remove ($cv); |
105 |
elmex |
1.3 |
$nb->set_current_page ($cur); |
106 |
elmex |
1.4 |
|
107 |
|
|
delete $chatviews->{$id}; |
108 |
elmex |
1.3 |
0 |
109 |
|
|
}); |
110 |
elmex |
1.4 |
|
111 |
elmex |
1.12 |
my $preadd = $nb->get_current_page; |
112 |
elmex |
1.4 |
$nb->add ($cv); |
113 |
elmex |
1.12 |
$nb->set_current_page ($preadd); |
114 |
elmex |
1.4 |
} |
115 |
|
|
|
116 |
|
|
sub touch_channel { |
117 |
|
|
my ($self, $id) = @_; |
118 |
elmex |
1.12 |
|
119 |
elmex |
1.10 |
if (not exists $self->{chatviews}->{$id}) { |
120 |
elmex |
1.4 |
$self->add_chat ($id); |
121 |
|
|
} |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
sub highlight_channel { |
125 |
elmex |
1.14 |
my ($self, $id, $hlt_func) = @_; |
126 |
|
|
|
127 |
|
|
$hlt_func ||= \&clr_hlt; |
128 |
elmex |
1.12 |
|
129 |
elmex |
1.4 |
my $cv = $self->{chatviews}->{$id}; |
130 |
elmex |
1.12 |
|
131 |
elmex |
1.17 |
# the clr_hlt2 has a "higher priority" |
132 |
|
|
unless ($cv->{_channel_highlighted} eq \&clr_hlt2) { |
133 |
|
|
$cv->{_channel_highlighted} = $hlt_func; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
$self->update_tabs; |
137 |
elmex |
1.4 |
} |
138 |
|
|
|
139 |
elmex |
1.17 |
sub set_current_tab { |
140 |
|
|
my ($self, $page) = @_; |
141 |
|
|
|
142 |
|
|
for ($self->{nb}->pages) { |
143 |
|
|
next if $_ eq $page; |
144 |
|
|
$_->{_active} = 0; |
145 |
|
|
} |
146 |
|
|
$page->{_active} = 1; |
147 |
|
|
|
148 |
|
|
$self->update_tabs; |
149 |
|
|
} |
150 |
elmex |
1.9 |
|
151 |
elmex |
1.17 |
sub update_tabs { |
152 |
|
|
my ($self) = @_; |
153 |
elmex |
1.8 |
|
154 |
elmex |
1.17 |
my $i = 1; |
155 |
elmex |
1.8 |
for ($self->{nb}->pages) { |
156 |
elmex |
1.17 |
if ($i <= 10) { |
157 |
|
|
$_->{_tab_pos} = $i++; |
158 |
|
|
} else { |
159 |
|
|
$_->{_tab_pos} = undef; |
160 |
|
|
} |
161 |
elmex |
1.12 |
|
162 |
elmex |
1.17 |
my $tab = $_->{_tab_label}; |
163 |
elmex |
1.8 |
next unless $tab; |
164 |
|
|
|
165 |
elmex |
1.17 |
my ($label, $tooltip) = |
166 |
|
|
("Log", "This is the main log of the server."); |
167 |
elmex |
1.12 |
|
168 |
elmex |
1.9 |
if (defined $_->{_chat_id}) { |
169 |
elmex |
1.17 |
my $chinfo = $self->{channel_info}->{$_->{_chat_id}}; |
170 |
|
|
$label = $chinfo->{title}; |
171 |
|
|
$tooltip = $chinfo->{tooltip}; |
172 |
|
|
|
173 |
|
|
if ($_->{_active}) { |
174 |
|
|
$_->{_channel_highlighted} = 0; |
175 |
|
|
} |
176 |
elmex |
1.9 |
} |
177 |
elmex |
1.12 |
|
178 |
elmex |
1.17 |
$_->{c_tab}->set_tooltip ( |
179 |
|
|
$tooltip |
180 |
|
|
. (defined $_->{_tab_pos} |
181 |
elmex |
1.18 |
? "\n\n<small>Alt+" |
182 |
elmex |
1.17 |
. ($_->{_tab_pos} == 10 ? '0' : $_->{_tab_pos}) |
183 |
elmex |
1.18 |
. " - activates this tab.\n" |
184 |
|
|
. "Return - toggles activity of the entry." |
185 |
elmex |
1.17 |
. "</small>" |
186 |
|
|
: "") |
187 |
|
|
); |
188 |
|
|
|
189 |
|
|
my $hltfunc = $_->{_channel_highlighted} |
190 |
|
|
|| ($_->{_active} ? \&clr_act : \&clr_def); |
191 |
elmex |
1.8 |
|
192 |
elmex |
1.19 |
$tab->set_markup ( |
193 |
|
|
$hltfunc->($label . (defined $_->{_tab_pos} ? "\-$_->{_tab_pos}" : "")) |
194 |
|
|
); |
195 |
elmex |
1.8 |
} |
196 |
elmex |
1.4 |
} |
197 |
|
|
|
198 |
|
|
sub add_channel { |
199 |
|
|
my ($self, $info) = @_; |
200 |
elmex |
1.12 |
|
201 |
elmex |
1.4 |
$self->{channel_info}->{$info->{id}} = $info; |
202 |
|
|
$self->touch_channel ($info->{id}); |
203 |
|
|
} |
204 |
|
|
|
205 |
elmex |
1.3 |
sub message { |
206 |
|
|
my ($self, $para) = @_; |
207 |
elmex |
1.12 |
|
208 |
|
|
#d# require Data::Dumper; |
209 |
|
|
#d# print "FOO[".Data::Dumper->Dump ([$para])."]\n"; |
210 |
|
|
|
211 |
elmex |
1.4 |
my $id = $para->{type}; |
212 |
|
|
|
213 |
elmex |
1.9 |
if (exists $self->{channel_info}->{$id}) { |
214 |
elmex |
1.4 |
$self->touch_channel ($id); |
215 |
elmex |
1.12 |
|
216 |
elmex |
1.10 |
if (my $cv = $self->{chatviews}->{$id}) { |
217 |
elmex |
1.12 |
|
218 |
elmex |
1.10 |
if ($cv != $self->{nb}->get_current_page) { |
219 |
elmex |
1.14 |
|
220 |
|
|
if (($para->{color_flags} & NDI_COLOR_MASK) == NDI_RED) { |
221 |
|
|
$self->highlight_channel ($id, \&clr_hlt2); |
222 |
|
|
} else { |
223 |
|
|
$self->highlight_channel ($id); |
224 |
|
|
} |
225 |
elmex |
1.10 |
} |
226 |
elmex |
1.12 |
|
227 |
|
|
if ($para->{color_flags} & NDI_REPLY) { |
228 |
|
|
$self->{nb}->set_current_page ($cv); |
229 |
elmex |
1.14 |
} |
230 |
|
|
|
231 |
elmex |
1.21 |
if ($para->{color_flags} & NDI_CLEAR) { |
232 |
root |
1.22 |
$cv->clear_log; |
233 |
elmex |
1.21 |
} |
234 |
elmex |
1.10 |
} |
235 |
elmex |
1.12 |
|
236 |
elmex |
1.4 |
$self->{chatviews}->{$id}->message ($para); |
237 |
elmex |
1.12 |
|
238 |
elmex |
1.4 |
} else { |
239 |
|
|
$self->{main_log}->message ($para); |
240 |
|
|
} |
241 |
elmex |
1.1 |
} |
242 |
|
|
|
243 |
|
|
sub activate_console { |
244 |
|
|
my ($self, $preset) = @_; |
245 |
elmex |
1.12 |
|
246 |
elmex |
1.3 |
$self->{main_log}->activate_console ($preset); |
247 |
elmex |
1.1 |
} |
248 |
|
|
|
249 |
elmex |
1.12 |
sub activate_current { |
250 |
|
|
my ($self) = @_; |
251 |
|
|
|
252 |
|
|
$self->{nb}->get_current_page->activate_console; |
253 |
|
|
} |
254 |
|
|
|
255 |
elmex |
1.1 |
sub set_fontsize { |
256 |
|
|
my ($self, $size) = @_; |
257 |
elmex |
1.12 |
|
258 |
elmex |
1.11 |
for (values %{$self->{chatviews}}, $self->{main_log}) { |
259 |
|
|
$_->set_fontsize ($size); |
260 |
|
|
} |
261 |
elmex |
1.1 |
} |
262 |
|
|
|
263 |
|
|
sub set_max_para { |
264 |
|
|
my ($self, $max_par) = @_; |
265 |
elmex |
1.12 |
|
266 |
elmex |
1.11 |
for (values %{$self->{chatviews}}, $self->{main_log}) { |
267 |
|
|
$_->set_max_para ($max_par); |
268 |
|
|
} |
269 |
elmex |
1.1 |
} |
270 |
root |
1.2 |
|
271 |
elmex |
1.12 |
sub user_switch_to_page { |
272 |
|
|
my ($self, $page) = @_; |
273 |
|
|
|
274 |
|
|
$page = $page eq '0' ? 10 : $page; |
275 |
|
|
|
276 |
|
|
my @tabs = $self->{nb}->pages; |
277 |
|
|
|
278 |
|
|
for (my $i = 0; $i < ($page - 1); $i++) { |
279 |
|
|
shift @tabs; |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
my $page = shift @tabs; |
283 |
|
|
return unless $page; |
284 |
|
|
|
285 |
|
|
$self->{nb}->set_current_page ($page); |
286 |
|
|
} |
287 |
|
|
|
288 |
root |
1.22 |
1 |
289 |
root |
1.2 |
|