ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.56
Committed: Sat Apr 1 19:13:27 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.55: +4 -4 lines
Log Message:
fixed bug with preferences dialog

File Contents

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