14
#----------------------------------------------------------------------
17
class TestPanel(wx.Panel):
18
def __init__(self, parent, log):
20
wx.Panel.__init__(self, parent, -1)
22
self.Bind(wx.EVT_PAINT, self.OnPaint)
25
def OnPaint(self, evt):
26
#dc = wx.PaintDC(self)
27
dc = wx.BufferedPaintDC(self)
28
dc.SetBackground(wx.Brush('white'))
35
# Draw some stuff on the plain dc
37
dc.SetPen(wx.Pen("navy", 1))
39
while x < sz.width * 2 or y < sz.height * 2:
42
dc.DrawLine(x, 0, 0, y)
44
# now draw something with cairo
45
ctx = wx.lib.wxcairo.ContextFromDC(dc)
46
ctx.set_line_width(15)
49
ctx.rel_line_to(-200, 0)
51
ctx.set_source_rgba(0, 0, 0.5, 1)
54
# and something else...
55
ctx.arc(200, 200, 80, 0, math.pi*2)
56
ctx.set_source_rgba(0, 1, 1, 0.5)
58
ctx.set_source_rgb(1, 0.5, 0)
61
# here's a gradient pattern
62
ptn = cairo.RadialGradient(315, 70, 25,
64
ptn.add_color_stop_rgba(0, 1,1,1,1)
65
ptn.add_color_stop_rgba(1, 0,0,0,1)
67
ctx.arc(328, 96, 75, 0, math.pi*2)
71
face = wx.lib.wxcairo.FontFaceFromFont(
72
wx.FFont(10, wx.SWISS, wx.FONTFLAG_BOLD))
73
ctx.set_font_face(face)
76
ctx.set_source_rgb(0, 0, 0)
77
ctx.show_text("Hello")
79
# Text as a path, with fill and stroke
81
ctx.text_path("World")
82
ctx.set_source_rgb(0.39, 0.07, 0.78)
84
ctx.set_source_rgb(0,0,0)
88
# Show iterating and modifying a (text) path
91
ctx.set_source_rgb(0.3, 0.3, 0.3)
93
text = "This path was warped..."
95
tw, th = ctx.text_extents(text)[2:4]
96
self.warpPath(ctx, tw, th, 360,300)
99
# Drawing a bitmap. Note that we can easily load a PNG file
100
# into a surface, but I wanted to show how to convert from a
101
# wx.Bitmap here instead. This is how to do it using just cairo:
102
#img = cairo.ImageSurface.create_from_png(opj('bitmaps/toucan.png'))
104
# And this is how to convert a wx.Btmap to a cairo image
105
# surface. NOTE: currently on Mac there appears to be a
106
# problem using conversions of some types of images. They
107
# show up totally transparent when used. The conversion itself
108
# appears to be working okay, because converting back to
109
# wx.Bitmap or writing the image surface to a file produces
110
# the expected result. The other platforms are okay.
111
bmp = wx.Bitmap(opj('bitmaps/toucan.png'))
112
#bmp = wx.Bitmap(opj('bitmaps/splash.png'))
113
img = wx.lib.wxcairo.ImageSurfaceFromBitmap(bmp)
115
ctx.set_source_surface(img, 70, 230)
118
# this is how to convert an image surface to a wx.Bitmap
119
bmp2 = wx.lib.wxcairo.BitmapFromImageSurface(img)
120
dc.DrawBitmap(bmp2, 280, 300)
123
def warpPath(self, ctx, tw, th, dx, dy):
126
yn = y+ xn ** 3 / ((tw/2)**3) * 70
129
path = ctx.copy_path()
131
for type, points in path:
132
if type == cairo.PATH_MOVE_TO:
136
elif type == cairo.PATH_LINE_TO:
140
elif type == cairo.PATH_CURVE_TO:
141
x1, y1, x2, y2, x3, y3 = points
145
ctx.curve_to(x1, y1, x2, y2, x3, y3)
147
elif type == cairo.PATH_CLOSE_PATH:
150
#----------------------------------------------------------------------
153
from wx.lib.msgpanel import MessagePanel
154
def runTest(frame, nb, log):
155
win = MessagePanel(nb, 'This demo requires the Pycairo package,\n'
156
'or there is some other unmet dependency.',
157
'Sorry', wx.ICON_WARNING)
161
def runTest(frame, nb, log):
162
win = TestPanel(nb, log)
165
#----------------------------------------------------------------------
168
extra = "\n<h3>wx.lib.wxcairo</h3>\n%s" % (
169
wx.lib.wxcairo.__doc__.replace('\n\n', '\n<p>'))
171
extra = '\n<p>See the docstring in the wx.lib.wxcairo module for details about installing dependencies.'
175
overview = """<html><body>
176
<h2><center>Cairo Integration</center></h2>
178
This sample shows how to draw on a DC using the cairo 2D graphics
179
library and the pycairo Python extension module that wraps the cairo
180
API. For more information about cairo please see
181
http://cairographics.org/.
190
if __name__ == '__main__':
193
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])