root/wxPython/branches/WX_2_8_BRANCH/sandbox/test_EmptyBitmapRGBA.py

Revision 56198, 2.6 kB (checked in by RD, 3 months ago)

Try alternate approaches to creating the bitmap

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1import wx
2print wx.version()
3
4USE_CAIRO = False
5if USE_CAIRO:
6    import wx.lib.wxcairo
7    import cairo
8
9DIM = 100
10POS = 20
11
12class TestPanel(wx.Panel):
13    def __init__(self, *args, **kw):
14        wx.Panel.__init__(self, *args, **kw)
15
16        self.Bind(wx.EVT_PAINT, self.OnPaint)
17
18        self.bmps = []
19        self.bmps.append( self.MakeBitmap(blue=128, alpha=128) )
20        self.bmps.append( self.MakeBitmap() )
21        self.bmps.append( self.MakeBitmap(blue=255, alpha=255) )
22
23
24    def MakeBitmap(self, red=0, green=0, blue=0, alpha=0):
25        if not USE_CAIRO:
26            # This one works fine on Mac and Windows, but not wxGTK...
27            bmp = wx.EmptyBitmapRGBA(DIM, DIM, red, green, blue, alpha)
28            dc = wx.MemoryDC(bmp)
29            gc = wx.GraphicsContext.Create(dc)           
30            path = gc.CreatePath()
31            path.MoveToPoint(POS,POS)
32            path.AddLineToPoint(DIM-POS, DIM-POS)
33            gc.SetPen(wx.Pen("red", 8))
34            gc.StrokePath(path)
35           
36        else:
37            # try out a couple possible workarounds
38            sfc = cairo.ImageSurface(cairo.FORMAT_ARGB32, DIM, DIM)
39            ctx = cairo.Context(sfc)
40            if False:
41                # use straight cairo
42                ctx.set_source_rgba(red/255.0, green/255.0, blue/255.0, alpha/255.0)
43                ctx.paint()
44                ctx.set_line_width(8)
45                ctx.set_line_cap(cairo.LINE_CAP_ROUND)
46                ctx.set_source_rgb(1, 0, 0)
47                ctx.move_to(POS, POS)
48                ctx.line_to(DIM-POS, DIM-POS)
49                ctx.stroke()
50            else:
51                # use our GC-like wrapper
52                import wx.lib.graphics as g
53                gc = g.GraphicsContext.CreateFromNative(ctx)
54                gc.Clear(wx.Colour(red, green, blue, alpha))
55                path = gc.CreatePath()
56                path.MoveToPoint(POS,POS)
57                path.AddLineToPoint(DIM-POS, DIM-POS)
58                gc.SetPen(wx.Pen("red", 8))
59                gc.StrokePath(path)
60            bmp = wx.lib.wxcairo.BitmapFromImageSurface(sfc)
61
62        return bmp
63   
64       
65    def OnPaint(self, evt):
66        dc = wx.PaintDC(self)
67        sz = self.GetSize()
68        dc.SetPen(wx.Pen("navy", 1))
69        x = y = 0
70        while x < sz.width * 2 or y < sz.height * 2:
71            x += 20
72            y += 20
73            dc.DrawLine(x, 0, 0, y)
74
75        x, y = 25,25
76        for bmp in self.bmps:
77            dc.DrawBitmap(bmp, x, y, True)
78            x += DIM + 25
79       
80       
81       
82app = wx.App(redirect=False)
83frm = wx.Frame(None, title="EmptyBitmapRGBA")
84pnl = TestPanel(frm)
85frm.Show()
86app.MainLoop()
Note: See TracBrowser for help on using the browser.