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 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 ); $nb->add_tab ( "Log", $self->{main_log} = CFPlus::UI::ChatView->new (expand => 1, say_command => ''), "This is the main log of the server." ); $self->{nb} = $nb; $self->{chatviews} = {}; $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 => $chaninfo->{title}, valign => 0, align => 0 ) ); $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 (my $cv = $self->{chatviews}->{$id}) { if ($cv != $self->{nb}->get_current_page) { $self->highlight_channel ($id); } } else { $self->add_chat ($id); } } sub highlight_channel { my ($self, $id) = @_; my $cv = $self->{chatviews}->{$id}; my $tab = $cv->{_tab_button}; $tab->set_markup ("" . $self->{channel_info}->{$id}->{title} . ""); } sub unhighlight_channel { my ($self, $id) = @_; my $cv = $self->{chatviews}->{$id}; my $tab = $cv->{_tab_button}; $tab->set_markup ($self->{channel_info}->{$id}->{title}); } sub add_channel { my ($self, $info) = @_; require Data::Dumper; print "DUMPER:" . Data::Dumper->Dump ([$info]) . "\n"; $self->{channel_info}->{$info->{id}} = $info; $self->touch_channel ($info->{id}); } sub message { my ($self, $para) = @_; my $id = $para->{type}; if ($self->{channel_info}->{$id}) { $self->touch_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;