I am trying to write a application with Gtk2 which involves several new custom controls which I am attempting to build using Cairo on a Gtk2::DrawingArea.

My question is fairly simple, though. When (ie, in what contexts) can I/should I draw on the DrawingArea? I've noticed that although a draw command will work when in the event handler for a button callback, it won't when in the callback for the "configure_event" signal on my DrawingArea! Being as the configure_event signal is sent when the DrawingArea changes size, it seemed like a logical place to redraw the control, but that's not working.

I noticed that if I, while in the configure_event callback, add a function to Glib::Idle to paint, then it succeeds. What gives?

Here is some sample code which tries to paint the drawing area three different ways. When the button is depressed, it immediately paints it red. When a configure_event signal is received, then, if the checkbox is checked, it will add an anonymous sub to paint to the idle loop. If the checkbox is not checked, it will attempt to paint immediately.

Why does this work when the checkbox is checked, but not when it is not?

(I'm using camelbox, which I highly recommend for a quick and easy distribution of perl with GTK on Windows.)

#!perl use strict; use Gtk2 -init; my $window = Gtk2::Window->new (); $window->signal_connect (delete_event => sub { Gtk2->main_quit } ); my $vbox = Gtk2::VBox->new; $window->add ($vbox); my $check = Gtk2::CheckButton->new ("paint on configure in idle?"); $check->set_active (0); $vbox->pack_start ($check, 0, 0, 0); my $but = Gtk2::Button->new ("paint red, now"); $but->signal_connect (clicked => sub {set_to_color ([255*257,0,0])}); $vbox->pack_start ($but, 0, 0, 0); my $draw = Gtk2::DrawingArea->new (); $draw->set_size_request (200, 200); $draw->signal_connect (configure_event => sub { if ($check->get_active ()) { Glib::Idle->add (\&set_to_color, [0, 255*257, 0]); } else { set_to_color ([0, 255*257, 0]); } }); $vbox->pack_start ($draw, 1, 1, 0); $window->show_all; Gtk2->main; sub set_to_color { (my ($r, $g, $b)) = @{$_[0]}; my $cr = Gtk2::Gdk::Cairo::Context->create ($draw->window); $cr->set_source_color (Gtk2::Gdk::Color->new ($r, $g, $b)); $cr->rectangle (0, 0, $draw->allocation->width, $draw->allocation->height); $cr->fill (); $cr = undef; return 0; }

In reply to When can I draw to a GTK+ window? by jpollack

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.