1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
|
#
# This file is part of Tk-ObjEditor
#
# This software is copyright (c) 2014 by Dominique Dumont.
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#
package Tk::ObjEditor;
$Tk::ObjEditor::VERSION = '2.010';
use Carp;
use Tk::Derived;
use Tk::Frame;
use Tk::ObjScanner 2.010;
use Tk::Dialog;
use Tk::DialogBox;
use warnings;
use strict;
use 5.10.1;
use Scalar::Util 1.01 qw(reftype);
use vars qw/$VERSION @ISA/;
use Storable qw(dclone);
use base qw(Tk::Derived Tk::ObjScanner);
Tk::Widget->Construct('ObjEditor');
sub _isa {
return (reftype($_[0]) // '') eq $_[1] ;
}
sub edit_object {
require Tk;
import Tk;
my $object = shift;
my $mw = MainWindow->new;
$mw->geometry('+10+10');
my $s = $mw->ObjEditor(
'-caller' => $object,
-direct => 1,
-title => 'object editor'
);
$s->pack( -expand => 1, -fill => 'both' );
$s->OnDestroy( sub { $mw->destroy; } );
&MainLoop; # Tk's
}
sub InitObject {
my ( $cw, $args ) = @_;
my $data = delete $args->{'caller'} || delete $args->{'-caller'};
$cw->{direct} =
delete $args->{'direct'} || delete $args->{'-direct'} || 0;
$cw->{user_data} = $data;
my $edited_data = $cw->{direct} ? $data : dclone($data);
$args->{'-caller'} = $edited_data; # to pass to ObjScanner
$args->{'-show_tied'} = 0; # do not show tied data internal
$args->{title} = ref($data) . ' editor'
unless ( defined $args->{title} || defined $args->{-title} );
$cw->SUPER::InitObject($args);
$cw->Subwidget('hlist')->bind( '<B3-ButtonRelease>', sub { $cw->modify_menu() } );
$cw->{actions} = [];
return $cw;
}
sub modify_menu {
my $cw = shift;
my $item = shift; # reserved for tests
unless ( defined $item ) {
# pointery and rooty are common widget method and must called on
# the right widget to give accurate results
my $hlist = $cw->Subwidget('hlist');
$item = $cw->nearest( $hlist->pointery - $hlist->rooty );
}
$cw->selectionClear(); # clear all
$cw->selectionSet($item);
#print "item $item to modify\n";
my $menu = $cw->Menu;
my $ref = $cw->info( "data", $item )->{item_ref};
my @children = $cw->infoChildren($item);
if ( not ref($$ref) ) {
$menu->add(
'command',
'-label' => 'modify element',
'-command' => sub { $cw->modify_entry($item); } );
}
if ( $item eq 'root' ) {
$menu->add(
'command',
'-label' => 'reset',
'-command' => sub { $cw->reset; } ) unless $cw->{direct};
}
else {
$menu->add(
'command',
'-label' => 'delete',
'-command' => sub { $cw->delete_entry($item); } );
}
$menu->Popup( -popover => 'cursor', -popanchor => 'nw' );
return $menu;
}
sub reset {
my $cw = shift;
$cw->{chief} = dclone( $cw->{user_data} );
$cw->updateListBox();
}
# returns the edited data (a clone in case of no-direct option)
sub get_data {
my $cw = shift;
return $cw->{chief};
}
sub get_orig_data {
my $cw = shift;
return $cw->{user_data};
}
sub modify_entry {
my $cw = shift;
my $item = shift;
my $is_text = shift || 0;
my $text = $cw->entrycget( $item, '-text' );
my ($item_key) = ( $text =~ m/^[\[\{](.*?)[\]\}]->/ );
my $c_data = $cw->entrycget( $item, '-data' );
my $ref = $c_data->{item_ref};
my $modified = $cw->modify_widget( $ref, $is_text );
return unless $modified;
# replace value in parent sdata structure
$cw->update_item($item);
}
sub update_item {
my ( $cw, $item ) = @_[ 0, 1 ];
my $direction = $_[2] || '';
my $c_data = $cw->entrycget( $item, '-data' );
my $parent_item = $cw->info( "parent", $item );
my $parent_c_data = $cw->entrycget( $parent_item, '-data' );
my $parent_ref =
$item eq 'root' ? $cw->{chief}
: $parent_c_data->{tied_display} ? \tied( ${ $parent_c_data->{item_ref} } )
: $parent_c_data->{item_ref};
# update HList display
my $text = $cw->describe_element( $parent_ref, $c_data->{index} );
$cw->entryconfigure( $item, -text => $text );
# update parent if necessary
if ( $parent_c_data->{tied_display} and $direction ne 'down' ) {
$cw->update_item( $parent_item, 'up' );
}
# update below if necessary
if ( $c_data->{tied_display} and $direction ne 'up' ) {
my $h = $cw->Subwidget('hlist');
foreach my $child ( $h->infoChildren($item) ) {
$cw->update_item( $child, 'down' );
}
}
}
sub modify_widget {
my $cw = shift;
my $ref = shift;
my $is_text = shift;
# construct popup dialog to change item value.
my $db = $cw->DialogBox(
-title => 'modify element',
-buttons => [ "OK", "Cancel" ] );
$cw->{current_dialog} = $db;
# Note: focus is taken over by DialogBox and given to "OK"
$db->add( 'Label', -text => 'Please enter new value' )->pack;
my $textw;
if ( $is_text or ( defined $$ref and $$ref =~ /\n/ ) ) {
$textw = $db->add('Text')->pack( -expand => 1, -fill => 'both' );
$textw->insert( 'end', $$ref );
$db->bind( '<Return>', '' ); # remove Dialog Box binding on return
$db->Advertise( 'Entry' => $textw );
}
else {
my $entry = $db->add( 'Entry', -textvariable => $ref )->pack( -expand => 1, -fill => 'x' );
$db->Advertise( 'Entry' => $entry );
}
# Show method should be enhanced to accept a "focus" parameter
# so focus could be given to the actual editing widget
my $answer = $db->Show;
return 0 unless $answer eq "OK";
# the '- 1c' skips the newline erroneously added by the text widget
# Thanks Slaven
$$ref = $textw->get( '1.0', 'end - 1c' ) if defined $textw;
return 1;
}
sub add_entry {
my $cw = shift;
my $item = shift;
# construct popup dialog to change item value.
my $db = $cw->DialogBox(
-title => 'add element',
-buttons => [ "OK", "Cancel" ] );
my $ok = $db->Subwidget('B_OK');
# enforce that a type is choosen by the user and that a new key is used
my ( $key_ok, $type_ok ) = ( 0, 0 );
my $check = sub {
my $what = $key_ok && $type_ok ? 'normal' : 'disabled';
$ok->configure( -state => $what );
};
&$check; # for fun and for test
my $ref_ref = $cw->entrycget( $item, '-data' )->{item_ref};
my $ref = $$ref_ref;
my $is_hash_like = _isa( $ref, 'HASH' );
my $what = $is_hash_like ? 'key' : 'index';
$db->add( 'Label', -text => "enter new $what" )->pack;
my $key = $is_hash_like ? '' : scalar(@$ref);
$db->add(
'Entry',
-textvariable => \$key,
-validate => 'key',
-validatecommand => sub {
my $prop = shift;
#print "key: '$prop'\n";
if ( ( $is_hash_like and not exists $ref->{$prop} )
or ( _isa( $ref, 'ARRAY' ) and not defined $ref->[$prop] ) ) {
$key_ok = 1;
}
else { $key_ok = 0; }
&$check;
1;
} )->pack;
$db->add( 'Label', -text => "Specify new element type" )->pack;
my $type = 'undef';
my $mb = $db->add(
'Menubutton',
-textvariable => \$type,
qw/-indicatoron 1 -relief raised/
);
my $menu = $mb->Menu( -tearoff => 0 );
$mb->configure( -menu => $menu );
foreach (qw/array hash scalar text/) {
$mb->radiobutton(
-label => $_,
-value => $_,
-variable => \$type,
-indicatoron => 0,
-command => sub { $type_ok = 1; &$check; } );
}
$mb->pack;
return if $db->Show ne 'OK';
# update data structure
my $new;
if ( $type eq 'hash' ) { $new = {}; }
elsif ( $type eq 'array' ) { $new = []; }
elsif ( $type eq 'text' ) { $cw->modify_widget( \$new, 1 ); }
else { $cw->modify_widget( \$new, 0 ); }
return unless defined $new;
$ref->{$key} = $new if _isa( $ref, 'HASH' );
$ref->[$key] = $new if _isa( $ref, 'ARRAY' );
#recompute the text for parent widget
my $text = $cw->element( \$ref );
$cw->entryconfigure( $item, '-text', $text );
#(re)display the children
$cw->deleteOffsprings($item);
$cw->displaySubItem($item); # by default do not display tied internals
}
sub delete_entry {
my $cw = shift;
my $item = shift;
my $item_key = $cw->entrycget( $item, '-data' )->{index} || '';
my $dialog = $cw->Dialog(
-title => 'WARNING',
-text => "Delete the '$item_key' element and all its children ?",
-buttons => [ "Yes", "No" ] );
my $answer = $dialog->Show();
return if $answer eq "No";
my $parent_item = $cw->info( "parent", $item );
my $text_parent = $cw->entrycget( $parent_item, "-text" );
my $parent_ref = $cw->entrycget( $parent_item, '-data' )->{item_ref};
delete $$parent_ref->{$item_key} if _isa( $$parent_ref, 'HASH' );
splice @$$parent_ref, $item_key, 1 if _isa( $$parent_ref, 'ARRAY' );
$cw->entryconfigure( $parent_item, "-text", $cw->element($parent_ref) );
$cw->deleteEntry($item);
}
# used for tests
sub get_current_dialog {
my $self = shift;
return $self->{current_dialog};
}
1;
__END__
=head1 NAME
Tk::ObjEditor - Tk composite widget Obj editor
=head1 SYNOPSIS
use Tk::ObjEditor;
my $editor = $mw->ObjEditor( caller => $object,
direct => [1|0],
[title=>"windows"]) -> pack ;
=head1 DESCRIPTION
This widget provides a GUI to edit the attributes of an object or the
elements of a simple hash or array.
The editor is a L<Tk::ObjScanner> with additional function to edit
data. The editor can be used in an autonomous way with the
C<edit_object> function.
When the user double clicks (with left button) on an item, the
value of the item will be displayed in the HList.
If the value is a scalar, the scalar will be displayed in the text window.
(Which is handy if the value is a multi-line string)
If you use the middle button and the item (either hash, array or
scalar) is tied to an object , you will open the object hidden behind
the tied variable.
Use the right button of the mouse of an element to modify its
value. Depending on the context, you will also be able to delete the
element or to add a sub-element.
This may be not clear. If yes, I think that trying this widget will be
much clearer than any explanation I can write. So run the Tk widget
demo and you'll find the Obj editor demo in the "User Contributed
Demonstration" section.
=head1 Direct or undirect edit
As the constructor will pass a reference to the data structure to be
edited, the data can be edited :
=over
=item not directly
In this case, the data structure is cloned. The widget will edit the
cloned version of the data structure. This enable the user to cancel
the edition. This means that any reference to the internals of old
data structure will stay on the old datastructure and will not be
aware of the new values entered with this widget.
Unforunately, undirect edition will break if the cloned data structure
contains code reference.
=item directly
In this case, the data structure is not cloned. The edition is
performed on the passed reference. Any reference to the internals of
old data structure will be updated on-line. The drawback is that the
user cannot cancel (or undo) the edition.
=back
=head1 Constructor parameters
=over 4
=item *
-caller: The ref of the object or hash or array to edit (mandatory).
=item *
-title: the title of the menu created by the editor (optional)
=item *
-direct: Set to 1 if you want to perform direct edition.
=back
=head1 Autonomous widget
=head2 edit_object( data )
This function is not exported and must be called this way:
Tk::ObjEditor::edit_object($data);
This function will load Tk and pop up an editor widget. When the user
destroy the widget (with C<File -> destroy> menu), the user code is
resumed.
=head1 CAVEATS
Like L<Tk::ObjScanner> ObjEditor does not detect recursive data
structures. It will just keep on displaying the tree until the user
gets tired of clicking on the HList items.
ObjEditor cannot edit code reference. The module will break if you
try undirect edition of data containing code references.
=head1 AUTHOR
Dominique Dumont (ddumont at cpan.org), Guillaume Degremont.
=head1 LICENSE
Copyright (c) 1997-2004,2007,2014 Dominique Dumont, Guillaume Degremont. All
rights reserved. This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
=head1 SEE ALSO
perl(1), L<Tk>, L<Tk::HList>, L<Tk::ObjScanner>
=cut
|