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

I'm trying to draw a coloured bar inside a Gtk2::Frame.

The bar is drawn on a Gnome2::Canvas, contained within the frame. The bar should be as long as possible, but a fixed height. The bar should also be exactly in the middle of the frame.

My problem is that I can't find the size of the Gtk2::Frame that's actually drawn, therefore I can't set the size of the Gnome2::Canvas that would fill it, therefore I can't draw the coloured bar in the right place.

The example script draws a red bar whose width isn't long enough, and whose vertical position is slightly off of centre.

(The call to Gnome2::Canvas->get_size() just returns default values of 100x100, which is not related to the frame's actual size.)

#!/usr/bin/perl use strict; use diagnostics; use warnings; use Gtk2 '-init'; use Glib qw(TRUE FALSE); use Gnome2::Canvas; # Draw a Gtk2 window my $window = Gtk2::Window->new(); $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }); $window->set_default_size(500, 500); $window->set_border_width(5); my $vBox = Gtk2::VBox->new(FALSE, 0); $window->add($vBox); # Draw a Gtk2::Frame containing a Gnome2::Canvas # The frame should be as long as possible, but only 50 pixels high my $frameHeight = 50; my $frame = Gtk2::Frame->new(undef); $frame->set_border_width(0); $frame->set_size_request(-1, $frameHeight); my $canvas = Gnome2::Canvas->new(); # (This line appears to move the canvas to the top-left corner of # the frame) $canvas->set_center_scroll_region(FALSE); $frame->add($canvas); $vBox->pack_start($frame, FALSE, FALSE, 0); # Draw a red bar, almost as long as the frame, but with a fixed height my ($width, $height) = $canvas->get_size(); my $rect = Gnome2::Canvas::Item->new( $canvas->root(), 'Gnome2::Canvas::Rect', x1 => 20, y1 => 20, x2 => ($width - 40), y2 => ($frameHeight - 20), fill_color => '#FF0000', outline_color => '#000000', ); $rect->raise_to_top(); $window->show_all(); Gtk2->main;