Selection box in a Gtk::DrawingArea

Hi, I was wondering if anybody knows the best way to make a “selected”
box by clicking and dragging within a Gtk:DrawingArea. I’m looking
around the Gtk2 API, but I’m not sure of the terminology for this kind
of thing. I don’t need to cut or paste or anything, just looking for
coordinates and the visual effect.

Hope that makes sense, anybody know much about this?

Am Mittwoch, den 18.07.2007, 20:11 +0200 schrieb Conan R.:

Hi, I was wondering if anybody knows the best way to make a “selected”
box by clicking and dragging within a Gtk:DrawingArea. I’m looking
around the Gtk2 API, but I’m not sure of the terminology for this kind
of thing. I don’t need to cut or paste or anything, just looking for
coordinates and the visual effect.

Hope that makes sense, anybody know much about this?

Hi,

the coordinates, you will get with the button-press and the
button-release events. To show the selected area in the way like the
marching ants in i.e. the gimp do the following:

draw a rectangle on button press; the GdkFuntion of the drawing GC has
to be INVERT and the the rectangle has to be set to not filled.
then listen to the pointer-motion-event and with every event remove the
rectangle at the old coordinates by drawing it an other time and then
draw the new one. On button release remove the last rectangle, by
drawing the last rectangle again.

With this so far you get a rectangle, that is inverted and follows your
mouse pointer. If you want real marching ants (the line is stipled and
the dots are wandering) you have to specify the dots with set_dashes of
your drawing GC and alter the dash offset in this method within a
timeout.

Cheers
detlef


This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

That makes a lot of sense, thanks! I’ll give it a try a little later.

Worked beautifully with just a bit of code. I’ve included it below as
an example.

#—Snip—
@selection = nil
@selecting = false

win.add_events(Gdk::Event::BUTTON_PRESS_MASK)
win.add_events(Gdk::Event::BUTTON_RELEASE_MASK)
win.add_events(Gdk::Event::POINTER_MOTION_MASK)

win.signal_connect(“button-press-event”) do |widget, event|
@selection = [event.x,event.y,event.x,event.y]
@selecting = true
widget.queue_draw
end

win.signal_connect(“motion-notify-event”) do |widget, event|
if(@selecting and @selection)
@selection[2] = event.x
@selection[3] = event.y
widget.queue_draw
end
end

win.signal_connect(“button-release-event”) do |widget, event|
if(@selecting and @selection)
@selection[2] = event.x
@selection[3] = event.y
@selecting = false
widget.queue_draw
end
end

#—Snip—
#in my drawing function

if(@selection)
gc.function = Gdk::GC::INVERT
x = @selection[0] > @selection[2] ? @selection[2] : @selection[0]
y = @selection[1] > @selection[3] ? @selection[3] : @selection[1]
width = (@selection[0] - @selection[2]).abs
height = (@selection[1] - @selection[3]).abs
window.draw_rectangle(gc, true, x, y, width, height)
gc.function = Gdk::GC::COPY
end

#—Snip