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