]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | from wxPython.wx import * | |
3 | ||
4 | import images | |
5 | ||
6 | BUFFERED = 1 | |
7 | ||
8 | #--------------------------------------------------------------------------- | |
9 | ||
10 | class MyCanvas(wxScrolledWindow): | |
11 | def __init__(self, parent, id = -1, size = wxDefaultSize): | |
12 | wxScrolledWindow.__init__(self, parent, id, wxPoint(0, 0), size, wxSUNKEN_BORDER) | |
13 | ||
14 | self.lines = [] | |
15 | self.maxWidth = 1000 | |
16 | self.maxHeight = 1000 | |
17 | self.x = self.y = 0 | |
18 | self.curLine = [] | |
19 | self.drawing = false | |
20 | ||
21 | self.SetBackgroundColour("WHITE") | |
22 | self.SetCursor(wxStockCursor(wxCURSOR_PENCIL)) | |
23 | bmp = images.getTest2Bitmap() | |
24 | mask = wxMaskColour(bmp, wxBLUE) | |
25 | bmp.SetMask(mask) | |
26 | self.bmp = bmp | |
27 | ||
28 | self.SetScrollbars(20, 20, self.maxWidth/20, self.maxHeight/20) | |
29 | ||
30 | if BUFFERED: | |
31 | # Initialize the buffer bitmap. No real DC is needed at this point. | |
32 | self.buffer = wxEmptyBitmap(self.maxWidth, self.maxHeight) | |
33 | dc = wxBufferedDC(None, self.buffer) | |
34 | dc.SetBackground(wxBrush(self.GetBackgroundColour())) | |
35 | dc.Clear() | |
36 | self.DoDrawing(dc) | |
37 | ||
38 | EVT_LEFT_DOWN(self, self.OnLeftButtonEvent) | |
39 | EVT_LEFT_UP(self, self.OnLeftButtonEvent) | |
40 | EVT_MOTION(self, self.OnLeftButtonEvent) | |
41 | EVT_PAINT(self, self.OnPaint) | |
42 | ##EVT_MOUSEWHEEL(self, self.OnWheel) | |
43 | ||
44 | ||
45 | def getWidth(self): | |
46 | return self.maxWidth | |
47 | ||
48 | def getHeight(self): | |
49 | return self.maxHeight | |
50 | ||
51 | ||
52 | def OnPaint(self, event): | |
53 | if BUFFERED: | |
54 | # Create a buffered paint DC. It will create the real | |
55 | # wxPaintDC and then blit the bitmap to it when dc is | |
56 | # deleted. Since we don't need to draw anything else | |
57 | # here that's all there is to it. | |
58 | dc = wxBufferedPaintDC(self, self.buffer) | |
59 | else: | |
60 | dc = wxPaintDC(self) | |
61 | self.PrepareDC(dc) | |
62 | # since we're not buffering in this case, we have to | |
63 | # paint the whole window, potentially very time consuming. | |
64 | self.DoDrawing(dc) | |
65 | ||
66 | ||
67 | def DoDrawing(self, dc): | |
68 | dc.BeginDrawing() | |
69 | dc.SetPen(wxPen('RED')) | |
70 | dc.DrawRectangle(5, 5, 50, 50) | |
71 | ||
72 | dc.SetBrush(wxLIGHT_GREY_BRUSH) | |
73 | dc.SetPen(wxPen('BLUE', 4)) | |
74 | dc.DrawRectangle(15, 15, 50, 50) | |
75 | ||
76 | dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL)) | |
77 | dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF)) | |
78 | te = dc.GetTextExtent("Hello World") | |
79 | dc.DrawText("Hello World", 60, 65) | |
80 | ||
81 | dc.SetPen(wxPen('VIOLET', 4)) | |
82 | dc.DrawLine(5, 65+te[1], 60+te[0], 65+te[1]) | |
83 | ||
84 | lst = [(100,110), (150,110), (150,160), (100,160)] | |
85 | dc.DrawLines(lst, -60) | |
86 | dc.SetPen(wxGREY_PEN) | |
87 | dc.DrawPolygon(lst, 75) | |
88 | dc.SetPen(wxGREEN_PEN) | |
89 | dc.DrawSpline(lst+[(100,100)]) | |
90 | ||
91 | dc.DrawBitmap(self.bmp, 200, 20, true) | |
92 | dc.SetTextForeground(wxColour(0, 0xFF, 0x80)) | |
93 | dc.DrawText("a bitmap", 200, 85) | |
94 | ||
95 | font = wxFont(20, wxSWISS, wxNORMAL, wxNORMAL) | |
96 | dc.SetFont(font) | |
97 | dc.SetTextForeground(wxBLACK) | |
98 | for a in range(0, 360, 45): | |
99 | dc.DrawRotatedText("Rotated text...", 300, 300, a) | |
100 | ||
101 | dc.SetPen(wxTRANSPARENT_PEN) | |
102 | dc.SetBrush(wxBLUE_BRUSH) | |
103 | dc.DrawRectangle(50,500,50,50) | |
104 | dc.DrawRectangle(100,500,50,50) | |
105 | ||
106 | dc.SetPen(wxPen('RED')) | |
107 | dc.DrawEllipticArc(200, 500, 50, 75, 0, 90) | |
108 | ||
109 | y = 20 | |
110 | for style in [wxDOT, wxLONG_DASH, wxSHORT_DASH, wxDOT_DASH, wxUSER_DASH]: | |
111 | pen = wxPen("DARK ORCHID", 1, style) | |
112 | if style == wxUSER_DASH: | |
113 | pen.SetCap(wxCAP_BUTT) | |
114 | pen.SetDashes([1,2]) | |
115 | pen.SetColour("RED") | |
116 | dc.SetPen(pen) | |
117 | dc.DrawLine(300, y, 400, y) | |
118 | y = y + 10 | |
119 | ||
120 | dc.SetBrush(wxTRANSPARENT_BRUSH) | |
121 | dc.SetPen(wxPen(wxColour(0xFF, 0x20, 0xFF), 1, wxSOLID)) | |
122 | dc.DrawRectangle(450, 50, 100, 100) | |
123 | old_pen = dc.GetPen() | |
124 | new_pen = wxPen("BLACK", 5) | |
125 | dc.SetPen(new_pen) | |
126 | dc.DrawRectangle(470, 70, 60, 60) | |
127 | dc.SetPen(old_pen) | |
128 | dc.DrawRectangle(490, 90, 20, 20) | |
129 | ||
130 | self.DrawSavedLines(dc) | |
131 | dc.EndDrawing() | |
132 | ||
133 | ||
134 | def DrawSavedLines(self, dc): | |
135 | dc.SetPen(wxPen('MEDIUM FOREST GREEN', 4)) | |
136 | for line in self.lines: | |
137 | for coords in line: | |
138 | apply(dc.DrawLine, coords) | |
139 | ||
140 | ||
141 | def SetXY(self, event): | |
142 | self.x, self.y = self.ConvertEventCoords(event) | |
143 | ||
144 | def ConvertEventCoords(self, event): | |
145 | xView, yView = self.GetViewStart() | |
146 | xDelta, yDelta = self.GetScrollPixelsPerUnit() | |
147 | return (event.GetX() + (xView * xDelta), | |
148 | event.GetY() + (yView * yDelta)) | |
149 | ||
150 | def OnLeftButtonEvent(self, event): | |
151 | if event.LeftDown(): | |
152 | self.SetXY(event) | |
153 | self.curLine = [] | |
154 | self.CaptureMouse() | |
155 | self.drawing = true | |
156 | ||
157 | elif event.Dragging() and self.drawing: | |
158 | if BUFFERED: | |
159 | # If doing buffered drawing, create the buffered DC, giving it | |
160 | # it a real DC to blit to when done. | |
161 | cdc = wxClientDC(self) | |
162 | self.PrepareDC(cdc) | |
163 | dc = wxBufferedDC(cdc, self.buffer) | |
164 | else: | |
165 | dc = wxClientDC(self) | |
166 | self.PrepareDC(dc) | |
167 | ||
168 | dc.BeginDrawing() | |
169 | dc.SetPen(wxPen('MEDIUM FOREST GREEN', 4)) | |
170 | coords = (self.x, self.y) + self.ConvertEventCoords(event) | |
171 | self.curLine.append(coords) | |
172 | apply(dc.DrawLine, coords) | |
173 | self.SetXY(event) | |
174 | dc.EndDrawing() | |
175 | ||
176 | ||
177 | elif event.LeftUp() and self.drawing: | |
178 | self.lines.append(self.curLine) | |
179 | self.curLine = [] | |
180 | self.ReleaseMouse() | |
181 | self.drawing = false | |
182 | ||
183 | ||
184 | ## This is an example of what to do for the EVT_MOUSEWHEEL event, | |
185 | ## but since wxScrolledWindow does this already it's not | |
186 | ## necessary to do it ourselves. | |
187 | ||
188 | ## wheelScroll = 0 | |
189 | ## def OnWheel(self, evt): | |
190 | ## delta = evt.GetWheelDelta() | |
191 | ## rot = evt.GetWheelRotation() | |
192 | ## linesPer = evt.GetLinesPerAction() | |
193 | ## print delta, rot, linesPer | |
194 | ## ws = self.wheelScroll | |
195 | ## ws = ws + rot | |
196 | ## lines = ws / delta | |
197 | ## ws = ws - lines * delta | |
198 | ## self.wheelScroll = ws | |
199 | ## if lines != 0: | |
200 | ## lines = lines * linesPer | |
201 | ## vsx, vsy = self.GetViewStart() | |
202 | ## scrollTo = vsy - lines | |
203 | ## self.Scroll(-1, scrollTo) | |
204 | ||
205 | #--------------------------------------------------------------------------- | |
206 | ||
207 | def runTest(frame, nb, log): | |
208 | win = MyCanvas(nb) | |
209 | return win | |
210 | ||
211 | #--------------------------------------------------------------------------- | |
212 | ||
213 | ||
214 | ||
215 | ||
216 | ||
217 | overview = """\ | |
218 | """ | |
219 | ||
220 | ||
221 | ||
222 | ||
223 | if __name__ == '__main__': | |
224 | import sys,os | |
225 | import run | |
226 | run.main(['', os.path.basename(sys.argv[0])]) | |
227 |