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
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’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