]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/Mask.py
doc tweaks, typo fixed, etc.
[wxWidgets.git] / wxPython / demo / Mask.py
1 # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 import wx
7
8 #----------------------------------------------------------------------
9
10 logicList = [
11 ('wx.AND', wx.AND),
12 ('wx.AND_INVERT', wx.AND_INVERT),
13 ('wx.AND_REVERSE', wx.AND_REVERSE),
14 ('wx.CLEAR', wx.CLEAR),
15 ('wx.COPY', wx.COPY),
16 ('wx.EQUIV', wx.EQUIV),
17 ('wx.INVERT', wx.INVERT),
18 ('wx.NAND', wx.NAND),
19
20 # this one causes an assert on wxGTK, and doesn't seem to
21 # do much on MSW anyway, so I'll just take it out....
22 #('wxNOR', wxNOR),
23
24 ('wx.NO_OP', wx.NO_OP),
25 ('wx.OR', wx.OR),
26 ('wx.OR_INVERT', wx.OR_INVERT),
27 ('wx.OR_REVERSE', wx.OR_REVERSE),
28 ('wx.SET', wx.SET),
29 ('wx.SRC_INVERT', wx.SRC_INVERT),
30 ('wx.XOR', wx.XOR),
31 ]
32
33 import images
34
35 class TestMaskWindow(wx.ScrolledWindow):
36 def __init__(self, parent):
37 wx.ScrolledWindow.__init__(self, parent, -1)
38 self.SetBackgroundColour(wx.Colour(0,128,0))
39
40 # A reference bitmap that we won't mask
41 self.bmp_nomask = images.getTestStar2Bitmap()
42
43 # One that we will
44 self.bmp_withmask = images.getTestStar2Bitmap()
45
46 # this mask comes from a monochrome bitmap
47 self.bmp_themask = wx.BitmapFromImage(images.getTestMaskImage(), 1)
48 mask = wx.Mask(self.bmp_themask)
49
50 # set the mask on our bitmap
51 self.bmp_withmask.SetMask(mask)
52
53 # Now we'll create a mask in a bit of an easier way, by picking a
54 # colour in the image that is to be the transparent colour.
55 self.bmp_withcolourmask = images.getTestStar2Bitmap()
56 mask = wx.MaskColour(self.bmp_withcolourmask, wx.WHITE)
57 self.bmp_withcolourmask.SetMask(mask)
58
59 self.SetScrollbars(20, 20, 700/20, 460/20)
60
61 self.Bind(wx.EVT_PAINT, self.OnPaint)
62
63
64 def OnPaint (self, e):
65 dc = wx.PaintDC(self)
66 self.PrepareDC(dc)
67 dc.SetTextForeground(wx.WHITE)
68
69 # make an interesting background...
70 dc.SetPen(wx.MEDIUM_GREY_PEN)
71 for i in range(100):
72 dc.DrawLine((0,i*10), (i*10,0))
73
74 # draw raw image, mask, and masked images
75 dc.DrawText('original image', (0,0))
76 dc.DrawBitmap(self.bmp_nomask, (0,20), 0)
77 dc.DrawText('with colour mask', (0,100))
78 dc.DrawBitmap(self.bmp_withcolourmask, (0,120), 1)
79 dc.DrawText('the mask image', (0,200))
80 dc.DrawBitmap(self.bmp_themask, (0,220), 0)
81 dc.DrawText('masked image', (0,300))
82 dc.DrawBitmap(self.bmp_withmask, (0,320), 1)
83
84 cx,cy = self.bmp_themask.GetWidth(), self.bmp_themask.GetHeight()
85
86 # draw array of assorted blit operations
87 mdc = wx.MemoryDC()
88 i = 0
89
90 for text, code in logicList:
91 x,y = 120+150*(i%4), 20+100*(i/4)
92 dc.DrawText(text, (x, y-20))
93 mdc.SelectObject(self.bmp_withcolourmask)
94 dc.Blit((x,y), (cx,cy), mdc, (0,0), code, True)
95 i = i + 1
96
97
98 # On wxGTK there needs to be a panel under wx.ScrolledWindows if they are
99 # going to be in a wxNotebook...
100 class TestPanel(wx.Panel):
101 def __init__(self, parent, ID):
102 wx.Panel.__init__(self, parent, ID)
103 self.win = TestMaskWindow(self)
104 self.Bind(wx.EVT_SIZE, self.OnSize)
105
106 def OnSize(self, evt):
107 self.win.SetSize(evt.GetSize())
108
109
110 #----------------------------------------------------------------------
111
112 def runTest(frame, nb, log):
113 win = TestPanel(nb, -1)
114 return win
115
116 #----------------------------------------------------------------------
117
118
119 overview = """\
120 This class encapsulates a monochrome mask bitmap, where the masked area is black
121 and the unmasked area is white. When associated with a bitmap and drawn in a device
122 context, the unmasked area of the bitmap will be drawn, and the masked area will
123 not be drawn.
124
125 This example shows not only how to create a Mask, but the effects of the Device
126 Context (dc) <code>Blit()</code> method's logic codes.
127 """
128
129
130
131 if __name__ == '__main__':
132 import sys,os
133 import run
134 run.main(['', os.path.basename(sys.argv[0])])
135