Gnome2::Canvas help reprise




Trying again, since the last attempt was garbled...
 
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 the text to 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, placed like this:
 
$bg->lower_to_bottom();
 
There are some red squares, which are placed like this:
 
$square->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 script doesn't work. Initially, four of the five text labels are drawn BENEATH squares. 
The obscured text is only revealed by clicking on the square above it.
 
Can anyone tell me where I'm going 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]