# Using a different cairo context of Gtk::DrawingArea

**URL:** <https://www.ruby-forum.com/t/using-a-different-cairo-context-of-gtk-drawingarea/78737>\
**Category:** Ruby-Gnome 2\
**Created:** [December 8, 2006, 6:01am UTC](https://www.ruby-forum.com/t/using-a-different-cairo-context-of-gtk-drawingarea/78737 "2006-12-08T06:01:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asubedi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@asubedi](https://www.ruby-forum.com/u/asubedi)\
**Post date:** [December 8, 2006, 6:01am UTC](https://www.ruby-forum.com/t/using-a-different-cairo-context-of-gtk-drawingarea/78737/1 "2006-12-08T06:01:51Z")

</div>

Hi all,

I am trying to write a graph widget by subclassing Gtk::DrawingArea  
and using cairo (ruby-gnome2 version 0.15 in ubuntu edgy). The  
examples about using cairo with DrawingArea create a cairo context in  
the expose\_event handler of the DrawingArea and pass that cairo  
context to some drawing method (or draw stuff in the handler itself).  
However, for a graph widget, I will not know what to draw until the  
user of my widget issues commands like draw\_line, draw\_rectangle, etc.  
(which are the members of Graph class).

So I would like to have a cairo context as a member of the class that  
would be created when an object of Graph is initialized. Then drawing  
functions would draw on the context. This cairo context will be drawn  
on the DrawingArea.

However, I have been unable to do this. I have tried to create an  
instance variable using @cr = widget.window.create\_cairo\_context using  
the expose\_event handler, but since this is not run until the  
DrawingArea is exposed, I cannot use it in other methods. Does anyone  
know how to create a cairo context and then set it to be viewed on  
DrawingArea. Is there a function like  
drawing\_area.window.set\_cairo\_context() for DrawingArea?

My skeleton for rgraph.rb looks something like this:

module RGraph  
class Graph \< Gtk::DrawingArea  
def initialize  
super  
signal\_connect(“expose\_event”){|widget, event|  
expose(widget, event)}  
@height = 200  
… # more instance variables like @width, @xmax,  
@xmin, etc.  
# here I would like to create a cairo context:  
# @cr = Cairo::Context.new  
end

```
   def expose(widget, event)
         # here I want to do something like
         # widget.window.set_cairo_context = @cr
         ...
   end # expose

   def draw_line(xi, yi, xf, yf) # x_initiail, y_initial, x_final, 

```

y\_final  
# convert this to equivalent cairo code  
@cr.move\_to(xic, yic)  
@cr.line\_to(xfc, yfc)  
@cr.stroke  
redraw\_canvas  
end # draw\_line

```
  def redraw_canvas
      alloc = allocation
      queue_draw_area(alloc.x, alloc.y, alloc.width, alloc.height)
      window.process_updates(true)
  end # redraw_canvas

end # class Graph

```

end # module RGraph

For an example of how this class might be used, my test file looks  
something like:

require ‘gtk2’  
require ‘rgraph’

win = Gtk::Window.new  
win.signal\_connect(“destroy”){Gtk.main\_quit}

gr = RGraph::Graph.new  
gr.height = 200  
gr.width = 200  
gr.draw\_line(0, 2, 2, 2)  
win.add(gr)  
win.show\_all  
Gtk.main

# end test file

Also following code shows some inconsistency:  
da = Gtk::DrawingArea.new  
da.realize  
puts da.window.class # prints NilClass  
da.signal\_connect(“expose\_event”){|widget, event| puts  
widget.window.class }

# this prints Gdk::Window. Shouldn’t both print Gdk::Window?

win.add(da)  
win.show\_all  
Gtk.main

Finally, I have converted the code in  
[http://gnomejournal.org/article/34/writing-a-widget-using-cairo-and-gtk28](http://gnomejournal.org/article/34/writing-a-widget-using-cairo-and-gtk28)  
and it’s sequel to ruby. I tried to create new page for this at  
“Tutorial” section, but was unable to do so.

Thanks in advance for any help!

* * *

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)

---

<div class="post-metadata">

**Author:** ![asubedi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@asubedi](https://www.ruby-forum.com/u/asubedi)\
**Post date:** [December 8, 2006, 6:45am UTC](https://www.ruby-forum.com/t/using-a-different-cairo-context-of-gtk-drawingarea/78737/2 "2006-12-08T06:45:54Z")

</div>

Hi,

2006/12/8, asubedi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

What about the following changes?

> module RGraph  
> class Graph \< Gtk::DrawingArea  
> def expose(widget, event)  
> # here I want to do something like  
> # widget.window.set\_cairo\_context = @cr  
> …  
> end # expose

```
  def expose(widget, event)
    @cr = widget.window.create_cairo_context
    false
  end

```

> ```
> end # class Graph
> 
> ```
> 
> end # module RGraph

> require ‘gtk2’  
> require ‘rgraph’
> 
> win = Gtk::Window.new  
> win.signal\_connect(“destroy”){Gtk.main\_quit}
> 
> gr = RGraph::Graph.new  
> gr.height = 200  
> gr.width = 200  
> gr.draw\_line(0, 2, 2, 2)

gr.signal\_connect(“expose\_event”) do  
gr.draw\_line(0, 2, 2, 2)  
false  
end

> win.add(gr)  
> win.show\_all  
> Gtk.main
> 
> # end test file

## 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)

---

<div class="post-metadata">

**Author:** ![asubedi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@asubedi](https://www.ruby-forum.com/u/asubedi)\
**Post date:** [December 8, 2006, 7:31am UTC](https://www.ruby-forum.com/t/using-a-different-cairo-context-of-gtk-drawingarea/78737/3 "2006-12-08T07:31:57Z")

</div>

Hi  
On 12/8/06, Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > require ‘rgraph’  
> > gr.draw\_line(0, 2, 2, 2)  
> > false  
> > end

This works, but I was wondering if it could be done without making the  
user to use the expose\_event handler. Is it not possible to use some  
other cairo context for the DrawingArea?

Thanks.

* * *

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)

---

<div class="post-metadata">

**Author:** ![asubedi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@asubedi](https://www.ruby-forum.com/u/asubedi)\
**Post date:** [December 8, 2006, 7:53am UTC](https://www.ruby-forum.com/t/using-a-different-cairo-context-of-gtk-drawingarea/78737/4 "2006-12-08T07:53:58Z")

</div>

Hi,

2006/12/8, asubedi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> user to use the expose\_event handler. Is it not possible to use some  
> other cairo context for the DrawingArea?

No. You can only use cairo context made by  
drawing\_area.window.create\_cairo\_context.

## 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)
