package CFPlus::UI::MessageWindow; use strict; use utf8; use Scalar::Util qw/weaken/; use CFPlus::UI::ChatView; use Crossfire::Protocol::Constants; our @ISA = CFPlus::UI::Toplevel::; our %channel_info; sub clr_def($) { "$_[0]" } sub clr_act($) { "$_[0]" } sub clr_hlt($) { "$_[0]" } sub clr_hlt2($) { "$_[0]" } sub new { my $class = shift; my $self = $class->SUPER::new ( name => "message_window", title => "Messages", border_bg => [1, 1, 1, 1], x => "max", y => 0, force_w => $::WIDTH * 0.4, force_h => $::HEIGHT * 0.5, child => (my $nb = CFPlus::UI::Notebook->new (expand => 1)), has_close_button => 1 ); $self->{nb} = $nb; $self->{chatviews} = {}; $nb->connect (page_changed => sub { my ($nb, $page) = @_; $self->update_current_tab ($page); }); my $l = $self->{main_log} = CFPlus::UI::ChatView->new (expand => 1, say_command => ''); $l->{_tab_button} = $l->{c_tab} = CFPlus::UI::Button->new ( markup => "Log", tooltip => "This is the main log of the server." ); $nb->add ($l); $self->update_current_tab ($l); $self } sub add_chat { my ($self, $id) = @_; my $chatviews = $self->{chatviews}; my $chaninfo = $self->{channel_info}->{$id}; my $nb = $self->{nb}; my $cv = $chatviews->{$id} = CFPlus::UI::ChatView->new ( expand => 1, say_command => $chaninfo->{reply}, entry_tooltip => $chaninfo->{tooltip}, text_tooltip => "Conversation with $chaninfo->{title}" ); my $bb = CFPlus::UI::ButtonBin->new (tooltip => $chaninfo->{tooltip}); $cv->{c_tab} = $bb; $bb->add (my $vb = CFPlus::UI::Box->new); $bb->connect (activate => sub { $self->unhighlight_channel ($id); 0 }); $vb->add ( my $b = CFPlus::UI::Label->new ( expand => 1, markup => clr_def ($chaninfo->{title}), valign => 0, align => 0 ) ); $cv->{_chat_id} = $id; $cv->{_tab_button} = $b; weaken $cv->{_tab_button}; $vb->add ( my $b = CFPlus::UI::ImageButton->new ( path => 'x1_close.png', scale => 0.3 ) ); $b->connect (activate => sub { my $b = shift; my @chld = $nb->pages; my $cur = pop @chld; while (@chld && $cur != $cv) { $cur = pop @chld; } $cur = pop @chld; $nb->remove ($cv); $nb->set_current_page ($cur); delete $chatviews->{$id}; 0 }); my $preadd = $nb->get_current_page; $nb->add ($cv); $nb->set_current_page ($preadd); } sub touch_channel { my ($self, $id) = @_; if (not exists $self->{chatviews}->{$id}) { $self->add_chat ($id); } } sub highlight_channel { my ($self, $id) = @_; my $cv = $self->{chatviews}->{$id}; $cv->{_channel_highlighted} = 1; my $tab = $cv->{_tab_button}; $tab->set_markup ( clr_hlt (defined $id ? $self->{channel_info}->{$id}->{title} : "Log") ); } sub unhighlight_channel { my ($self, $id) = @_; my $cv = $self->{chatviews}->{$id}; $cv->{_channel_highlighted} = 0; my $tab = $cv->{_tab_button}; $tab->set_markup (clr_act (defined $id ? $self->{channel_info}->{$id}->{title} : "Log")); } sub update_current_tab { my ($self, $page) = @_; my $tab = $page->{_tab_button}; my $label; if (defined $page->{_chat_id}) { $label = $self->{channel_info}->{$page->{_chat_id}}->{title}; } $label = "Log" unless defined $label; $tab->set_markup (clr_act ($label)); for ($self->{nb}->pages) { next if $_ eq $page; next if $_->{_channel_highlighted}; my $tab = $_->{_tab_button}; next unless $tab; my $label; if (defined $_->{_chat_id}) { $label = $self->{channel_info}->{$_->{_chat_id}}->{title}; } $label = "Log" unless defined $label; $tab->set_markup (clr_def ($label)); } } sub add_channel { my ($self, $info) = @_; $self->{channel_info}->{$info->{id}} = $info; $self->touch_channel ($info->{id}); } sub message { my ($self, $para) = @_; #d# require Data::Dumper; #d# print "FOO[".Data::Dumper->Dump ([$para])."]\n"; my $id = $para->{type}; if (exists $self->{channel_info}->{$id}) { $self->touch_channel ($id); if (my $cv = $self->{chatviews}->{$id}) { if ($cv != $self->{nb}->get_current_page) { $self->highlight_channel ($id); } if ($para->{color_flags} & NDI_REPLY) { $self->{nb}->set_current_page ($cv); $cv->activate_console; } } $self->{chatviews}->{$id}->message ($para); } else { $self->{main_log}->message ($para); } } sub activate_console { my ($self, $preset) = @_; $self->{main_log}->activate_console ($preset); } sub activate_current { my ($self) = @_; $self->{nb}->get_current_page->activate_console; } sub set_fontsize { my ($self, $size) = @_; for (values %{$self->{chatviews}}, $self->{main_log}) { $_->set_fontsize ($size); } } sub set_max_para { my ($self, $max_par) = @_; for (values %{$self->{chatviews}}, $self->{main_log}) { $_->set_max_para ($max_par); } } sub user_switch_to_page { my ($self, $page) = @_; $page = $page eq '0' ? 10 : $page; my @tabs = $self->{nb}->pages; for (my $i = 0; $i < ($page - 1); $i++) { shift @tabs; } my $page = shift @tabs; return unless $page; $self->{nb}->set_current_page ($page); $page->activate_console; } 1;