# Copyright (c) 1999 Greg London. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
# code for bindings taken from Listbox.pm
# comments specifying method functionality taken from
# "Perl/Tk Pocket Reference" by Stephen Lidie.
#######################################################################
# this module uses a text module as its base class to create a list box.
# this will allow list box functionality to also have all the functionality
# note that most methods use an element number to indicate which
# element in the list to work on.
# the exception to this is the tag and mark methods which
# are dual natured. These methods may accept either the
# normal element number, or they will also take a element.char index,
# which would be useful for applying tags to part of a line in the list.
#######################################################################
$VERSION = '3.002'; # $Id: //depot/Tk8/TextList/TextList.pm#2 $
use Tk::Reindex qw(Tk::ROText ReindexedROText);
use base
qw(Tk::Derived Tk::ReindexedROText );
use base
qw(Tk::ReindexedROText);
Construct Tk
::Widget
'TextList';
#######################################################################
# the following line causes Populate to get called
# @ISA = qw(Tk::Derived ... );
#######################################################################
my $option=delete $args->{'-selectmode'};
$w->SUPER::Populate
($args);
$w->ConfigSpecs( -selectmode
=> ['PASSIVE','selectMode','SelectMode','browse'] );
$w->ConfigSpecs( -takefocus
=> ['PASSIVE','takeFocus','TakeFocus','browse'] );
#######################################################################
#######################################################################
# Standard Motif bindings:
$mw->bind($class,'<1>',['BeginSelect',Ev
('index',Ev
('@'))]);
$mw->bind($class,'<B1-Motion>',['Motion',Ev
('index',Ev
('@'))]);
$mw->bind($class,'<ButtonRelease-1>','ButtonRelease_1');
$mw->bind($class,'<Shift-1>',['BeginExtend',Ev
('index',Ev
('@'))]);
$mw->bind($class,'<Control-1>',['BeginToggle',Ev
('index',Ev
('@'))]);
$mw->bind($class,'<B1-Leave>',['AutoScan',Ev
('x'),Ev
('y')]);
$mw->bind($class,'<B1-Enter>','CancelRepeat');
$mw->bind($class,'<Up>',['UpDown',-1]);
$mw->bind($class,'<Shift-Up>',['ExtendUpDown',-1]);
$mw->bind($class,'<Down>',['UpDown',1]);
$mw->bind($class,'<Shift-Down>',['ExtendUpDown',1]);
$mw->XscrollBind($class);
$mw->PriorNextBind($class);
$mw->bind($class,'<Control-Home>','Cntrl_Home');
$mw->bind($class,'<Shift-Control-Home>',['DataExtend',0]);
$mw->bind($class,'<Control-End>','Cntrl_End');
$mw->bind($class,'<Shift-Control-End>',['DataExtend','end']);
$class->clipboardOperations($mw,'Copy');
$mw->bind($class,'<space>',['BeginSelect',Ev
('index','active')]);
$mw->bind($class,'<Select>',['BeginSelect',Ev
('index','active')]);
$mw->bind($class,'<Control-Shift-space>',['BeginExtend',Ev
('index','active')]);
$mw->bind($class,'<Shift-Select>',['BeginExtend',Ev
('index','active')]);
$mw->bind($class,'<Escape>','Cancel');
$mw->bind($class,'<Control-slash>','SelectAll');
$mw->bind($class,'<Control-backslash>','Cntrl_backslash');
# Additional Tk bindings that aren't part of the Motif look and feel:
$mw->bind($class,'<2>',['scan','mark',Ev
('x'),Ev
('y')]);
$mw->bind($class,'<B2-Motion>',['scan','dragto',Ev
('x'),Ev
('y')]);
$mw->bind($class,'<FocusIn>' , ['tagConfigure','_ACTIVE_TAG', -underline
=>1]);
$mw->bind($class,'<FocusOut>', ['tagConfigure','_ACTIVE_TAG', -underline
=>0]);
#######################################################################
# set the active element to index
# "active" is a text "mark" which underlines the marked text.
#######################################################################
$element=$w->index($element).'.0';
$w->SUPER::tagRemove
('_ACTIVE_TAG', '1.0','end');
$w->SUPER::tagAdd
('_ACTIVE_TAG',
$element.' linestart', $element.' lineend');
$w->SUPER::markSet
('active', $element);
#######################################################################
# bbox returns a list (x,y,width,height) giving an approximate
# bounding box of character given by index
#######################################################################
$element=$w->index($element).'.0' unless ($element=~/./);
return $w->SUPER::bbox
($element);
#######################################################################
# returns a list of indices of all elements currently selected
#######################################################################
my @ranges = $w->SUPER::tagRanges
('sel');
my ($first,$firstcol) = split(/\./,shift(@ranges));
my ($last,$lastcol) = split(/\./,shift(@ranges));
#########################################################################
# if previous selection ended on the same line that this selection starts,
# then fiddle the numbers so that this line number isnt included twice.
#########################################################################
if (defined($selection_list[-1]) and ($first == $selection_list[-1]))
$first++; # count this selection starting from the next line.
#########################################################################
# if incrementing $first causes it to be greater than $last,
# else add (first .. last) to list
#########################################################################
push(@selection_list, $first .. $last);
#######################################################################
# deletes range of elements from element1 to element2
#######################################################################
my ($w, $element1, $element2)=@_;
$element1=$w->index($element1);
$element2=$element1 unless(defined($element2));
$element2=$w->index($element2);
$w->SUPER::delete($element1.'.0' , $element2.'.0 lineend');
#######################################################################
# deletes range of characters from index1 to index2
# index is line.char notation.
#######################################################################
my ($w, $index1, $index2)=@_;
$index1=$w->index($index1);
$index2=$index1.' +1c' unless(defined($index2));
$index2=$w->index($index2);
$w->SUPER::delete($index1, $index2);
#######################################################################
# returns as a list contents of elements from $element1 to $element2
#######################################################################
my ($w, $element1, $element2)=@_;
$element1=$w->index($element1);
$element2=$element1 unless(defined($element2));
$element2=$w->index($element2);
for(my $i=$element1; $i<=$element2; $i++)
push(@getlist, $w->SUPER::get
($i.'.0 linestart', $i.'.0 lineend'));
#######################################################################
# return text between index1 and index2 which are line.char notation.
# return value is a single string. index2 defaults to index1+1c
# index is line.char notation.
######################################################################
return $w->SUPER::get
(@_);
#######################################################################
# returns index in number notation
# this method returns an element number, ie the 5th element.
#######################################################################
return undef unless(defined($element));
$element .= '.0' unless $element=~/\D/;
$element = $w->SUPER::index($element);
my($line,$col)=split(/\./,$element);
#######################################################################
# returns index in line.char notation
# this method returns an index specific to a character within an element
#######################################################################
return $w->SUPER::index(@_);
#######################################################################
# inserts specified elements just before element at index
#######################################################################
$element=$w->index($element);
$w->SUPER::insert
($element++.'.0', $item);
#######################################################################
# inserts string just before character at index.
# index is line.char notation.
#######################################################################
#######################################################################
# returns index of element nearest to y-coordinate
#######################################################################
#######################################################################
# Sets the selection anchor to element at index
#######################################################################
$element=$w->index($element);
$w->SUPER::markSet
('anchor', $element.'.0');
#######################################################################
# deselects elements between index1 and index2, inclusive
#######################################################################
my ($w, $element1, $element2)=@_;
$element1=$w->index($element1);
$element2=$element1 unless(defined($element2));
$element2=$w->index($element2);
$w->SUPER::tagRemove
('sel', $element1.'.0', $element2.'.0 lineend +1c');
#######################################################################
# returns 1 if element at index is selected, 0 otherwise.
#######################################################################
$element=$w->index($element);
my @list = $w->curselection;
if ($line == $element) {return 1;}
#######################################################################
# adds all elements between element1 and element2 inclusive to selection
#######################################################################
my ($w, $element1, $element2)=@_;
$element1=$w->index($element1);
$element2=$element1 unless(defined($element2));
$element2=$w->index($element2);
$w->SUPER::tagAdd
('sel', $element1.'.0', $element2.'.0 lineend +1c');
#######################################################################
# for ->selection(option,args) calling convention
#######################################################################
# my ($w,$sub)=(shift,"selection".ucfirst(shift));
# # can't use $w->$sub, since it might call overridden method-- bleh
#######################################################################
# adjusts the view in window so element at index is completely visible
#######################################################################
$element=$w->index($element);
$w->SUPER::see
($element.'.0');
#######################################################################
# returns number of elements in listbox
#######################################################################
my $element = $w->index('end');
# theres a weird thing with the 'end' mark sometimes being on a line
# with text, and sometimes being on a line all by itself
my ($text) = $w->get($element);
#######################################################################
# add a tag based on element numbers
#######################################################################
my ($w, $tagName, $element1, $element2)=@_;
$element1=$w->index($element1);
$element2=$element1.' lineend' unless(defined($element2));
$element2=$w->index($element2);
$element2.='.0 lineend +1c';
$w->SUPER::tagAdd
($tagName, $element1, $element2);
#######################################################################
# add a tag based on line.char indexes
#######################################################################
#######################################################################
# remove a tag based on element numbers
#######################################################################
my ($w, $tagName, $element1, $element2)=@_;
$element1=$w->index($element1);
$element2=$element1.' lineend' unless(defined($element2));
$element2=$w->index($element2);
$element2.='.0 lineend +1c';
$w->SUPER::tagRemove
('sel', $element1, $element2);
#######################################################################
# remove a tag based on line.char indexes
#######################################################################
$w->SUPER::tagRemove
(@_);
#######################################################################
# perform tagNextRange based on element numbers
#######################################################################
my ($w, $tagName, $element1, $element2)=@_;
$element1=$w->index($element1);
$element2=$element1 unless(defined($element2));
$element2=$w->index($element2);
$element2.='.0 lineend +1c';
my $index = $w->SUPER::tagNextrange
('sel', $element1, $element2);
my ($line,$col)=split(/\./,$index);
#######################################################################
# perform tagNextRange based on line.char indexes
#######################################################################
$w->SUPER::tagNextrange
(@_);
#######################################################################
# perform tagPrevRange based on element numbers
#######################################################################
my ($w, $tagName, $element1, $element2)=@_;
$element1=$w->index($element1);
$element2=$element1 unless(defined($element2));
$element2=$w->index($element2);
$element2.='.0 lineend +1c';
my $index = $w->SUPER::tagPrevrange
('sel', $element1, $element2);
my ($line,$col)=split(/\./,$index);
#######################################################################
# perform tagPrevRange based on line.char indexes
#######################################################################
$w->SUPER::tagPrevrange
(@_);
#######################################################################
# perform markSet based on element numbers
#######################################################################
my ($w,$mark,$element1)=@_;
$element1=$w->index($element1);
$w->SUPER::markSet
($element1,$mark);
#######################################################################
# perform markSet based on line.char indexes
#######################################################################
#######################################################################
# perform markNext based on element numbers
#######################################################################
$element1=$w->index($element1);
return $w->SUPER::markNext
($element1);
#######################################################################
# perform markNext based on line.char indexes
#######################################################################
#######################################################################
# perform markPrevious based on element numbers
#######################################################################
$element1=$w->index($element1);
return $w->SUPER::markPrevious
($element1);
#######################################################################
# perform markPrevious based on line.char indexes
#######################################################################
$w->SUPER::markPrevious
(@_);
$w->selectionClear(0,'end');
$w->selectionClear(0,'end');
if ($w->cget('-selectmode') ne 'browse')
$w->selectionClear(0,'end');
# 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.
# w - The listbox widget.
# el - The element for the selection operation (typically the
# one under the pointer). Must be in numerical form.
if ($w->cget('-selectmode') eq 'multiple')
if ($w->selectionIncludes($el))
$w->selectionClear(0,'end');
$w->selectionAnchor($el);
$w->{'SELECTION_LIST_REF'} = \
@list;
$w->{'PREVIOUS_ELEMENT'} = $el
$w->focus if ($w->cget('-takefocus'));
# This procedure is called to process mouse motion events while
# button 1 is down. It may move or extend the selection, depending
# on the listbox's selection mode.
# w - The listbox widget.
# el - The element under the pointer (must be a number).
if (defined($w->{'PREVIOUS_ELEMENT'}) && $el == $w->{'PREVIOUS_ELEMENT'})
# if no selections, select current
$w->selectionAnchor($el);
$w->{'PREVIOUS_ELEMENT'}=$el;
my $anchor = $w->index('anchor');
my $mode = $w->cget('-selectmode');
$w->selectionClear(0,'end');
$w->{'PREVIOUS_ELEMENT'} = $el;
elsif ($mode eq 'extended')
my $i = $w->{'PREVIOUS_ELEMENT'};
if ($w->selectionIncludes('anchor'))
$w->selectionClear($i,$el);
$w->selectionSet('anchor',$el)
$w->selectionClear($i,$el);
$w->selectionClear('anchor',$el)
while ($i < $el && $i < $anchor)
if (Tk
::lsearch
($w->{'SELECTION_LIST_REF'},$i) >= 0)
while ($i > $el && $i > $anchor)
if (Tk
::lsearch
($w->{'SELECTION_LIST_REF'},$i) >= 0)
$w->{'PREVIOUS_ELEMENT'} = $el
# This procedure is typically invoked on shift-button-1 presses. It
# begins the process of extending 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.
# w - The listbox widget.
# el - The element for the selection operation (typically the
# one under the pointer). Must be in numerical form.
# if no selections, select current
$w->selectionAnchor($el);
$w->{'PREVIOUS_ELEMENT'}=$el;
if ($w->cget('-selectmode') eq 'extended' && $w->selectionIncludes('anchor'))
# This procedure is typically invoked on control-button-1 presses. It
# begins the process of toggling 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.
# w - The listbox widget.
# el - The element for the selection operation (typically the
# one under the pointer). Must be in numerical form.
if ($w->cget('-selectmode') eq 'extended')
my @list = $w->curselection();
$w->{'SELECTION_LIST_REF'} = \
@list;
$w->{'PREVIOUS_ELEMENT'} = $el;
$w->selectionAnchor($el);
if ($w->selectionIncludes($el))
# This procedure is invoked when the mouse leaves an entry window
# with button 1 down. It scrolls the window up, down, left, or
# right, depending on where the mouse left the window, and reschedules
# itself as an "after" command so that the window continues to scroll until
# the mouse moves back into the window or the mouse button is released.
# x - The x-coordinate of the mouse when it left the window.
# y - The y-coordinate of the mouse when it left the window.
$w->yview('scroll',1,'units')
$w->yview('scroll',-1,'units')
$w->xview('scroll',2,'units')
$w->xview('scroll',-2,'units')
$w->Motion($w->index("@" . $x . ',' . $y));
$w->RepeatId($w->after(50,'AutoScan',$w,$x,$y));
# Moves the location cursor (active element) up or down by one element,
# and changes the selection if we're in browse or extended selection
# w - The listbox widget.
# amount - +1 to move down one item, -1 to move back one item.
$w->activate($w->index('active')+$amount);
my $selectmode = $w->cget('-selectmode');
if ($selectmode eq 'browse')
$w->selectionClear(0,'end');
$w->selectionSet('active')
elsif ($selectmode eq 'extended')
$w->selectionClear(0,'end');
$w->selectionSet('active');
$w->selectionAnchor('active');
$w->{'PREVIOUS_ELEMENT'} = $w->index('active');
$w->{'SELECTION_LIST_REF'}=\
@list;
# Does nothing unless we're in extended selection mode; in this
# case it moves the location cursor (active element) up or down by
# one element, and extends the selection to that point.
# w - The listbox widget.
# amount - +1 to move down one item, -1 to move back one item.
if ($w->cget('-selectmode') ne 'extended')
$w->activate($w->index('active')+$amount);
$w->Motion($w->index('active'))
# This procedure is called for key-presses such as Shift-KEndData.
# If the selection mode isn't multiple or extend then it does nothing.
# Otherwise it moves the active element to el and, if we're in
# extended mode, extends the selection to that point.
# w - The listbox widget.
# el - An integer element number.
my $mode = $w->cget('-selectmode');
if ($w->selectionIncludes('anchor'))
elsif ($mode eq 'multiple')
# This procedure is invoked to cancel an extended selection in
# progress. If there is an extended selection in progress, it
# restores all of the items between the active one and the anchor
# to their previous selection state.
# w - The listbox widget.
if ($w->cget('-selectmode') ne 'extended' || !defined $w->{'PREVIOUS_ELEMENT'})
my $first = $w->index('anchor');
my $last = $w->{'PREVIOUS_ELEMENT'};
($first,$last)=($last,$first);
$w->selectionClear($first,$last);
if (Tk
::lsearch
($w->{'SELECTION_LIST_REF'},$first) >= 0)
# This procedure is invoked to handle the "select all" operation.
# For single and browse mode, it just selects the active element.
# Otherwise it selects everything in the widget.
# w - The listbox widget.
my $mode = $w->cget('-selectmode');
if ($mode eq 'single' || $mode eq 'browse')
$w->selectionClear(0,'end');
$w->selectionSet('active')
$w->selectionSet(0,'end')
foreach $i (reverse $w->curselection)
my $element = $w->index('active') || $w->index($w->XEvent->xy);
eval {local $SIG{__DIE__
}; $str = $w->clipboardGet };
foreach (split("\n",$str))
$w->insert($element++,$_);
foreach $i ($w->curselection)
push(@result,$w->get($i));
return (wantarray) ?
@result : $result[0];