ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/HashDialog.pm
Revision: 1.4
Committed: Sat Jan 6 09:41:17 2007 UTC (17 years, 4 months ago) by elmex
Branch: MAIN
Changes since 1.3: +9 -0 lines
Log Message:
improved coordselection dialog

File Contents

# User Rev Content
1 elmex 1.1 package GCE::HashDialogue;
2    
3     =head1 NAME
4    
5 elmex 1.2 GCE::HashDialogue - this is a editor for hashes (eg. for properties and meta info)
6 elmex 1.1
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 elmex 1.2 if ($type eq 'string' || $type eq 'password') {
53 elmex 1.1 $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 elmex 1.2 $edwid->set (visibility => 0) if $type eq 'password';
56 elmex 1.1 $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 elmex 1.2 } elsif ($type eq 'desc') {
70     $table->attach_defaults (Gtk2::Label->new ($desc), 0, 2, $idx, $idx + 1);
71    
72 elmex 1.4 } 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 elmex 1.1 } 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;
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     $self->{ref_hash}->{$_} = $self->{edit_hash}->{$_};
109     }
110     }
111    
112     sub reset {
113     my ($self) = @_;
114    
115     for (@{$self->{hash_keys}}) {
116     $self->{edit_hash}->{$_} = $self->{ref_hash}->{$_};
117     }
118     }
119    
120     sub build_table {
121     my ($self) = @_;
122     $self->{table}->remove ($_) for $self->{table}->get_children;
123    
124     $self->{table_idx} = 0;
125    
126     for (@{$self->{dialog}}) {
127     $self->_add_prop_entry (@$_);
128     }
129     }
130    
131     sub init {
132     my ($self, %args) = @_;
133     for (keys %args) { $self->{$_} = $args{$_} }
134    
135     $self->set_title ("gcrossedit - $self->{title}");
136    
137     ::set_pos_and_size ($self, $::CFG->{$self->{layout_name}}, @{$self->{dialog_default_size}});
138    
139     my $cfg_lines = scalar @{$self->{dialog}};
140    
141     for (@{$self->{dialog}}) {
142     push @{$self->{hash_keys}}, $_->[0] if defined $_->[0];
143     }
144    
145 elmex 1.2 $self->add (my $vb = Gtk2::VBox->new);
146 elmex 1.3 $vb->pack_start (my $ilbl = Gtk2::Label->new ($self->{info}), 0, 1, 0)
147     if $self->{info};
148 elmex 1.2 $vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
149 elmex 1.1 $sw->set_policy ('automatic', 'automatic');
150     $sw->add_with_viewport (my $v = Gtk2::VBox->new);
151     $v->pack_start ($self->{table} = Gtk2::Table->new (2, $cfg_lines), 0, 0, 0);
152 elmex 1.2 if ($self->{text_entry}) {
153     $vb->pack_start (Gtk2::Label->new ($self->{text_entry}->{label}), 0, 1, 0)
154     if $self->{text_entry}->{label};
155     $vb->pack_start (my $txt = Gtk2::TextView->new, 1, 1, 0);
156     # TODO/XXX: Implement hash-changes!
157     }
158     $vb->pack_start (my $hb = Gtk2::HBox->new (1, 0), 0, 0, 0);
159     $hb->pack_start (my $save_btn = Gtk2::Button->new ($self->{save_button_label} || "save"), 1, 1, 0);
160     $hb->pack_start (my $reset_btn = Gtk2::Button->new ("reset"), 1, 1, 0);
161     $hb->pack_start (my $close_btn = Gtk2::Button->new ("cancel"), 1, 1, 0);
162 elmex 1.1
163     $self->reset;
164    
165     $self->build_table;
166    
167     $save_btn->signal_connect (clicked => sub {
168     $self->save;
169     });
170    
171     $reset_btn->signal_connect (clicked => sub {
172     $self->reset;
173     $self->build_table;
174     $self->show_all;
175     });
176    
177     $close_btn->signal_connect (clicked => sub {
178     $self->destroy;
179     });
180     }
181    
182     sub INIT_INSTANCE {
183     my ($self) = @_;
184     }
185    
186     1;