HdrResizeButton.pm untested

filter: Statementstmt 0.0% 0 / 162 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 257 CC 20 SCAR 28.1
CC
20
Cov
0%
ButtonRelease
56.0
OpenCloseColumn
20.0
ButtonOver
12.0
CRAP 16.7
1 package Tk::HdrResizeButton;
2 #------------------------------------------------
3 # automagically updated versioning variables -- CVS modifies these!
4 #------------------------------------------------
5 our $Revision = '$Revision: 1.4 $';
6 our $CheckinDate = '$Date: 2009/04/06 20:46:00 $';
7 our $CheckinUser = '$Author: xpix $';
8
9 # we need to clean these up right here
10 $Revision =~ s/^\$\S+:\s*(.*?)\s*\$$/$1/sx;
11 $CheckinDate =~ s/^\$\S+:\s*(.*?)\s*\$$/$1/sx;
12 $CheckinUser =~ s/^\$\S+:\s*(.*?)\s*\$$/$1/sx;
13
14 #-------------------------------------------------
15 #-- package Tk::HdrResizeButton ---------------------
16 #-------------------------------------------------
17 use vars qw ($VERSION);
18 $VERSION = '1.5';
19
20 #########################################################################
21 # Tk::HdrResizeButton
22 # Summary: This widget creates a button for use in an HList header which
23 # provides methods for resizing a column. This was heavily
24 # leveraged from Columns.pm by Damion Wilson.
25 # Author: Shaun Wandler
26 # Date: $Date: 2009/04/06 20:46:00 $
27 # Revision: $Revision: 1.5 $
28 #########################################################################=
29 #####
30 #
31 # Updated by Slaven Rezic and Frank Herrmann, Michael Krause
32 #
33
34 # XXX needs lot of work:
35 # DONE (MK) * position columnbar correctly and only use MoveColumnBar to move it instead
36 # of destroying it and re-creating with CreateColumnBar
37 # (for what?) * use Subwidget('scrolled') if it exists
38 # DONE (MK) * don't give error if -command is not specified
39 # DONE (MK) * don't let the user hide columns (minwidth?)
40 # DONE (MK) * double-click on column should not more execute the single-click command callback
41 # DONE (MK) * configurable closedcolWidth, ResizeWidth
42
43 use base qw(Tk::Derived Tk::Button);
44 use strict;
45
46 Construct Tk::Widget 'HdrResizeButton';
47
48 sub ClassInit
49 {
50 my ($class, $mw) = @_;
51 $class->SUPER::ClassInit($mw);
52 $mw->bind( $class, '<ButtonRelease-1>', 'ButtonRelease' );
53 $mw->bind( $class, '<ButtonPress-1>', 'ButtonPress' );
54 $mw->bind( $class, '<Motion>', 'ButtonOver' );
55 $mw->bind( $class, '<ButtonRelease-3>', 'ColumnFullSize' );
56 $mw->bind( $class, '<Double-1>', 'ButtonDouble1' );
57
58 # Override these ones too
59 $mw->bind($class, '<Enter>', 'BttnEnter' );
60 $mw->bind($class, '<Leave>', 'BttnLeave' );
61
62 return $class;
63 }
64
65 sub Populate
66 {
67 my ($this, $args) = @_;
68
69 # CREATE THE RESIZE CONTROL
70 my $r_Widget;
71 my $r_width = delete $args->{-resizerwidth} || 1;
72 $r_Widget = $this->Component(
73 'Frame' => 'Trim_R',
74 #-background => 'white',
75 #-relief => 'raised',
76 -borderwidth => 1,
77 -width => $r_width,
78 -cursor => 'sb_h_double_arrow',
79 )->place(
80 -bordermode => 'outside',
81 -relheight => '1.0',
82 -anchor => 'ne',
83 -relx => '1.0',
84 );
85
86 # CREATE THE COLUMNBAR
87 $this->{columnBar} = $this->parent->Frame(
88 -background => 'white',
89 -relief => 'raised',
90 -borderwidth => 2,
91 -width => 2,
92 );
93
94 $r_Widget->bind( '<ButtonRelease-1>' => sub { $this->ButtonRelease(1); } );
95 $r_Widget->bind( '<ButtonPress-1>' => sub { $this->ButtonPress(1); } );
96 $r_Widget->bind( '<Motion>' => sub { $this->ButtonOver(1); } );
97 $r_Widget->bind( '<Enter>' => sub { $this->TrimEnter(); } );
98 $r_Widget->bind( '<Leave>' => sub { $this->TrimLeave(); } );
99
100 # Override these ones too
101 $this->bind( '<Enter>' => sub { $this->BttnEnter(); } );
102 $this->bind( '<Leave>' => sub { $this->BttnLeave(); } );
103
104 $this->SUPER::Populate($args);
105 $this->ConfigSpecs(
106 -column => [ [ 'SELF', 'PASSIVE' ], 'column', 'Column', 0 ],
107 -minwidth => [ [ 'SELF', 'PASSIVE' ], 'minwidth', 'MinWidth', 50 ],
108 -closedminwidth => [ [ 'SELF', 'PASSIVE' ], 'closedminwidth', 'ClosedMinWidth', 10 ],
109 -command => [ 'CALLBACK',undef,undef, sub {}],
110 -activebackground => [ [ 'SELF', 'PASSIVE' ], 'activebackground', 'activebackground', $this->SUPER::cget(-background) ],
111 -activeforeground => [ [ 'SELF', 'PASSIVE' ], 'activeforeground', 'activeforeground', 'red' ],
112 -buttondownrelief => [ [ 'SELF', 'PASSIVE' ], 'buttondownrelief', 'buttondownrelief', 'groove' ],
113 -relief => [ [ 'SELF', 'PASSIVE' ], 'relief', 'relief', 'flat' ],
114 -pady => [ [ 'SELF', 'PASSIVE' ], 'pady', 'pady', 0 ],
115 -padx => [ [ 'SELF', 'PASSIVE' ], 'padx', 'padx', 0 ],
116 -pady => [ [ 'SELF', 'PASSIVE' ], 'pady', 'pady', 0 ],
117 -anchor => [ [ 'SELF', 'PASSIVE' ], 'anchor', 'Anchor', 'w' ],
118 -lastcolumn => [ [ 'SELF', 'PASSIVE' ], 'lastcolumn', 'LastColumn', 0 ],
119 -takefocus => [ [ 'SELF', 'PASSIVE' ], 'takefocus', 'TakeFocus', 1 ],
120 );
121
122 # Keep track of last trim widget
123 $this->{m_LastTrim} = $r_Widget;
124 # Initialize the Enter/Leave level counter
125 $this->{m_Level} = 0;
126 }
127 # CALLED IF WE ENTER THE TRIM AREA
128 sub BttnEnter
129 {
130 my $this = shift;
131 $this->StateSalvation(1);
132 $this->configure(-relief => $this->cget('-buttondownrelief')) if $this->{m_ButtonPress};
133 }
134 # CALLED IF WE LEAVE THE TRIM AREA
135 sub BttnLeave
136 {
137 my $this = shift;
138 $this->StateSalvation(-1);
139 $this->configure(-relief => $this->{m_relief}) if $this->{m_relief};
140 }
141 # CALLED IF WE ENTER THE TRIM AREA
142 # sub TrimEnter
143 # {
144 # my $this = shift;
145 # $this->ButtonOver(1);
146 # $this->StateSalvation(2);
147 # }
148 sub TrimEnter
149 {
150 my $this = shift;
151 if ($this->cget('-lastcolumn')) {
152 $this->Subwidget('Trim_R')->configure(-cursor => undef);
153 }
154 else {
155 $this->Subwidget('Trim_R')->configure(-cursor => 'sb_h_double_arrow');
156 }
157 $this->ButtonOver(1);
158 $this->StateSalvation(2);
159 }
160 # CALLED IF WE LEAVE THE TRIM AREA
161 sub TrimLeave
162 {
163 my $this = shift;
164 $this->StateSalvation(-2);
165 $this->HideColumnBar();
166 }
167
168 # CALLED IF WE CLICK/DOUBLECLICK
169 sub OpenCloseColumn
170 {
171 my $this = shift;
172
173 my $column = $this->cget('-column');
174 if ($this->{m_ColumClosed}{$column}) {
175 $this->{m_ColumClosed}{$column} = 0;
176 if ($this->{m_LastColumWidth}) {
177 $this->parent->columnWidth($column, $this->{m_LastColumWidth});
178 }
179 else {
180 $this->parent->columnWidth($column, '');
181 $this->{m_LastColumWidth} = $this->parent->columnWidth($column);
182 }
183 $this->configure(-anchor => $this->{m_LastAnchor}) if $this->{m_LastAnchor};
184 }
185 else {
186 $this->{m_ColumClosed}{$column} = 1;
187 $this->{m_LastColumWidth} = $this->parent->columnWidth($column);
188 $this->parent->columnWidth($column, $this->cget('-closedminwidth'));
189 $this->{m_LastAnchor} = $this->cget('-anchor');
190 $this->configure(-anchor => 'w');
191 }
192
193 }
194 # CALLED TO RESIZE A COLUMN TO THE NEEDED EXTENT
195 sub ColumnFullSize
196 {
197 my $this = shift;
198 my $column = $this->cget('-column');
199 if ($this->{m_ColumClosed}{$column}) {
200 delete $this->{m_LastColumWidth}; # This ensure immediate update
201 $this->OpenCloseColumn();
202 }
203 else {
204 $this->parent->columnWidth($column, '');
205 }
206 }
207
208 ## Event Handlers
209 sub ButtonPress
210 {
211 my ($this, $p_Trim) = @_;
212 $this->{m_LastEvent} = 'ButtonPress';
213 $this->{m_relief} = $this->cget('-relief');
214 if ($this->ButtonEdgeSelected() || $p_Trim) {
215 $this->{m_EdgeSelected} = 1;
216 $this->{m_X} = $this->pointerx() - $this->rootx();
217 $this->ButtonOver();
218 }
219 else {
220 $this->configure(-relief => $this->cget('-buttondownrelief'));
221 $this->{m_X} = -1;
222 }
223 $this->{m_ButtonPress} = 1;
224 }
225
226 sub ButtonRelease
227 {
228 my ($this, $p_Trim) = @_;
229 delete $this->{m_ButtonPress};
230 $this->{m_EdgeSelected} = 0;
231 $this->configure(-relief => $this->{m_relief});
232 if ($this->{columnBar}) {
233 $this->HideColumnBar();
234 }
235 if ($this->{m_X} >= 0) {
236 my $l_NewWidth = ( $this->pointerx() - $this->rootx() );
237
238 my $hlist = $this->parent;
239 my $col = $this->cget('-column');
240 # Better resize to minimum than to do nothing
241 $l_NewWidth = $this->cget('-minwidth') if ($l_NewWidth + 5) < $this->cget('-minwidth');
242 $hlist->columnWidth( $col, $l_NewWidth + 5 );
243
244 $this->GeometryRequest( $l_NewWidth, $this->reqheight() );
245 }
246 elsif ( !$this->ButtonEdgeSelected() ) {
247 # Run only if we're still over the header and if we're in TRUE Release Mode (No Dbl-Click)
248 if ($this->cget('-state') eq 'active') {
249 $this->after(500, sub { $this->Callback(-command => $this) if $this->{m_LastEvent} eq 'ButtonPress' } );
250 }
251 }
252
253 $this->{m_X} = -1;
254 }
255
256 # CALLED IF WE DOUBLECLICK
257 sub ButtonDouble1
258 {
259 my $this = shift;
260
261 # Cancel a scheduled Release-Bttn-1 - attached Event
262 $this->{m_LastEvent} = 'DoubleClick';
263
264 # Execute the double-click default action
265 $this->OpenCloseColumn();
266 }
267
268
269 # CHECK IF THE RESIZE CONTROL IS SELECTED
270 sub ButtonEdgeSelected
271 {
272 my $this = shift;
273 return ( $this->pointerx() - $this->{m_LastTrim}->rootx() ) > -1;
274 }
275
276 # CHANGE THE CURSOR OVER THE RESIZE CONTROL
277 sub ButtonOver
278 {
279 my ($this, $p_Trim) = @_;
280 if ( $this->{'m_EdgeSelected'} || $this->ButtonEdgeSelected() || $p_Trim ) {
281 $this->MoveColumnBar() if $this->{columnBar};
282 }
283 }
284 # AVOID ACTIVATING THE BUTTON, IF WE ARE IN THE TRIM
285 sub StateSalvation
286 {
287 my ($this, $newlevel) = @_;
288 if ($newlevel > 0) {
289 $this->{m_Level} |= $newlevel;
290 }
291 else {
292 $this->{m_Level} &= ~$newlevel;
293 }
294 if ($this->{m_Level} == 1 and not $this->{m_EdgeSelected}) {
295 $this->configure(-state => 'active');
296 }
297 else {
298 $this->configure(-state => 'normal');
299 }
300 }
301
302 # Move a column bar which displays on top of the HList widget
303 # to indicate the eventual size of the column.
304 sub MoveColumnBar
305 {
306 my $this = shift;
307
308 my $hlist = $this->parent;
309 my $height = $hlist->height() - $this->height();
310 my $x = $hlist->pointerx() - $hlist->rootx() + 1; # +1 for move right into gap
311
312 $this->{columnBar}->place(
313 '-x' => $x,
314 '-height' => $height - 5,
315 '-y' => $this->height() + 5,
316 ) unless $this->cget('-lastcolumn');
317 }
318 # REMOVES IT FROM DISPLAY without destroying it
319 sub HideColumnBar
320 {
321 my $this = shift;
322 $this->{columnBar}->placeForget();
323 }
324
325 ########################################################################
326 1;
327 __END__