Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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;

In reply to Can't position Gnome2::Canvas within Gtk2::Frame by Anonymous Monk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (4)
    As of 2018-07-21 17:59 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (449 votes). Check out past polls.