ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrList.pm
Revision: 1.7
Committed: Tue Dec 5 15:00:32 2006 UTC (17 years, 6 months ago) by elmex
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +0 -0 lines
State: FILE REMOVED
Log Message:
Some cleanup of old unused files and a bugfix in layout saving when no
map window was open yet.

File Contents

# User Rev Content
1 elmex 1.1 package GCE::AttrList;
2    
3     =head1 NAME
4    
5     GCE::AttrList - an list widgt for attributes
6    
7     =cut
8    
9     use Gtk2;
10     use Gtk2::Gdk::Keysyms;
11     use Gtk2::SimpleList;
12    
13     use Glib::Object::Subclass
14     Gtk2::VBox,
15     properties => [
16     Glib::ParamSpec->boolean (
17     "editable", "Editable flag",
18     "a boolean whether this list is editable",
19     1, [qw/writable readable/]
20     ),
21     Glib::ParamSpec->boolean (
22     "raw", "Raw display",
23     "a boolean whether this list is intelligent: takes the arch attrs from AttrTypemap",
24     1, [qw/writable readable/]
25     )
26    
27     ];
28    
29     sub INIT_INSTANCE {
30     my ($self) = @_;
31    
32     $self->{raw} = 1;
33     $self->{editable} = 1;
34    
35 elmex 1.2 my $ls = $self->{liststore} =
36 elmex 1.1 Gtk2::ListStore->new (
37     'Glib::String', 'Glib::String', 'Glib::String'
38     );
39    
40     my $tv = $self->{tv} = Gtk2::TreeView->new ($ls);
41     $tv->set_rules_hint (TRUE);
42    
43     $self->add_columns ($tv);
44    
45     $self->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
46     $sw->set_shadow_type ('etched-in');
47 elmex 1.2 $sw->set_policy ('automatic', 'automatic');
48 elmex 1.1 $sw->add ($tv);
49     }
50    
51     sub SET_PROPERTY {
52     my ($self, $pspec, $newval) = @_;
53    
54     $pspec = $pspec->get_name;
55    
56     if ($pspec eq 'editable') {
57    
58     if ($newval) {
59    
60     $self->{edit_column}->set (editable => 1);
61    
62     } else {
63    
64     $self->{edit_column}->set (editable => 0);
65     }
66    
67     } elsif ($pspec eq 'raw') {
68    
69     $self->{raw} = $newval;
70     }
71     }
72    
73     sub get_attr_name {
74     my ($self, $arch, $key) = @_;
75    
76     return GCE::AttrTypemap::get_attr_alias ($arch, $key);
77     }
78    
79     sub add_columns {
80     my ($self, $tv) = @_;
81    
82     my $model = $tv->get_model;
83    
84     my $col =
85     Gtk2::TreeViewColumn->new_with_attributes (
86     "Key", Gtk2::CellRendererText->new, text => 0
87     );
88     $col->set_sizing ('autosize');
89    
90     $tv->append_column ($col);
91    
92     $col =
93     Gtk2::TreeViewColumn->new_with_attributes (
94     "Value", my $txtr = $self->{edit_column} = Gtk2::CellRendererText->new, text => 1
95     );
96     $col->set_sizing ('autosize');
97    
98     # XXX: at this time the editable field isn't initalized, find fix?
99     if ($self->{editable}) {
100    
101     $txtr->set (editable => 1);
102     }
103    
104     $txtr->signal_connect (
105     edited => sub {
106     my $key = $self->{attr_list}->[$_[1]]->[0];
107     $self->set_attr ($key, $_[2]);
108     }
109     );
110    
111     $tv->append_column ($col);
112     }
113    
114     sub set_arch {
115     my ($self, $arch) = @_;
116    
117     $self->{arch} = $arch;
118    
119 elmex 1.6 my $ar = Crossfire::arch_attr $arch;
120     warn "FO:" . Data::Dumper::Dumper ($ar) . ">\n";
121    
122 elmex 1.2 $self->{liststore}->clear;
123 elmex 1.1
124 elmex 1.2 # these are all attribute keys that will be _displayed_
125     my @attrib_keys =
126     grep {
127 elmex 1.4 # filter out msg, lore and inventory they are special and/or multiline!
128 elmex 1.3 $_ ne 'msg'
129     and $_ ne 'lore'
130 elmex 1.4 and $_ ne 'inventory'
131 elmex 1.5 and $_ ne '_virtual'
132     and $_ ne '_virtual_x'
133     and $_ ne '_virtual_y'
134     and $_ ne '_more'
135     and $_ ne '_face'
136 elmex 1.3 and ($self->{raw} or $_ ne '_name') # remove _name only when not raw
137 elmex 1.2 } (
138     keys %$arch, # include all defined attribute keys
139     grep {
140     # we exclude the interesting attributes from
141     # the Typemap when raw is enabled
142 elmex 1.1
143 elmex 1.2 !$self->{raw} and not exists $self->{arch}->{$_}
144 elmex 1.1
145 elmex 1.2 } GCE::AttrTypemap::interesting_fields ($arch)
146     );
147 elmex 1.1
148 elmex 1.2 # construct the sorted list of displayed attributes
149     my @list =
150     sort {
151     $a->[0] cmp $b->[0]
152 elmex 1.4 } map { [$_, $arch->{$_}] } @attrib_keys;
153 elmex 1.1
154     for (@list) {
155    
156 elmex 1.2 my $it = $self->{liststore}->append; # append
157 elmex 1.1
158 elmex 1.2 # get the attribute display name
159 elmex 1.1 my $attrname =
160     $self->{raw}
161     ? $_->[0]
162     : $self->get_attr_name ($arch, $_->[0]);
163    
164 elmex 1.2 $self->{liststore}->set (
165     $it,
166     0 => $attrname,
167     1 => $_->[1],
168     2 => $_->[2]
169     );
170 elmex 1.1 }
171    
172 elmex 1.2 # XXX: How do i update the model correctly?
173     $self->{tv}->set_model ($self->{liststore});
174 elmex 1.1
175     $self->{attr_list} = \@list;
176     }
177    
178     sub set_attr {
179     my ($self, $key, $value) = @_;
180    
181     my $attr = $self->{arch}->{$key};
182    
183     unless (ref $attr) {
184    
185     $self->call_arch_cb ($self->{arch}, $key, $value);
186     }
187     }
188    
189     sub set_arch_cb {
190     my ($self, $cb) = @_;
191 elmex 1.4
192 elmex 1.1 $self->{arch_cb} = $cb;
193     }
194    
195     sub call_arch_cb {
196     my ($self, $arch, $key, $val) = @_;
197    
198     return unless $self->{arch_cb};
199    
200     $self->{arch_cb}->($arch, $key, $val);
201     }
202    
203    
204     =head1 AUTHOR
205    
206     Marc Lehmann <schmorp@schmorp.de>
207     http://home.schmorp.de/
208    
209     Robin Redeker <elmex@ta-sa.org>
210     http://www.ta-sa.org/
211    
212     =cut
213     1;