ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI/MessageWindow.pm
Revision: 1.25
Committed: Wed Dec 26 18:09:30 2007 UTC (16 years, 7 months ago) by root
Branch: MAIN
Changes since 1.24: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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 root 1.25 use Deliantra::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.24 $self->close_chatview ($cv);
98 elmex 1.3 0
99     });
100 elmex 1.4
101 elmex 1.12 my $preadd = $nb->get_current_page;
102 elmex 1.4 $nb->add ($cv);
103 elmex 1.12 $nb->set_current_page ($preadd);
104 elmex 1.4 }
105    
106 elmex 1.24 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 elmex 1.4 sub touch_channel {
125     my ($self, $id) = @_;
126 elmex 1.12
127 elmex 1.10 if (not exists $self->{chatviews}->{$id}) {
128 elmex 1.4 $self->add_chat ($id);
129     }
130     }
131    
132     sub highlight_channel {
133 elmex 1.14 my ($self, $id, $hlt_func) = @_;
134    
135     $hlt_func ||= \&clr_hlt;
136 elmex 1.12
137 elmex 1.4 my $cv = $self->{chatviews}->{$id};
138 elmex 1.12
139 elmex 1.17 # 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 elmex 1.4 }
146    
147 elmex 1.17 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 elmex 1.9
159 elmex 1.24 sub close_current_tab {
160     my ($self) = @_;
161     $self->close_chatview ($self->{nb}->get_current_page);
162     }
163    
164 elmex 1.17 sub update_tabs {
165     my ($self) = @_;
166 elmex 1.8
167 elmex 1.17 my $i = 1;
168 elmex 1.8 for ($self->{nb}->pages) {
169 elmex 1.17 if ($i <= 10) {
170     $_->{_tab_pos} = $i++;
171     } else {
172     $_->{_tab_pos} = undef;
173     }
174 elmex 1.12
175 elmex 1.17 my $tab = $_->{_tab_label};
176 elmex 1.8 next unless $tab;
177    
178 elmex 1.17 my ($label, $tooltip) =
179     ("Log", "This is the main log of the server.");
180 elmex 1.12
181 elmex 1.9 if (defined $_->{_chat_id}) {
182 elmex 1.17 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 elmex 1.9 }
190 elmex 1.12
191 elmex 1.17 $_->{c_tab}->set_tooltip (
192     $tooltip
193     . (defined $_->{_tab_pos}
194 elmex 1.18 ? "\n\n<small>Alt+"
195 elmex 1.17 . ($_->{_tab_pos} == 10 ? '0' : $_->{_tab_pos})
196 elmex 1.18 . " - activates this tab.\n"
197     . "Return - toggles activity of the entry."
198 elmex 1.17 . "</small>"
199     : "")
200     );
201    
202     my $hltfunc = $_->{_channel_highlighted}
203     || ($_->{_active} ? \&clr_act : \&clr_def);
204 elmex 1.8
205 elmex 1.19 $tab->set_markup (
206     $hltfunc->($label . (defined $_->{_tab_pos} ? "\-$_->{_tab_pos}" : ""))
207     );
208 elmex 1.8 }
209 elmex 1.4 }
210    
211     sub add_channel {
212     my ($self, $info) = @_;
213 elmex 1.12
214 elmex 1.4 $self->{channel_info}->{$info->{id}} = $info;
215     $self->touch_channel ($info->{id});
216     }
217    
218 elmex 1.3 sub message {
219     my ($self, $para) = @_;
220 elmex 1.12
221     #d# require Data::Dumper;
222     #d# print "FOO[".Data::Dumper->Dump ([$para])."]\n";
223    
224 elmex 1.4 my $id = $para->{type};
225    
226 elmex 1.9 if (exists $self->{channel_info}->{$id}) {
227 elmex 1.4 $self->touch_channel ($id);
228 elmex 1.12
229 elmex 1.10 if (my $cv = $self->{chatviews}->{$id}) {
230 elmex 1.12
231 elmex 1.10 if ($cv != $self->{nb}->get_current_page) {
232 elmex 1.14
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 elmex 1.10 }
239 elmex 1.12
240     if ($para->{color_flags} & NDI_REPLY) {
241     $self->{nb}->set_current_page ($cv);
242 elmex 1.14 }
243    
244 elmex 1.21 if ($para->{color_flags} & NDI_CLEAR) {
245 root 1.22 $cv->clear_log;
246 elmex 1.21 }
247 elmex 1.10 }
248 elmex 1.12
249 elmex 1.4 $self->{chatviews}->{$id}->message ($para);
250 elmex 1.12
251 elmex 1.4 } else {
252     $self->{main_log}->message ($para);
253     }
254 elmex 1.1 }
255    
256     sub activate_console {
257     my ($self, $preset) = @_;
258 elmex 1.12
259 elmex 1.3 $self->{main_log}->activate_console ($preset);
260 elmex 1.1 }
261    
262 elmex 1.12 sub activate_current {
263     my ($self) = @_;
264    
265     $self->{nb}->get_current_page->activate_console;
266     }
267    
268 elmex 1.1 sub set_fontsize {
269     my ($self, $size) = @_;
270 elmex 1.12
271 elmex 1.11 for (values %{$self->{chatviews}}, $self->{main_log}) {
272     $_->set_fontsize ($size);
273     }
274 elmex 1.1 }
275    
276     sub set_max_para {
277     my ($self, $max_par) = @_;
278 elmex 1.12
279 elmex 1.11 for (values %{$self->{chatviews}}, $self->{main_log}) {
280     $_->set_max_para ($max_par);
281     }
282 elmex 1.1 }
283 root 1.2
284 elmex 1.12 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 root 1.22 1
302 root 1.2