| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Tk::HMListbox - Sortable Multicolumn HListbox (allowing icons, along with text) with arrows in headers indicating sort order. | ||
| 4 | |||
| 5 | =head1 AUTHOR | ||
| 6 | |||
| 7 | Jim Turner | ||
| 8 | |||
| 9 | (c) 2015-2022, Jim Turner under the same license that Perl 5 itself is. All rights reserved. | ||
| 10 | |||
| 11 | Tk::SMListbox author: me | ||
| 12 | |||
| 13 | Tk::HListbox author: me | ||
| 14 | |||
| 15 | Tk::MListbox authors: Hans Jorgen Helgesen, hans_helgesen@hotmail.com (from March 2000: hans.helgesen@novit.no) | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | use Tk::HMListbox; | ||
| 20 | |||
| 21 | $hml = $parent->HMListbox (<options>); | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Tk::HMListbox is a derivitive of my L<Tk::SMListbox> (and thus L<Tk::MListbox>) | ||
| 26 | that uses L<Tk::HListbox> (instead of L<Tk::Listbox>, as used by the latter | ||
| 27 | two) which was done to allow for image icons to be included in columns | ||
| 28 | along with the traditional text strings. I created both of these | ||
| 29 | Tk::H*Listbox widgets in order to include a column of tiny file-type icons, | ||
| 30 | in addition to the other file attribute columns in my JFM5 Perl/Tk-based | ||
| 31 | file-manager application. Tk::HMListbox is designed to work as a drop-in | ||
| 32 | replacement for either Tk::SMListbox or Tk::MListbox, maintaining | ||
| 33 | backward-compatability with both. | ||
| 34 | |||
| 35 | Sorting is done by clicking on one of the column headings in the | ||
| 36 | widget. The first click will sort the data with the selected column as | ||
| 37 | key, a new click will reverse the sort order. | ||
| 38 | |||
| 39 | =head1 EXAMPLES | ||
| 40 | |||
| 41 | my $table = $w->Scrolled('HMListbox' | ||
| 42 | |||
| 43 | -scrollbars => 'se', | ||
| 44 | |||
| 45 | -height => 12, | ||
| 46 | |||
| 47 | -relief => 'sunken', | ||
| 48 | |||
| 49 | -sortable => 1, #USER CAN SORT BY COLUMN BY CLICKING ON THE COLUMN HEADER BUTTON. | ||
| 50 | |||
| 51 | -selectmode => 'extended', | ||
| 52 | |||
| 53 | -showallsortcolumns => 1, #SHOW SORT DIRECTION ARROW ON ALL SORTED COLUMNS. | ||
| 54 | |||
| 55 | -takefocus => 1, | ||
| 56 | |||
| 57 | -borderwidth => 1, | ||
| 58 | |||
| 59 | -fillcolumn => 1, #IF WINDOW RESIZED, EXPAND COLUMN 1 ("Name") TO FILL THE EXTRA SPACE. | ||
| 60 | |||
| 61 | -columns => [ #COLUMN WIDTH, SORT FUNCTIONS, HEADER BUTTON INFORMATION, ETC.: | ||
| 62 | |||
| 63 | [-itemtype => 'imagetext', -text => '~D', -width => 4, | ||
| 64 | -comparecmd => sub { $_[0]->{'-sort'} cmp $_[1]->{'-sort'} } ], | ||
| 65 | |||
| 66 | [-text => '~Name', -width => 25 ], | ||
| 67 | |||
| 68 | [-text => '~Perm.', -width => 10 ], | ||
| 69 | |||
| 70 | [-text => '~Owner:Group', -width => 14 ], | ||
| 71 | |||
| 72 | [-text => '~Size', -width => 8 ], | ||
| 73 | |||
| 74 | [-text => 'Date/~Time', -width => 15, -reversearrow => 1, -comparecommand => sub { $_[1] cmp $_[0] }], | ||
| 75 | |||
| 76 | ] | ||
| 77 | |||
| 78 | )->pack( | ||
| 79 | |||
| 80 | -expand => 'yes', | ||
| 81 | |||
| 82 | ); | ||
| 83 | |||
| 84 | #See also "make test" (test.pl). | ||
| 85 | |||
| 86 | =head1 STANDARD OPTIONS | ||
| 87 | |||
| 88 | B<-background> B<-borderwidth> B<-cursor> B<-disabledforeground> | ||
| 89 | B<-exportselection> B<-font> B<-foreground> B<-height> | ||
| 90 | B<-highlightbackground> B<-highlightcolor> B<-highlightthickness> | ||
| 91 | B<-relief> B<-selectbackground> B<-selectborderwidth> | ||
| 92 | B<-selectforeground> B<-setgrid> B<-state> B<-takefocus> B<-width> | ||
| 93 | B<-xscrollcommand> B<-yscrollcommand> | ||
| 94 | |||
| 95 | =head1 WIDGET SPECIFIC OPTIONS | ||
| 96 | |||
| 97 | =over 4 | ||
| 98 | |||
| 99 | =item B<-columns> => I<list> | ||
| 100 | |||
| 101 | Defines the columns in the widget. Each element in the list | ||
| 102 | describes a column. See the B<COLUMNS> section below. | ||
| 103 | |||
| 104 | =item B<-configurecommand> => I<callback> | ||
| 105 | |||
| 106 | The -configurecommand callback will be called whenever the layout of the | ||
| 107 | widget has changed due to user interaction. That is, the user changes the | ||
| 108 | width of a column by dragging the separator, or moves a column by dragging | ||
| 109 | the column header. | ||
| 110 | |||
| 111 | This option is useful if the application wants to store the widget layout | ||
| 112 | for later retrieval. The widget layout can be obtained by the callback | ||
| 113 | by calling the method columnPackInfo(). | ||
| 114 | |||
| 115 | =item B<-fillcolumn> => I<index> | ||
| 116 | |||
| 117 | Specify the index number of a column that will expand to fill any new | ||
| 118 | space opened up by dragging the window to a larger size. All other | ||
| 119 | columns maintain their current widths unless changed by the user | ||
| 120 | dragging a column separator. Otherwise all columns maintain their | ||
| 121 | sizes and the extra space is unused (if no column specified here). | ||
| 122 | Default: -1 (no column expands horizontally). All columns always | ||
| 123 | expand vertically though. The same column keeps it's expansion | ||
| 124 | privilege even if user rearranges their positions. | ||
| 125 | |||
| 126 | =item B<-focuscolumn> => I<index> | ||
| 127 | |||
| 128 | DEPRECIATED: Sets the index of the column whose listbox | ||
| 129 | is to receive the keyboard focus when the HMListbox widget receives | ||
| 130 | keyboard focus. This is a holdover from Tk::SMListbox, where it | ||
| 131 | is useful to better display the active element cursor due to the | ||
| 132 | fact that it depends on Tk::Listbox, which does not display the | ||
| 133 | active element (usually underlined) unless the listbox itself has the | ||
| 134 | focus. This allows the programmer to specify a colum other than the | ||
| 135 | first one, ie, if the main column of interest to the user is not the | ||
| 136 | first one. | ||
| 137 | |||
| 138 | Tk::HMListbox depends instead on Tk::HListbox, which does | ||
| 139 | display the active element without specifically being focused | ||
| 140 | making it unnecessary to designate a specific widget to show the | ||
| 141 | active cursor, so therefore all column listboxes show the active | ||
| 142 | element regardless of which has the focus and Tk::HListbox can | ||
| 143 | assign the first column (the default if this isn't specified here) | ||
| 144 | the focus and everything displays properly. | ||
| 145 | |||
| 146 | NOTE: none of this applies if B<-nocolumnfocus> is set, which | ||
| 147 | also has the side effect of disabling most keyboard bindings on | ||
| 148 | the HMListbox. Default: I<0> (First column listbox gets focus). | ||
| 149 | |||
| 150 | =item B<-headerbackground> => I<color> | ||
| 151 | |||
| 152 | Specify a different background color for the top ("header") row. | ||
| 153 | Default: B<-background> or parent window's background. | ||
| 154 | |||
| 155 | =item B<-headerforeground> => I<color> | ||
| 156 | |||
| 157 | Specify a different foreground color for the top ("header") row. | ||
| 158 | Default: B<-foreground> or parent window's foreground. | ||
| 159 | |||
| 160 | =item B<-ipadx> => I<number> | ||
| 161 | |||
| 162 | Specify horizontal padding style in pixels around the image | ||
| 163 | for the rows in the listbox which are type I<image> or I<imagetext>. | ||
| 164 | Default: B<0> | ||
| 165 | |||
| 166 | =item B<-ipady> => I<number> | ||
| 167 | |||
| 168 | Specify vertical padding style in pixels around the image | ||
| 169 | for the rows in the listbox which are type I<image> or I<imagetext>. | ||
| 170 | This can be useful in correcting vertical misallignment between the | ||
| 171 | columns when some columns contain images and others are text only! | ||
| 172 | NOTE: This changes the height of the affected rows. | ||
| 173 | Default: B<1> (and setting to B<0> is seems to be the same as B<2>). | ||
| 174 | |||
| 175 | =item B<-moveable> => I<boolean> | ||
| 176 | |||
| 177 | A value of B<1> indicates that it is okay for the user to move | ||
| 178 | the columns by dragging the column headers. B<0> disables this | ||
| 179 | function. Default: B<1>. | ||
| 180 | |||
| 181 | =item B<-nocolumnfocus> => I<boolean> | ||
| 182 | |||
| 183 | Prevents the HMListbox widget from giving the keyboard focus to | ||
| 184 | a specific column. In HMListbox, it doesn't matter which column | ||
| 185 | receives the focus (the default without setting B<-focuscolumn> | ||
| 186 | is that the first column normally gets the focus when the | ||
| 187 | HMListbox widgets receives keyboard focus). The net effect of | ||
| 188 | this option being set is that the widget itself takes focus, but | ||
| 189 | many keyboard bindings will not work, namely selecting rows with | ||
| 190 | the spacebar. Default: I<0> (Take focus normally and give the | ||
| 191 | focus to one of the column listboxes under the hood so that | ||
| 192 | row-based keyboard bindings will all work properly). To have | ||
| 193 | the HMListbox widget itself be skipped in the focus order, set | ||
| 194 | B<-takefocus> to I<0>. | ||
| 195 | |||
| 196 | =item B<-resizeable> => I<boolean> | ||
| 197 | |||
| 198 | A value of B<1> indicates that it is okay for the user to resize | ||
| 199 | the columns by dragging the column separators. B<0> disables | ||
| 200 | this function. Default: B<1>. | ||
| 201 | |||
| 202 | Note that you can also specify -resizeable on a column | ||
| 203 | by column basis. See the B<COLUMNS> section below. | ||
| 204 | |||
| 205 | =item B<-selectmode> => I<string> | ||
| 206 | |||
| 207 | Should be "single", "browse", "multiple", or "extended". | ||
| 208 | Default: B<"browse">. See L<Tk::HListbox>. | ||
| 209 | |||
| 210 | =item B<-separatorcolor> => I<string> | ||
| 211 | |||
| 212 | Specifies the color of the separator lines | ||
| 213 | (the vertical lines that separates the columns). | ||
| 214 | Default: B<black>. | ||
| 215 | |||
| 216 | Note that you can also specify -separatorcolor on a column | ||
| 217 | by column basis. See the B<COLUMNS> section below. | ||
| 218 | |||
| 219 | =item B<-separatorwidth> => I<integer> | ||
| 220 | |||
| 221 | Specifies the width in pixels of the separator lines | ||
| 222 | (the vertical lines that separates the columns). | ||
| 223 | Default: B<1>. | ||
| 224 | |||
| 225 | Note that you can also specify -separatorwidth on a column | ||
| 226 | by column basis. See the B<COLUMNS> section below. | ||
| 227 | |||
| 228 | =item B<-showcursoralways> I<boolean> | ||
| 229 | |||
| 230 | Starting with version 3.20 (and Tk::HListbox version 2.4), Tk::HMListbox | ||
| 231 | no longer displays the keyboard cursor (active row) when the HMListbox | ||
| 232 | widget does not have the keyboard focus, in order to be consistent with the | ||
| 233 | behaviour of Tk::SMListbox and Tk::MListbox. This option, when set to 1 | ||
| 234 | (or a "true" value) restores the pre-v3.20 behaviour of always showing the | ||
| 235 | keyboard cursor. Default I<0> (False). | ||
| 236 | |||
| 237 | =item B<-sortable> => I<boolean> | ||
| 238 | |||
| 239 | A value of B<1> indicates that it is okay for the user to sort | ||
| 240 | the data by clicking column headings. B<0> disables this function. | ||
| 241 | Default: B<1>. | ||
| 242 | |||
| 243 | Note that you can also specify B<-sortable> on a column | ||
| 244 | by column basis. See I<COLUMNS> below. | ||
| 245 | |||
| 246 | =item B<-state> | ||
| 247 | |||
| 248 | Specifies one of two states for the listbox: B<normal> or B<disabled>. | ||
| 249 | If the listbox is disabled then user can interact with the widget in a very | ||
| 250 | limited (read-only) way. Items may not be selected, but currently, the | ||
| 251 | list can be resorted or the columns potentially expanded for easier viewing. | ||
| 252 | Items are drawn in the B<-disabledforeground> color, and selection | ||
| 253 | cannot be modified and is not shown (though selection information is | ||
| 254 | retained). | ||
| 255 | |||
| 256 | =item B<-takefocus> => I<number> | ||
| 257 | |||
| 258 | There are actually three different focusing options: Specify B<1> to both | ||
| 259 | allow the widget to take keyboard focus itself and to enable grabbing the | ||
| 260 | keyboard focus when a user clicks on a row in the listbox. Specify B<''> | ||
| 261 | to allow the widget to take focus (via the <TAB> circulate order) but do | ||
| 262 | not grab the focus when a user clicks on (selects) a row. This is the | ||
| 263 | default focusing model. Specify B<0> to not allow the widget to receive | ||
| 264 | the keyboard focus. | ||
| 265 | |||
| 266 | =item B<-tpadx> => I<number> | ||
| 267 | |||
| 268 | Specify horizontal padding style in pixels around the text | ||
| 269 | for the rows in the listbox which are type I<text>. | ||
| 270 | Default: B<0> | ||
| 271 | |||
| 272 | =item B<-tpady> => I<number> | ||
| 273 | |||
| 274 | Specify vertical padding style in pixels around the text | ||
| 275 | for the rows in the listbox which are type I<text>. | ||
| 276 | This can be useful in correcting vertical misallignment between the | ||
| 277 | columns when some columns contain images and others are text only! | ||
| 278 | NOTE: This changes the height of the affected rows. | ||
| 279 | Default (seems to be): B<2> (and setting to B<0> is the same as B<2>). | ||
| 280 | |||
| 281 | =back | ||
| 282 | |||
| 283 | =head1 WIDGET SPECIFIC, COLUMN-SPECIFIC OPTIONS | ||
| 284 | |||
| 285 | =over 4 | ||
| 286 | |||
| 287 | =item B<-comparecmd> => I<callback> | ||
| 288 | |||
| 289 | Specifies a callback to use when sorting the HMListbox with this | ||
| 290 | column as key. The callback will be called with two scalar arguments, | ||
| 291 | each a value from this particular column. The callback should | ||
| 292 | return an integer less than, equal to, or greater than 0, depending | ||
| 293 | on how the tow arguments are ordered. If for example the column | ||
| 294 | should be sorted by numerical value: | ||
| 295 | |||
| 296 | -comparecommand => sub { $_[0] <=> $_[1]} | ||
| 297 | |||
| 298 | Default: B<sub { $_[0] cmp $_[1] }> #(Ascending by string) | ||
| 299 | NOTE: If the column data is images or hashrefs (both | ||
| 300 | text and images or contains additional cell-specific | ||
| 301 | options, then one should specify a component of that | ||
| 302 | hash to sort by, ie. the special "-sort" key is handy, then | ||
| 303 | one can sort using something like: | ||
| 304 | |||
| 305 | sub { $_[0]->{'-sort'} cmp $_[1]->{'-sort'} } or maybe: | ||
| 306 | |||
| 307 | sub { $_[0]->{'-text'} cmp $_[1]->{'-text'} } | ||
| 308 | |||
| 309 | =item B<-itemtype> => "text" | "imagetext" | ||
| 310 | |||
| 311 | New option (not included with Tk::SMListbox or Tk::MListbox) that | ||
| 312 | permits a column to contain either images and or text in it's | ||
| 313 | entries. Valid values are "text" and "imagetext". | ||
| 314 | Default: B<"text">. | ||
| 315 | |||
| 316 | =item B<-reversearrow> => 0 | 1 | ||
| 317 | |||
| 318 | New option (not included with the old Tk::MListbox) that | ||
| 319 | causes the up / down direction of the sort arrow in the | ||
| 320 | column header that shows the direction the sorted-by | ||
| 321 | column is currently sorted. Default: B<0> (zero): (Up-arrow) | ||
| 322 | if sorted in ascending order, Down-arrow if in | ||
| 323 | descending order. Setting to 1 reverses this. This is | ||
| 324 | often necessary to show desired results, ie. if the sort | ||
| 325 | function is reversed, such as: sub { $_[1] cmp $_[0] } | ||
| 326 | |||
| 327 | =back | ||
| 328 | |||
| 329 | =head1 HMListbox COLUMN CONFIGURATION | ||
| 330 | |||
| 331 | NOTE: See also the additional options described in the previous | ||
| 332 | section "WIDGET SPECIFIC COLUMN-SPECIFIC OPTIONS". | ||
| 333 | |||
| 334 | The HMListbox widget is a collection of I<HMLColumn> widgets. | ||
| 335 | Each HMLColumn contains a HListbox, a heading and the separator bar. | ||
| 336 | The columns are created and maintained through the -columns | ||
| 337 | option or the column methods of the widget. The columns are indexed | ||
| 338 | from 0 and up. Initially, column 0 is the leftmost column of the | ||
| 339 | widget. The column indices B<are not changed> when the columns | ||
| 340 | are moved or hidden. The only ways to change the column indices | ||
| 341 | are to call columnInsert(), columnDelete() or configure(-column). | ||
| 342 | |||
| 343 | Each column has its own set of options which might be passed to | ||
| 344 | HMListbox::configure(-columns), HMListbox::insert(), | ||
| 345 | HMListbox::columnConfigure() or HMLColumn::configure(). | ||
| 346 | |||
| 347 | The following code snippets are all equal: | ||
| 348 | |||
| 349 | 1. my $hml=$mw->HMListbox(-columns=>[[-text=>'Heading1', | ||
| 350 | -sortable=>0], | ||
| 351 | [-text=>'Heading2']]); | ||
| 352 | |||
| 353 | 2. my $hml=$mw->HMListbox; | ||
| 354 | $hml->columnInsert(0,-text=>'Heading1', -sortable=>0); | ||
| 355 | $hml->columnInsert(0,-text=>'Heading2'); | ||
| 356 | |||
| 357 | 3. my $hml=$mw->HMListbox; | ||
| 358 | $c=$hml->columnInsert(0,-text=>'Heading1'); | ||
| 359 | $hml->columnInsert(0,-text=>'Heading2'); | ||
| 360 | $c->configure(-sortable=>0); | ||
| 361 | |||
| 362 | 4. my $hml=$mw->HMListbox; | ||
| 363 | $hml->columnInsert(0,-text=>'Heading1'); | ||
| 364 | $hml->columnInsert(0,-text=>'Heading2'); | ||
| 365 | $hml->columnConfigure(0,-sortable=>0); | ||
| 366 | |||
| 367 | (See the columnConfigure() method below for details on column options). | ||
| 368 | |||
| 369 | All column methods expects one or two column indices as arguments. | ||
| 370 | The column indices might be an integer (between 0 and the number | ||
| 371 | of columns minus one), 'end' for the last column, or a reference | ||
| 372 | to the HMLColumn widget (obtained by calling HMListbox->columnGet() | ||
| 373 | or by storing the return value from HMListbox->columnInsert()). | ||
| 374 | |||
| 375 | =head1 WIDGET METHODS | ||
| 376 | |||
| 377 | =over 4 | ||
| 378 | |||
| 379 | =item $hml->B<compound>(I<[left|right|top|bottom]>) | ||
| 380 | |||
| 381 | Gets / sets the side of the column header that the | ||
| 382 | ascending / descending arrow is to appear (left, right, top, | ||
| 383 | bottom). Default: I<"right">. | ||
| 384 | |||
| 385 | =item $hml->B<focusColumn>(I<[index]>) | ||
| 386 | |||
| 387 | DEPRECIATED: Gets or sets the index of the column whose listbox | ||
| 388 | is to receive the keyboard focus when the HMListbox widget receives | ||
| 389 | keyboard focus. This is a holdover from Tk::SMListbox, where it | ||
| 390 | is useful to better display the active element cursor due to the | ||
| 391 | fact that it depends on Tk::Listbox, which does not display the | ||
| 392 | active element (usually underlined) unless the listbox itself has the | ||
| 393 | focus. This allows the programmer to specify a colum other than the | ||
| 394 | first one, ie, if the main column of interest to the user is not the | ||
| 395 | first one. | ||
| 396 | |||
| 397 | Tk::HMListbox depends instead on Tk::HListbox, which does | ||
| 398 | display the active element without specifically being focused | ||
| 399 | making it unnecessary to designate a specific widget to show the | ||
| 400 | active cursor, so therefore all column listboxes show the active | ||
| 401 | element regardless of which has the focus and Tk::HListbox can | ||
| 402 | assign the first column (the default if this isn't specified here) | ||
| 403 | the focus and everything displays properly. | ||
| 404 | |||
| 405 | NOTE: none of this applies if B<-nocolumnfocus> is set, which | ||
| 406 | also has the side effect of disabling most keyboard bindings on | ||
| 407 | the HMListbox. | ||
| 408 | |||
| 409 | =item $hml->B<getSortOrder>() | ||
| 410 | |||
| 411 | Returns an array that is in the same format accepted by the | ||
| 412 | $smListBox->sort method. The 1st element is | ||
| 413 | either true for descending sort, false for assending. | ||
| 414 | Subsequent elements represent the column indices of one or | ||
| 415 | more columns by which the data is sorted. | ||
| 416 | Default (if nothing previously set): B<(0, 0)>. | ||
| 417 | |||
| 418 | =item $hml->B<setSortOrder>(I<descending>, I<columnindex>...) | ||
| 419 | |||
| 420 | Sets the default sort order and columns to sort without actually | ||
| 421 | sorting. The arguments are the same as the sort() function. | ||
| 422 | |||
| 423 | =item $hml->B<setButtonHeight>([I<pady>]) | ||
| 424 | |||
| 425 | Sets (alters) the "-pady" value for the header buttons. | ||
| 426 | Should be called AFTER all columns have been created. | ||
| 427 | Requires version 2 or higher. | ||
| 428 | |||
| 429 | =item $hml->B<showallsortcolumns>([I<1|0>]) | ||
| 430 | |||
| 431 | Gets or sets whether a sort direction arrow is to be displayed | ||
| 432 | on each column involved in the sorting or just the 1st | ||
| 433 | (primary sort). Default: B<0> (false) - show arrow only on the | ||
| 434 | primary sort column. | ||
| 435 | |||
| 436 | =item $hml->B<bindColumns>(I<sequence>,I<callback>) | ||
| 437 | |||
| 438 | Adds the binding to all column headers in the widget. See the section | ||
| 439 | BINDING EVENTS TO HMListbox below. | ||
| 440 | |||
| 441 | =item $hml->B<bindRows>(I<sequence>,I<callback>) | ||
| 442 | |||
| 443 | Adds the binding to all listboxes in the widget. See the section | ||
| 444 | BINDING EVENTS TO HMListbox below. | ||
| 445 | |||
| 446 | =item $hml->B<bindSeparators>(I<sequence>,I<callback>) | ||
| 447 | |||
| 448 | Adds the binding to all separators in the widget. See the section | ||
| 449 | BINDING EVENTS TO HMListbox below. | ||
| 450 | |||
| 451 | =back | ||
| 452 | |||
| 453 | =head2 COLUMN METHODS | ||
| 454 | |||
| 455 | (Methods for accessing and manipulating individual columns | ||
| 456 | in the HMListbox widget) | ||
| 457 | |||
| 458 | =over 4 | ||
| 459 | |||
| 460 | =item $hml->B<columnConfigure>(I<index>,I<option>=>I<value>...) | ||
| 461 | |||
| 462 | Set option values for a specific column. | ||
| 463 | Equal to $hml->columnGet(I<index>)->configure(...). | ||
| 464 | |||
| 465 | The following column options are supported: | ||
| 466 | |||
| 467 | =over 4 | ||
| 468 | |||
| 469 | =item B<-comparecommand> => I<callback> | ||
| 470 | |||
| 471 | Specifies a callback to use when sorting the HMListbox with this | ||
| 472 | column as key. The callback will be called with two scalar arguments, | ||
| 473 | each a value from this particular column. The callback should | ||
| 474 | return an integer less than, equal to, or greater than 0, depending | ||
| 475 | on how the tow arguments are ordered. If for example the column | ||
| 476 | should be sorted by numerical value: | ||
| 477 | |||
| 478 | -comparecommand => sub { $_[0] <=> $_[1]} | ||
| 479 | |||
| 480 | Default: B<sub { $_[0] cmp $_[1] }> #(Ascending by string) | ||
| 481 | NOTE: If the column data is images or hashrefs (both | ||
| 482 | text and images or contains additional cell-specific | ||
| 483 | options, then one should specify a component of that | ||
| 484 | hash to sort by, ie. the special "-sort" key is handy, then | ||
| 485 | one can sort using something like: | ||
| 486 | |||
| 487 | sub { $_[0]->{'-sort'} cmp $_[1]->{'-sort'} } or maybe: | ||
| 488 | |||
| 489 | sub { $_[0]->{'-text'} cmp $_[1]->{'-text'} } | ||
| 490 | |||
| 491 | =item B<-text> => I<string> | ||
| 492 | |||
| 493 | Specifies the text to be used in the heading button of the column. | ||
| 494 | |||
| 495 | =item B<-resizeable> => I<boolean> | ||
| 496 | |||
| 497 | A value of B<1> indicates that it is okay for the user to resize | ||
| 498 | this column by dragging the separator. B<0> disables this function. | ||
| 499 | Default: B<1>. | ||
| 500 | |||
| 501 | =item B<-separatorcolor> => I<string> | ||
| 502 | |||
| 503 | Specifies the color of the separator line. Default: B<black>. | ||
| 504 | |||
| 505 | =item B<-separatorwidth> => I<integer> | ||
| 506 | |||
| 507 | Specifies the width of the separator line in pixels. Default: B<1>. | ||
| 508 | |||
| 509 | =item B<-sortable> => I<boolean> | ||
| 510 | |||
| 511 | A value of B<1> indicates that it is okay for the user to sort | ||
| 512 | the data by clicking this column's heading. B<0> disables this | ||
| 513 | function. Default: B<1>. | ||
| 514 | |||
| 515 | =item B<-itemtype> => "text" | "imagetext" | ||
| 516 | |||
| 517 | New option (not included with Tk::SMListbox or Tk::MListbox) that | ||
| 518 | permits a column to contain either images and or text in it's | ||
| 519 | entries. Valid values are "text" and "imagetext". | ||
| 520 | Default: B<"text">. | ||
| 521 | |||
| 522 | =item B<-reversearrow> => 0 | 1 | ||
| 523 | |||
| 524 | New option (not included with the old Tk::MListbox) that | ||
| 525 | causes the up / down direction of the sort arrow in the | ||
| 526 | column header that shows the direction the sorted-by | ||
| 527 | column is currently sorted. Default: B<0> (zero) - (Up-arrow | ||
| 528 | if sorted in ascending order, Down-arrow if in | ||
| 529 | descending order. Setting to 1 reverses this. This is | ||
| 530 | often necessary to show desired results, ie. if the sort | ||
| 531 | function is reversed, such as: sub { $_[1] cmp $_[0] } | ||
| 532 | |||
| 533 | =back | ||
| 534 | |||
| 535 | =item $hml->B<columnDelete>(I<first>,I<last>) | ||
| 536 | |||
| 537 | If I<last> is omitted, deletes column I<first>. If I<last> is | ||
| 538 | specified, deletes all columns from I<first> to I<last>, inclusive. | ||
| 539 | |||
| 540 | All previous column indices greater than I<last> (or I<first> if | ||
| 541 | I<last> is omitted) are decremented by the number of columns | ||
| 542 | deleted. | ||
| 543 | |||
| 544 | =item $hml->B<columnGet>(I<first>,I<last>) | ||
| 545 | |||
| 546 | If I<last> is not specified, returns the HMLColumn widget specified by I<first>. | ||
| 547 | If both I<first> and I<last> are specified, returns an array containing all | ||
| 548 | columns from I<first> to I<last>. | ||
| 549 | |||
| 550 | =item $hml->B<columnHide>(I<first>,I<last>) | ||
| 551 | |||
| 552 | If I<last> is omitted, hides column I<first>. If I<last> is | ||
| 553 | specified, hides all columns from I<first> to I<last>, inclusive. | ||
| 554 | |||
| 555 | Hiding a column is equal to calling $hml->columnGet(I<index>)->packForget. | ||
| 556 | The column is B<not> deleted, all data are still available, | ||
| 557 | and the column indices remain the same. | ||
| 558 | |||
| 559 | See also the columnShow() method below. | ||
| 560 | |||
| 561 | =item $hml->B<columnIndex>(I<index>) | ||
| 562 | |||
| 563 | Returns an integer index for the column specifed by I<index>. | ||
| 564 | |||
| 565 | =item $hml->B<columnInsert>(I<index>,I<option>=>I<value>...) | ||
| 566 | |||
| 567 | Creates a new column in the HMListbox widget. The column will | ||
| 568 | get the index specified by I<index>. If I<index> is 'end', the | ||
| 569 | new column's index will be one more than the previous highest | ||
| 570 | column index. | ||
| 571 | |||
| 572 | If column I<index> exists, the new column will be placed | ||
| 573 | to the B<left> of this column. All previous column indices | ||
| 574 | equal to or greater than I<index> will be incremented by one. | ||
| 575 | |||
| 576 | Returns the newly created HMLColumn widget. | ||
| 577 | |||
| 578 | (See the columnConfigure() method above for details on column options). | ||
| 579 | |||
| 580 | =item $hml->B<columnPack>(I<array>) | ||
| 581 | |||
| 582 | Repacks all columns in the HMListbox widget according to the | ||
| 583 | specification in I<array>. Each element in I<array> is a string | ||
| 584 | on the format B<index:width>. I<index> is a column index, I<width> | ||
| 585 | defines the columns width in pixels (may be omitted). The columns | ||
| 586 | are packed left to right in the order specified by by I<array>. | ||
| 587 | Columns not specified in I<array> will be hidden. | ||
| 588 | |||
| 589 | This method is most useful if used together with the | ||
| 590 | columnPackInfo() method. | ||
| 591 | |||
| 592 | =item $hml->B<columnPackInfo>() | ||
| 593 | |||
| 594 | Returns an array describing the current layout of the HMListbox | ||
| 595 | widget. Each element of the array is a string on the format | ||
| 596 | B<index:width> (see columnPack() above). Only indices of columns that | ||
| 597 | are currently shown (not hidden) will be returned. The first element | ||
| 598 | in the returned array represents the leftmost column. | ||
| 599 | |||
| 600 | This method may be used in conjunction with columnPack() to save | ||
| 601 | and restore the column configuration. | ||
| 602 | |||
| 603 | =item $hml->B<columnShow>(I<index>,I<option>=>I<value>) | ||
| 604 | |||
| 605 | Shows a hidden column (see the columnHide() method above). | ||
| 606 | The column to show is specified by I<index>. | ||
| 607 | |||
| 608 | By default, the column is pack'ed at the rigthmost end of the | ||
| 609 | HMListbox widget. This might be overridden by specifying one of | ||
| 610 | the following options: | ||
| 611 | |||
| 612 | =item B<-after> => I<index> | ||
| 613 | |||
| 614 | Place the column B<after> (to the right of) the column specified | ||
| 615 | by I<index>. | ||
| 616 | |||
| 617 | =item B<-before> => I<index> | ||
| 618 | |||
| 619 | Place the column B<before> (to the left of) the column specified | ||
| 620 | by I<index>. | ||
| 621 | |||
| 622 | =back | ||
| 623 | |||
| 624 | =head2 ROW METHODS | ||
| 625 | |||
| 626 | (Methods for accessing and manipulating rows of data) | ||
| 627 | |||
| 628 | Many of the methods for HMListbox take one or more indices as | ||
| 629 | arguments. See L<Tk::HListbox> for a description of row indices. | ||
| 630 | |||
| 631 | =over 4 | ||
| 632 | |||
| 633 | =item $hml->B<delete>(I<first>,I<last>) | ||
| 634 | |||
| 635 | Deletes one or more row elements of the HMListbox. I<First> and I<last> | ||
| 636 | are indices specifying the first and last elements in the range to | ||
| 637 | delete. If I<last> isn't specified it defaults to B<first>, | ||
| 638 | i.e. a single element is deleted. | ||
| 639 | |||
| 640 | =item $hml->B<get>(I<first>,I<last>) | ||
| 641 | |||
| 642 | If I<last> is omitted, returns the content of the HMListbox row | ||
| 643 | indicated by I<first>. If I<last> is specified, the command returns | ||
| 644 | a list whose elements are all of the listbox rows between | ||
| 645 | I<first> and I<last>. | ||
| 646 | |||
| 647 | The returned elements are all array references. The referenced | ||
| 648 | arrays contains one element for each column of the HMListbox. | ||
| 649 | |||
| 650 | =item $hml->B<getRow>(I<index>) | ||
| 651 | |||
| 652 | In scalar context, returns the value of column 0 in the HMListbox | ||
| 653 | row specified by I<index>. In list context, returns the content | ||
| 654 | of all columns in the row as an array. | ||
| 655 | |||
| 656 | This method is provided for convenience, since retrieving a single | ||
| 657 | row with the get() method might produce some ugly code. | ||
| 658 | |||
| 659 | The following two code snippets are equal: | ||
| 660 | |||
| 661 | 1. @row=$hml->getRow(0); | ||
| 662 | |||
| 663 | 2. @row=@{($hml->get(0))[0]}; | ||
| 664 | |||
| 665 | |||
| 666 | =item $hml->B<sort>(I<descending>, I<columnindex>...) | ||
| 667 | |||
| 668 | Sorts the content of the HMListbox. If I<descending> is a B<true> | ||
| 669 | value, the sort order will be descending. Default is B<0> - ascending | ||
| 670 | sort on B<first> column (0). | ||
| 671 | |||
| 672 | If I<columnindex> is specified, the sort will be done with the | ||
| 673 | specified column as key. You can specify as many I<columnindex> | ||
| 674 | arguments as you wish. Sorting is done on the first column, then | ||
| 675 | on the second, etc... | ||
| 676 | |||
| 677 | The default is to sort the data on all columns of the listbox, | ||
| 678 | with column 0 as the first sort key, column 1 as the second, etc. | ||
| 679 | |||
| 680 | =back | ||
| 681 | |||
| 682 | =head1 OTHER LISTBOX METHODS | ||
| 683 | |||
| 684 | Most other Tk::HListbox methods works for the HMListbox widget. This | ||
| 685 | includes the methods activate, cget, curselection, index, nearest, see, | ||
| 686 | selectionXXX, size, xview, yview. | ||
| 687 | |||
| 688 | See L<Tk::HListbox> | ||
| 689 | |||
| 690 | =head1 BINDING EVENTS TO HMLISTBOX | ||
| 691 | |||
| 692 | Calling $hml->bind(...) probably makes little sense, since the call does not | ||
| 693 | specify whether the binding should apply to the listbox, the header button | ||
| 694 | or the separator line between each column. | ||
| 695 | |||
| 696 | In stead of the ordinary bind, the following methods should be used: | ||
| 697 | |||
| 698 | =over 4 | ||
| 699 | |||
| 700 | =item $hml->B<bind>(I<sequence>,I<callback>) | ||
| 701 | |||
| 702 | Synonym for $hml->B<bindRows>(I<sequence>,I<callback>). | ||
| 703 | |||
| 704 | =item $hml->B<bindRows>(I<sequence>,I<callback>) | ||
| 705 | |||
| 706 | Synonym for $hml->B<bindSubwidgets>('listbox',I<sequence>,I<callback>) | ||
| 707 | |||
| 708 | =item $hml->B<bindColumns>(I<sequence>,I<callback>) | ||
| 709 | |||
| 710 | Synonym for $hml->B<bindSubwidgets>('heading',I<sequence>,I<callback>) | ||
| 711 | |||
| 712 | =item $hml->B<bindSeparators>(I<sequence>,I<callback>) | ||
| 713 | |||
| 714 | Synonym for $hml->B<bindSubwidgets>('separator',I<sequence>,I<callback>) | ||
| 715 | |||
| 716 | =item $hml->B<bindSubwidgets>(I<subwidget>,I<sequence>,I<callback>) | ||
| 717 | |||
| 718 | Adds the binding specified by I<sequence> and I<callback> to all subwidgets | ||
| 719 | of the given type (should be 'listbox', 'heading' or 'separator'). | ||
| 720 | |||
| 721 | The binding is stored in the widget, and if you create a new column | ||
| 722 | by calling $hml->columnInsert(), all bindings created by $hml->bindSubwidgets() | ||
| 723 | are automatically copied to the new column. | ||
| 724 | |||
| 725 | The callback is called with the HMListbox widget as first argument, and | ||
| 726 | the index of the column where the event occured as the second argument. | ||
| 727 | |||
| 728 | NOTE that $hml->bindSubwidgets() does not support all of Tk's callback formats. | ||
| 729 | The following are supported: | ||
| 730 | |||
| 731 | \&subname | ||
| 732 | sub { code } | ||
| 733 | [ \&subname, arguments...] | ||
| 734 | [ sub { code }, arguments...] | ||
| 735 | |||
| 736 | If I<sequence> is undefined, then the return value is a list whose elements | ||
| 737 | are all the sequences for which there exist bindings for I<subwidget>. | ||
| 738 | |||
| 739 | If I<sequence> is specified without I<callback>, then the callback currently | ||
| 740 | bound to sequence is returned, or an empty string is returned if there is no | ||
| 741 | binding for sequence. | ||
| 742 | |||
| 743 | If I<sequence> is specified, and I<callback> is an empty string, then the | ||
| 744 | current binding for sequence is destroyed, leaving sequence unbound. | ||
| 745 | An empty string is returned. | ||
| 746 | |||
| 747 | An empty string is returned in all other cases. | ||
| 748 | |||
| 749 | =back | ||
| 750 | |||
| 751 | =head1 KEYWORDS | ||
| 752 | |||
| 753 | hmlistbox, smlistbox, mlistbox, listbox, hlist, widget | ||
| 754 | |||
| 755 | =head1 SEE ALSO | ||
| 756 | |||
| 757 | L<Tk::SMListbox> L<Tk::MListbox> L<Tk::HListbox> | ||
| 758 | |||
| 759 | =cut | ||
| 760 | |||
| 761 | ## Tk::HMListbox | ||
| 762 | ## | ||
| 763 | ## Tk::HMListbox is a Tk::HList-based derivitive of Tk::SMListbox that | ||
| 764 | ## adds the ability to include either image icons or text (or both) | ||
| 765 | ## columns along with other HList-specific style and configuration options. | ||
| 766 | ## | ||
| 767 | ## Tk::HMListbox adds 2 new options: "itemtype", and "reversearrow". | ||
| 768 | ## | ||
| 769 | ############################################################################## | ||
| 770 | ## MListbox Version 1.11 (26 Dec 2001) | ||
| 771 | ## | ||
| 772 | ## Original Author: Hans J. Helgesen, Dec 1999 | ||
| 773 | ## Maintainer: Rob Seegel (versions 1.10+) | ||
| 774 | ## | ||
| 775 | ## This version is a maintenance release of Hans' MListbox widget. | ||
| 776 | ## I have tried to avoid adding too many new features and just ensure | ||
| 777 | ## that the existing ones work properly. | ||
| 778 | ## | ||
| 779 | ## Please post feedback to comp.lang.perl.tk or email to RobSeegel@aol.com | ||
| 780 | ## | ||
| 781 | ## This module contains four classes. Of the four, HMListbox is | ||
| 782 | ## is the only one intended for standalone use, the other three: | ||
| 783 | ## HMCListbox, HMLColumn, HMButton are accessible as Subwidgets, but | ||
| 784 | ## not intended to be used in any other way other than as | ||
| 785 | ## components of HMListbox. | ||
| 786 | ## | ||
| 787 | ############################################################################## | ||
| 788 | ## HMCListbox is similar to an ordinary listbox, but with the following | ||
| 789 | ## differences: | ||
| 790 | ## - Calls an -updatecommand whenever something happens to it. | ||
| 791 | ## - Horizontal scanning is disabled, calls -xscancommand to let parent widget | ||
| 792 | ## handle this. | ||
| 793 | |||
| 794 | { | ||
| 795 | package Tk::HMListbox::HMCListbox; | ||
| 796 | use base qw(Tk::Derived Tk::HListbox); | ||
| 797 | |||
| 798 | Tk::Widget->Construct('HMCListbox'); | ||
| 799 | |||
| 800 | sub Populate { | ||
| 801 | my ($w, $args) = @_; | ||
| 802 | $w->SUPER::Populate($args); | ||
| 803 | $w->ConfigSpecs( | ||
| 804 | -updatecommand => ['CALLBACK'], | ||
| 805 | -xscancommand => ['CALLBACK'], | ||
| 806 | ); | ||
| 807 | } | ||
| 808 | |||
| 809 | sub selectionSet { #JWT:NOTE:Callback->can() EATS 1ST ARGUMENT, SO WE MUST *NOT* SHIFT @_!!! | ||
| 810 | my ($w) = @_; | ||
| 811 | $w->Callback(-updatecommand=>$w->can('Tk::HListbox::selectionSet'),@_); | ||
| 812 | } | ||
| 813 | |||
| 814 | sub selectionClear { | ||
| 815 | my ($w) = @_; | ||
| 816 | $w->Callback(-updatecommand=>$w->can('Tk::HListbox::selectionClear'),@_); | ||
| 817 | } | ||
| 818 | |||
| 819 | sub selectionAnchor { | ||
| 820 | my ($w) = @_; | ||
| 821 | $w->Callback(-updatecommand=>$w->can('Tk::HListbox::selectionAnchor'),@_); | ||
| 822 | } | ||
| 823 | |||
| 824 | sub activate { | ||
| 825 | my ($w) = @_; | ||
| 826 | $w->Callback(-updatecommand=>$w->can('Tk::HListbox::activate'),@_); | ||
| 827 | } | ||
| 828 | |||
| 829 | sub see { | ||
| 830 | my ($w) = @_; | ||
| 831 | $w->Callback(-updatecommand=>$w->can('Tk::HListbox::see'),@_); | ||
| 832 | } | ||
| 833 | |||
| 834 | sub yview { | ||
| 835 | my ($w) = @_; #JWT:NOTE:Callback->can() EATS 1ST ARGUMENT, SO WE MUST *NOT* SHIFT @_!!! | ||
| 836 | @args = @_; | ||
| 837 | shift @args; #JWT:NOTE:WE DO SHIFT THE MODULE OFF BEFORE CALLING yview, ETC. DIRECTLY! | ||
| 838 | $w->Callback(-updatecommand=>$w->can('Tk::HListbox::yview'),@_); | ||
| 839 | $w->Tk::HListbox::yview unless (@args); #JWT:REQUIRED BY _yscrollCallback! | ||
| 840 | } | ||
| 841 | |||
| 842 | sub scan { | ||
| 843 | my ($w,$type,$x,$y) = @_; | ||
| 844 | # Disable horizontal scanning. | ||
| 845 | if ($type eq 'mark') { | ||
| 846 | $w->{'_scanmark_x'} = $x; | ||
| 847 | } | ||
| 848 | $w->Callback(-updatecommand=>$w->can('Tk::HListbox::scan'), | ||
| 849 | $w, $type, $w->{'_scanmark_x'}, $y | ||
| 850 | ); | ||
| 851 | $w->Callback(-xscancommand=>$type,$x); | ||
| 852 | } | ||
| 853 | |||
| 854 | sub SpaceSelect | ||
| 855 | { | ||
| 856 | my ($w) = @_; | ||
| 857 | # $w->Callback(-updatecommand=>$w->can('Tk::HListbox::SpaceSelect'),@_); | ||
| 858 | eval { shift; $w->Tk::HListbox::SpaceSelect(@_); }; | ||
| 859 | } | ||
| 860 | |||
| 861 | sub CtrlPriorNext | ||
| 862 | { | ||
| 863 | my ($w) = @_; | ||
| 864 | $w->Callback(-updatecommand=>$w->can('Tk::HListbox::CtrlPriorNext'),@_); | ||
| 865 | } | ||
| 866 | } | ||
| 867 | |||
| 868 | ############################################################################## | ||
| 869 | ## HMButton is like an ordinary Button, but with an addition option: | ||
| 870 | ## -pixelwidth | ||
| 871 | ## The new configure method makes sure the pixelwidth is always retained. | ||
| 872 | { | ||
| 873 | package Tk::HMListbox::HMButton; | ||
| 874 | use base qw(Tk::Derived Tk::Button); | ||
| 875 | Tk::Widget->Construct('HMButton'); | ||
| 876 | |||
| 877 | sub Populate { | ||
| 878 | my ($w, $args) = @_; | ||
| 879 | $w->SUPER::Populate($args); | ||
| 880 | $w->ConfigSpecs( | ||
| 881 | -pixelwidth => ['PASSIVE'], | ||
| 882 | -bitmap => [qw/SELF bitmap bitmap/, 'noarrow'], | ||
| 883 | -compound => [qw/SELF compound compound/, 'right'], | ||
| 884 | -background => [qw/SELF background Background/, $Tk::NORMAL_BG], | ||
| 885 | -foreground => [qw/SELF foreground Foreground/, $Tk::NORMAL_FG] | ||
| 886 | ); | ||
| 887 | } | ||
| 888 | |||
| 889 | sub configure { | ||
| 890 | my $w = shift; | ||
| 891 | my (@ret) = $w->SUPER::configure(@_); | ||
| 892 | unless (@ret) { | ||
| 893 | if (defined(my $pixels = $w->cget('-pixelwidth'))) { | ||
| 894 | $w->GeometryRequest($pixels,$w->reqheight); | ||
| 895 | } | ||
| 896 | } | ||
| 897 | return @ret; | ||
| 898 | } | ||
| 899 | } | ||
| 900 | |||
| 901 | ############################################################################### | ||
| 902 | ## HMLColumn implements a single column in the HMListbox. HMLColumn is a composite | ||
| 903 | ## containing a heading (an HMButton), a listbox (HMCListbox) and a frame which | ||
| 904 | ## frame which serves as a draggable separator | ||
| 905 | { | ||
| 906 | package Tk::HMListbox::HMLColumn; | ||
| 907 | use base qw(Tk::Frame); | ||
| 908 | Tk::Widget->Construct('HMLColumn'); | ||
| 909 | |||
| 910 | sub Populate { | ||
| 911 | my ($w, $args) = @_; | ||
| 912 | my $undln = delete($args->{'-underline'}); | ||
| 913 | $w->SUPER::Populate($args); | ||
| 914 | my $hdrBG = $args->{'-headerbackground'} || $args->{'-background'} || undef; | ||
| 915 | my $hdrFG = $args->{'-headerforeground'} || $args->{'-foreground'} || undef; | ||
| 916 | my $disableFG = $args->{'-disabledforeground'} || $Tk::DISABLED_FG; | ||
| 917 | |||
| 918 | ## HMLColumn Components | ||
| 919 | ## $sep - separator - Frame | ||
| 920 | ## $hdr - heading - HMButton | ||
| 921 | ## $f - frame - Frame | ||
| 922 | ## $lb - listbox - HMCListbox | ||
| 923 | |||
| 924 | my $sep = $w->Component( | ||
| 925 | Frame => 'separator', | ||
| 926 | -height => 1, | ||
| 927 | -takefocus => 0, | ||
| 928 | )->pack(qw/-side right -fill y -anchor w/); | ||
| 929 | |||
| 930 | $sep->bind( "<B1-Motion>", | ||
| 931 | [$w=>'adjustMotion']); | ||
| 932 | $sep->bind("<ButtonRelease-1>", | ||
| 933 | [$w=>'Callback','-configurecommand']); | ||
| 934 | |||
| 935 | my $f = $w->Component( | ||
| 936 | Frame => "frame", | ||
| 937 | -takefocus => 0, | ||
| 938 | )->pack(qw/-side left -anchor n -fill both -expand 1/); | ||
| 939 | |||
| 940 | my $hdr; | ||
| 941 | if (defined $undln) { | ||
| 942 | |||
| 943 | $hdr = $f->HMButton( | ||
| 944 | -takefocus=>0, | ||
| 945 | -padx=>0, | ||
| 946 | -width=>1, | ||
| 947 | -borderwidth=>2, | ||
| 948 | -underline=>$undln, | ||
| 949 | -highlightthickness=>0 | ||
| 950 | )->pack(qw/-side top -anchor n -fill x -expand 0/); | ||
| 951 | } else { | ||
| 952 | $hdr = $f->HMButton( | ||
| 953 | -takefocus=>0, | ||
| 954 | -padx=>0, | ||
| 955 | -width=>1, | ||
| 956 | -borderwidth=>2, | ||
| 957 | -highlightthickness=>0 | ||
| 958 | )->pack(qw/-side top -anchor n -fill x -expand 0/); | ||
| 959 | } | ||
| 960 | $w->Advertise("heading" => $hdr); | ||
| 961 | |||
| 962 | my $lb; | ||
| 963 | my $dynamic = <<'END_STR'; | ||
| 964 | $lb = $f->HMCListbox( | ||
| 965 | -highlightthickness=>0, | ||
| 966 | -relief=>'flat', | ||
| 967 | -bd=>0, | ||
| 968 | -exportselection=>0, | ||
| 969 | -takefocus=>0, | ||
| 970 | END_STR | ||
| 971 | |||
| 972 | if ($Tk::HListbox::VERSION >= 2.3) { | ||
| 973 | foreach my $padarg (qw/-tpady -tpadx -ipady -ipadx/) { | ||
| 974 | $dynamic .= " $padarg => $args->{$padarg}," if (defined($args->{$padarg}) && $args->{$padarg} =~ /\d/o); | ||
| 975 | } | ||
| 976 | $dynamic .= "-showcursoralways => 1," if ($Tk::HListbox::VERSION >= 2.4); | ||
| 977 | if ($Tk::HListbox::VERSION >= 2.42) { | ||
| 978 | $dynamic .= "-state => '".$args->{'-state'}."',"; | ||
| 979 | $dynamic .= "-disabledforeground => '".$args->{'-disabledforeground'}."'," | ||
| 980 | if (defined $args->{'-disabledforeground'}); | ||
| 981 | } | ||
| 982 | } else { | ||
| 983 | foreach my $padarg (qw/-tpady -tpadx -ipady -ipadx/) { | ||
| 984 | delete $args->{$padarg} if (defined $args->{$padarg}); | ||
| 985 | } | ||
| 986 | } | ||
| 987 | $dynamic =~ s/\,$/\)\;/; | ||
| 988 | eval $dynamic; | ||
| 989 | $lb->pack(qw/-side top -anchor n -expand 1 -fill both/); | ||
| 990 | $w->Advertise("listbox" => $lb); | ||
| 991 | $w->Delegates (DEFAULT => $lb); | ||
| 992 | |||
| 993 | my $bgWidgets = $hdrBG ? [$f, $lb] : [$f, $hdr, $lb]; | ||
| 994 | my $fgWidgets = $hdrFG ? [$f, $lb] : [$f, $hdr, $lb]; | ||
| 995 | $w->ConfigSpecs( | ||
| 996 | # -background => [[$f, $lb], | ||
| 997 | -background => [$bgWidgets, | ||
| 998 | qw/background Background/, $Tk::NORMAL_BG], | ||
| 999 | -comparecommand => ['CALLBACK', undef, undef, | ||
| 1000 | sub{$_[0] cmp $_[1]}], | ||
| 1001 | -configurecommand => ['CALLBACK'], | ||
| 1002 | |||
| 1003 | -font => [[$hdr, $lb], qw/font Font/, undef], | ||
| 1004 | # -foreground => [[$lb], | ||
| 1005 | -foreground => [$fgWidgets, | ||
| 1006 | qw/foreground Foreground/, $Tk::NORMAL_FG], | ||
| 1007 | -headerbackground => [qw/PASSIVE headerBackground HeaderBackground/, $hdrBG], | ||
| 1008 | -headerforeground => [qw/PASSIVE headerForeground HeaderForeground/, $hdrFG], | ||
| 1009 | -disabledforeground => [qw/PASSIVE disabledForeground DisabledForeground/, $disableFG], | ||
| 1010 | -separatorwidth => [{-width => $sep}, | ||
| 1011 | qw/separatorWidth Separator 1/], | ||
| 1012 | -separatorcolor => [{-background => $sep}, | ||
| 1013 | qw/separatorColor Separator black/], | ||
| 1014 | -resizeable => [qw/METHOD resizeable Resizeable 1/], | ||
| 1015 | -sortable => [qw/PASSIVE sortable Sortable 1/], | ||
| 1016 | -text => [$hdr], | ||
| 1017 | -compound => [[$hdr], 'compound', 'compound', 'right'], | ||
| 1018 | -updatecommand => [$lb], | ||
| 1019 | -textwidth => [{-width => [$lb, $hdr]}], | ||
| 1020 | -reversearrow => [qw/PASSIVE reversearrow reversearrow 0/], | ||
| 1021 | -state => ['METHOD', 'state', 'State', 'normal'], | ||
| 1022 | -ipadx => [qw/PASSIVE/], | ||
| 1023 | -ipady => [qw/PASSIVE/], | ||
| 1024 | -tpadx => [qw/PASSIVE/], | ||
| 1025 | -tpady => [qw/PASSIVE/], | ||
| 1026 | DEFAULT => [$lb] | ||
| 1027 | ); | ||
| 1028 | $w->ConfigAlias( | ||
| 1029 | -comparecmd => '-comparecommand', | ||
| 1030 | -width => '-textwidth' | ||
| 1031 | ); | ||
| 1032 | |||
| 1033 | $hdr->configure('-background' => $hdrBG) if ($hdrBG); | ||
| 1034 | $hdr->configure('-foreground' => $hdrFG) if ($hdrFG); | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | ###################################################################### | ||
| 1038 | ## HMLColumn Configuration methods (call via configure/cget). | ||
| 1039 | ###################################################################### | ||
| 1040 | |||
| 1041 | sub resizeable { | ||
| 1042 | my ($w, $value) = @_; | ||
| 1043 | return $w->{Configure}{-resizeable} unless (defined $value); | ||
| 1044 | |||
| 1045 | $w->Subwidget("separator")->configure( | ||
| 1046 | -cursor => ($value ? 'sb_h_double_arrow' : 'xterm') | ||
| 1047 | ); | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | sub compare { | ||
| 1051 | # my ($w,$a,$b) = @_; | ||
| 1052 | |||
| 1053 | # $w->Callback(-comparecommand => $a, $b); | ||
| 1054 | shift->Callback(-comparecommand => $_[0], $_[1]); | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | sub setWidth { | ||
| 1058 | my ($w, $pixels) = @_; | ||
| 1059 | $pixels -= $w->Subwidget("separator")->width; | ||
| 1060 | return unless ($pixels >= 0); | ||
| 1061 | |||
| 1062 | $w->Subwidget("listbox")->GeometryRequest( | ||
| 1063 | $pixels,$w->Subwidget("listbox")->height); | ||
| 1064 | $w->Subwidget("heading")->configure(-pixelwidth=>$pixels); | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | ###################################################################### | ||
| 1068 | ## HMLColumn Private methods (Do not depend on these methods being present) | ||
| 1069 | ###################################################################### | ||
| 1070 | |||
| 1071 | sub _setButtonHeight { | ||
| 1072 | my $w = shift; | ||
| 1073 | my $pady = shift; | ||
| 1074 | $w->Subwidget("heading")->configure(-pady => $pady) if (defined $pady); | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | ## Adjust size of column. | ||
| 1078 | sub adjustMotion { | ||
| 1079 | my ($w) = @_; | ||
| 1080 | return if ($w->getState() =~ /d/o); | ||
| 1081 | |||
| 1082 | $w->setWidth($w->pointerx - $w->rootx) if ($w->resizeable); | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | sub state { | ||
| 1086 | my $w = shift; | ||
| 1087 | my $state = shift; | ||
| 1088 | #FOOBARS FOCUS?! return $w->{Configure}{'-state'} || undef unless (defined($state) && $state); | ||
| 1089 | |||
| 1090 | $w->Subwidget("listbox")->configure('-state' => $state, -takefocus => 0); | ||
| 1091 | $w->Subwidget("heading")->configure('-state' => $state, -takefocus => 0); | ||
| 1092 | $w->{'_hlistboxstate'} = $state; | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | sub getState { | ||
| 1096 | return shift->{'_hlistboxstate'}; | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | } ## END PRELOADING OF HMLColumn | ||
| 1100 | |||
| 1101 | ###################################################################### | ||
| 1102 | ## Package: Tk::HMListbox | ||
| 1103 | ## Purpose: Multicolumn widget used to display tabular data | ||
| 1104 | ## with optional sorted-column indicator arrows and keyboard- | ||
| 1105 | ## bound column-sorting capability. | ||
| 1106 | ## This widget has the ability to sort data by column, | ||
| 1107 | ## hide/show columns, and the ability to change the order | ||
| 1108 | ## of columns on the fly | ||
| 1109 | |||
| 1110 | package Tk::HMListbox; | ||
| 1111 | use strict; | ||
| 1112 | use Carp; | ||
| 1113 | use vars qw($VERSION); | ||
| 1114 | $VERSION = '4'; | ||
| 1115 | |||
| 1116 | use Tk; | ||
| 1117 | |||
| 1118 | ## Overidden Scrolled method to suit the purposes of HMListbox | ||
| 1119 | ## I want -columns to be configured LAST no matter what. | ||
| 1120 | ## I know full well that I'm overriding the Scrolled method | ||
| 1121 | ## and I don't need a warning broadcasting the fact. | ||
| 1122 | |||
| 1123 | no warnings; | ||
| 1124 | |||
| 1125 | sub Tk::Widget::Scrolled { | ||
| 1126 | my ($parent, $kind, %args) = @_; | ||
| 1127 | |||
| 1128 | my $colAR; | ||
| 1129 | $colAR = delete $args{'-columns'} if $kind eq "HMListbox"; | ||
| 1130 | |||
| 1131 | ## Find args that are Frame create time args | ||
| 1132 | my @args = Tk::Frame->CreateArgs($parent,\%args); | ||
| 1133 | my $name = delete $args{'Name'}; | ||
| 1134 | push(@args,'Name' => $name) if (defined $name); | ||
| 1135 | my $cw = $parent->Frame(@args); | ||
| 1136 | @args = (); | ||
| 1137 | my $scrollbarfocus = (defined $args{'-scrollbarfocus'}) ? delete($args{'-scrollbarfocus'}) : undef; | ||
| 1138 | |||
| 1139 | ## Now remove any args that Frame can handle | ||
| 1140 | foreach my $k ('-scrollbars',map($_->[0],$cw->configure)) { | ||
| 1141 | push(@args,$k,delete($args{$k})) if (exists $args{$k}) | ||
| 1142 | } | ||
| 1143 | ## Anything else must be for target widget - pass at widget create time | ||
| 1144 | my $w = $cw->$kind(%args); | ||
| 1145 | $cw->{'__HMListbox__'} = $w if ($w =~ /HMListbox/o); #JWT:SAVE ME FOR USE IN THE LEFT-TAB BIND ABOVE! | ||
| 1146 | ## Now re-set %args to be ones Frame can handle | ||
| 1147 | ## RCS NOTE: I've also slightly modified the ConfigSpecs | ||
| 1148 | %args = @args; | ||
| 1149 | $args{'-scrollbarfocus'} = $scrollbarfocus if (defined($scrollbarfocus) && $scrollbarfocus =~ /[xy01]/); | ||
| 1150 | $cw->ConfigSpecs( | ||
| 1151 | '-scrollbars' => [qw/METHOD scrollbars Scrollbars se/], | ||
| 1152 | '-background' => [$w, qw/background Background/, undef], | ||
| 1153 | '-foreground' => [$w, qw/foreground Foreground/, undef], | ||
| 1154 | '-scrollbarfocus' => ['METHOD', 'scrollbarfocus', 'Focus', ''], | ||
| 1155 | ); | ||
| 1156 | $cw->AddScrollbars($w); | ||
| 1157 | $cw->Default("\L$kind" => $w); | ||
| 1158 | $cw->Delegates('bind' => $w, 'bindtags' => $w, 'menu' => $w); | ||
| 1159 | $cw->ConfigDefault(\%args); | ||
| 1160 | delete($args{'-scrollbarfocus'}) if (defined $args{'-scrollbarfocus'}); #DELETE IT AGAIN SO THIS ONE WON'T BITCH! | ||
| 1161 | $cw->configure(%args); | ||
| 1162 | $args{'-scrollbarfocus'} = $scrollbarfocus if (defined($scrollbarfocus) && $scrollbarfocus =~ /[xy01]/); | ||
| 1163 | $cw->configure(-columns => $colAR) if $colAR; | ||
| 1164 | $w->scrollbarfocus($scrollbarfocus) if ($scrollbarfocus =~ /[xy01]/); | ||
| 1165 | |||
| 1166 | #JWT:FOR SOME STUPID REASON THIS 1 DIRECTIONAL BINDING REQUIRES A SLIGHT FRACTION ABOVE ZERO?! | ||
| 1167 | #(THE OTHER 3 WORK FINE AUTOMATICALLY!) :-/ | ||
| 1168 | $w->parent->Subwidget('yscrollbar')->bind('<Home>', sub { | ||
| 1169 | $cw->{'__HMListbox__'}->_chgView('yview','moveto',0.0001) if (defined $cw->{'__HMListbox__'}); | ||
| 1170 | }); | ||
| 1171 | return $cw; | ||
| 1172 | } | ||
| 1173 | |||
| 1174 | use warnings; | ||
| 1175 | |||
| 1176 | require Tk::Pane; | ||
| 1177 | use base qw(Tk::Frame); | ||
| 1178 | Tk::Widget->Construct('HMListbox'); | ||
| 1179 | |||
| 1180 | sub ClassInit { | ||
| 1181 | my ($class,$mw) = @_; | ||
| 1182 | $mw->bind($class,'<Configure>',['_yscrollCallback']); | ||
| 1183 | $mw->bind($class,'<Down>',['_upDown',1]); | ||
| 1184 | $mw->bind($class,'<Up>', ['_upDown',-1]); | ||
| 1185 | $mw->bind($class,'<Control-Down>',['_ctrlupDown',1]); | ||
| 1186 | $mw->bind($class,'<Control-Up>', ['_ctrlupDown',-1]); | ||
| 1187 | $mw->bind($class,'<Shift-Up>', ['_extendUpDown',-1]); | ||
| 1188 | $mw->bind($class,'<Shift-Down>',['_extendUpDown',1]); | ||
| 1189 | $mw->bind($class,'<Control-Home>','_cntrlHome'); | ||
| 1190 | $mw->bind($class,'<Control-End>','_cntrlEnd'); | ||
| 1191 | $mw->bind($class,'<Shift-Control-Home>',['_dataExtend',0]); | ||
| 1192 | $mw->bind($class,'<Shift-Control-End>',['_dataExtend',Ev('index', 'end')]); | ||
| 1193 | $mw->bind($class,'<Control-slash>','_selectAll'); | ||
| 1194 | $mw->bind($class,'<Control-backslash>','_deselectAll'); | ||
| 1195 | $mw->bind($class,'<FocusIn>','focus'); | ||
| 1196 | $mw->bind($class,'<FocusOut>','unfocus'); | ||
| 1197 | $mw->bind($class,'<Escape>', '_Cancel'); | ||
| 1198 | $mw->bind($class, '<Home>', ['_chgView','xview','moveto',0.001]); | ||
| 1199 | $mw->bind($class, '<End>', ['_chgView','xview','moveto',1]); | ||
| 1200 | $mw->bind($class, '<Prior>', ['_chgView','yview','scroll',-1,'pages']); | ||
| 1201 | $mw->bind($class, '<Next>', ['_chgView','yview','scroll',1,'pages']); | ||
| 1202 | $mw->bind($class, '<Control-Prior>', ['CtrlPriorNext',-1]); | ||
| 1203 | $mw->bind($class, '<Control-Next>', ['CtrlPriorNext',1]); | ||
| 1204 | $mw->bind($class,'<space>',['SpaceSelect',Ev('index','active')]); | ||
| 1205 | $mw->bind($class,'<Select>',['SpaceSelect',Ev('index','active')]); | ||
| 1206 | |||
| 1207 | my $downArrowBits = pack("b10"x10, | ||
| 1208 | "..........", | ||
| 1209 | "..........", | ||
| 1210 | "..........", | ||
| 1211 | ".#########", | ||
| 1212 | "..#######.", | ||
| 1213 | "...#####..", | ||
| 1214 | "....###...", | ||
| 1215 | ".....#....", | ||
| 1216 | "..........", | ||
| 1217 | ".........." | ||
| 1218 | ); | ||
| 1219 | $mw->DefineBitmap('downarrow' => 10,10, $downArrowBits); | ||
| 1220 | my $upArrowBits = pack("b10"x10, | ||
| 1221 | "..........", | ||
| 1222 | "..........", | ||
| 1223 | "..........", | ||
| 1224 | ".....#....", | ||
| 1225 | "....###...", | ||
| 1226 | "...#####..", | ||
| 1227 | "..#######.", | ||
| 1228 | ".#########", | ||
| 1229 | "..........", | ||
| 1230 | ".........." | ||
| 1231 | ); | ||
| 1232 | $mw->DefineBitmap('uparrow' => 10,10, $upArrowBits); | ||
| 1233 | my $noArrowBits = pack("b10"x10, | ||
| 1234 | "..........", | ||
| 1235 | "..........", | ||
| 1236 | "..........", | ||
| 1237 | "..........", | ||
| 1238 | "..........", | ||
| 1239 | "..........", | ||
| 1240 | "..........", | ||
| 1241 | "..........", | ||
| 1242 | "..........", | ||
| 1243 | ".........." | ||
| 1244 | ); | ||
| 1245 | $mw->DefineBitmap('noarrow' => 10,10, $noArrowBits); | ||
| 1246 | |||
| 1247 | } | ||
| 1248 | |||
| 1249 | ## Do some slightly tricky stuff: The -columns option, if called is | ||
| 1250 | ## guaranteed to be confiugred last of all the options submitted. | ||
| 1251 | ## NOTE: The args hash is cleared out if a columns option is sent | ||
| 1252 | ## so that all the options won't be reconfigured again immediately | ||
| 1253 | ## after this method finishes. ALso, if Scrolled is called, then | ||
| 1254 | ## the -columns option will never make it down to this level so | ||
| 1255 | |||
| 1256 | sub Populate { | ||
| 1257 | my ($w, $args) = @_; | ||
| 1258 | |||
| 1259 | $w->{'-showcursoralways'} = delete($args->{'-showcursoralways'}) if (defined $args->{'-showcursoralways'}); | ||
| 1260 | # $w->SUPER::Populate($args); | ||
| 1261 | |||
| 1262 | $w->{'_columns'} = []; ## Array of HMLColumn objects | ||
| 1263 | $w->{'_sortcol'} = -1; ## Column used for sorting | ||
| 1264 | $w->{'_sortcolumns'} = []; | ||
| 1265 | $w->{'_sort_descending'} = 0; ## Flag for ascending/desc. sort order | ||
| 1266 | $w->{'_top'} = 0; | ||
| 1267 | $w->{'_bottom'} = 0; | ||
| 1268 | $w->{'_lastactive'} = 0; | ||
| 1269 | $w->{'_hasfocus'} = 0; | ||
| 1270 | |||
| 1271 | my $pane = $w->Component( | ||
| 1272 | Pane => "pane", | ||
| 1273 | -sticky => 'nsew', | ||
| 1274 | -takefocus => 0, | ||
| 1275 | )->pack(-expand=>1,-fill=>'both'); | ||
| 1276 | |||
| 1277 | my $font; | ||
| 1278 | if ($Tk::platform eq 'MSWin32') { | ||
| 1279 | $font = "{MS Sans Serif} 8"; | ||
| 1280 | } else { | ||
| 1281 | $font = "Helvetica -12 bold"; | ||
| 1282 | } | ||
| 1283 | |||
| 1284 | my $hdr = $w->Subwidget('heading'); | ||
| 1285 | |||
| 1286 | $w->ConfigSpecs( | ||
| 1287 | -background => [qw/METHOD background Background/, $Tk::NORMAL_BG ], | ||
| 1288 | -columns => [qw/METHOD/], | ||
| 1289 | -configurecommand => [qw/CALLBACK/], | ||
| 1290 | -font => [qw/METHOD font Font/, $font], | ||
| 1291 | -foreground => [qw/METHOD foreground Foreground/, $Tk::NORMAL_FG ], | ||
| 1292 | -headerbackground => [qw/METHOD headerbackground headerBackground/, $Tk::NORMAL_BG ], | ||
| 1293 | -headerforeground => [qw/METHOD headerforeground headerForeground/, $Tk::NORMAL_FG ], | ||
| 1294 | -height => [qw/METHOD height Height 10/], | ||
| 1295 | -moveable => [qw/PASSIVE moveable Moveable 1/], | ||
| 1296 | -resizeable => [qw/METHOD resizeable Resizeable 1/], | ||
| 1297 | -selectbackground => [qw/METHOD selectBackground Background/, $Tk::SELECT_BG], | ||
| 1298 | -selectborderwidth => [qw/METHOD selectBorderwidth Borderwidth 1/], | ||
| 1299 | -selectforeground => [qw/METHOD selectForeground Foreground/, $Tk::SELECT_FG], | ||
| 1300 | -disabledforeground => [qw/PASSIVE disabledForeground DisabledForeground/, $Tk::DISABLED_FG], | ||
| 1301 | -selectmode => [qw/METHOD selectMode Mode browse/], | ||
| 1302 | -compound => [qw/METHOD compound compound right/], | ||
| 1303 | -showallsortcolumns => [qw/METHOD showallsortcolumns showallsortcolumns 0/], | ||
| 1304 | -jwtlistboxhack => [qw/PASSIVE jwtlistboxhack jwtlistboxhack 0/], #NOW USELESS (W/HList), LEFT FOR BACK-COMPAT. | ||
| 1305 | -fillcolumn => [qw/PASSIVE fillcolumn fillColumn -1/], | ||
| 1306 | -focuscolumn => [qw/PASSIVE focuscolumn focusColumn -1/], | ||
| 1307 | -nocolumnfocus => [qw/PASSIVE nocolumnfocus nocolumnfocus 0/], | ||
| 1308 | -separatorcolor => [qw/METHOD separatorColor Foreground black/], | ||
| 1309 | -separatorwidth => [qw/METHOD separatorWidth SeparatorWidth 1/], | ||
| 1310 | -sortable => [qw/METHOD sortable Sortable 1/], | ||
| 1311 | -itemtype => [qw/PASSIVE itemtype Itemtype text/], | ||
| 1312 | -takefocus => ['METHOD', 'takeFocus', 'TakeFocus', ''], | ||
| 1313 | -textwidth => [qw/METHOD textWidth Width 10/], | ||
| 1314 | -width => [qw/METHOD width Width/, undef], | ||
| 1315 | -xscrollcommand => [$pane], | ||
| 1316 | -yscrollcommand => ['CALLBACK'], | ||
| 1317 | -ipadx => [qw/PASSIVE/], | ||
| 1318 | -ipady => [qw/PASSIVE/], | ||
| 1319 | -tpadx => [qw/PASSIVE/], | ||
| 1320 | -tpady => [qw/PASSIVE/], | ||
| 1321 | -showcursoralways => [qw/PASSIVE showcursoralways showcursoralways 0/], | ||
| 1322 | -state => ['METHOD', 'state', 'State', 'normal'], | ||
| 1323 | ); | ||
| 1324 | |||
| 1325 | $w->ConfigAlias( | ||
| 1326 | -selectbg => "-selectbackground", | ||
| 1327 | -selectbd => "-selectborderwidth", | ||
| 1328 | -selectfg => "-selectforeground", | ||
| 1329 | -sepcolor => "-separatorcolor", | ||
| 1330 | -sepwidth => "-separatorwidth", | ||
| 1331 | ); | ||
| 1332 | #JWT:HAD TO DO THESE THIS WAY INSTEAD OF IN THE CLASS, ELSE THEY DON'T WORK!: | ||
| 1333 | $w->bindRows('<Home>', ['_chgView', 'xview','moveto',0.001]); | ||
| 1334 | $w->bindRows('<End>', ['_chgView', 'xview','moveto',1]); | ||
| 1335 | $w->bindRows('<Control-ButtonPress-1>', ['CtrlButtonPress', Ev('index',Ev('@'))]); | ||
| 1336 | |||
| 1337 | $w->bindRows('<ButtonPress-1>', [sub { | ||
| 1338 | my $w = shift; | ||
| 1339 | my $clickedon = $_[0]; | ||
| 1340 | |||
| 1341 | if (Tk::Exists($w)) { | ||
| 1342 | return if ($w->{Configure}{'-state'} =~ /d/o); | ||
| 1343 | $w->focus if (!$w->{'_hasfocus'} && $w->{'_ourtakefocus'}); | ||
| 1344 | } | ||
| 1345 | }, Ev('index',Ev('@'))] | ||
| 1346 | ); | ||
| 1347 | |||
| 1348 | $w->bindRows('<Alt-ButtonPress-1>', [sub { | ||
| 1349 | my $w = shift; | ||
| 1350 | my $clickedon = $_[0]; | ||
| 1351 | |||
| 1352 | if (Tk::Exists($w)) { | ||
| 1353 | return if ($w->{Configure}{'-state'} =~ /d/o); | ||
| 1354 | my $index = $w->_firstVisible->index('anchor'); | ||
| 1355 | $w->focus if (!$w->{'_hasfocus'} && $w->{'_ourtakefocus'}); | ||
| 1356 | $w->activate($index); | ||
| 1357 | } | ||
| 1358 | }, Ev('index',Ev('@'))] | ||
| 1359 | ); | ||
| 1360 | } | ||
| 1361 | |||
| 1362 | sub CtrlButtonPress { | ||
| 1363 | my $w = shift; | ||
| 1364 | return unless (defined $w->_firstVisible); | ||
| 1365 | |||
| 1366 | my $clickedon = $w->_firstVisible->index('active'); | ||
| 1367 | my @cursel = $w->_firstVisible->curselection; | ||
| 1368 | if (Tk::Exists($w)) { | ||
| 1369 | return if ($w->{Configure}{'-state'} =~ /d/o); | ||
| 1370 | $w->activate($w->_firstVisible->index('anchor')); | ||
| 1371 | } | ||
| 1372 | } | ||
| 1373 | |||
| 1374 | sub scrollbarfocus { #JWT: ALLOW THEM TO SPECIFY WHICH, IF ANY SCROLLBARS TAKE FOCUS: | ||
| 1375 | my $w = shift; | ||
| 1376 | my $focusopt = shift; | ||
| 1377 | return $w->{Configure}{'-scrollbarfocus'} || undef unless (defined($focusopt)); | ||
| 1378 | |||
| 1379 | if ($focusopt eq '0') { | ||
| 1380 | $w->parent->Subwidget('xscrollbar')->configure(-takefocus => 0); | ||
| 1381 | $w->parent->Subwidget('yscrollbar')->configure(-takefocus => 0); | ||
| 1382 | } elsif (! $focusopt) { | ||
| 1383 | return; | ||
| 1384 | } elsif ($focusopt eq '1' || ($focusopt =~ /x/o && $focusopt =~ /y/o)) { | ||
| 1385 | $w->parent->Subwidget('xscrollbar')->configure(-takefocus => 1); | ||
| 1386 | $w->parent->Subwidget('yscrollbar')->configure(-takefocus => 1); | ||
| 1387 | } elsif ($focusopt =~ /x/o) { | ||
| 1388 | $w->parent->Subwidget('xscrollbar')->configure(-takefocus => 1); | ||
| 1389 | $w->parent->Subwidget('yscrollbar')->configure(-takefocus => 0); | ||
| 1390 | } elsif ($focusopt =~ /y/o || $focusopt !~ /x/o) { | ||
| 1391 | $w->parent->Subwidget('yscrollbar')->configure(-takefocus => 1); | ||
| 1392 | $w->parent->Subwidget('xscrollbar')->configure(-takefocus => 0); | ||
| 1393 | } | ||
| 1394 | } | ||
| 1395 | |||
| 1396 | sub getSortOrder { #JWT: ADDED THIS METHOD TO FETCH HOW THE LIST IS SORTED! | ||
| 1397 | my $w = shift; | ||
| 1398 | |||
| 1399 | my @l = scalar(@{$w->{'_sortcolumns'}}) ? @{$w->{'_sortcolumns'}} : (0); | ||
| 1400 | return ($w->{'_sort_descending'}||0, @l); | ||
| 1401 | } | ||
| 1402 | |||
| 1403 | sub setSortOrder { #JWT: ADDED THIS METHOD TO FETCH HOW THE LIST IS SORTED! | ||
| 1404 | my $w = shift; | ||
| 1405 | my ($sOrder, @sColumns) = @_; | ||
| 1406 | |||
| 1407 | $w->{'_sort_descending'} = $sOrder; | ||
| 1408 | $w->{'_sortcolumns'} = [@sColumns] if (defined $sColumns[0]); | ||
| 1409 | } | ||
| 1410 | |||
| 1411 | ###################################################################### | ||
| 1412 | ## Configuration methods (call via configure). | ||
| 1413 | ###################################################################### | ||
| 1414 | |||
| 1415 | ## Background is a slightly tricky option, this option would be a | ||
| 1416 | ## great candidate for "DESCENDANTS", except for the separator subwidget in | ||
| 1417 | ## each column set by separatorcolor which I'd prefer not to set in such | ||
| 1418 | ## a clumsy way. All other background colors are fair game, but I'd like | ||
| 1419 | ## to leave open the possibility for other exceptions such as separator. | ||
| 1420 | ## Besides I prefer that composite subwidgets manage their own component parts | ||
| 1421 | ## as much as possible. | ||
| 1422 | |||
| 1423 | sub background { | ||
| 1424 | my ($w, $val) = @_; | ||
| 1425 | return $w->{Configure}{'-background'} | ||
| 1426 | unless $val; | ||
| 1427 | |||
| 1428 | ## Ensure that the base Frame, pane and columns (if any) get set | ||
| 1429 | Tk::configure($w, "-background", $val); | ||
| 1430 | $w->Subwidget("pane")->configure("-background", $val); | ||
| 1431 | $w->_configureColumns("-background", $val); | ||
| 1432 | } | ||
| 1433 | |||
| 1434 | ## columns needs to be called last during creation time if set and I | ||
| 1435 | ## went to a great deal of trouble to guarantee this. The reason | ||
| 1436 | ## being is that it needs to use many of the other configurations to | ||
| 1437 | ## use as defaults for columns, and the ability to override any of them | ||
| 1438 | ## for individual columns. If these options (that the columns override) | ||
| 1439 | ## were called afterwards, then the reverse would happen. All the default | ||
| 1440 | ## would override the individually specified options. | ||
| 1441 | |||
| 1442 | sub columns { | ||
| 1443 | my ($w, $vAR) = @_; | ||
| 1444 | return $w->{Configure}{'-columns'} unless (defined $vAR); | ||
| 1445 | |||
| 1446 | $w->columnDelete(0, 'end'); | ||
| 1447 | map {$w->columnInsert('end', @$_)} @$vAR; | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | sub font { shift->_configureColumns('-font', @_) } | ||
| 1451 | sub foreground { shift->_configureColumns('-foreground', @_) } | ||
| 1452 | sub headerbackground { shift->_configureColumns('-headerbackground', @_); } | ||
| 1453 | sub headerforeground { shift->_configureColumns('-headerforeground', @_); } | ||
| 1454 | sub disabledforeground { shift->_configureColumns('-disabledforeground', @_); } | ||
| 1455 | sub height { shift->_configureColumns('-height', @_) } | ||
| 1456 | sub resizeable { shift->_configureColumns('-resizeable', @_) } | ||
| 1457 | sub selectbackground { shift->_configureColumns('-selectbackground', @_) } | ||
| 1458 | sub selectborderwidth { shift->_configureColumns('-selectborderwidth', @_) } | ||
| 1459 | sub selectforeground { shift->_configureColumns('-selectforeground', @_) } | ||
| 1460 | sub selectmode { shift->_configureColumns('-selectmode', @_) } | ||
| 1461 | sub compound { #JWT: ADDED THIS METHOD TO ALLOW USER TO DYNAMICALLY OVERWRITE WHERE THE SORT ARROW IMAGE DISPLAYS. | ||
| 1462 | my $w = shift; | ||
| 1463 | if (scalar(@_) > 0) { | ||
| 1464 | |||
| 1465 | $w->_configureColumns('-compound', @_); | ||
| 1466 | $w->{'-compound'} = $_[0]; | ||
| 1467 | } else { | ||
| 1468 | return $w->{'-compound'}; | ||
| 1469 | } | ||
| 1470 | } | ||
| 1471 | sub showallsortcolumns { #JWT: ADDED THIS METHOD TO ALLOW USER TO DYNAMICALLY OVERWRITE WHERE THE SORT ARROW IMAGE DISPLAYS. | ||
| 1472 | my $w = shift; if (scalar(@_) > 0) { | ||
| 1473 | |||
| 1474 | $w->{'-showallsortcolumns'} = $_[0]; | ||
| 1475 | } else | ||
| 1476 | |||
| 1477 | { | ||
| 1478 | return $w->{'-showallsortcolumns'}; | ||
| 1479 | } | ||
| 1480 | } | ||
| 1481 | sub columnFocus { #JWT:ALLOW A SPECIFIED COLUMN'S LISTBOX GET FOCUS SO THAT KEYBOARD CURSOR "UNDERSCORE" MAY BECOME (MORE) VISIBLE: | ||
| 1482 | my $w = shift; | ||
| 1483 | |||
| 1484 | if ($w->cget('-nocolumnfocus') != 1 && scalar(@_) > 0) { | ||
| 1485 | $w->{Configure}{'-focuscolumn'} = $_[0] if ($_[0] =~ /\d/o); | ||
| 1486 | } else { | ||
| 1487 | return $w->{Configure}{'-focuscolumn'}; | ||
| 1488 | } | ||
| 1489 | } | ||
| 1490 | sub focusColumn { #JWT:DEPRECIATED:USE columnFocus! | ||
| 1491 | return shift->columnFocus(@_) | ||
| 1492 | } | ||
| 1493 | sub setButtonHeight { #JWT:ALLOW USER TO SET HEIGHT OF BUTTONS (-pady): | ||
| 1494 | my $w = shift; | ||
| 1495 | foreach my $c (@{$w->{'_columns'}}) { | ||
| 1496 | $c->_setButtonHeight(@_); | ||
| 1497 | } | ||
| 1498 | } | ||
| 1499 | sub separatorcolor { shift->_configureColumns('-separatorcolor', @_ ) } | ||
| 1500 | sub separatorwidth { shift->_configureColumns('-separatorwidth', @_ ) } | ||
| 1501 | sub sortable { shift->_configureColumns('-sortable', @_) } | ||
| 1502 | sub itemtype { shift->_configureColumns('-itemtype', @_) } | ||
| 1503 | sub textwidth { shift->_configureColumns('-textwidth', @_) } | ||
| 1504 | |||
| 1505 | sub takefocus { | ||
| 1506 | my ($w, $val) = @_; | ||
| 1507 | return $w->{Configure}{'-takefocus'} unless (defined($val) && $val); | ||
| 1508 | |||
| 1509 | #JWT:NEEDS TO BE '' INSTEAD OF 1 FOR Tk (SO WE KEEP IT IN OUR OWN VARIABLE FOR OUR USE)!: | ||
| 1510 | $w->{'_ourtakefocus'} = $val; | ||
| 1511 | $w->{Configure}{'-takefocus'} = ($val =~ /0/o) ? 0 : ''; | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | sub width { | ||
| 1515 | my ($w, $v) = @_; | ||
| 1516 | |||
| 1517 | return $w->{Configure}{'-width'} unless defined $v; | ||
| 1518 | if ($v == 0) { | ||
| 1519 | $w->afterIdle(['_setWidth', $w]); | ||
| 1520 | } else { | ||
| 1521 | $w->Subwidget('pane')->configure(-width => $v); | ||
| 1522 | } | ||
| 1523 | } | ||
| 1524 | |||
| 1525 | ###################################################################### | ||
| 1526 | ## Private methods (Do not depend on these methods being present) | ||
| 1527 | ## | ||
| 1528 | ## For all methods which have _firstVisible, the method is delegated | ||
| 1529 | ## to the first visible (packed) HListbox | ||
| 1530 | ###################################################################### | ||
| 1531 | |||
| 1532 | ## This is the main callback that is bound to the subwidgets | ||
| 1533 | ## when using any of the public bind methods, The defined | ||
| 1534 | ## defined callback ($cb) is called from within it | ||
| 1535 | |||
| 1536 | sub _bindCallback { | ||
| 1537 | my ($w, $cb, $sw, $ci, $yCoord) = @_; | ||
| 1538 | |||
| 1539 | my $iHR = { '-subwidget' => $sw, '-column' => $ci }; | ||
| 1540 | if (defined($yCoord)) { | ||
| 1541 | $iHR->{'-row'} = $w->_getEntryFromY($sw, $yCoord); | ||
| 1542 | } | ||
| 1543 | if (ref $cb eq 'ARRAY') { | ||
| 1544 | my ($code,@args) = @$cb; | ||
| 1545 | return $w->$code($iHR, @args); | ||
| 1546 | } else { | ||
| 1547 | return $w->$cb($iHR); | ||
| 1548 | } | ||
| 1549 | } | ||
| 1550 | |||
| 1551 | ## bind subwidgets is used by other public bind methods to | ||
| 1552 | ## apply a callback to an event dequence of a particular subwidget | ||
| 1553 | ## within each of the columns. Any defined callbacks are passed | ||
| 1554 | ## to the _bindCallback which is actually the callback that gets | ||
| 1555 | ## bound. | ||
| 1556 | |||
| 1557 | sub _bindSubwidgets { | ||
| 1558 | my ($w,$subwidget,$sequence,$callback) = @_; | ||
| 1559 | my $col = 0; | ||
| 1560 | |||
| 1561 | return (keys %{$w->{'_bindings'}->{$subwidget}}) | ||
| 1562 | unless (defined $sequence); | ||
| 1563 | |||
| 1564 | unless (defined $callback) { | ||
| 1565 | $callback = $w->{'_bindings'}->{$subwidget}->{$sequence}; | ||
| 1566 | $callback = '' unless defined $callback; | ||
| 1567 | return $callback; | ||
| 1568 | } | ||
| 1569 | |||
| 1570 | if ($callback eq '') { | ||
| 1571 | foreach (@{$w->{'_columns'}}) { | ||
| 1572 | $_->Subwidget($subwidget)->Tk::bind($sequence,''); | ||
| 1573 | } | ||
| 1574 | delete $w->{'_bindings'}->{$subwidget}->{$sequence}; | ||
| 1575 | return ''; | ||
| 1576 | } | ||
| 1577 | my @args = ('_bindCallback', $callback); | ||
| 1578 | foreach (@{$w->{'_columns'}}) { | ||
| 1579 | my $sw = $_->Subwidget($subwidget); | ||
| 1580 | if ($sw->class ne "HMCListbox") { | ||
| 1581 | $sw->Tk::bind($sequence, [$w => @args, $sw, $col++]); | ||
| 1582 | } else { | ||
| 1583 | $sw->Tk::bind($sequence, [$w => @args, $sw, $col++, Ev('y')]); | ||
| 1584 | } | ||
| 1585 | } | ||
| 1586 | $w->{'_bindings'}->{$subwidget}->{$sequence} = $callback; | ||
| 1587 | return ''; | ||
| 1588 | } | ||
| 1589 | |||
| 1590 | ## handles config options that should be propagated to all HMLColumn | ||
| 1591 | ## subwidgets. Using the DEFAULT setting in ConfigSpecs would be one | ||
| 1592 | ## idea, but the pane subwidget is also a child, and Pane will not | ||
| 1593 | ## be able to handle many of the options being passed to this method. | ||
| 1594 | |||
| 1595 | sub _configureColumns { | ||
| 1596 | my ($w, $option, $value) = @_; | ||
| 1597 | return $w->{Configure}{$option} unless (defined $value); | ||
| 1598 | |||
| 1599 | foreach (@{$w->{'_columns'}}) { | ||
| 1600 | $_->configure("$option" => $value); | ||
| 1601 | } | ||
| 1602 | } | ||
| 1603 | |||
| 1604 | sub _cntrlEnd { shift->_firstVisible->Cntrl_End; } | ||
| 1605 | |||
| 1606 | sub _cntrlHome { shift->_firstVisible->Cntrl_Home; } | ||
| 1607 | |||
| 1608 | sub _chgView { | ||
| 1609 | my ($w, $fn, @args) = @_; | ||
| 1610 | |||
| 1611 | $fn = shift(@args) if ($fn =~ /HASH/o); | ||
| 1612 | my $code = "\$w->$fn('".join("','",@args)."')"; | ||
| 1613 | eval $code; | ||
| 1614 | } | ||
| 1615 | |||
| 1616 | sub _dataExtend { | ||
| 1617 | my ($w, $el) = @_; | ||
| 1618 | my $mode = $w->cget('-selectmode'); | ||
| 1619 | if ($mode eq 'extended') { | ||
| 1620 | my $anchor = $w->_firstVisible->index('anchor'); #JWT:ADDED - SINCE HList DOESN'T SUPPORT Listbox's Motion() FUNCTION: | ||
| 1621 | $w->activate($el); | ||
| 1622 | $w->see($el); | ||
| 1623 | $w->selectionSet($w->index($el), $anchor); #JWT:ADDED " | ||
| 1624 | # if ($w->selectionIncludes('anchor')) { #JWT:NEXT 3 REMOVED | ||
| 1625 | # $w->_firstVisible->Motion($el) | ||
| 1626 | # } | ||
| 1627 | } elsif ($mode eq 'multiple') { | ||
| 1628 | $w->activate($el); | ||
| 1629 | $w->see($el) | ||
| 1630 | } | ||
| 1631 | } | ||
| 1632 | |||
| 1633 | sub _deselectAll { | ||
| 1634 | my $w = shift; | ||
| 1635 | if ($w->cget('-selectmode') ne 'browse') { | ||
| 1636 | $w->selectionClear(0, 'end'); | ||
| 1637 | } | ||
| 1638 | } | ||
| 1639 | |||
| 1640 | ## implements sorting and dragging & drop of a column | ||
| 1641 | sub _dragOrSort { | ||
| 1642 | my ($w, $c, $sortAnyway) = @_; | ||
| 1643 | |||
| 1644 | return if ($w->state() =~ /disable/o); #DON'T ALLOW SORTING OR DRAGGING IF DISABLED! | ||
| 1645 | |||
| 1646 | if (!$w->cget('-moveable') || (defined($sortAnyway) && $sortAnyway)) { | ||
| 1647 | if ($c->cget('-sortable') && $w->{Configure}{-sortable}) { | ||
| 1648 | $w->sort(undef, $c); | ||
| 1649 | } | ||
| 1650 | return; | ||
| 1651 | } | ||
| 1652 | |||
| 1653 | my $h=$c->Subwidget("heading"); # The heading button of the column. | ||
| 1654 | |||
| 1655 | my $start_mouse_x = $h->pointerx; | ||
| 1656 | my $y_pos = $h->rooty; # This is constant through the whole operation. | ||
| 1657 | my $width = $h->width; | ||
| 1658 | my $left_limit = $w->rootx - 1; | ||
| 1659 | |||
| 1660 | # Find the rightmost, visible column | ||
| 1661 | my $right_end = 0; | ||
| 1662 | foreach (@{$w->{'_columns'}}) { | ||
| 1663 | if ($_->rootx + $_->width > $right_end) { | ||
| 1664 | $right_end = $_->rootx + $_->width; | ||
| 1665 | } | ||
| 1666 | } | ||
| 1667 | my $right_limit = $right_end + 1; | ||
| 1668 | |||
| 1669 | # Create a "copy" of the heading button, put it in a toplevel that matches | ||
| 1670 | # the size of the button, put the toplevel on top of the button. | ||
| 1671 | my $tl=$w->Toplevel; | ||
| 1672 | $tl->overrideredirect(1); | ||
| 1673 | $tl->geometry(sprintf("%dx%d+%d+%d", | ||
| 1674 | $h->width, $h->height, $h->rootx, $y_pos)); | ||
| 1675 | |||
| 1676 | my $b=$tl->HMButton | ||
| 1677 | (map{defined($_->[4]) ? ($_->[0]=>$_->[4]) : ()} $h->configure) | ||
| 1678 | ->pack(-expand=>1,-fill=>'both'); | ||
| 1679 | |||
| 1680 | # Move the toplevel with the mouse (as long as Button-1 is down). | ||
| 1681 | $h->bind("<Motion>", sub { | ||
| 1682 | my $new_x = $h->rootx - ($start_mouse_x - $h->pointerx); | ||
| 1683 | unless ($new_x + $width/2 < $left_limit || | ||
| 1684 | $new_x + $width/2 > $right_limit) | ||
| 1685 | { | ||
| 1686 | $tl->geometry(sprintf("+%d+%d",$new_x,$y_pos)); | ||
| 1687 | } | ||
| 1688 | }); | ||
| 1689 | |||
| 1690 | $h->bind("<ButtonRelease-1>", sub { | ||
| 1691 | my $rootx; | ||
| 1692 | eval {$rootx = $tl->rootx;}; | ||
| 1693 | return unless (defined $rootx); #ASSUME WE'RE DISABLED. | ||
| 1694 | |||
| 1695 | my $x = $rootx + ($tl->width/2); | ||
| 1696 | $tl->destroy; # Don't need this anymore... | ||
| 1697 | $h->bind("<Motion>",''); # Cancel binding | ||
| 1698 | |||
| 1699 | if ($h->rootx == $rootx) { | ||
| 1700 | # Button NOT moved, sort the column.... | ||
| 1701 | if ($c->cget('-sortable') && $w->{Configure}{-sortable}) { | ||
| 1702 | $w->sort(undef, $c); | ||
| 1703 | } | ||
| 1704 | return; | ||
| 1705 | } | ||
| 1706 | |||
| 1707 | # Button moved..... | ||
| 1708 | # Decide where to put the column. If the center of the dragged | ||
| 1709 | # button is on the left half of another heading, insert it -before | ||
| 1710 | # the column, otherwise insert it -after the column. | ||
| 1711 | foreach (@{$w->{'_columns'}}) { | ||
| 1712 | if ($_->ismapped) { | ||
| 1713 | my $left = $_->rootx; | ||
| 1714 | my $right = $left + $_->width; | ||
| 1715 | if ($left <= $x && $x <= $right) { | ||
| 1716 | if ($x - $left < $right - $x) { | ||
| 1717 | $w->columnShow($c,-before=>$_); | ||
| 1718 | } else { | ||
| 1719 | $w->columnShow($c,'-after'=>$_); | ||
| 1720 | } | ||
| 1721 | $w->update; | ||
| 1722 | $w->Callback(-configurecommand => $w); | ||
| 1723 | } | ||
| 1724 | } | ||
| 1725 | } | ||
| 1726 | }); | ||
| 1727 | } | ||
| 1728 | |||
| 1729 | sub _extendUpDown { shift->_firstVisible->ShiftUpDown(@_) } | ||
| 1730 | |||
| 1731 | ## Many of the methods in this package are very similar in that they | ||
| 1732 | ## delagate calls to the HMLColumn widgets. Because widgets can be | ||
| 1733 | ## be moved around (repacked) and hidden (packForget), any | ||
| 1734 | ## one widget may not be the "best" to be delegating calls to. The | ||
| 1735 | ## _columns variable holds an array of the columns but the order of | ||
| 1736 | ## this array does not correspond to the order in which they might | ||
| 1737 | ## by displayed, therefore this method is used to return the first | ||
| 1738 | ## "visible" or packed HMLColumn. RCS Note: It might be reasonable to | ||
| 1739 | ## make this a public method as it could conceivably useful to someone | ||
| 1740 | ## who might want to subclass this widget or use their own bindings. | ||
| 1741 | sub _firstVisible { | ||
| 1742 | my $w = shift; | ||
| 1743 | foreach my $c (@{$w->{'_columns'}}) { | ||
| 1744 | return $c if $c->ismapped; | ||
| 1745 | } | ||
| 1746 | return $w->{'_columns'}->[0]; | ||
| 1747 | } | ||
| 1748 | |||
| 1749 | sub _getEntryFromY { | ||
| 1750 | my ($cw, $sw, $yCoord) = @_; | ||
| 1751 | my $nearest = $sw->indexOf($sw->nearest($yCoord)); | ||
| 1752 | |||
| 1753 | return $nearest if ($nearest <= ($sw->size() - 1)); | ||
| 1754 | |||
| 1755 | #x my ($x, $y, $w, $h) = $sw->bbox($nearest); #JWT:NEXT 4 REMOVED SINCE HLIST DOESN'T SUPPORT bbox & THIS CODE DOESN'T SEEM TO BE NEEDED ANYWAY. | ||
| 1756 | #x my $lastY = $y + $h; | ||
| 1757 | #x return -1 if ($yCoord > $lastY); | ||
| 1758 | #x return $nearest; | ||
| 1759 | return -1; | ||
| 1760 | } | ||
| 1761 | |||
| 1762 | ## Used to distribute method calls which would otherwise be called for | ||
| 1763 | ## for one HMCListbox (Within a column), Each HMCListbox is a modified | ||
| 1764 | ## HListbox whose methods end up passing the code and arguments that need | ||
| 1765 | ## to be called to this method where they are invoked for each column | ||
| 1766 | ## It's an interesting, although complex, interaction and it's worth | ||
| 1767 | ## tracing to follow the program flow. | ||
| 1768 | |||
| 1769 | #sub _motion { shift->_firstVisible->Motion(@_) } #JWT:REMOVED - SINCE HList DOESN'T SUPPORT Listbox's Motion() FUNCTION: | ||
| 1770 | sub _selectAll { shift->_firstVisible->SelectAll; } | ||
| 1771 | sub _Cancel { shift->_firstVisible->Cancel; } | ||
| 1772 | |||
| 1773 | sub _selectionUpdate { | ||
| 1774 | my ($w, $code, $l, @args) = @_; | ||
| 1775 | |||
| 1776 | if (@args) { | ||
| 1777 | foreach (@{$w->{'_columns'}}) { | ||
| 1778 | &$code($_->Subwidget("listbox"), @args); | ||
| 1779 | } | ||
| 1780 | } else { | ||
| 1781 | # &$code($w->{'_columns'}->[0]->Subwidget("listbox")); | ||
| 1782 | foreach (@{$w->{'_columns'}}) { | ||
| 1783 | &$code($_->Subwidget("listbox")); | ||
| 1784 | } | ||
| 1785 | } | ||
| 1786 | } | ||
| 1787 | |||
| 1788 | ## dynamically sets the width of the widget by calculating | ||
| 1789 | ## the width of each of the currently visible columns. | ||
| 1790 | ## This is generally called during creation time when -width | ||
| 1791 | ## is set to 0. | ||
| 1792 | |||
| 1793 | sub _setWidth { | ||
| 1794 | my ($w) = shift; | ||
| 1795 | my $width = 0; | ||
| 1796 | foreach my $c (@{$w->{'_columns'}}) { | ||
| 1797 | my $lw = $c->Subwidget('heading')->reqwidth; | ||
| 1798 | my $sw = $c->Subwidget('separator')->reqwidth; | ||
| 1799 | $width += ($lw + $sw); | ||
| 1800 | } | ||
| 1801 | $w->Subwidget('pane')->configure(-width => $width); | ||
| 1802 | } | ||
| 1803 | |||
| 1804 | |||
| 1805 | |||
| 1806 | sub _upDown { shift->_firstVisible->UpDown(@_) } | ||
| 1807 | |||
| 1808 | sub _ctrlupDown #JWT:TAKE ADVANTAGE OF PERSONAL LISTBOX HACK TO ALLOW MULTIPLE SELECTIONS IN "BROWSE" MODE VIA KEYBOARD (Ctrl-Up, Ctrl-Down, and Spacebar): | ||
| 1809 | { | ||
| 1810 | my $w = shift; | ||
| 1811 | if (defined($w->{Configure}{'-jwtlistboxhack'}) && $w->{Configure}{'-jwtlistboxhack'} == 1) { | ||
| 1812 | no warnings; | ||
| 1813 | eval { $w->_firstVisible->Tk::Listbox::CtrlUpDown(@_); }; | ||
| 1814 | use warnings; | ||
| 1815 | } else { | ||
| 1816 | $w->_firstVisible->UpDown(@_); | ||
| 1817 | } | ||
| 1818 | } | ||
| 1819 | |||
| 1820 | sub _yscrollCallback { | ||
| 1821 | my ($w, $top, $bottom) = @_; | ||
| 1822 | |||
| 1823 | return unless ($w->cget(-yscrollcommand)); | ||
| 1824 | |||
| 1825 | unless (defined($top)) { | ||
| 1826 | # Called internally | ||
| 1827 | my $c = $w->_firstVisible; | ||
| 1828 | if (Exists($c) && $c->ismapped){ | ||
| 1829 | ($top,$bottom) = $c->yview; | ||
| 1830 | } else { | ||
| 1831 | ($top,$bottom) = (0,1); | ||
| 1832 | } | ||
| 1833 | } | ||
| 1834 | |||
| 1835 | if ($top != $w->{'_top'} || $bottom != $w->{'_bottom'}) { | ||
| 1836 | $w->Callback(-yscrollcommand=>$top,$bottom); | ||
| 1837 | $w->{'_top'} = $top; | ||
| 1838 | $w->{'_bottom'} = $bottom; | ||
| 1839 | } | ||
| 1840 | } | ||
| 1841 | |||
| 1842 | ###################################################################### | ||
| 1843 | ## Exported (Public) methods (listed alphabetically) | ||
| 1844 | ###################################################################### | ||
| 1845 | |||
| 1846 | ## Activate a row | ||
| 1847 | sub activate { | ||
| 1848 | my $w = shift; | ||
| 1849 | |||
| 1850 | return unless (defined $w->_firstVisible); | ||
| 1851 | |||
| 1852 | if ($w->{Configure}{'-state'} !~ /d/o | ||
| 1853 | && ($w->{'-showcursoralways'} || ($w->focusCurrent && $w->{'_hasfocus'}))) { | ||
| 1854 | $w->_firstVisible->activate(@_); | ||
| 1855 | } else { | ||
| 1856 | $w->{'_lastactive'} = $_[0]; | ||
| 1857 | $w->_firstVisible->activate(undef); | ||
| 1858 | $w->_firstVisible->anchorSet(@_); | ||
| 1859 | } | ||
| 1860 | } | ||
| 1861 | |||
| 1862 | #JWT: NOTE:FOCUSING IS VERY TRICKY AND REQUIRED MUCH EXPERIMATION AND HACKS TO GET WORKING PROPERLY: | ||
| 1863 | # THE MAIN ISSUE HS LEFT-TABBING AND THE INABILITY TO TELL HOW FOCUS ARRIVED. IN Tk WHEN FOCUS LANDS | ||
| 1864 | # ON A DISABLED WIDGET, A "focusNext" IS DONE TO MOVE FOCUS TO THE NEXT WIDGET AUTOMATICALLY. THIS | ||
| 1865 | # WORKS IN ALL CASES *EXCEPT* IF FOCUS ARRIVED FROM A LEFT-TAB, IN WHICH CASE, A "focusPrev" SHOULD | ||
| 1866 | # BE EXECUTED INSTEAD. FORTUNATELY, IN OUR CASE, THE "SCROLLED" FRAME THAT SURROUNDS OUR WIDGET | ||
| 1867 | # ALSO HAS A SECOND WIDGET-FRAME FOR THE SCROLLBARS (WHETHER DISPLAYED OR NOT) THAT'S "NEXT" IN THE | ||
| 1868 | # TAB-CIRCULATE ORDER FROM OUR WIDGET. BY BINDING LEFT-TAB TO THE "SCROLLED" FRAME, WE ARE ABLE TO | ||
| 1869 | # KNOW THAT ANY FOCUS COMING TO OUR WIDGET FIRST COMES THROUGH THE SCROLLBARS (EVEN IF NONE, ONE, OR | ||
| 1870 | # BOTH ARE VISIBLE AND TAKE FOCUS OR NOT. ARMED WITH THAT INFORMATION, WE CAN THEN DETERMINE WHETHER | ||
| 1871 | # A focusPrev OR focusNext SHOULD BE DONE TO PASS ON THE FOCUS WHEN OUR WIDGET IS DISABLED! | ||
| 1872 | |||
| 1873 | sub focus | ||
| 1874 | { | ||
| 1875 | my $w = shift; | ||
| 1876 | if ($w->{Configure}{'-state'} =~ /d/o) { | ||
| 1877 | $w->focusNext; | ||
| 1878 | return; | ||
| 1879 | } | ||
| 1880 | |||
| 1881 | $w->Tk::focus; | ||
| 1882 | $w->{'_hasfocus'} = 1; | ||
| 1883 | my $c = (defined($w->{Configure}{'-focuscolumn'}) && $w->{Configure}{'-focuscolumn'} >= 0) | ||
| 1884 | ? $w->columnGet($w->{Configure}{'-focuscolumn'}) #User specified which one to get focus. | ||
| 1885 | : $w->_firstVisible; | ||
| 1886 | #Default to 1st one visible if user did not pick one. | ||
| 1887 | if (defined($c) && $w->cget('-nocolumnfocus') != 1) { | ||
| 1888 | $c->Subwidget("listbox")->focus(@_); | ||
| 1889 | $c->Subwidget("listbox")->bind('<<LeftTab>>', sub { | ||
| 1890 | $w->focusPrev; | ||
| 1891 | Tk->break; | ||
| 1892 | }); | ||
| 1893 | } | ||
| 1894 | return if ($w->{'-showcursoralways'}); | ||
| 1895 | |||
| 1896 | #RESTORE CURSOR WHEN FOCUS IS GAINED: | ||
| 1897 | my $indx = $w->index('active'); | ||
| 1898 | $indx = $w->{'_lastactive'} unless (defined($indx) && $indx =~ /\d/); | ||
| 1899 | $indx = 0 unless (defined($indx) && $indx =~ /\d/); | ||
| 1900 | $w->activate($indx) if ($w->index('end') >= 0); | ||
| 1901 | } | ||
| 1902 | |||
| 1903 | sub unfocus | ||
| 1904 | { | ||
| 1905 | my $w = shift; | ||
| 1906 | if ($w->{'-showcursoralways'}) { | ||
| 1907 | $w->{'_hasfocus'} = 0; | ||
| 1908 | return; | ||
| 1909 | } | ||
| 1910 | |||
| 1911 | $w->{'_lastactive'} = $w->index('active'); | ||
| 1912 | $w->{'_hasfocus'} = 0; | ||
| 1913 | $w->activate($w->{'_lastactive'}) if ($w->index('end') >= 0); | ||
| 1914 | } | ||
| 1915 | |||
| 1916 | sub bindColumns { shift->_bindSubwidgets('heading',@_) } | ||
| 1917 | sub bindRows { shift->_bindSubwidgets('listbox',@_) } | ||
| 1918 | sub bindSeparators { shift->_bindSubwidgets('separator',@_) } | ||
| 1919 | |||
| 1920 | sub columnConfigure { | ||
| 1921 | my ($w, $index, %args) = @_; | ||
| 1922 | $w->columnGet($index)->configure(%args); | ||
| 1923 | } | ||
| 1924 | |||
| 1925 | ## Delete a column. | ||
| 1926 | sub columnDelete { | ||
| 1927 | my ($w, $first, $last) = @_; | ||
| 1928 | |||
| 1929 | $last ||= $first; #JWT:ADDED 20150429 | ||
| 1930 | for (my $i=$w->columnIndex($first); $i<=$w->columnIndex($last); $i++) { | ||
| 1931 | eval { $w->columnGet($i)->destroy; }; | ||
| 1932 | } | ||
| 1933 | @{$w->{'_columns'}} = map{Exists($_) ? $_ : ()} @{$w->{'_columns'}}; | ||
| 1934 | } | ||
| 1935 | |||
| 1936 | sub columnGet { | ||
| 1937 | my ($w, $from, $to) = @_; | ||
| 1938 | if (defined($to)) { | ||
| 1939 | $from= $w->columnIndex($from); | ||
| 1940 | $to = $w->columnIndex($to); | ||
| 1941 | return @{$w->{'_columns'}}[$from..$to]; | ||
| 1942 | } else { | ||
| 1943 | return $w->{'_columns'}->[$w->columnIndex($from)]; | ||
| 1944 | } | ||
| 1945 | } | ||
| 1946 | |||
| 1947 | sub columnHide { | ||
| 1948 | my ($w, $first, $last) = @_; | ||
| 1949 | $last = $first unless defined $last; | ||
| 1950 | |||
| 1951 | for (my $i=$w->columnIndex($first); $i<=$w->columnIndex($last); $i++) { | ||
| 1952 | eval { $w->columnGet($i)->packForget; }; | ||
| 1953 | } | ||
| 1954 | } | ||
| 1955 | |||
| 1956 | ## Converts a column index to a numeric index. $index might be a number, | ||
| 1957 | ## 'end' or a reference to a HMLColumn widget (see columnGet). Note that | ||
| 1958 | ## the index return by this method may not match up with it's current | ||
| 1959 | ## visual location due to columns being moved around | ||
| 1960 | |||
| 1961 | sub columnIndex { | ||
| 1962 | my ($w, $index, $after_end) = @_; | ||
| 1963 | |||
| 1964 | if ($index eq 'end') { | ||
| 1965 | if (defined $after_end) { | ||
| 1966 | return $#{$w->{'_columns'}} + 1; | ||
| 1967 | } else { | ||
| 1968 | return $#{$w->{'_columns'}}; | ||
| 1969 | } | ||
| 1970 | } | ||
| 1971 | |||
| 1972 | if (ref($index) eq "Tk::HMListbox::HMLColumn") { | ||
| 1973 | foreach (0..$#{$w->{'_columns'}}) { | ||
| 1974 | if ($index eq $w->{'_columns'}->[$_]) { | ||
| 1975 | return $_; | ||
| 1976 | } | ||
| 1977 | } | ||
| 1978 | } | ||
| 1979 | |||
| 1980 | if ($index =~ m/^\s*(\d+)\s*$/o) { | ||
| 1981 | return $1; | ||
| 1982 | } | ||
| 1983 | croak "Invalid column index: $index\n"; | ||
| 1984 | } | ||
| 1985 | |||
| 1986 | ## JWT:Bind "underlined" Alt-<letter> keys in column headers to sort function (as if header button pressed) to HMListbox widget (when it has focus) | ||
| 1987 | |||
| 1988 | sub _headerAltBindit { | ||
| 1989 | my ($x, $w, $c, $idx) = @_; | ||
| 1990 | |||
| 1991 | $w->_dragOrSort($c, 1); | ||
| 1992 | $x->break; | ||
| 1993 | } | ||
| 1994 | |||
| 1995 | ## JWT:Bind "underlined" Alt-<letter> keys in column headers to sort function (as if header button pressed) to all column HListbox widgets (when it has focus using JWT's hack) | ||
| 1996 | |||
| 1997 | sub _headerAltBinditAll { | ||
| 1998 | my ($x, $w) = @_; | ||
| 1999 | my $mykey = $x->XEvent->A; | ||
| 2000 | foreach my $c (@{$w->{'_columns'}}) { | ||
| 2001 | if (defined($c->{'sorthotkey'}) && $c->{'sorthotkey'} eq $mykey) { | ||
| 2002 | $w->_dragOrSort($c, 1); | ||
| 2003 | } | ||
| 2004 | } | ||
| 2005 | $x->break; | ||
| 2006 | } | ||
| 2007 | |||
| 2008 | ## Insert a column. $index should be a number or 'end'. | ||
| 2009 | |||
| 2010 | sub columnInsert { | ||
| 2011 | my ($w, $index, %args) = @_; | ||
| 2012 | |||
| 2013 | $index = $w->columnIndex($index,1); | ||
| 2014 | my %opts = (); | ||
| 2015 | |||
| 2016 | ## Copy these options from the megawidget. | ||
| 2017 | foreach (qw/-background -foreground -headerbackground -headerforeground -font -height | ||
| 2018 | -resizeable -selectbackground -selectforeground -disabledforeground | ||
| 2019 | -selectborderwidth -selectmode -separatorcolor | ||
| 2020 | -separatorwidth -sortable -itemtype -textwidth -reversearrow -state/) | ||
| 2021 | { | ||
| 2022 | $opts{$_} = $w->cget($_) if defined $w->cget($_); | ||
| 2023 | } | ||
| 2024 | if ($Tk::HListbox::VERSION >= 2.3) { | ||
| 2025 | foreach (qw/-tpady -tpadx -ipady -ipadx/) { | ||
| 2026 | $opts{$_} = $w->cget($_) if defined $w->cget($_); | ||
| 2027 | } | ||
| 2028 | } | ||
| 2029 | ## All options (and more) might be overridden by %args. | ||
| 2030 | map {$opts{$_} = $args{$_}} keys %args; | ||
| 2031 | |||
| 2032 | ## JWT:See if column header contains "~" indicating a "hot-character (next letter) to use as a keyboard binding for sorting the column using the keyboard!: | ||
| 2033 | my $hotchar; if (defined($opts{'-text'}) && $opts{'-text'} =~ s/^([^\~]*)\~(\w)/$1$2/o) { | ||
| 2034 | |||
| 2035 | $hotchar = $2; | ||
| 2036 | $opts{'-underline'} = length($1); #Cause the hotkey to be underlined in the column header button. | ||
| 2037 | } | ||
| 2038 | my $c = $w->Subwidget("pane")->HMLColumn(%opts, #Create the new column: | ||
| 2039 | -yscrollcommand => [ $w => '_yscrollCallback'], | ||
| 2040 | -configurecommand => [ $w => 'Callback', '-configurecommand', $w], | ||
| 2041 | -xscancommand => [ $w => 'xscan' ], | ||
| 2042 | -updatecommand => [ $w => '_selectionUpdate'] | ||
| 2043 | ); | ||
| 2044 | unless ($index || defined $w->{Configure}{'-jwtlistboxhack'} || $w->cget('-nocolumnfocus') == 1) { #JWT:If 1st column (now that we've created a listbox), see if we're using JWT's HACKED listbox: | ||
| 2045 | no warnings; | ||
| 2046 | eval { $c->Subwidget("listbox")->Tk::Listbox::CtrlUpDown(0); }; | ||
| 2047 | use warnings; | ||
| 2048 | $w->{Configure}{'-jwtlistboxhack'} = $@ ? 0 : 1; #eval succeeds if using JWT's hacked listbox! | ||
| 2049 | } | ||
| 2050 | $c->{'__HMListbox__'} = $w; #JWT:SAVE ME FOR FUTURE REFERENCES. | ||
| 2051 | ## RCS: Review this later - questionable implementation | ||
| 2052 | ## Fill the new column with empty values, making sure all columns have | ||
| 2053 | ## the same number of rows. | ||
| 2054 | unless (scalar(@{$w->{'_columns'}}) == 0) { | ||
| 2055 | foreach (1..$w->size) { | ||
| 2056 | $c->insert('end',''); | ||
| 2057 | } | ||
| 2058 | } | ||
| 2059 | $c->Subwidget("heading")->bind("<ButtonPress-1>", [ $w => '_dragOrSort', $c]); | ||
| 2060 | $c->Subwidget("heading")->configure(-compound => $w->{'-compound'}) if ($w->{'-compound'}); | ||
| 2061 | my @bindTags = $w->bindtags; | ||
| 2062 | if (defined($hotchar) && $hotchar =~ /\S/o) { #JWT:Set up Alt-<hotkey> bindings if we have a hotkey character defined: | ||
| 2063 | |||
| 2064 | $hotchar =~ tr/A-Z/a-z/; | ||
| 2065 | $c->{'sorthotkey'} = $hotchar; | ||
| 2066 | $w->bind($bindTags[1], "<Alt-$hotchar>", [\&_headerAltBindit, $w, $c, $index]); | ||
| 2067 | $c->Subwidget("listbox")->Tk::HListbox::bind("<Alt-Key>", [\&_headerAltBinditAll, $w, $c, $index]); | ||
| 2068 | } | ||
| 2069 | |||
| 2070 | my $carr = $w->{'_columns'}; | ||
| 2071 | splice(@$carr,$index,0,$c); | ||
| 2072 | |||
| 2073 | ## Update the selection to also include the new column. | ||
| 2074 | map {$w->selectionSet($_, $_)} $w->curselection | ||
| 2075 | if $w->curselection; | ||
| 2076 | |||
| 2077 | ## Copy all bindings that are created by calls to | ||
| 2078 | ## bindRows, bindColumns and/or bindSeparators. | ||
| 2079 | ## RCS: check this out, on the next pass | ||
| 2080 | foreach my $subwidget (qw/listbox heading separator/) { | ||
| 2081 | foreach (keys %{$w->{'_bindings'}->{$subwidget}}) { | ||
| 2082 | $c->Subwidget($subwidget)->Tk::bind($_, | ||
| 2083 | [ | ||
| 2084 | $w => '_bindCallback', | ||
| 2085 | $w->{'_bindings'}->{$subwidget}->{$_}, | ||
| 2086 | $index | ||
| 2087 | ] | ||
| 2088 | ); | ||
| 2089 | } | ||
| 2090 | } | ||
| 2091 | |||
| 2092 | if (Tk::Exists($w->{'_columns'}->[$index+1])) { | ||
| 2093 | $w->columnShow($index, -before=>$index+1); | ||
| 2094 | } else { | ||
| 2095 | $w->columnShow($index); | ||
| 2096 | } | ||
| 2097 | return $c; | ||
| 2098 | } | ||
| 2099 | |||
| 2100 | sub columnPack { | ||
| 2101 | my ($w, @packinfo) = @_; | ||
| 2102 | $w->columnHide(0,'end'); | ||
| 2103 | foreach (@packinfo) { | ||
| 2104 | my ($index, $width) = split /:/o; | ||
| 2105 | $w->columnShow ($index); | ||
| 2106 | if (defined($width) && $width =~ /^\d+$/o) { | ||
| 2107 | $w->columnGet($index)->setWidth($width) | ||
| 2108 | } | ||
| 2109 | } | ||
| 2110 | } | ||
| 2111 | |||
| 2112 | sub columnPackInfo { | ||
| 2113 | my ($w) = @_; | ||
| 2114 | |||
| 2115 | ## Widget needs to have an update call first, otherwise | ||
| 2116 | ## the method will not return anything if called prior to | ||
| 2117 | ## MainLoop - RCS | ||
| 2118 | |||
| 2119 | $w->update; | ||
| 2120 | map {$w->columnIndex($_) . ':' . $_->width} | ||
| 2121 | sort {$a->rootx <=> $b->rootx} | ||
| 2122 | map {$_->ismapped ? $_ : ()} @{$w->{'_columns'}}; | ||
| 2123 | } | ||
| 2124 | |||
| 2125 | sub columnShow { | ||
| 2126 | my ($w, $index, %args) = @_; | ||
| 2127 | |||
| 2128 | my $numericIndex = $w->columnIndex($index); | ||
| 2129 | my $c = $w->columnGet($index); | ||
| 2130 | my @packopts = ($numericIndex == $w->{Configure}{'-fillcolumn'}) | ||
| 2131 | ? (-anchor=>'w',-side=>'left',-fill=>'both',-expand=>1) | ||
| 2132 | : (-anchor=>'w',-side=>'left',-fill=>'both'); | ||
| 2133 | if (defined($args{'-before'})) { | ||
| 2134 | push (@packopts, '-before'=>$w->columnGet($args{'-before'})); | ||
| 2135 | } elsif (defined($args{'-after'})) { | ||
| 2136 | push (@packopts, '-after'=>$w->columnGet($args{'-after'})); | ||
| 2137 | } | ||
| 2138 | eval { $c->pack(@packopts); }; | ||
| 2139 | } | ||
| 2140 | |||
| 2141 | sub curselection { shift->_firstVisible->curselection(@_)} | ||
| 2142 | |||
| 2143 | sub delete { | ||
| 2144 | my $w = shift; | ||
| 2145 | foreach (@{$w->{'_columns'}}) { | ||
| 2146 | my $saved_width = $_->width; | ||
| 2147 | $_->delete(@_); | ||
| 2148 | if ($_->ismapped) { | ||
| 2149 | $_->setWidth($saved_width); | ||
| 2150 | } | ||
| 2151 | } | ||
| 2152 | $w->_yscrollCallback; | ||
| 2153 | } | ||
| 2154 | |||
| 2155 | sub get { | ||
| 2156 | my @result = (); | ||
| 2157 | my ($colnum,$rownum) = (0,0); | ||
| 2158 | |||
| 2159 | foreach (@{shift->{'_columns'}}) { | ||
| 2160 | my @coldata = $_->get(@_); | ||
| 2161 | $rownum = 0; | ||
| 2162 | map {$result[$rownum++][$colnum] = $_} @coldata; | ||
| 2163 | $colnum++; | ||
| 2164 | } | ||
| 2165 | @result; | ||
| 2166 | } | ||
| 2167 | |||
| 2168 | sub getRow { | ||
| 2169 | my @result = map {$_->get(@_)} @{shift->{'_columns'}}; | ||
| 2170 | if (wantarray) { | ||
| 2171 | @result; | ||
| 2172 | } else { | ||
| 2173 | $result[0]; | ||
| 2174 | } | ||
| 2175 | } | ||
| 2176 | |||
| 2177 | sub index { | ||
| 2178 | my $w = shift; | ||
| 2179 | |||
| 2180 | return undef unless (defined($_[0]) && defined($w->_firstVisible)); | ||
| 2181 | my $ret = ($w->{'-showcursoralways'} || ($w->{'_hasfocus'} && $w->focusCurrent) || $_[0] !~ /^active/o) | ||
| 2182 | ? $w->_firstVisible->index(@_) : $w->{'_lastactive'}; | ||
| 2183 | return $ret; | ||
| 2184 | } | ||
| 2185 | |||
| 2186 | sub insert { | ||
| 2187 | my ($w, $index, @data) = @_; | ||
| 2188 | my ($rownum, $colnum); | ||
| 2189 | my $rowcnt = $#data; | ||
| 2190 | |||
| 2191 | # Insert data into one column at a time, calling $listbox->insert | ||
| 2192 | # ONCE for each column. (The first version of this widget call insert | ||
| 2193 | # once for each row in each column). | ||
| 2194 | # | ||
| 2195 | foreach $colnum (0..$#{$w->{'_columns'}}) { | ||
| 2196 | my $c = $w->{'_columns'}->[$colnum]; | ||
| 2197 | |||
| 2198 | # The listbox might get resized after insert/delete, which is a | ||
| 2199 | # behaviour we don't like.... | ||
| 2200 | my $saved_width = $c->width; | ||
| 2201 | |||
| 2202 | my @coldata = (); | ||
| 2203 | |||
| 2204 | foreach (0..$#data) { | ||
| 2205 | if (defined($data[$_][$colnum])) { | ||
| 2206 | push @coldata, $data[$_][$colnum]; | ||
| 2207 | } else { | ||
| 2208 | push @coldata, ''; | ||
| 2209 | } | ||
| 2210 | } | ||
| 2211 | $c->insert($index,@coldata); | ||
| 2212 | |||
| 2213 | if ($c->ismapped) { | ||
| 2214 | # Restore saved width. | ||
| 2215 | $c->setWidth($saved_width); | ||
| 2216 | } | ||
| 2217 | } | ||
| 2218 | $w->_yscrollCallback; | ||
| 2219 | } | ||
| 2220 | |||
| 2221 | ## These methods all delegate to the first visible column's | ||
| 2222 | ## HListbox. Refer to HListbox docs and description for _firstVisible | ||
| 2223 | |||
| 2224 | sub nearest { shift->_firstVisible->nearest(@_)} | ||
| 2225 | sub see { shift->_firstVisible->see(@_)} | ||
| 2226 | sub selectionAnchor { shift->_firstVisible->selectionAnchor(@_)} | ||
| 2227 | sub selectionClear { shift->_firstVisible->selectionClear(@_)} | ||
| 2228 | sub selectionIncludes { shift->_firstVisible->selectionIncludes(@_)} | ||
| 2229 | sub selectionSet { shift->_firstVisible->selectionSet(@_)} | ||
| 2230 | sub SpaceSelect { shift->_firstVisible->SpaceSelect(@_)} | ||
| 2231 | sub CtrlPriorNext { shift->_firstVisible->CtrlPriorNext(@_)} | ||
| 2232 | sub size { shift->_firstVisible->size(@_)} | ||
| 2233 | |||
| 2234 | sub sort { | ||
| 2235 | my ($w, $descending, @indexes) = @_; | ||
| 2236 | |||
| 2237 | #my @l = @{$w->{'_sortcolumns'}}; #JWT:CHGD. TO NEXT 20150429 TO PREVENT STRAY ARROWS: | ||
| 2238 | my @l = (0..$#{$w->{'_columns'}}); | ||
| 2239 | foreach my $i (@l) { | ||
| 2240 | $w->{'_columns'}->[$i]->Subwidget("heading")->configure(-bitmap => 'noarrow'); | ||
| 2241 | } | ||
| 2242 | |||
| 2243 | # Hack to avoid problem with older Tk versions which do not support | ||
| 2244 | # the -recurse=>1 option. | ||
| 2245 | $w->Busy; # This works always (but not very good...) | ||
| 2246 | Tk::catch {$w->Busy(-recurse=>1)};# This works on newer Tk versions, | ||
| 2247 | # harmless on old versions. | ||
| 2248 | |||
| 2249 | @indexes = (0..$#{$w->{'_columns'}}) unless @indexes; | ||
| 2250 | |||
| 2251 | # Convert all indexes to integers. | ||
| 2252 | map {$_=$w->columnIndex($_)} @indexes; | ||
| 2253 | # This works on Solaris, but not on Linux??? | ||
| 2254 | # Store the -comparecommand for each row in a local array. In the sort, | ||
| 2255 | # the store command is called directly in stead of via the HMLColumn | ||
| 2256 | # subwidget. This saves a lot of callbacks and function calls. | ||
| 2257 | # | ||
| 2258 | # my @cmp_subs = map {$_->cget(-comparecommand)} @{$w->{'_columns'}}; | ||
| 2259 | |||
| 2260 | # If sort order is not defined | ||
| 2261 | unless (defined $descending) { | ||
| 2262 | if ($#indexes == 0 && | ||
| 2263 | $w->{'_sortcol'} == $indexes[0] && | ||
| 2264 | $w->{'_sort_descending'} == 0) | ||
| 2265 | { | ||
| 2266 | # Already sorted on this column, reverse sort order. | ||
| 2267 | $descending = 1; | ||
| 2268 | } else { | ||
| 2269 | $descending = 0; | ||
| 2270 | } | ||
| 2271 | } | ||
| 2272 | |||
| 2273 | # To retain the selection after the sort we have to save information | ||
| 2274 | # about the current selection before the sort. Adds a dummy column | ||
| 2275 | # to the two dimensional data array, this last column will be true | ||
| 2276 | # for all rows that are currently selected. | ||
| 2277 | my $dummy_column = scalar(@{$w->{'_columns'}}); | ||
| 2278 | |||
| 2279 | my $wasActive = $w->index('active'); | ||
| 2280 | my @data = $w->get(0,'end'); | ||
| 2281 | foreach ($w->curselection) { | ||
| 2282 | $data[$_]->[$dummy_column] = 1; # Selected... | ||
| 2283 | } | ||
| 2284 | $data[$wasActive]->[$dummy_column] += 2 if (defined($wasActive) && $wasActive >= 0); | ||
| 2285 | |||
| 2286 | @data = sort { | ||
| 2287 | local $^W = 0; | ||
| 2288 | foreach (@indexes) { | ||
| 2289 | my $res = do { | ||
| 2290 | if ($descending) { | ||
| 2291 | # Call via cmp_subs works fine on Solaris, but no | ||
| 2292 | # on Linux. The column->compare method is much slower... | ||
| 2293 | # | ||
| 2294 | # &{$cmp_subs[$_]} ($b->[$_],$a->[$_]); | ||
| 2295 | $w->{'_columns'}->[$_]->compare($b->[$_],$a->[$_]); | ||
| 2296 | } else { | ||
| 2297 | # &{$cmp_subs[$_]} ($a->[$_],$b->[$_]); | ||
| 2298 | $w->{'_columns'}->[$_]->compare($a->[$_],$b->[$_]); | ||
| 2299 | } | ||
| 2300 | }; | ||
| 2301 | return $res if $res; | ||
| 2302 | } | ||
| 2303 | return 0; | ||
| 2304 | } @data; | ||
| 2305 | |||
| 2306 | # Replace data with the new, sorted list. | ||
| 2307 | $w->delete(0,'end'); | ||
| 2308 | $w->insert('end',@data); | ||
| 2309 | |||
| 2310 | my @new_selection = (); | ||
| 2311 | foreach (0..$#data) { | ||
| 2312 | if (defined $data[$_]->[$dummy_column]) { | ||
| 2313 | $w->selectionSet($_,$_) if ($data[$_]->[$dummy_column] % 2); | ||
| 2314 | $w->activate($_) if ($data[$_]->[$dummy_column] > 1); | ||
| 2315 | } | ||
| 2316 | } | ||
| 2317 | |||
| 2318 | $w->{'_sortcol'} = $indexes[0]; | ||
| 2319 | @{$w->{'_sortcolumns'}} = @indexes; | ||
| 2320 | $w->{'_sort_descending'} = $descending; | ||
| 2321 | |||
| 2322 | $w->Unbusy; #(-recurse=>1); | ||
| 2323 | if ($w->{'-showallsortcolumns'}) { | ||
| 2324 | for (my $i=0;$i<=$#indexes;$i++) { #UNCOMMENT TO SHOW ALL SORTED COLUMNS: | ||
| 2325 | if ($w->{'_columns'}->[$indexes[$i]]->cget('-reversearrow') == 1) { | ||
| 2326 | $w->{'_columns'}->[$indexes[$i]]->Subwidget("heading")->configure(-bitmap => ($w->{'_sort_descending'} ? 'uparrow' : 'downarrow')); | ||
| 2327 | } else { | ||
| 2328 | $w->{'_columns'}->[$indexes[$i]]->Subwidget("heading")->configure(-bitmap => ($w->{'_sort_descending'} ? 'downarrow' : 'uparrow')); | ||
| 2329 | } | ||
| 2330 | } | ||
| 2331 | } else { | ||
| 2332 | if ($w->{'_columns'}->[$indexes[0]]->cget('-reversearrow') == 1) { | ||
| 2333 | $w->{'_columns'}->[$indexes[0]]->Subwidget("heading")->configure(-bitmap => ($w->{'_sort_descending'} ? 'uparrow' : 'downarrow')); | ||
| 2334 | } else { | ||
| 2335 | $w->{'_columns'}->[$indexes[0]]->Subwidget("heading")->configure(-bitmap => ($w->{'_sort_descending'} ? 'downarrow' : 'uparrow')); | ||
| 2336 | } | ||
| 2337 | } | ||
| 2338 | } | ||
| 2339 | |||
| 2340 | sub state { | ||
| 2341 | my ($w, $val) = @_; | ||
| 2342 | |||
| 2343 | return $w->{Configure}{'-state'} || undef unless (defined($val) && $val); | ||
| 2344 | return if (defined($w->{'_prevstate'}) && $val eq $w->{'_prevstate'}); #DON'T DO TWICE IN A ROW! | ||
| 2345 | |||
| 2346 | $w->{'_statechg'} = 1; | ||
| 2347 | if ($val =~ /d/o) { #WE'RE DISABLING (SAVE CURRENT ENABLED STATUS STUFF, THEN DISABLE USER-INTERACTION): | ||
| 2348 | $w->{Configure}{'-state'} = 'normal'; | ||
| 2349 | $w->{'_saveactive'} = $w->index('active') || 0; | ||
| 2350 | $w->{'_foreground'} = $w->cget('-foreground'); #SAVE CURRENT (ENABLED) FG COLOR! | ||
| 2351 | $w->{Configure}{'-state'} = $val; | ||
| 2352 | foreach my $i (0..$#{$w->{'_columns'}}) { | ||
| 2353 | $w->{'_columns'}->[$i]->state($val); | ||
| 2354 | } | ||
| 2355 | $w->activate($w->{'_saveactive'}) unless ($w->{'_hasfocus'}); | ||
| 2356 | #? $w->focusNext if ($w->{'_hasfocus'}); #MOVE FOCUS OFF WIDGET IF IT HAS IT. | ||
| 2357 | } elsif ($w->{'_prevstate'}) { #WE'RE ENABLING (RESTORE PREV. ENABLED STUFF AND REALLOW USER-INTERACTION): | ||
| 2358 | my $fg = $w->{'_foreground'}; | ||
| 2359 | $w->{Configure}{'-state'} = $val; | ||
| 2360 | foreach my $i (0..$#{$w->{'_columns'}}) { | ||
| 2361 | $w->{'_columns'}->[$i]->state($val); | ||
| 2362 | } | ||
| 2363 | if (defined $w->_firstVisible) { #RESTORE SELECTED LIST AND ACTIVE CURSOR: | ||
| 2364 | my @selected = $w->_firstVisible->curselection; | ||
| 2365 | $w->selectionSet(shift @selected) while (@selected); | ||
| 2366 | $w->activate($w->{'_saveactive'}); | ||
| 2367 | } | ||
| 2368 | } | ||
| 2369 | $w->{'_prevstate'} = $w->{Configure}{'-state'}; | ||
| 2370 | $w->{'_statechg'} = 0; | ||
| 2371 | } | ||
| 2372 | |||
| 2373 | # Implements horizontal scanning. | ||
| 2374 | sub xscan { | ||
| 2375 | my ($w, $type, $x) = @_; | ||
| 2376 | |||
| 2377 | if ($type eq 'dragto') { | ||
| 2378 | my $dist = $w->{'_scanmark_x'} - $w->pointerx; | ||
| 2379 | |||
| 2380 | # Looks like there is a bug in Pane: If no -xscrollcommand | ||
| 2381 | # is defined, xview() fails. This is fixed by this hack: | ||
| 2382 | # | ||
| 2383 | my $p = $w->Subwidget("pane"); | ||
| 2384 | unless (defined ($p->cget(-xscrollcommand))) { | ||
| 2385 | $p->configure(-xscrollcommand => sub {}); | ||
| 2386 | } | ||
| 2387 | $p->xview('scroll',$dist,'units'); | ||
| 2388 | } | ||
| 2389 | $w->{'_scanmark_x'} = $w->pointerx; | ||
| 2390 | } | ||
| 2391 | |||
| 2392 | sub xview { shift->Subwidget("pane")->xview(@_) } | ||
| 2393 | sub yview { shift->_firstVisible->yview(@_)} | ||
| 2394 | 1; | ||
| 2395 | |||
| 2396 | __END__ |