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

Re^2: How to determine if nothing is selected in a listbox (only select actually clicked element) with bbox

by Anonymous Monk
on Nov 30, 2013 at 10:13 UTC ( [id://1065044]=note: print w/replies, xml ) Need Help??


in reply to Re: How to determine if nothing is selected in a listbox (only select actually clicked element)
in thread How to determine if nothing is selected in a listbox

here is monkeypatching proof of concept, use bbox to get coordinates of the listitem, math, compare to mouse coordinates
#!/usr/bin/perl -- $Devel::Trace::TRACE = 0; ## off use strict; use warnings; use Tk; my $mw = MainWindow ->new ; my $listFrame = $mw->Frame->pack; my $listBox = $listFrame->Scrolled( 'Listbox', -scrollbars => 'osoe', -selectmode => 'extended', -background => 'white', -height => 10, -width => 10, -selectborderwidth => 0, -relief => 'flat', )->pack; $listBox->insert('end', qw/red yellow green blue grey/); @ARGV and eval { require Tk::WidgetDump; $mw->WidgetDump; }; MainLoop; exit 0; # BeginSelect -- # # This procedure is typically invoked on button-1 presses. It begins # the process of making a selection in the listbox. Its exact behavior # depends on the selection mode currently in effect for the listbox; # see the Motif documentation for details. # # Arguments: # w - The listbox widget. # el - The element for the selection operation (typically the # one under the pointer). Must be in numerical form. sub Tk::Listbox::BeginSelect { package Tk::Listbox; use vars qw/ @Selection $Prev /; $Devel::Trace::TRACE = 1; ## on my $w = shift; my $el = shift; if ( $w->cget('-selectmode') eq 'multiple' ) { if ( $w->selectionIncludes($el) ) { $w->selectionClear($el); } else { $w->selectionSet($el); } } else { ## fix it here my ( $iltx, $ilty, $iw, $ih ) = $w->bbox($el); my $rooty = $listBox->rooty; my $evY = $Tk::event->Y; my $ylt = $rooty + $ilty + $ih; print "$ylt < $evY\n"; if ( $ylt < $evY ) { ### clear the board when clicking off the end ### no selection ### anchor $w->selectionClear( 0, 'end' ) } else { ## the original logic $w->selectionClear( 0, 'end' ); $w->selectionSet($el); $w->selectionAnchor($el); } @Selection = (); $Prev = $el; } $w->focus if ( $w->cget('-takefocus') ); $w->eventGenerate("<<ListboxSelect>>"); $Devel::Trace::TRACE = 0; ## off }
  • Comment on Re^2: How to determine if nothing is selected in a listbox (only select actually clicked element) with bbox
  • Download Code

Replies are listed 'Best First'.
Re^3: How to determine if nothing is selected in a listbox (only select actually clicked element) with bbox
by simonz (Sexton) on Dec 04, 2013 at 08:42 UTC

    Thank you! This is very helpful.
    Is there any such function for the Button-3 event ?
    Lets say I want a menu to appear only when I do a right click on any of the listitem and nothing to get selected if I right click on an empty area.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found