Re: Dragging a Canvas::Line with the mouse?



On Thu, 19 Jun 2008 11:38:42 -0700
walt <w41ter gmail com> wrote:


I need to draw a vertical line on a canvas, to be used as a cursor
that can be moved horizontally in response to a mouse click or a
keystroke.

Is this what you are looking for?

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


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();
    my $white = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xFFFF);
    $canvas->modify_bg('normal',$white);

    $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();

    my $line1 = Gnome2::Canvas::Item->new ($root,
          'Gnome2::Canvas::Line',
         points => [150, 150, 150, 230],
        fill_color => '#ff0000',
       #     fill_color_rgba => 0x3cb37180,
        width_units => 6.0,
       );

        # Make it movable
     $line1->signal_connect( "event", \&item_move );
     
    
}


# Callback for moving items on the canvas
my ( $dragging, $last_x, $last_y );    # item_move static data

sub item_move {
    my ( $item, $event ) = @_;

    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;
    }
}

__END__


zentara

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



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