| 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::TkUI 1.382; | ||
| 11 | |||
| 12 | use 5.20.1; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use Carp; | ||
| 16 | |||
| 17 | use feature qw/postderef signatures/; | ||
| 18 | no warnings qw/experimental::postderef experimental::signatures/; | ||
| 19 | |||
| 20 | use base qw/Tk::Toplevel/; | ||
| 21 | use vars qw/$icon_path $error_img $warn_img/; | ||
| 22 | use subs qw/menu_struct/; | ||
| 23 | use Scalar::Util qw/weaken/; | ||
| 24 | use Log::Log4perl 1.11; | ||
| 25 | use Path::Tiny; | ||
| 26 | use YAML::PP; | ||
| 27 | use File::HomeDir; | ||
| 28 | |||
| 29 | use Pod::POM; | ||
| 30 | use Pod::POM::View::Text; | ||
| 31 | |||
| 32 | use Tk::DoubleClick; | ||
| 33 | |||
| 34 | use Tk::Balloon; | ||
| 35 | use Tk::Photo; | ||
| 36 | use Tk::PNG; # required for Tk::Photo to be able to load pngs | ||
| 37 | use Tk::Adjuster; | ||
| 38 | use Tk::FontDialog; | ||
| 39 | |||
| 40 | use Tk::Pod; | ||
| 41 | use Tk::Pod::Text; # for findpod | ||
| 42 | |||
| 43 | use Config::Model 2.135; # reset config clears changes | ||
| 44 | |||
| 45 | use Config::Model::Tk::Filter qw/apply_filter/; | ||
| 46 | |||
| 47 | use Config::Model::Tk::LeafEditor; | ||
| 48 | use Config::Model::Tk::CheckListEditor; | ||
| 49 | |||
| 50 | use Config::Model::Tk::LeafViewer; | ||
| 51 | use Config::Model::Tk::CheckListViewer; | ||
| 52 | |||
| 53 | use Config::Model::Tk::ListViewer; | ||
| 54 | use Config::Model::Tk::ListEditor; | ||
| 55 | |||
| 56 | use Config::Model::Tk::HashViewer; | ||
| 57 | use Config::Model::Tk::HashEditor; | ||
| 58 | |||
| 59 | use Config::Model::Tk::NodeViewer; | ||
| 60 | use Config::Model::Tk::NodeEditor; | ||
| 61 | |||
| 62 | use Config::Model::Tk::Wizard; | ||
| 63 | use Config::Model::Tk::CmeDialog; | ||
| 64 | |||
| 65 | Construct Tk::Widget 'ConfigModelUI'; | ||
| 66 | |||
| 67 | my $cust_img; | ||
| 68 | my $tool_img; | ||
| 69 | my %gnome_img; | ||
| 70 | |||
| 71 | my $mod_file = 'Config/Model/TkUI.pm'; | ||
| 72 | $icon_path = $INC{$mod_file}; | ||
| 73 | $icon_path =~ s/TkUI.pm//; | ||
| 74 | $icon_path .= 'Tk/icons/'; | ||
| 75 | |||
| 76 | my $logger = Log::Log4perl::get_logger('TkUI'); | ||
| 77 | |||
| 78 | no warnings "redefine"; | ||
| 79 | |||
| 80 | sub Tk::Error { | ||
| 81 | my ( $cw, $error, @locations ) = @_; | ||
| 82 | my $msg = ( ref($error) && $error->can('as_string') ) ? $error->as_string : $error; | ||
| 83 | warn $msg; | ||
| 84 | $msg .= "Tk stack: \n@locations\n"; | ||
| 85 | $cw->CmeDialog( | ||
| 86 | -title => 'Config::Model error', | ||
| 87 | -text => $msg, | ||
| 88 | )->Show; | ||
| 89 | } | ||
| 90 | |||
| 91 | use warnings "redefine"; | ||
| 92 | |||
| 93 | my $default_config = { | ||
| 94 | font => { -family => 'DejaVu Sans', qw/-size -13 -weight normal/ } | ||
| 95 | }; | ||
| 96 | |||
| 97 | my $main_window; | ||
| 98 | my $home_str = File::HomeDir->my_home || '/tmp/'; | ||
| 99 | my $config_path = path($home_str)->child('.cme/config/'); | ||
| 100 | my $config_file = $config_path->child('tkui.yml'); | ||
| 101 | my $ypp = YAML::PP->new; | ||
| 102 | |||
| 103 | $config_path -> mkpath; | ||
| 104 | |||
| 105 | my $config = $config_file->is_file ? $ypp->load_file($config_file) : $default_config ; | ||
| 106 | |||
| 107 | # Tk::CmdLine::SetArguments( -font => $config->{font} ) ; | ||
| 108 | |||
| 109 | sub ClassInit { | ||
| 110 | my ( $class, $mw ) = @_; | ||
| 111 | $main_window = $mw; | ||
| 112 | # ClassInit is often used to define bindings and/or other | ||
| 113 | # resources shared by all instances, e.g., images. | ||
| 114 | |||
| 115 | # cw->Advertise(name=>$widget); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub set_font { | ||
| 119 | my $cw = shift; | ||
| 120 | |||
| 121 | my $tk_font = $main_window->FontDialog->Show; | ||
| 122 | if (defined $tk_font) { | ||
| 123 | $main_window->RefontTree(-font => $tk_font); | ||
| 124 | $config->{font} = {$tk_font->actual} ; | ||
| 125 | $cw->ConfigSpecs( -font => ['DESCENDANTS', 'font','Font', $tk_font ]); | ||
| 126 | $ypp->dump_file($config_file->stringify, $config); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | sub Populate { | ||
| 131 | my ( $cw, $args ) = @_; | ||
| 132 | |||
| 133 | unless ( defined $error_img ) { | ||
| 134 | $error_img = $cw->Photo( -file => $icon_path . 'stop.png' ); | ||
| 135 | $cust_img = $cw->Photo( -file => $icon_path . 'next.png' ); | ||
| 136 | |||
| 137 | # snatched from oxygen-icon-theme | ||
| 138 | $warn_img = $cw->Photo( -file => $icon_path . 'dialog-warning.png' ); | ||
| 139 | |||
| 140 | # snatched from openclipart-png | ||
| 141 | $tool_img = $cw->Photo( -file => $icon_path . 'tools_nicu_buculei_01.png' ); | ||
| 142 | |||
| 143 | # snatched from gnome gnome-icon-theme package | ||
| 144 | foreach my $img_name (qw/next previous window-close gtk-execute/) { | ||
| 145 | $gnome_img{$img_name} = $cw->Photo( | ||
| 146 | -file => $icon_path . "gnome-$img_name.png" | ||
| 147 | ); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | if ($args->{-root}) { | ||
| 152 | carp "TkUI: -root parameter is deprecated in favor of -instance"; | ||
| 153 | my $root = delete $args->{-root}; | ||
| 154 | $cw->{instance} = $root->instance; | ||
| 155 | } | ||
| 156 | |||
| 157 | $cw->{instance} //= delete $args->{-instance}; | ||
| 158 | |||
| 159 | foreach my $parm (qw/-store_sub -quit/) { | ||
| 160 | my $attr = $parm; | ||
| 161 | $attr =~ s/^-//; | ||
| 162 | $cw->{$attr} = delete $args->{$parm}; | ||
| 163 | } | ||
| 164 | |||
| 165 | my $extra_menu = delete $args->{'-extra-menu'} || []; | ||
| 166 | |||
| 167 | my $title = delete $args->{'-title'} | ||
| 168 | || $0 . " " . $cw->{instance}->config_root->config_class_name; | ||
| 169 | |||
| 170 | # check unknown parameters | ||
| 171 | croak "Unknown parameter ", join( ' ', keys %$args ) if %$args; | ||
| 172 | |||
| 173 | # initialize internal attributes | ||
| 174 | $cw->{location} = ''; | ||
| 175 | $cw->{current_mode} = 'view'; | ||
| 176 | |||
| 177 | $cw->setup_scanner(); | ||
| 178 | |||
| 179 | # create top menu | ||
| 180 | require Tk::Menubutton; | ||
| 181 | my $menubar = $cw->Menu; | ||
| 182 | $cw->configure( -menu => $menubar ); | ||
| 183 | $cw->{my_menu} = $menubar; | ||
| 184 | |||
| 185 | $cw->add_file_menu($menubar, $extra_menu); | ||
| 186 | $cw->add_help_menu($menubar); | ||
| 187 | |||
| 188 | $cw->bind( '<Control-s>', sub { $cw->save } ); | ||
| 189 | $cw->bind( '<Control-q>', sub { $cw->quit } ); | ||
| 190 | $cw->bind( '<Control-c>', sub { $cw->edit_copy } ); | ||
| 191 | $cw->bind( '<Control-v>', sub { $cw->edit_paste } ); | ||
| 192 | $cw->bind( '<Control-f>', sub { $cw->pack_find_widget } ); | ||
| 193 | |||
| 194 | $cw->add_edit_menu($menubar); | ||
| 195 | |||
| 196 | my $history_menu = $menubar->cascade(-label => 'History'); | ||
| 197 | |||
| 198 | my $option_menu = $menubar->cascade( -label => 'Options'); | ||
| 199 | $option_menu->command( -label => 'Font', -command => sub { $cw->set_font(); }); | ||
| 200 | |||
| 201 | # create 'hide empty values' | ||
| 202 | $cw->{hide_empty_values} = 0; | ||
| 203 | $option_menu->checkbutton( | ||
| 204 | -label => "Hide empty values", | ||
| 205 | -variable => \$cw->{hide_empty_values}, | ||
| 206 | -command => sub { $cw->reload($cw->{location}) }, | ||
| 207 | ); | ||
| 208 | |||
| 209 | # create 'show only custom values' | ||
| 210 | $cw->{show_only_custom} = 0; | ||
| 211 | $option_menu->checkbutton( | ||
| 212 | -label => 'Show only custom values', | ||
| 213 | -variable => \$cw->{show_only_custom}, | ||
| 214 | -command => sub { $cw->reload($cw->{location}) }, | ||
| 215 | ); | ||
| 216 | |||
| 217 | # create 'show only custom values' | ||
| 218 | $cw->{auto_save_mode} = 0; | ||
| 219 | $option_menu->checkbutton( | ||
| 220 | -label => 'Auto save', | ||
| 221 | -variable => \$cw->{auto_save_mode}, | ||
| 222 | ); | ||
| 223 | |||
| 224 | my $weak_cw = $cw; | ||
| 225 | weaken($weak_cw); | ||
| 226 | $cw->{instance}->on_change_cb( sub { | ||
| 227 | $weak_cw->save if $weak_cw->{auto_save_mode};; | ||
| 228 | }); | ||
| 229 | |||
| 230 | # create frame for location entry | ||
| 231 | my $loc_frame = $cw->Frame( -relief => 'sunken', -borderwidth => 1 ) | ||
| 232 | ->pack( -pady => 0, -fill => 'x' ); | ||
| 233 | $cw->{path_history} = []; | ||
| 234 | $cw->{path_index} = 0; | ||
| 235 | |||
| 236 | # add button | ||
| 237 | my $previous_btn = $loc_frame->Button ( | ||
| 238 | -image => $gnome_img{'previous'}, | ||
| 239 | -state => 'disabled', | ||
| 240 | -command => sub { $cw->go_to_previous();}, | ||
| 241 | ); | ||
| 242 | $previous_btn->pack(-side => 'left'); | ||
| 243 | my $next_btn = $loc_frame->Button ( | ||
| 244 | -image => $gnome_img{'next'}, | ||
| 245 | -state => 'disabled', | ||
| 246 | -command => sub { $cw->go_to_next();}, | ||
| 247 | ); | ||
| 248 | $next_btn->pack(-side => 'left'); | ||
| 249 | |||
| 250 | $loc_frame->Label( -text => 'location :' )->pack( -side => 'left' ); | ||
| 251 | $loc_frame->Label( -textvariable => \$cw->{location} )->pack( -side => 'left' ); | ||
| 252 | |||
| 253 | # add bottom frame | ||
| 254 | my $bottom_frame = $cw->Frame->pack(qw/-pady 0 -fill both -expand 1/); | ||
| 255 | my $tree_frame = $bottom_frame->Frame->pack(qw/-fill both -expand 1 -side left/); | ||
| 256 | my $filter_frame = $tree_frame->Frame->pack(qw/-fill x -side top/); | ||
| 257 | |||
| 258 | # create the widget for tree navigation | ||
| 259 | require Tk::Tree; | ||
| 260 | my $tree = $tree_frame->Scrolled( | ||
| 261 | qw/Tree/, | ||
| 262 | -columns => 4, | ||
| 263 | -header => 1, | ||
| 264 | -opencmd => sub { $cw->open_item(@_); }, | ||
| 265 | -closecmd => sub { $cw->close_item(@_); }, | ||
| 266 | )->pack(qw/-fill both -expand 1 -side bottom/); | ||
| 267 | |||
| 268 | $cw->{tktree} = $tree; | ||
| 269 | |||
| 270 | my $sub_filter = sub { | ||
| 271 | $cw->reload; | ||
| 272 | }; | ||
| 273 | my $clear_filter = sub { | ||
| 274 | $cw->{elt_filter_value} = ''; | ||
| 275 | $cw->reload; | ||
| 276 | }; | ||
| 277 | my $reload_on_key = sub { | ||
| 278 | $cw->reload; | ||
| 279 | }; | ||
| 280 | |||
| 281 | my $filter_clear = $filter_frame->Button ( | ||
| 282 | -image => $gnome_img{'window-close'}, | ||
| 283 | -command => $clear_filter | ||
| 284 | ); | ||
| 285 | $cw->Balloon(-state => 'balloon')->attach($filter_clear, -msg => 'clear filter'); | ||
| 286 | $filter_clear->pack(-side => 'right'); | ||
| 287 | $filter_frame->Label(-text => 'filter elements',)->pack(-side => 'left'); | ||
| 288 | |||
| 289 | $cw->{elt_filter_value} = ''; | ||
| 290 | my $element_filter_w = $filter_frame->Entry( | ||
| 291 | -textvariable => \$cw->{elt_filter_value}, | ||
| 292 | ); | ||
| 293 | $cw->Balloon(-state => 'balloon')->attach( | ||
| 294 | $element_filter_w, | ||
| 295 | -msg => 'define a filter applied to element name. At least 3 character long.' | ||
| 296 | .' This can be a Perl regexp.'); | ||
| 297 | $element_filter_w->pack(qw/-side right -fill x -expand 1/); | ||
| 298 | $element_filter_w->bind('<KeyRelease>', $reload_on_key); | ||
| 299 | |||
| 300 | # add adjuster | ||
| 301 | $bottom_frame->Adjuster()->packAfter( $tree_frame, -side => 'left' ); | ||
| 302 | |||
| 303 | # add headers | ||
| 304 | $tree->headerCreate( 0, -text => "element" ); | ||
| 305 | $tree->headerCreate( 1, -text => "status" ); | ||
| 306 | $tree->headerCreate( 2, -text => "value" ); | ||
| 307 | $tree->headerCreate( 3, -text => "standard value" ); | ||
| 308 | |||
| 309 | $cw->reload; | ||
| 310 | |||
| 311 | # add frame on the right for entry and help | ||
| 312 | my $eh_frame = $bottom_frame->Frame->pack(qw/-fill both -expand 1 -side right/); | ||
| 313 | |||
| 314 | # add entry frame, filled by call-back | ||
| 315 | # should be a composite widget | ||
| 316 | my $e_frame = $eh_frame->Frame->pack(qw/-side top -fill both -expand 1/); | ||
| 317 | $e_frame->Label( #-text => "placeholder", | ||
| 318 | -image => $tool_img, | ||
| 319 | -width => 400, # width in pixel for image | ||
| 320 | )->pack( -side => 'top' ); | ||
| 321 | $e_frame->Button( | ||
| 322 | -text => "Run Wizard !", | ||
| 323 | -command => sub { $cw->wizard } )->pack( -side => 'bottom' ); | ||
| 324 | |||
| 325 | my $b1_sub = sub { | ||
| 326 | my $item = $tree->nearest( $tree->pointery - $tree->rooty ); | ||
| 327 | $cw->on_browse($item); | ||
| 328 | }; | ||
| 329 | my $b3_sub = sub { | ||
| 330 | my $item = $tree->nearest( $tree->pointery - $tree->rooty ); | ||
| 331 | $cw->on_select($item); | ||
| 332 | }; | ||
| 333 | |||
| 334 | $tree->bind( '<Return>', $b3_sub ); | ||
| 335 | $tree->bind( '<ButtonRelease-3>', $b3_sub ); | ||
| 336 | bind_clicks($tree,$b1_sub, $b3_sub); | ||
| 337 | |||
| 338 | # bind button2 to get cut buffer content and try to store cut buffer content | ||
| 339 | my $b2_sub = sub { | ||
| 340 | my $item = $tree->nearest( $tree->pointery - $tree->rooty ); | ||
| 341 | $cw->on_cut_buffer_dump($item); | ||
| 342 | }; | ||
| 343 | $tree->bind( '<ButtonRelease-2>', $b2_sub ); | ||
| 344 | |||
| 345 | $tree->bind( '<Control-c>', sub { $cw->edit_copy } ); | ||
| 346 | $tree->bind( '<Control-v>', sub { $cw->edit_paste } ); | ||
| 347 | $tree->bind( '<Control-f>', sub { $cw->pack_find_widget } ); | ||
| 348 | |||
| 349 | my $find_frame = $cw->create_find_widget; | ||
| 350 | |||
| 351 | # create frame for message | ||
| 352 | my $msg_label = $cw->Label( | ||
| 353 | -textvariable => \$cw->{message}, | ||
| 354 | -relief => 'sunken', | ||
| 355 | -borderwidth => 1, | ||
| 356 | -anchor =>'w', | ||
| 357 | ); | ||
| 358 | $msg_label->pack( -pady => 0, -fill => 'x' ); | ||
| 359 | |||
| 360 | $args->{-title} = $title; | ||
| 361 | $cw->SUPER::Populate($args); | ||
| 362 | |||
| 363 | my $tk_font = $cw->Font(%{$config->{font}}); | ||
| 364 | $cw->ConfigSpecs( | ||
| 365 | -font => ['DESCENDANTS', 'font','Font', $tk_font ], | ||
| 366 | #-background => ['DESCENDANTS', 'background', 'Background', $background], | ||
| 367 | #-selectbackground => [$hlist, 'selectBackground', 'SelectBackground', | ||
| 368 | # $selectbackground], | ||
| 369 | -tree_width => [ 'METHOD', undef, undef, 80 ], | ||
| 370 | -tree_height => [ 'METHOD', undef, undef, 30 ], | ||
| 371 | -width => [ $eh_frame, qw/width Width 1280/ ], | ||
| 372 | -height => [ $eh_frame, qw/height Height 1024/ ], | ||
| 373 | -selectmode => [ $tree, 'selectMode', 'SelectMode', 'single' ], #single', | ||
| 374 | #-oldcursor => [$hlist, undef, undef, undef], | ||
| 375 | DEFAULT => [$tree] ); | ||
| 376 | |||
| 377 | $cw->Advertise( tree => $tree ); | ||
| 378 | $cw->Advertise( menubar => $menubar ); | ||
| 379 | $cw->Advertise( history => $history_menu ); | ||
| 380 | $cw->Advertise( right_frame => $eh_frame ); | ||
| 381 | $cw->Advertise( ed_frame => $e_frame ); | ||
| 382 | $cw->Advertise( find_frame => $find_frame ); | ||
| 383 | $cw->Advertise( msg_label => $msg_label ); | ||
| 384 | $cw->Advertise( prev_btn => $previous_btn ); | ||
| 385 | $cw->Advertise( next_btn => $next_btn ); | ||
| 386 | |||
| 387 | $cw->OnDestroy(sub {$cw->Parent->destroy if ref($cw->Parent) eq 'MainWindow'} ); | ||
| 388 | |||
| 389 | $cw->Delegates; | ||
| 390 | } | ||
| 391 | |||
| 392 | sub show_message { | ||
| 393 | my ( $cw, $msg ) = @_; | ||
| 394 | # $cw->Subwidget('msg_label')->configure(-background => "red"); # does not work | ||
| 395 | $cw->{message} = $msg; | ||
| 396 | |||
| 397 | if (my $id = $cw->{id}) { | ||
| 398 | $cw->afterCancel($id) ; | ||
| 399 | } ; | ||
| 400 | |||
| 401 | my $unshow = sub { | ||
| 402 | delete $cw->{id}; | ||
| 403 | $cw->{message} = ''; | ||
| 404 | } ; | ||
| 405 | $cw->{id} = $cw->after(5000,$unshow) ; | ||
| 406 | } | ||
| 407 | |||
| 408 | sub tree_width { | ||
| 409 | my ( $cw, $value ) = @_; | ||
| 410 | $cw->Subwidget('tree')->configure( -width => $value ); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub tree_height { | ||
| 414 | my ( $cw, $value ) = @_; | ||
| 415 | $cw->Subwidget('tree')->configure( -height => $value ); | ||
| 416 | } | ||
| 417 | |||
| 418 | my $parser = Pod::POM->new(); | ||
| 419 | |||
| 420 | # parse from my documentation | ||
| 421 | my $pom = $parser->parse_file(__FILE__) | ||
| 422 | || die $parser->error(); | ||
| 423 | |||
| 424 | my $help_text; | ||
| 425 | my $info_text; | ||
| 426 | foreach my $head1 ( $pom->head1() ) { | ||
| 427 | $help_text = Pod::POM::View::Text->view_head1($head1) | ||
| 428 | if $head1->title eq 'USAGE'; | ||
| 429 | $info_text = Pod::POM::View::Text->view_head1($head1) | ||
| 430 | if $head1->title =~ /more information/i; | ||
| 431 | |||
| 432 | } | ||
| 433 | |||
| 434 | sub add_file_menu($cw, $menubar, $extra_menu) { | ||
| 435 | my $file_items = [ | ||
| 436 | [ qw/command wizard -command/, sub { $cw->wizard } ], | ||
| 437 | [ command => 'redraw tree', -command => sub { $cw->reload } ], | ||
| 438 | [ command => 'reload from file', -command => sub { $cw->ask_reset; } ], | ||
| 439 | [ command => 'check for errors', -command => sub { $cw->check(1) } ], | ||
| 440 | [ command => 'check for warnings', -command => sub { $cw->check( 1, 1 ) } ], | ||
| 441 | [ command => 'show unsaved changes', -command => sub { $cw->show_changes; } ], | ||
| 442 | [ command => 'save (Ctrl-s)', -command => sub { $cw->save } ], | ||
| 443 | @$extra_menu, | ||
| 444 | [ | ||
| 445 | command => 'debug ...', | ||
| 446 | -command => sub { | ||
| 447 | require Tk::ObjScanner; | ||
| 448 | Tk::ObjScanner::scan_object( $cw->{instance}->config_root ); | ||
| 449 | } | ||
| 450 | ], | ||
| 451 | [ command => 'quit (Ctrl-q)', -command => sub { $cw->quit } ], | ||
| 452 | ]; | ||
| 453 | $menubar->cascade( -label => 'File', -menuitems => $file_items ); | ||
| 454 | |||
| 455 | return; | ||
| 456 | } | ||
| 457 | |||
| 458 | sub add_help_menu { | ||
| 459 | my ( $cw, $menubar ) = @_; | ||
| 460 | |||
| 461 | my $about_sub = sub { | ||
| 462 | $cw->Dialog( | ||
| 463 | -title => 'About', | ||
| 464 | -text => "Config::Model::TkUI \n" | ||
| 465 | . "(c) 2008-2021 Dominique Dumont \n" | ||
| 466 | . "Licensed under LGPLv2\n" | ||
| 467 | )->Show; | ||
| 468 | }; | ||
| 469 | |||
| 470 | my $info_sub = sub { | ||
| 471 | $cw->CmeDialog( | ||
| 472 | -title => 'TODO', | ||
| 473 | -text => $info_text | ||
| 474 | )->Show; | ||
| 475 | }; | ||
| 476 | |||
| 477 | my $help_sub = sub { | ||
| 478 | $cw->CmeDialog( | ||
| 479 | -title => 'help', | ||
| 480 | -text => $help_text | ||
| 481 | )->Show; | ||
| 482 | }; | ||
| 483 | |||
| 484 | my $class = $cw->{instance}->config_root->config_class_name; | ||
| 485 | my $man_sub = sub { | ||
| 486 | $cw->Pod( | ||
| 487 | -tree => 0, | ||
| 488 | -file => "Config::Model::models::" . $class, | ||
| 489 | -title => $class, | ||
| 490 | -exitbutton => 0, | ||
| 491 | ); | ||
| 492 | }; | ||
| 493 | |||
| 494 | my $help_items = [ | ||
| 495 | [ qw/command About -command/, $about_sub ], | ||
| 496 | [ qw/command Usage -command/, $help_sub ], | ||
| 497 | [ command => 'More info', -command => $info_sub ], | ||
| 498 | [ command => "$class help", -command => $man_sub ], | ||
| 499 | ]; | ||
| 500 | $menubar->cascade( -label => 'Help', -menuitems => $help_items ); | ||
| 501 | } | ||
| 502 | |||
| 503 | sub add_edit_menu($cw, $menubar) { | ||
| 504 | my $edit_items = [ | ||
| 505 | # [ qw/command cut -command/, sub{ $cw->edit_cut }], | ||
| 506 | [ command => 'copy (Ctrl-c)', '-command', sub { $cw->edit_copy } ], | ||
| 507 | [ command => 'paste (Ctrl-v)', '-command', sub { $cw->edit_paste } ], | ||
| 508 | [ command => 'find (Ctrl-f)', '-command', sub { $cw->pack_find_widget; } ], | ||
| 509 | ]; | ||
| 510 | $menubar->cascade( -label => 'Edit', -menuitems => $edit_items ); | ||
| 511 | return; | ||
| 512 | } | ||
| 513 | |||
| 514 | # Note: this callback is called by Tk::Tree *before* changing the | ||
| 515 | # indicator. And the indicator is used by Tk::Tree to store the | ||
| 516 | # open/close/none mode. So we can't rely on getmode for path that are | ||
| 517 | # opening. Hence the parameter passed to the sub stored with each | ||
| 518 | # Tk::Tree item | ||
| 519 | sub open_item { | ||
| 520 | my ( $cw, $path ) = @_; | ||
| 521 | my $tktree = $cw->{tktree}; | ||
| 522 | $logger->trace("open_item on $path"); | ||
| 523 | my $data = $tktree->infoData($path); | ||
| 524 | |||
| 525 | # invoke the scanner part (to create children) | ||
| 526 | # the parameter indicates that we are opening this path | ||
| 527 | $data->[0]->(1); | ||
| 528 | |||
| 529 | $cw->show_single_list_value ($tktree, $data->[1], $path, 0); | ||
| 530 | |||
| 531 | my @children = $tktree->infoChildren($path); | ||
| 532 | $logger->trace("open_item show @children"); | ||
| 533 | map { $tktree->show( -entry => $_ ); } @children; | ||
| 534 | } | ||
| 535 | |||
| 536 | sub close_item { | ||
| 537 | my ( $cw, $path ) = @_; | ||
| 538 | my $tktree = $cw->{tktree}; | ||
| 539 | $logger->trace("close_item on $path"); | ||
| 540 | my $data = $tktree->infoData($path); | ||
| 541 | |||
| 542 | $cw->show_single_list_value ($tktree, $data->[1], $path, 1); | ||
| 543 | |||
| 544 | my @children = $tktree->infoChildren($path); | ||
| 545 | $logger->trace("close_item hide @children"); | ||
| 546 | map { $tktree->hide( -entry => $_ ); } @children; | ||
| 547 | } | ||
| 548 | |||
| 549 | sub check { | ||
| 550 | my $cw = shift; | ||
| 551 | my $show = shift || 0; | ||
| 552 | my $check_warnings = shift || 0; | ||
| 553 | |||
| 554 | my $wiz = $cw->setup_wizard( sub { $cw->check_end( $show, @_ ); } ); | ||
| 555 | |||
| 556 | $wiz->start_wizard( stop_on_warning => $check_warnings ); | ||
| 557 | } | ||
| 558 | |||
| 559 | sub check_end { | ||
| 560 | my $cw = shift; | ||
| 561 | my $show = shift; | ||
| 562 | my $has_stopped = shift; | ||
| 563 | |||
| 564 | $cw->reload if $has_stopped; | ||
| 565 | |||
| 566 | if ( $show and not $has_stopped ) { | ||
| 567 | $cw->Dialog( | ||
| 568 | -title => 'Check', | ||
| 569 | -text => "No issue found" | ||
| 570 | )->Show; | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | sub save { | ||
| 575 | my $cw = shift; | ||
| 576 | my $cb = shift || sub {}; | ||
| 577 | |||
| 578 | my $dir = $cw->{save_dir}; | ||
| 579 | my $trace_dir = defined $dir ? $dir : 'default'; | ||
| 580 | my @wb_args = defined $dir ? ( config_dir => $dir ) : (); | ||
| 581 | |||
| 582 | my $save_job = sub { | ||
| 583 | $cw->check(); # may be long | ||
| 584 | |||
| 585 | if ( defined $cw->{store_sub} ) { | ||
| 586 | $logger->info("Saving data in $trace_dir directory with store call-back"); | ||
| 587 | eval { $cw->{store_sub}->($dir) }; | ||
| 588 | } | ||
| 589 | else { | ||
| 590 | $logger->info("Saving data in $trace_dir directory with instance write_back"); | ||
| 591 | eval { $cw->{instance}->write_back(@wb_args); }; | ||
| 592 | } | ||
| 593 | |||
| 594 | if ($@) { | ||
| 595 | my $err = $@ ; | ||
| 596 | my $answer = $cw->CmeDialog( | ||
| 597 | -title => 'Save error', | ||
| 598 | -text => "Cannot save: $err", | ||
| 599 | -buttons => [ qw/quit cancel/ ], | ||
| 600 | -default_button => 'cancel', | ||
| 601 | )->Show; | ||
| 602 | $cb->($err) if $answer eq 'quit'; # indicate failure | ||
| 603 | } | ||
| 604 | else { | ||
| 605 | $cw->show_message("Save done ..."); | ||
| 606 | $cb->(); | ||
| 607 | } | ||
| 608 | }; | ||
| 609 | |||
| 610 | $cw->show_message("Saving... please wait ..."); | ||
| 611 | |||
| 612 | # use a short delay to let tk show the message above and then save | ||
| 613 | $cw->after(100, $save_job) ; | ||
| 614 | |||
| 615 | } | ||
| 616 | |||
| 617 | sub ask_reset { | ||
| 618 | my $text = "Discard changes and reload from file ?"; | ||
| 619 | my $cw = shift; | ||
| 620 | |||
| 621 | if ( $cw->{instance}->needs_save ) { | ||
| 622 | my $answer = $cw->Dialog( | ||
| 623 | -title => "reload from file", | ||
| 624 | -text => $text, | ||
| 625 | -buttons => [ qw/yes cancel/, 'show changes' ], | ||
| 626 | -default_button => 'yes', | ||
| 627 | )->Show; | ||
| 628 | |||
| 629 | if ( $answer eq 'yes' ) { | ||
| 630 | $cw->do_reset; | ||
| 631 | } | ||
| 632 | elsif ( $answer =~ /show/ ) { | ||
| 633 | $cw->show_changes( sub { $cw->ask_reset } ); | ||
| 634 | } | ||
| 635 | } | ||
| 636 | else { | ||
| 637 | $cw->do_reset; | ||
| 638 | } | ||
| 639 | } | ||
| 640 | |||
| 641 | sub do_reset { | ||
| 642 | my $cw = shift; | ||
| 643 | $cw->{instance}->reset_config; | ||
| 644 | $cw->reload; | ||
| 645 | } | ||
| 646 | |||
| 647 | sub quit { | ||
| 648 | my $cw = shift; | ||
| 649 | my $text = shift || "Save data ?"; | ||
| 650 | |||
| 651 | if ( $cw->{instance}->needs_save ) { | ||
| 652 | my $answer = $cw->Dialog( | ||
| 653 | -title => "quit", | ||
| 654 | -text => $text, | ||
| 655 | -buttons => [ qw/yes no cancel/, 'show changes' ], | ||
| 656 | -default_button => 'yes', | ||
| 657 | )->Show; | ||
| 658 | |||
| 659 | if ( $answer eq 'yes' ) { | ||
| 660 | $cw->save( sub {$cw->self_destroy;}); | ||
| 661 | } | ||
| 662 | elsif ( $answer eq 'no' ) { | ||
| 663 | $cw->self_destroy; | ||
| 664 | } | ||
| 665 | elsif ( $answer =~ /show/ ) { | ||
| 666 | $cw->show_changes( sub { $cw->quit } ); | ||
| 667 | } | ||
| 668 | } | ||
| 669 | else { | ||
| 670 | $cw->self_destroy; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | |||
| 675 | sub self_destroy { | ||
| 676 | my $cw = shift; | ||
| 677 | |||
| 678 | if ( defined $cw->{quit} and $cw->{quit} eq 'soft' ) { | ||
| 679 | $cw->destroy; | ||
| 680 | } | ||
| 681 | else { | ||
| 682 | # destroy main window to exit Tk Mainloop; | ||
| 683 | $cw->parent->destroy; | ||
| 684 | } | ||
| 685 | } | ||
| 686 | |||
| 687 | sub show_changes { | ||
| 688 | my $cw = shift; | ||
| 689 | my $cb = shift; | ||
| 690 | |||
| 691 | my $changes = $cw->{instance}->list_changes; | ||
| 692 | my $change_widget = $cw->Toplevel; | ||
| 693 | $change_widget->Scrolled('ROText')->pack( -expand => 1, -fill => 'both' ) | ||
| 694 | ->insert( '1.0', $changes ); | ||
| 695 | $change_widget->Button( | ||
| 696 | -command => sub { $change_widget->destroy; $cb->() if defined $cb; }, | ||
| 697 | -text => 'ok', | ||
| 698 | )->pack; | ||
| 699 | } | ||
| 700 | |||
| 701 | sub reload { | ||
| 702 | my $cw = shift; | ||
| 703 | carp "reload: too many parameters" if @_ > 1; | ||
| 704 | my $force_display_path = shift // ''; # force open editor on this path | ||
| 705 | |||
| 706 | $logger->trace( "reloading tk tree" | ||
| 707 | . ( defined $force_display_path ? " (force display $force_display_path)" : '' ) ); | ||
| 708 | |||
| 709 | my $actions = $cw->{cm_actions} //= {}; | ||
| 710 | |||
| 711 | # eval is required to trap bad regexp entered in filter widget | ||
| 712 | my %filter_args = map { ($_ => $cw->{$_} // '') } | ||
| 713 | qw/elt_filter_value show_only_custom hide_empty_values instance/ ; | ||
| 714 | |||
| 715 | eval { | ||
| 716 | apply_filter(actions => $actions, fd_path => $force_display_path, %filter_args); | ||
| 717 | }; | ||
| 718 | if ($@) { | ||
| 719 | my $msg = $@; | ||
| 720 | say "filter error: $msg"; | ||
| 721 | $msg =~ s/at lib.*//s; # remove file from error message | ||
| 722 | $cw->show_message("filter error: $msg"); | ||
| 723 | } | ||
| 724 | |||
| 725 | my $tree = $cw->{tktree}; | ||
| 726 | |||
| 727 | my $instance_name = $cw->{instance}->name; | ||
| 728 | my $root = $cw->{instance}->config_root; | ||
| 729 | |||
| 730 | my $new_drawing = not $tree->infoExists($instance_name); | ||
| 731 | |||
| 732 | my $scan_root = sub { | ||
| 733 | my $opening = shift ; | ||
| 734 | $tree->itemConfigure($instance_name, 2, -text => $root->fetch_gist); | ||
| 735 | $cw->{scanner}->scan_node( [ $instance_name, $cw, $opening, $actions, $force_display_path ], $root ); | ||
| 736 | }; | ||
| 737 | |||
| 738 | if ($new_drawing) { | ||
| 739 | $tree->add( $instance_name, -data => [ $scan_root, $root ] ); | ||
| 740 | $tree->itemCreate( $instance_name, 0, -text => $instance_name, ); | ||
| 741 | $tree->itemCreate( $instance_name, 2, -text => '' ); | ||
| 742 | $tree->setmode( $instance_name, 'close' ); | ||
| 743 | $tree->open($instance_name); | ||
| 744 | } | ||
| 745 | |||
| 746 | # the first parameter indicates that we are opening the root | ||
| 747 | $scan_root->( 1 ); | ||
| 748 | $cw->{editor}->reload if defined $cw->{editor}; | ||
| 749 | } | ||
| 750 | |||
| 751 | # call-back when Tree element is selected | ||
| 752 | sub on_browse { | ||
| 753 | my ( $cw, $path ) = @_; | ||
| 754 | $cw->update_loc_bar($path); | ||
| 755 | $cw->update_history($path); | ||
| 756 | $cw->create_element_widget('view'); | ||
| 757 | } | ||
| 758 | |||
| 759 | sub update_loc_bar { | ||
| 760 | my ( $cw, $path ) = @_; | ||
| 761 | |||
| 762 | #$cw->{path}=$path ; | ||
| 763 | my $datar = $cw->{tktree}->infoData($path); | ||
| 764 | my $obj = $datar->[1]; | ||
| 765 | my $loc = $cw->{location} = $obj->location_short; | ||
| 766 | return $loc; | ||
| 767 | } | ||
| 768 | |||
| 769 | sub update_history ($cw, $path) { | ||
| 770 | my $history = $cw->{path_history}; | ||
| 771 | |||
| 772 | # avoid consecutive duplicated entries | ||
| 773 | if ($history->@* > 1 and $path eq $history->[-1]) { | ||
| 774 | return; | ||
| 775 | } | ||
| 776 | |||
| 777 | push $history->@*, $path; | ||
| 778 | my $path_idx = $cw->{path_index} = $history->$#*; | ||
| 779 | |||
| 780 | # enable previous button when history has more than one item | ||
| 781 | $cw->Subwidget('prev_btn')->configure(-state => $path_idx > 0 ? 'normal' : 'disabled'); | ||
| 782 | |||
| 783 | my $h_cascade = $cw->Subwidget('history'); | ||
| 784 | |||
| 785 | my $max_count = 20; | ||
| 786 | $cw->{history_count} //= 0; | ||
| 787 | |||
| 788 | # remove oldest entry | ||
| 789 | if ($cw->{history_count}++ > $max_count) { | ||
| 790 | $h_cascade->menu->delete('end'); | ||
| 791 | } | ||
| 792 | |||
| 793 | # add newest entry | ||
| 794 | $h_cascade->command( | ||
| 795 | -label => $cw->{tktree}->infoData($path)->[1]->location_short, | ||
| 796 | -command => sub { $cw->go_to_loc($path_idx); } | ||
| 797 | ); | ||
| 798 | |||
| 799 | return; | ||
| 800 | } | ||
| 801 | |||
| 802 | sub go_to_loc ($cw, $idx) { | ||
| 803 | my $path = $cw->{path_history}[$idx]; | ||
| 804 | my $loc = $cw->update_loc_bar($path); | ||
| 805 | # enable previous button when history has more than one item | ||
| 806 | $cw->Subwidget('prev_btn')->configure(-state => $idx > 0 ? 'normal' : 'disabled'); | ||
| 807 | # when jumping into history, the next location does not make | ||
| 808 | # sense, hence next button is disabled | ||
| 809 | $cw->Subwidget('next_btn')->configure(-state => 'disabled'); | ||
| 810 | $cw->force_display($path, $loc); | ||
| 811 | $cw->create_element_widget('view', $path); | ||
| 812 | } | ||
| 813 | |||
| 814 | sub go_to_previous ($cw) { | ||
| 815 | my $idx = --$cw->{path_index}; | ||
| 816 | my $path = $cw->{path_history}[$idx]; | ||
| 817 | my $loc = $cw->update_loc_bar($path); | ||
| 818 | $cw->Subwidget('prev_btn')->configure(-state => $idx > 0 ? 'normal' : 'disabled'); | ||
| 819 | $cw->Subwidget('next_btn')->configure(-state => 'normal'); | ||
| 820 | $cw->force_display($path, $loc); | ||
| 821 | $cw->create_element_widget('view', $path); | ||
| 822 | } | ||
| 823 | |||
| 824 | sub go_to_next ($cw) { | ||
| 825 | my $idx = ++$cw->{path_index}; | ||
| 826 | my $path = $cw->{path_history}[$idx]; | ||
| 827 | my $history_last_idx = $cw->{path_history}->$#*; | ||
| 828 | |||
| 829 | my $loc = $cw->update_loc_bar($path); | ||
| 830 | $cw->Subwidget('prev_btn')->configure(-state => 'normal'); | ||
| 831 | $cw->Subwidget('next_btn')->configure(-state => $idx < $history_last_idx ? 'normal' : 'disabled'); | ||
| 832 | $cw->force_display($path, $loc); | ||
| 833 | $cw->create_element_widget('view', $path); | ||
| 834 | } | ||
| 835 | |||
| 836 | sub on_select { | ||
| 837 | my ( $cw, $path ) = @_; | ||
| 838 | $cw->update_loc_bar($path); | ||
| 839 | $cw->update_history($path); | ||
| 840 | $cw->create_element_widget('edit'); | ||
| 841 | } | ||
| 842 | |||
| 843 | sub on_cut_buffer_dump { | ||
| 844 | my ( $cw, $tree_path, $selection_for_test ) = @_; | ||
| 845 | $cw->update_loc_bar($tree_path); | ||
| 846 | $cw->update_history($tree_path); | ||
| 847 | |||
| 848 | # get cut buffer content, See Perl/Tk book p297 | ||
| 849 | my $sel = $selection_for_test // eval { $cw->SelectionGet; }; | ||
| 850 | |||
| 851 | return if $@; # no selection | ||
| 852 | |||
| 853 | my $obj = $cw->{tktree}->infoData($tree_path)->[1]; | ||
| 854 | my $type = $obj->get_type; | ||
| 855 | |||
| 856 | if ( $type eq "leaf" ) { | ||
| 857 | |||
| 858 | # if leaf store content | ||
| 859 | $obj->store( value => $sel, callback => sub { $cw->reload; } ); | ||
| 860 | } | ||
| 861 | elsif ( $type eq 'hash' ) { | ||
| 862 | |||
| 863 | # if hash create keys | ||
| 864 | my @keys = ( $sel =~ /\n/m ) ? split( /\n/, $sel ) : ($sel); | ||
| 865 | foreach my $key (@keys) { | ||
| 866 | $obj->fetch_with_id($key); | ||
| 867 | }; | ||
| 868 | } | ||
| 869 | elsif ( $type eq 'list') { | ||
| 870 | if ( $obj->get_cargo_type =~ /node/ ) { | ||
| 871 | $cw->show_message("cannot paste on list of node"); | ||
| 872 | } | ||
| 873 | else { | ||
| 874 | # if array, push values. Don't mix \n and , separators | ||
| 875 | my @v = | ||
| 876 | ( $sel =~ /\n/m ) ? split( /\n/, $sel ) | ||
| 877 | : ( $sel =~ /,/ ) ? split( /,/, $sel ) | ||
| 878 | : ($sel); | ||
| 879 | $obj->push(@v); | ||
| 880 | } | ||
| 881 | } | ||
| 882 | else { | ||
| 883 | $cw->show_message("cannot paste on $type parameter"); | ||
| 884 | } | ||
| 885 | |||
| 886 | # else ignore | ||
| 887 | |||
| 888 | # display result | ||
| 889 | $cw->reload; | ||
| 890 | $cw->create_element_widget($cw->{current_mode}, $tree_path); | ||
| 891 | $cw->open_item($tree_path); | ||
| 892 | $cw->{tktree}->setmode( $tree_path => 'close' ) if $type eq 'list' or $type eq 'hash'; | ||
| 893 | } | ||
| 894 | |||
| 895 | # replace dot in str by _|_ | ||
| 896 | sub to_path { my $str = shift; $str =~ s/\./_|_/g; return $str; } | ||
| 897 | |||
| 898 | sub force_element_display { | ||
| 899 | my $cw = shift; | ||
| 900 | my $elt_obj = shift; | ||
| 901 | |||
| 902 | $logger->trace( "force display of " . $elt_obj->location ); | ||
| 903 | $cw->reload( $elt_obj->location ); | ||
| 904 | } | ||
| 905 | |||
| 906 | sub prune { | ||
| 907 | my $cw = shift; | ||
| 908 | my $path = shift; | ||
| 909 | $logger->trace("prune $path"); | ||
| 910 | my %list = map { "$path." . to_path($_) => 1 } @_; | ||
| 911 | |||
| 912 | # remove entries that are not part of the list | ||
| 913 | my $tkt = $cw->{tktree}; | ||
| 914 | |||
| 915 | map { $tkt->deleteEntry($_) if $_ and not defined $list{$_}; } $tkt->infoChildren($path); | ||
| 916 | $logger->trace("prune $path done"); | ||
| 917 | } | ||
| 918 | |||
| 919 | # Beware: TkTree items store tree object and not tree cds path. These | ||
| 920 | # object might become irrelevant when warp master values are | ||
| 921 | # modified. So the whole Tk Tree layout must be redone every time a | ||
| 922 | # config value is modified. This is a bit heavy, but a smarter | ||
| 923 | # alternative would need hooks in the configuration tree to | ||
| 924 | # synchronise the Tk Tree with the configuration tree :-p | ||
| 925 | |||
| 926 | my %elt_mode = ( | ||
| 927 | leaf => 'none', | ||
| 928 | hash => 'open', | ||
| 929 | list => 'open', | ||
| 930 | node => 'open', | ||
| 931 | check_list => 'none', | ||
| 932 | warped_node => 'open', | ||
| 933 | ); | ||
| 934 | |||
| 935 | sub disp_obj_elt { | ||
| 936 | my ( $scanner, $data_ref, $node, @orig_element_list ) = @_; | ||
| 937 | my ( $path, $cw, $opening, $actions, $force_display_path ) = @$data_ref; | ||
| 938 | my $tkt = $cw->{tktree}; | ||
| 939 | my $mode = $tkt->getmode($path); | ||
| 940 | |||
| 941 | my @element_list; | ||
| 942 | foreach my $elt (@orig_element_list) { | ||
| 943 | my $obj = $node->fetch_element($elt); | ||
| 944 | my $loc = $obj->location; | ||
| 945 | my $action = $actions->{$loc} // ''; | ||
| 946 | if ($action ne 'hide') { | ||
| 947 | push @element_list, $elt; | ||
| 948 | } | ||
| 949 | } | ||
| 950 | |||
| 951 | $logger->trace( "disp_obj_elt path $path mode $mode opening $opening " . "(@element_list)" ); | ||
| 952 | |||
| 953 | $cw->prune( $path, @element_list ); | ||
| 954 | |||
| 955 | my $node_loc = $node->location; | ||
| 956 | |||
| 957 | my $prevpath = ''; | ||
| 958 | foreach my $elt (@element_list) { | ||
| 959 | my $newpath = "$path." . to_path($elt); | ||
| 960 | my $scan_sub = sub { | ||
| 961 | $scanner->scan_element( [ $newpath, $cw, $opening, $actions, $force_display_path ], $node, $elt ); | ||
| 962 | }; | ||
| 963 | my @data = ( $scan_sub, $node->fetch_element($elt) ); | ||
| 964 | |||
| 965 | # It's necessary to store a weakened reference of a tree | ||
| 966 | # object as these ones tend to disappear when warped out. In | ||
| 967 | # this case, the object must be destroyed. This does not | ||
| 968 | # happen if a non-weakened reference is kept in Tk Tree. | ||
| 969 | weaken( $data[1] ); | ||
| 970 | |||
| 971 | my $elt_type = $node->element_type($elt); | ||
| 972 | my $eltmode = $elt_mode{$elt_type}; | ||
| 973 | if ( $tkt->infoExists($newpath) ) { | ||
| 974 | $eltmode = $tkt->getmode($newpath); # will reuse mode below | ||
| 975 | } | ||
| 976 | else { | ||
| 977 | my @opt = $prevpath ? ( -after => $prevpath ) : ( -at => 0 ); | ||
| 978 | $logger->trace("disp_obj_elt add $newpath mode $eltmode type $elt_type"); | ||
| 979 | $tkt->add( $newpath, -data => \@data, @opt ); | ||
| 980 | $tkt->itemCreate( $newpath, 0, -text => $elt ); | ||
| 981 | $tkt->setmode( $newpath => $eltmode ); | ||
| 982 | } | ||
| 983 | |||
| 984 | my $elt_loc = $node_loc ? $node_loc . ' ' . $elt : $elt; | ||
| 985 | |||
| 986 | $cw->setmode( 'node', $newpath, $eltmode, $elt_loc, $opening, $actions, $scan_sub ); | ||
| 987 | |||
| 988 | my $obj = $node->fetch_element($elt); | ||
| 989 | |||
| 990 | if ($elt_type =~ 'node') { | ||
| 991 | $tkt->itemCreate( $newpath, 2, -text => $obj->fetch_gist ); | ||
| 992 | } | ||
| 993 | |||
| 994 | if ( $elt_type eq 'hash' ) { | ||
| 995 | $cw->update_hash_image( $obj, $newpath ); | ||
| 996 | } | ||
| 997 | |||
| 998 | if ($elt_type eq 'hash' or $elt_type eq 'list') { | ||
| 999 | my $size = $obj->fetch_size; | ||
| 1000 | $tkt->entryconfigure($newpath, -text => "$elt [$size]"); | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | $cw->show_single_list_value ($tkt, $obj, $newpath, $tkt->getmode($newpath) eq 'open' ? 1 : 0); | ||
| 1004 | |||
| 1005 | if ( $force_display_path and $force_display_path eq $elt_loc ) { | ||
| 1006 | $cw->force_display($newpath, $elt_loc); | ||
| 1007 | } | ||
| 1008 | if (not $force_display_path and $cw->{location} eq $elt_loc) { | ||
| 1009 | $cw->force_display($newpath, $elt_loc); | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | $prevpath = $newpath; | ||
| 1013 | } | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | sub force_display { | ||
| 1017 | my ($cw, $path, $loc) = @_; | ||
| 1018 | $logger->debug("force_display called on $path, location $loc"); | ||
| 1019 | my $tree = $cw->{tktree}; | ||
| 1020 | $tree->selectionClear(); | ||
| 1021 | $tree->anchorClear(); | ||
| 1022 | $tree->see($path); | ||
| 1023 | $tree->anchorSet($path); | ||
| 1024 | $cw->{location} = $loc; | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | # show a list like a leaf value when the list contains *one* item | ||
| 1028 | sub show_single_list_value { | ||
| 1029 | my ($cw, $tkt, $obj, $path, $show) = @_; | ||
| 1030 | my $elt_type = $obj->get_type; | ||
| 1031 | |||
| 1032 | # leave alone element that is not a list of leaf | ||
| 1033 | return unless $elt_type eq 'list' and $obj->get_cargo_type eq 'leaf'; | ||
| 1034 | my $size = $obj->fetch_size; | ||
| 1035 | |||
| 1036 | $logger->trace("show_single_list_value called on ", $obj->location, " show is $show, size is $size" ); | ||
| 1037 | if ($size == 1 and $show) { | ||
| 1038 | disp_leaf(undef,[ $path, $cw ], $obj->parent, $obj->element_name, 0, $obj->fetch_with_id(0)); | ||
| 1039 | } | ||
| 1040 | else { | ||
| 1041 | foreach my $column (qw/1 2 3/) { | ||
| 1042 | $tkt->itemDelete( $path, $column ) if $tkt->itemExists($path, $column); | ||
| 1043 | }; | ||
| 1044 | } | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | sub disp_hash { | ||
| 1048 | my ( $scanner, $data_ref, $node, $element_name, @all_idx ) = @_; | ||
| 1049 | my ( $path, $cw, $opening, $actions, $force_display_path ) = @$data_ref; | ||
| 1050 | my $tkt = $cw->{tktree}; | ||
| 1051 | my $mode = $tkt->getmode($path); | ||
| 1052 | |||
| 1053 | my @idx; | ||
| 1054 | my $hash = $node->fetch_element($element_name); | ||
| 1055 | foreach my $id (@all_idx) { | ||
| 1056 | my $loc = $hash->fetch_with_id($id)->location; | ||
| 1057 | my $action = $actions->{$loc} // ''; | ||
| 1058 | if ($action ne 'hide') { | ||
| 1059 | push @idx, $id; | ||
| 1060 | } | ||
| 1061 | } | ||
| 1062 | |||
| 1063 | $logger->trace("disp_hash path is $path mode $mode (@idx)"); | ||
| 1064 | $cw->prune( $path, @idx ); | ||
| 1065 | |||
| 1066 | my $elt = $node->fetch_element($element_name); | ||
| 1067 | my $elt_type = $elt->get_cargo_type(); | ||
| 1068 | |||
| 1069 | |||
| 1070 | # need to keep track myself of previous sibling as | ||
| 1071 | # $tkt->entrycget($path,'-after') dies | ||
| 1072 | # and $tkt->info('prev',$path) return the path above in the displayed tree, which | ||
| 1073 | # is not necessarily a sibling :-( | ||
| 1074 | my $prev_sibling = ''; | ||
| 1075 | my %tk_previous_path; | ||
| 1076 | foreach ( $tkt->info( 'children', $path ) ) { | ||
| 1077 | $tk_previous_path{$_} = $prev_sibling; | ||
| 1078 | $prev_sibling = $_; | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | my $prevpath = ''; | ||
| 1082 | foreach my $idx (@idx) { | ||
| 1083 | my $newpath = $path . '.' . to_path($idx); | ||
| 1084 | my $scan_sub = sub { | ||
| 1085 | $scanner->scan_hash( | ||
| 1086 | [ $newpath, $cw, $opening, $actions, $force_display_path ], | ||
| 1087 | $node, $element_name, $idx | ||
| 1088 | ); | ||
| 1089 | }; | ||
| 1090 | |||
| 1091 | my $eltmode = $elt_mode{$elt_type}; | ||
| 1092 | my $sub_elt = $elt->fetch_with_id($idx); | ||
| 1093 | |||
| 1094 | # check for display order mismatch | ||
| 1095 | if ( $tkt->infoExists($newpath) ) { | ||
| 1096 | if ( $prevpath ne $tk_previous_path{$newpath} ) { | ||
| 1097 | $logger->trace( | ||
| 1098 | "disp_hash deleting mismatching $newpath mode $eltmode cargo_type $elt_type"); | ||
| 1099 | $tkt->delete( entry => $newpath ); | ||
| 1100 | } | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | # check for content mismatch | ||
| 1104 | if ( $tkt->infoExists($newpath) ) { | ||
| 1105 | my $previous_data = $tkt->info( data => $newpath ); | ||
| 1106 | |||
| 1107 | # $previous_data is an object (or an empty string to avoid warnings) | ||
| 1108 | my $previous_elt = $previous_data->[1] || ''; | ||
| 1109 | $eltmode = $tkt->getmode($newpath); # will reuse mode below | ||
| 1110 | $logger->trace( "disp_hash reuse $newpath mode $eltmode cargo_type $elt_type" | ||
| 1111 | . " obj $previous_elt (expect $sub_elt)" ); | ||
| 1112 | |||
| 1113 | # string comparison of objects is intentional to check that the tree | ||
| 1114 | # refers to the correct Config::Model object | ||
| 1115 | if ( $sub_elt ne $previous_elt ) { | ||
| 1116 | $logger->trace( "disp_hash delete $newpath mode $eltmode (got " | ||
| 1117 | . "$previous_elt expected $sub_elt)" ); | ||
| 1118 | |||
| 1119 | # wrong order, delete the entry | ||
| 1120 | $tkt->delete( entry => $newpath ); | ||
| 1121 | } | ||
| 1122 | } | ||
| 1123 | |||
| 1124 | if ( not $tkt->infoExists($newpath) ) { | ||
| 1125 | my @opt = $prevpath ? ( -after => $prevpath ) : ( -at => 0 ); | ||
| 1126 | $logger->trace( | ||
| 1127 | "disp_hash add $newpath mode $eltmode cargo_type $elt_type" . " elt $sub_elt" ); | ||
| 1128 | my @data = ( $scan_sub, $sub_elt ); | ||
| 1129 | weaken( $data[1] ); | ||
| 1130 | $tkt->add( $newpath, -data => \@data, @opt ); | ||
| 1131 | $tkt->itemCreate( $newpath, 0, -text => $node->shorten_idx($idx) ); | ||
| 1132 | $tkt->setmode( $newpath => $eltmode ); | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | # update the node gist | ||
| 1136 | my $gist = $elt_type =~ /node/ ? $elt->fetch_with_id($idx)->fetch_gist : ''; | ||
| 1137 | $tkt->itemCreate( $newpath, 2, -text => $gist ); | ||
| 1138 | |||
| 1139 | my $elt_loc = $sub_elt->location; | ||
| 1140 | |||
| 1141 | # hide new entry if hash is not yet opened | ||
| 1142 | $cw->setmode( 'hash', $newpath, $eltmode, $elt_loc, $opening, $actions, $scan_sub ); | ||
| 1143 | |||
| 1144 | if ( $force_display_path and $force_display_path eq $elt_loc ) { | ||
| 1145 | $cw->force_display($newpath, $elt_loc) | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | $prevpath = $newpath; | ||
| 1149 | } | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | sub update_hash_image { | ||
| 1153 | my ( $cw, $elt, $path ) = @_; | ||
| 1154 | my $tkt = $cw->{tktree}; | ||
| 1155 | |||
| 1156 | # check hash status and set warning image if necessary | ||
| 1157 | my $img; | ||
| 1158 | { | ||
| 1159 | no warnings qw/uninitialized/; | ||
| 1160 | $img = $warn_img if $elt->warning_msg; | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | if ( defined $img ) { | ||
| 1164 | $tkt->itemCreate( $path, 1, -itemtype => 'image', -image => $img ); | ||
| 1165 | } | ||
| 1166 | else { | ||
| 1167 | $tkt->itemDelete( $path, 1 ) if $tkt->itemExists( $path, 1 ); | ||
| 1168 | } | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | sub setmode { | ||
| 1172 | my ( $cw, $type, $newpath, $eltmode, $elt_loc, $opening, $actions, $scan_sub ) = @_; | ||
| 1173 | my $tkt = $cw->{tktree}; | ||
| 1174 | |||
| 1175 | $actions->{$elt_loc} //= ''; | ||
| 1176 | my $force_open = $actions->{$elt_loc} eq 'show' ? 1 : 0; | ||
| 1177 | my $force_close = $actions->{$elt_loc} eq 'hide' ? 1 : 0; | ||
| 1178 | |||
| 1179 | $logger->trace( "$type: elt_loc '$elt_loc', opening $opening " | ||
| 1180 | . "eltmode $eltmode force_open $force_open"); | ||
| 1181 | |||
| 1182 | if ( not $force_close and ($eltmode ne 'open' or $force_open or $opening )) { | ||
| 1183 | $tkt->show( -entry => $newpath ); | ||
| 1184 | |||
| 1185 | # counter-intuitive: want to display [-] if force opening and not leaf item | ||
| 1186 | $tkt->setmode( $newpath => 'close' ) if ( $force_open and $eltmode ne 'none' ); | ||
| 1187 | } | ||
| 1188 | elsif ($force_close and $eltmode eq 'open') { | ||
| 1189 | $tkt->hide( -entry => $newpath ); | ||
| 1190 | } | ||
| 1191 | else { | ||
| 1192 | $tkt->close($newpath); | ||
| 1193 | } | ||
| 1194 | |||
| 1195 | # counterintuitive but right: scan will be done when the entry | ||
| 1196 | # is opened. mode can be open, close, none | ||
| 1197 | $scan_sub->( $force_open ) if ( ( $eltmode ne 'open' ) or $force_open ); | ||
| 1198 | } | ||
| 1199 | |||
| 1200 | sub trim_value { | ||
| 1201 | my $cw = shift; | ||
| 1202 | my $value = shift; | ||
| 1203 | |||
| 1204 | # undef value required lest Tk dies with: | ||
| 1205 | # value for "-text" missing at /usr/lib/x86_64-linux-gnu/perl5/5.28/Tk.pm line 251 | ||
| 1206 | return undef unless defined $value; ## no critic(Subroutines::ProhibitExplicitReturnUndef) | ||
| 1207 | |||
| 1208 | $value =~ s/\n/ /g; | ||
| 1209 | $value = substr( $value, 0, 15 ) . '...' if length($value) > 15; | ||
| 1210 | return $value; | ||
| 1211 | } | ||
| 1212 | |||
| 1213 | sub disp_check_list { | ||
| 1214 | my ( $scanner, $data_ref, $node, $element_name, $index, $leaf_object ) = @_; | ||
| 1215 | my ( $path, $cw, $opening, $actions ) = @$data_ref; | ||
| 1216 | $logger->trace("disp_check_list path is $path"); | ||
| 1217 | |||
| 1218 | my $value = $leaf_object->fetch; | ||
| 1219 | |||
| 1220 | my $tkt = $cw->{tktree}; | ||
| 1221 | $tkt->itemCreate( $path, 2, -text => $cw->trim_value($value) ); | ||
| 1222 | |||
| 1223 | my $std_v = $leaf_object->fetch('standard'); | ||
| 1224 | $tkt->itemCreate( $path, 3, -text => $cw->trim_value($std_v) ); | ||
| 1225 | |||
| 1226 | if ( $leaf_object->has_data ) { | ||
| 1227 | $tkt->itemCreate( $path, 1, -itemtype => 'image', -image => $cust_img ); | ||
| 1228 | } | ||
| 1229 | else { | ||
| 1230 | # remove image when value is identical to standard value | ||
| 1231 | $tkt->itemDelete( $path, 1 ) if $tkt->itemExists( $path, 1 ); | ||
| 1232 | } | ||
| 1233 | } | ||
| 1234 | |||
| 1235 | sub disp_leaf { | ||
| 1236 | my ( $scanner, $data_ref, $node, $element_name, $index, $leaf_object ) = @_; | ||
| 1237 | my ( $path, $cw, $opening, $actions ) = @$data_ref; | ||
| 1238 | $logger->trace("disp_leaf path is $path"); | ||
| 1239 | |||
| 1240 | my $std_v = $leaf_object->fetch(qw/mode standard check no silent 1/); | ||
| 1241 | my $value = $leaf_object->fetch( check => 'no', silent => 1 ); | ||
| 1242 | my $tkt = $cw->{tktree}; | ||
| 1243 | |||
| 1244 | my $img; | ||
| 1245 | if ($leaf_object->has_error) { | ||
| 1246 | $img = $error_img; | ||
| 1247 | } | ||
| 1248 | elsif ($leaf_object->has_warning) { | ||
| 1249 | $img = $warn_img; | ||
| 1250 | } | ||
| 1251 | elsif ($leaf_object->has_data) { | ||
| 1252 | $img = $cust_img; | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | if ( defined $img ) { | ||
| 1256 | $tkt->itemCreate( | ||
| 1257 | $path, 1, | ||
| 1258 | -itemtype => 'image', | ||
| 1259 | -image => $img | ||
| 1260 | ); | ||
| 1261 | } | ||
| 1262 | elsif ($tkt->itemExists( $path, 1 )) { | ||
| 1263 | # remove image | ||
| 1264 | $tkt->itemDelete( $path, 1 ) ; | ||
| 1265 | } | ||
| 1266 | |||
| 1267 | $tkt->itemCreate( $path, 2, -text => $cw->trim_value($value) ); | ||
| 1268 | |||
| 1269 | $tkt->itemCreate( $path, 3, -text => $cw->trim_value($std_v) ); | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | sub disp_node { | ||
| 1273 | my ( $scanner, $data_ref, $node, $element_name, $key, $contained_node ) = @_; | ||
| 1274 | my ( $path, $cw, $opening, $actions ) = @$data_ref; | ||
| 1275 | $logger->trace("disp_node path is $path"); | ||
| 1276 | my $curmode = $cw->{tktree}->getmode($path); | ||
| 1277 | $cw->{tktree}->setmode( $path, 'open' ) if $curmode eq 'none'; | ||
| 1278 | |||
| 1279 | # explore next node | ||
| 1280 | $scanner->scan_node( $data_ref, $contained_node ); | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | sub setup_scanner { | ||
| 1284 | my ($cw) = @_; | ||
| 1285 | require Config::Model::ObjTreeScanner; | ||
| 1286 | |||
| 1287 | my $scanner = Config::Model::ObjTreeScanner->new( | ||
| 1288 | check => 'no', | ||
| 1289 | |||
| 1290 | # node callback | ||
| 1291 | node_content_cb => \&disp_obj_elt, | ||
| 1292 | |||
| 1293 | # element callback | ||
| 1294 | list_element_cb => \&disp_hash, | ||
| 1295 | check_list_element_cb => \&disp_check_list, | ||
| 1296 | hash_element_cb => \&disp_hash, | ||
| 1297 | node_element_cb => \&disp_node, | ||
| 1298 | |||
| 1299 | # leaf callback | ||
| 1300 | leaf_cb => \&disp_leaf, | ||
| 1301 | ); | ||
| 1302 | |||
| 1303 | $cw->{scanner} = $scanner; | ||
| 1304 | |||
| 1305 | } | ||
| 1306 | |||
| 1307 | my %widget_table = ( | ||
| 1308 | edit => { | ||
| 1309 | leaf => 'ConfigModelLeafEditor', | ||
| 1310 | check_list => 'ConfigModelCheckListEditor', | ||
| 1311 | list => 'ConfigModelListEditor', | ||
| 1312 | hash => 'ConfigModelHashEditor', | ||
| 1313 | node => 'ConfigModelNodeEditor', | ||
| 1314 | }, | ||
| 1315 | view => { | ||
| 1316 | leaf => 'ConfigModelLeafViewer', | ||
| 1317 | check_list => 'ConfigModelCheckListViewer', | ||
| 1318 | list => 'ConfigModelListViewer', | ||
| 1319 | hash => 'ConfigModelHashViewer', | ||
| 1320 | node => 'ConfigModelNodeViewer', | ||
| 1321 | }, | ||
| 1322 | ); | ||
| 1323 | |||
| 1324 | sub create_element_widget { | ||
| 1325 | my $cw = shift; | ||
| 1326 | my $mode = shift; | ||
| 1327 | my $tree_path = shift; # optional | ||
| 1328 | my $obj = shift; # optional if tree is not opened to path | ||
| 1329 | |||
| 1330 | my $tree = $cw->{tktree}; | ||
| 1331 | |||
| 1332 | unless ( defined $tree_path ) { | ||
| 1333 | # pointery and rooty are common widget method and must called on | ||
| 1334 | # the right widget to give accurate results | ||
| 1335 | $tree_path = $tree->nearest( $tree->pointery - $tree->rooty ); | ||
| 1336 | } | ||
| 1337 | |||
| 1338 | if ( $tree->info( exists => $tree_path ) ) { | ||
| 1339 | $tree->selectionClear(); # clear all | ||
| 1340 | $tree->selectionSet($tree_path); | ||
| 1341 | my $data_ref = $tree->infoData($tree_path); | ||
| 1342 | unless ( defined $data_ref->[1] ) { | ||
| 1343 | $cw->reload; | ||
| 1344 | return; | ||
| 1345 | } | ||
| 1346 | $obj = $data_ref->[1]; | ||
| 1347 | weaken($obj); | ||
| 1348 | } | ||
| 1349 | |||
| 1350 | my $loc = $obj->location; | ||
| 1351 | my $type = $obj->get_type; | ||
| 1352 | $logger->trace("item $loc to $mode (type $type)"); | ||
| 1353 | |||
| 1354 | my $e_frame = $cw->Subwidget('ed_frame'); | ||
| 1355 | |||
| 1356 | # cleanup existing widget contained in this frame | ||
| 1357 | delete $cw->{editor}; | ||
| 1358 | map { $_->destroy if Tk::Exists($_) } $e_frame->children; | ||
| 1359 | |||
| 1360 | my $widget = $widget_table{$mode}{$type} | ||
| 1361 | || die "Cannot find $mode widget for type $type"; | ||
| 1362 | my $weak_cw = $cw; | ||
| 1363 | weaken($weak_cw); | ||
| 1364 | my @store = $mode eq 'edit' ? ( -store_cb => sub { $weak_cw->reload(@_) } ) : (); | ||
| 1365 | $cw->{current_mode} = $mode; | ||
| 1366 | |||
| 1367 | my $tk_font = $cw->cget('-font'); | ||
| 1368 | $cw->{editor} = $e_frame->$widget( | ||
| 1369 | -item => $obj, | ||
| 1370 | -path => $tree_path, | ||
| 1371 | -font => $tk_font, | ||
| 1372 | @store, | ||
| 1373 | ); | ||
| 1374 | |||
| 1375 | $cw->{editor}->ConfigSpecs( -font => ['DESCENDANTS', 'font','Font', $tk_font ]); | ||
| 1376 | |||
| 1377 | $cw->{editor}->pack( -expand => 1, -fill => 'both' ); | ||
| 1378 | return $cw->{editor}; | ||
| 1379 | } | ||
| 1380 | |||
| 1381 | sub edit_copy { | ||
| 1382 | my $cw = shift; | ||
| 1383 | my $tkt = $cw->{tktree}; | ||
| 1384 | |||
| 1385 | my @selected = @_ ? @_ : $tkt->info('selection'); | ||
| 1386 | |||
| 1387 | #print "edit_copy @selected\n"; | ||
| 1388 | my @res; | ||
| 1389 | |||
| 1390 | foreach my $selection (@selected) { | ||
| 1391 | my $data_ref = $tkt->infoData($selection); | ||
| 1392 | |||
| 1393 | my $cfg_elt = $data_ref->[1]; | ||
| 1394 | my $type = $cfg_elt->get_type; | ||
| 1395 | my $cfg_class = $type eq 'node' ? $cfg_elt->config_class_name : ''; | ||
| 1396 | |||
| 1397 | #print "edit_copy '",$cfg_elt->location, "' type '$type' class '$cfg_class'\n"; | ||
| 1398 | |||
| 1399 | push @res, | ||
| 1400 | [ | ||
| 1401 | $cfg_elt->element_name, $cfg_elt->index_value, $cfg_elt->composite_name, | ||
| 1402 | $type, $cfg_class, $cfg_elt->dump_as_data() ]; | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | $cw->{cut_buffer} = \@res; | ||
| 1406 | |||
| 1407 | #use Data::Dumper; print "cut_buffer: ", Dumper( \@res ) ,"\n"; | ||
| 1408 | |||
| 1409 | return \@res; # for tests | ||
| 1410 | } | ||
| 1411 | |||
| 1412 | sub edit_paste { | ||
| 1413 | my $cw = shift; | ||
| 1414 | my $tkt = $cw->{tktree}; | ||
| 1415 | |||
| 1416 | my @selected = @_ ? @_ : $tkt->info('selection'); | ||
| 1417 | |||
| 1418 | return unless @selected; | ||
| 1419 | |||
| 1420 | #print "edit_paste in @selected\n"; | ||
| 1421 | my @res; | ||
| 1422 | |||
| 1423 | my $selection = $selected[0]; | ||
| 1424 | |||
| 1425 | my $data_ref = $tkt->infoData($selection); | ||
| 1426 | |||
| 1427 | my $cfg_elt = $data_ref->[1]; | ||
| 1428 | |||
| 1429 | #print "edit_paste '",$cfg_elt->location, "' type '", $cfg_elt->get_type,"'\n"; | ||
| 1430 | my $t_type = $cfg_elt->get_type; | ||
| 1431 | my $t_class = $t_type eq 'node' ? $cfg_elt->config_class_name : ''; | ||
| 1432 | my $t_name = $cfg_elt->element_name; | ||
| 1433 | my $cut_buf = $cw->{cut_buffer} || []; | ||
| 1434 | |||
| 1435 | foreach my $data (@$cut_buf) { | ||
| 1436 | my ( $name, $index, $composite, $type, $cfg_class, $dump ) = @$data; | ||
| 1437 | |||
| 1438 | #print "from composite name '$composite' type $type\n"; | ||
| 1439 | #print "t_name '$t_name' t_type '$t_type' class '$t_class'\n"; | ||
| 1440 | if ( ( $name eq $t_name and $type eq $t_type ) | ||
| 1441 | or $t_class eq $cfg_class ) { | ||
| 1442 | $cfg_elt->load_data($dump); | ||
| 1443 | } | ||
| 1444 | elsif ( ( $t_type eq 'hash' or $t_type eq 'list' ) and defined $index ) { | ||
| 1445 | $cfg_elt->fetch_with_id($index)->load_data($dump); | ||
| 1446 | } | ||
| 1447 | elsif ( $t_type eq 'hash' or $t_type eq 'list' or $t_type eq 'leaf' ) { | ||
| 1448 | $cfg_elt->load_data($dump); | ||
| 1449 | } | ||
| 1450 | else { | ||
| 1451 | $cfg_elt->grab($composite)->load_data($dump); | ||
| 1452 | } | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | $cw->reload() if @$cut_buf; | ||
| 1456 | $cw->create_element_widget($cw->{current_mode}, $selection); | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | sub wizard { | ||
| 1460 | my $cw = shift; | ||
| 1461 | |||
| 1462 | my $wiz = $cw->setup_wizard( sub { $cw->deiconify; $cw->raise; $cw->reload; } ); | ||
| 1463 | |||
| 1464 | # hide main window while wizard is running | ||
| 1465 | # end_cb callback will raise the main window | ||
| 1466 | $cw->withdraw; | ||
| 1467 | |||
| 1468 | $wiz->prepare_wizard(); | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | sub setup_wizard { | ||
| 1472 | my $cw = shift; | ||
| 1473 | my $end_sub = shift; | ||
| 1474 | |||
| 1475 | # when wizard is run, there's no need to update editor window in | ||
| 1476 | # main widget | ||
| 1477 | my $tk_font = $cw->cget('-font'); | ||
| 1478 | return $cw->ConfigModelWizard( | ||
| 1479 | -root => $cw->{instance}->config_root, | ||
| 1480 | -end_cb => $end_sub, | ||
| 1481 | -font => $tk_font, | ||
| 1482 | ); | ||
| 1483 | } | ||
| 1484 | |||
| 1485 | # FIXME: need to be able to search different types. | ||
| 1486 | # 2 choices | ||
| 1487 | # - destroy and re-create the searcher when it's modified | ||
| 1488 | # - change the searcher (TreeSearcher) to accept type modif | ||
| 1489 | # For the latter: it would be better to accept a set of types instead of | ||
| 1490 | # all or just one type (to implement a set of check buttons) | ||
| 1491 | |||
| 1492 | sub create_find_widget { | ||
| 1493 | my $cw = shift; | ||
| 1494 | my $f = $cw->Frame( -relief => 'ridge', -borderwidth => 1, ); | ||
| 1495 | my $remove_img = $cw->Photo( -file => $icon_path . 'remove.png' ); | ||
| 1496 | |||
| 1497 | $f->Button( | ||
| 1498 | -image => $remove_img, | ||
| 1499 | -command => sub { $f->packForget(); }, | ||
| 1500 | -relief => 'flat', | ||
| 1501 | )->pack( -side => 'left' ); | ||
| 1502 | |||
| 1503 | my $searcher = $cw->{instance}->config_root->tree_searcher( type => 'all' ); | ||
| 1504 | |||
| 1505 | my $search = ''; | ||
| 1506 | my @result; | ||
| 1507 | $f->Label( -text => 'Find: ' )->pack( -side => 'left' ); | ||
| 1508 | my $e = $f->Entry( | ||
| 1509 | -textvariable => \$search, | ||
| 1510 | -validate => 'key', | ||
| 1511 | |||
| 1512 | # ditch the search results when find entry is modified. | ||
| 1513 | -validatecommand => sub { @result = (); return 1; }, | ||
| 1514 | )->pack( -side => 'left' ); | ||
| 1515 | |||
| 1516 | $cw->Advertise( find_entry => $e ); | ||
| 1517 | |||
| 1518 | foreach my $direction (qw/previous next/) { | ||
| 1519 | my $s = sub { $cw->find_item( $direction, $searcher, \$search, \@result ); }; | ||
| 1520 | $f->Button( | ||
| 1521 | -compound => 'left', | ||
| 1522 | -image => $gnome_img{$direction}, | ||
| 1523 | -text => ucfirst($direction), | ||
| 1524 | -command => $s, | ||
| 1525 | -relief => 'flat', | ||
| 1526 | )->pack( -side => 'left' ); | ||
| 1527 | } | ||
| 1528 | |||
| 1529 | # bind Return (or Enter) key | ||
| 1530 | $e->bind( '<Key-Return>', sub { $cw->find_item( 'next', $searcher, \$search, \@result ); } ); | ||
| 1531 | |||
| 1532 | return $f; | ||
| 1533 | } | ||
| 1534 | |||
| 1535 | sub pack_find_widget { | ||
| 1536 | my $cw = shift; | ||
| 1537 | $cw->Subwidget('find_frame')->pack( -anchor => 'w', -fill => 'x' ); | ||
| 1538 | $cw->Subwidget('find_entry')->focus; | ||
| 1539 | } | ||
| 1540 | |||
| 1541 | sub find_item { | ||
| 1542 | my ( $cw, $direction, $searcher, $search_ref, $result ) = @_; | ||
| 1543 | |||
| 1544 | my $find_frame = $cw->Subwidget('find_frame'); | ||
| 1545 | |||
| 1546 | # search the tree, store the result | ||
| 1547 | if (not @$result) { | ||
| 1548 | $logger->debug("Running search on $$search_ref"); | ||
| 1549 | @$result = $searcher->search($$search_ref); | ||
| 1550 | $logger->trace("Search on $$search_ref result: @$result"); | ||
| 1551 | } | ||
| 1552 | |||
| 1553 | # and jump in the list widget any time next is hit. | ||
| 1554 | if (@$result) { | ||
| 1555 | if ( defined $cw->{old_path} and $direction eq 'next' ) { | ||
| 1556 | push @$result, shift @$result; | ||
| 1557 | } | ||
| 1558 | elsif ( defined $cw->{old_path} ) { | ||
| 1559 | unshift @$result, pop @$result; | ||
| 1560 | } | ||
| 1561 | my $path = $result->[0]; | ||
| 1562 | $cw->{old_path} = $path; | ||
| 1563 | |||
| 1564 | $cw->force_element_display( $cw->{instance}->config_root->grab($path) ); | ||
| 1565 | } | ||
| 1566 | } | ||
| 1567 | 1; | ||
| 1568 | |||
| 1569 | # ABSTRACT: a GUI to edit configuration data with Config::Model framework | ||
| 1570 | |||
| 1571 | __END__ |