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 |
my $ls = $self->{liststore} = |
36 |
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 |
$sw->set_policy ('automatic', 'automatic'); |
48 |
$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 |
$self->{liststore}->clear; |
120 |
|
121 |
# these are all attribute keys that will be _displayed_ |
122 |
my @attrib_keys = |
123 |
grep { |
124 |
# filter out msg, lore and inventory they are special and/or multiline! |
125 |
$_ ne 'msg' |
126 |
and $_ ne 'lore' |
127 |
and $_ ne 'inventory' |
128 |
and ($self->{raw} or $_ ne '_name') # remove _name only when not raw |
129 |
} ( |
130 |
keys %$arch, # include all defined attribute keys |
131 |
grep { |
132 |
# we exclude the interesting attributes from |
133 |
# the Typemap when raw is enabled |
134 |
|
135 |
!$self->{raw} and not exists $self->{arch}->{$_} |
136 |
|
137 |
} GCE::AttrTypemap::interesting_fields ($arch) |
138 |
); |
139 |
|
140 |
# construct the sorted list of displayed attributes |
141 |
my @list = |
142 |
sort { |
143 |
|
144 |
$a->[0] cmp $b->[0] |
145 |
|
146 |
} map { [$_, $arch->{$_}] } @attrib_keys; |
147 |
|
148 |
for (@list) { |
149 |
|
150 |
my $it = $self->{liststore}->append; # append |
151 |
|
152 |
# get the attribute display name |
153 |
my $attrname = |
154 |
$self->{raw} |
155 |
? $_->[0] |
156 |
: $self->get_attr_name ($arch, $_->[0]); |
157 |
|
158 |
$self->{liststore}->set ( |
159 |
$it, |
160 |
0 => $attrname, |
161 |
1 => $_->[1], |
162 |
2 => $_->[2] |
163 |
); |
164 |
} |
165 |
|
166 |
# XXX: How do i update the model correctly? |
167 |
$self->{tv}->set_model ($self->{liststore}); |
168 |
|
169 |
$self->{attr_list} = \@list; |
170 |
} |
171 |
|
172 |
sub set_attr { |
173 |
my ($self, $key, $value) = @_; |
174 |
|
175 |
my $attr = $self->{arch}->{$key}; |
176 |
|
177 |
unless (ref $attr) { |
178 |
|
179 |
$self->call_arch_cb ($self->{arch}, $key, $value); |
180 |
} |
181 |
} |
182 |
|
183 |
sub set_arch_cb { |
184 |
my ($self, $cb) = @_; |
185 |
|
186 |
$self->{arch_cb} = $cb; |
187 |
} |
188 |
|
189 |
sub call_arch_cb { |
190 |
my ($self, $arch, $key, $val) = @_; |
191 |
|
192 |
return unless $self->{arch_cb}; |
193 |
|
194 |
$self->{arch_cb}->($arch, $key, $val); |
195 |
} |
196 |
|
197 |
|
198 |
=head1 AUTHOR |
199 |
|
200 |
Marc Lehmann <schmorp@schmorp.de> |
201 |
http://home.schmorp.de/ |
202 |
|
203 |
Robin Redeker <elmex@ta-sa.org> |
204 |
http://www.ta-sa.org/ |
205 |
|
206 |
=cut |
207 |
1; |