ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.80
Committed: Mon Aug 20 07:56:58 2007 UTC (19 years, 1 month ago) by elmex
Branch: MAIN
CVS Tags: rel-2_2
Changes since 1.79: +74 -1 lines
Log Message:
applied irwiss patch with two minor modifications:

- ->isa instead of =~ /MenuBar/
- wrapped the reading of recent files in a block

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 qw/abs_path getcwd/;
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 Glib::Object::Subclass
25 Gtk2::Window;
26
27 use GCE::Util;
28 use GCE::DragHelper;
29
30 use strict;
31
32 my $recentfile = "$Crossfire::VARDIR/gcerecent";
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->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win};
43 $main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win};
44
45 if ($self->{last_map_window}) {
46 $main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window});
47 $self->{last_map_window}->save_layout ();
48 }
49
50 $self->{worldmap_coord_query}->save_layout ()
51 if $self->{worldmap_coord_query};
52
53 $main::CFG->{last_folders} = $self->{fc_last_folders};
54
55 $main::CFG->{open_pickers} = [];
56
57 for (@{$self->{open_pick_windows}}) {
58
59 next unless defined $_;
60
61 push @{$main::CFG->{open_pickers}}, {
62 p_and_s => main::get_pos_and_size ($_),
63 selection => $_->{last_selection}
64 };
65 }
66
67 $self->{attr_edit}->save_layout;
68
69 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
70 }
71
72 sub load_layout {
73 my ($self) = @_;
74
75 $self->{fc_last_folders} = $main::CFG->{last_folders};
76
77 # $main::CFG->{attr_edit_on}
78 # and $self->show_attr_editor;
79
80 $main::CFG->{stack_view_on}
81 and $self->show_stack_view;
82
83 for (@{$main::CFG->{open_pickers}}) {
84 $self->open_pick_window ($_);
85 }
86
87 $self->{attr_edit}->load_layout;
88 }
89
90 sub open_map_editor {
91 my ($self, $mapfile) = @_;
92
93 my $mapkey;
94 unless (ref $mapfile) {
95 # unless (File::Spec->file_name_is_absolute ($mapfile)) {
96 # $mapfile = File::Spec->rel2abs ($mapfile);
97 # }
98 $mapkey = abs_path ($mapfile);
99 # File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::MAPDIR));
100 } else {
101 $mapkey = "$mapfile";
102 }
103
104 # XXX: last_map_window is a dirty trick to get the position and size
105 # for save layout
106
107 if (defined $self->{loaded_maps}->{$mapkey}) {
108 $self->{loaded_maps}->{$mapkey}->get_toplevel->present;
109 return;
110 }
111
112 my $w = $self->{last_map_window} = GCE::MapEditor->new;
113
114 $self->{editors}->{$w} = $w;
115
116 $w->signal_connect (destroy => sub {
117 my ($w) = @_;
118 $w->close_windows;
119 delete $self->{loaded_maps}->{$w->{mapkey}};
120 delete $self->{editors}->{$w};
121 0;
122 });
123
124 $self->{loaded_maps}->{$mapkey} = $w;
125
126 eval { $w->open_map ($mapfile, $mapkey) };
127 if ($@) {
128 quick_msg ($self, "$@", 1);
129 $w->close_windows;
130 delete $self->{loaded_maps}->{$w->{mapkey}};
131 delete $self->{editors}->{$w};
132 $w->destroy;
133 return;
134 }
135
136 $w->set_edit_tool ($self->{sel_editaction});
137
138 $w->show_all;
139 }
140
141 sub show_help_window {
142 my ($self) = @_;
143
144 return if defined $self->{help_win};
145 require Gtk2::Ex::PodViewer;
146 my $w = $self->{help_win} = Gtk2::Window->new;
147 $w->set_title ("gcrossedit - help");
148 $w->set_default_size (500, 300);
149 $w->signal_connect (destroy => sub {
150 $self->{help_win}->hide; $self->{help_win} = undef;
151 0
152 });
153 $w->add (my $sw = Gtk2::ScrolledWindow->new);
154 $sw->add (my $h = Gtk2::Ex::PodViewer->new);
155 $h->load_string ($::DOCUMENTATION);
156 $w->show_all;
157 }
158
159 sub show_stack_view {
160 my ($self) = @_;
161
162 return if defined $self->{sv};
163
164 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
165 $w->set_title ('gcrossedit - stack view');
166 $w->signal_connect (destroy => sub { delete $self->{sv}; 0 });
167 $w->add ($self->{sv} = GCE::StackView->new);
168
169 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
170
171 $w->show_all;
172 }
173
174 sub show_editor_properties {
175 my ($self) = @_;
176
177 return if $self->{prop_edit};
178
179 my $w = $self->{prop_edit} = Gtk2::Window->new;
180 $w->set_title ("gcrossedit - preferences");
181 $w->add (my $t = Gtk2::Table->new (2, 5));
182 $t->attach_defaults (my $lbl1 = Gtk2::Label->new ("LIBDIR"), 0, 1, 0, 1);
183 $t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1);
184 $lib->set_text ($::CFG->{LIBDIR});
185 $t->attach_defaults (my $lbl2 = Gtk2::Label->new ("MAPDIR"), 0, 1, 1, 2);
186 $t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2);
187 $map->set_text ($::CFG->{MAPDIR});
188 $t->attach_defaults (my $lbl1 = Gtk2::Label->new ("Username"), 0, 1, 2, 3);
189 $t->attach_defaults (my $usern = Gtk2::Entry->new, 1, 2, 2, 3);
190 $usern->set_text ($::CFG->{username});
191 $t->attach_defaults (my $save = Gtk2::Button->new ('save'), 0, 2, 3, 4);
192 $save->signal_connect (clicked => sub {
193 $::CFG->{LIBDIR} = $lib->get_text;
194 $::CFG->{MAPDIR} = $map->get_text;
195 $::LIBDIR = $::CFG->{LIBDIR} if $::CFG->{LIBDIR};
196 $::MAPDIR = $::CFG->{MAPDIR} if $::CFG->{MAPDIR};
197 $::CFG->{username} = $usern->get_text;
198 Crossfire::set_libdir ($::LIBDIR);
199 Crossfire::load_archetypes;
200 Crossfire::load_tilecache;
201 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
202 $w->destroy;
203 });
204 $t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 4, 5);
205 $close->signal_connect (clicked => sub { $w->destroy });
206
207 $w->signal_connect (destroy => sub { delete $self->{prop_edit}; 0 });
208
209 main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200);
210
211 $w->show_all;
212 }
213
214 sub show_attr_editor {
215 my ($self) = @_;
216
217 return if $self->{attr_edit_win};
218
219 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
220 $w->set_title ("gcrossedit - edit attrs");
221 $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
222 $w->signal_connect (destroy => sub { delete $self->{attr_edit_win}; 0 });
223
224 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 400, 300, 250, 0);
225
226 $w->show_all;
227 }
228
229 sub update_attr_editor {
230 my ($self, $ar) = @_;
231
232 if (ref ($ar) ne 'GCE::ArchRef') { require Carp; Carp::confess ("$ar no ARCHREF!") }
233
234 $self->{attr_edit}
235 or die "SERIOUS BUG: Couldn't find attribut editor!";
236
237 $self->{attr_edit}->set_arch ($ar, 1);
238 $self->{attr_edit_win}->set_title ("gcrossedit - edit " . $ar->longname);
239 }
240
241 sub update_stack_view {
242 my ($self, $mapedit, $x, $y) = @_;
243
244 return unless $self->{sv};
245
246 $self->{sv}->set_stack ($mapedit, $x, $y);
247
248 }
249
250 sub open_pick_window {
251 my ($self, $layout) = @_;
252
253 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
254 # selection
255 my $p = GCE::PickWindow->new ();
256
257 push @{$self->{open_pick_windows}}, $p;
258
259 my $idx = (@{$self->{open_pick_windows}}) - 1;
260
261 $p->signal_connect ('delete-event' => sub {
262 $self->{open_pick_windows}->[$idx] = undef;
263 });
264
265 if ($layout) {
266 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
267 }
268
269 $p->show_all;
270
271 $p->set_selection ($layout->{selection});
272 }
273
274 sub add_recent {
275 my ($self, $entry) = @_;
276 our @recent_entries;
277
278 @recent_entries = grep { $_ ne $entry } @recent_entries;
279 unshift @recent_entries, $entry;
280 splice @recent_entries, 5;
281
282 open my $fh, ">$recentfile" or die "Can't write to file $recentfile: $!";
283 binmode $fh;
284 local $/;
285 print $fh (join "\0", @recent_entries);
286 close $fh;
287
288 $self->build_menu;
289 }
290
291 sub escape_filename {
292 my $str = shift;
293
294 $str = Glib::filename_to_unicode($str);
295
296 # escape to prevent Gtk2::SimpleMenu parsing these especially
297 $str =~ s/\//\\\//g;
298 $str =~ s/_/__/g;
299
300 $str;
301 }
302
303 sub recent {
304 my ($self) = @_;
305 my @recent;
306
307 our @recent_entries;
308
309 foreach my $entry(@recent_entries) {
310 push @recent, escape_filename($entry) => {
311 callback => sub { $self->open_map_editor($entry);
312 $self->build_menu }
313 };
314 }
315
316 push @recent, Empty => { callback => sub {} }
317 unless @recent;
318
319 @recent;
320 }
321
322 sub build_menu {
323 my ($self) = @_;
324
325 my $menu_tree = [
326 _File => {
327 item_type => '<Branch>',
328 children => [
329 _New => {
330 callback => sub { $self->new_cb },
331 accelerator => '<ctrl>N'
332 },
333 _Open => {
334 callback => sub { $self->open_cb },
335 accelerator => '<ctrl>O'
336 },
337 'Open special' => {
338 item_type => '<Branch>',
339 children => [
340 "world map at"=> {
341 callback => sub { $self->open_worldmap_cb },
342 },
343 "recent files"=> {
344 item_type => '<Branch>',
345 children => [ $self->recent ],
346 },
347 ]
348 },
349 "_Save Layout" => {
350 callback => sub { $self->save_layout },
351 accelerator => '<ctrl>L'
352 },
353 "_Preferences" => {
354 callback => sub { $self->show_editor_properties },
355 accelerator => "<ctrl>T"
356 },
357 _Quit => {
358 callback => sub { Gtk2->main_quit },
359 accelerator => '<ctrl>Q'
360 },
361 ]
362 },
363 _Dialogs => {
364 item_type => '<Branch>',
365 children => [
366 "_Picker" => {
367 callback => sub { $self->open_pick_window },
368 accelerator => "<ctrl>P"
369 },
370 "_Stack View" => {
371 callback => sub { $self->show_stack_view },
372 accelerator => "<ctrl>V"
373 },
374 "_Attributes" => {
375 callback => sub { $self->show_attr_editor },
376 accelerator => "<ctrl>A"
377 },
378 ]
379 },
380 _Help => {
381 item_type => '<Branch>',
382 children => [
383 _Manual => {
384 callback => sub { $self->show_help_window },
385 accelerator => "<ctrl>H"
386 },
387 ]
388 },
389 ];
390
391 my $men =
392 Gtk2::SimpleMenu->new (
393 menu_tree => $menu_tree,
394 default_callback => \&default_cb,
395 );
396
397 for ($self->{vb}->get_children) { # Rebuild menu
398 if ($_->isa ('Gtk2::MenuBar')) {
399 $_->hide;
400 $self->{vb}->remove($_);
401
402 $self->{vb}->pack_start ($men->{widget}, 0, 1, 0);
403 $self->{vb}->reorder_child ($men->{widget}, 0);
404 $self->{vb}->show_all;
405 }
406 }
407
408 $self->add_accel_group ($men->{accel_group});
409
410 return $men->{widget};
411 }
412
413 sub add_button {
414 my ($self, $table, $plcinfo, $lbl, $cb) = @_;
415
416 my ($lx, $ly) = @{$plcinfo->{next}};
417
418 unless ($lx < $plcinfo->{width}) {
419
420 $ly++;
421 $lx = 0;
422 }
423
424 $ly < $plcinfo->{height}
425 or die "too many buttons, make table bigger!";
426
427 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
428 $btn->signal_connect (clicked => $cb);
429
430 $plcinfo->{next} = [$lx + 1, $ly];
431 }
432
433 sub build_buttons {
434 my ($self) = @_;
435
436 my $tbl = Gtk2::Table->new (2, 4);
437 my $plcinfo = { width => 2, height => 4, next => [0, 0] };
438
439 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
440 $self->{edit_collection}{place} = GCE::EditAction::Place->new;
441 $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
442 $self->{edit_collection}{select} = GCE::EditAction::Select->new;
443 $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
444 $self->{edit_collection}{connect} = GCE::EditAction::Connect->new;
445 $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
446
447 $self->set_edit_tool ('pick');
448
449 $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') });
450 $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') });
451 $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') });
452 $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') });
453 $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') });
454 $self->add_button ($tbl, $plcinfo, "Connec_t", sub { $self->set_edit_tool ('connect') });
455 $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
456
457 return $tbl;
458 }
459
460 sub set_edit_tool {
461 my ($self, $name) = @_;
462
463 if ($name eq 'pick') {
464 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
465 } elsif ($name eq 'place') {
466 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
467 } elsif ($name eq 'erase') {
468 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
469 } elsif ($name eq 'select') {
470 $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
471 $self->{edit_collection}{select}->update_overlay;
472 } elsif ($name eq 'perl') {
473 $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
474 } elsif ($name eq 'connect') {
475 $self->update_edit_tool ($self->{edit_collection}{connect}, "Connect");;
476 } elsif ($name eq 'followexit') {
477 $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
478 }
479 }
480
481 sub update_edit_tool {
482 my ($self, $tool, $name) = @_;
483
484 for (values %{$self->{loaded_maps}}) {
485 $_->{map}->overlay ('selection')
486 }
487
488 $self->{edit_tool}->set_text ($name);
489 $self->{sel_editaction} = $tool;
490
491 my $widget = $tool->tool_widget;
492
493 for ($self->{edit_tool_cont}->get_children) {
494 $_->hide;
495 $self->{edit_tool_cont}->remove ($_);
496 }
497
498 $_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}});
499
500 defined $widget or return;
501
502 $self->{edit_tool_cont}->add ($widget);
503 $widget->show_all;
504 }
505
506 sub update_pick_view {
507 my ($self, $arch) = @_;
508
509 defined $arch->{_face}
510 or $arch = $Crossfire::ARCH{$arch->{_name}};
511
512 fill_pb_from_arch ($self->{pick_view_pb}, $arch);
513 $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
514
515 $self->{pick_view_btn}->set_label ($arch->{_name});
516 }
517
518 sub INIT_INSTANCE {
519 my ($self) = @_;
520
521 {
522 open my $fh, "<$recentfile";
523 binmode $fh;
524 local $/;
525 our @recent_entries = split /\0/, <$fh>;
526 close $fh;
527 }
528
529 $::MAINWIN = $self;
530
531 $self->set_title ("gcrossedit - toolbox");
532
533 $self->{edit_tool} = Gtk2::Label->new;
534 $self->{edit_tool_cont} = Gtk2::VBox->new;
535
536 $self->add (my $vb = $self->{vb} = Gtk2::VBox->new);
537 $vb->pack_start ($self->build_menu, 0, 1, 0);
538 $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
539
540 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
541 $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
542
543 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
544 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
545
546 # XXX:load $ARGV _cleanly_?
547 $self->open_map_editor ($_)
548 for @ARGV;
549
550 $self->signal_connect ('delete-event' => sub {
551 Gtk2->main_quit;
552 });
553
554 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
555
556
557 $self->show_attr_editor;
558 }
559
560 sub new_cb {
561 my ($self) = @_;
562
563 my $w = Gtk2::Window->new ('toplevel');
564 my $width = [width => 20];
565 my $height = [height => 20];
566 $w->add (my $tbl = Gtk2::Table->new (2, 3));
567 add_table_widget ($tbl, 0, $width, 'string');
568 add_table_widget ($tbl, 1, $height, 'string');
569 add_table_widget ($tbl, 2, 'new', 'button', sub {
570 if ($width->[1] > 0 and $height->[1] > 0) {
571 my $map = Crossfire::Map->new ($width->[1], $height->[1]);
572 $map->{info}->{width} = $width->[1];
573 $map->{info}->{height} = $height->[1];
574 $map->resize ($width->[1], $height->[1]);
575 $self->open_map_editor ($map);
576 }
577 $w->destroy;
578 1;
579 });
580 add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
581 $w->show_all;
582 }
583
584 sub new_filechooser {
585 my ($self, $title, $save, $filename) = @_;
586
587 $title ||= 'gcrossedit - open map';
588 my $fc = new Gtk2::FileChooserDialog (
589 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
590 );
591
592 my @shortcut_folders =
593 grep { $_ && ($_ ne '') && -e $_ } keys %{$self->{fc_last_folders}};
594
595 $fc->add_shortcut_folder ($_) for @shortcut_folders;
596
597 unless (grep { $::MAPDIR eq $_ } @shortcut_folders) {
598 $fc->add_shortcut_folder ($::MAPDIR)
599 if -d $::MAPDIR;
600 }
601
602 $fc->set_current_folder (getcwd);
603
604 if ($filename) {
605 $fc->set_filename ($filename);
606 }
607
608 $fc
609 }
610
611 sub new_coord_query {
612 my ($self, $finishcb) = @_;
613
614 my $coordhash = { x => 105, y => 115, worldmap => 1, overlay => 0 };
615 my $diag = GCE::HashDialogue->new;
616 $self->{"worldmap_coord_query"} = $diag;
617 $diag->signal_connect (destroy => sub {
618 delete $self->{"worldmap_coord_query"};
619 });
620 $diag->init (
621 layout_name => 'worldmap_coord_query',
622 info => "Open worldmap at ...",
623 dialog_default_size => [ 200, 200, 200, 0 ],
624 title => 'Worldmap coordinate entry',
625 ref_hash => $coordhash,
626 dialog => [
627 [x => 'X Coordinate' => 'spin', sub { (0, 999, 1) }],
628 [y => 'Y Coordinate' => 'spin', sub { (0, 999, 1) }],
629 [worldmap => 'Open worldmap' => 'check'],
630 [overlay => 'Open overlay (CF+)' => 'check'],
631 ],
632 save_button_label => 'open',
633 save_cb => sub {
634 $finishcb->($_[0]);
635 }
636 );
637 $diag->show_all;
638 }
639
640 sub open_worldmap_cb {
641 my ($self) = @_;
642
643 $self->new_coord_query (sub {
644 my ($info) = @_;
645 my ($x, $y) = ($info->{x}, $info->{y});
646 my ($worldmap, $overlay) = ($info->{worldmap}, $info->{overlay});
647 $self->open_map_editor ($::MAPDIR . "/world/world_$x\_$y")
648 if $worldmap;
649 $self->open_map_editor ($::MAPDIR . "/world-overlay/world_$x\_$y")
650 if $overlay;
651 });
652 }
653
654 sub open_cb {
655 my ($self) = @_;
656
657 my $fc = $self->new_filechooser;
658
659 if ('ok' eq $fc->run) {
660
661 $self->{fc_last_folder} = $fc->get_current_folder;
662 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
663
664 $self->open_map_editor ($fc->get_filename);
665 }
666
667 $fc->destroy;
668 }
669
670 sub get_pick {
671 my ($self) = @_;
672
673 $self->{attr_edit}
674 or die "Couldn't find attribute editor! SERIOUS BUG!";
675
676 # XXX: This is just to make sure that this function always returns something
677
678 my $ar = $self->{attr_edit}->get_arch;
679 return { _name => 'platinacoin' } unless defined $ar;
680 return $ar->getarch || { _name => 'platinacoin' };
681 }
682
683 =head1 AUTHOR
684
685 Marc Lehmann <schmorp@schmorp.de>
686 http://home.schmorp.de/
687
688 Robin Redeker <elmex@ta-sa.org>
689 http://www.ta-sa.org/
690
691 =cut
692 1;
693