jpollack has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|