Tk/LeafViewer.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 108 CC 6 SCAR 28.3
CC
6
Cov
0%
Populate
42.0
ClassInit
2.0
cme_object
2.0
CRAP 17.0
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::LeafViewer 1.382;
11  
12 use strict;
13 use warnings;
14 use 5.10.1;
15 use Carp;
16 use Log::Log4perl;
17 use Text::Diff;
18  
19 use base qw/Tk::Frame Config::Model::Tk::AnyViewer/;
20  
21 Construct Tk::Widget 'ConfigModelLeafViewer';
22  
23 my @fbe1 = qw/-fill both -expand 1/;
24 my @fxe1 = qw/-fill x -expand 1/;
25 my @fx = qw/-fill x /;
26  
27 my $logger = Log::Log4perl::get_logger("Tk::LeafViewer");
28  
29 sub ClassInit {
30 my ( $cw, $args ) = @_;
31  
32 # ClassInit is often used to define bindings and/or other
33 # resources shared by all instances, e.g., images.
34  
35 # cw->Advertise(name=>$widget);
36 }
37  
38 sub Populate {
39 my ( $cw, $args ) = @_;
40 my $leaf = $cw->{leaf} = delete $args->{-item}
41 || die "LeafViewer: no -item, got ", keys %$args;
42 my $path = delete $args->{-path}
43 || die "LeafViewer: no -path, got ", keys %$args;
44 my $cme_font = delete $args->{-font};
45  
46 my $inst = $leaf->instance;
47  
48 my $vt = $leaf->value_type;
49 $logger->info("Creating leaf viewer for value_type $vt");
50 my $v = $leaf->fetch( check => 'no' );
51  
52 $cw->add_header( View => $leaf )->pack(@fx);
53  
54 my @pack_args = @fx;
55 @pack_args = @fbe1 if $vt eq 'string';
56 my $lv_frame = $cw->Frame(qw/-relief raised -borderwidth 2/)->pack(@pack_args);
57 $lv_frame->Label( -text => 'Value' )->pack();
58  
59 if ( $vt eq 'string') {
60 require Tk::ROText;
61 $cw->{e_widget} = $lv_frame->Scrolled(
62 'ROText',
63 -height => 5,
64 -scrollbars => 'ow',
65 )->pack(@fbe1);
66 $cw->{e_widget}->insert( 'end', $v, 'value' );
67 $cw->{e_widget}->tagConfigure(qw/value -lmargin1 2 -lmargin2 2 -rmargin 2/);
68  
69 my $std = $cw->{leaf}->fetch_standard ;
70 if ($std) {
71 $lv_frame->Label( -text => 'Diff compared to standard value' )->pack();
72 $cw->{diff_widget} = $lv_frame->Scrolled(
73 'ROText',
74 -height => 5,
75 -scrollbars => 'ow',
76 )->pack(@fbe1);
77  
78 # Text::Diff does not handle well files without trailing \n
79 $std .= "\n" unless $std =~ /\n$/;
80 my $new = $v // '';
81 $new .= "\n" unless $new =~ /\n$/;
82  
83 my $diff = diff( \$std, \$new , { STYLE => "Unified" } );
84 $cw->{diff_widget}->insert( 'end', $diff, 'value' );
85 $cw->{diff_widget}->tagConfigure(qw/value -lmargin1 2 -lmargin2 2 -rmargin 2/);
86 }
87 }
88 else {
89 my $v_frame = $lv_frame->Frame(qw/-relief sunken -borderwidth 1/)->pack(@fxe1);
90 $v_frame->Label( -text => $v, -anchor => 'w' )->pack( @fxe1, -side => 'left' );
91 }
92  
93 $cw->add_annotation($leaf)->pack(@fx);
94 $cw->add_summary($leaf)->pack(@fx);
95 $cw->add_description($leaf)->pack(@fbe1);
96 $cw->add_warning( $leaf, 'view' )->pack(@fx);
97 $cw->add_help( 'value help' => $leaf->get_help( $cw->{value} ) )->pack(@fx);
98 $cw->add_info_button()->pack( @fxe1, -side => 'left', -anchor => 'n' );
99 $cw->add_editor_button($path)->pack( @fxe1, -side => 'right', -anchor => 'n' );
100  
101 $cw->ConfigSpecs(
102 -font => [['SELF','DESCENDANTS'], 'font','Font', $cme_font ],
103 #-fill => [ qw/SELF fill Fill both/],
104 #-expand => [ qw/SELF expand Expand 1/],
105 -relief => [qw/SELF relief Relief groove/],
106 -borderwidth => [qw/SELF borderwidth Borderwidth 2/],
107 DEFAULT => [qw/SELF/],
108 );
109  
110 $cw->SUPER::Populate($args);
111 }
112  
113 sub cme_object {
114 my $cw = shift;
115 return $cw->{leaf};
116 }
117  
118 1;