Re: Goo::Canvas, how to delete an item



On Sun, 3 Aug 2008 11:48:22 -0400
zentara <zentara1 sbcglobal net> wrote:

Hi,
I can't figure out how to delete an item from a Goo::Canvas.
In the Gnome2::Canvas and the Goo::Canvas, items are
based on Glib::Object.
In Gnome2::Canvas, you can say $item->destroy, and it works.
In Goo::Canvas, an error is generated:
 
  "Can't locate object method "destroy" via package "Goo::Canvas::Ellipse" 

even though it is based on a Glib::Object.


Well, I did work out a way, but it seems like a bit of juggling just to destroy
an item.

You need to remove child items from a group, the group can be
root or a group of your making.

#!/usr/bin/perl -w
use strict;
use warnings;
use Goo::Canvas;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);

my $e1;
my $e0;

my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(680, 600);

my $vbox = Gtk2::VBox->new;
$vbox->set_border_width(4);
$vbox->show;

my $hbox = Gtk2::HBox->new(FALSE, 4);
$vbox->pack_start($hbox, FALSE, FALSE, 0);
$hbox->show;

$window->add($vbox);

my $swin = Gtk2::ScrolledWindow->new;
$swin->set_shadow_type('in');

$vbox->pack_start($swin, 1, 1, 0); 

my $cwidth = 1000;
my $cheight = 1000;

my $canvas = Goo::Canvas->new();
$canvas->set_size_request(600, 450); # minimum size on screen
$canvas->set_bounds(0, 0, $cwidth, $cheight); # scrollregion
$swin->add($canvas);

my $root = $canvas->get_root_item();
$canvas->signal_connect('button-press-event',
                      \&on_can_button_press);

my $g = Goo::Canvas::Group->new($root); 
$g->translate(310,300);


my $button = Gtk2::Button->new ('Draw');
$button->signal_connect (clicked => \&clicked );
$hbox->pack_start($button, FALSE, FALSE, 10);
$button->show();

$window->show_all();
Gtk2->main;


sub clicked {

my $cur_lab = $button->get_label();
#print "$cur_lab\n";
 if($cur_lab eq 'Draw'){
     $button->set_label('Clear');
     print "Starting action\n";
     &draw(); 

  }elsif($cur_lab eq 'Clear'){
     $button->set_label('Draw');
     print "Stopping action\n";
    &clear;
 }

}

sub draw{

 $e0 = Goo::Canvas::Ellipse->new(
        $g, 0, 0,300, 225,
        'stroke-color' => 'black',
        'line-width' => 2,
        'fill-color-rgba' => 0xffb3f111,
    );

 $e1 = Goo::Canvas::Ellipse->new(
        $g, 100, 100,300, 225,
        'stroke-color' => 'black',
        'line-width' => 2,
        'fill-color-rgba' => 0x3cb3f111,
    );

print $g->get_n_children,"\n";
print $root->get_n_children,"\n";

return 1;

}

sub clear{  
 
 #dosn't work, the number seems to shift as you remove 
 # $g->remove_child(0); 
 # $g->remove_child(1); 

#works 
  for(1..$g->get_n_children){
     $g->remove_child(0); 
   }

# or you can remove the group from root
# works
#$root->remove_child(0);

  return 1;
}

sub on_can_button_press {
     my ( $widget, $event ) = @_;
#     print $widget ,' ',$event->type,"\n";
     my ($x,$y) = ($event->x,$event->y);
      print "$x  $y\n";
    return TRUE;
}

__END__


zentara

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



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