in reply to Re: Can't position Gnome2::Canvas within Gtk2::Frame
in thread Can't position Gnome2::Canvas within Gtk2::Frame

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; }

Replies are listed 'Best First'.
Re^3: Can't position Gnome2::Canvas within Gtk2::Frame
by syphilis (Archbishop) on Oct 08, 2017 at 12:05 UTC
    Both are posted below, in case anyone is interested

    I'm (obviously) not very knowledgeable on Gtk, and it always seems to me that one has to pull things out of mid-air.
    Maybe things would appear a little less arcane if I actually read the documentation ;-)

    Thanks for taking the time to post those 2 approaches here. They both work fine for me on Windows.

    Cheers,
    Rob