ListBrowser/LBHeader.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 218 CC 12 SCAR 20.1
CC
12
Cov
0%
B1Motion
20.0
Populate
12.0
Resize
12.0
CRAP 7.5
1 package Tk::ListBrowser::LBHeader;
2  
3 use strict;
4 use warnings;
5 use vars qw($VERSION);
6 $VERSION = 0.11;
7  
8 use base qw(Tk::Derived Tk::Frame);
9 Construct Tk::Widget 'LBHeader';
10  
11 use Tk;
12  
13 my $down_arrow = '#define down_width 10
14 #define down_height 10
15 static unsigned char down_bits[] = {
16 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xfe, 0x01, 0xfc, 0x00, 0x78, 0x00,
17 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
18 ';
19  
20 my $empty_icon = '#define empty_width 10
21 #define empty_height 10
22 static unsigned char empty_bits[] = {
23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
25 ';
26  
27 my $up_arrow = '#define up_width 10
28 #define up_height 10
29 static unsigned char up_bits[] = {
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x78, 0x00, 0xfc, 0x00,
31 0xfe, 0x01, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00 };
32 ';
33  
34 sub Populate {
35 my ($self,$args) = @_;
36  
37 my $column = delete $args->{'-column'};
38 die "You need to specify the -column option" unless defined $column;
39  
40 my $lb = delete $args->{'-listbrowser'};
41 die "You need to specify the -listbrowser option" unless defined $lb;
42  
43 $self->SUPER::Populate($args);
44
45 $self->{ACTIVE} = 0;
46 $self->{COLUMN} = $column;
47 $self->{LISTBROWSER} = $lb;
48
49 my $label = $self->Label(
50 )->pack(-side => 'left');
51 $self->Advertise(Label => $label);
52  
53 my $sizer = $self->Label(
54 -borderwidth => 2,
55 )->pack(-side => 'right', -fill => 'y');
56 $self->Advertise(Sizer => $sizer);
57 $sizer->bind('<Enter>', [$self, 'SizerEnter']);
58 $sizer->bind('<Leave>', [$self, 'SizerLeave']);
59 $sizer->bind('<Button-1>', [$self, 'SizerClick', $self, Ev('x'), Ev('y')]);
60 $sizer->bind('<ButtonRelease-1>', [$self, 'SizerRelease']);
61 $sizer->bind('<B1-Motion>', [$self, 'Resize', $self, Ev('x'), Ev('y')]);
62
63  
64 my $sort = $self->Label->pack(-side => 'right');
65 $self->Advertise(Sort => $sort);
66
67 for ($self, $label, $sort) {
68 my $item = $_;
69 $item->bind('<ButtonRelease-3>', [$self, 'Callback', '-contextcall', Ev('x'), Ev('y')]);
70 # $item->bind('<Button-1>', [$self, 'SortClick']);
71 $item->bind('<B1-Motion>', [$self, 'B1Motion', $item, Ev('x')]);
72 $item->bind('<ButtonRelease-1>', [$self, 'B1', $item, Ev('x')]);
73 }
74  
75 my $fg = $sort->cget('-foreground');
76 $self->{ICONS} = {
77 ascending => $self->Bitmap(
78 -data => $up_arrow,
79 -foreground => $fg,
80 ),
81 descending => $self->Bitmap(
82 -data => $down_arrow,
83 -foreground => $fg,
84 ),
85 none => $self->Bitmap(
86 -data => $empty_icon,
87 -foreground => $fg,
88 ),
89 };
90 $self->{SORT} = undef;
91
92 $self->ConfigSpecs(
93 -contextcall => ['CALLBACK', undef, undef, sub {}],
94 -motioncall => ['CALLBACK', undef, undef, sub {}],
95 -releasecall => ['CALLBACK', undef, undef, sub {}],
96 -sortcall => ['CALLBACK', undef, undef, sub {}],
97 -sortorder => ['METHOD', undef, undef, 'none'],
98 -image => [$label],
99 -text => [$label],
100 DEFAULT => [ $self ],
101 );
102 return $self;
103 }
104  
105 sub B1 {
106 my $self = shift;
107 if (exists $self->{CURSORSAVE}) {
108 my $c = $self->{CURSORSAVE};
109 $self->configure(-cursor => $c);
110 delete $self->{CURSORSAVE};
111 $self->Callback('-releasecall');
112 } else {
113 $self->SortClick;
114 }
115 }
116  
117 sub B1Motion {
118 my ($self, $item, $x) = @_;
119 return if $self->column eq '';
120 unless (exists $self->{CURSORSAVE}) {
121 $self->{CURSORSAVE} = $self->cget('-cursor');
122 $self->configure(-cursor => 'fleur');
123 }
124 $x = $x + $item->x unless $item eq $self;
125 $self->Callback('-motioncall', $self->column, $x);
126 }
127  
128 sub column { return $_[0]->{COLUMN} }
129  
130 sub listbrowser { return $_[0]->{LISTBROWSER} }
131  
132 sub NeedlePos {
133 my $self = shift;
134 my $xn;
135 my $lb = $self->listbrowser;
136 my $c = $lb->Subwidget('Canvas');
137 my $column = $self->column;
138 my $next = $lb->columnNext($column);
139 if (defined $next) {
140 $xn = $lb->headerGet($next)->x;
141 } else {
142 my $h = $lb->headerGet($column);
143 $xn = $h->x + $h->width;
144 }
145 return $xn
146 }
147  
148 sub Resize {
149 my ($self, $widget, $x, $y) = @_;
150 if ($self->{ACTIVE}) {
151 my $root = $self->rootx;
152 my $dest = $root + $self->Subwidget('Sizer')->x + $self->{CLICKPOS} + $x;
153 my $width = $dest - $root;
154
155 my $l = $self->Subwidget('Label');
156 my $s = $self->Subwidget('Sizer');
157 my $r = $self->Subwidget('Sort');
158
159 my $bw = $self->cget('-borderwidth');
160 my $lb = $l->cget('-borderwidth');
161 my $sb = $s->cget('-borderwidth');
162 my $rb = $r->cget('-borderwidth');
163 my $bordersize = ($bw +$lb + $sb + $rb);
164 my $min = $l->width + $r->width + $s->width + $bordersize;
165 unless ($width <= $min) {
166 my $column = $self->column;
167 my $lb = $self->listbrowser;
168 $lb->columnWidth($column, $width);
169 $lb->headerPlace;
170 my $c = $lb->Subwidget('Canvas');
171 my $xnold = $self->{'needlepos'};
172 my $newpos = $self->NeedlePos;
173 my $needle = $self->{'needle'};
174 my $delta = $newpos - $xnold;
175 $c->move($needle, $delta, 0);
176 $self->{'needlepos'} = $newpos;
177 $self->update;
178 }
179 }
180 }
181  
182 sub SetSort {
183 my ($self, $sort) = @_;
184 my $icon = $self->{ICONS}->{$sort};
185 $self->Subwidget('Sort')->configure(-image => $icon);
186 }
187  
188 sub SizerClick {
189 my ($self, $widget, $x, $y) = @_;
190 $self->{CLICKPOS} = $x;
191 $self->{ACTIVE} = 1;
192 my $lb = $self->listbrowser;
193 my $c = $lb->Subwidget('Canvas');
194 my $region = $lb->cget('-scrollregion');
195 my $xn = $self->NeedlePos;
196 $self->{'needlepos'} = $xn;
197 my $needle = $c->createLine($xn, 0, $xn, $region->[3],
198 -fill => $lb->cget('-foreground'),
199 );
200 $c->raise($needle);
201 $self->{'needle'} = $needle;
202 }
203  
204 sub SizerEnter {
205 my $self = shift;
206 my $s = $self->Subwidget('Sizer');
207 $self->{CURSORSAVE} = $s->cget('-cursor');
208 $s->configure(-cursor => 'sb_h_double_arrow');
209 }
210  
211 sub SizerLeave {
212 my $self = shift;
213 my $s = $self->Subwidget('Sizer');
214 my $cs = $self->{CURSORSAVE};
215 $s->configure(-cursor => $cs) if defined $cs;
216 delete $self->{CURSORSAVE};
217 }
218  
219 sub SizerRelease {
220 my $self = shift;
221 $self->{ACTIVE} = 0;
222 my $lb = $self->listbrowser;
223 my $c = $lb->Subwidget('Canvas');
224 my $needle = $self->{'needle'};
225 delete $self->{'needle'};
226 delete $self->{'needlepos'};
227 $c->delete($needle);
228 $lb->refreshPurge;
229 }
230  
231 my %sortmatrix = (
232 ascending => 'descending',
233 descending => 'ascending',
234 none => 'ascending',
235 );
236  
237 sub SortClick {
238 my $self = shift;
239 # my $name = $self->Subwidget('Label')->cget('-text');
240 my $order = $sortmatrix{$self->cget('-sortorder')};
241 $self->Callback('-sortcall', $self->column, $order);
242 $self->listbrowser->sortList;
243 }
244  
245  
246 sub sortorder {
247 my ($self, $sort) = @_;
248 if (defined $sort) {
249 $self->{SORT} = $sort;
250 $self->after(1, ['SetSort', $self, $sort]);
251 }
252 return $self->{SORT}
253 }
254  
255  
256 1;
257  
258  
259  
260  
261  
262  
263  
264  
265