[cairo-commit] [cairo-www] src/quickframework.mdwn
Carl Worth
cworth at freedesktop.org
Tue Nov 6 06:11:47 PST 2007
src/quickframework.mdwn | 66 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+)
New commits:
commit f218c94611e21afd230be6f8b17977f4422afb0a
Author: Donn <donn.ingle at gmail.com>
Date: Tue Nov 6 06:11:47 2007 -0800
Donn
diff --git a/src/quickframework.mdwn b/src/quickframework.mdwn
new file mode 100644
index 0000000..91358c2
--- /dev/null
+++ b/src/quickframework.mdwn
@@ -0,0 +1,66 @@
+##A Quick GTK GNU/Linux Framework
+
+I can't recall where I got this online, but it's handy for running snippets of cairo code to see what's going on.
+
+
+ #! /usr/bin/env python
+ import pygtk
+ pygtk.require('2.0')
+ import gtk, gobject, cairo
+
+ # Create a GTK+ widget on which we will draw using Cairo
+ class Screen(gtk.DrawingArea):
+
+ # Draw in response to an expose-event
+ __gsignals__ = { "expose-event": "override" }
+
+ # Handle the expose-event by drawing
+ def do_expose_event(self, event):
+
+ # Create the cairo context
+ cr = self.window.cairo_create()
+
+ # Restrict Cairo to the exposed area; avoid extra work
+ cr.rectangle(event.area.x, event.area.y,
+ event.area.width, event.area.height)
+ cr.clip()
+
+ self.draw(cr, *self.window.get_size())
+
+ def draw(self, cr, width, height):
+ # Fill the background with gray
+ cr.set_source_rgb(0.5, 0.5, 0.5)
+ cr.rectangle(0, 0, width, height)
+ cr.fill()
+
+ # GTK mumbo-jumbo to show the widget in a window and quit when it's closed
+ def run(Widget):
+ window = gtk.Window()
+ window.connect("delete-event", gtk.main_quit)
+ widget = Widget()
+ widget.show()
+ window.add(widget)
+ window.present()
+ gtk.main()
+
+ #######################
+ ## Do all your testing in Shapes ##
+ #######################
+
+ class Shapes(Screen):
+ def draw(self, cr, width, height):
+
+ ## This will draw using a mask.
+ cr.scale(width,height)
+ self.linear = cairo.LinearGradient(0, 0, 1, 1)
+ self.linear.add_color_stop_rgb(0, 0, 0.3, 0.8)
+ self.linear.add_color_stop_rgb(1, 0, 0.8, 0.3)
+
+ self.radial = cairo.RadialGradient(0.5, 0.5, 0.25, 0.5, 0.5, 0.5)
+ self.radial.add_color_stop_rgba(0, 0, 0, 0, 1)
+ self.radial.add_color_stop_rgba(0.5, 0, 0, 0, 0)
+
+ cr.set_source(self.linear)
+ cr.mask(self.radial)
+
+ run(Shapes)
More information about the cairo-commit
mailing list