package GCE::MainWindow; =head1 NAME GCE::MainWindow - the main window class for gce =cut use Gtk2; use Gtk2::Gdk::Keysyms; use Gtk2::SimpleMenu; use Crossfire; use Crossfire::Map; use Crossfire::MapWidget; use GCE::AttrEdit; use GCE::MapEditor; use GCE::StackView; use GCE::EditAction; use GCE::PickWindow; use GCE::AttrTypemap; use Glib::Object::Subclass Gtk2::Window; use GCE::Util; use strict; # XXX: make a recursive call from save_layout to all (interesting) sub-widgets sub save_layout { my ($self) = @_; $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0; $main::CFG->{stack_view_on} = exists $self->{sv} ? 1 : 0; $main::CFG->{picker_on} = exists $self->{last_pick_window} ? 1 : 0; $main::CFG->{main_window} = main::get_pos_and_size ($self); $main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window}) if $self->{last_map_window}; $main::CFG->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win}; $main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win}; $main::CFG->{last_folder} = $self->{fc_last_folder}; $main::CFG->{last_folders} = $self->{fc_last_folders}; $main::CFG->{open_pickers} = []; for (@{$self->{open_pick_windows}}) { next unless defined $_; push @{$main::CFG->{open_pickers}}, { p_and_s => main::get_pos_and_size ($_), selection => $_->{last_selection} }; } main::write_cfg ("$Crossfire::VARDIR/gceconfig"); } sub load_layout { my ($self) = @_; $self->{fc_last_folder} = $main::CFG->{last_folder}; $self->{fc_last_folders} = $main::CFG->{last_folders}; $main::CFG->{attr_edit_on} and $self->show_attr_editor; $main::CFG->{stack_view_on} and $self->show_stack_view; for (@{$main::CFG->{open_pickers}}) { $self->open_pick_window ($_); } } sub open_map_editor { my ($self, $mapfile) = @_; unless (ref $mapfile) { unless (File::Spec->file_name_is_absolute ($mapfile)) { $mapfile = File::Spec->rel2abs ($mapfile); } } # XXX: last_map_window is a dirty trick to get the position and size # for save layout unless (ref $mapfile) { if (defined $self->{loaded_maps}->{$mapfile}) { $self->{loaded_maps}->{$mapfile}->get_toplevel->present; return; } } my $w = $self->{last_map_window} = GCE::MapEditor->new; unless (ref $mapfile) { unless (-e $mapfile) { quick_msg ("file '$mapfile' does not exist!", 0); return; } $self->{loaded_maps}->{$mapfile} = $w; $w->signal_connect ('delete-event' => sub { delete $self->{loaded_maps}->{$mapfile}; }); } $w->open_map ($mapfile); ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0); $w->show_all; } sub show_stack_view { my ($self) = @_; return if defined $self->{sv}; my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel'); $w->set_title ('gce - stack view'); $w->signal_connect (delete_event => sub { delete $self->{sv}; 0 }); $w->add ($self->{sv} = GCE::StackView->new); main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250); $w->show_all; } sub show_attr_editor { my ($self) = @_; return if $self->{attr_edit}; my $w = $self->{attr_edit_win} = Gtk2::Window->new; $w->set_title ("gce - edit attrs"); $w->add ($self->{attr_edit} = GCE::AttrEdit->new); $w->signal_connect (delete_event => sub { delete $self->{attr_edit}; 0 }); main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200); $w->show_all; } sub update_attr_editor { my ($self, $arch, $cb) = @_; return unless $self->{attr_edit}; $self->{attr_edit}->set_arch ($arch, $cb); $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}"); } sub update_stack_view { my ($self, $mapedit, $x, $y) = @_; return unless $self->{sv}; $self->{sv}->set_stack ($mapedit, $x, $y); } sub open_pick_window { my ($self, $layout) = @_; # XXX: Yes, also fix this, save _every_ pick window and their positions and their # selection my $p = GCE::PickWindow->new (); push @{$self->{open_pick_windows}}, $p; my $idx = (@{$self->{open_pick_windows}}) - 1; $p->signal_connect ('delete-event' => sub { $self->{open_pick_windows}->[$idx] = undef; }); if ($layout) { main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200); } $p->show_all; $p->set_selection ($layout->{selection}); } sub build_menu { my ($self) = @_; my $menu_tree = [ _File => { item_type => '', children => [ _New => { callback => sub { $self->new_cb }, accelerator => 'N' }, _Open => { callback => sub { $self->open_cb }, accelerator => 'O' }, "_Save Layout" => { callback => sub { $self->save_layout }, accelerator => 'L' }, _Quit => { callback => sub { Gtk2->main_quit }, accelerator => 'Q' } ] }, _Dialogs => { item_type => '', children => [ "_Attr Editor" => { callback => sub { $self->show_attr_editor }, accelerator => "A" }, "_Picker" => { callback => sub { $self->open_pick_window }, accelerator => "P" }, "_Stack View" => { callback => sub { $self->show_stack_view }, accelerator => "V" }, ] } ]; my $men = Gtk2::SimpleMenu->new ( menu_tree => $menu_tree, default_callback => \&default_cb, ); $self->add_accel_group ($men->{accel_group}); return $men->{widget}; } sub add_button { my ($self, $table, $plcinfo, $lbl, $cb) = @_; my ($lx, $ly) = @{$plcinfo->{next}}; unless ($lx < $plcinfo->{width}) { $ly++; $lx = 0; } $ly < $plcinfo->{height} or die "too many buttons, make table bigger!"; $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1); $btn->signal_connect (clicked => $cb); $plcinfo->{next} = [$lx + 1, $ly]; } sub build_buttons { my ($self) = @_; my $tbl = Gtk2::Table->new (2, 4); my $plcinfo = { width => 2, height => 4, next => [0, 0] }; $self->{edit_collection}{pick} = GCE::EditAction::Pick->new; $self->{edit_collection}{place} = GCE::EditAction::Place->new; $self->{edit_collection}{erase} = GCE::EditAction::Erase->new; $self->{edit_collection}{select} = GCE::EditAction::Select->new; $self->{edit_collection}{perl} = GCE::EditAction::Perl->new; $self->{edit_collection}{connectexit} = GCE::EditAction::ConnectExit->new; $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new; $self->set_edit_tool ('pick'); $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') }); $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') }); $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') }); $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') }); $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') }); $self->add_button ($tbl, $plcinfo, "Connect E_xit", sub { $self->set_edit_tool ('connectexit') }); $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') }); return $tbl; } sub set_edit_tool { my ($self, $name) = @_; if ($name eq 'pick') { $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");; } elsif ($name eq 'place') { $self->update_edit_tool ($self->{edit_collection}{place}, "Place");; } elsif ($name eq 'erase') { $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");; } elsif ($name eq 'select') { $self->update_edit_tool ($self->{edit_collection}{select}, "Select");; } elsif ($name eq 'perl') { $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");; } elsif ($name eq 'connectexit') { $self->update_edit_tool ($self->{edit_collection}{connectexit}, "Connect Exit");; } elsif ($name eq 'followexit') { $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");; } } sub update_edit_tool { my ($self, $tool, $name) = @_; $self->{edit_tool}->set_text ($name); $self->{sel_editaction} = $tool; my $widget = $tool->tool_widget; for ($self->{edit_tool_cont}->get_children) { $_->hide; $self->{edit_tool_cont}->remove ($_); } defined $widget or return; $self->{edit_tool_cont}->add ($widget); $widget->show_all; } sub update_pick_view { my ($self, $arch) = @_; defined $arch->{_face} or $arch = $Crossfire::ARCH{$arch->{_name}}; fill_pb_from_arch ($self->{pick_view_pb}, $arch); $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb}); $self->{pick_view_btn}->set_label ($arch->{_name}); } sub INIT_INSTANCE { my ($self) = @_; $::MAINWIN = $self; $self->set_title ("gce - main window"); $self->{edit_tool} = Gtk2::Label->new; $self->{edit_tool_cont} = Gtk2::VBox->new; $self->add (my $vb = Gtk2::VBox->new); $vb->pack_start ($self->build_menu, 0, 1, 0); $vb->pack_start (my $hb = $self->{pick_view_hb} = Gtk2::HBox->new, 0, 1, 0); $hb->pack_start ($self->{pick_view_img} = Gtk2::Image->new, 0, 1, 0); $hb->pack_start ($self->{pick_view_btn} = Gtk2::Button->new, 0, 1, 0); $self->{pick_view_btn}->drag_source_set (['button1_mask'], ['move'], { target => 'STRING', flags => [], info => 'TARGET_STRING' } ); $self->{pick_view_btn}->signal_connect (drag_data_get => sub { my ($widget, $context, $data, $info, $time) = @_; $data->set ($data->target, 8, "pick"); }); $self->{pick_view_btn}->signal_connect (clicked => sub { $self->update_attr_editor ($self->{pick_arch}); }); $self->{pick_view_pb} = new_arch_pb (); $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0); $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); $vb->pack_start ($self->{edit_tool}, 0, 1, 0); $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0); # XXX:load $ARGV _cleanly_? $self->open_map_editor ($_) for @ARGV; $self->signal_connect ('delete-event' => sub { Gtk2->main_quit; }); ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0); } sub new_cb { my ($self) = @_; my $w = Gtk2::Window->new ('toplevel'); my $width = [width => 20]; my $height = [height => 20]; $w->add (my $tbl = Gtk2::Table->new (2, 3)); add_table_widget ($tbl, 0, $width, 'string'); add_table_widget ($tbl, 1, $height, 'string'); add_table_widget ($tbl, 2, 'new', 'button', sub { if ($width->[1] > 0 and $height->[1] > 0) { my $map = Crossfire::Map->new ($width->[1], $height->[1]); $map->resize ($width->[1], $height->[1]); $self->open_map_editor ($map); } $w->destroy; 1; }); add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy }); $w->show_all; } sub new_filechooser { my ($self, $title, $save, $filename) = @_; $title ||= 'gce - open map'; my $fc = new Gtk2::FileChooserDialog ( $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok' ); $fc->add_shortcut_folder ("$Crossfire::LIB/maps") if -d "$Crossfire::LIB/maps"; $fc->add_shortcut_folder ($_) for keys %{$self->{fc_last_folders}}; $fc->set_current_folder ($self->{fc_last_folder} || "$Crossfire::LIB/maps"); if ($filename) { $fc->set_filename ($filename); } $fc } sub open_cb { my ($self) = @_; my $fc = $self->new_filechooser; if ('ok' eq $fc->run) { $self->{fc_last_folder} = $fc->get_current_folder; $self->{fc_last_folders}->{$self->{fc_last_folder}}++; $self->open_map_editor ($fc->get_filename); } $fc->destroy; } sub set_pick { my ($self, $arch) = @_; $self->{pick_arch} = $arch; $self->update_pick_view ($arch); } sub get_pick { my ($self) = @_; # XXX: This is just to make sure that this function always returns something return $self->{pick_arch} || { _name => 'platinacoin' }; } =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ Robin Redeker http://www.ta-sa.org/ =cut 1;