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