blib/lib/Gtk2/Ex/FileLocator/PathBar.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 141 CC 9 SCAR 22.1
CC
9
Cov
0%
set_filename
30.0
on_click
12.0
get_filename
6.0
CRAP 9.2
1 package Gtk2::Ex::FileLocator::PathBar;
2 use strict;
3 use warnings;
4  
5 use Gtk2;
6 use Glib qw(TRUE FALSE);
7  
8 use Gtk2::Ex::FileLocator::Helper;
9  
10 use Glib::Object::Subclass Gtk2::Ex::FileLocator::FileChooser::,
11 properties => [
12 Glib::ParamSpec->object(
13 'scrolledWindow', 'scrolledWindow',
14 '', 'Gtk2::ScrolledWindow',
15 [qw/readable writable/]
16 ),
17 ],
18 ;
19  
20 sub INIT_INSTANCE {
21 my ($this) = @_;
22  
23 $this->set( 'scrolledWindow', new Gtk2::ScrolledWindow );
24 $this->get('scrolledWindow')->set_policy( 'never', 'never' );
25 $this->add($this->get('scrolledWindow'));
26  
27 $this->{hbox} = new Gtk2::HBox;
28 $this->{hbox}->set_size_request( -1, 10 );
29 $this->{hbox}->set_spacing(0);
30 $this->{hbox}->show;
31  
32 $this->signal_connect_after( 'map' => \&on_map );
33 }
34  
35 sub on_map {
36 my $this = shift;
37  
38 $this->get('scrolledWindow')->add_with_viewport( $this->{hbox} );
39 $this->get('scrolledWindow')->child->set_shadow_type('none');
40 1;
41 }
42  
43 sub get_uri {
44 my ($this) = @_;
45 printf "PathBar %s\n", $this->get_filename;
46 return sprintf "file://%s", $this->get_filename;
47 }
48  
49 sub get_filename {
50 my $this = shift;
51 my $filename = join "/", map { $_->{string} } $this->{hbox}->get_children;
52 $filename =~ s|/+|/|sgo;
53 $filename =~ s|/*$|/|o if -d $filename;
54 return $filename;
55 }
56  
57 sub set_uri {
58 my ( $this, $uri ) = @_;
59 $uri =~ s|^.*?\://||o;
60 printf "set_filename %s\n", $uri;
61 $this->set_filename($uri);
62 }
63  
64 sub set_filename ($$) {
65 my ( $this, $filename ) = @_;
66  
67 $this->clear_all;
68 return unless $filename;
69 printf "set_filename %s\n", $filename if -e $filename;
70  
71 my @filename = split '/', $filename;
72  
73 unless ( $filename[0] ) {
74 shift @filename;
75 $this->{hbox}->pack_start( $this->make_button("/"), FALSE, FALSE, 0 )
76 if @filename;
77 }
78  
79 $this->{hbox}
80 ->pack_end( $this->make_button( pop @filename || '/' ), TRUE, TRUE, 0 );
81 $this->{hbox}->pack_start( $this->make_button($_), FALSE, FALSE, 0 )
82 foreach @filename;
83  
84 $this->configure_buttons;
85  
86 return;
87 }
88  
89 sub clear_all {
90 my $this = shift;
91 $this->{hbox}->remove($_) foreach $this->{hbox}->get_children;
92 return;
93 }
94  
95 sub make_button {
96 my ( $this, $string ) = @_;
97  
98 my $button = new Gtk2::Button;
99 $button->{string} = $string || '';
100 $button->show;
101 $button->signal_connect_after( 'clicked' => \&on_click, $this );
102 return $button;
103 }
104  
105 sub configure_buttons {
106 my ($this) = @_;
107  
108 #printf "configure_buttons\n";
109 return unless $this->{hbox}->get_children;
110  
111 my $separator = $this->get_separator_width;
112  
113 foreach ( $this->{hbox}->get_children ) {
114 my $layout = $this->get('scrolledWindow')->create_pango_layout( $_->{string} );
115 $_->set_size_request( ( $layout->get_pixel_size )[0] + $separator, 0 );
116 }
117  
118 $this->queue_resize;
119 }
120  
121 sub get_separator_width {
122 my $this = shift;
123 my $layout = $this->get('scrolledWindow')->create_pango_layout('/');
124 return ( $layout->get_pixel_size )[0];
125 }
126  
127 sub on_click {
128 my ( $button, $this ) = @_;
129  
130 my @children = $this->{hbox}->get_children;
131 file_open( $this->{filename} ) if $button == $children[$#children];
132  
133 foreach ( reverse @children ) {
134 last if $_ == $button;
135 $this->{hbox}->remove($_);
136 }
137 $this->{hbox}->set_child_packing( $button, TRUE, TRUE, 0, 'end' );
138  
139 #printf "%s\n", $this->get_filename;
140 $this->signal_emit( 'selection-changed' );
141 0;
142 }
143  
144 sub set_scroll_offset {
145 my ( $this, $scrollOffset ) = @_;
146  
147 #printf "%s\n", $scrollOffset;
148 $this->get('scrolledWindow')->get_hadjustment->set_value($scrollOffset);
149 return;
150 }
151  
152 1;
153 __END__