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