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 |
$vb->add ( |
76 |
my $b = CFPlus::UI::Label->new ( |
77 |
expand => 1, markup => clr_def ($chaninfo->{title}), valign => 0, align => 0 |
78 |
) |
79 |
); |
80 |
|
81 |
$cv->{_chat_id} = $id; |
82 |
$cv->{_tab_button} = $b; |
83 |
weaken $cv->{_tab_button}; |
84 |
|
85 |
$vb->add ( |
86 |
my $b = CFPlus::UI::ImageButton->new ( |
87 |
path => 'x1_close.png', |
88 |
scale => 0.3 |
89 |
) |
90 |
); |
91 |
$b->connect (activate => sub { |
92 |
my $b = shift; |
93 |
|
94 |
my @chld = $nb->pages; |
95 |
my $cur = pop @chld; |
96 |
while (@chld && $cur != $cv) { |
97 |
$cur = pop @chld; |
98 |
} |
99 |
$cur = pop @chld; |
100 |
$nb->remove ($cv); |
101 |
$nb->set_current_page ($cur); |
102 |
|
103 |
delete $chatviews->{$id}; |
104 |
0 |
105 |
}); |
106 |
|
107 |
my $preadd = $nb->get_current_page; |
108 |
$nb->add ($cv); |
109 |
$nb->set_current_page ($preadd); |
110 |
} |
111 |
|
112 |
sub touch_channel { |
113 |
my ($self, $id) = @_; |
114 |
|
115 |
if (not exists $self->{chatviews}->{$id}) { |
116 |
$self->add_chat ($id); |
117 |
} |
118 |
} |
119 |
|
120 |
sub highlight_channel { |
121 |
my ($self, $id, $hlt_func) = @_; |
122 |
|
123 |
$hlt_func ||= \&clr_hlt; |
124 |
|
125 |
my $cv = $self->{chatviews}->{$id}; |
126 |
$cv->{_channel_highlighted} = 1; |
127 |
my $tab = $cv->{_tab_button}; |
128 |
|
129 |
$tab->set_markup ( |
130 |
$hlt_func->(defined $id ? $self->{channel_info}->{$id}->{title} : "Log") |
131 |
); |
132 |
} |
133 |
|
134 |
sub unhighlight_channel { |
135 |
my ($self, $id) = @_; |
136 |
|
137 |
my $cv = $self->{chatviews}->{$id}; |
138 |
$cv->{_channel_highlighted} = 0; |
139 |
my $tab = $cv->{_tab_button}; |
140 |
|
141 |
$tab->set_markup (clr_act (defined $id ? $self->{channel_info}->{$id}->{title} : "Log")); |
142 |
} |
143 |
|
144 |
sub update_current_tab { |
145 |
my ($self, $page) = @_; |
146 |
|
147 |
my $tab = $page->{_tab_button}; |
148 |
my $label; |
149 |
|
150 |
if (defined $page->{_chat_id}) { |
151 |
$label = $self->{channel_info}->{$page->{_chat_id}}->{title}; |
152 |
$self->unhighlight_channel ($page->{_chat_id}); |
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 |
} |
264 |
|
265 |
1; |
266 |
|