]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/ScrolledWindow.py
wxUSE_STL/wxArrayString fixes.
[wxWidgets.git] / wxPython / demo / ScrolledWindow.py
1
2 import wx
3 import images
4
5 BUFFERED = 1
6
7 #---------------------------------------------------------------------------
8
9 class MyCanvas(wx.ScrolledWindow):
10 def __init__(self, parent, id = -1, size = wx.DefaultSize):
11 wx.ScrolledWindow.__init__(self, parent, id, (0, 0), size=size, style=wx.SUNKEN_BORDER)
12
13 self.lines = []
14 self.maxWidth = 1000
15 self.maxHeight = 1000
16 self.x = self.y = 0
17 self.curLine = []
18 self.drawing = False
19
20 self.SetBackgroundColour("WHITE")
21 self.SetCursor(wx.StockCursor(wx.CURSOR_PENCIL))
22 bmp = images.getTest2Bitmap()
23 mask = wx.Mask(bmp, wx.BLUE)
24 bmp.SetMask(mask)
25 self.bmp = bmp
26
27 self.SetScrollbars(20, 20, self.maxWidth/20, self.maxHeight/20)
28
29 if BUFFERED:
30 # Initialize the buffer bitmap. No real DC is needed at this point.
31 self.buffer = wx.EmptyBitmap(self.maxWidth, self.maxHeight)
32 dc = wx.BufferedDC(None, self.buffer)
33 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
34 dc.Clear()
35 self.DoDrawing(dc)
36
37 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftButtonEvent)
38 self.Bind(wx.EVT_LEFT_UP, self.OnLeftButtonEvent)
39 self.Bind(wx.EVT_MOTION, self.OnLeftButtonEvent)
40 self.Bind(wx.EVT_PAINT, self.OnPaint)
41
42
43 def getWidth(self):
44 return self.maxWidth
45
46 def getHeight(self):
47 return self.maxHeight
48
49
50 def OnPaint(self, event):
51 if BUFFERED:
52 # Create a buffered paint DC. It will create the real
53 # wx.PaintDC and then blit the bitmap to it when dc is
54 # deleted. Since we don't need to draw anything else
55 # here that's all there is to it.
56 dc = wx.BufferedPaintDC(self, self.buffer)
57 else:
58 dc = wx.PaintDC(self)
59 self.PrepareDC(dc)
60 # since we're not buffering in this case, we have to
61 # paint the whole window, potentially very time consuming.
62 self.DoDrawing(dc)
63
64
65 def DoDrawing(self, dc, printing=False):
66 dc.BeginDrawing()
67 dc.SetPen(wx.Pen('RED'))
68 dc.DrawRectangle(5, 5, 50, 50)
69
70 dc.SetBrush(wx.LIGHT_GREY_BRUSH)
71 dc.SetPen(wx.Pen('BLUE', 4))
72 dc.DrawRectangle(15, 15, 50, 50)
73
74 dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL))
75 dc.SetTextForeground(wx.Colour(0xFF, 0x20, 0xFF))
76 te = dc.GetTextExtent("Hello World")
77 dc.DrawText("Hello World", 60, 65)
78
79 dc.SetPen(wx.Pen('VIOLET', 4))
80 dc.DrawLine(5, 65+te[1], 60+te[0], 65+te[1])
81
82 lst = [(100,110), (150,110), (150,160), (100,160)]
83 dc.DrawLines(lst, -60)
84 dc.SetPen(wx.GREY_PEN)
85 dc.DrawPolygon(lst, 75)
86 dc.SetPen(wx.GREEN_PEN)
87 dc.DrawSpline(lst+[(100,100)])
88
89 dc.DrawBitmap(self.bmp, 200, 20, True)
90 dc.SetTextForeground(wx.Colour(0, 0xFF, 0x80))
91 dc.DrawText("a bitmap", 200, 85)
92
93 ## dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL))
94 ## dc.SetTextForeground("BLACK")
95 ## dc.DrawText("TEST this STRING", 10, 200)
96 ## print dc.GetFullTextExtent("TEST this STRING")
97
98 font = wx.Font(20, wx.SWISS, wx.NORMAL, wx.NORMAL)
99 dc.SetFont(font)
100 dc.SetTextForeground(wx.BLACK)
101
102 for a in range(0, 360, 45):
103 dc.DrawRotatedText("Rotated text...", 300, 300, a)
104
105 dc.SetPen(wx.TRANSPARENT_PEN)
106 dc.SetBrush(wx.BLUE_BRUSH)
107 dc.DrawRectangle(50,500, 50,50)
108 dc.DrawRectangle(100,500, 50,50)
109
110 dc.SetPen(wx.Pen('RED'))
111 dc.DrawEllipticArc(200,500, 50,75, 0, 90)
112
113 if not printing:
114 # This has troubles when used on a print preview in wxGTK,
115 # probably something to do with the pen styles and the scaling
116 # it does...
117 y = 20
118
119 for style in [wx.DOT, wx.LONG_DASH, wx.SHORT_DASH, wx.DOT_DASH, wx.USER_DASH]:
120 pen = wx.Pen("DARK ORCHID", 1, style)
121 if style == wx.USER_DASH:
122 pen.SetCap(wx.CAP_BUTT)
123 pen.SetDashes([1,2])
124 pen.SetColour("RED")
125 dc.SetPen(pen)
126 dc.DrawLine(300,y, 400,y)
127 y = y + 10
128
129 dc.SetBrush(wx.TRANSPARENT_BRUSH)
130 dc.SetPen(wx.Pen(wx.Colour(0xFF, 0x20, 0xFF), 1, wx.SOLID))
131 dc.DrawRectangle(450,50, 100,100)
132 old_pen = dc.GetPen()
133 new_pen = wx.Pen("BLACK", 5)
134 dc.SetPen(new_pen)
135 dc.DrawRectangle(470,70, 60,60)
136 dc.SetPen(old_pen)
137 dc.DrawRectangle(490,90, 20,20)
138
139 self.DrawSavedLines(dc)
140 dc.EndDrawing()
141
142
143 def DrawSavedLines(self, dc):
144 dc.SetPen(wx.Pen('MEDIUM FOREST GREEN', 4))
145
146 for line in self.lines:
147 for coords in line:
148 apply(dc.DrawLine, coords)
149
150
151 def SetXY(self, event):
152 self.x, self.y = self.ConvertEventCoords(event)
153
154 def ConvertEventCoords(self, event):
155 xView, yView = self.GetViewStart()
156 xDelta, yDelta = self.GetScrollPixelsPerUnit()
157 return (event.GetX() + (xView * xDelta),
158 event.GetY() + (yView * yDelta))
159
160 def OnLeftButtonEvent(self, event):
161 if event.LeftDown():
162 self.SetFocus()
163 self.SetXY(event)
164 self.curLine = []
165 self.CaptureMouse()
166 self.drawing = True
167
168 elif event.Dragging() and self.drawing:
169 if BUFFERED:
170 # If doing buffered drawing, create the buffered DC, giving it
171 # it a real DC to blit to when done.
172 cdc = wx.ClientDC(self)
173 self.PrepareDC(cdc)
174 dc = wx.BufferedDC(cdc, self.buffer)
175 else:
176 dc = wx.ClientDC(self)
177 self.PrepareDC(dc)
178
179 dc.BeginDrawing()
180 dc.SetPen(wx.Pen('MEDIUM FOREST GREEN', 4))
181 coords = (self.x, self.y) + self.ConvertEventCoords(event)
182 self.curLine.append(coords)
183 dc.DrawLine(*coords)
184 self.SetXY(event)
185 dc.EndDrawing()
186
187
188 elif event.LeftUp() and self.drawing:
189 self.lines.append(self.curLine)
190 self.curLine = []
191 self.ReleaseMouse()
192 self.drawing = False
193
194
195 ## This is an example of what to do for the EVT_MOUSEWHEEL event,
196 ## but since wx.ScrolledWindow does this already it's not
197 ## necessary to do it ourselves. You would need to add an event table
198 ## entry to __init__() to direct wheelmouse events to this handler.
199
200 ## wheelScroll = 0
201 ## def OnWheel(self, evt):
202 ## delta = evt.GetWheelDelta()
203 ## rot = evt.GetWheelRotation()
204 ## linesPer = evt.GetLinesPerAction()
205 ## print delta, rot, linesPer
206 ## ws = self.wheelScroll
207 ## ws = ws + rot
208 ## lines = ws / delta
209 ## ws = ws - lines * delta
210 ## self.wheelScroll = ws
211 ## if lines != 0:
212 ## lines = lines * linesPer
213 ## vsx, vsy = self.GetViewStart()
214 ## scrollTo = vsy - lines
215 ## self.Scroll(-1, scrollTo)
216
217 #---------------------------------------------------------------------------
218
219 def runTest(frame, nb, log):
220 win = MyCanvas(nb)
221 return win
222
223 #---------------------------------------------------------------------------
224
225
226
227 overview = """
228 <html>
229 <body>
230 The wx.ScrolledWindow class manages scrolling for its client area, transforming the
231 coordinates according to the scrollbar positions, and setting the scroll positions,
232 thumb sizes and ranges according to the area in view.
233 </body>
234 </html>
235 """
236
237
238 if __name__ == '__main__':
239 import sys,os
240 import run
241 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
242