ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/HashDialog.pm
Revision: 1.1
Committed: Sun Oct 15 15:35:18 2006 UTC (17 years, 7 months ago) by elmex
Branch: MAIN
Log Message:
changed and added propertie/resize/meta info dialoges.

File Contents

# Content
1 package GCE::HashDialogue;
2
3 =head1 NAME
4
5 GCE::AttachEditor - this is a editor for attachments for maps and objects
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') {
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_text ($self->{edit_hash}{$key});
56 $edwid->signal_connect (changed => sub {
57 $self->{edit_hash}->{$key} = $_[0]->get_text;
58 });
59
60 } elsif ($type eq 'button') {
61 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($desc), 0, 2, $idx, $idx + 1);
62 $b->signal_connect (clicked => sub { $cb->(\$self->{edit_hash}{$key}) if $cb });
63
64 } elsif ($type eq 'label') {
65 $table->attach_defaults (Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
66 $table->attach_defaults (Gtk2::Label->new ($self->{edit_hash}{$key}), 1, 2, $idx, $idx + 1);
67
68 } elsif ($type eq 'check') {
69 $table->attach_defaults (Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
70 $table->attach_defaults (my $chk = Gtk2::CheckButton->new, 1, 2, $idx, $idx + 1);
71 $chk->set_active ($self->{edit_hash}{$key});
72 $chk->signal_connect (toggled => sub {
73 my ($lbl) = @_;
74 $self->{edit_hash}{$key} = $lbl->get_active * 1;
75 });
76
77 } elsif ($type eq 'sep') {
78 $table->attach_defaults (Gtk2::HSeparator->new, 0, 2, $idx, $idx + 1);
79
80 } else {
81 die "Unrecognized dialogue type: $type\n";
82 }
83
84 $self->{table_idx}++;
85 }
86
87 sub save {
88 my ($self) = @_;
89
90 if ($self->{save_cb}) {
91 $self->{save_cb}->($self->{edit_hash});
92 }
93
94 for (@{$self->{hash_keys}}) {
95 $self->{ref_hash}->{$_} = $self->{edit_hash}->{$_};
96 }
97 }
98
99 sub reset {
100 my ($self) = @_;
101
102 for (@{$self->{hash_keys}}) {
103 $self->{edit_hash}->{$_} = $self->{ref_hash}->{$_};
104 }
105 }
106
107 sub build_table {
108 my ($self) = @_;
109 $self->{table}->remove ($_) for $self->{table}->get_children;
110
111 $self->{table_idx} = 0;
112
113 for (@{$self->{dialog}}) {
114 $self->_add_prop_entry (@$_);
115 }
116 }
117
118 sub init {
119 my ($self, %args) = @_;
120 for (keys %args) { $self->{$_} = $args{$_} }
121
122 $self->set_title ("gcrossedit - $self->{title}");
123
124 ::set_pos_and_size ($self, $::CFG->{$self->{layout_name}}, @{$self->{dialog_default_size}});
125
126 my $cfg_lines = scalar @{$self->{dialog}};
127
128 for (@{$self->{dialog}}) {
129 push @{$self->{hash_keys}}, $_->[0] if defined $_->[0];
130 }
131
132 $self->add (my $sw = Gtk2::ScrolledWindow->new);
133 $sw->set_policy ('automatic', 'automatic');
134 $sw->add_with_viewport (my $v = Gtk2::VBox->new);
135 $v->pack_start ($self->{table} = Gtk2::Table->new (2, $cfg_lines), 0, 0, 0);
136 $v->pack_start (my $hb = Gtk2::HBox->new (1, 0), 0, 0, 0);
137 $hb->pack_start (my $save_btn = Gtk2::Button->new ("save"), 1, 1, 0);
138 $hb->pack_start (my $reset_btn = Gtk2::Button->new ("reset"), 1, 1, 0);
139 $hb->pack_start (my $close_btn = Gtk2::Button->new ("cancel"), 1, 1, 0);
140
141 $self->reset;
142
143 $self->build_table;
144
145 $save_btn->signal_connect (clicked => sub {
146 $self->save;
147 });
148
149 $reset_btn->signal_connect (clicked => sub {
150 $self->reset;
151 $self->build_table;
152 $self->show_all;
153 });
154
155 $close_btn->signal_connect (clicked => sub {
156 $self->destroy;
157 });
158 }
159
160 sub INIT_INSTANCE {
161 my ($self) = @_;
162 }
163
164 1;