| 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.1 |
} 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 |
elmex |
1.2 |
$self->add (my $vb = Gtk2::VBox->new); |
| 137 |
elmex |
1.3 |
$vb->pack_start (my $ilbl = Gtk2::Label->new ($self->{info}), 0, 1, 0) |
| 138 |
|
|
if $self->{info}; |
| 139 |
elmex |
1.2 |
$vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0); |
| 140 |
elmex |
1.1 |
$sw->set_policy ('automatic', 'automatic'); |
| 141 |
|
|
$sw->add_with_viewport (my $v = Gtk2::VBox->new); |
| 142 |
|
|
$v->pack_start ($self->{table} = Gtk2::Table->new (2, $cfg_lines), 0, 0, 0); |
| 143 |
elmex |
1.2 |
if ($self->{text_entry}) { |
| 144 |
|
|
$vb->pack_start (Gtk2::Label->new ($self->{text_entry}->{label}), 0, 1, 0) |
| 145 |
|
|
if $self->{text_entry}->{label}; |
| 146 |
|
|
$vb->pack_start (my $txt = Gtk2::TextView->new, 1, 1, 0); |
| 147 |
|
|
# TODO/XXX: Implement hash-changes! |
| 148 |
|
|
} |
| 149 |
|
|
$vb->pack_start (my $hb = Gtk2::HBox->new (1, 0), 0, 0, 0); |
| 150 |
|
|
$hb->pack_start (my $save_btn = Gtk2::Button->new ($self->{save_button_label} || "save"), 1, 1, 0); |
| 151 |
|
|
$hb->pack_start (my $reset_btn = Gtk2::Button->new ("reset"), 1, 1, 0); |
| 152 |
|
|
$hb->pack_start (my $close_btn = Gtk2::Button->new ("cancel"), 1, 1, 0); |
| 153 |
elmex |
1.1 |
|
| 154 |
|
|
$self->reset; |
| 155 |
|
|
|
| 156 |
|
|
$self->build_table; |
| 157 |
|
|
|
| 158 |
|
|
$save_btn->signal_connect (clicked => sub { |
| 159 |
|
|
$self->save; |
| 160 |
|
|
}); |
| 161 |
|
|
|
| 162 |
|
|
$reset_btn->signal_connect (clicked => sub { |
| 163 |
|
|
$self->reset; |
| 164 |
|
|
$self->build_table; |
| 165 |
|
|
$self->show_all; |
| 166 |
|
|
}); |
| 167 |
|
|
|
| 168 |
|
|
$close_btn->signal_connect (clicked => sub { |
| 169 |
|
|
$self->destroy; |
| 170 |
|
|
}); |
| 171 |
|
|
} |
| 172 |
|
|
|
| 173 |
|
|
sub INIT_INSTANCE { |
| 174 |
|
|
my ($self) = @_; |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
1; |