andyok has asked for the wisdom of the Perl Monks concerning the following question:
I have trouble with focus and a HList.
I wanted my HList to have clickable headers:
- The intention being that the user could click on a header to indicate that the list should be sorted by that column
- I don't think HList natively supports clickable headers
- So I thought I'd use the -itemtype=>"window" feature
The code snippet attached at the end is the simplest test-case I could come up with.
You might think "Why would anyone have a GUI like this" ... but this code is purely to demonstrate the problem.
With the script running, and with the window made nice'n'wide, to demonstrate the problem:
- Start with your mouse hovering over Button1 and then move the mouse left and then vertically down through the ABC header and down into the HList ... the HList focus highlight rectangle appears (there is no need to click a HList entry).
- Return to Button1 and then move the mouse left and then vertically down through the DEF label and down into the HList ... the HList focus highlight rectangle no longer appears
- Return to Button1 and then move the mouse left and then vertically down through the GHI button and down into the HList ... again, the HList focus highlight rectangle no longer appears.
Isn't it curious that the highlight rectangle doesn't appear when in the HList entries when the mouse transitions through the Label and button widgets?
You may well be asking, "why is this important" ... well because, in my full script (not in this example) I have a keyboard binding for the Escape-key to unselect all HList entries ... but this only works when the HList has the keyboard focus ... and the keyboard focus seems to be very dependent on the route the mouse took to get to the HList.
Questions:
- Can anyone else reproduce this?
- As a side issue, why is the label DEF not centred (centered)?
- Any ideas as to how to get the HList focus when traversing through widgets as headers?
- Is there a better way to get clickable headers?
Andrew
#!/usr/bin/perl use Tk; use Tk::HList; my $mw = MainWindow->new(); $b1 = $mw->Button(-text=>"Button1")->pack(); $b2 = $mw->Button(-text=>"Button2")->pack(); $hlist = $mw->HList( -selectmode => 'single', -columns=>3, -background +=>"Wheat", -header=>1)->pack(-fill=>'both',-expand=>1); $hlist -> focusFollowsMouse(); $hlist -> headerCreate(0, -text=>"ABC"); $label = $hlist->Label(-text=>"DEF", -pady=>1); $hlist -> headerCreate(1, -itemtype=>"window", -window=>$label); $button = $hlist->Button(-text=>"GHI"); $hlist -> headerCreate(2, -itemtype=>"window", -window=>$button); for (1..9) { $hlist->add($_); $hlist->itemCreate($_, 0, -text=>"$_"x5); $hlist->itemCreate($_, 1, -text=>"$_"x10); } MainLoop;