ruby-****@sourc*****
ruby-****@sourc*****
2013年 3月 23日 (土) 14:48:54 JST
------------------------- REMOTE_ADDR = 70.49.48.128 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-dancr-rbcatut ------------------------- @@ -105,6 +105,89 @@ As you can see, if you run this program it displays an empty grey cairo drawing area. + However, as the title of this paragraph (12.3.0.1) suggests, we need the Gtk/Cairo framework because we would like to run some cairo code of our own. It would be desirable, if we had a template file into which we could always include the new code. This is what we will explain next. + +# (12.3.0.1.2) +:The Template Ruby File To Run Arbitrary Cairo Code: + (12.3.0.1.2){{br}} + + Indeed, to create the template we are going to use the code shown in the above 'minimal-rb-cr.rb'program. Eventually, the new template will act as wrapper code, with a stub method called((*draw(cr, drawing_area).*)) Basically, the template is an executable program in which you create a new class inheriting directly from the CairoWindow, and overriding its((*draw*))method. This 'draw' method is where you will place your cairo code. Lets look at the empty ready to use template: + + #!/usr/bin/env ruby + + $: << '~/work/HikiLib' + require 'hiki2-gtk-w-cairo.rb' + include HikiGtk + + class MyCairoCodeClass < CairoWindow # Your own CairoWindow class + def draw(cr, drawing_area) # Override the draw method + width, height = drawing_area.window.size # You will likely need w, h + o o o # Your code comes here + end # + end # + + window=MyCairoCodeClass.new # Replace 'CairoWindow' with your new class name. + # Optionally, supply the your name to the constructor. + window.show_all + Gtk.main + + The lines containing comments are the additions that turn the above 'minimal-rb-cr.rb' program into our template file. + + :A Real Life Example With Runing Cairo Code + + (12.3.0.1.2.2) {{image_right("1203-smiley-face-s1.png")}} + In Michael Urman's Cairo tutorial called ((<Cairo Tutorial for PyGTK Programmers|URL:http://www.tortall.net/mu/wiki/PyGTKCairoTutorial>)), where Nichael also creates a similar Gtk/Cairo framework as is ours, you can find a neat piece of Python cairo code generating a((*smiley face.*)) I translated his smiley face Python code to Ruby, and run it in our Ruby Gtk/Cairo wrapper. + + ((*SmileyFace.rb*)) + + #!/usr/bin/env ruby + + $: << '~/work/HikiLib' + require 'hiki2-gtk-w-cairo.rb' + include HikiGtk + + class SmileyFace < CairoWindow + + def draw(cr, drawing_area) + + width, height = drawing_area.window.size + + # Fill the background with a colour of your choice + cr.set_source_rgb(0.5, 0.5, 0.9) + cr.rectangle(0, 0, width, height) + cr.fill() + + # draw a rectangle + cr.set_source_rgb(1.0, 1.0, 1.0) + cr.rectangle(10, 10, width - 20, height - 20) + cr.fill() + + # and a circle + cr.set_source_rgb(1.0, 0.0, 0.0) + radius = [width, height].min + cr.arc(width / 2.0, height / 2.0, radius / 2.0 - 20, 0, 2 * Math::PI) + cr.stroke() + cr.arc(width / 2.0, height / 2.0, radius / 3.0 - 10, Math::PI / 3, 2 * Math::PI / 3) + cr.stroke() + + # draw lines + cr.set_source_rgb(0.0, 0.0, 0.8) + cr.move_to(width / 3.0, height / 3.0) + cr.rel_line_to(0, height / 6.0) + cr.move_to(2 * width / 3.0, height / 3.0) + cr.rel_line_to(0, height / 6.0) + cr.stroke() + end + end + + w=SmileyFace.new("Smiley Face") + w.show_all + w.set_size_request(75, 50) + Gtk.main + + + +{{br}}