# Selection box in a Gtk::DrawingArea

**URL:** <https://www.ruby-forum.com/t/selection-box-in-a-gtk-drawingarea/109764>\
**Category:** Ruby-Gnome 2\
**Created:** [July 18, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/selection-box-in-a-gtk-drawingarea/109764 "2007-07-18T20:11:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [July 18, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/selection-box-in-a-gtk-drawingarea/109764/1 "2007-07-18T20:11:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [July 18, 2007, 8:33pm UTC](https://www.ruby-forum.com/t/selection-box-in-a-gtk-drawingarea/109764/2 "2007-07-18T20:33:38Z")

</div>

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](http://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/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [July 18, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/selection-box-in-a-gtk-drawingarea/109764/3 "2007-07-18T22:15:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [July 18, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/selection-box-in-a-gtk-drawingarea/109764/4 "2007-07-18T23:19:27Z")

</div>

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
