]>
Commit | Line | Data |
---|---|---|
cf694132 RD |
1 | |
2 | from wxPython.wx import * | |
3 | ||
96bfd053 RD |
4 | import images |
5 | ||
cf694132 RD |
6 | #--------------------------------------------------------------------------- |
7 | ||
8 | class MyCanvas(wxScrolledWindow): | |
e166644c RD |
9 | def __init__(self, parent, id = -1, size = wxDefaultSize): |
10 | wxScrolledWindow.__init__(self, parent, id, wxPoint(0, 0), size, wxSUNKEN_BORDER) | |
cf694132 RD |
11 | |
12 | self.lines = [] | |
bb0054cd RD |
13 | self.maxWidth = 1000 |
14 | self.maxHeight = 1000 | |
b5a5d647 | 15 | self.count = 0 |
cf694132 RD |
16 | |
17 | self.SetBackgroundColour(wxNamedColor("WHITE")) | |
f6bcfd97 BP |
18 | EVT_LEFT_DOWN(self, self.OnLeftButtonEvent) |
19 | EVT_LEFT_UP(self, self.OnLeftButtonEvent) | |
20 | EVT_MOTION(self, self.OnLeftButtonEvent) | |
f6bcfd97 BP |
21 | EVT_PAINT(self, self.OnPaint) |
22 | ||
cf694132 | 23 | self.SetCursor(wxStockCursor(wxCURSOR_PENCIL)) |
96bfd053 | 24 | bmp = images.getTest2Bitmap() |
5ed3dab2 RD |
25 | mask = wxMaskColour(bmp, wxBLUE) |
26 | bmp.SetMask(mask) | |
bb0054cd | 27 | self.bmp = bmp |
cf694132 | 28 | |
bb0054cd RD |
29 | self.SetScrollbars(20, 20, self.maxWidth/20, self.maxHeight/20) |
30 | ||
a3015b16 | 31 | |
bb0054cd RD |
32 | def getWidth(self): |
33 | return self.maxWidth | |
34 | ||
35 | def getHeight(self): | |
36 | return self.maxHeight | |
cf694132 RD |
37 | |
38 | ||
39 | def OnPaint(self, event): | |
b5a5d647 RD |
40 | #self.count += 1 |
41 | #print self.count, "begin paint...", | |
cf694132 RD |
42 | dc = wxPaintDC(self) |
43 | self.PrepareDC(dc) | |
44 | self.DoDrawing(dc) | |
b5a5d647 | 45 | #print "end paint" |
cf694132 RD |
46 | |
47 | ||
48 | def DoDrawing(self, dc): | |
49 | dc.BeginDrawing() | |
0569df0f | 50 | dc.SetPen(wxPen(wxNamedColour('RED'))) |
cf694132 RD |
51 | dc.DrawRectangle(5, 5, 50, 50) |
52 | ||
53 | dc.SetBrush(wxLIGHT_GREY_BRUSH) | |
54 | dc.SetPen(wxPen(wxNamedColour('BLUE'), 4)) | |
55 | dc.DrawRectangle(15, 15, 50, 50) | |
56 | ||
0569df0f | 57 | dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL)) |
cf694132 RD |
58 | dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF)) |
59 | te = dc.GetTextExtent("Hello World") | |
60 | dc.DrawText("Hello World", 60, 65) | |
61 | ||
62 | dc.SetPen(wxPen(wxNamedColour('VIOLET'), 4)) | |
63 | dc.DrawLine(5, 65+te[1], 60+te[0], 65+te[1]) | |
64 | ||
65 | lst = [(100,110), (150,110), (150,160), (100,160)] | |
66 | dc.DrawLines(lst, -60) | |
67 | dc.SetPen(wxGREY_PEN) | |
68 | dc.DrawPolygon(lst, 75) | |
69 | dc.SetPen(wxGREEN_PEN) | |
70 | dc.DrawSpline(lst+[(100,100)]) | |
71 | ||
5ed3dab2 | 72 | dc.DrawBitmap(self.bmp, 200, 20, true) |
bb0054cd RD |
73 | dc.SetTextForeground(wxColour(0, 0xFF, 0x80)) |
74 | dc.DrawText("a bitmap", 200, 85) | |
cf694132 | 75 | |
6999b0d8 RD |
76 | font = wxFont(20, wxSWISS, wxNORMAL, wxNORMAL) |
77 | dc.SetFont(font) | |
78 | dc.SetTextForeground(wxBLACK) | |
79 | for a in range(0, 360, 45): | |
80 | dc.DrawRotatedText("Rotated text...", 300, 300, a) | |
81 | ||
eec92d76 RD |
82 | dc.SetPen(wxTRANSPARENT_PEN) |
83 | dc.SetBrush(wxBLUE_BRUSH) | |
84 | dc.DrawRectangle(50,500,50,50) | |
85 | dc.DrawRectangle(100,500,50,50) | |
86 | ||
0569df0f | 87 | dc.SetPen(wxPen(wxNamedColour('RED'))) |
c368d904 RD |
88 | dc.DrawEllipticArc(200, 500, 50, 75, 0, 90) |
89 | ||
217cb2fa RD |
90 | y = 20 |
91 | for style in [wxDOT, wxLONG_DASH, wxSHORT_DASH, wxDOT_DASH, wxUSER_DASH]: | |
92 | pen = wxPen("DARK ORCHID", 1, style) | |
93 | if style == wxUSER_DASH: | |
05f30eec | 94 | pen.SetDashes([1, 2, 3, 4, 5, 6, 7, 8]) |
ecc08ead | 95 | pen.SetColour("RED") |
217cb2fa RD |
96 | dc.SetPen(pen) |
97 | dc.DrawLine(300, y, 400, y) | |
98 | y = y + 10 | |
99 | ||
c5943253 RD |
100 | dc.SetBrush(wxNullBrush) |
101 | dc.SetPen(wxPen(wxColour(0xFF, 0x20, 0xFF), 1, wxSOLID)) | |
102 | dc.DrawRectangle(450, 50, 100, 100) | |
103 | old_pen = dc.GetPen() | |
104 | new_pen = wxPen("BLACK", 5) | |
105 | dc.SetPen(new_pen) | |
106 | dc.DrawRectangle(470, 70, 60, 60) | |
107 | dc.SetPen(old_pen) | |
108 | dc.DrawRectangle(490, 90, 20, 20) | |
109 | ||
110 | ||
cf694132 RD |
111 | self.DrawSavedLines(dc) |
112 | dc.EndDrawing() | |
113 | ||
114 | ||
115 | def DrawSavedLines(self, dc): | |
116 | dc.SetPen(wxPen(wxNamedColour('MEDIUM FOREST GREEN'), 4)) | |
117 | for line in self.lines: | |
118 | for coords in line: | |
119 | apply(dc.DrawLine, coords) | |
120 | ||
121 | ||
122 | def SetXY(self, event): | |
123 | self.x, self.y = self.ConvertEventCoords(event) | |
124 | ||
125 | def ConvertEventCoords(self, event): | |
3ca6a5f0 | 126 | xView, yView = self.GetViewStart() |
cf694132 RD |
127 | xDelta, yDelta = self.GetScrollPixelsPerUnit() |
128 | return (event.GetX() + (xView * xDelta), | |
129 | event.GetY() + (yView * yDelta)) | |
130 | ||
131 | def OnLeftButtonEvent(self, event): | |
132 | if event.LeftDown(): | |
133 | self.SetXY(event) | |
134 | self.curLine = [] | |
714d23b4 | 135 | self.CaptureMouse() |
cf694132 RD |
136 | |
137 | elif event.Dragging(): | |
138 | dc = wxClientDC(self) | |
139 | self.PrepareDC(dc) | |
140 | dc.BeginDrawing() | |
141 | dc.SetPen(wxPen(wxNamedColour('MEDIUM FOREST GREEN'), 4)) | |
142 | coords = (self.x, self.y) + self.ConvertEventCoords(event) | |
143 | self.curLine.append(coords) | |
144 | apply(dc.DrawLine, coords) | |
145 | self.SetXY(event) | |
146 | dc.EndDrawing() | |
147 | ||
148 | elif event.LeftUp(): | |
149 | self.lines.append(self.curLine) | |
150 | self.curLine = [] | |
714d23b4 | 151 | self.ReleaseMouse() |
cf694132 | 152 | |
d1679124 RD |
153 | |
154 | ## This is an example of what to do for the EVT_MOUSEWHEEL event, | |
155 | ## but since wxScrolledWindow does this already it's not | |
156 | ## necessary to do it ourselves. | |
157 | ||
158 | ## wheelScroll = 0 | |
159 | ## def OnWheel(self, evt): | |
160 | ## delta = evt.GetWheelDelta() | |
161 | ## rot = evt.GetWheelRotation() | |
162 | ## linesPer = evt.GetLinesPerAction() | |
163 | ## ws = self.wheelScroll | |
164 | ## ws = ws + rot | |
165 | ## lines = ws / delta | |
166 | ## ws = ws - lines * delta | |
167 | ## self.wheelScroll = ws | |
168 | ## if lines != 0: | |
169 | ## lines = lines * linesPer | |
170 | ## vsx, vsy = self.GetViewStart() | |
171 | ## scrollTo = vsy - lines | |
172 | ## self.Scroll(-1, scrollTo) | |
173 | ||
cf694132 RD |
174 | #--------------------------------------------------------------------------- |
175 | ||
176 | def runTest(frame, nb, log): | |
177 | win = MyCanvas(nb) | |
178 | return win | |
179 | ||
180 | #--------------------------------------------------------------------------- | |
181 | ||
182 | ||
183 | ||
184 | ||
185 | ||
186 | ||
187 | ||
188 | ||
189 | ||
190 | ||
191 | ||
192 | ||
193 | ||
194 | overview = """\ | |
195 | """ |