| 1 | # | ||
| 2 | # This file is part of Config-Model-TkUI | ||
| 3 | # | ||
| 4 | # This software is Copyright (c) 2008-2021 by Dominique Dumont. | ||
| 5 | # | ||
| 6 | # This is free software, licensed under: | ||
| 7 | # | ||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||
| 9 | # | ||
| 10 | package Config::Model::Tk::LeafEditor 1.382; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use warnings; | ||
| 14 | use Carp; | ||
| 15 | use Log::Log4perl; | ||
| 16 | use Config::Model::Tk::NoteEditor; | ||
| 17 | use Config::Model::Tk::CmeDialog; | ||
| 18 | use Path::Tiny; | ||
| 19 | use Tk::Balloon; | ||
| 20 | |||
| 21 | use base qw/Config::Model::Tk::LeafViewer/; | ||
| 22 | |||
| 23 | Construct Tk::Widget 'ConfigModelLeafEditor'; | ||
| 24 | |||
| 25 | my @fbe1 = qw/-fill both -expand 1/; | ||
| 26 | my @fxe1 = qw/-fill x -expand 1/; | ||
| 27 | my @fx = qw/-fill x /; | ||
| 28 | |||
| 29 | my $logger = Log::Log4perl::get_logger("Tk::LeafEditor"); | ||
| 30 | |||
| 31 | sub ClassInit { | ||
| 32 | my ( $cw, $args ) = @_; | ||
| 33 | |||
| 34 | # ClassInit is often used to define bindings and/or other | ||
| 35 | # resources shared by all instances, e.g., images. | ||
| 36 | |||
| 37 | # cw->Advertise(name=>$widget); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub Populate { | ||
| 41 | my ( $cw, $args ) = @_; | ||
| 42 | my $leaf = $cw->{leaf} = delete $args->{-item} | ||
| 43 | || die "LeafEditor: no -item, got ", keys %$args; | ||
| 44 | delete $args->{-path}; | ||
| 45 | $cw->{store_cb} = delete $args->{-store_cb} || die __PACKAGE__, "no -store_cb"; | ||
| 46 | my $cme_font = delete $args->{-font}; | ||
| 47 | |||
| 48 | my $inst = $leaf->instance; | ||
| 49 | my $vt = $leaf->value_type; | ||
| 50 | $logger->info("Creating leaf editor for value_type $vt"); | ||
| 51 | $cw->{value} = $leaf->fetch( check => 'no', mode => 'user' ); | ||
| 52 | $logger->info( "Creating leaf editor" ); | ||
| 53 | |||
| 54 | $cw->add_header( Edit => $leaf )->pack(@fx); | ||
| 55 | |||
| 56 | my $vref = \$cw->{value}; | ||
| 57 | |||
| 58 | my @pack_args = @fx; | ||
| 59 | @pack_args = @fbe1 | ||
| 60 | if $vt eq 'string' | ||
| 61 | or $vt eq 'enum' | ||
| 62 | or $vt eq 'reference'; | ||
| 63 | |||
| 64 | my $ed_frame = $cw->Frame(qw/-relief raised -borderwidth 2/)->pack(@pack_args); | ||
| 65 | $ed_frame->Label( -text => 'Edit value' )->pack(); | ||
| 66 | |||
| 67 | my $balloon = $cw->Balloon( -state => 'balloon' ); | ||
| 68 | |||
| 69 | if ( $vt eq 'string' ) { | ||
| 70 | $cw->add_string_editor($leaf, $ed_frame, $balloon); | ||
| 71 | } | ||
| 72 | elsif ( $vt eq 'boolean' and $leaf->write_as) { | ||
| 73 | $cw->add_written_as_boolean_editor($leaf, $ed_frame); | ||
| 74 | } | ||
| 75 | elsif ( $vt eq 'boolean' ) { | ||
| 76 | $ed_frame->Checkbutton( | ||
| 77 | -text => $leaf->element_name, | ||
| 78 | -variable => $vref, | ||
| 79 | -command => sub { $cw->try }, | ||
| 80 | )->pack(); | ||
| 81 | $cw->add_buttons($ed_frame); | ||
| 82 | } | ||
| 83 | elsif ( $vt eq 'uniline' or $vt eq 'integer' ) { | ||
| 84 | $ed_frame->Entry( -textvariable => $vref )->pack(@fx); | ||
| 85 | $cw->add_buttons($ed_frame); | ||
| 86 | } | ||
| 87 | elsif ( $vt eq 'enum' or $vt eq 'reference' ) { | ||
| 88 | my $lb = $ed_frame->Scrolled( | ||
| 89 | 'Listbox', | ||
| 90 | -height => 5, | ||
| 91 | -scrollbars => 'osow', | ||
| 92 | |||
| 93 | #-listvariable => $vref, | ||
| 94 | #-selectmode => 'single', | ||
| 95 | )->pack(@fbe1); | ||
| 96 | my @choice = $leaf->get_choice; | ||
| 97 | $lb->insert( 'end', $leaf->get_choice ); | ||
| 98 | my $idx = 0; | ||
| 99 | if ( defined $$vref ) { | ||
| 100 | foreach my $c (@choice) { | ||
| 101 | $lb->selectionSet($idx) if $c eq $$vref; | ||
| 102 | $idx++; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | $lb->bind( '<Button-1>', sub { $cw->try( $lb->get( $lb->curselection() ) ) } ); | ||
| 106 | $cw->add_buttons($ed_frame); | ||
| 107 | |||
| 108 | } | ||
| 109 | |||
| 110 | $cw->ConfigModelNoteEditor( -object => $leaf )->pack; | ||
| 111 | $cw->add_warning( $leaf, 'edit' )->pack(@fx); | ||
| 112 | $cw->add_info_button()->pack( @fx, qw/-anchor n/ ); | ||
| 113 | $cw->add_summary($leaf)->pack(@fx); | ||
| 114 | $cw->add_description($leaf)->pack(@fbe1); | ||
| 115 | my ( $help_frame, $help_widget ) = $cw->add_help( 'help on value' => '', 1 ); | ||
| 116 | $help_frame->pack(@fx); | ||
| 117 | |||
| 118 | $cw->Advertise( value_help_widget => $help_widget ); | ||
| 119 | $cw->Advertise( value_help_frame => $help_frame ); | ||
| 120 | |||
| 121 | $cw->set_value_help; | ||
| 122 | |||
| 123 | $cw->ConfigSpecs( | ||
| 124 | -font => [['SELF','DESCENDANTS'], 'font','Font', $cme_font ], | ||
| 125 | |||
| 126 | #-fill => [ qw/SELF fill Fill both/], | ||
| 127 | #-expand => [ qw/SELF expand Expand 1/], | ||
| 128 | -relief => [qw/SELF relief Relief groove/], | ||
| 129 | -borderwidth => [qw/SELF borderwidth Borderwidth 2/], | ||
| 130 | DEFAULT => [qw/SELF/], | ||
| 131 | ); | ||
| 132 | |||
| 133 | # don't call directly SUPER::Populate as it's LeafViewer's populate | ||
| 134 | $cw->Tk::Frame::Populate($args); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub add_string_editor { | ||
| 138 | my ($cw, $leaf, $ed_frame, $balloon) = @_; | ||
| 139 | |||
| 140 | $cw->{e_widget} = $ed_frame->Scrolled( | ||
| 141 | 'Text', | ||
| 142 | -height => 5, | ||
| 143 | -scrollbars => 'ow', | ||
| 144 | )->pack(@fbe1); | ||
| 145 | $cw->{e_widget}->tagConfigure(qw/value -lmargin1 2 -lmargin2 2 -rmargin 2/); | ||
| 146 | $cw->reset_value; | ||
| 147 | |||
| 148 | my $bframe = $cw->add_buttons($ed_frame); | ||
| 149 | $bframe->Button( | ||
| 150 | -text => 'Cleanup', | ||
| 151 | -command => sub { $cw->cleanup }, | ||
| 152 | )->pack( -side => 'left' ); | ||
| 153 | |||
| 154 | my $ext_ed_b = $bframe->Button( | ||
| 155 | -text => 'Ext editor', | ||
| 156 | -command => sub { $cw->exec_external_editor }, | ||
| 157 | -state => defined $ENV{EDITOR} ? 'normal' : 'disabled', | ||
| 158 | )->pack( -side => 'left' ); | ||
| 159 | |||
| 160 | $balloon->attach( | ||
| 161 | $ext_ed_b, | ||
| 162 | -msg => "Run external editor (if EDITOR environment variable is set" | ||
| 163 | ); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub add_written_as_boolean_editor { | ||
| 167 | my ($cw, $leaf, $ed_frame) = @_; | ||
| 168 | |||
| 169 | my $vref = \$cw->{value}; | ||
| 170 | |||
| 171 | my $rb_frame = $ed_frame->Frame->pack(); | ||
| 172 | foreach my $value (@{$leaf->write_as}) { | ||
| 173 | $rb_frame->Radiobutton( | ||
| 174 | -text => $value, | ||
| 175 | -value => $value, | ||
| 176 | -variable => $vref, | ||
| 177 | -command => sub { $cw->try }, | ||
| 178 | )->pack(-side => 'left'); | ||
| 179 | } | ||
| 180 | $cw->add_buttons($ed_frame); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub cleanup { | ||
| 184 | my ($cw) = @_; | ||
| 185 | my $text_widget = $cw->{e_widget} || return; | ||
| 186 | my $selected = $text_widget->getSelected; | ||
| 187 | my $text = $selected || $text_widget->Contents; | ||
| 188 | $text =~ s/^\s+//gm; | ||
| 189 | $text =~ s/\s+$//gm; | ||
| 190 | $text =~ s/\s+/ /g; | ||
| 191 | |||
| 192 | if ($selected) { | ||
| 193 | $text_widget->Insert($text); | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | $text_widget->Contents($text); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | sub add_buttons { | ||
| 201 | my ( $cw, $frame ) = @_; | ||
| 202 | my $bframe = $frame->Frame->pack(); | ||
| 203 | |||
| 204 | my $balloon = $cw->Balloon( -state => 'balloon' ); | ||
| 205 | |||
| 206 | my $reset_b = $bframe->Button( | ||
| 207 | -text => 'Reset', | ||
| 208 | -command => sub { $cw->reset_value; }, | ||
| 209 | )->pack( -side => 'left' ); | ||
| 210 | $balloon->attach( $reset_b, -msg => "reset entry value from tree value" ); | ||
| 211 | |||
| 212 | my $del_label = defined $cw->{leaf}->fetch_standard ? 'Back to default' : 'Delete'; | ||
| 213 | $bframe->Button( | ||
| 214 | -text => $del_label, | ||
| 215 | -command => sub { $cw->delete }, | ||
| 216 | )->pack( -side => 'left' ); | ||
| 217 | my $store_b = $bframe->Button( | ||
| 218 | -text => 'Store', | ||
| 219 | -command => sub { $cw->store }, | ||
| 220 | )->pack( -side => 'right' ); | ||
| 221 | |||
| 222 | $balloon->attach( $store_b, -msg => "store entry value in config tree" ); | ||
| 223 | |||
| 224 | return $bframe; | ||
| 225 | } | ||
| 226 | |||
| 227 | # Try is invoked for enum and boolean, where the possible value are | ||
| 228 | # set in the widget. Hence the user cannot enter a wrong | ||
| 229 | # value. However there may be side effects (like warp) that trigger | ||
| 230 | # and error, hence this check. | ||
| 231 | sub try { | ||
| 232 | my $cw = shift; | ||
| 233 | my $v = shift; | ||
| 234 | |||
| 235 | if ( defined $v ) { | ||
| 236 | $cw->{value} = $v; | ||
| 237 | } | ||
| 238 | else { | ||
| 239 | my $e_w = $cw->{e_widget}; | ||
| 240 | |||
| 241 | # tk widget use a reference | ||
| 242 | if (defined $e_w) { | ||
| 243 | $v = $e_w->get( '1.0', 'end' ); | ||
| 244 | chomp $v; | ||
| 245 | } | ||
| 246 | else { | ||
| 247 | $v = $cw->{value}; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | $v = '' unless defined $v; | ||
| 252 | |||
| 253 | $logger->debug("try: value $v"); | ||
| 254 | |||
| 255 | my @errors = $cw->{leaf}->check( value => $v, quiet => 1 ); | ||
| 256 | |||
| 257 | if (@errors) { | ||
| 258 | $cw->CmeDialog( | ||
| 259 | -title => 'Check value error', | ||
| 260 | -text => \@errors, | ||
| 261 | )->Show; | ||
| 262 | $cw->reset_value; | ||
| 263 | return; | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | $cw->set_value_help($v); | ||
| 267 | return $v; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | sub delete { | ||
| 272 | my $cw = shift; | ||
| 273 | |||
| 274 | eval { $cw->{leaf}->clear; }; | ||
| 275 | |||
| 276 | if ($@) { | ||
| 277 | $cw->CmeDialog( | ||
| 278 | -title => 'Delete error', | ||
| 279 | -text => "$@", | ||
| 280 | )->Show; | ||
| 281 | } | ||
| 282 | else { | ||
| 283 | # trigger redraw of Tk Tree | ||
| 284 | $cw->reset_value; | ||
| 285 | $cw->update_warning( $cw->{leaf} ); | ||
| 286 | $cw->{store_cb}->(); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | # can be used without parameters to store value from widget into config tree | ||
| 291 | sub store { | ||
| 292 | my $cw = shift; | ||
| 293 | my $arg = shift; | ||
| 294 | my $e_w = $cw->{e_widget}; | ||
| 295 | |||
| 296 | # tk widget use a reference | ||
| 297 | my $v; | ||
| 298 | if (defined $arg) { | ||
| 299 | $v = $arg; | ||
| 300 | } | ||
| 301 | elsif (defined $e_w) { | ||
| 302 | $v = $e_w->get( '1.0', 'end' ); | ||
| 303 | chomp $v; # Tk::Text::get always add a "\n"; | ||
| 304 | } | ||
| 305 | else { | ||
| 306 | $v = $cw->{value}; | ||
| 307 | } | ||
| 308 | |||
| 309 | $v = '' unless defined $v; | ||
| 310 | |||
| 311 | my $leaf = $cw->{leaf}; | ||
| 312 | |||
| 313 | print "Storing '$v'\n"; | ||
| 314 | |||
| 315 | eval { $leaf->store($v); }; | ||
| 316 | |||
| 317 | if ($@) { | ||
| 318 | $cw->CmeDialog( | ||
| 319 | -title => 'Failed to store value', | ||
| 320 | -text => "$@", | ||
| 321 | )->Show; | ||
| 322 | $cw->reset_value; | ||
| 323 | } | ||
| 324 | elsif ($leaf->has_error) { | ||
| 325 | $cw->CmeDialog ( | ||
| 326 | -title => 'Value error', | ||
| 327 | -text => "Cannot store the value:\n* ".join("\n* ",$leaf->all_errors), | ||
| 328 | )->Show; | ||
| 329 | $cw->reset_value; | ||
| 330 | } | ||
| 331 | else { | ||
| 332 | # trigger redraw of Tk Tree | ||
| 333 | $cw->{store_cb}->(); | ||
| 334 | $cw->update_warning( $leaf ); | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | sub set_value_help { | ||
| 339 | my $cw = shift; | ||
| 340 | my $v = $cw->{value}; | ||
| 341 | my $value_help = defined $v ? $cw->{leaf}->get_help($v) : ''; | ||
| 342 | |||
| 343 | my $w = $cw->Subwidget('value_help_widget'); | ||
| 344 | my $f = $cw->Subwidget('value_help_frame'); | ||
| 345 | |||
| 346 | if ($value_help) { | ||
| 347 | |||
| 348 | #$w->delete( '0.0', 'end' ); | ||
| 349 | #$w->insert( 'end', $value_help ) ; | ||
| 350 | $cw->update_help( $w, $value_help ); | ||
| 351 | $f->pack(@fbe1); | ||
| 352 | } | ||
| 353 | else { | ||
| 354 | $f->packForget; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | sub reset_value { | ||
| 359 | my $cw = shift; | ||
| 360 | $cw->{value} = $cw->{leaf}->fetch( check => 'no' ); | ||
| 361 | if ( defined $cw->{e_widget} ) { | ||
| 362 | $cw->{e_widget}->delete( '1.0', 'end' ); | ||
| 363 | $cw->{e_widget}->insert( 'end', $cw->{value}, 'value' ); | ||
| 364 | } | ||
| 365 | $cw->set_value_help if defined $cw->{value_help_widget}; | ||
| 366 | } | ||
| 367 | |||
| 368 | sub exec_external_editor { | ||
| 369 | my $cw = shift; | ||
| 370 | |||
| 371 | my @pt_args; | ||
| 372 | |||
| 373 | # ugly hack to use pod mode only for Model description parameter | ||
| 374 | # i.e. for 'cme meta edit; | ||
| 375 | my $leaf = $cw->{leaf}; | ||
| 376 | if ($leaf->parent->config_class_name =~ /^Itself/ and | ||
| 377 | $leaf->element_name =~ /description/ | ||
| 378 | ) { | ||
| 379 | # the .pod suffix let the editor use the correct mode | ||
| 380 | @pt_args = (SUFFIX => '.pod'); | ||
| 381 | } | ||
| 382 | |||
| 383 | my $pt = Path::Tiny->tempfile(@pt_args); | ||
| 384 | |||
| 385 | die "Can't create Path::Tiny:$!" unless defined $pt; | ||
| 386 | my $orig_data = $cw->{e_widget}->get( '1.0', 'end' ); | ||
| 387 | chomp $orig_data; # Tk::Text::get always add a "\n"; | ||
| 388 | $pt->spew_utf8( $orig_data ); | ||
| 389 | |||
| 390 | # See mastering Perl/Tk p382 | ||
| 391 | my $h = $cw->{ed_handle} = IO::Handle->new; | ||
| 392 | die "IO::Handle->new failed." unless defined $h; | ||
| 393 | |||
| 394 | my $ed = $ENV{EDITOR} . ' ' . $pt->canonpath; | ||
| 395 | $cw->{ed_pid} = open( $h, '|-', $ed ); | ||
| 396 | |||
| 397 | if ( not defined $cw->{ed_pid} ) { | ||
| 398 | $cw->CmeDialog( | ||
| 399 | -title => 'External editor error', | ||
| 400 | -text => "'$ed' : $!", | ||
| 401 | )->Show; | ||
| 402 | return; | ||
| 403 | } | ||
| 404 | $h->autoflush(1); | ||
| 405 | $cw->fileevent( $h, 'readable' => [ \&_read_stdout, $cw ] ); | ||
| 406 | |||
| 407 | # prevent navigation in the tree (and destruction of this widget | ||
| 408 | # while the external editor is active). See mastering Perl/Tk p302 | ||
| 409 | $cw->grab; | ||
| 410 | |||
| 411 | $cw->waitVariable( \$cw->{ed_done} ); | ||
| 412 | |||
| 413 | $cw->grabRelease; | ||
| 414 | |||
| 415 | my $new_v = $pt->slurp_utf8(); | ||
| 416 | print "exec_external_editor done with '$new_v'\n"; | ||
| 417 | $cw->store($new_v); | ||
| 418 | $cw->reset_value; | ||
| 419 | } | ||
| 420 | |||
| 421 | # also from Mastering Perl/Tk | ||
| 422 | sub _read_stdout { | ||
| 423 | |||
| 424 | # Called when input is available for the output window. Also checks | ||
| 425 | # to see if the user has clicked Cancel. | ||
| 426 | print "_read_stdout called\n"; | ||
| 427 | my ($cw) = @_; | ||
| 428 | |||
| 429 | my $h = $cw->{ed_handle}; | ||
| 430 | die "External editor handle is udefined!\n" unless defined $h; | ||
| 431 | my $stat; | ||
| 432 | |||
| 433 | if ( $stat = sysread $h, $_, 4096 ) { | ||
| 434 | print; | ||
| 435 | } | ||
| 436 | elsif ( $stat == 0 ) { | ||
| 437 | print "edition done\n"; | ||
| 438 | $h->close; | ||
| 439 | $cw->{ed_done} = 1; | ||
| 440 | } | ||
| 441 | else { | ||
| 442 | die "External editor sysread error: $!"; | ||
| 443 | } | ||
| 444 | } # end _read_stdout | ||
| 445 | |||
| 446 | sub reload { | ||
| 447 | my $cw = shift; | ||
| 448 | $cw->reset_value; | ||
| 449 | $cw->update_warning( $cw->{leaf} ); | ||
| 450 | } | ||
| 451 | 1; |