source: SHX/trunk/sandbox/wx-overlay.py @ 215

Revision 215, 2.5 KB checked in by marcus, 10 years ago (diff)

added overlay example

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Rev Id Date
Line 
1"""
2A simple sample of using a wx.Overlay to draw a rubberband effect
3"""
4
5import wx
6print wx.version()
7
8class TestPanel(wx.Panel):
9    def __init__(self, *args, **kw):
10        wx.Panel.__init__(self, *args, **kw)
11
12        self.Bind(wx.EVT_PAINT, self.OnPaint)
13        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
14        self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
15        self.Bind(wx.EVT_MOTION, self.OnMouseMove)
16
17        self.startPos = None
18        self.overlay = wx.Overlay()
19
20
21    def OnPaint(self, evt):
22        # Just some simple stuff to paint in the window for an example
23        dc = wx.PaintDC(self)
24        coords = ((40,40),(200,220),(210,120),(120,300))
25        dc.SetBackground(wx.Brush("sky blue"))
26        dc.Clear()
27        dc.SetPen(wx.Pen("red", 2))
28        dc.SetBrush(wx.CYAN_BRUSH)
29        dc.DrawPolygon(coords)
30        dc.DrawLabel("Draw the mouse across this window to see \n"
31                    "a rubber-band effect using wx.Overlay",
32                    (140, 50, -1, -1))
33
34
35    def OnLeftDown(self, evt):
36        # Capture the mouse and save the starting posiiton for the
37        # rubber-band
38        self.CaptureMouse()
39        self.startPos = evt.GetPosition()
40
41
42    def OnMouseMove(self, evt):
43        if evt.Dragging() and evt.LeftIsDown():
44            rect = wx.RectPP(self.startPos, evt.GetPosition())
45
46            # Draw the rubber-band rectangle using an overlay so it
47            # will manage keeping the rectangle and the former window
48            # contents separate.
49            dc = wx.ClientDC(self)
50            odc = wx.DCOverlay(self.overlay, dc)
51            odc.Clear()
52
53            dc.SetPen(wx.Pen("black", 2))
54            if 'wxMac' in wx.PlatformInfo:
55                dc.SetBrush(wx.Brush(wx.Colour(0xC0, 0xC0, 0xC0, 0x80)))
56            else:
57                dc.SetBrush(wx.TRANSPARENT_BRUSH)
58            dc.DrawRectangleRect(rect)
59
60            del odc # work around a bug in the Python wrappers to make
61                    # sure the odc is destroyed before the dc is.
62
63
64    def OnLeftUp(self, evt):
65        if self.HasCapture():
66            self.ReleaseMouse()
67        self.startPos = None
68
69        # When the mouse is released we reset the overlay and it
70        # restores the former content to the window.
71        dc = wx.ClientDC(self)
72        odc = wx.DCOverlay(self.overlay, dc)
73        odc.Clear()
74        del odc
75        self.overlay.Reset()
76
77
78
79app = wx.App(redirect=False)
80frm = wx.Frame(None, title="wx.Overlay Test", size=(450,450))
81pnl = TestPanel(frm)
82frm.Show()
83app.MainLoop()
84
Note: See TracBrowser for help on using the repository browser.