| 1 | ######################################## SOH ########################################### | ||
| 2 | ## Function : Additional Tk Class for Listbox-type HList with Data per Item, Sorting | ||
| 3 | ## | ||
| 4 | ## Copyright (c) 2004 - 2009 Michael Krause. All rights reserved. | ||
| 5 | ## Special Thanks to B<Shaun Wandler> <wandler@unixmail.compaq.com>, whose | ||
| 6 | ## Tk::HeaderResizeButton V1.3 has been used here. | ||
| 7 | ## This program is free software; you can redistribute it and/or modify it | ||
| 8 | ## under the same terms as Perl itself. | ||
| 9 | ## | ||
| 10 | ## History : V0.1 14-Jan-2004 Class compound from HList, ResizeButton. MK | ||
| 11 | ## V0.2 20-Jan-2004 Bugfix 'headerCreate' was not catched and %args->@args. MK | ||
| 12 | ## V0.3 14-Jul-2005 Bugfix 'header Height' was not called correctly for TK 804.xx. MK | ||
| 13 | ## V0.4 13-Oct-2006 Enhancement based on feedback from Rob Seegel. MK | ||
| 14 | ## V0.5 06-Apr-2009 Enhancement based on feedback from Kai Ludick (DblClick on Header always raised HBttn-Cmd-CB). MK | ||
| 15 | ## V0.6 07-Apr-2009 Enhancement based on feedback from Kai Ludick (configurable closedcolWidth, ResizeWidth). MK | ||
| 16 | ######################################## EOH ########################################### | ||
| 17 | |||
| 18 | ############################################## | ||
| 19 | ### Use | ||
| 20 | ############################################## | ||
| 21 | use Tk::HList; | ||
| 22 | use Tk::ItemStyle; | ||
| 23 | use Tk qw(Ev); | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use Carp; | ||
| 27 | |||
| 28 | use vars qw ($VERSION); | ||
| 29 | $VERSION = '0.6'; | ||
| 30 | |||
| 31 | ######################################################################## | ||
| 32 | package Tk::HeaderResizeButton; | ||
| 33 | ######################################################################### | ||
| 34 | # Tk::HeaderResizeButton | ||
| 35 | # NOTE: This is an improved version of the Tk::ResizeButton | ||
| 36 | # Summary: This widget creates a button for use in an HList header which | ||
| 37 | # provides methods for resizing a column. This was heavily | ||
| 38 | # leveraged from Columns.pm by Damion Wilson. | ||
| 39 | # Author: Shaun Wandler, Updated by Slaven Rezic and Frank Herrmann, Michael Krause | ||
| 40 | # Date: 2009/04/07 | ||
| 41 | # Revision: 0.6 | ||
| 42 | #########################################################################= | ||
| 43 | # Note: For space reason all other documentation of Tk::HeaderResizeButton has | ||
| 44 | # been removed See Tk::HeaderResizeButton-Pod for details. | ||
| 45 | # | ||
| 46 | use base qw(Tk::Derived Tk::Button); | ||
| 47 | |||
| 48 | Construct Tk::Widget 'HeaderResizeButton'; | ||
| 49 | |||
| 50 | sub ClassInit { | ||
| 51 | my ($class, $window) = @_; | ||
| 52 | |||
| 53 | $class->SUPER::ClassInit($window); | ||
| 54 | $window->bind($class, '<ButtonRelease-1>', 'ButtonRelease'); | ||
| 55 | $window->bind($class, '<ButtonPress-1>', 'ButtonPress'); | ||
| 56 | $window->bind($class, '<Motion>', 'ButtonOver'); | ||
| 57 | $window->bind($class, '<ButtonRelease-3>', 'ColumnFullSize'); | ||
| 58 | $window->bind($class, '<Double-1>', 'ButtonDouble1'); | ||
| 59 | # Override these ones too | ||
| 60 | $window->bind($class, '<Enter>', 'BttnEnter' ); | ||
| 61 | $window->bind($class, '<Leave>', 'BttnLeave' ); | ||
| 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 | |||
| 128 | # CALLED IF WE ENTER THE HEADER AREA | ||
| 129 | sub BttnEnter | ||
| 130 | { | ||
| 131 | my $this = shift; | ||
| 132 | #print "BttnEnter\n"; | ||
| 133 | $this->StateSalvation(1); | ||
| 134 | $this->configure(-relief => $this->cget('-buttondownrelief')) if $this->{m_ButtonPress}; | ||
| 135 | |||
| 136 | } | ||
| 137 | # CALLED IF WE LEAVE THE HEADER AREA | ||
| 138 | sub BttnLeave | ||
| 139 | { | ||
| 140 | my $this = shift; | ||
| 141 | #print "BttnLeave\n"; | ||
| 142 | $this->StateSalvation(-1); | ||
| 143 | $this->configure(-relief => $this->{m_relief}) if $this->{m_relief}; | ||
| 144 | } | ||
| 145 | # CALLED IF WE ENTER THE TRIM AREA | ||
| 146 | sub TrimEnter | ||
| 147 | { | ||
| 148 | my $this = shift; | ||
| 149 | if ($this->cget(-lastcolumn)) { | ||
| 150 | $this->Subwidget('Trim_R')->configure(-cursor => undef); | ||
| 151 | } | ||
| 152 | else { | ||
| 153 | $this->Subwidget('Trim_R')->configure(-cursor => 'sb_h_double_arrow'); | ||
| 154 | } | ||
| 155 | $this->ButtonOver(1); | ||
| 156 | $this->StateSalvation(2); | ||
| 157 | } | ||
| 158 | # CALLED IF WE LEAVE THE TRIM AREA | ||
| 159 | sub TrimLeave | ||
| 160 | { | ||
| 161 | my $this = shift; | ||
| 162 | $this->StateSalvation(-2); | ||
| 163 | $this->HideColumnBar(); | ||
| 164 | } | ||
| 165 | |||
| 166 | # CALLED IF WE CLICK/DOUBLECLICK | ||
| 167 | sub OpenCloseColumn | ||
| 168 | { | ||
| 169 | my $this = shift; | ||
| 170 | |||
| 171 | my $column = $this->cget('-column'); | ||
| 172 | if ($this->{m_ColumClosed}{$column}) { | ||
| 173 | $this->{m_ColumClosed}{$column} = 0; | ||
| 174 | if ($this->{m_LastColumWidth}) { | ||
| 175 | $this->parent->columnWidth($column, $this->{m_LastColumWidth}); | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | $this->parent->columnWidth($column, ''); | ||
| 179 | $this->{m_LastColumWidth} = $this->parent->columnWidth($column); | ||
| 180 | } | ||
| 181 | $this->configure(-anchor => $this->{m_LastAnchor}) if $this->{m_LastAnchor}; | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $this->{m_ColumClosed}{$column} = 1; | ||
| 185 | $this->{m_LastColumWidth} = $this->parent->columnWidth($column); | ||
| 186 | $this->parent->columnWidth($column, $this->cget('-closedminwidth')); | ||
| 187 | $this->{m_LastAnchor} = $this->cget('-anchor'); | ||
| 188 | $this->configure(-anchor => 'w'); | ||
| 189 | } | ||
| 190 | |||
| 191 | } | ||
| 192 | # CALLED TO RESIZE A COLUMN TO THE NEEDED EXTENT | ||
| 193 | sub ColumnFullSize | ||
| 194 | { | ||
| 195 | my $this = shift; | ||
| 196 | my $column = $this->cget(-column); | ||
| 197 | if ($this->{m_ColumClosed}{$column}) { | ||
| 198 | delete $this->{m_LastColumWidth}; # This ensure immediate update | ||
| 199 | $this->OpenCloseColumn(); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | $this->parent->columnWidth($column, ''); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | ## Event Handlers | ||
| 207 | sub ButtonPress | ||
| 208 | { | ||
| 209 | my ($this, $p_Trim) = @_; | ||
| 210 | $this->{m_LastEvent} = 'ButtonPress'; | ||
| 211 | $this->{m_relief} = $this->cget('-relief'); | ||
| 212 | if ($this->ButtonEdgeSelected() || $p_Trim) { | ||
| 213 | $this->{m_EdgeSelected} = 1; | ||
| 214 | $this->{m_X} = $this->pointerx() - $this->rootx(); | ||
| 215 | $this->ButtonOver(); | ||
| 216 | } | ||
| 217 | else { | ||
| 218 | $this->configure(-relief => $this->cget('-buttondownrelief')); | ||
| 219 | $this->{m_X} = -1; | ||
| 220 | } | ||
| 221 | $this->{m_ButtonPress} = 1; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub ButtonRelease | ||
| 225 | { | ||
| 226 | my ( $this, $p_Trim ) = @_; | ||
| 227 | delete $this->{m_ButtonPress}; | ||
| 228 | $this->{m_EdgeSelected} = 0; | ||
| 229 | $this->configure(-relief => $this->{m_relief}); | ||
| 230 | if ($this->{columnBar}) { | ||
| 231 | $this->HideColumnBar(); | ||
| 232 | } | ||
| 233 | |||
| 234 | if ($this->{m_X} >= 0) { | ||
| 235 | my $l_NewWidth = ( $this->pointerx() - $this->rootx() ); | ||
| 236 | my $hlist = $this->parent; | ||
| 237 | my $col = $this->cget( -column ); | ||
| 238 | # Better resize to minimum than to do nothing | ||
| 239 | $l_NewWidth = $this->cget(-minwidth) if ($l_NewWidth + 5) < $this->cget( -minwidth ); | ||
| 240 | $hlist->columnWidth( $col, $l_NewWidth + 5 ); | ||
| 241 | |||
| 242 | $this->GeometryRequest( $l_NewWidth, $this->reqheight() ); | ||
| 243 | } | ||
| 244 | elsif ( !$this->ButtonEdgeSelected() ) { | ||
| 245 | # Run only if we're still over the header and if we're in TRUE Release Mode (No Dbl-Click) | ||
| 246 | if ($this->cget('-state') eq 'active') { | ||
| 247 | $this->after(500, sub { $this->Callback(-command => $this) if $this->{m_LastEvent} eq 'ButtonPress' } ); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | $this->{m_X} = -1; | ||
| 252 | } | ||
| 253 | |||
| 254 | # CALLED IF WE DOUBLECLICK | ||
| 255 | sub ButtonDouble1 | ||
| 256 | { | ||
| 257 | my $this = shift; | ||
| 258 | $this->{m_LastEvent} = 'DoubleClick'; | ||
| 259 | $this->OpenCloseColumn(); | ||
| 260 | } | ||
| 261 | |||
| 262 | # CHECK IF THE RESIZE CONTROL IS SELECTED | ||
| 263 | sub ButtonEdgeSelected | ||
| 264 | { | ||
| 265 | my $this = shift; | ||
| 266 | return ( $this->pointerx() - $this->{m_LastTrim}->rootx() ) > -1; | ||
| 267 | } | ||
| 268 | |||
| 269 | # CHANGE THE CURSOR OVER THE RESIZE CONTROL | ||
| 270 | sub ButtonOver | ||
| 271 | { | ||
| 272 | my ($this, $p_Trim) = @_; | ||
| 273 | if ( $this->{'m_EdgeSelected'} || $this->ButtonEdgeSelected() || $p_Trim ) { | ||
| 274 | $this->MoveColumnBar() if $this->{columnBar}; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | # AVOID ACTIVATING THE BUTTON, IF WE ARE IN THE TRIM | ||
| 278 | sub StateSalvation | ||
| 279 | { | ||
| 280 | my ($this, $newlevel) = @_; | ||
| 281 | if ($newlevel > 0) { | ||
| 282 | $this->{m_Level} |= $newlevel; | ||
| 283 | } | ||
| 284 | else { | ||
| 285 | $this->{m_Level} &= ~$newlevel; | ||
| 286 | } | ||
| 287 | if ($this->{m_Level} == 1 and not $this->{m_EdgeSelected}) { | ||
| 288 | $this->configure(-state => 'active'); | ||
| 289 | } | ||
| 290 | else { | ||
| 291 | $this->configure(-state => 'normal'); | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | # Move a column bar which displays on top of the HList widget | ||
| 296 | # to indicate the eventual size of the column. | ||
| 297 | sub MoveColumnBar | ||
| 298 | { | ||
| 299 | my $this = shift; | ||
| 300 | |||
| 301 | my $hlist = $this->parent; | ||
| 302 | my $height = $hlist->height() - $this->height(); | ||
| 303 | my $x = $hlist->pointerx() - $hlist->rootx() + 1; # +1 for move right into gap | ||
| 304 | |||
| 305 | $this->{columnBar}->place( | ||
| 306 | '-x' => $x, | ||
| 307 | '-height' => $height - 5, | ||
| 308 | '-y' => $this->height() + 5, | ||
| 309 | ) unless $this->cget(-lastcolumn); | ||
| 310 | } | ||
| 311 | # REMOVES IT FROM DISPLAY without destroying it | ||
| 312 | sub HideColumnBar | ||
| 313 | { | ||
| 314 | my $this = shift; | ||
| 315 | $this->{columnBar}->placeForget(); | ||
| 316 | } | ||
| 317 | |||
| 318 | 1; | ||
| 319 | |||
| 320 | |||
| 321 | # sub EnterFocus | ||
| 322 | # { | ||
| 323 | # print "reached EnterFocus of HList\n"; | ||
| 324 | # my $w = shift; | ||
| 325 | # print "widget is >$w<\n"; | ||
| 326 | # # return unless defined $w; | ||
| 327 | # # my $Ev = $w->XEvent; | ||
| 328 | # # my $d = $Ev->d; | ||
| 329 | # # $w->Tk::focus() if ($d eq 'NotifyAncestor' || $d eq 'NotifyNonlinear' || $d eq 'NotifyInferior'); | ||
| 330 | # | ||
| 331 | # } | ||
| 332 | |||
| 333 | ######################################################################## | ||
| 334 | package Tk::HListplus; | ||
| 335 | |||
| 336 | use base qw (Tk::Derived Tk::HList); | ||
| 337 | |||
| 338 | Construct Tk::Widget 'HListplus'; | ||
| 339 | |||
| 340 | # needed to include also the aliased commands | ||
| 341 | use Tk::Submethods ( 'header' => [qw(configure cget create delete exists size)] ); | ||
| 342 | |||
| 343 | |||
| 344 | #--------------------------------------------- | ||
| 345 | # internal Setup function | ||
| 346 | #--------------------------------------------- | ||
| 347 | sub CreateArgs | ||
| 348 | { | ||
| 349 | my ($class, $this, $args) = @_; | ||
| 350 | |||
| 351 | # New for V0.4 auto-increase the Column-num by 1 to have a more Win32 behavior | ||
| 352 | $args->{-columns}++ if $args->{-columns}; | ||
| 353 | |||
| 354 | return $class->SUPER::CreateArgs($this, $args); | ||
| 355 | } | ||
| 356 | sub Populate | ||
| 357 | { | ||
| 358 | my ($this, $args) = @_; | ||
| 359 | |||
| 360 | my $data_background = delete $args->{-databackground}; | ||
| 361 | $data_background = $this->cget ('-background') unless defined $data_background; | ||
| 362 | $this->{m_headerstyle} = delete $args->{-headerstyle} || $this->ItemStyle ('window', -padx => '0', -pady => '0', ); | ||
| 363 | |||
| 364 | #Invoke Superclass fill func | ||
| 365 | $this->SUPER::Populate($args); | ||
| 366 | } | ||
| 367 | |||
| 368 | #--------------------------------------------- | ||
| 369 | # OVERRIDE: new header function | ||
| 370 | #--------------------------------------------- | ||
| 371 | sub header | ||
| 372 | { | ||
| 373 | # Parameters | ||
| 374 | my ($this, $cmd, $column, @args) = @_; | ||
| 375 | # Locals | ||
| 376 | my (%args, %hlist_args, $key); | ||
| 377 | #print "initial header args = >@_<\n" . "- " x 60 . "\n"; | ||
| 378 | |||
| 379 | # Note that we process here only the create command | ||
| 380 | if ($cmd eq 'create') { | ||
| 381 | %args = @args; | ||
| 382 | if (defined $args{-itemtype} and $args{-itemtype} eq 'resizebutton') { | ||
| 383 | # Rip off all relevant options | ||
| 384 | foreach $key (qw(-itemtype -widget -style -borderwidth -headerbackground -relief)) { | ||
| 385 | $hlist_args{$key} = delete $args{$key} if defined $args{$key}; | ||
| 386 | } | ||
| 387 | # Take over those that make sense | ||
| 388 | $args{relief} = delete $hlist_args{relief} if $hlist_args{relief}; | ||
| 389 | $args{background} = delete $hlist_args{headerbackground} if $hlist_args{headerbackground}; | ||
| 390 | |||
| 391 | # Create a new Resize Button | ||
| 392 | my $header = $this->HeaderResizeButton( | ||
| 393 | -column => $column, | ||
| 394 | -lastcolumn => ($this->cget(-columns) == $column + 1), | ||
| 395 | -highlightthickness => 0, | ||
| 396 | %args, | ||
| 397 | ); | ||
| 398 | $header->bind('all','<Enter>','EnterFocus'); | ||
| 399 | |||
| 400 | # store it for later cget retrieval | ||
| 401 | $this->{m_headerwidget}{$column} = $header; | ||
| 402 | |||
| 403 | # Add options for parent class setup | ||
| 404 | $hlist_args{-itemtype} = 'window'; | ||
| 405 | $hlist_args{-widget} = $header; | ||
| 406 | $hlist_args{-style} = $this->{m_headerstyle} unless $hlist_args{-style}; | ||
| 407 | |||
| 408 | # pass on as new args for parental class | ||
| 409 | @args = %hlist_args; | ||
| 410 | } | ||
| 411 | } | ||
| 412 | elsif ($cmd eq 'cget') { | ||
| 413 | if ($args[0] eq '-widget') { | ||
| 414 | return $this->{m_headerwidget}{$column}; | ||
| 415 | } | ||
| 416 | # all other requests are processed the common way | ||
| 417 | } | ||
| 418 | #print "cmd = >$cmd<, column = >$column<, Args is >@args< args: " . scalar(@_) . "<\n"; | ||
| 419 | |||
| 420 | # Install the 'normal view after we have something on the screen.. | ||
| 421 | if (defined $column) { | ||
| 422 | return $this->SUPER::header($cmd, $column, @args); | ||
| 423 | } | ||
| 424 | else { | ||
| 425 | return $this->SUPER::header($cmd); | ||
| 426 | } | ||
| 427 | } | ||
| 428 | |||
| 429 | ######################################################################## | ||
| 430 | 1; | ||
| 431 | __END__ |