ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/HashDialog.pm
Revision: 1.7
Committed: Thu Dec 27 22:28:01 2007 UTC (16 years, 4 months ago) by root
Branch: MAIN
CVS Tags: pre_cursor_branch, HEAD
Branch point for: cursor
Changes since 1.6: +1 -1 lines
Log Message:
upgrade Crossfire to Deliantra

File Contents

# Content
1 package GCE::HashDialogue;
2
3 =head1 NAME
4
5 GCE::HashDialogue - this is a editor for hashes (eg. for properties and meta info)
6
7 =cut
8
9 use Gtk2;
10 use Gtk2::Gdk::Keysyms;
11 use Gtk2::SimpleMenu;
12
13 use Deliantra;
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 =head1 SYNOPSIS
24
25 my $to_edit_hash = { test => 1, blah => "foo" },
26
27 my $diag = GCE::HashDialogue->new ();
28 $diag->init (
29 layout_name => 'cool_dialog_cfg_layout',
30 title => 'Cool dialog!',
31 ref_hash => $to_edit_hash,
32 dialog => [
33 [qw/blah Blah string/],
34 [undef, qw/x sep/],
35 [qw/test Test check/],
36 ],
37 );
38
39 =cut
40
41 sub save_layout {
42 my ($self) = @_;
43 $::CFG->{$self->{layout_name}} = ::get_pos_and_size ($self);
44 }
45
46 sub _add_prop_entry {
47 my ($self, $key, $desc, $type, $cb) = @_;
48
49 my $table = $self->{table};
50 my $idx = $self->{table_idx};
51
52 if ($type eq 'string' || $type eq 'password') {
53 $table->attach_defaults (Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
54 $table->attach_defaults (my $edwid = Gtk2::Entry->new, 1, 2, $idx, $idx + 1);
55 $edwid->set (visibility => 0) if $type eq 'password';
56 $edwid->set_text ($self->{edit_hash}{$key});
57 $edwid->signal_connect (changed => sub {
58 $self->{edit_hash}->{$key} = $_[0]->get_text;
59 });
60
61 } elsif ($type eq 'button') {
62 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($desc), 0, 2, $idx, $idx + 1);
63 $b->signal_connect (clicked => sub { $cb->(\$self->{edit_hash}{$key}) if $cb });
64
65 } elsif ($type eq 'label') {
66 $table->attach_defaults (Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
67 $table->attach_defaults (Gtk2::Label->new ($self->{edit_hash}{$key}), 1, 2, $idx, $idx + 1);
68
69 } elsif ($type eq 'desc') {
70 $table->attach_defaults (Gtk2::Label->new ($desc), 0, 2, $idx, $idx + 1);
71
72 } elsif ($type eq 'spin') {
73 $table->attach_defaults (Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
74 $table->attach_defaults (my $spn = Gtk2::SpinButton->new_with_range ($cb->()), 1, 2, $idx, $idx + 1);
75 $spn->set_value ($self->{edit_hash}{$key});
76 $spn->signal_connect (value_changed => sub {
77 my ($spn) = @_;
78 $self->{edit_hash}{$key} = $spn->get_value;
79 });
80
81 } elsif ($type eq 'check') {
82 $table->attach_defaults (Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
83 $table->attach_defaults (my $chk = Gtk2::CheckButton->new, 1, 2, $idx, $idx + 1);
84 $chk->set_active ($self->{edit_hash}{$key});
85 $chk->signal_connect (toggled => sub {
86 my ($lbl) = @_;
87 $self->{edit_hash}{$key} = $lbl->get_active ? 1 : undef;
88 });
89
90 } elsif ($type eq 'sep') {
91 $table->attach_defaults (Gtk2::HSeparator->new, 0, 2, $idx, $idx + 1);
92
93 } else {
94 die "Unrecognized dialogue type: $type\n";
95 }
96
97 $self->{table_idx}++;
98 }
99
100 sub save {
101 my ($self) = @_;
102
103 if ($self->{save_cb}) {
104 $self->{save_cb}->($self->{edit_hash});
105 }
106
107 for (@{$self->{hash_keys}}) {
108 if ((defined $self->{edit_hash}->{$_}) && $self->{edit_hash}->{$_} ne "") {
109 $self->{ref_hash}->{$_} = $self->{edit_hash}->{$_}
110 } else {
111 delete $self->{ref_hash}->{$_};
112 }
113 }
114 }
115
116 sub reset {
117 my ($self) = @_;
118
119 for (@{$self->{hash_keys}}) {
120 $self->{edit_hash}->{$_} = $self->{ref_hash}->{$_};
121 }
122 }
123
124 sub build_table {
125 my ($self) = @_;
126 $self->{table}->remove ($_) for $self->{table}->get_children;
127
128 $self->{table_idx} = 0;
129
130 for (@{$self->{dialog}}) {
131 $self->_add_prop_entry (@$_);
132 }
133 }
134
135 sub init {
136 my ($self, %args) = @_;
137 for (keys %args) { $self->{$_} = $args{$_} }
138
139 $self->set_title ("deliantra editor - $self->{title}");
140
141 ::set_pos_and_size ($self, $::CFG->{$self->{layout_name}}, @{$self->{dialog_default_size}});
142
143 my $cfg_lines = scalar @{$self->{dialog}};
144
145 for (@{$self->{dialog}}) {
146 push @{$self->{hash_keys}}, $_->[0] if defined $_->[0];
147 }
148
149 $self->add (my $vb = Gtk2::VBox->new);
150 $vb->pack_start (my $ilbl = Gtk2::Label->new ($self->{info}), 0, 1, 0)
151 if $self->{info};
152 $vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
153 $sw->set_policy ('automatic', 'automatic');
154 $sw->add_with_viewport (my $v = Gtk2::VBox->new);
155 $v->pack_start ($self->{table} = Gtk2::Table->new (2, $cfg_lines), 0, 0, 0);
156 if ($self->{text_entry}) {
157 $vb->pack_start (Gtk2::Label->new ($self->{text_entry}->{label}), 0, 1, 0)
158 if $self->{text_entry}->{label};
159 $vb->pack_start (my $txt = Gtk2::TextView->new, 1, 1, 0);
160 # TODO/XXX: Implement hash-changes!
161 }
162 $vb->pack_start (my $hb = Gtk2::HBox->new (1, 0), 0, 0, 0);
163 $hb->pack_start (my $save_btn = Gtk2::Button->new ($self->{save_button_label} || "save"), 1, 1, 0);
164 $hb->pack_start (my $reset_btn = Gtk2::Button->new ("reset"), 1, 1, 0);
165 $hb->pack_start (my $close_btn = Gtk2::Button->new ("cancel"), 1, 1, 0);
166
167 $self->reset;
168
169 $self->build_table;
170
171 $save_btn->signal_connect (clicked => sub {
172 $self->save;
173 $self->destroy if $self->{close_on_save};
174 });
175
176 $reset_btn->signal_connect (clicked => sub {
177 $self->reset;
178 $self->build_table;
179 $self->show_all;
180 });
181
182 $close_btn->signal_connect (clicked => sub {
183 $self->destroy;
184 });
185 }
186
187 sub INIT_INSTANCE {
188 my ($self) = @_;
189 }
190
191 1;