]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | # 11/12/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
2 | # | |
3 | # o Updated for wx namespace | |
4 | # o Rudimentary overview doc added. | |
5 | # | |
cf694132 | 6 | |
8fa876ca | 7 | import wx |
cf694132 | 8 | |
8fa876ca | 9 | import images |
96bfd053 | 10 | |
d3bfec74 RD |
11 | BUFFERED = 1 |
12 | ||
cf694132 RD |
13 | #--------------------------------------------------------------------------- |
14 | ||
8fa876ca | 15 | class MyCanvas(wx.ScrolledWindow): |
372bde9b | 16 | def __init__(self, parent, id = -1, size = wx.DefaultSize): |
8fa876ca | 17 | wx.ScrolledWindow.__init__(self, parent, id, (0, 0), size=size, style=wx.SUNKEN_BORDER) |
cf694132 RD |
18 | |
19 | self.lines = [] | |
bb0054cd RD |
20 | self.maxWidth = 1000 |
21 | self.maxHeight = 1000 | |
d3bfec74 RD |
22 | self.x = self.y = 0 |
23 | self.curLine = [] | |
1e4a197e | 24 | self.drawing = False |
cf694132 | 25 | |
d43d449b | 26 | self.SetBackgroundColour("WHITE") |
8fa876ca | 27 | self.SetCursor(wx.StockCursor(wx.CURSOR_PENCIL)) |
96bfd053 | 28 | bmp = images.getTest2Bitmap() |
8fa876ca | 29 | mask = wx.MaskColour(bmp, wx.BLUE) |
5ed3dab2 | 30 | bmp.SetMask(mask) |
bb0054cd | 31 | self.bmp = bmp |
cf694132 | 32 | |
bb0054cd RD |
33 | self.SetScrollbars(20, 20, self.maxWidth/20, self.maxHeight/20) |
34 | ||
d3bfec74 RD |
35 | if BUFFERED: |
36 | # Initialize the buffer bitmap. No real DC is needed at this point. | |
8fa876ca RD |
37 | self.buffer = wx.EmptyBitmap(self.maxWidth, self.maxHeight) |
38 | dc = wx.BufferedDC(None, self.buffer) | |
39 | dc.SetBackground(wx.Brush(self.GetBackgroundColour())) | |
d3bfec74 RD |
40 | dc.Clear() |
41 | self.DoDrawing(dc) | |
42 | ||
8fa876ca RD |
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) | |
0af45411 | 47 | |
a3015b16 | 48 | |
bb0054cd RD |
49 | def getWidth(self): |
50 | return self.maxWidth | |
51 | ||
52 | def getHeight(self): | |
53 | return self.maxHeight | |
cf694132 RD |
54 | |
55 | ||
56 | def OnPaint(self, event): | |
d3bfec74 RD |
57 | if BUFFERED: |
58 | # Create a buffered paint DC. It will create the real | |
8fa876ca | 59 | # wx.PaintDC and then blit the bitmap to it when dc is |
d3bfec74 RD |
60 | # deleted. Since we don't need to draw anything else |
61 | # here that's all there is to it. | |
8fa876ca | 62 | dc = wx.BufferedPaintDC(self, self.buffer) |
d3bfec74 | 63 | else: |
8fa876ca | 64 | dc = wx.PaintDC(self) |
d3bfec74 RD |
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) | |
cf694132 RD |
69 | |
70 | ||
1fded56b | 71 | def DoDrawing(self, dc, printing=False): |
cf694132 | 72 | dc.BeginDrawing() |
8fa876ca | 73 | dc.SetPen(wx.Pen('RED')) |
d14a1e28 | 74 | dc.DrawRectangle((5, 5), (50, 50)) |
cf694132 | 75 | |
8fa876ca RD |
76 | dc.SetBrush(wx.LIGHT_GREY_BRUSH) |
77 | dc.SetPen(wx.Pen('BLUE', 4)) | |
d14a1e28 | 78 | dc.DrawRectangle((15, 15), (50, 50)) |
cf694132 | 79 | |
8fa876ca RD |
80 | dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL)) |
81 | dc.SetTextForeground(wx.Colour(0xFF, 0x20, 0xFF)) | |
cf694132 | 82 | te = dc.GetTextExtent("Hello World") |
d14a1e28 | 83 | dc.DrawText("Hello World", (60, 65)) |
cf694132 | 84 | |
8fa876ca | 85 | dc.SetPen(wx.Pen('VIOLET', 4)) |
d14a1e28 | 86 | dc.DrawLine((5, 65+te[1]), (60+te[0], 65+te[1])) |
cf694132 RD |
87 | |
88 | lst = [(100,110), (150,110), (150,160), (100,160)] | |
89 | dc.DrawLines(lst, -60) | |
8fa876ca | 90 | dc.SetPen(wx.GREY_PEN) |
cf694132 | 91 | dc.DrawPolygon(lst, 75) |
8fa876ca | 92 | dc.SetPen(wx.GREEN_PEN) |
cf694132 RD |
93 | dc.DrawSpline(lst+[(100,100)]) |
94 | ||
d14a1e28 | 95 | dc.DrawBitmap(self.bmp, (200, 20), True) |
8fa876ca | 96 | dc.SetTextForeground(wx.Colour(0, 0xFF, 0x80)) |
d14a1e28 | 97 | dc.DrawText("a bitmap", (200, 85)) |
cf694132 | 98 | |
8fa876ca | 99 | ## dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL)) |
1fded56b | 100 | ## dc.SetTextForeground("BLACK") |
8fa876ca | 101 | ## dc.DrawText("TEST this STRING", (10, 200)) |
1fded56b RD |
102 | ## print dc.GetFullTextExtent("TEST this STRING") |
103 | ||
8fa876ca | 104 | font = wx.Font(20, wx.SWISS, wx.NORMAL, wx.NORMAL) |
6999b0d8 | 105 | dc.SetFont(font) |
8fa876ca RD |
106 | dc.SetTextForeground(wx.BLACK) |
107 | ||
6999b0d8 | 108 | for a in range(0, 360, 45): |
d14a1e28 | 109 | dc.DrawRotatedText("Rotated text...", (300, 300), a) |
6999b0d8 | 110 | |
8fa876ca RD |
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)) | |
eec92d76 | 115 | |
8fa876ca | 116 | dc.SetPen(wx.Pen('RED')) |
d14a1e28 | 117 | dc.DrawEllipticArc((200, 500), (50, 75), 0, 90) |
c368d904 | 118 | |
1fded56b RD |
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 | |
8fa876ca RD |
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) | |
1fded56b RD |
129 | pen.SetDashes([1,2]) |
130 | pen.SetColour("RED") | |
131 | dc.SetPen(pen) | |
8fa876ca | 132 | dc.DrawLine((300, y), (400, y)) |
1fded56b | 133 | y = y + 10 |
217cb2fa | 134 | |
8fa876ca RD |
135 | dc.SetBrush(wx.TRANSPARENT_BRUSH) |
136 | dc.SetPen(wx.Pen(wx.Colour(0xFF, 0x20, 0xFF), 1, wx.SOLID)) | |
d14a1e28 | 137 | dc.DrawRectangle((450, 50), (100, 100)) |
c5943253 | 138 | old_pen = dc.GetPen() |
8fa876ca | 139 | new_pen = wx.Pen("BLACK", 5) |
c5943253 | 140 | dc.SetPen(new_pen) |
d14a1e28 | 141 | dc.DrawRectangle((470, 70), (60, 60)) |
c5943253 | 142 | dc.SetPen(old_pen) |
d14a1e28 | 143 | dc.DrawRectangle((490, 90), (20, 20)) |
c5943253 | 144 | |
cf694132 RD |
145 | self.DrawSavedLines(dc) |
146 | dc.EndDrawing() | |
147 | ||
148 | ||
149 | def DrawSavedLines(self, dc): | |
8fa876ca RD |
150 | dc.SetPen(wx.Pen('MEDIUM FOREST GREEN', 4)) |
151 | ||
cf694132 RD |
152 | for line in self.lines: |
153 | for coords in line: | |
8fa876ca | 154 | apply(dc.DrawLine, coords) |
cf694132 RD |
155 | |
156 | ||
157 | def SetXY(self, event): | |
158 | self.x, self.y = self.ConvertEventCoords(event) | |
159 | ||
160 | def ConvertEventCoords(self, event): | |
3ca6a5f0 | 161 | xView, yView = self.GetViewStart() |
cf694132 RD |
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(): | |
1e4a197e | 168 | self.SetFocus() |
cf694132 RD |
169 | self.SetXY(event) |
170 | self.curLine = [] | |
714d23b4 | 171 | self.CaptureMouse() |
1e4a197e | 172 | self.drawing = True |
d3bfec74 RD |
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. | |
8fa876ca | 178 | cdc = wx.ClientDC(self) |
d3bfec74 | 179 | self.PrepareDC(cdc) |
8fa876ca | 180 | dc = wx.BufferedDC(cdc, self.buffer) |
d3bfec74 | 181 | else: |
8fa876ca | 182 | dc = wx.ClientDC(self) |
d3bfec74 | 183 | self.PrepareDC(dc) |
cf694132 | 184 | |
cf694132 | 185 | dc.BeginDrawing() |
8fa876ca RD |
186 | dc.SetPen(wx.Pen('MEDIUM FOREST GREEN', 4)) |
187 | coords = [(self.x, self.y) , self.ConvertEventCoords(event)] | |
cf694132 | 188 | self.curLine.append(coords) |
8fa876ca | 189 | apply(dc.DrawLine, coords) |
cf694132 RD |
190 | self.SetXY(event) |
191 | dc.EndDrawing() | |
192 | ||
d3bfec74 RD |
193 | |
194 | elif event.LeftUp() and self.drawing: | |
cf694132 RD |
195 | self.lines.append(self.curLine) |
196 | self.curLine = [] | |
714d23b4 | 197 | self.ReleaseMouse() |
1e4a197e | 198 | self.drawing = False |
cf694132 | 199 | |
d1679124 RD |
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 | |
8fa876ca RD |
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. | |
d1679124 RD |
205 | |
206 | ## wheelScroll = 0 | |
207 | ## def OnWheel(self, evt): | |
208 | ## delta = evt.GetWheelDelta() | |
209 | ## rot = evt.GetWheelRotation() | |
210 | ## linesPer = evt.GetLinesPerAction() | |
3b5ccda1 | 211 | ## print delta, rot, linesPer |
d1679124 RD |
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 | ||
cf694132 RD |
223 | #--------------------------------------------------------------------------- |
224 | ||
225 | def runTest(frame, nb, log): | |
226 | win = MyCanvas(nb) | |
227 | return win | |
228 | ||
229 | #--------------------------------------------------------------------------- | |
230 | ||
231 | ||
232 | ||
8fa876ca RD |
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> | |
f9b24f07 | 241 | """ |
cf694132 RD |
242 | |
243 | ||
f9b24f07 RD |
244 | if __name__ == '__main__': |
245 | import sys,os | |
246 | import run | |
247 | run.main(['', os.path.basename(sys.argv[0])]) | |
cf694132 | 248 |