# Ruby + cairo problem

**URL:** <https://www.ruby-forum.com/t/ruby-cairo-problem/89994>\
**Category:** Ruby-Gnome 2\
**Created:** [March 24, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/ruby-cairo-problem/89994 "2007-03-24T19:40:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gergely\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@Gergely\_K](https://www.ruby-forum.com/u/Gergely_K)\
**Post date:** [March 24, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/ruby-cairo-problem/89994/1 "2007-03-24T19:40:47Z")

</div>

Hi!

I’ve a problem with cairo. When I have a DrawingArea in a VBox, and  
don’t know what’s wrong with the code (see the attached image: it  
seems that allocation has incorrect values).  
Does anybody know what could be the problem?

thanks  
Gergo

require ‘gtk2’  
class Diagram \< Gtk::DrawingArea  
def initialize  
super

```
set_size_request 200, 200
signal_connect("expose_event") {|w,e|
  cr = self.window().create_cairo_context
  cr.rectangle e.area.x, e.area.y, e.area.width, e.area.height
  cr.clip
  draw cr
  false
}

```

end  
def draw cr  
xcenter = allocation.x + allocation.width / 2  
ycenter = allocation.y + allocation.height / 2  
puts “origin: (#{allocation.x}, #{allocation.y})  
size:(#{allocation.width}, #{allocation.height})”  
cr.circle xcenter, ycenter, [allocation.width / 2,  
allocation.height / 2].min  
cr.stroke  
cr.move\_to allocation.x, allocation.y  
cr.line\_to allocation.x+allocation.width,  
allocation.y+allocation.height  
cr.move\_to allocation.x+allocation.width, allocation.y  
cr.line\_to allocation.x, allocation.y+allocation.height  
cr.stroke  
end  
end

class App \< Gtk::Window  
def initialize  
super  
self.title = “Cairo”  
signal\_connect(“delete\_event”){quit}  
signal\_connect(“destroy”){quit}  
add(hbox = Gtk::VBox.new)  
hbox.pack\_start Gtk::Label.new(‘cairo’), false, false  
hbox.pack\_start Gtk::Label.new(‘cairo’), false, false  
hbox.pack\_start Diagram.new, true, true  
hbox.pack\_start Gtk::Label.new(‘cairo’), false, false  
hbox.pack\_start Gtk::Label.new(‘cairo’), false, false  
hbox.pack\_start Gtk::Label.new(‘cairo’), false, false  
hbox.pack\_start Gtk::Label.new(‘cairo’), false, false  
show\_all  
end  
def quit  
Gtk.main\_quit  
end  
end

App.new  
Gtk.main

---

<div class="post-metadata">

**Author:** ![Gergely\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@Gergely\_K](https://www.ruby-forum.com/u/Gergely_K)\
**Post date:** [March 25, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/ruby-cairo-problem/89994/2 "2007-03-25T13:50:06Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] ruby + cairo problem” on Sat, 24 Mar 2007  
19:39:40 +0100,  
“Gergely K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve a problem with cairo. When I have a DrawingArea in a VBox, and  
> don’t know what’s wrong with the code (see the attached image: it  
> seems that allocation has incorrect values).  
> Does anybody know what could be the problem?

GtkAllocation’s description may help you:

```
A GtkAllocation of a widget represents region which has
been allocated to the widget by its parent. It is a
subregion of its parents allocation. See the section
called "Size Allocation" for more information.

gint x; the X position of the widget's area relative to
        its parents allocation.
gint y; the Y position of the widget's area relative to
        its parents allocation.
gint width; the width of the widget's allocated area.
gint height; the height of the widget's allocated area.

```

from  
[http://developer.gnome.org/doc/API/2.4/gtk/GtkWidget.html#GtkAllocation](http://developer.gnome.org/doc/API/2.4/gtk/GtkWidget.html#GtkAllocation)

x and y are _relative to their parents allocation_.

## Thanks,

kou

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys-and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)
