Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to determine if nothing is selected in a listbox

by simonz (Sexton)
on Nov 30, 2013 at 07:56 UTC ( #1065039=perlquestion: print w/ replies, xml ) Need Help??
simonz has asked for the wisdom of the Perl Monks concerning the following question:

Hi ,
The Tk::Listbox behaves in way so that even if you click on the empty bottom area, the last element in the list gets selected. How can I chanhe this so that if I explicitly click on a element then only it will get selected otherwise not.
In the following code $selectedIndex is coming as 4 if I click on the empty bottom area of the Listbox. By checking the $selectIndex value I want to know whether I have clicked on an element or on the empty area. How can I do that ?

Also according to CPAN :

$listbox->nearest(y) Given a y-coordinate within the listbox window, this command returns the index of the (visible) listbox element nearest to that y-coordinate.

Please help me how can I then achieve my objective ?

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/); $listBox->bind('<Button-1>', sub { my @elements = $listBox->curselection ; my $rooty = $listBox->rooty; my $y = $Tk::event->Y - $rooty; my $selectedIndex = $listBox->nearest( $y ); my $selectedElement = $elements[$selectedIndex]; print "rooty $rooty \n"; print "elements @elements |selectedIndex $selectedIndex |selectedEleme +nt $selectedElement \n"; } ); MainLoop;

Comment on How to determine if nothing is selected in a listbox
Download Code
Re: How to determine if nothing is selected in a listbox (only select actually clicked element)
by Anonymous Monk on Nov 30, 2013 at 08:03 UTC

    How can I chanhe this so that if I explicitly click on a element then only it will get selected otherwise not.

    UTSL, examine the bindings, there are a lot of them, then change some of them ... sub UpDown

      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 }

        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
Node Status?
node history
Node Type: perlquestion [id://1065039]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (15)
As of 2014-09-02 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (25 votes), past polls