Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

TK/HList and focus

by andyok (Novice)
on Nov 26, 2021 at 16:18 UTC ( #11139147=perlquestion: print w/replies, xml ) Need Help??

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?

Thank you very much for your time.

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;

Replies are listed 'Best First'.
Re: TK/HList and focus
by choroba (Archbishop) on Nov 26, 2021 at 17:18 UTC
    That's because the label or the button steal the focus from the HList. You can prevent it with
    $_->bind('<FocusIn>', sub { $hlist->focus }) for $label, $button;

    but it might interfere with how sorting the columns works which you haven't showed.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      That's brilliant. Thank you very much. Works a treat.

      Andrew

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11139147]
Approved by hippo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2022-01-27 10:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (70 votes). Check out past polls.

    Notices?