Thanks, Rob. One working solution is to use ->allocate(). Another is to use Gtk2::Gdk::Cairo::Context instead of Gnome2::Canvas.

Both are posted below, in case anyone is interested.

#!/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 $frame = Gtk2::Frame->new(undef); $frame->set_border_width(0); $frame->set_size_request(-1, 50); my $canvas = Gnome2::Canvas->new(); $frame->add($canvas); $vBox->pack_start($frame, FALSE, FALSE, 0); # The ->show_all() call here makes everything work (for some reason) $window->show_all(); my $rect = $canvas->allocation(); $canvas->set_scroll_region( 0, 0, $rect->width, $rect->height, ); # Draw a red bar, almost as long as the frame, but with a fixed # height, centred in the middle my ($width, $height) = $canvas->get_size(); # (The Gtk2::Frame is 490x50, so the Gnome2::Canvas should be 488x48) #print "canvas w $width h $height\n"; my $canvasItem = Gnome2::Canvas::Item->new( $canvas->root(), 'Gnome2::Canvas::Rect', x1 => 20, y1 => 20, x2 => ($width - 20), y2 => ($height - 20), fill_color => '#FF0000', outline_color => '#000000', ); $canvasItem->raise_to_top(); $window->show_all(); Gtk2->main;

Second one...

#!/usr/bin/perl use strict; use diagnostics; use warnings; use Gtk2 '-init'; use Glib qw(TRUE FALSE); # 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 $eBox = Gtk2::EventBox->new(); $eBox->set_app_paintable(TRUE); $eBox->signal_connect('expose_event' => \&draw_event_box); my $Label1 = Gtk2::Label->new("Label1"); $Label1->set_markup("<span foreground='green' font='25'>Label1</span>" +); my $Label2 = Gtk2::Label->new("Label2"); $Label2->set_markup("<span foreground='red' font='25'>Label2</span>"); my $vBox = Gtk2::VBox->new(FALSE, 0); $vBox->pack_start_defaults($Label1); $vBox->pack_start_defaults($Label2); $eBox->add($vBox); $window->add($eBox); $window->show_all(); Gtk2->main; sub draw_event_box { my ($widget, $event, $user_data) = @_; my $cr = Gtk2::Gdk::Cairo::Context->create( $widget->window ); my $rectangle = $widget->allocation; #Paint background. $cr->set_source_rgb(0, 1, 1); $cr->paint; #Draw yellow line in middle. $cr->set_source_rgb(1, 1, 0); $cr->set_line_width(10); $cr->move_to(0, ($rectangle->height) / 2); $cr->line_to($rectangle->width, ($rectangle->height) / 2); $cr->stroke(); #Draw frame. $cr->set_source_rgb(0, 0, 1); $cr->set_line_width(15); $cr->rectangle(0, 0, $rectangle->width, $rectangle->height); $cr->stroke(); return FALSE; }

In reply to Re^2: Can't position Gnome2::Canvas within Gtk2::Frame by Anonymous Monk
in thread Can't position Gnome2::Canvas within Gtk2::Frame by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.