ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/HashDialog.pm
Revision: 1.2
Committed: Tue Nov 28 16:26:22 2006 UTC (17 years, 6 months ago) by elmex
Branch: MAIN
Changes since 1.1: +18 -7 lines
Log Message:
removed some debugging output.
and some changes on HashDialog.

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 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 =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 'check') {
73 $table->attach_defaults (Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
74 $table->attach_defaults (my $chk = Gtk2::CheckButton->new, 1, 2, $idx, $idx + 1);
75 $chk->set_active ($self->{edit_hash}{$key});
76 $chk->signal_connect (toggled => sub {
77 my ($lbl) = @_;
78 $self->{edit_hash}{$key} = $lbl->get_active * 1;
79 });
80
81 } elsif ($type eq 'sep') {
82 $table->attach_defaults (Gtk2::HSeparator->new, 0, 2, $idx, $idx + 1);
83
84 } else {
85 die "Unrecognized dialogue type: $type\n";
86 }
87
88 $self->{table_idx}++;
89 }
90
91 sub save {
92 my ($self) = @_;
93
94 if ($self->{save_cb}) {
95 $self->{save_cb}->($self->{edit_hash});
96 }
97
98 for (@{$self->{hash_keys}}) {
99 $self->{ref_hash}->{$_} = $self->{edit_hash}->{$_};
100 }
101 }
102
103 sub reset {
104 my ($self) = @_;
105
106 for (@{$self->{hash_keys}}) {
107 $self->{edit_hash}->{$_} = $self->{ref_hash}->{$_};
108 }
109 }
110
111 sub build_table {
112 my ($self) = @_;
113 $self->{table}->remove ($_) for $self->{table}->get_children;
114
115 $self->{table_idx} = 0;
116
117 for (@{$self->{dialog}}) {
118 $self->_add_prop_entry (@$_);
119 }
120 }
121
122 sub init {
123 my ($self, %args) = @_;
124 for (keys %args) { $self->{$_} = $args{$_} }
125
126 $self->set_title ("gcrossedit - $self->{title}");
127
128 ::set_pos_and_size ($self, $::CFG->{$self->{layout_name}}, @{$self->{dialog_default_size}});
129
130 my $cfg_lines = scalar @{$self->{dialog}};
131
132 for (@{$self->{dialog}}) {
133 push @{$self->{hash_keys}}, $_->[0] if defined $_->[0];
134 }
135
136 $self->add (my $vb = Gtk2::VBox->new);
137 $vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
138 $sw->set_policy ('automatic', 'automatic');
139 $sw->add_with_viewport (my $v = Gtk2::VBox->new);
140 $v->pack_start ($self->{table} = Gtk2::Table->new (2, $cfg_lines), 0, 0, 0);
141 if ($self->{text_entry}) {
142 $vb->pack_start (Gtk2::Label->new ($self->{text_entry}->{label}), 0, 1, 0)
143 if $self->{text_entry}->{label};
144 $vb->pack_start (my $txt = Gtk2::TextView->new, 1, 1, 0);
145 # TODO/XXX: Implement hash-changes!
146 }
147 $vb->pack_start (my $hb = Gtk2::HBox->new (1, 0), 0, 0, 0);
148 $hb->pack_start (my $save_btn = Gtk2::Button->new ($self->{save_button_label} || "save"), 1, 1, 0);
149 $hb->pack_start (my $reset_btn = Gtk2::Button->new ("reset"), 1, 1, 0);
150 $hb->pack_start (my $close_btn = Gtk2::Button->new ("cancel"), 1, 1, 0);
151
152 $self->reset;
153
154 $self->build_table;
155
156 $save_btn->signal_connect (clicked => sub {
157 $self->save;
158 });
159
160 $reset_btn->signal_connect (clicked => sub {
161 $self->reset;
162 $self->build_table;
163 $self->show_all;
164 });
165
166 $close_btn->signal_connect (clicked => sub {
167 $self->destroy;
168 });
169 }
170
171 sub INIT_INSTANCE {
172 my ($self) = @_;
173 }
174
175 1;