I'm getting a headache working with Gtk2::DrawingAreas. Seven headaches, to be precise. Perhaps someone can offer some Perl paracetemol?
(As always, "Go away and read this document" or "Go away and read that code example" would be terrific.)
I cobbled together a working example (drawarea.pl, attached). It opens a Gtk2::Window with a Gtk2::DrawingArea inside it. There's a subroutine to draw a rectangle inside the drawing area, and another to draw some pango markup text. So far, so good.
1. I want to be able to detect mouse clicks. Specifically, I want to know when the user clicks on one of the rectangles, so I can write some code to 'select' the rectangle by changing its colour.
The example does half the job. The 'button-press-event' signal causes the example to print the x/y coordinates of every mouse click.
Now, I need the user to be able to select *multiple* rectangles by clicking on them while holding down the SHIFT or CTRL key. I don't know how to detect the combination of mouse click + SHIFT/CTRL.
It's a pretty standard job - there must be an easy way to do it, right?
2. The 'button-press-event' signal doesn't help me with detecting double-clicks. Is there a different signal for this?
3. I also want the user to be able to select multiple rectangles by dragging the mouse across a portion of the DrawingArea.
Don't know what the technical term for this is - but the sequence is 'click on position, hold down left mouse button, move mouse, release left mouse button'. Again, a fairly standard job in graphics editing software, in which a rectangle gets drawn (and updated) showing the area selected so far, before the mouse button has been release.
The Gtk-Perl docs promise a simple graphic editor code example, but it's still at the 'not written yet' stage. Can anyone point me in the right direction?
4. The call to Gtk2::Gdk::Drawable->draw_rectangle produces a rectangle that's a different size, depending on whether it's filled in, or not.
(In the example, the filled-in blue rectangle is 1 pixel smaller in width and in height, than the black rectangle. Actually, the blue rectangle is the one that's the correct size.)
Is this really the expected behaviour of Gtk2::Gdk::Drawable->draw_rectangle? It seems pretty odd to me.
5. I'm confused about how the pango text is drawn. Specifically, I want to be able to draw in a font of a fixed size.
In the example, the line...
my $fontDescrip = Gtk2::Pango::FontDescription->from_string("Arial 12");
...doesn't cause text of 12 pixels to be drawn, not even if we change the call to &drawPangoText to remove the 'size' attribute:
&drawPangoText(
$drawingArea,
150, 10,
"<span background = '' foreground = '#FF0000'"
# . " size = '20000' weight = 'heavy'>Hello world!</span>" # This line commented out
. " weight = 'heavy'>Hello world!</span>" # Replaced by this line
);
I can't find anything in the Gtk2-perl docs that give a clue about what 'size' is doing. A value of about 500 seems to produce roughly the right size characters, but heaven knows why. Where should I go from here?
6. Let's say that I use this code to draw a rectangle, and then draw some pango text nearby.
Now let's say that the user clicks on the rectangle to 'select it', and I've written some nice code to change the rectangle's colour. If, by any chance, the pango text has been drawn ON TOP of the rectangle, I need to re-draw the pango text, too.
It would be easy to do, if I knew that the pango text has filled a rectangle starting at (x,y) of size (width, height). In the example, this is the area that has been overdrawn with a black background.
I have not the slightest idea how to get these values. I wonder if anyone has any ideas?
7. It's pretty easy to set up tooltips for a Gtk2::Button or Gtk2::Label, but I haven't had any luck with the drawing area yet. I found a 'query_tooltip' signal but I can't get it to work, nor can I find any perl scripts which do this job.
Specifically, this modification to the example doesn't work:
# Drawing area signals
$drawingArea->set_events ([
'exposure-mask',
'leave-notify-mask',
'button-press-mask',
'pointer-motion-mask',
'pointer-motion-hint-mask',
]);
$drawingArea->signal_connect(query_tooltip => sub {
...
I'd like some kind of notification just before the tooltip appears, so I can decide what text to display (namely, 'this is a blue rectangle!' or 'this is a black rectangle!')