]>
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 = [] | |
1e4a197e | 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) | |
3b5ccda1 | 42 | ##EVT_MOUSEWHEEL(self, self.OnWheel) |
0af45411 | 43 | |
a3015b16 | 44 | |
bb0054cd RD |
45 | def getWidth(self): |
46 | return self.maxWidth | |
47 | ||
48 | def getHeight(self): | |
49 | return self.maxHeight | |
cf694132 RD |
50 | |
51 | ||
52 | def OnPaint(self, event): | |
d3bfec74 RD |
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) | |
cf694132 RD |
65 | |
66 | ||
1fded56b | 67 | def DoDrawing(self, dc, printing=False): |
cf694132 | 68 | dc.BeginDrawing() |
d43d449b | 69 | dc.SetPen(wxPen('RED')) |
cf694132 RD |
70 | dc.DrawRectangle(5, 5, 50, 50) |
71 | ||
72 | dc.SetBrush(wxLIGHT_GREY_BRUSH) | |
d43d449b | 73 | dc.SetPen(wxPen('BLUE', 4)) |
cf694132 RD |
74 | dc.DrawRectangle(15, 15, 50, 50) |
75 | ||
0569df0f | 76 | dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL)) |
cf694132 RD |
77 | dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF)) |
78 | te = dc.GetTextExtent("Hello World") | |
79 | dc.DrawText("Hello World", 60, 65) | |
80 | ||
d43d449b | 81 | dc.SetPen(wxPen('VIOLET', 4)) |
cf694132 RD |
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 | ||
1e4a197e | 91 | dc.DrawBitmap(self.bmp, 200, 20, True) |
bb0054cd RD |
92 | dc.SetTextForeground(wxColour(0, 0xFF, 0x80)) |
93 | dc.DrawText("a bitmap", 200, 85) | |
cf694132 | 94 | |
1fded56b RD |
95 | ## dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL)) |
96 | ## dc.SetTextForeground("BLACK") | |
97 | ## dc.DrawText("TEST this STRING", 10, 200) | |
98 | ## print dc.GetFullTextExtent("TEST this STRING") | |
99 | ||
6999b0d8 RD |
100 | font = wxFont(20, wxSWISS, wxNORMAL, wxNORMAL) |
101 | dc.SetFont(font) | |
102 | dc.SetTextForeground(wxBLACK) | |
103 | for a in range(0, 360, 45): | |
104 | dc.DrawRotatedText("Rotated text...", 300, 300, a) | |
105 | ||
eec92d76 RD |
106 | dc.SetPen(wxTRANSPARENT_PEN) |
107 | dc.SetBrush(wxBLUE_BRUSH) | |
108 | dc.DrawRectangle(50,500,50,50) | |
109 | dc.DrawRectangle(100,500,50,50) | |
110 | ||
d43d449b | 111 | dc.SetPen(wxPen('RED')) |
c368d904 RD |
112 | dc.DrawEllipticArc(200, 500, 50, 75, 0, 90) |
113 | ||
1fded56b RD |
114 | if not printing: |
115 | # This has troubles when used on a print preview in wxGTK, | |
116 | # probably something to do with the pen styles and the scaling | |
117 | # it does... | |
118 | y = 20 | |
119 | for style in [wxDOT, wxLONG_DASH, wxSHORT_DASH, wxDOT_DASH, wxUSER_DASH]: | |
120 | pen = wxPen("DARK ORCHID", 1, style) | |
121 | if style == wxUSER_DASH: | |
122 | pen.SetCap(wxCAP_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 | |
217cb2fa | 128 | |
d43d449b | 129 | dc.SetBrush(wxTRANSPARENT_BRUSH) |
c5943253 RD |
130 | dc.SetPen(wxPen(wxColour(0xFF, 0x20, 0xFF), 1, wxSOLID)) |
131 | dc.DrawRectangle(450, 50, 100, 100) | |
132 | old_pen = dc.GetPen() | |
133 | new_pen = wxPen("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 | ||
cf694132 RD |
139 | self.DrawSavedLines(dc) |
140 | dc.EndDrawing() | |
141 | ||
142 | ||
143 | def DrawSavedLines(self, dc): | |
d43d449b | 144 | dc.SetPen(wxPen('MEDIUM FOREST GREEN', 4)) |
cf694132 RD |
145 | for line in self.lines: |
146 | for coords in line: | |
147 | apply(dc.DrawLine, coords) | |
148 | ||
149 | ||
150 | def SetXY(self, event): | |
151 | self.x, self.y = self.ConvertEventCoords(event) | |
152 | ||
153 | def ConvertEventCoords(self, event): | |
3ca6a5f0 | 154 | xView, yView = self.GetViewStart() |
cf694132 RD |
155 | xDelta, yDelta = self.GetScrollPixelsPerUnit() |
156 | return (event.GetX() + (xView * xDelta), | |
157 | event.GetY() + (yView * yDelta)) | |
158 | ||
159 | def OnLeftButtonEvent(self, event): | |
160 | if event.LeftDown(): | |
1e4a197e | 161 | self.SetFocus() |
cf694132 RD |
162 | self.SetXY(event) |
163 | self.curLine = [] | |
714d23b4 | 164 | self.CaptureMouse() |
1e4a197e | 165 | self.drawing = True |
d3bfec74 RD |
166 | |
167 | elif event.Dragging() and self.drawing: | |
168 | if BUFFERED: | |
169 | # If doing buffered drawing, create the buffered DC, giving it | |
170 | # it a real DC to blit to when done. | |
171 | cdc = wxClientDC(self) | |
172 | self.PrepareDC(cdc) | |
173 | dc = wxBufferedDC(cdc, self.buffer) | |
174 | else: | |
175 | dc = wxClientDC(self) | |
176 | self.PrepareDC(dc) | |
cf694132 | 177 | |
cf694132 | 178 | dc.BeginDrawing() |
d43d449b | 179 | dc.SetPen(wxPen('MEDIUM FOREST GREEN', 4)) |
cf694132 RD |
180 | coords = (self.x, self.y) + self.ConvertEventCoords(event) |
181 | self.curLine.append(coords) | |
182 | apply(dc.DrawLine, coords) | |
183 | self.SetXY(event) | |
184 | dc.EndDrawing() | |
185 | ||
d3bfec74 RD |
186 | |
187 | elif event.LeftUp() and self.drawing: | |
cf694132 RD |
188 | self.lines.append(self.curLine) |
189 | self.curLine = [] | |
714d23b4 | 190 | self.ReleaseMouse() |
1e4a197e | 191 | self.drawing = False |
cf694132 | 192 | |
d1679124 RD |
193 | |
194 | ## This is an example of what to do for the EVT_MOUSEWHEEL event, | |
195 | ## but since wxScrolledWindow does this already it's not | |
196 | ## necessary to do it ourselves. | |
197 | ||
198 | ## wheelScroll = 0 | |
199 | ## def OnWheel(self, evt): | |
200 | ## delta = evt.GetWheelDelta() | |
201 | ## rot = evt.GetWheelRotation() | |
202 | ## linesPer = evt.GetLinesPerAction() | |
3b5ccda1 | 203 | ## print delta, rot, linesPer |
d1679124 RD |
204 | ## ws = self.wheelScroll |
205 | ## ws = ws + rot | |
206 | ## lines = ws / delta | |
207 | ## ws = ws - lines * delta | |
208 | ## self.wheelScroll = ws | |
209 | ## if lines != 0: | |
210 | ## lines = lines * linesPer | |
211 | ## vsx, vsy = self.GetViewStart() | |
212 | ## scrollTo = vsy - lines | |
213 | ## self.Scroll(-1, scrollTo) | |
214 | ||
cf694132 RD |
215 | #--------------------------------------------------------------------------- |
216 | ||
217 | def runTest(frame, nb, log): | |
218 | win = MyCanvas(nb) | |
219 | return win | |
220 | ||
221 | #--------------------------------------------------------------------------- | |
222 | ||
223 | ||
224 | ||
225 | ||
226 | ||
f9b24f07 RD |
227 | overview = """\ |
228 | """ | |
cf694132 RD |
229 | |
230 | ||
231 | ||
232 | ||
f9b24f07 RD |
233 | if __name__ == '__main__': |
234 | import sys,os | |
235 | import run | |
236 | run.main(['', os.path.basename(sys.argv[0])]) | |
cf694132 | 237 |