home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / libgnome2-canvas-perl / examples / canvas-features.pm < prev    next >
Encoding:
Perl POD Document  |  2004-03-20  |  3.0 KB  |  131 lines

  1. package CanvasFeatures;
  2.  
  3. use strict;
  4. use Glib qw(TRUE FALSE);
  5. use Gnome2::Canvas;
  6.  
  7. use constant GNOME_PAD_SMALL => 4;
  8.  
  9. #
  10. # Event handler for the item to be reparented.  When the user clicks on the
  11. # item, it will be reparented to another group.
  12. #
  13.  
  14. sub item_event {
  15.     my ($item, $event) = @_;
  16.  
  17.     return FALSE
  18.         if ($event->type ne 'button-press') || ($event->button != 1);
  19.  
  20.     my $parent1 = $item->{parent1};
  21.     my $parent2 = $item->{parent2};
  22.  
  23.     $item->reparent ($item->parent == $parent1 ? $parent2 : $parent1);
  24.  
  25.     return TRUE;
  26. }
  27.  
  28. sub create {
  29.     my $vbox = Gtk2::VBox->new (FALSE, GNOME_PAD_SMALL);
  30.     $vbox->set_border_width (GNOME_PAD_SMALL);
  31.     $vbox->show;
  32.  
  33.     # Instructions
  34.  
  35.     my $w = Gtk2::Label->new ("Reparent test:  click on the items to switch them between parents");
  36.     $vbox->pack_start ($w, FALSE, FALSE, 0);
  37.     $w->show;
  38.  
  39.     # Frame and canvas
  40.  
  41.     my $alignment = Gtk2::Alignment->new (0.5, 0.5, 0.0, 0.0);
  42.     $vbox->pack_start ($alignment, FALSE, FALSE, 0);
  43.     $alignment->show;
  44.  
  45.     my $frame = Gtk2::Frame->new;
  46.     $frame->set_shadow_type ('in');
  47.     $alignment->add ($frame);
  48.     $frame->show;
  49.  
  50.     my $canvas = Gnome2::Canvas->new;
  51.     $canvas->set_size_request (400, 200);
  52.     $canvas->set_scroll_region (0, 0, 400, 200);
  53.     $frame->add ($canvas);
  54.     $canvas->show;
  55.  
  56.     # First parent and box
  57.  
  58.     my $parent1 = Gnome2::Canvas::Item->new ($canvas->root,
  59.                          'Gnome2::Canvas::Group',
  60.                          x => 0.0,
  61.                          y => 0.0);
  62.  
  63.     Gnome2::Canvas::Item->new ($parent1, 'Gnome2::Canvas::Rect',
  64.                    x1 => 0.0,
  65.                    y1 => 0.0,
  66.                    x2 => 200.0,
  67.                    y2 => 200.0,
  68.                    fill_color => 'tan');
  69.  
  70.     # Second parent and box
  71.  
  72.     my $parent2 = Gnome2::Canvas::Item->new ($canvas->root,
  73.                                              'Gnome2::Canvas::Group',
  74.                          x => 200.0,
  75.                          y => 0.0);
  76.  
  77.     Gnome2::Canvas::Item->new ($parent2, 'Gnome2::Canvas::Rect',
  78.                    x1 => 0.0,
  79.                    y1 => 0.0,
  80.                    x2 => 200.0,
  81.                    y2 => 200.0,
  82.                    fill_color => "#204060");
  83.  
  84.     # Big circle to be reparented
  85.  
  86.     my $item = Gnome2::Canvas::Item->new ($parent1,
  87.                           'Gnome2::Canvas::Ellipse',
  88.                           x1 => 10.0,
  89.                           y1 => 10.0,
  90.                           x2 => 190.0,
  91.                           y2 => 190.0,
  92.                           outline_color => 'black',
  93.                           fill_color => 'mediumseagreen',
  94.                           width_units => 3.0);
  95.     $item->{parent1} = $parent1;
  96.     $item->{parent2} = $parent2;
  97.     $item->signal_connect (event => \&item_event);
  98.  
  99.     # A group to be reparented
  100.  
  101.     my $group =
  102.         Gnome2::Canvas::Item->new ($parent2, 'Gnome2::Canvas::Group',
  103.                         x => 100.0,
  104.                         y => 100.0);
  105.  
  106.     Gnome2::Canvas::Item->new ($group, 'Gnome2::Canvas::Ellipse',
  107.                    x1 => -50.0,
  108.                    y1 => -50.0,
  109.                    x2 => 50.0,
  110.                    y2 => 50.0,
  111.                    outline_color => 'black',
  112.                    fill_color => 'wheat',
  113.                    width_units => 3.0);
  114.     Gnome2::Canvas::Item->new ($group, 'Gnome2::Canvas::Ellipse',
  115.                    x1 => -25.0,
  116.                    y1 => -25.0,
  117.                    x2 => 25.0,
  118.                    y2 => 25.0,
  119.                    fill_color => 'steelblue');
  120.  
  121.     $group->{parent1} = $parent1;
  122.     $group->{parent2} = $parent2;
  123.     $group->signal_connect (event => \&item_event);
  124.  
  125.     # Done
  126.  
  127.     return $vbox;
  128. }
  129.  
  130. 1;
  131.