Gnome2::Canvas help



Hello all,
 
I have a few Perl/Gtk2 problems which have been hanging around for months, cooking up a stink. I wonder if anyone can help me resolve them?
 
First, I'm trying to use a Gnome2::Canvas to draw various simple shapes.
 
This example script draws a combination of squares and text. I want to guarantee that the text will always be visible above the squares, but the script doesn't work as intended.
 
I'm using Gnome2::Canvas::Item->lower_to_bottom() and ->raise(). There's a white background, which is placed like this:
    $bg->lower_to_bottom();
 
Then there are some red squares, which are placed like this:
    $square->lower_to_bottom();
    $square->raise(1);
 
Finally, there is some text, which is placed like this:
    $text->lower_to_bottom();
    $text->raise(2);
 
Clicking on a red square destroys its canvas object, and replaces it with a (new) yellow square, placed in the same way:
    $replaceSquare->lower_to_bottom();
    $replaceSquare->raise(1);
 
Unfortunately, the example script doesn't have the desired effect. Initially, four of the five text labels appear BENEATH their (red) squares. The text is only revealed by clicking on the red square.
 
Can anyone tell me what I'm doing wrong?
 
---
 
#!/usr/bin/perl
package canvas;
 
use strict;
use diagnostics;
use warnings;
 
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
use Gnome2::Canvas;
 
# Canvas size doesn't matter that much, as long as it's a bit bigger than the window
my $size = 600;
my $flipFlag = FALSE;
 
# Create the window
my $window = Gtk2::Window->new();
 
$window->set_title('Canvas');
$window->set_position('center');
$window->set_default_size($size, $size);
$window->set_border_width(5);
$window->signal_connect (destroy => sub { Gtk2->main_quit; });
 
# Add a VBox containing a Gnome2::Canvas
my $vBox = Gtk2::VBox->new(FALSE, 0);
 
my $canvasFrame = Gtk2::Frame->new(undef);
$canvasFrame->set_border_width(3);
 
my $canvasScroller = Gtk2::ScrolledWindow->new();
$canvasScroller->set_border_width(3);
$canvasScroller->set_policy('always','always');
 
my $canvas = Gnome2::Canvas->new();
$canvas->set_scroll_region(0, 0, $size, $size);
$canvas->set_center_scroll_region(1);
$canvas->set_pixels_per_unit(1);

my $canvasRoot = $canvas->root();
 
$canvasScroller->add($canvas);
$canvasFrame->add($canvasScroller);
$vBox->pack_start($canvasFrame, TRUE, TRUE, 0);
$window->add($vBox);
 
# Draw a white background
my $bg = Gnome2::Canvas::Item->new (
    $canvasRoot,
    'Gnome2::Canvas::Rect',
    x1 => 0,
    y1 => 0,
    x2 => $size,
    y2 => $size,
    fill_color => '#FFFFFF',
    outline_color => '#FFFFFF',
);
 
$bg->lower_to_bottom();
 
# Draw some squares above the background
for (my $count = 0; $count < 5; $count++) {
 
    my ($square, $posn, $newColour);
    
    $posn = ($count * 100);
    
    $square = Gnome2::Canvas::Item->new(
                $canvasRoot,
                'Gnome2::Canvas::Rect',
                x1 => ($posn + 10),
                y1 => ($posn + 10),
                x2 => ($posn + 60),
                y2 => ($posn + 60),
                outline_color => '#000000',
                fill_color => '#FF0000',
    );
 
    $square->lower_to_bottom();
    $square->raise(1);
    
    # Click on a red square? Then destroy it, and replace it with a yellow one!
    $square->signal_connect (event => sub {
 
        my ($widget, $event) = @_;  
        
        my $replaceSquare;
        
        if ($event->type eq 'button-press') {
            
            $square->destroy();
            
            $replaceSquare = Gnome2::Canvas::Item->new(
                $canvasRoot,
                'Gnome2::Canvas::Rect',
                x1 => ($posn + 10),
                y1 => ($posn + 10),
                x2 => ($posn + 60),
                y2 => ($posn + 60),
                outline_color => '#000000',
                fill_color => '#FFFF00',
            );          
        
            $replaceSquare->lower_to_bottom();
            $replaceSquare->raise(1);
        }
    });
}
 
# Draw some blue text above the squares
for (my $count = 0; $count < 5; $count++) {
 
        my $text = Gnome2::Canvas::Item->new(
            $canvasRoot,
            'Gnome2::Canvas::Text',
            x => (($count * 100) + 25),
            y => (($count * 100) + 25),
            fill_color => '#0000FF',
            font => 'Sans',
            size => 10000,
            anchor => 'GTK_ANCHOR_W',
            text => 'Hello world!',
        );
 
        $text->lower_to_bottom();
        $text->raise(2);
}
 
# Open the window
$window->show_all();
Gtk2->main();
 
 


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