ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/MainWindow.pm
Revision: 1.53
Committed: Sun Mar 26 15:41:59 2006 UTC (20 years, 7 months ago) by elmex
Branch: MAIN
Changes since 1.52: +16 -3 lines
Log Message:
fixed hopefully some modifier bugs and made code easier

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