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=\"#777777\">$_[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->update_current_tab ($page); |
40 |
}); |
41 |
|
42 |
my $l = $self->{main_log} = |
43 |
CFPlus::UI::ChatView->new (expand => 1, say_command => ''); |
44 |
$l->{_tab_button} = |
45 |
$l->{c_tab} = |
46 |
CFPlus::UI::Button->new ( |
47 |
markup => "Log", tooltip => "This is the main log of the server." |
48 |
); |
49 |
|
50 |
$nb->add ($l); |
51 |
$self->update_current_tab ($l); |
52 |
|
53 |
$self |
54 |
} |
55 |
|
56 |
sub add_chat { |
57 |
my ($self, $id) = @_; |
58 |
|
59 |
my $chatviews = $self->{chatviews}; |
60 |
my $chaninfo = $self->{channel_info}->{$id}; |
61 |
my $nb = $self->{nb}; |
62 |
|
63 |
my $cv = $chatviews->{$id} = |
64 |
CFPlus::UI::ChatView->new ( |
65 |
expand => 1, |
66 |
say_command => $chaninfo->{reply}, |
67 |
entry_tooltip => $chaninfo->{tooltip}, |
68 |
text_tooltip => "Conversation with $chaninfo->{title}" |
69 |
); |
70 |
|
71 |
my $bb = CFPlus::UI::ButtonBin->new (tooltip => $chaninfo->{tooltip}); |
72 |
$cv->{c_tab} = $bb; |
73 |
|
74 |
$bb->add (my $vb = CFPlus::UI::Box->new); |
75 |
$bb->connect (activate => sub { $self->unhighlight_channel ($id); 0 }); |
76 |
$vb->add ( |
77 |
my $b = CFPlus::UI::Label->new ( |
78 |
expand => 1, markup => clr_def ($chaninfo->{title}), valign => 0, align => 0 |
79 |
) |
80 |
); |
81 |
|
82 |
$cv->{_chat_id} = $id; |
83 |
$cv->{_tab_button} = $b; |
84 |
weaken $cv->{_tab_button}; |
85 |
|
86 |
$vb->add ( |
87 |
my $b = CFPlus::UI::ImageButton->new ( |
88 |
path => 'x1_close.png', |
89 |
scale => 0.3 |
90 |
) |
91 |
); |
92 |
$b->connect (activate => sub { |
93 |
my $b = shift; |
94 |
|
95 |
my @chld = $nb->pages; |
96 |
my $cur = pop @chld; |
97 |
while (@chld && $cur != $cv) { |
98 |
$cur = pop @chld; |
99 |
} |
100 |
$cur = pop @chld; |
101 |
$nb->remove ($cv); |
102 |
$nb->set_current_page ($cur); |
103 |
|
104 |
delete $chatviews->{$id}; |
105 |
0 |
106 |
}); |
107 |
|
108 |
my $preadd = $nb->get_current_page; |
109 |
$nb->add ($cv); |
110 |
$nb->set_current_page ($preadd); |
111 |
} |
112 |
|
113 |
sub touch_channel { |
114 |
my ($self, $id) = @_; |
115 |
|
116 |
if (not exists $self->{chatviews}->{$id}) { |
117 |
$self->add_chat ($id); |
118 |
} |
119 |
} |
120 |
|
121 |
sub highlight_channel { |
122 |
my ($self, $id, $hlt_func) = @_; |
123 |
|
124 |
$hlt_func ||= \&clr_hlt; |
125 |
|
126 |
my $cv = $self->{chatviews}->{$id}; |
127 |
$cv->{_channel_highlighted} = 1; |
128 |
my $tab = $cv->{_tab_button}; |
129 |
|
130 |
$tab->set_markup ( |
131 |
$hlt_func->(defined $id ? $self->{channel_info}->{$id}->{title} : "Log") |
132 |
); |
133 |
} |
134 |
|
135 |
sub unhighlight_channel { |
136 |
my ($self, $id) = @_; |
137 |
|
138 |
my $cv = $self->{chatviews}->{$id}; |
139 |
$cv->{_channel_highlighted} = 0; |
140 |
my $tab = $cv->{_tab_button}; |
141 |
|
142 |
$tab->set_markup (clr_act (defined $id ? $self->{channel_info}->{$id}->{title} : "Log")); |
143 |
} |
144 |
|
145 |
sub update_current_tab { |
146 |
my ($self, $page) = @_; |
147 |
|
148 |
my $tab = $page->{_tab_button}; |
149 |
my $label; |
150 |
|
151 |
if (defined $page->{_chat_id}) { |
152 |
$label = $self->{channel_info}->{$page->{_chat_id}}->{title}; |
153 |
} |
154 |
|
155 |
$label = "Log" unless defined $label; |
156 |
|
157 |
$tab->set_markup (clr_act ($label)); |
158 |
|
159 |
for ($self->{nb}->pages) { |
160 |
next if $_ eq $page; |
161 |
next if $_->{_channel_highlighted}; |
162 |
|
163 |
my $tab = $_->{_tab_button}; |
164 |
next unless $tab; |
165 |
|
166 |
my $label; |
167 |
|
168 |
if (defined $_->{_chat_id}) { |
169 |
$label = $self->{channel_info}->{$_->{_chat_id}}->{title}; |
170 |
} |
171 |
|
172 |
$label = "Log" unless defined $label; |
173 |
|
174 |
$tab->set_markup (clr_def ($label)); |
175 |
} |
176 |
} |
177 |
|
178 |
sub add_channel { |
179 |
my ($self, $info) = @_; |
180 |
|
181 |
$self->{channel_info}->{$info->{id}} = $info; |
182 |
$self->touch_channel ($info->{id}); |
183 |
} |
184 |
|
185 |
sub message { |
186 |
my ($self, $para) = @_; |
187 |
|
188 |
#d# require Data::Dumper; |
189 |
#d# print "FOO[".Data::Dumper->Dump ([$para])."]\n"; |
190 |
|
191 |
my $id = $para->{type}; |
192 |
|
193 |
if (exists $self->{channel_info}->{$id}) { |
194 |
$self->touch_channel ($id); |
195 |
|
196 |
if (my $cv = $self->{chatviews}->{$id}) { |
197 |
|
198 |
if ($cv != $self->{nb}->get_current_page) { |
199 |
|
200 |
if (($para->{color_flags} & NDI_COLOR_MASK) == NDI_RED) { |
201 |
$self->highlight_channel ($id, \&clr_hlt2); |
202 |
} else { |
203 |
$self->highlight_channel ($id); |
204 |
} |
205 |
} |
206 |
|
207 |
if ($para->{color_flags} & NDI_REPLY) { |
208 |
$self->{nb}->set_current_page ($cv); |
209 |
} |
210 |
|
211 |
} |
212 |
|
213 |
$self->{chatviews}->{$id}->message ($para); |
214 |
|
215 |
} else { |
216 |
$self->{main_log}->message ($para); |
217 |
} |
218 |
} |
219 |
|
220 |
sub activate_console { |
221 |
my ($self, $preset) = @_; |
222 |
|
223 |
$self->{main_log}->activate_console ($preset); |
224 |
} |
225 |
|
226 |
sub activate_current { |
227 |
my ($self) = @_; |
228 |
|
229 |
$self->{nb}->get_current_page->activate_console; |
230 |
} |
231 |
|
232 |
sub set_fontsize { |
233 |
my ($self, $size) = @_; |
234 |
|
235 |
for (values %{$self->{chatviews}}, $self->{main_log}) { |
236 |
$_->set_fontsize ($size); |
237 |
} |
238 |
} |
239 |
|
240 |
sub set_max_para { |
241 |
my ($self, $max_par) = @_; |
242 |
|
243 |
for (values %{$self->{chatviews}}, $self->{main_log}) { |
244 |
$_->set_max_para ($max_par); |
245 |
} |
246 |
} |
247 |
|
248 |
sub user_switch_to_page { |
249 |
my ($self, $page) = @_; |
250 |
|
251 |
$page = $page eq '0' ? 10 : $page; |
252 |
|
253 |
my @tabs = $self->{nb}->pages; |
254 |
|
255 |
for (my $i = 0; $i < ($page - 1); $i++) { |
256 |
shift @tabs; |
257 |
} |
258 |
|
259 |
my $page = shift @tabs; |
260 |
return unless $page; |
261 |
|
262 |
$self->{nb}->set_current_page ($page); |
263 |
$page->activate_console; |
264 |
} |
265 |
|
266 |
1; |
267 |
|