ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttachEditor.pm
Revision: 1.2
Committed: Mon Aug 28 18:24:49 2006 UTC (17 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.1: +120 -39 lines
Log Message:
finished attachment editor mostly, only multiline editing is still missing :(

File Contents

# User Rev Content
1 elmex 1.1 package GCE::AttachEditor;
2    
3     =head1 NAME
4    
5     GCE::AttachEditor - this is a editor for attachments for maps and objects
6    
7     =cut
8    
9     use Gtk2;
10     use Gtk2::Gdk::Keysyms;
11     use Gtk2::SimpleMenu;
12    
13     use Crossfire;
14    
15     use GCE::Util;
16    
17     use Glib::Object::Subclass Gtk2::Window;
18    
19     use Storable qw/dclone/;
20    
21     use strict;
22    
23 elmex 1.2 sub try_add_known_attachment {
24     my ($self, $att) = @_;
25    
26     unless ($::CFG->{known_attachments}->{$att}) {
27     $self->{attach_combobox}->append_text ($att);
28     $::CFG->{known_attachments}->{$att} = 1;
29     }
30     }
31    
32     #sub known_attachments {
33     # my ($self) = @_;
34     #
35     # my $model = $self->{attach_combobox}->get_model;
36     # my @known_attachments;
37     #
38     # my $iter = $model->get_iter_first;
39     # while ($iter) {
40     # push @known_attachments, $model->get ($iter);
41     # $iter = $model->iter_next ($iter);
42     # }
43     #
44     # return @known_attachments;
45     #}
46    
47 elmex 1.1 sub save_layout {
48     my ($self) = @_;
49     $::CFG->{attach_editor} = ::get_pos_and_size ($self);
50     }
51    
52     sub INIT_INSTANCE {
53     my ($self) = @_;
54     $self->set_title ("gcrossedit - attachment editor");
55 elmex 1.2 ::set_pos_and_size ($self, $::CFG->{attach_editor}, 400, 400, 200, 0);
56    
57     $self->add (my $vb = Gtk2::VBox->new);
58     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 1, 0);
59     $hb->pack_start ($self->{attach_combobox} = Gtk2::ComboBoxEntry->new_text, 0, 1, 0);
60     $hb->pack_start (my $btn = Gtk2::Button->new ("new"), 0, 1, 0);
61     $btn->signal_connect (clicked => sub {
62     my $ent = $self->{attach_combobox}->child->get_text;
63     $self->add_attachment ($ent);
64     });
65    
66     $vb->pack_start ($self->{attachbox} = Gtk2::VBox->new, 1, 1, 0);
67    
68     $self->{attach_combobox}->append_text ($_)
69     for keys %{$::CFG->{known_attachments} || {}};
70     }
71    
72     sub add_attachment {
73     my ($self, $name, $args) = @_;
74    
75     return if $name =~ /^\s*$/;
76    
77     $self->try_add_known_attachment ($name);
78     push @{$self->{attach}}, [$name, $args];
79     $self->{ntbook}->append_page (my $pg = $self->make_page ($name, $args), $name);
80     $pg->show_all;
81    
82     $self->{upd_cb}->($self->{attach});
83 elmex 1.1 }
84    
85     sub update_attachments {
86     my ($self, $name, $args) = @_;
87 elmex 1.2
88 elmex 1.1 for (@{$self->{attach}}) {
89     if ($_->[0] eq $name) {
90     @$_ = ($name, $args);
91     $name = undef;
92     last;
93     }
94     }
95    
96 elmex 1.2 $name and warn "Couldn't find attachment '$name', this is a bug!";
97 elmex 1.1
98     $self->{upd_cb}->($self->{attach});
99     }
100    
101     sub kill_attachment {
102     my ($self, $pos) = @_;
103     my $name = $self->{ntbook}->get_tab_label_text ($self->{ntbook}->get_nth_page ($pos));
104     $self->{ntbook}->remove_page ($pos);
105     @{$self->{attach}} = grep { $_->[0] ne $name } @{$self->{attach}};
106     $self->{upd_cb}->($self->{attach});
107     }
108    
109     sub set_attachment {
110     my ($self, $attach, $update_cb) = @_;
111     $self->{upd_cb} = $update_cb
112     or die "No update callback given!";
113    
114     my $ntbook = $self->{ntbook} = Gtk2::Notebook->new;
115     $ntbook->set_scrollable (1);
116 elmex 1.2 $self->add_attachment (@$_) for @$attach;
117    
118     $self->{attachbox}->remove ($_) for $self->{attachbox}->get_children;
119     $self->{attachbox}->add ($ntbook);
120     $self->{attachbox}->show_all;
121     }
122    
123     sub add_attachment_key {
124     my ($self, $name, $key, $args) = @_;
125    
126     my $tbl = $self->{table};
127     my $i = $tbl->get ('n-rows');
128     $tbl->resize ($i + 1, $self->{table}->get ('n-columns'));
129    
130     $args->{$key} = "" unless defined $args->{$key};
131    
132     $tbl->attach (my $ed = Gtk2::Entry->new, 0, 1, $i, $i + 1, ['fill'], 'fill', 0, 0);
133     $ed->set_editable (0);
134     $ed->set_text ($key);
135     # $ed->signal_connect (changed => sub {
136     # my ($ed) = @_;
137     # my $newkey = $ed->get_text;
138     # my $val = delete $args->{$ed->{key}};
139     # $args->{$ed->{key} = $newkey} = $val;
140     # $self->update_attachments ($name, $args);
141     # 0
142     # });
143    
144     $tbl->attach (my $ed2 = Gtk2::Entry->new, 1, 2, $i, $i + 1, ['fill', 'expand'], 'fill', 0, 0);
145     $ed2->set_text ($ed2->{value} = $args->{$key});
146     $ed2->signal_connect (changed => sub {
147     my ($ed2) = @_;
148     $args->{$key} = $ed2->get_text;
149     $self->update_attachments ($name, $args);
150     0
151     });
152    
153     $tbl->attach (my $b = Gtk2::Button->new ("X"), 2, 3, $i, $i + 1, 'fill', 'fill', 0, 0);
154     $b->signal_connect (clicked => sub {
155     delete $args->{$key};
156     $self->rebuild_key_table ($name, $args);
157     0
158     });
159 elmex 1.1
160 elmex 1.2 $tbl->show_all;
161     }
162 elmex 1.1
163 elmex 1.2 sub rebuild_key_table {
164     my ($self, $name, $args) = @_;
165     my $tbl = $self->{table};
166     $tbl->resize (scalar keys %$args, $tbl->get ('n-columns'));
167     $tbl->remove ($_) for $tbl->get_children;
168     $self->add_attachment_key ($name, $_, $args) for keys %$args;
169 elmex 1.1 }
170    
171 elmex 1.2
172 elmex 1.1 sub make_page {
173     my ($self, $name, $args) = @_;
174    
175     my $vb = Gtk2::VBox->new;
176 elmex 1.2
177     $vb->pack_start (my $kb = Gtk2::Button->new ("remove attachment"), 0, 1, 0);
178 elmex 1.1 $kb->signal_connect (clicked => sub {
179     $self->kill_attachment ($self->{ntbook}->page_num ($vb));
180     1
181     });
182    
183 elmex 1.2 $vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
184     $sw->set_policy (qw/automatic automatic/);
185     $sw->add_with_viewport ($self->{table} = Gtk2::Table->new (1, 3));
186    
187     $self->add_attachment_key ($name, $_, $args) for keys %$args;
188    
189     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 1, 0);
190     $hb->pack_start (my $ed = Gtk2::Entry->new, 1, 1, 0);
191     $hb->pack_start (my $kb = Gtk2::Button->new ("new key"), 0, 1, 0);
192     $kb->signal_connect (clicked => sub {
193 elmex 1.1 my $newkey = $ed->get_text;
194 elmex 1.2 return 0 if exists $args->{$newkey};
195 elmex 1.1 $self->update_attachments ($name, $args);
196 elmex 1.2 $self->add_attachment_key ($name, $newkey, $args);
197 elmex 1.1 0
198     });
199    
200    
201     $vb
202     }
203    
204     =head1 AUTHOR
205    
206     Robin Redeker <elmex@ta-sa.org>
207     http://www.ta-sa.org/
208    
209     =cut
210     1;
211