package CFPlus::UI::MessageWindow; use strict; use utf8; use Scalar::Util qw/weaken/; use CFPlus::UI::ChatView; our @ISA = CFPlus::UI::Toplevel::; our %channel_info; sub add_channel_info { my ($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 }); $nb->add ($cv); #$nb->set_current_page ($cv); } 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; my $tab = $_->{_tab_button}; next if $_->{_channel_highlighted}; 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) = @_; 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); } } $self->{chatviews}->{$id}->message ($para); } else { $self->{main_log}->message ($para); } } sub activate_console { my ($self, $preset) = @_; $self->{main_log}->activate_console ($preset); } sub set_fontsize { my ($self, $size) = @_; $self->{main_log}->set_fontsize ($size); } sub set_max_para { my ($self, $max_par) = @_; $self->{main_log}->set_max_para ($max_par); } 1;