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 |
|
98 |
my @chld = $nb->pages; |
99 |
my $cur = pop @chld; |
100 |
while (@chld && $cur != $cv) { |
101 |
$cur = pop @chld; |
102 |
} |
103 |
$cur = pop @chld; |
104 |
$nb->remove ($cv); |
105 |
$nb->set_current_page ($cur); |
106 |
|
107 |
delete $chatviews->{$id}; |
108 |
0 |
109 |
}); |
110 |
|
111 |
my $preadd = $nb->get_current_page; |
112 |
$nb->add ($cv); |
113 |
$nb->set_current_page ($preadd); |
114 |
} |
115 |
|
116 |
sub touch_channel { |
117 |
my ($self, $id) = @_; |
118 |
|
119 |
if (not exists $self->{chatviews}->{$id}) { |
120 |
$self->add_chat ($id); |
121 |
} |
122 |
} |
123 |
|
124 |
sub highlight_channel { |
125 |
my ($self, $id, $hlt_func) = @_; |
126 |
|
127 |
$hlt_func ||= \&clr_hlt; |
128 |
|
129 |
my $cv = $self->{chatviews}->{$id}; |
130 |
|
131 |
# 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 |
} |
138 |
|
139 |
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 |
|
151 |
sub update_tabs { |
152 |
my ($self) = @_; |
153 |
|
154 |
my $i = 1; |
155 |
for ($self->{nb}->pages) { |
156 |
if ($i <= 10) { |
157 |
$_->{_tab_pos} = $i++; |
158 |
} else { |
159 |
$_->{_tab_pos} = undef; |
160 |
} |
161 |
|
162 |
my $tab = $_->{_tab_label}; |
163 |
next unless $tab; |
164 |
|
165 |
my ($label, $tooltip) = |
166 |
("Log", "This is the main log of the server."); |
167 |
|
168 |
if (defined $_->{_chat_id}) { |
169 |
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 |
} |
177 |
|
178 |
$_->{c_tab}->set_tooltip ( |
179 |
$tooltip |
180 |
. (defined $_->{_tab_pos} |
181 |
? "\n\n<small>Alt+" |
182 |
. ($_->{_tab_pos} == 10 ? '0' : $_->{_tab_pos}) |
183 |
. " - activates this tab.\n" |
184 |
. "Return - toggles activity of the entry." |
185 |
. "</small>" |
186 |
: "") |
187 |
); |
188 |
|
189 |
my $hltfunc = $_->{_channel_highlighted} |
190 |
|| ($_->{_active} ? \&clr_act : \&clr_def); |
191 |
|
192 |
$tab->set_markup ( |
193 |
$hltfunc->($label . (defined $_->{_tab_pos} ? "\-$_->{_tab_pos}" : "")) |
194 |
); |
195 |
} |
196 |
} |
197 |
|
198 |
sub add_channel { |
199 |
my ($self, $info) = @_; |
200 |
|
201 |
$self->{channel_info}->{$info->{id}} = $info; |
202 |
$self->touch_channel ($info->{id}); |
203 |
} |
204 |
|
205 |
sub message { |
206 |
my ($self, $para) = @_; |
207 |
|
208 |
#d# require Data::Dumper; |
209 |
#d# print "FOO[".Data::Dumper->Dump ([$para])."]\n"; |
210 |
|
211 |
my $id = $para->{type}; |
212 |
|
213 |
if (exists $self->{channel_info}->{$id}) { |
214 |
$self->touch_channel ($id); |
215 |
|
216 |
if (my $cv = $self->{chatviews}->{$id}) { |
217 |
|
218 |
if ($cv != $self->{nb}->get_current_page) { |
219 |
|
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 |
} |
226 |
|
227 |
if ($para->{color_flags} & NDI_REPLY) { |
228 |
$self->{nb}->set_current_page ($cv); |
229 |
} |
230 |
|
231 |
if ($para->{color_flags} & NDI_CLEAR) { |
232 |
$cv->clear_log; |
233 |
} |
234 |
} |
235 |
|
236 |
$self->{chatviews}->{$id}->message ($para); |
237 |
|
238 |
} else { |
239 |
$self->{main_log}->message ($para); |
240 |
} |
241 |
} |
242 |
|
243 |
sub activate_console { |
244 |
my ($self, $preset) = @_; |
245 |
|
246 |
$self->{main_log}->activate_console ($preset); |
247 |
} |
248 |
|
249 |
sub activate_current { |
250 |
my ($self) = @_; |
251 |
|
252 |
$self->{nb}->get_current_page->activate_console; |
253 |
} |
254 |
|
255 |
sub set_fontsize { |
256 |
my ($self, $size) = @_; |
257 |
|
258 |
for (values %{$self->{chatviews}}, $self->{main_log}) { |
259 |
$_->set_fontsize ($size); |
260 |
} |
261 |
} |
262 |
|
263 |
sub set_max_para { |
264 |
my ($self, $max_par) = @_; |
265 |
|
266 |
for (values %{$self->{chatviews}}, $self->{main_log}) { |
267 |
$_->set_max_para ($max_par); |
268 |
} |
269 |
} |
270 |
|
271 |
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 |
1 |
289 |
|