| 1 | ## MListbox Version 1.11 (26 Dec 2001) | ||
| 2 | ## | ||
| 3 | ## Original Author: Hans J. Helgesen, Dec 1999 | ||
| 4 | ## Maintainer: Rob Seegel (versions 1.10+) | ||
| 5 | ## | ||
| 6 | ## This version is a maintenance release of Hans' MListbox widget. | ||
| 7 | ## I have tried to avoid adding too many new features and just ensure | ||
| 8 | ## that the existing ones work properly. | ||
| 9 | ## | ||
| 10 | ## Please post feedback to comp.lang.perl.tk or email to RobSeegel@aol.com | ||
| 11 | ## | ||
| 12 | ## This module contains four classes. Of the four MListbox is | ||
| 13 | ## is the only one intended for standalone use, the other three: | ||
| 14 | ## CListbox, MLColumn, HButton are accessible as Subwidgets, but | ||
| 15 | ## not intended to be used in any other way other than as | ||
| 16 | ## components of MListbox | ||
| 17 | ## | ||
| 18 | ############################################################################## | ||
| 19 | ## CListbox is similar to an ordinary listbox, but with the following | ||
| 20 | ## differences: | ||
| 21 | ## - Calls an -updatecommand whenever something happens to it. | ||
| 22 | ## - Horizontal scanning is disabled, calls -xscancommand to let parent widget | ||
| 23 | ## handle this. | ||
| 24 | { | ||
| 25 | package Tk::CListbox; | ||
| 26 | use base qw(Tk::Derived Tk::Listbox); | ||
| 27 | |||
| 28 | Tk::Widget->Construct('CListbox'); | ||
| 29 | |||
| 30 | sub Populate { | ||
| 31 | my ($w, $args) = @_; | ||
| 32 | $w->SUPER::Populate($args); | ||
| 33 | $w->ConfigSpecs( | ||
| 34 | -background => [qw/SELF background Background/, $Tk::NORMAL_BG], | ||
| 35 | -foreground => [qw/SELF foreground Foreground/, $Tk::NORMAL_FG], | ||
| 36 | -updatecommand => ['CALLBACK'], | ||
| 37 | -xscancommand => ['CALLBACK'] | ||
| 38 | ); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub selectionSet { | ||
| 42 | my ($w) = @_; | ||
| 43 | $w->Callback(-updatecommand=>$w->can('SUPER::selectionSet'),@_); | ||
| 44 | } | ||
| 45 | sub selectionClear { | ||
| 46 | my ($w)=@_; | ||
| 47 | $w->Callback(-updatecommand=>$w->can('SUPER::selectionClear'),@_); | ||
| 48 | } | ||
| 49 | sub selectionAnchor { | ||
| 50 | my ($w)=@_; | ||
| 51 | $w->Callback(-updatecommand=>$w->can('SUPER::selectionAnchor'),@_); | ||
| 52 | } | ||
| 53 | sub activate { | ||
| 54 | my ($w)=@_; | ||
| 55 | $w->Callback(-updatecommand=>$w->can('SUPER::activate'),@_); | ||
| 56 | } | ||
| 57 | sub see { | ||
| 58 | my ($w)=@_; | ||
| 59 | $w->Callback(-updatecommand=>$w->can('SUPER::see'),@_); | ||
| 60 | } | ||
| 61 | sub yview { | ||
| 62 | my ($w)=@_; | ||
| 63 | $w->Callback(-updatecommand=>$w->can('SUPER::yview'),@_); | ||
| 64 | } | ||
| 65 | sub scan { | ||
| 66 | my ($w,$type,$x,$y) = @_; | ||
| 67 | # Disable horizontal scanning. | ||
| 68 | if ($type eq 'mark') { | ||
| 69 | $w->{'_scanmark_x'} = $x; | ||
| 70 | } | ||
| 71 | $w->Callback(-updatecommand=>$w->can('SUPER::scan'), | ||
| 72 | $w, $type, $w->{'_scanmark_x'}, $y | ||
| 73 | ); | ||
| 74 | $w->Callback(-xscancommand=>$type,$x); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | ############################################################################## | ||
| 79 | ## HButton is like an ordinary Button, but with an addition option: | ||
| 80 | ## -pixelwidth | ||
| 81 | ## The new configure method makes sure the pixelwidth is always retained. | ||
| 82 | { | ||
| 83 | package Tk::HButton; | ||
| 84 | use base qw(Tk::Derived Tk::Button); | ||
| 85 | Tk::Widget->Construct('HButton'); | ||
| 86 | |||
| 87 | sub Populate { | ||
| 88 | my ($w, $args) = @_; | ||
| 89 | $w->SUPER::Populate($args); | ||
| 90 | $w->ConfigSpecs( | ||
| 91 | -pixelwidth => ['PASSIVE'], | ||
| 92 | -background => [qw/SELF background Background/, $Tk::NORMAL_BG], | ||
| 93 | -foreground => [qw/SELF foreground Foreground/, $Tk::NORMAL_FG] | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub configure { | ||
| 98 | my $w = shift; | ||
| 99 | my (@ret) = $w->SUPER::configure(@_); | ||
| 100 | unless (@ret) { | ||
| 101 | if (defined(my $pixels = $w->cget('-pixelwidth'))) { | ||
| 102 | $w->GeometryRequest($pixels,$w->reqheight); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | return @ret; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | ############################################################################### | ||
| 110 | ## MLColumn implements a single column in the MListbox. MLColumn is a composite | ||
| 111 | ## containing a heading (an HButton), a listbox (CListbox) and a frame which | ||
| 112 | ## frame which serves as a draggable separator | ||
| 113 | { | ||
| 114 | package Tk::MLColumn; | ||
| 115 | use base qw(Tk::Frame); | ||
| 116 | Tk::Widget->Construct('MLColumn'); | ||
| 117 | |||
| 118 | sub Populate { | ||
| 119 | my ($w, $args) = @_; | ||
| 120 | $w->SUPER::Populate($args); | ||
| 121 | |||
| 122 | ## MLColumn Components | ||
| 123 | ## $sep - separator - Frame | ||
| 124 | ## $hdr - heading - HButton | ||
| 125 | ## $f - frame - Frame | ||
| 126 | ## $lb - listbox - CListbox | ||
| 127 | |||
| 128 | my $sep = $w->Component( | ||
| 129 | Frame => 'separator', | ||
| 130 | -height => 1 | ||
| 131 | )->pack(qw/-side right -fill y -anchor w/); | ||
| 132 | |||
| 133 | $sep->bind( "<B1-Motion>", | ||
| 134 | [$w=>'adjustMotion']); | ||
| 135 | $sep->bind("<ButtonRelease-1>", | ||
| 136 | [$w=>'Callback','-configurecommand']); | ||
| 137 | |||
| 138 | my $f = $w->Component( | ||
| 139 | Frame => "frame" | ||
| 140 | )->pack(qw/-side left -anchor e -fill y -expand 1/); | ||
| 141 | |||
| 142 | my $hdr = $f->HButton( | ||
| 143 | -takefocus=>0, | ||
| 144 | -padx=>0, | ||
| 145 | -width=>1, | ||
| 146 | -borderwidth=>2, | ||
| 147 | -highlightthickness=>0 | ||
| 148 | )->pack(qw/-side top -anchor n -fill x/); | ||
| 149 | $w->Advertise("heading" => $hdr); | ||
| 150 | |||
| 151 | my $lb = $f->CListbox( | ||
| 152 | -highlightthickness=>0, | ||
| 153 | -relief=>'flat', | ||
| 154 | -bd=>0, | ||
| 155 | -exportselection=>0, | ||
| 156 | -takefocus=>0 | ||
| 157 | )->pack(qw/-side top -anchor n -expand 1 -fill both/); | ||
| 158 | $w->Advertise("listbox" => $lb); | ||
| 159 | |||
| 160 | $w->Delegates (DEFAULT => $lb); | ||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | $w->ConfigSpecs( | ||
| 165 | -background => [[$f, $hdr, $lb], | ||
| 166 | qw/background Background/, $Tk::NORMAL_BG], | ||
| 167 | -comparecommand => ['CALLBACK', undef, undef, | ||
| 168 | sub{$_[0] cmp $_[1]}], | ||
| 169 | -configurecommand => ['CALLBACK'], | ||
| 170 | |||
| 171 | -font => [[$hdr, $lb], qw/font Font/, undef], | ||
| 172 | -foreground => [[$hdr, $lb], | ||
| 173 | qw/foreground Foreground/, $Tk::NORMAL_FG], | ||
| 174 | -separatorwidth => [{-width => $sep}, | ||
| 175 | qw/separatorWidth Separator 1/], | ||
| 176 | -separatorcolor => [{-background => $sep}, | ||
| 177 | qw/sepaatorColor Separator black/], | ||
| 178 | -resizeable => [qw/METHOD resizeable Resizeable 1/], | ||
| 179 | -sortable => [qw/PASSIVE sortable Sortable 1/], | ||
| 180 | -text => [$hdr], | ||
| 181 | -updatecommand => [$lb], | ||
| 182 | -textwidth => [{-width => [$lb, $hdr]}], | ||
| 183 | DEFAULT => [$lb] | ||
| 184 | ); | ||
| 185 | $w->ConfigAlias( | ||
| 186 | -comparecmd => '-comparecommand', | ||
| 187 | -width => '-textwidth' | ||
| 188 | ); | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | ###################################################################### | ||
| 193 | ## MLColumn Configuration methods (call via configure/cget). | ||
| 194 | ###################################################################### | ||
| 195 | |||
| 196 | sub resizeable { | ||
| 197 | my ($w, $value) = @_; | ||
| 198 | return $w->{Configure}{-resizeable} unless | ||
| 199 | defined $value; | ||
| 200 | $w->Subwidget("separator")->configure( | ||
| 201 | -cursor => ($value ? 'sb_h_double_arrow' : 'xterm') | ||
| 202 | ); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub compare { | ||
| 206 | my ($w,$a,$b) = @_; | ||
| 207 | $w->Callback(-comparecommand => $a, $b); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub setWidth { | ||
| 211 | my ($w, $pixels) = @_; | ||
| 212 | $pixels -= $w->Subwidget("separator")->width; | ||
| 213 | return | ||
| 214 | unless $pixels >= 0; | ||
| 215 | |||
| 216 | $w->Subwidget("listbox")->GeometryRequest( | ||
| 217 | $pixels,$w->Subwidget("listbox")->height); | ||
| 218 | $w->Subwidget("heading")->configure(-pixelwidth=>$pixels); | ||
| 219 | } | ||
| 220 | |||
| 221 | ###################################################################### | ||
| 222 | ## MLColumn Private methods (Do not depend on these methods being present) | ||
| 223 | ###################################################################### | ||
| 224 | |||
| 225 | ## Adjust size of column. | ||
| 226 | sub adjustMotion { | ||
| 227 | my ($w) = @_; | ||
| 228 | $w->setWidth($w->pointerx - $w->rootx); | ||
| 229 | } | ||
| 230 | |||
| 231 | } ## END PRELOADING OF MLColumn | ||
| 232 | |||
| 233 | ###################################################################### | ||
| 234 | ## Package: Tk::MListbox | ||
| 235 | ## Purpose: Multicolumn widget used to display tabular data | ||
| 236 | ## This widget has the ability to sort data by column, | ||
| 237 | ## hide/show columns, and the ability to change the order | ||
| 238 | ## of columns on the fly | ||
| 239 | |||
| 240 | package Tk::MListbox; | ||
| 241 | use strict; | ||
| 242 | use Carp; | ||
| 243 | use vars qw($VERSION); | ||
| 244 | $VERSION = '1.11'; | ||
| 245 | |||
| 246 | use Tk; | ||
| 247 | |||
| 248 | ## Overidden Scrolled method to suit the purposes of MListbox | ||
| 249 | ## I want -columns to be configured LAST no matter what. | ||
| 250 | ## I know full well that I'm overriding the Scrolled method | ||
| 251 | ## and I don't need a warning broadcasting the fact. | ||
| 252 | |||
| 253 | no warnings; | ||
| 254 | sub Tk::Widget::Scrolled { | ||
| 255 | my ($parent, $kind, %args) = @_; | ||
| 256 | |||
| 257 | my $colAR; | ||
| 258 | $colAR = delete $args{'-columns'} if $kind eq "MListbox"; | ||
| 259 | |||
| 260 | ## Find args that are Frame create time args | ||
| 261 | my @args = Tk::Frame->CreateArgs($parent,\%args); | ||
| 262 | my $name = delete $args{'Name'}; | ||
| 263 | push(@args,'Name' => $name) if (defined $name); | ||
| 264 | my $cw = $parent->Frame(@args); | ||
| 265 | @args = (); | ||
| 266 | |||
| 267 | ## Now remove any args that Frame can handle | ||
| 268 | foreach my $k ('-scrollbars',map($_->[0],$cw->configure)) { | ||
| 269 | push(@args,$k,delete($args{$k})) if (exists $args{$k}) | ||
| 270 | } | ||
| 271 | ## Anything else must be for target widget - pass at widget create time | ||
| 272 | my $w = $cw->$kind(%args); | ||
| 273 | |||
| 274 | ## Now re-set %args to be ones Frame can handle | ||
| 275 | ## RCS NOTE: I've also slightly modified the ConfigSpecs | ||
| 276 | %args = @args; | ||
| 277 | $cw->ConfigSpecs( | ||
| 278 | '-scrollbars' => [qw/METHOD scrollbars Scrollbars se/], | ||
| 279 | '-background' => [$w, qw/background Background/, undef], | ||
| 280 | '-foreground' => [$w, qw/foreground Foreground/, undef], | ||
| 281 | ); | ||
| 282 | $cw->AddScrollbars($w); | ||
| 283 | $cw->Default("\L$kind" => $w); | ||
| 284 | $cw->Delegates('bind' => $w, 'bindtags' => $w, 'menu' => $w); | ||
| 285 | $cw->ConfigDefault(\%args); | ||
| 286 | $cw->configure(%args); | ||
| 287 | $cw->configure(-columns => $colAR) if $colAR; | ||
| 288 | return $cw; | ||
| 289 | } | ||
| 290 | use warnings; | ||
| 291 | |||
| 292 | require Tk::Pane; | ||
| 293 | use base qw(Tk::Frame); | ||
| 294 | Tk::Widget->Construct('MListbox'); | ||
| 295 | |||
| 296 | sub ClassInit { | ||
| 297 | my ($class,$mw) = @_; | ||
| 298 | $mw->bind($class,'<Configure>',['_yscrollCallback']); | ||
| 299 | $mw->bind($class,'<Down>',['_upDown',1]); | ||
| 300 | $mw->bind($class,'<Up>', ['_upDown',-1]); | ||
| 301 | $mw->bind($class,'<Shift-Up>', ['_extendUpDown',-1]); | ||
| 302 | $mw->bind($class,'<Shift-Down>',['_extendUpDown',1]); | ||
| 303 | $mw->bind($class,'<Control-Home>','_cntrlHome'); | ||
| 304 | $mw->bind($class,'<Control-End>','_cntrlEnd'); | ||
| 305 | $mw->bind($class,'<Shift-Control-Home>',['_dataExtend',0]); | ||
| 306 | $mw->bind($class,'<Shift-Control-End>',['_dataExtend',Ev('index', 'end')]); | ||
| 307 | $mw->bind($class,'<Control-slash>','_selectAll'); | ||
| 308 | $mw->bind($class,'<Control-backslash>','_deselectAll'); | ||
| 309 | } | ||
| 310 | |||
| 311 | ## Do some slightly tricky stuff: The -columns option, if called is | ||
| 312 | ## guaranteed to be confiugred last of all the options submitted. | ||
| 313 | ## NOTE: The args hash is cleared out if a columns option is sent | ||
| 314 | ## so that all the options won't be reconfigured again immediately | ||
| 315 | ## after this method finishes. ALso, if Scrolled is called, then | ||
| 316 | ## the -columns option will never make it down to this level so | ||
| 317 | |||
| 318 | sub InitObject { | ||
| 319 | my ($w, $args) = @_; | ||
| 320 | |||
| 321 | my $colAR = delete $args->{'-columns'}; | ||
| 322 | $w->Populate($args); | ||
| 323 | $w->ConfigDefault($args); | ||
| 324 | if ($colAR) { | ||
| 325 | $w->configure(%$args); | ||
| 326 | $w->configure(-columns => $colAR); | ||
| 327 | %$args = (); | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | sub Populate { | ||
| 332 | my ($w, $args) = @_; | ||
| 333 | |||
| 334 | # $w->SUPER::Populate($args); | ||
| 335 | |||
| 336 | $w->{'_columns'} = []; ## Array of MLColumn objects | ||
| 337 | $w->{'_sortcol'} = -1; ## Column used for sorting | ||
| 338 | $w->{'_sort_descending'} = 0; ## Flag for ascending/desc. sort order | ||
| 339 | $w->{'_top'} = 0; | ||
| 340 | $w->{'_bottom'} = 0; | ||
| 341 | |||
| 342 | my $pane = $w->Component( | ||
| 343 | Pane => "pane", | ||
| 344 | -sticky => 'nsew' | ||
| 345 | )->pack(-expand=>1,-fill=>'both'); | ||
| 346 | |||
| 347 | my $font; | ||
| 348 | if ($Tk::platform eq 'MSWin32') { | ||
| 349 | $font = "{MS Sans Serif} 8"; | ||
| 350 | } else { | ||
| 351 | $font = "Helvetica -12 bold"; | ||
| 352 | } | ||
| 353 | |||
| 354 | $w->ConfigSpecs( | ||
| 355 | -background => [qw/METHOD background Background/, | ||
| 356 | $TK::NORMAL_BG ], | ||
| 357 | -columns => [qw/METHOD/], | ||
| 358 | -configurecommand => [qw/CALLBACK/], | ||
| 359 | -font => [qw/METHOD font Font/, $font], | ||
| 360 | -foreground => [qw/METHOD foreground Foreground/, | ||
| 361 | $Tk::NORMAL_FG ], | ||
| 362 | -height => [qw/METHOD height Height 10/], | ||
| 363 | -moveable => [qw/PASSIVE moveable Moveable 1/], | ||
| 364 | -resizeable => [qw/METHOD resizeable Resizeable 1/], | ||
| 365 | -selectbackground => [qw/METHOD selectBackground Background/, | ||
| 366 | $Tk::SELECT_BG], | ||
| 367 | -selectborderwidth => [qw/METHOD selectBorderwidth Borderwidth 1/], | ||
| 368 | -selectforeground => [qw/METHOD selectForeground Foreground/, | ||
| 369 | $Tk::SELECT_FG], | ||
| 370 | -selectmode => [qw/METHOD selectMode Mode browse/], | ||
| 371 | -separatorcolor => [qw/METHOD separatorColor Separator black/], | ||
| 372 | -separatorwidth => [qw/METHOD separatorWidth Separator 1/], | ||
| 373 | -sortable => [qw/METHOD sortable Sortable 1/], | ||
| 374 | -takefocus => [qw/PASSIVE takeFocus Focus 1/], | ||
| 375 | -textwidth => [qw/METHOD textWidth Width 10/], | ||
| 376 | -width => [qw/METHOD width Width/, undef], | ||
| 377 | -xscrollcommand => [$pane], | ||
| 378 | -yscrollcommand => ['CALLBACK'], | ||
| 379 | ); | ||
| 380 | |||
| 381 | $w->ConfigAlias( | ||
| 382 | -selectbg => "-selectbackground", | ||
| 383 | -selectbd => "-selectborderwidth", | ||
| 384 | -selectfg => "-selectforeground", | ||
| 385 | -sepcolor => "-separatorcolor", | ||
| 386 | -sepwidth => "-separatorwidth", | ||
| 387 | ); | ||
| 388 | } | ||
| 389 | |||
| 390 | ###################################################################### | ||
| 391 | ## Configuration methods (call via configure). | ||
| 392 | ###################################################################### | ||
| 393 | |||
| 394 | ## Background is a slightly tricky option, this option would be a | ||
| 395 | ## great candidate for "DESCENDANTS", except for the separator subwidget in | ||
| 396 | ## each column set by separatorcolor which I'd prefer not to set in such | ||
| 397 | ## a clumsy way. All other background colors are fair game, but I'd like | ||
| 398 | ## to leave open the possibility for other exceptions such as separator. | ||
| 399 | ## Besides I prefer that composite subwidgets manage their own component parts | ||
| 400 | ## as much as possible. | ||
| 401 | |||
| 402 | sub background { | ||
| 403 | my ($w, $val) = @_; | ||
| 404 | return $w->{Configure}{'-background'} | ||
| 405 | unless $val; | ||
| 406 | |||
| 407 | ## Ensure that the base Frame, pane and columns (if any) get set | ||
| 408 | Tk::configure($w, "-background", $val); | ||
| 409 | $w->Subwidget("pane")->configure("-background", $val); | ||
| 410 | $w->_configureColumns("-background", $val); | ||
| 411 | } | ||
| 412 | |||
| 413 | ## columns needs to be called last during creation time if set and I | ||
| 414 | ## went to a great deal of trouble to guarantee this. The reason | ||
| 415 | ## being is that it needs to use many of the other configurations to | ||
| 416 | ## use as defaults for columns, and the ability to override any of them | ||
| 417 | ## for individual columns. If these options (that the columns override) | ||
| 418 | ## were called afterwards, then the reverse would happen. All the default | ||
| 419 | ## would override the individually specified options. | ||
| 420 | |||
| 421 | sub columns { | ||
| 422 | my ($w, $vAR) = @_; | ||
| 423 | return $w->{Configure}{'-columns'} unless | ||
| 424 | defined $vAR; | ||
| 425 | $w->columnDelete(0, 'end'); | ||
| 426 | map {$w->columnInsert('end', @$_)} @$vAR; | ||
| 427 | } | ||
| 428 | |||
| 429 | sub font { shift->_configureColumns('-font', @_) } | ||
| 430 | sub foreground { shift->_configureColumns('-foreground', @_) } | ||
| 431 | sub height { shift->_configureColumns('-height', @_) } | ||
| 432 | sub resizeable { shift->_configureColumns('-resizeable', @_) } | ||
| 433 | sub selectbackground { shift->_configureColumns('-selectbackground', @_) } | ||
| 434 | sub selectborderwidth { shift->_configureColumns('-selectborderwidth', @_) } | ||
| 435 | sub selectforeground { shift->_configureColumns('-selectforeground', @_) } | ||
| 436 | sub selectmode { shift->_configureColumns('-selectmode', @_) } | ||
| 437 | sub separatorcolor { shift->_configureColumns('-separatorcolor', @_ ) } | ||
| 438 | sub separatorwidth { shift->_configureColumns('-separatorwidth', @_ ) } | ||
| 439 | sub sortable { shift->_configureColumns('-sortable', @_) } | ||
| 440 | sub textwidth { shift->_configureColumns('-textwidth', @_) } | ||
| 441 | |||
| 442 | sub width { | ||
| 443 | my ($w, $v) = @_; | ||
| 444 | |||
| 445 | return $w->{Configure}{'-width'} unless defined $v; | ||
| 446 | if ($v == 0) { | ||
| 447 | $w->afterIdle(['_setWidth', $w]); | ||
| 448 | } else { | ||
| 449 | $w->Subwidget('pane')->configure(-width => $v); | ||
| 450 | } | ||
| 451 | } | ||
| 452 | |||
| 453 | ###################################################################### | ||
| 454 | ## Private methods (Do not depend on these methods being present) | ||
| 455 | ## | ||
| 456 | ## For all methods which have _firstVisible, the method is delegated | ||
| 457 | ## to the first visible (packed) Listbox | ||
| 458 | ###################################################################### | ||
| 459 | |||
| 460 | ## This is the main callback that is bound to the subwidgets | ||
| 461 | ## when using any of the public bind methods, The defined | ||
| 462 | ## defined callback ($cb) is called from within it | ||
| 463 | |||
| 464 | sub _bindCallback { | ||
| 465 | my ($w, $cb, $sw, $ci, $yCoord) = @_; | ||
| 466 | |||
| 467 | my $iHR = { '-subwidget' => $sw, '-column' => $ci }; | ||
| 468 | if (defined($yCoord)) { | ||
| 469 | $iHR->{'-row'} = $w->_getEntryFromY($sw, $yCoord); | ||
| 470 | } | ||
| 471 | if (ref $cb eq 'ARRAY') { | ||
| 472 | my ($code,@args) = @$cb; | ||
| 473 | return $w->$code($iHR, @args); | ||
| 474 | } else { | ||
| 475 | return $w->$cb($iHR); | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | ## bind subwidgets is used by other public bind methods to | ||
| 480 | ## apply a callback to an event dequence of a particular subwidget | ||
| 481 | ## within each of the columns. Any defined callbacks are passed | ||
| 482 | ## to the _bindCallback which is actually the callback that gets | ||
| 483 | ## bound. | ||
| 484 | |||
| 485 | sub _bindSubwidgets { | ||
| 486 | my ($w,$subwidget,$sequence,$callback) = @_; | ||
| 487 | my $col = 0; | ||
| 488 | |||
| 489 | return (keys %{$w->{'_bindings'}->{$subwidget}}) | ||
| 490 | unless (defined $sequence); | ||
| 491 | |||
| 492 | unless (defined $callback) { | ||
| 493 | $callback = $w->{'_bindings'}->{$subwidget}->{$sequence}; | ||
| 494 | $callback = '' unless defined $callback; | ||
| 495 | return $callback; | ||
| 496 | } | ||
| 497 | |||
| 498 | if ($callback eq '') { | ||
| 499 | foreach (@{$w->{'_columns'}}) { | ||
| 500 | $_->Subwidget($subwidget)->Tk::bind($sequence,''); | ||
| 501 | } | ||
| 502 | delete $w->{'_bindings'}->{$subwidget}->{$sequence}; | ||
| 503 | return ''; | ||
| 504 | } | ||
| 505 | my @args = ('_bindCallback', $callback); | ||
| 506 | foreach (@{$w->{'_columns'}}) { | ||
| 507 | my $sw = $_->Subwidget($subwidget); | ||
| 508 | if ($sw->class ne "CListbox") { | ||
| 509 | $sw->Tk::bind($sequence, [$w => @args, $sw, $col++]); | ||
| 510 | } else { | ||
| 511 | $sw->Tk::bind($sequence, [$w => @args, $sw, $col++, Ev('y')]); | ||
| 512 | } | ||
| 513 | } | ||
| 514 | $w->{'_bindings'}->{$subwidget}->{$sequence} = $callback; | ||
| 515 | return ''; | ||
| 516 | } | ||
| 517 | |||
| 518 | ## handles config options that should be propagated to all MLColumn | ||
| 519 | ## subwidgets. Using the DEFAULT setting in ConfigSpecs would be one | ||
| 520 | ## idea, but the pane subwidget is also a child, and Pane will not | ||
| 521 | ## be able to handle many of the options being passed to this method. | ||
| 522 | |||
| 523 | sub _configureColumns { | ||
| 524 | my ($w, $option, $value) = @_; | ||
| 525 | return $w->{Configure}{$option} | ||
| 526 | unless $value; | ||
| 527 | |||
| 528 | foreach (@{$w->{'_columns'}}) { | ||
| 529 | $_->configure("$option" => $value); | ||
| 530 | } | ||
| 531 | } | ||
| 532 | |||
| 533 | sub _cntrlEnd { shift->_firstVisible->Cntrl_End; } | ||
| 534 | |||
| 535 | sub _cntrlHome { shift->_firstVisible->Cntrl_Home; } | ||
| 536 | |||
| 537 | sub _dataExtend { | ||
| 538 | my ($w, $el) = @_; | ||
| 539 | my $mode = $w->cget('-selectmode'); | ||
| 540 | if ($mode eq 'extended') { | ||
| 541 | $w->activate($el); | ||
| 542 | $w->see($el); | ||
| 543 | if ($w->selectionIncludes('anchor')) { | ||
| 544 | $w->_firstVisible->Motion($el) | ||
| 545 | } | ||
| 546 | } elsif ($mode eq 'multiple') { | ||
| 547 | $w->activate($el); | ||
| 548 | $w->see($el) | ||
| 549 | } | ||
| 550 | } | ||
| 551 | |||
| 552 | sub _deselectAll { | ||
| 553 | my $w = shift; | ||
| 554 | if ($w->cget('-selectmode') ne 'browse') { | ||
| 555 | $w->selectionClear(0, 'end'); | ||
| 556 | } | ||
| 557 | } | ||
| 558 | |||
| 559 | ## implements sorting and dragging & drop of a column | ||
| 560 | sub _dragOrSort { | ||
| 561 | my ($w, $c) = @_; | ||
| 562 | |||
| 563 | unless ($w->cget('-moveable')) { | ||
| 564 | if ($c->cget('-sortable')) { | ||
| 565 | $w->sort (undef, $c); | ||
| 566 | } | ||
| 567 | return; | ||
| 568 | } | ||
| 569 | |||
| 570 | my $h=$c->Subwidget("heading"); # The heading button of the column. | ||
| 571 | |||
| 572 | my $start_mouse_x = $h->pointerx; | ||
| 573 | my $y_pos = $h->rooty; # This is constant through the whole operation. | ||
| 574 | my $width = $h->width; | ||
| 575 | my $left_limit = $w->rootx - 1; | ||
| 576 | |||
| 577 | # Find the rightmost, visible column | ||
| 578 | my $right_end = 0; | ||
| 579 | foreach (@{$w->{'_columns'}}) { | ||
| 580 | if ($_->rootx + $_->width > $right_end) { | ||
| 581 | $right_end = $_->rootx + $_->width; | ||
| 582 | } | ||
| 583 | } | ||
| 584 | my $right_limit = $right_end + 1; | ||
| 585 | |||
| 586 | # Create a "copy" of the heading button, put it in a toplevel that matches | ||
| 587 | # the size of the button, put the toplevel on top of the button. | ||
| 588 | my $tl=$w->Toplevel; | ||
| 589 | $tl->overrideredirect(1); | ||
| 590 | $tl->geometry(sprintf("%dx%d+%d+%d", | ||
| 591 | $h->width, $h->height, $h->rootx, $y_pos)); | ||
| 592 | |||
| 593 | my $b=$tl->HButton | ||
| 594 | (map{defined($_->[4]) ? ($_->[0]=>$_->[4]) : ()} $h->configure) | ||
| 595 | ->pack(-expand=>1,-fill=>'both'); | ||
| 596 | |||
| 597 | # Move the toplevel with the mouse (as long as Button-1 is down). | ||
| 598 | $h->bind("<Motion>", sub { | ||
| 599 | my $new_x = $h->rootx - ($start_mouse_x - $h->pointerx); | ||
| 600 | unless ($new_x + $width/2 < $left_limit || | ||
| 601 | $new_x + $width/2 > $right_limit) | ||
| 602 | { | ||
| 603 | $tl->geometry(sprintf("+%d+%d",$new_x,$y_pos)); | ||
| 604 | } | ||
| 605 | }); | ||
| 606 | |||
| 607 | $h->bind("<ButtonRelease-1>", sub { | ||
| 608 | my $rootx = $tl->rootx; | ||
| 609 | my $x = $rootx + ($tl->width/2); | ||
| 610 | $tl->destroy; # Don't need this anymore... | ||
| 611 | $h->bind("<Motion>",''); # Cancel binding | ||
| 612 | |||
| 613 | if ($h->rootx == $rootx) { | ||
| 614 | # Button NOT moved, sort the column.... | ||
| 615 | if ($c->cget('-sortable')) { | ||
| 616 | $w->sort(undef, $c); | ||
| 617 | } | ||
| 618 | return; | ||
| 619 | } | ||
| 620 | |||
| 621 | # Button moved..... | ||
| 622 | # Decide where to put the column. If the center of the dragged | ||
| 623 | # button is on the left half of another heading, insert it -before | ||
| 624 | # the column, otherwise insert it -after the column. | ||
| 625 | foreach (@{$w->{'_columns'}}) { | ||
| 626 | if ($_->ismapped) { | ||
| 627 | my $left = $_->rootx; | ||
| 628 | my $right = $left + $_->width; | ||
| 629 | if ($left <= $x && $x <= $right) { | ||
| 630 | if ($x - $left < $right - $x) { | ||
| 631 | $w->columnShow($c,-before=>$_); | ||
| 632 | } else { | ||
| 633 | $w->columnShow($c,'-after'=>$_); | ||
| 634 | } | ||
| 635 | $w->update; | ||
| 636 | $w->Callback(-configurecommand => $w); | ||
| 637 | } | ||
| 638 | } | ||
| 639 | } | ||
| 640 | }); | ||
| 641 | } | ||
| 642 | |||
| 643 | sub _extendUpDown { | ||
| 644 | my ($w, $amount) = @_; | ||
| 645 | if ($w->cget('-selectmode') ne 'extended') { | ||
| 646 | return; | ||
| 647 | } | ||
| 648 | $w->activate($w->index('active')+$amount); | ||
| 649 | $w->see('active'); | ||
| 650 | $w->_motion($w->index('active')) | ||
| 651 | } | ||
| 652 | |||
| 653 | ## Many of the methods in this package are very similar in that they | ||
| 654 | ## delagate calls to the MLColumn widgets. Because widgets can be | ||
| 655 | ## be moved around (repacked) and hidden (packForget), any | ||
| 656 | ## one widget may not be the "best" to be delegating calls to. The | ||
| 657 | ## _columns variable holds an array of the columns but the order of | ||
| 658 | ## this array does not correspond to the order in which they might | ||
| 659 | ## by displayed, therefore this method is used to return the first | ||
| 660 | ## "visible" or packed MLColumn. RCS Note: It might be reasonable to | ||
| 661 | ## make this a public method as it could conceivably useful to someone | ||
| 662 | ## who might want to subclass this widget or use their own bindings. | ||
| 663 | sub _firstVisible { | ||
| 664 | my $w = shift; | ||
| 665 | foreach my $c (@{$w->{'_columns'}}) { | ||
| 666 | return $c if $c->ismapped; | ||
| 667 | } | ||
| 668 | return $w->{'_columns'}->[0]; | ||
| 669 | } | ||
| 670 | |||
| 671 | sub _getEntryFromY { | ||
| 672 | my ($cw, $sw, $yCoord) = @_; | ||
| 673 | my $nearest = $sw->nearest($yCoord); | ||
| 674 | |||
| 675 | return $nearest | ||
| 676 | if ($nearest < ($sw->size() - 1)); | ||
| 677 | |||
| 678 | my ($x, $y, $w, $h) = $sw->bbox($nearest); | ||
| 679 | my $lastY = $y + $h; | ||
| 680 | return -1 | ||
| 681 | if ($yCoord > $lastY); | ||
| 682 | return $nearest; | ||
| 683 | } | ||
| 684 | |||
| 685 | ## Used to distribute method calls which would otherwise be called for | ||
| 686 | ## for one CListbox (Within a column), Each CListbox is a modified | ||
| 687 | ## Listbox whose methods end up passing the code and arguments that need | ||
| 688 | ## to be called to this method where they are invoked for each column | ||
| 689 | ## It's an interesting, although complex, interaction and it's worth | ||
| 690 | ## tracing to follow the program flow. | ||
| 691 | |||
| 692 | sub _motion { shift->_firstVisible->Motion(@_) } | ||
| 693 | sub _selectAll { shift->_firstVisible->SelectAll; } | ||
| 694 | |||
| 695 | sub _selectionUpdate { | ||
| 696 | my ($w, $code, $l, @args) = @_; | ||
| 697 | |||
| 698 | if (@args) { | ||
| 699 | foreach (@{$w->{'_columns'}}) { | ||
| 700 | &$code($_->Subwidget("listbox"), @args); | ||
| 701 | } | ||
| 702 | } else { | ||
| 703 | &$code($w->{'_columns'}->[0]->Subwidget("listbox")); | ||
| 704 | } | ||
| 705 | } | ||
| 706 | |||
| 707 | ## dynamically sets the width of the widget by calculating | ||
| 708 | ## the width of each of the currently visible columns. | ||
| 709 | ## This is generally called during creation time when -width | ||
| 710 | ## is set to 0. | ||
| 711 | |||
| 712 | sub _setWidth { | ||
| 713 | my ($w) = shift; | ||
| 714 | my $width = 0; | ||
| 715 | foreach my $c (@{$w->{'_columns'}}) { | ||
| 716 | my $lw = $c->Subwidget('heading')->reqwidth; | ||
| 717 | my $sw = $c->Subwidget('separator')->reqwidth; | ||
| 718 | $width += ($lw + $sw); | ||
| 719 | } | ||
| 720 | $w->Subwidget('pane')->configure(-width => $width); | ||
| 721 | } | ||
| 722 | |||
| 723 | |||
| 724 | |||
| 725 | sub _upDown { shift->_firstVisible->UpDown(@_) } | ||
| 726 | |||
| 727 | sub _yscrollCallback { | ||
| 728 | my ($w, $top, $bottom) = @_; | ||
| 729 | |||
| 730 | unless ($w->cget(-yscrollcommand)) { | ||
| 731 | return; | ||
| 732 | } | ||
| 733 | |||
| 734 | unless (defined($top)) { | ||
| 735 | # Called internally | ||
| 736 | my $c = $w->_firstVisible; | ||
| 737 | if (Exists($c) && $c->ismapped){ | ||
| 738 | ($top,$bottom) = $c->yview; | ||
| 739 | } else { | ||
| 740 | ($top,$bottom) = (0,1); | ||
| 741 | } | ||
| 742 | } | ||
| 743 | |||
| 744 | if ($top != $w->{'_top'} || $bottom != $w->{'_bottom'}) { | ||
| 745 | $w->Callback(-yscrollcommand=>$top,$bottom); | ||
| 746 | $w->{'_top'} = $top; | ||
| 747 | $w->{'_bottom'} = $bottom; | ||
| 748 | } | ||
| 749 | } | ||
| 750 | |||
| 751 | ###################################################################### | ||
| 752 | ## Exported (Public) methods (listed alphabetically) | ||
| 753 | ###################################################################### | ||
| 754 | |||
| 755 | ## Activate a row | ||
| 756 | sub activate { shift->_firstVisible->activate(@_)} | ||
| 757 | |||
| 758 | sub bindColumns { shift->_bindSubwidgets('heading',@_) } | ||
| 759 | sub bindRows { shift->_bindSubwidgets('listbox',@_) } | ||
| 760 | sub bindSeparators { shift->_bindSubwidgets('separator',@_) } | ||
| 761 | |||
| 762 | sub columnConfigure { | ||
| 763 | my ($w, $index, %args) = @_; | ||
| 764 | $w->columnGet($index)->configure(%args); | ||
| 765 | } | ||
| 766 | |||
| 767 | ## Delete a column. | ||
| 768 | sub columnDelete { | ||
| 769 | my ($w, $first, $last) = @_; | ||
| 770 | |||
| 771 | for (my $i=$w->columnIndex($first); $i<=$w->columnIndex($last); $i++) { | ||
| 772 | $w->columnGet($i)->destroy; | ||
| 773 | } | ||
| 774 | @{$w->{'_columns'}} = map{Exists($_) ? $_ : ()} @{$w->{'_columns'}}; | ||
| 775 | } | ||
| 776 | |||
| 777 | sub columnGet { | ||
| 778 | my ($w, $from, $to) = @_; | ||
| 779 | if (defined($to)) { | ||
| 780 | $from= $w->columnIndex($from); | ||
| 781 | $to = $w->columnIndex($to); | ||
| 782 | return @{$w->{'_columns'}}[$from..$to]; | ||
| 783 | } else { | ||
| 784 | return $w->{'_columns'}->[$w->columnIndex($from)]; | ||
| 785 | } | ||
| 786 | } | ||
| 787 | |||
| 788 | sub columnHide { | ||
| 789 | my ($w, $first, $last) = @_; | ||
| 790 | $last = $first unless defined $last; | ||
| 791 | |||
| 792 | for (my $i=$w->columnIndex($first); $i<=$w->columnIndex($last); $i++) { | ||
| 793 | $w->columnGet($i)->packForget; | ||
| 794 | } | ||
| 795 | } | ||
| 796 | |||
| 797 | ## Converts a column index to a numeric index. $index might be a number, | ||
| 798 | ## 'end' or a reference to a MLColumn widget (see columnGet). Note that | ||
| 799 | ## the index return by this method may not match up with it's current | ||
| 800 | ## visual location due to columns being moved around | ||
| 801 | |||
| 802 | sub columnIndex { | ||
| 803 | my ($w, $index, $after_end) = @_; | ||
| 804 | |||
| 805 | if ($index eq 'end') { | ||
| 806 | if (defined $after_end) { | ||
| 807 | return $#{$w->{'_columns'}} + 1; | ||
| 808 | } else { | ||
| 809 | return $#{$w->{'_columns'}}; | ||
| 810 | } | ||
| 811 | } | ||
| 812 | |||
| 813 | if (ref($index) eq "Tk::MLColumn") { | ||
| 814 | foreach (0..$#{$w->{'_columns'}}) { | ||
| 815 | if ($index eq $w->{'_columns'}->[$_]) { | ||
| 816 | return $_; | ||
| 817 | } | ||
| 818 | } | ||
| 819 | } | ||
| 820 | |||
| 821 | if ($index =~ m/^\s*(\d+)\s*$/) { | ||
| 822 | return $1; | ||
| 823 | } | ||
| 824 | croak "Invalid column index: $index\n"; | ||
| 825 | } | ||
| 826 | |||
| 827 | ## Insert a column. $index should be a number or 'end'. | ||
| 828 | sub columnInsert { | ||
| 829 | my ($w, $index, %args) = @_; | ||
| 830 | |||
| 831 | $index = $w->columnIndex($index,1); | ||
| 832 | my %opts = (); | ||
| 833 | |||
| 834 | ## Copy these options from the megawidget. | ||
| 835 | foreach (qw/-background -foreground -font -height | ||
| 836 | -resizeable -selectbackground -selectforeground | ||
| 837 | -selectborderwidth -selectmode -separatorcolor | ||
| 838 | -separatorwidth -sortable -textwidth/) | ||
| 839 | { | ||
| 840 | $opts{$_} = $w->cget($_) if defined $w->cget($_); | ||
| 841 | } | ||
| 842 | ## All options (and more) might be overridden by %args. | ||
| 843 | map {$opts{$_} = $args{$_}} keys %args; | ||
| 844 | |||
| 845 | my $c = $w->Subwidget("pane")->MLColumn(%opts, | ||
| 846 | -yscrollcommand => [ $w => '_yscrollCallback'], | ||
| 847 | -configurecommand => [ $w => 'Callback', '-configurecommand', $w], | ||
| 848 | -xscancommand => [ $w => 'xscan' ], | ||
| 849 | -updatecommand => [ $w => '_selectionUpdate'] | ||
| 850 | ); | ||
| 851 | |||
| 852 | ## RCS: Review this later - questionable implementation | ||
| 853 | ## Fill the new column with empty values, making sure all columns have | ||
| 854 | ## the same number of rows. | ||
| 855 | unless (scalar(@{$w->{'_columns'}}) == 0) { | ||
| 856 | foreach (1..$w->size) { | ||
| 857 | $c->insert('end',''); | ||
| 858 | } | ||
| 859 | } | ||
| 860 | $c->Subwidget("heading")->bind("<Button-1>", [ $w => '_dragOrSort', $c]); | ||
| 861 | |||
| 862 | my $carr = $w->{'_columns'}; | ||
| 863 | splice(@$carr,$index,0,$c); | ||
| 864 | |||
| 865 | ## Update the selection to also include the new column. | ||
| 866 | map {$w->selectionSet($_, $_)} $w->curselection | ||
| 867 | if $w->curselection; | ||
| 868 | |||
| 869 | ## Copy all bindings that are created by calls to | ||
| 870 | ## bindRows, bindColumns and/or bindSeparators. | ||
| 871 | ## RCS: check this out, on the next pass | ||
| 872 | foreach my $subwidget (qw/listbox heading separator/) { | ||
| 873 | foreach (keys %{$w->{'_bindings'}->{$subwidget}}) { | ||
| 874 | $c->Subwidget($subwidget)->Tk::bind($_, | ||
| 875 | [ | ||
| 876 | $w => 'bindCallback', | ||
| 877 | $w->{'_bindings'}->{$subwidget}->{$_}, | ||
| 878 | $index | ||
| 879 | ] | ||
| 880 | ); | ||
| 881 | } | ||
| 882 | } | ||
| 883 | |||
| 884 | if (Tk::Exists($w->{'_columns'}->[$index+1])) { | ||
| 885 | $w->columnShow($index, -before=>$index+1); | ||
| 886 | } else { | ||
| 887 | $w->columnShow($index); | ||
| 888 | } | ||
| 889 | return $c; | ||
| 890 | } | ||
| 891 | |||
| 892 | sub columnPack { | ||
| 893 | my ($w, @packinfo) = @_; | ||
| 894 | $w->columnHide(0,'end'); | ||
| 895 | foreach (@packinfo) { | ||
| 896 | my ($index, $width) = split /:/; | ||
| 897 | $w->columnShow ($index); | ||
| 898 | if (defined($width) && $width =~ /^\d+$/) { | ||
| 899 | $w->columnGet($index)->setWidth($width) | ||
| 900 | } | ||
| 901 | } | ||
| 902 | } | ||
| 903 | |||
| 904 | sub columnPackInfo { | ||
| 905 | my ($w) = @_; | ||
| 906 | |||
| 907 | ## Widget needs to have an update call first, otherwise | ||
| 908 | ## the method will not return anything if called prior to | ||
| 909 | ## MainLoop - RCS | ||
| 910 | |||
| 911 | $w->update; | ||
| 912 | map {$w->columnIndex($_) . ':' . $_->width} | ||
| 913 | sort {$a->rootx <=> $b->rootx} | ||
| 914 | map {$_->ismapped ? $_ : ()} @{$w->{'_columns'}}; | ||
| 915 | } | ||
| 916 | |||
| 917 | sub columnShow { | ||
| 918 | my ($w, $index, %args) = @_; | ||
| 919 | |||
| 920 | my $c = $w->columnGet($index); | ||
| 921 | my @packopts = (-anchor=>'w',-side=>'left',-fill=>'both'); | ||
| 922 | if (defined($args{'-before'})) { | ||
| 923 | push (@packopts, '-before'=>$w->columnGet($args{'-before'})); | ||
| 924 | } elsif (defined($args{'-after'})) { | ||
| 925 | push (@packopts, '-after'=>$w->columnGet($args{'-after'})); | ||
| 926 | } | ||
| 927 | $c->pack(@packopts); | ||
| 928 | } | ||
| 929 | |||
| 930 | sub curselection { shift->_firstVisible->curselection(@_)} | ||
| 931 | |||
| 932 | sub delete { | ||
| 933 | my $w = shift; | ||
| 934 | foreach (@{$w->{'_columns'}}) { | ||
| 935 | my $saved_width = $_->width; | ||
| 936 | $_->delete(@_); | ||
| 937 | if ($_->ismapped) { | ||
| 938 | $_->setWidth($saved_width); | ||
| 939 | } | ||
| 940 | } | ||
| 941 | $w->_yscrollCallback; | ||
| 942 | } | ||
| 943 | |||
| 944 | sub get { | ||
| 945 | my @result = (); | ||
| 946 | my ($colnum,$rownum) = (0,0); | ||
| 947 | |||
| 948 | foreach (@{shift->{'_columns'}}) { | ||
| 949 | my @coldata = $_->get(@_); | ||
| 950 | $rownum = 0; | ||
| 951 | map {$result[$rownum++][$colnum] = $_} @coldata; | ||
| 952 | $colnum++; | ||
| 953 | } | ||
| 954 | @result; | ||
| 955 | } | ||
| 956 | |||
| 957 | sub getRow { | ||
| 958 | my @result = map {$_->get(@_)} @{shift->{'_columns'}}; | ||
| 959 | if (wantarray) { | ||
| 960 | @result; | ||
| 961 | } else { | ||
| 962 | $result[0]; | ||
| 963 | } | ||
| 964 | } | ||
| 965 | |||
| 966 | sub index { shift->_firstVisible->index(@_)} | ||
| 967 | |||
| 968 | sub insert { | ||
| 969 | my ($w, $index, @data) = @_; | ||
| 970 | my ($rownum, $colnum); | ||
| 971 | |||
| 972 | my $rowcnt = $#data; | ||
| 973 | |||
| 974 | # Insert data into one column at a time, calling $listbox->insert | ||
| 975 | # ONCE for each column. (The first version of this widget call insert | ||
| 976 | # once for each row in each column). | ||
| 977 | # | ||
| 978 | foreach $colnum (0..$#{$w->{'_columns'}}) { | ||
| 979 | my $c = $w->{'_columns'}->[$colnum]; | ||
| 980 | |||
| 981 | # The listbox might get resized after insert/delete, which is a | ||
| 982 | # behaviour we don't like.... | ||
| 983 | my $saved_width = $c->width; | ||
| 984 | |||
| 985 | my @coldata = (); | ||
| 986 | |||
| 987 | foreach (0..$#data) { | ||
| 988 | if (defined($data[$_][$colnum])) { | ||
| 989 | push @coldata, $data[$_][$colnum]; | ||
| 990 | } else { | ||
| 991 | push @coldata, ''; | ||
| 992 | } | ||
| 993 | } | ||
| 994 | $c->insert($index,@coldata); | ||
| 995 | |||
| 996 | if ($c->ismapped) { | ||
| 997 | # Restore saved width. | ||
| 998 | $c->setWidth($saved_width); | ||
| 999 | } | ||
| 1000 | } | ||
| 1001 | $w->_yscrollCallback; | ||
| 1002 | } | ||
| 1003 | |||
| 1004 | ## These methods all delegate to the first visible column's | ||
| 1005 | ## Listbox. Refer to Listbox docs and description for _firstVisible | ||
| 1006 | |||
| 1007 | sub nearest { shift->_firstVisible->nearest(@_)} | ||
| 1008 | sub see { shift->_firstVisible->see(@_)} | ||
| 1009 | sub selectionAnchor { shift->_firstVisible->selectionAnchor(@_)} | ||
| 1010 | sub selectionClear { shift->_firstVisible->selectionClear(@_)} | ||
| 1011 | sub selectionIncludes { shift->_firstVisible->selectionIncludes(@_)} | ||
| 1012 | sub selectionSet { shift->_firstVisible->selectionSet(@_)} | ||
| 1013 | sub size { shift->_firstVisible->size(@_)} | ||
| 1014 | |||
| 1015 | sub sort { | ||
| 1016 | my ($w, $descending, @indexes) = @_; | ||
| 1017 | |||
| 1018 | # Hack to avoid problem with older Tk versions which do not support | ||
| 1019 | # the -recurse=>1 option. | ||
| 1020 | $w->Busy; # This works always (but not very good...) | ||
| 1021 | Tk::catch {$w->Busy(-recurse=>1)};# This works on newer Tk versions, | ||
| 1022 | # harmless on old versions. | ||
| 1023 | |||
| 1024 | @indexes = (0..$#{$w->{'_columns'}}) unless @indexes; | ||
| 1025 | |||
| 1026 | # Convert all indexes to integers. | ||
| 1027 | map {$_=$w->columnIndex($_)} @indexes; | ||
| 1028 | |||
| 1029 | # This works on Solaris, but not on Linux??? | ||
| 1030 | # Store the -comparecommand for each row in a local array. In the sort, | ||
| 1031 | # the store command is called directly in stead of via the MLColumn | ||
| 1032 | # subwidget. This saves a lot of callbacks and function calls. | ||
| 1033 | # | ||
| 1034 | # my @cmp_subs = map {$_->cget(-comparecommand)} @{$w->{'_columns'}}; | ||
| 1035 | |||
| 1036 | # If sort order is not defined | ||
| 1037 | unless (defined $descending) { | ||
| 1038 | if ($#indexes == 0 && | ||
| 1039 | $w->{'_sortcol'} == $indexes[0] && | ||
| 1040 | $w->{'_sort_descending'} == 0) | ||
| 1041 | { | ||
| 1042 | # Already sorted on this column, reverse sort order. | ||
| 1043 | $descending = 1; | ||
| 1044 | } else { | ||
| 1045 | $descending = 0; | ||
| 1046 | } | ||
| 1047 | } | ||
| 1048 | |||
| 1049 | # To retain the selection after the sort we have to save information | ||
| 1050 | # about the current selection before the sort. Adds a dummy column | ||
| 1051 | # to the two dimensional data array, this last column will be true | ||
| 1052 | # for all rows that are currently selected. | ||
| 1053 | my $dummy_column = scalar(@{$w->{'_columns'}}); | ||
| 1054 | |||
| 1055 | my @data = $w->get(0,'end'); | ||
| 1056 | foreach ($w->curselection) { | ||
| 1057 | $data[$_]->[$dummy_column] = 1; # Selected... | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | @data = sort { | ||
| 1061 | local $^W = 0; | ||
| 1062 | foreach (@indexes) { | ||
| 1063 | my $res = do { | ||
| 1064 | if ($descending) { | ||
| 1065 | # Call via cmp_subs works fine on Solaris, but no | ||
| 1066 | # on Linux. The column->compare method is much slower... | ||
| 1067 | # | ||
| 1068 | # &{$cmp_subs[$_]} ($b->[$_],$a->[$_]); | ||
| 1069 | $w->{'_columns'}->[$_]->compare($b->[$_],$a->[$_]); | ||
| 1070 | } else { | ||
| 1071 | # &{$cmp_subs[$_]} ($a->[$_],$b->[$_]); | ||
| 1072 | $w->{'_columns'}->[$_]->compare($a->[$_],$b->[$_]); | ||
| 1073 | } | ||
| 1074 | }; | ||
| 1075 | return $res if $res; | ||
| 1076 | } | ||
| 1077 | return 0; | ||
| 1078 | } @data; | ||
| 1079 | |||
| 1080 | # Replace data with the new, sorted list. | ||
| 1081 | $w->delete(0,'end'); | ||
| 1082 | $w->insert(0,@data); | ||
| 1083 | |||
| 1084 | my @new_selection = (); | ||
| 1085 | foreach (0..$#data) { | ||
| 1086 | if ($data[$_]->[$dummy_column]) { | ||
| 1087 | $w->selectionSet($_,$_); | ||
| 1088 | } | ||
| 1089 | } | ||
| 1090 | |||
| 1091 | $w->{'_sortcol'} = $indexes[0]; | ||
| 1092 | $w->{'_sort_descending'} = $descending; | ||
| 1093 | |||
| 1094 | $w->Unbusy; #(-recurse=>1); | ||
| 1095 | } | ||
| 1096 | |||
| 1097 | # Implements horizontal scanning. | ||
| 1098 | sub xscan { | ||
| 1099 | my ($w, $type, $x) = @_; | ||
| 1100 | |||
| 1101 | if ($type eq 'dragto') { | ||
| 1102 | my $dist = $w->{'_scanmark_x'} - $w->pointerx; | ||
| 1103 | |||
| 1104 | # Looks like there is a bug in Pane: If no -xscrollcommand | ||
| 1105 | # is defined, xview() fails. This is fixed by this hack: | ||
| 1106 | # | ||
| 1107 | my $p = $w->Subwidget("pane"); | ||
| 1108 | unless (defined ($p->cget(-xscrollcommand))) { | ||
| 1109 | $p->configure(-xscrollcommand => sub {}); | ||
| 1110 | } | ||
| 1111 | $p->xview('scroll',$dist,'units'); | ||
| 1112 | } | ||
| 1113 | $w->{'_scanmark_x'} = $w->pointerx; | ||
| 1114 | } | ||
| 1115 | |||
| 1116 | sub xview { shift->Subwidget("pane")->xview(@_) } | ||
| 1117 | sub yview { shift->_firstVisible->yview(@_)} | ||
| 1118 | |||
| 1119 | 1; | ||
| 1120 | __END__ |