That works perfectly! And significantly reduces the amount of code!
FWIW, Here is the new code.
use warnings;
use strict;
use Tk;
use Tk::Table;
use Data::Dumper;
my $buttonsPerRow = 5;
$buttonsPerRow -= 1;
my $row = 0;
my $col = 1;
my $buttonNum = 0;
my @buttons;
my $mw = tkinit();
my $menuFrame = $mw->Frame()->pack(-expand=>1,-fill=>'both');
my $buttonFrame = $mw->Frame()->pack(-expand=>1,-fill=>'both');
my $table = $buttonFrame->Table(
-columns => 5,
-scrollbars => 'se',
-fixedrows => 1,
)->pack(-expand=>1,-fill=>'both');
my $addButton = $menuFrame->Button(
-text => 'Add Button',
-command => sub{
$buttons[$buttonNum] = $table->Button();
print "Button $buttonNum->$row:$col\n";
$buttons[$buttonNum]{'row'} = $row;
$buttons[$buttonNum]{'col'} = $col;
$table->put($row,$col,$buttons[$buttonNum]);
configureButton($buttonNum);
$col>$buttonsPerRow?$row++:undef;
$col>$buttonsPerRow?$col=1:$col++;
$buttonNum++;
},
)->pack(-side=>'top');
################################
#DragNDrop
my $isDragging = 0; #boolean - left mouse button pressed and dragging
my $selectedButton = 0;
$mw->bind('<Motion>', \&motion);
################################
$mw->MainLoop();
sub configureButton{
#configure the new button
my $btnNum = shift;
$buttons[$btnNum]->bind('<Enter>', sub{
$selectedButton = $btnNum;
}
);
$buttons[$btnNum]->configure(
-text => "Button $btnNum",
-command => sub{
print "Button $btnNum pressed\n";
}
);
$buttons[$btnNum]->bind('<ButtonPress-1>', \&buttonPress);
$buttons[$btnNum]->bind('<ButtonRelease-1>', \&buttonRelease);
}
sub motion {
#what to do when the mouse is moving
my($widget) = @_;
my $e = $widget->XEvent;
if ($isDragging){
findCell($e->X,$e->Y);
}
return unless $isDragging;
}
sub buttonPress {
$isDragging = 1;
}
sub buttonRelease {
#what to do when the left mouse button is released
$isDragging = 0;
}
sub findCell{
my $tmpX = shift;
my $tmpY = shift;
my ( $row, $col ) = $table->Posn($table->containing($tmpX,$tmpY));
print "$tmpX:$tmpY: Row $row Col $col\n";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.