ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/MainWindow.pm
Revision: 1.39
Committed: Thu Mar 16 21:18:54 2006 UTC (20 years, 7 months ago) by elmex
Branch: MAIN
Changes since 1.38: +4 -2 lines
Log Message:
fixed a bug

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