ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.49
Committed: Mon Mar 20 04:11:41 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.48: +10 -5 lines
Log Message:
implemented help menu

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