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