Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This script below draws a combination of squares and text on a Gnome2::Canvas. 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 are drawn 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 t +han 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 ye +llow 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();

Replies are listed 'Best First'.
Re: Gnome2::Canvas - things on top of other things
by Anonymous Monk on May 25, 2015 at 20:47 UTC

    I think you haven't checked the docs for ->raise, the real docs for ->raise

    You want to raise to the top? use raise_to_top

    You want to raise more than one step? Give raise a big number of steps

      Yes, thank you. I had thought that ->raise() and ->lower() were using levels, rather than a stack.

      The solution here is to use ->raise_to_top() with the text. A general solution is to use ->lower_to_bottom and ->raise(n) with the text, where n is the number of squares.

      # 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(); # Raise the text above the 5 squares in the Canvas stack $text->raise(5); }
Re: Gnome2::Canvas - things on top of other things
by Anonymous Monk on May 25, 2015 at 18:28 UTC
    This is one problem Click on a red square? Then destroy it, and replace it with a yellow one! why not simply change the color?

      That would not solve the underlying problem: text is obscured beneath the squares, but should be above the squares.

        That would not solve the underlying problem: text is obscured beneath the squares, but should be above the squares.

        It wasn't meant to, it identified another problem