Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How to make a Tk::Button move/slide within a Tk::Table

by thundergnat (Deacon)
on Feb 09, 2012 at 17:01 UTC ( #952787=note: print w/ replies, xml ) Need Help??


in reply to How to make a Tk::Button move/slide within a Tk::Table

I wasn't sure of the desired behavior, (should each button be replaced as you drag over it, or should only the beginning and ending button be swapped?) so each is in here. Uncomment the appropriate line(s) in the slideTo{} sub.

Also, I changed the drag binding to the right mouse button so it wouldn't collide with left button presses. Change it back if that isn't what you want.

Your addButton command logic seems over-complex too, but I didn't mess with it as I wasn't sure what else you might be using it for.

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' ); ################################ my $isDragging = 0; #boolean - mouse button pressed and draggi +ng my $selectedButton = 0; my ( @original, @previous ); $mw->bind( '<Motion>', \&motion ); ################################ $mw->MainLoop(); sub configureButton { 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-3>', \&buttonPress ); $buttons[$btnNum]->bind( '<ButtonRelease-3>', \&buttonRelease ); } sub motion { slideTo( findCell( $_[0] ) ) if $isDragging; } sub buttonPress { $isDragging = 1; @original = @previous = findCell( $_[0] ); } sub buttonRelease { $isDragging = 0; } sub findCell { my $e = $_[0]->XEvent; my ( $row, $col ) = $table->Posn( $table->containing( $e->X, $e->Y + ) ); return ( $row, $col ); } sub slideTo { my @next = @_; if ( $previous[0] == $next[0] and $previous[1] == $next[1] ) { return; # still in same cell } else { #swap( \@previous, \@next ); # depending on desired behaviour, uncomment either the previou +s or the next two lines swap( \@previous, \@original ); swap( \@original, \@next ); @previous = @next; } } sub swap { my ( $this, $that ) = @_; my $s1 = $table->get(@$this); my $s2 = $table->get(@$that); $s2->UnmanageGeometry; $table->LostSlave($s2); $table->put( @$this, $s2 ); $table->put( @$that, $s1 ); }


Comment on Re: How to make a Tk::Button move/slide within a Tk::Table
Download Code
Re^2: How to make a Tk::Button move/slide within a Tk::Table
by bcarroll (Pilgrim) on Feb 09, 2012 at 19:22 UTC
    Thanks thundergnat!

    That (second option) is exactly what I was looking for, though I think it looks/works better using the first option (swap start and end cells).

    I think I will stick with dragging with the right mouse button, as it seems to be the better method...

    By the way... before I reinvent the wheel, is there an easy way to save the $table layout and @buttons to a file so i can import/recreate the layout? I read something about Data::Dumper being able to do that sort of thing, but i have never used it for that purpose.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-12-26 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (164 votes), past polls