ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/MainWindow.pm
Revision: 1.24
Committed: Tue Feb 21 18:06:35 2006 UTC (20 years, 8 months ago) by root
Branch: MAIN
Changes since 1.23: +3 -43 lines
Log Message:
*** empty log message ***

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     use Crossfire::MapWidget;
15    
16 elmex 1.2 use GCE::AttrEdit;
17 elmex 1.13 use GCE::MapEditor;
18 elmex 1.15 use GCE::StackView;
19 elmex 1.17 use GCE::EditAction;
20     use GCE::PickWindow;
21 elmex 1.2
22 elmex 1.14 use GCE::AttrTypemap;
23    
24 elmex 1.2 use Glib::Object::Subclass
25 elmex 1.13 Gtk2::Window;
26 elmex 1.2
27     use strict;
28    
29 elmex 1.18 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
30 elmex 1.17 sub save_layout {
31     my ($self) = @_;
32    
33     $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0;
34     $main::CFG->{stack_view_on} = exists $self->{sv} ? 1 : 0;
35     $main::CFG->{picker_on} = exists $self->{last_pick_window} ? 1 : 0;
36     $main::CFG->{main_window} = main::get_pos_and_size ($self);
37     $main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window}) if $self->{last_map_window};
38     $main::CFG->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win};
39     $main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win};
40    
41     $main::CFG->{open_pickers} = [];
42    
43     for (@{$self->{open_pick_windows}}) {
44    
45     next unless defined $_;
46    
47     push @{$main::CFG->{open_pickers}}, {
48     p_and_s => main::get_pos_and_size ($_),
49     selection => $_->{last_selection}
50     };
51     }
52    
53     main::write_cfg ($main::VARDIR . '/config');
54     }
55    
56     sub load_layout {
57     my ($self) = @_;
58    
59     $main::CFG->{attr_edit_on}
60     and $self->show_attr_editor;
61    
62     $main::CFG->{stack_view_on}
63     and $self->show_stack_view;
64    
65     for (@{$main::CFG->{open_pickers}}) {
66     $self->open_pick_window ($_);
67     }
68     }
69    
70     sub open_map_editor {
71     my ($self, $mapfile) = @_;
72    
73     # XXX: last_map_window is a dirty trick to get the position and size
74     # for save layout
75    
76     my $w = $self->{last_map_window} = GCE::MapEditor->new;
77     $w->open_map ($mapfile);
78    
79     main::set_pos_and_size ($w, $main::CFG->{map_window});
80    
81     $w->show_all;
82     # my $w = $self->{last_map_window} = Gtk2::Window->new ('toplevel');
83     # $w->set_title ('gce - map editor');
84     # $w->add (my $mapedit = $self->{mapedit} = new GCE::MapEditor);
85     # $mapedit->open_map ($mapfile);
86     #
87     #
88     # $w->show_all;
89     }
90    
91     sub show_stack_view {
92 elmex 1.15 my ($self) = @_;
93    
94     return if defined $self->{sv};
95    
96 elmex 1.17 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
97 elmex 1.15 $w->set_title ('gce - stack view');
98 root 1.23 $w->signal_connect (delete_event => sub { delete $self->{sv}; 0 });
99 elmex 1.15 $w->add ($self->{sv} = GCE::StackView->new);
100 elmex 1.17
101     main::set_pos_and_size ($w, $main::CFG->{stack_view});
102    
103 elmex 1.15 $w->show_all;
104     }
105    
106     sub show_attr_editor {
107     my ($self) = @_;
108    
109 elmex 1.17 return if $self->{attr_edit};
110    
111 elmex 1.15 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
112     $w->set_title ("gce - edit attrs");
113     $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
114 root 1.23 $w->signal_connect (delete_event => sub { delete $self->{attr_edit}; 0 });
115 elmex 1.17
116     main::set_pos_and_size ($w, $main::CFG->{attr_view});
117    
118 elmex 1.15 $w->show_all;
119     }
120    
121     sub update_attr_editor {
122 elmex 1.17 my ($self, $arch, $cb) = @_;
123 elmex 1.15
124     return unless $self->{attr_edit};
125    
126 elmex 1.17 $self->{attr_edit}->set_arch ($arch, $cb);
127 elmex 1.15 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
128     }
129    
130     sub update_stack_view {
131 elmex 1.17 my ($self, $mapedit, $x, $y) = @_;
132 elmex 1.15
133     return unless $self->{sv};
134    
135 elmex 1.17 $self->{sv}->set_stack ($mapedit, $x, $y);
136 elmex 1.15 }
137    
138 elmex 1.17 sub open_pick_window {
139     my ($self, $layout) = @_;
140    
141     # XXX: Yes, also fix this, save _every_ pick window and their posistions and their
142     # selection
143     my $p = GCE::PickWindow->new ();
144 elmex 1.15
145 elmex 1.17 push @{$self->{open_pick_windows}}, $p;
146    
147     my $idx = (@{$self->{open_pick_windows}}) - 1;
148 elmex 1.15
149 elmex 1.17 $p->signal_connect ('delete-event' => sub {
150     $self->{open_pick_windows}->[$idx] = undef;
151     });
152 elmex 1.10
153 elmex 1.17 if ($layout) {
154     main::set_pos_and_size ($p, $layout->{p_and_s});
155     }
156 elmex 1.10
157 elmex 1.17 $p->show_all;
158 elmex 1.10
159 elmex 1.17 $p->set_selection ($layout->{selection});
160 elmex 1.10 }
161    
162 elmex 1.13 sub build_menu {
163 elmex 1.1 my ($self) = @_;
164    
165 elmex 1.2 my $menu_tree = [
166     _File => {
167     item_type => '<Branch>',
168     children => [
169     _New => {
170     callback => sub { $self->new_cb },
171     accelerator => '<ctrl>N'
172     },
173     _Open => {
174     callback => sub { $self->open_cb },
175     accelerator => '<ctrl>O'
176     },
177 elmex 1.17 "_Save Layout" => {
178     callback => sub { $self->save_layout },
179     accelerator => '<ctrl>L'
180     },
181 elmex 1.2 _Quit => {
182     callback => sub { Gtk2->main_quit },
183     accelerator => '<ctrl>Q'
184     }
185     ]
186     },
187 root 1.24 _Dialogs => {
188 elmex 1.2 item_type => '<Branch>',
189     children => [
190 elmex 1.17 "_Attr Editor" => {
191     callback => sub { $self->show_attr_editor },
192     accelerator => "<ctrl>A"
193 elmex 1.2 },
194 elmex 1.17 "_Picker" => {
195     callback => sub { $self->open_pick_window },
196     accelerator => "<ctrl>P"
197     },
198     "_Stack View" => {
199     callback => sub { $self->show_stack_view },
200     accelerator => "<ctrl>V"
201     },
202 elmex 1.15 "Open _Attr Editor" => {
203     callback => sub { $self->show_attr_editor },
204     accelerator => "<ctrl>A"
205     },
206 elmex 1.9 "Open _Picker" => {
207 elmex 1.13 callback => sub { $self->open_pick_window },
208     accelerator => "<ctrl>P"
209 elmex 1.9 },
210 elmex 1.15 "Open _Stack View" => {
211 elmex 1.17 callback => sub { $self->show_stack_view },
212 elmex 1.15 accelerator => "<ctrl>V"
213     },
214    
215 elmex 1.2 ]
216 elmex 1.9 }
217 elmex 1.17
218 elmex 1.2 ];
219    
220 elmex 1.13 my $men =
221     Gtk2::SimpleMenu->new (
222     menu_tree => $menu_tree,
223     default_callback => \&default_cb,
224     );
225 elmex 1.10
226 elmex 1.2 $self->add_accel_group ($men->{accel_group});
227    
228 elmex 1.13 return $men->{widget};
229     }
230 elmex 1.10
231 elmex 1.17 sub add_button {
232     my ($self, $table, $plcinfo, $lbl, $cb) = @_;
233    
234     my ($lx, $ly) = @{$plcinfo->{next}};
235    
236     unless ($lx < $plcinfo->{width}) {
237    
238     $ly++;
239     $lx = 0;
240     }
241    
242     $ly < $plcinfo->{height}
243     or die "too many buttons, make table bigger!";
244    
245     $table->attach_defaults (my $btn = Gtk2::Button->new_with_label ($lbl), $lx, $lx + 1, $ly, $ly + 1);
246     $btn->signal_connect (clicked => $cb);
247    
248     $plcinfo->{next} = [$lx + 1, $ly];
249     }
250    
251     sub build_buttons {
252     my ($self) = @_;
253    
254     my $tbl = Gtk2::Table->new (2, 2);
255     my $plcinfo = { width => 1, height => 3, next => [0, 0] };
256    
257     $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
258     $self->{edit_collection}{place} = GCE::EditAction::Place->new;
259     $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
260    
261     $self->add_button ($tbl, $plcinfo, "Pick", sub {
262     $self->{sel_editaction} = $self->{edit_collection}{pick};
263     $self->{edit_tool}->set_text ("Pick");
264     });
265     $self->add_button ($tbl, $plcinfo, "Place", sub {
266     $self->{sel_editaction} = $self->{edit_collection}{place};
267     $self->{edit_tool}->set_text ("Place");
268     });
269     $self->add_button ($tbl, $plcinfo, "Erase", sub {
270     $self->{sel_editaction} = $self->{edit_collection}{erase};
271     $self->{edit_tool}->set_text ("Erase");
272     });
273    
274     return $tbl;
275     }
276 elmex 1.10
277 root 1.11
278 elmex 1.13 sub INIT_INSTANCE {
279     my ($self) = @_;
280 elmex 1.10
281 root 1.24 $::MAINWIN = $self;
282 elmex 1.15
283 elmex 1.13 $self->set_title ("gce - main window");
284 elmex 1.10
285 elmex 1.13 $self->add (my $vb = Gtk2::VBox->new);
286     $vb->pack_start ($self->build_menu, 0, 1, 0);
287 elmex 1.17 $vb->pack_start (my $tbl = $self->build_buttons, 1, 1, 0);
288     $vb->pack_start ($self->{edit_tool} = Gtk2::Label->new, 0, 1, 0);
289     $vb->pack_start ($self->{pick_view} = Gtk2::Label->new, 0, 1, 0);
290 elmex 1.10
291 elmex 1.13 # XXX:load $ARGV _cleanly_?
292 elmex 1.17 $self->open_map_editor ($ARGV[0] || "$Crossfire::LIB/maps/dragonisland/advguild3");
293 root 1.11
294 elmex 1.13 $self->signal_connect ('delete-event' => sub {
295     Gtk2->main_quit;
296 elmex 1.10 });
297 elmex 1.17
298 root 1.24 ::set_pos_and_size ($self, $main::CFG->{main_window});
299 elmex 1.2 }
300    
301     sub new_cb {
302     my ($self) = @_;
303     die "NOT IMPLEMENTED YET";
304     }
305    
306     sub open_cb {
307     my ($self) = @_;
308    
309 elmex 1.13 my $fc =
310     Gtk2::FileChooserDialog->new (
311     'gce - open map', undef, 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
312     );
313    
314 elmex 1.2 $fc->add_shortcut_folder ("$Crossfire::LIB/maps");
315 elmex 1.13 $fc->add_shortcut_folder ($_) for keys %{$self->{fc_last_folders}};
316 elmex 1.10 $fc->set_current_folder ($self->{fc_last_folder} || "$Crossfire::LIB/maps");
317 elmex 1.2
318     if ('ok' eq $fc->run) {
319 elmex 1.13
320 elmex 1.10 $self->{fc_last_folder} = $fc->get_current_folder;
321 elmex 1.13 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
322 elmex 1.17
323     $self->open_map_editor ($fc->get_filename);
324 elmex 1.2 }
325    
326     $fc->destroy;
327     }
328    
329 elmex 1.17 sub set_pick {
330     my ($self, $arch) = @_;
331    
332     $self->{pick_arch} = $arch;
333     $self->{pick_view}->set_text ($arch->{_name});
334     }
335    
336     sub get_pick {
337     my ($self) = @_;
338    
339     # XXX: This is just to make sure that this function always returns something
340     return $self->{pick_arch} || { _name => 'platinacoin' };
341     }
342    
343 elmex 1.1 =head1 AUTHOR
344    
345     Marc Lehmann <schmorp@schmorp.de>
346     http://home.schmorp.de/
347    
348     Robin Redeker <elmex@ta-sa.org>
349     http://www.ta-sa.org/
350    
351     =cut
352     1;
353