package GCE::AttrEdit; =head1 NAME GCE::AttrEdit - an edit wiget for attributes =cut use Gtk2; use Gtk2::Gdk::Keysyms; use Gtk2::SimpleList; use Glib::Object::Subclass Gtk2::ScrolledWindow; use Crossfire; sub INIT_INSTANCE { my ($self) = @_; $self->add_with_viewport (my $vb = new Gtk2::VBox); $self->set_policy ('automatic', 'automatic'); $vb->pack_start (my $lbl = $self->{arch_name_lbl} = Gtk2::Label->new, 0, 1, 0); $vb->pack_start (my $ntbook = $self->{ntbook} = Gtk2::Notebook->new, 1, 1, 0); } sub update_arch { my ($self, $arch, $key, $value) = @_; if ($value ne '') { my $al_arch = $Crossfire::ARCH{$arch->{_name}}; print "**********************> SET $key = $value\n"; if (ref $value) { $arch->{$key} = $value; } else { if (not defined $al_arch->{$key}) { if (not $value) { # try to normalize print ">>>>>>>>>>>>>>>>>>>>>> DEL $key\n"; delete $arch->{$key}; } else { # try to normalize print ">>>>>>>>>>>>>>>>>>>>>> SET $key = $value\n"; $arch->{$key} = $value; } } else { if ($al_arch->{$key} ne $value) { print ">>>>>>>>>>>>>>>>>>>>>>> SET $key = $value\n"; $arch->{$key} = $value; } else { # try to normalize print ">>>>>>>>>>>>>>>>>>>>>> DEL $key\n"; delete $arch->{$key}; } } } } else { delete $arch->{$key}; } $self->{change_cb}->($arch) if defined $self->{change_cb}; } sub set_attr { my ($self, $key, $value) = @_; my $attr = $self->{arch}->{$key}; unless (ref $attr) { $self->update_arch ($self->{arch}, $key, $value); } } sub set_arch { my ($self, $arch, $change_cb) = @_; $self->{change_cb} = $change_cb; $self->{arch} = $arch; $self->{arch_name_lbl}->set_text ( $arch->{_name} . ($arch->{name} ? " - $arch->{name}" : "") ); if (not defined $change_cb) { } else { } my $al_arch = $Crossfire::ARCH{$arch->{_name}}; if ($al_arch) { $self->hide; $self->{ntbook}->remove ($_) for $self->{ntbook}->get_children; my $ar = Crossfire::arch_attr $al_arch; warn "FO1:" . Data::Dumper::Dumper ($al_arch) . ">\n"; warn "FO2:" . Data::Dumper::Dumper ($ar) . ">\n"; warn "REAL: " . Data::Dumper::Dumper ($arch) . "\n"; $self->{ttip} = Gtk2::Tooltips->new; #$self->add_section_edit_widgets ($self->{ntbook}, 'general', $arch, $ar->{attr}); for my $sec (@{$ar->{section}}) { # my $cnt = scalar keys %{$sects{$sec}}; my $secname = shift @$sec; $self->add_section_edit_widgets ($self->{ntbook}, $secname, $arch, $sec);#$sects{$sec}); } for my $key (qw/lore msg/) { $self->{ntbook}->append_page (my $v = Gtk2::VBox->new, $key); $v->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0); $sw->set_policy ('automatic', 'automatic'); $sw->add (my $tb = $self->{"${key}_txt"} = Gtk2::TextView->new); my $buf = $tb->get_buffer->set_text ($arch->{$key}); $v->pack_start (my $b = Gtk2::Button->new_with_label ("save"), 0, 1, 0); $b->signal_connect (clicked => sub { my $buf = $tb->get_buffer; $self->update_arch ($arch, $key, $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0) ); }); } $self->{ttip}->enable; $self->show_all; } else { print "NOARCH FOR: $arch->{_name}\n"; } } sub add_section_edit_widgets { my ($self, $ntbook, $name, $arch, $section) = @_; $self->{ntbook}->append_page (my $vb = Gtk2::VBox->new, $name); $vb->pack_start (my $table = new Gtk2::Table (2, $cnt), 0, 1, 0); my $i = 0; for my $sec (@$section) { my $bwid = Gtk2::EventBox->new; my $key = $sec->[0]; $sec = $sec->[1]; $bwid->add (Gtk2::Label->new ($sec->{name} || $key)); if ($sec->{desc} !~ m/^\s*$/s) { $self->{ttip}->set_tip ($bwid, $sec->{desc}); } my $al = Gtk2::Alignment->new (0.0, 0.5, 0, 0); $al->add ($bwid); $table->attach ($al, 0, 1, $i, $i + 1, ['shrink','fill'], 'fill', 5, 0); $al = Gtk2::Alignment->new (0.0, 0.5, 1, 0); $al->add ($self->get_edit_widget ($key, $sec, $arch, $bwid)); $table->attach ($al, 1, 2, $i, $i + 1, ['expand', 'fill'], 'expand', 0, 0); $i++; } } sub label_set_color_default { my ($self, $lbl, $arch, $key, $val) = @_; my $al_arch = $Crossfire::ARCH{$arch->{_name}}; my $fgcolor = Gtk2::Gdk::Color->parse ("lightblue"); my $fgcolor2 = Gtk2::Gdk::Color->parse ("black");#grey"); if ($al_arch->{$key} eq $arch->{$key}) { $lbl->modify_base (qw/normal/, $fgcolor); } else { $lbl->modify_base (qw/normal/, $fgcolor2); } } sub get_edit_widget { my ($self, $key, $edspec, $arch, $lbl) = @_; my $type = $edspec->{type}; my $al_arch = $Crossfire::ARCH{$arch->{_name}}; if ($type eq 'bool') { my $chk = new Gtk2::CheckButton ($edspec->{name} || $key); $chk->set_active ($arch->{$key} || $al_arch->{$key}); $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key} || $al_arch->{$key}); $chk->signal_connect (clicked => sub { my ($chk) = @_; $self->update_arch ($arch, $key, $chk->get_active * 1); $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key}); print "SET $attr :" . $chk->get_active . "\n"; }); return $chk } elsif (grep { $type eq $_ } qw/string int treasurelist float/) { my $entry = new Gtk2::Entry; $entry->set_text ($arch->{$key} || $al_arch->{$key}); $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key} || $al_arch->{$key}); $entry->signal_connect (changed => sub { my ($entry) = @_; $self->update_arch ($arch, $key, $entry->get_text); $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key}); }); return $entry # } elsif ($type eq 'bitmask') { } elsif ($type eq 'spell' or $type eq 'nz_spell') { # XXX: nz_spell bug in datafiles? my $comb = Gtk2::ComboBox->new_text; my $spells_idx = {}; my $spells_cmb_idx = {}; my $sp = \%Crossfire::Data::SPELL; $comb->append_text (""); my $idx = 1; # XXX: replace this idx with a more save/correct method? for (sort { $sp->{$a} cmp $sp->{$b} } keys %$sp) { $spells_cmd_idx{$idx} = $_; $spells_idx{$_} = $idx++; $comb->append_text ($sp->{$_}); } $comb->set_active ($spells_idx{$arch->{$key} || $al_arch->{$key}}); $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key} || $al_arch->{$key}); $comb->signal_connect (changed => sub { my ($comb) = @_; $self->update_arch ($arch, $key, $spells_cmd_idx{$comb->get_active}); $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key}); }); return $comb } elsif ($type eq 'fixed') { return Gtk2::Label->new ("$edspec->{name} = $edspec->{value}"); } elsif ($type eq 'text') { my $b = $arch->{$key}; $b =~ s/\n\r?//gs; if (length $b > 20) { $b = (substr $b, 0, 20) . "..."; } return Gtk2::Label->new ($b); } else { return Gtk2::Label->new ("$key => $edspec->{name} ($type)"); } } =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ Robin Redeker http://www.ta-sa.org/ =cut 1;