ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/MainWindow.pm
Revision: 1.58
Committed: Tue Apr 4 11:29:54 2006 UTC (20 years, 6 months ago) by elmex
Branch: MAIN
Changes since 1.57: +45 -49 lines
Log Message:
Removed pick and made the attribut editor 'the current pick'.
Added context menu to map view with an follow item in it.

File Contents

# User Rev Content
1 elmex 1.1 package GCE::MainWindow;
2    
3     =head1 NAME
4    
5     GCE::MainWindow - the main window class for gce
6    
7     =cut
8    
9 elmex 1.58 use Cwd qw/abs_path getcwd/;
10 elmex 1.1 use Gtk2;
11     use Gtk2::Gdk::Keysyms;
12 elmex 1.2 use Gtk2::SimpleMenu;
13 root 1.7
14     use Crossfire;
15 elmex 1.32 use Crossfire::Map;
16 root 1.7 use Crossfire::MapWidget;
17    
18 elmex 1.2 use GCE::AttrEdit;
19 elmex 1.13 use GCE::MapEditor;
20 elmex 1.15 use GCE::StackView;
21 elmex 1.17 use GCE::EditAction;
22     use GCE::PickWindow;
23 elmex 1.2
24 elmex 1.14 use GCE::AttrTypemap;
25    
26 elmex 1.53 use Glib::Object::Subclass
27 elmex 1.13 Gtk2::Window;
28 elmex 1.2
29 elmex 1.28 use GCE::Util;
30 elmex 1.54 use GCE::DragHelper;
31 elmex 1.28
32 elmex 1.2 use strict;
33    
34 elmex 1.18 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
35 elmex 1.17 sub save_layout {
36     my ($self) = @_;
37    
38 elmex 1.58 # $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0;
39 elmex 1.17 $main::CFG->{stack_view_on} = exists $self->{sv} ? 1 : 0;
40     $main::CFG->{picker_on} = exists $self->{last_pick_window} ? 1 : 0;
41     $main::CFG->{main_window} = main::get_pos_and_size ($self);
42     $main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window}) if $self->{last_map_window};
43     $main::CFG->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win};
44     $main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win};
45    
46 elmex 1.37 $main::CFG->{last_folders} = $self->{fc_last_folders};
47    
48 elmex 1.17 $main::CFG->{open_pickers} = [];
49    
50     for (@{$self->{open_pick_windows}}) {
51    
52     next unless defined $_;
53    
54     push @{$main::CFG->{open_pickers}}, {
55     p_and_s => main::get_pos_and_size ($_),
56     selection => $_->{last_selection}
57     };
58     }
59    
60 root 1.26 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
61 elmex 1.17 }
62    
63     sub load_layout {
64     my ($self) = @_;
65    
66 elmex 1.37 $self->{fc_last_folders} = $main::CFG->{last_folders};
67    
68 elmex 1.58 # $main::CFG->{attr_edit_on}
69     # and $self->show_attr_editor;
70 elmex 1.17
71     $main::CFG->{stack_view_on}
72     and $self->show_stack_view;
73    
74     for (@{$main::CFG->{open_pickers}}) {
75     $self->open_pick_window ($_);
76     }
77     }
78    
79     sub open_map_editor {
80     my ($self, $mapfile) = @_;
81    
82 elmex 1.44 my $mapkey;
83 elmex 1.39 unless (ref $mapfile) {
84 elmex 1.58 # unless (File::Spec->file_name_is_absolute ($mapfile)) {
85     # $mapfile = File::Spec->rel2abs ($mapfile);
86     # }
87     $mapkey = abs_path ($mapfile);
88     # File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::CFG->{MAPDIR}));
89     } else {
90     $mapkey = "$mapfile";
91 elmex 1.38 }
92    
93 elmex 1.17 # XXX: last_map_window is a dirty trick to get the position and size
94     # for save layout
95    
96 elmex 1.58 if (defined $self->{loaded_maps}->{$mapkey}) {
97     $self->{loaded_maps}->{$mapkey}->get_toplevel->present;
98     return;
99 elmex 1.38 }
100    
101 elmex 1.17 my $w = $self->{last_map_window} = GCE::MapEditor->new;
102 root 1.25
103 elmex 1.53 $self->{editors}->{$w} = $w;
104    
105     $w->signal_connect (destroy => sub {
106     my ($w) = @_;
107     delete $self->{loaded_maps}->{$w->{mapkey}};
108     delete $self->{editors}->{$w};
109     0;
110     });
111    
112 elmex 1.58 $self->{loaded_maps}->{$mapkey} = $w;
113 elmex 1.38
114 elmex 1.44 $w->open_map ($mapfile, $mapkey);
115 elmex 1.17
116 elmex 1.30 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
117 elmex 1.17
118 elmex 1.54 $w->set_edit_tool ($self->{sel_editaction});
119    
120 elmex 1.17 $w->show_all;
121     }
122    
123 elmex 1.47 sub show_help_window {
124     my ($self) = @_;
125    
126     return if defined $self->{help_win};
127 elmex 1.51 require Gtk2::Ex::PodViewer;
128 elmex 1.47 my $w = $self->{help_win} = Gtk2::Window->new;
129     $w->set_title ("gce - help");
130     $w->set_default_size (500, 300);
131 elmex 1.56 $w->signal_connect (destroy => sub {
132 elmex 1.53 $self->{help_win}->hide; $self->{help_win} = undef;
133     0
134     });
135 elmex 1.47 $w->add (my $sw = Gtk2::ScrolledWindow->new);
136 elmex 1.51 $sw->add (my $h = Gtk2::Ex::PodViewer->new);
137 elmex 1.47 $h->load_string ($::DOCUMENTATION);
138     $w->show_all;
139     }
140    
141 elmex 1.17 sub show_stack_view {
142 elmex 1.15 my ($self) = @_;
143    
144     return if defined $self->{sv};
145    
146 elmex 1.17 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
147 elmex 1.15 $w->set_title ('gce - stack view');
148 elmex 1.56 $w->signal_connect (destroy => sub { delete $self->{sv}; 0 });
149 elmex 1.15 $w->add ($self->{sv} = GCE::StackView->new);
150 elmex 1.17
151 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
152 elmex 1.17
153 elmex 1.15 $w->show_all;
154     }
155    
156 elmex 1.46 sub show_editor_properties {
157     my ($self) = @_;
158    
159     return if $self->{prop_edit};
160    
161     my $w = $self->{prop_edit} = Gtk2::Window->new;
162 elmex 1.52 $w->set_title ("gce - preferences");
163 elmex 1.46 $w->add (my $t = Gtk2::Table->new (2, 4));
164     $t->attach_defaults (my $lbl1 = Gtk2::Label->new ("CROSSFIRE_LIBDIR"), 0, 1, 0, 1);
165     $t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1);
166     $lib->set_text ($::CFG->{LIBDIR});
167     $t->attach_defaults (my $lbl2 = Gtk2::Label->new ("Map path"), 0, 1, 1, 2);
168     $t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2);
169     $map->set_text ($::CFG->{MAPDIR});
170     $t->attach_defaults (my $save = Gtk2::Button->new ('save'), 0, 2, 2, 3);
171     $save->signal_connect (clicked => sub {
172     $::CFG->{LIBDIR} = $lib->get_text;
173     $::CFG->{MAPDIR} = $map->get_text;
174     Crossfire::set_libdir ($::CFG->{LIBDIR});
175     Crossfire::load_archetypes;
176     Crossfire::load_tilecache;
177     main::write_cfg ("$Crossfire::VARDIR/gceconfig");
178     $w->destroy;
179     });
180     $t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 3, 4);
181     $close->signal_connect (clicked => sub { $w->destroy });
182    
183 elmex 1.56 $w->signal_connect (destroy => sub { delete $self->{prop_edit}; 0 });
184 elmex 1.46
185     main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200);
186    
187     $w->show_all;
188     }
189    
190 elmex 1.15 sub show_attr_editor {
191     my ($self) = @_;
192    
193     my $w = $self->{attr_edit_win} = Gtk2::Window->new;
194     $w->set_title ("gce - edit attrs");
195     $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
196 elmex 1.58 $w->signal_connect (destroy => sub { Gtk2->main_quit; 0 });
197 elmex 1.17
198 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200);
199 elmex 1.17
200 elmex 1.15 $w->show_all;
201     }
202    
203     sub update_attr_editor {
204 elmex 1.17 my ($self, $arch, $cb) = @_;
205 elmex 1.15
206 elmex 1.58 $self->{attr_edit}
207     or die "SERIOUS BUG: Couldn't find attribut editor!";
208 elmex 1.15
209 elmex 1.17 $self->{attr_edit}->set_arch ($arch, $cb);
210 elmex 1.15 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
211     }
212    
213     sub update_stack_view {
214 elmex 1.17 my ($self, $mapedit, $x, $y) = @_;
215 elmex 1.15
216     return unless $self->{sv};
217    
218 elmex 1.17 $self->{sv}->set_stack ($mapedit, $x, $y);
219 elmex 1.15 }
220    
221 elmex 1.17 sub open_pick_window {
222     my ($self, $layout) = @_;
223    
224 root 1.29 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
225 elmex 1.17 # selection
226     my $p = GCE::PickWindow->new ();
227 elmex 1.15
228 elmex 1.17 push @{$self->{open_pick_windows}}, $p;
229    
230     my $idx = (@{$self->{open_pick_windows}}) - 1;
231 elmex 1.15
232 elmex 1.17 $p->signal_connect ('delete-event' => sub {
233     $self->{open_pick_windows}->[$idx] = undef;
234     });
235 elmex 1.10
236 elmex 1.17 if ($layout) {
237 elmex 1.30 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
238 elmex 1.17 }
239 elmex 1.10
240 elmex 1.17 $p->show_all;
241 elmex 1.10
242 elmex 1.17 $p->set_selection ($layout->{selection});
243 elmex 1.10 }
244    
245 elmex 1.13 sub build_menu {
246 elmex 1.1 my ($self) = @_;
247    
248 elmex 1.2 my $menu_tree = [
249     _File => {
250     item_type => '<Branch>',
251     children => [
252     _New => {
253     callback => sub { $self->new_cb },
254     accelerator => '<ctrl>N'
255     },
256     _Open => {
257     callback => sub { $self->open_cb },
258     accelerator => '<ctrl>O'
259     },
260 elmex 1.17 "_Save Layout" => {
261     callback => sub { $self->save_layout },
262     accelerator => '<ctrl>L'
263     },
264 elmex 1.52 "_Preferences" => {
265 elmex 1.46 callback => sub { $self->show_editor_properties },
266     accelerator => "<ctrl>T"
267     },
268 elmex 1.49 _Quit => {
269 elmex 1.2 callback => sub { Gtk2->main_quit },
270     accelerator => '<ctrl>Q'
271 elmex 1.47 },
272 elmex 1.2 ]
273     },
274 root 1.24 _Dialogs => {
275 elmex 1.2 item_type => '<Branch>',
276     children => [
277 elmex 1.17 "_Picker" => {
278     callback => sub { $self->open_pick_window },
279     accelerator => "<ctrl>P"
280     },
281     "_Stack View" => {
282     callback => sub { $self->show_stack_view },
283     accelerator => "<ctrl>V"
284     },
285 elmex 1.2 ]
286 elmex 1.47 },
287 elmex 1.49 _Help => {
288     item_type => '<Branch>',
289     children => [
290 elmex 1.50 _Manual => {
291 elmex 1.49 callback => sub { $self->show_help_window },
292     accelerator => "<ctrl>H"
293     },
294     ]
295     },
296 elmex 1.2 ];
297    
298 elmex 1.13 my $men =
299     Gtk2::SimpleMenu->new (
300     menu_tree => $menu_tree,
301     default_callback => \&default_cb,
302     );
303 elmex 1.10
304 elmex 1.2 $self->add_accel_group ($men->{accel_group});
305    
306 elmex 1.13 return $men->{widget};
307     }
308 elmex 1.10
309 elmex 1.17 sub add_button {
310     my ($self, $table, $plcinfo, $lbl, $cb) = @_;
311    
312     my ($lx, $ly) = @{$plcinfo->{next}};
313    
314     unless ($lx < $plcinfo->{width}) {
315    
316     $ly++;
317     $lx = 0;
318     }
319    
320     $ly < $plcinfo->{height}
321     or die "too many buttons, make table bigger!";
322    
323 elmex 1.42 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
324 elmex 1.17 $btn->signal_connect (clicked => $cb);
325    
326     $plcinfo->{next} = [$lx + 1, $ly];
327     }
328    
329     sub build_buttons {
330     my ($self) = @_;
331    
332 elmex 1.38 my $tbl = Gtk2::Table->new (2, 4);
333     my $plcinfo = { width => 2, height => 4, next => [0, 0] };
334 elmex 1.17
335 elmex 1.38 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
336     $self->{edit_collection}{place} = GCE::EditAction::Place->new;
337     $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
338     $self->{edit_collection}{select} = GCE::EditAction::Select->new;
339     $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
340     $self->{edit_collection}{connectexit} = GCE::EditAction::ConnectExit->new;
341     $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
342 elmex 1.17
343 elmex 1.33 $self->set_edit_tool ('pick');
344 elmex 1.28
345 elmex 1.42 $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') });
346     $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') });
347     $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') });
348     $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') });
349     $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') });
350     $self->add_button ($tbl, $plcinfo, "Connect E_xit", sub { $self->set_edit_tool ('connectexit') });
351     $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
352 elmex 1.33
353     return $tbl;
354     }
355    
356     sub set_edit_tool {
357     my ($self, $name) = @_;
358    
359     if ($name eq 'pick') {
360 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
361 elmex 1.33 } elsif ($name eq 'place') {
362 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
363 elmex 1.33 } elsif ($name eq 'erase') {
364 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
365 elmex 1.34 } elsif ($name eq 'select') {
366     $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
367 elmex 1.57 $self->{edit_collection}{select}->update_overlay;
368 elmex 1.35 } elsif ($name eq 'perl') {
369     $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
370 elmex 1.38 } elsif ($name eq 'connectexit') {
371     $self->update_edit_tool ($self->{edit_collection}{connectexit}, "Connect Exit");;
372     } elsif ($name eq 'followexit') {
373     $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
374 elmex 1.33 }
375 elmex 1.17 }
376 elmex 1.10
377 elmex 1.28 sub update_edit_tool {
378     my ($self, $tool, $name) = @_;
379    
380 elmex 1.57 for (values %{$self->{loaded_maps}}) {
381     $_->{map}->overlay ('selection')
382     }
383    
384 elmex 1.28 $self->{edit_tool}->set_text ($name);
385     $self->{sel_editaction} = $tool;
386    
387     my $widget = $tool->tool_widget;
388    
389     for ($self->{edit_tool_cont}->get_children) {
390     $_->hide;
391     $self->{edit_tool_cont}->remove ($_);
392     }
393    
394 elmex 1.53 $_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}});
395    
396 elmex 1.28 defined $widget or return;
397    
398     $self->{edit_tool_cont}->add ($widget);
399     $widget->show_all;
400     }
401    
402     sub update_pick_view {
403     my ($self, $arch) = @_;
404    
405     defined $arch->{_face}
406     or $arch = $Crossfire::ARCH{$arch->{_name}};
407    
408     fill_pb_from_arch ($self->{pick_view_pb}, $arch);
409     $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
410    
411 elmex 1.33 $self->{pick_view_btn}->set_label ($arch->{_name});
412 elmex 1.28 }
413    
414 elmex 1.13 sub INIT_INSTANCE {
415     my ($self) = @_;
416 elmex 1.10
417 root 1.24 $::MAINWIN = $self;
418 elmex 1.15
419 root 1.48 $self->set_title ("gce - toolbox");
420 elmex 1.10
421 elmex 1.28 $self->{edit_tool} = Gtk2::Label->new;
422     $self->{edit_tool_cont} = Gtk2::VBox->new;
423    
424 elmex 1.13 $self->add (my $vb = Gtk2::VBox->new);
425     $vb->pack_start ($self->build_menu, 0, 1, 0);
426 elmex 1.28
427 elmex 1.58 # $vb->pack_start (my $hb = $self->{pick_view_hb} = Gtk2::HBox->new, 0, 1, 0);
428     # $hb->pack_start ($self->{pick_view_img} = Gtk2::Image->new, 0, 1, 0);
429     # $hb->pack_start ($self->{pick_view_btn} = Gtk2::Button->new, 0, 1, 0);
430     # GCE::DragHelper::set_drag_source (
431     # $self->{pick_view_btn}, arch => sub { { arch => $self->get_pick } }
432     # );
433     # GCE::DragHelper::set_drag_sink (
434     # $self->{pick_view_btn}, arch => sub { $self->set_pick ($_[0]->{arch}) }
435     # );
436     # $self->{pick_view_btn}->signal_connect (clicked => sub {
437     # $self->update_attr_editor ($self->{pick_arch});
438     # });
439     # $self->{pick_view_pb} = new_arch_pb ();
440 elmex 1.28
441 elmex 1.58 # $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
442 elmex 1.28 $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
443    
444     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
445     $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
446    
447     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
448 elmex 1.35 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
449 elmex 1.10
450 elmex 1.13 # XXX:load $ARGV _cleanly_?
451 root 1.27 $self->open_map_editor ($_)
452     for @ARGV;
453 root 1.11
454 elmex 1.13 $self->signal_connect ('delete-event' => sub {
455     Gtk2->main_quit;
456 elmex 1.10 });
457 elmex 1.17
458 elmex 1.30 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
459 elmex 1.58
460    
461     $self->show_attr_editor;
462 elmex 1.2 }
463    
464     sub new_cb {
465     my ($self) = @_;
466 elmex 1.32
467     my $w = Gtk2::Window->new ('toplevel');
468     my $width = [width => 20];
469     my $height = [height => 20];
470     $w->add (my $tbl = Gtk2::Table->new (2, 3));
471     add_table_widget ($tbl, 0, $width, 'string');
472     add_table_widget ($tbl, 1, $height, 'string');
473     add_table_widget ($tbl, 2, 'new', 'button', sub {
474     if ($width->[1] > 0 and $height->[1] > 0) {
475     my $map = Crossfire::Map->new ($width->[1], $height->[1]);
476 elmex 1.33 $map->resize ($width->[1], $height->[1]);
477 elmex 1.32 $self->open_map_editor ($map);
478     }
479     $w->destroy;
480     1;
481     });
482     add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
483     $w->show_all;
484 elmex 1.2 }
485    
486 root 1.25 sub new_filechooser {
487 elmex 1.37 my ($self, $title, $save, $filename) = @_;
488 elmex 1.2
489 elmex 1.33 $title ||= 'gce - open map';
490 root 1.25 my $fc = new Gtk2::FileChooserDialog (
491 elmex 1.33 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
492 elmex 1.13 );
493    
494 elmex 1.46 $fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR};
495 elmex 1.45 $fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}};
496 elmex 1.55 $fc->set_current_folder (getcwd);
497 elmex 1.2
498 elmex 1.37 if ($filename) {
499     $fc->set_filename ($filename);
500     }
501    
502 root 1.25 $fc
503     }
504    
505     sub open_cb {
506     my ($self) = @_;
507    
508     my $fc = $self->new_filechooser;
509    
510 elmex 1.2 if ('ok' eq $fc->run) {
511 elmex 1.13
512 elmex 1.10 $self->{fc_last_folder} = $fc->get_current_folder;
513 elmex 1.13 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
514 elmex 1.17
515     $self->open_map_editor ($fc->get_filename);
516 elmex 1.2 }
517    
518     $fc->destroy;
519     }
520    
521 elmex 1.58 #sub set_pick {
522     # my ($self, $arch) = @_;
523     #
524     # $self->{pick_arch} = $arch;
525     # $self->update_pick_view ($arch);
526     #}
527 elmex 1.17
528     sub get_pick {
529     my ($self) = @_;
530    
531 elmex 1.58 $self->{attr_edit}
532     or die "Couldn't find attribute editor! SERIOUS BUG!";
533    
534 elmex 1.17 # XXX: This is just to make sure that this function always returns something
535 elmex 1.58 return $self->{attr_edit}->get_arch || { _name => 'platinacoin' };
536 elmex 1.17 }
537    
538 elmex 1.1 =head1 AUTHOR
539    
540     Marc Lehmann <schmorp@schmorp.de>
541     http://home.schmorp.de/
542    
543     Robin Redeker <elmex@ta-sa.org>
544     http://www.ta-sa.org/
545    
546     =cut
547     1;
548