]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/ShapedWindow.py
doc tweaks, typo fixed, etc.
[wxWidgets.git] / wxPython / demo / ShapedWindow.py
1 # 11/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 import wx
7 import images
8
9 #----------------------------------------------------------------------
10
11 class TestFrame(wx.Frame):
12 def __init__(self, parent, log):
13 self.log = log
14 wx.Frame.__init__(self, parent, -1, "Shaped Window",
15 style =
16 wx.FRAME_SHAPED
17 | wx.SIMPLE_BORDER
18 | wx.FRAME_NO_TASKBAR
19 | wx.STAY_ON_TOP
20 )
21
22 self.hasShape = False
23 self.delta = (0,0)
24
25 self.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
26 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
27 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
28 self.Bind(wx.EVT_MOTION, self.OnMouseMove)
29 self.Bind(wx.EVT_RIGHT_UP, self.OnExit)
30 self.Bind(wx.EVT_PAINT, self.OnPaint)
31
32 self.bmp = images.getTuxBitmap()
33 w, h = self.bmp.GetWidth(), self.bmp.GetHeight()
34 self.SetClientSize( (w, h) )
35
36 if wx.Platform != "__WXMAC__":
37 # wxMac clips the tooltip to the window shape, YUCK!!!
38 self.SetToolTipString("Right-click to close the window\n"
39 "Double-click the image to set/unset the window shape")
40
41 if wx.Platform == "__WXGTK__":
42 # wxGTK requires that the window be created before you can
43 # set its shape, so delay the call to SetWindowShape until
44 # this event.
45 self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
46 else:
47 # On wxMSW and wxMac the window has already been created, so go for it.
48 self.SetWindowShape()
49
50 dc = wx.ClientDC(self)
51 dc.DrawBitmap(self.bmp, (0,0), True)
52
53
54 def SetWindowShape(self, *evt):
55 # Use the bitmap's mask to determine the region
56 r = wx.RegionFromBitmap(self.bmp)
57 self.hasShape = self.SetShape(r)
58
59
60 def OnDoubleClick(self, evt):
61 if self.hasShape:
62 self.SetShape(wx.Region())
63 self.hasShape = False
64 else:
65 self.SetWindowShape()
66
67
68 def OnPaint(self, evt):
69 dc = wx.PaintDC(self)
70 dc.DrawBitmap(self.bmp, (0,0), True)
71
72 def OnExit(self, evt):
73 self.Close()
74
75
76 def OnLeftDown(self, evt):
77 self.CaptureMouse()
78 x, y = self.ClientToScreen(evt.GetPosition())
79 originx, originy = self.GetPosition()
80 dx = x - originx
81 dy = y - originy
82 self.delta = ((dx, dy))
83
84
85 def OnLeftUp(self, evt):
86 if self.HasCapture():
87 self.ReleaseMouse()
88
89
90 def OnMouseMove(self, evt):
91 if evt.Dragging() and evt.LeftIsDown():
92 x, y = self.ClientToScreen(evt.GetPosition())
93 fp = (x - self.delta[0], y - self.delta[1])
94 self.Move(fp)
95
96
97 #----------------------------------------------------------------------
98
99 def runTest(frame, nb, log):
100 win = TestFrame(nb, log)
101 frame.otherWin = win
102 win.Show(True)
103
104
105 #----------------------------------------------------------------------
106
107
108
109 overview = """<html><body>
110 <h2><center>Shaped Window</center></h2>
111
112 Top level windows now have a SetShape method that lets you set a
113 non-rectangular shape for the window using a wxRegion. All pixels
114 outside of the region will not be drawn and the window will not be
115 sensitive to the mouse in those areas either.
116
117 </body></html>
118 """
119
120
121
122 if __name__ == '__main__':
123 import sys,os
124 import run
125 run.main(['', os.path.basename(sys.argv[0])])
126