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