dragging text on Gnome2Canvas



Hi, I have a glitch here which I can't explain.

The script below creates a canvas, 4 circles, and 4 large Z's
The circles can be dragged repeatedly with no problem.
The text however acts funny. The Z near the green circle
can be dragged repeatedly, but the other 3 Z's seem
to lose the events after 1 drag.
Any explanation?

In another related problem, if I try to drag canvas text
over a canvas pixbuf, the text stops being draggable
if it is deposited on the pixbuf. This was my original problem,
I was trying to make text positioned by dragging, on an
canvas image. It would work for the first drop, but then
not respond, (similar to the 3 dead Z's below).

Thanks.

#!/usr/bin/perl
use warnings;
use strict;
use Gtk2 '-init';
use Gnome2::Canvas;

my ( $dragging, $last_x, $last_y ); 

my $mw = Gtk2::Window->new;
    $mw->signal_connect( destroy => sub { exit } );

    my $vbox = Gtk2::VBox->new( 0, 0 );
    $mw->add($vbox);

my $canvas = Gnome2::Canvas->new_aa();
    $vbox->pack_start( $canvas, 1, 1, 0 );
    $canvas->set_size_request( 300, 300 );
    $canvas->set_scroll_region( 0, 0, 300, 300 );

    my $quit = Gtk2::Button->new("Quit");
    $quit->signal_connect( clicked => sub { exit } );
    $vbox->pack_start( $quit, 0, 0, 0 );

$mw->show_all();

place_objects_on_canvas();

Gtk2->main();

##########################################################

sub place_objects_on_canvas {
    my $root = $canvas->root();

    for my $p (
        [ 50,  50,  "green" ],
        [ 50,  250, "orange" ],
        [ 250, 50,  "yellow" ],
        [ 250, 250, "blue" ]
      )
    {

        my ( $x, $y, $color ) = @$p;

        # Put a circle on the graph
        my $item = Gnome2::Canvas::Item->new(
            $root, "Gnome2::Canvas::Ellipse",
            x1            => $x - 16,
            y1            => $y - 16,
            x2            => $x + 16,
            y2            => $y + 16,
            fill_color    => $color,
            outline_color => "black",
        );

        # Put a circle on the graph
        my $text = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Text',
                                   text => 'Z',
                                   font => 'arial 30',
                                   x => $x , 
                                   y => $y ,
                                   anchor => 'w', 
                                   fill_color => 'black');



        # Make them movable
        $item->signal_connect( "event", \&item_move );
        $text->signal_connect( "event", \&item_move );
    
    }
}
######################################################
sub item_move {
    my ( $item, $event ) = @_;
     print "$item $event\n";

    if ( $event->type eq "button-press" ) {
        $item->raise_to_top();
        $last_x   = $event->x;
        $last_y   = $event->y;
        $dragging = 1;
    }
    elsif ( $event->type eq "motion-notify" ) {
        if ($dragging) {
            my $new_x = $event->x;
            my $new_y = $event->y;

            $item->move( $new_x - $last_x, $new_y - $last_y );
            $last_x = $new_x;
            $last_y = $new_y;
        }
    }
    elsif ( $event->type eq "button-release" ) {
        $dragging = 0;
    }

return 0;
}
__END__

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]