~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/demo/Cairo.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import wx
 
3
import math
 
4
 
 
5
try:
 
6
    import wx.lib.wxcairo
 
7
    import cairo
 
8
    haveCairo = True
 
9
except ImportError:
 
10
    haveCairo = False
 
11
 
 
12
from Main import opj
 
13
 
 
14
#----------------------------------------------------------------------
 
15
 
 
16
 
 
17
class TestPanel(wx.Panel):
 
18
    def __init__(self, parent, log):
 
19
        self.log = log
 
20
        wx.Panel.__init__(self, parent, -1)
 
21
 
 
22
        self.Bind(wx.EVT_PAINT, self.OnPaint)
 
23
 
 
24
 
 
25
    def OnPaint(self, evt):
 
26
        #dc = wx.PaintDC(self)
 
27
        dc = wx.BufferedPaintDC(self)
 
28
        dc.SetBackground(wx.Brush('white'))
 
29
        dc.Clear()
 
30
        
 
31
        self.Render(dc)
 
32
 
 
33
 
 
34
    def Render(self, dc):
 
35
        # Draw some stuff on the plain dc
 
36
        sz = self.GetSize()
 
37
        dc.SetPen(wx.Pen("navy", 1))
 
38
        x = y = 0
 
39
        while x < sz.width * 2 or y < sz.height * 2:
 
40
            x += 20
 
41
            y += 20
 
42
            dc.DrawLine(x, 0, 0, y)
 
43
        
 
44
        # now draw something with cairo
 
45
        ctx = wx.lib.wxcairo.ContextFromDC(dc)
 
46
        ctx.set_line_width(15)
 
47
        ctx.move_to(125, 25)
 
48
        ctx.line_to(225, 225)
 
49
        ctx.rel_line_to(-200, 0)
 
50
        ctx.close_path()
 
51
        ctx.set_source_rgba(0, 0, 0.5, 1)
 
52
        ctx.stroke()
 
53
 
 
54
        # and something else...
 
55
        ctx.arc(200, 200, 80, 0, math.pi*2)
 
56
        ctx.set_source_rgba(0, 1, 1, 0.5)
 
57
        ctx.fill_preserve()
 
58
        ctx.set_source_rgb(1, 0.5, 0)
 
59
        ctx.stroke()
 
60
 
 
61
        # here's a gradient pattern
 
62
        ptn = cairo.RadialGradient(315, 70, 25,
 
63
                                   302, 70, 128)
 
64
        ptn.add_color_stop_rgba(0, 1,1,1,1)
 
65
        ptn.add_color_stop_rgba(1, 0,0,0,1)
 
66
        ctx.set_source(ptn)
 
67
        ctx.arc(328, 96, 75, 0, math.pi*2)
 
68
        ctx.fill()
 
69
 
 
70
        # Draw some text
 
71
        face = wx.lib.wxcairo.FontFaceFromFont(
 
72
            wx.FFont(10, wx.SWISS, wx.FONTFLAG_BOLD))
 
73
        ctx.set_font_face(face)
 
74
        ctx.set_font_size(60)
 
75
        ctx.move_to(360, 180)
 
76
        ctx.set_source_rgb(0, 0, 0)
 
77
        ctx.show_text("Hello")
 
78
 
 
79
        # Text as a path, with fill and stroke
 
80
        ctx.move_to(400, 220)
 
81
        ctx.text_path("World")
 
82
        ctx.set_source_rgb(0.39, 0.07, 0.78)
 
83
        ctx.fill_preserve()
 
84
        ctx.set_source_rgb(0,0,0)
 
85
        ctx.set_line_width(2)
 
86
        ctx.stroke()
 
87
 
 
88
        # Show iterating and modifying a (text) path
 
89
        ctx.new_path()
 
90
        ctx.move_to(0, 0)
 
91
        ctx.set_source_rgb(0.3, 0.3, 0.3)
 
92
        ctx.set_font_size(30)
 
93
        text = "This path was warped..."
 
94
        ctx.text_path(text)
 
95
        tw, th = ctx.text_extents(text)[2:4]
 
96
        self.warpPath(ctx, tw, th, 360,300)
 
97
        ctx.fill()
 
98
 
 
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'))
 
103
 
 
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)
 
114
        
 
115
        ctx.set_source_surface(img, 70, 230)
 
116
        ctx.paint()
 
117
 
 
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)
 
121
        
 
122
        
 
123
    def warpPath(self, ctx, tw, th, dx, dy):
 
124
        def f(x, y):
 
125
            xn = x - tw/2
 
126
            yn = y+ xn ** 3 / ((tw/2)**3) * 70
 
127
            return xn+dx, yn+dy
 
128
 
 
129
        path = ctx.copy_path()
 
130
        ctx.new_path()
 
131
        for type, points in path:
 
132
            if type == cairo.PATH_MOVE_TO:
 
133
                x, y = f(*points)
 
134
                ctx.move_to(x, y)
 
135
 
 
136
            elif type == cairo.PATH_LINE_TO:
 
137
                x, y = f(*points)
 
138
                ctx.line_to(x, y)
 
139
 
 
140
            elif type == cairo.PATH_CURVE_TO:
 
141
                x1, y1, x2, y2, x3, y3 = points
 
142
                x1, y1 = f(x1, y1)
 
143
                x2, y2 = f(x2, y2)
 
144
                x3, y3 = f(x3, y3)
 
145
                ctx.curve_to(x1, y1, x2, y2, x3, y3)
 
146
 
 
147
            elif type == cairo.PATH_CLOSE_PATH:
 
148
                ctx.close_path()
 
149
        
 
150
#----------------------------------------------------------------------
 
151
 
 
152
if not haveCairo:
 
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)
 
158
        return win
 
159
else:
 
160
    
 
161
    def runTest(frame, nb, log):
 
162
        win = TestPanel(nb, log)
 
163
        return win
 
164
 
 
165
#----------------------------------------------------------------------
 
166
 
 
167
if haveCairo:
 
168
    extra = "\n<h3>wx.lib.wxcairo</h3>\n%s" % (
 
169
        wx.lib.wxcairo.__doc__.replace('\n\n', '\n<p>'))
 
170
else:
 
171
    extra = '\n<p>See the docstring in the wx.lib.wxcairo module for details about installing dependencies.'
 
172
 
 
173
 
 
174
 
 
175
overview = """<html><body>
 
176
<h2><center>Cairo Integration</center></h2>
 
177
 
 
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/.
 
182
 
 
183
%s
 
184
 
 
185
</body></html>
 
186
""" % extra
 
187
 
 
188
 
 
189
 
 
190
if __name__ == '__main__':
 
191
    import sys,os
 
192
    import run
 
193
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
 
194