Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Tk::Tree double click on +

by bbfu (Curate)
on Apr 29, 2004 at 13:58 UTC ( #349130=note: print w/ replies, xml ) Need Help??


in reply to Tk::Tree double click on +

You can override the default double-click binding with your own routine that checks to see if the item double-clicked on is the indicator and, if so, mutates the event into an open/close. Alternatively, you could just have it ignore double-click events on the indicator.

Note, however, that to do so you must replace the binding for the entire class Tk:Tree, so it will affect every Tree widget (and possibly every widget derived from a Tk::Tree) in that window. (You can't bind to the particular Tree instance, because then both callbacks are called, and yours is called last so you can't even use Tk->break to stop propagation, since it's already happened.)

#!/usr/bin/winperl use warnings; use strict; use Tk; use Tk::Tree; my $mw = MainWindow->new(); my $tr = $mw->Tree( -command => sub { print "Tree::command\n" }, )->pack(); $tr->add($_, -text => $_) for qw(Foo Bar Quux); $tr->addchild('Foo', -text => 'Zod'); $tr->autosetmode(); # Replace normal double-click handler with our special routine { my $old = $mw->bind('Tk::Tree', '<Double-ButtonPress-1>'); $mw->bind('Tk::Tree', '<Double-ButtonPress-1>', [ \&invoke, Ev('x'), Ev('y'), $old, ]); } MainLoop; sub invoke { my ($tree, $x, $y, $old) = @_; # Get info about what item was clicked on my ($entry, $subitem) = $tree->info('item', $x, $y); if(defined($subitem) and $subitem eq 'indicator') { #return; # ignore, if you want # Indicator, so just open/close the entry $tree->IndicatorCmd($entry, '<Activate>'); } else { # Not indicator, so propagate #$tree->Double1(); # Don't do this $old->Call($tree); # Instead, use old callback } }

Update: Changed the manual call to Tk::HList::Double1 to use the callback instead, to avoid breaking encapsulation and to be more forward-compatible.

bbfu
Black flowers blossom
Fearless on my breath


Comment on Re: Tk::Tree double click on +
Download Code
Re: Re: Tk::Tree double click on +
by gri6507 (Deacon) on Apr 29, 2004 at 17:53 UTC
    Thank you! This is exactly the behavior I wanted. But I do have a question: where is method Double1() defined? It's not part of Tree or HList or Widget.

    Also, a completely unrelated question, I can't seem to figure out how to unhighlight an entry in a tree! (in other words, user clicks on a leaf/branch in a tree, then user clicks elsewhere off the tree; i want to deselect the selected item)

      But I do have a question: where is method Double1() defined?

      You have to look into the source code for this one (either in Tk/HList.pm or Tk/Tree.pm).

      I can't seem to figure out how to unhighlight an entry in a tree

      To deselect an hlist or tree item programmatically, you have to use the selectionClear or selectionSet methods (documented in Tk::HList). This could be done in response to a FocusOut or Leave event.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://349130]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2014-07-25 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (171 votes), past polls