]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | from wxPython.wx import * | |
3 | ||
4 | import images | |
5 | ||
6 | #--------------------------------------------------------------------------- | |
7 | ||
8 | class MyCanvas(wxScrolledWindow): | |
9 | def __init__(self, parent, id = -1, size = wxDefaultSize): | |
10 | wxScrolledWindow.__init__(self, parent, id, wxPoint(0, 0), size, wxSUNKEN_BORDER) | |
11 | ||
12 | self.lines = [] | |
13 | self.maxWidth = 1000 | |
14 | self.maxHeight = 1000 | |
15 | self.count = 0 | |
16 | ||
17 | self.SetBackgroundColour("WHITE") | |
18 | EVT_LEFT_DOWN(self, self.OnLeftButtonEvent) | |
19 | EVT_LEFT_UP(self, self.OnLeftButtonEvent) | |
20 | EVT_MOTION(self, self.OnLeftButtonEvent) | |
21 | EVT_PAINT(self, self.OnPaint) | |
22 | ||
23 | self.SetCursor(wxStockCursor(wxCURSOR_PENCIL)) | |
24 | bmp = images.getTest2Bitmap() | |
25 | mask = wxMaskColour(bmp, wxBLUE) | |
26 | bmp.SetMask(mask) | |
27 | self.bmp = bmp | |
28 | ||
29 | self.SetScrollbars(20, 20, self.maxWidth/20, self.maxHeight/20) | |
30 | ||
31 | ||
32 | def getWidth(self): | |
33 | return self.maxWidth | |
34 | ||
35 | def getHeight(self): | |
36 | return self.maxHeight | |
37 | ||
38 | ||
39 | def OnPaint(self, event): | |
40 | #self.count += 1 | |
41 | #print self.count, "begin paint...", | |
42 | dc = wxPaintDC(self) | |
43 | self.PrepareDC(dc) | |
44 | self.DoDrawing(dc) | |
45 | #print "end paint" | |
46 | ||
47 | ||
48 | def DoDrawing(self, dc): | |
49 | dc.BeginDrawing() | |
50 | dc.SetPen(wxPen('RED')) | |
51 | dc.DrawRectangle(5, 5, 50, 50) | |
52 | ||
53 | dc.SetBrush(wxLIGHT_GREY_BRUSH) | |
54 | dc.SetPen(wxPen('BLUE', 4)) | |
55 | dc.DrawRectangle(15, 15, 50, 50) | |
56 | ||
57 | dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL)) | |
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('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 | ||
72 | dc.DrawBitmap(self.bmp, 200, 20, true) | |
73 | dc.SetTextForeground(wxColour(0, 0xFF, 0x80)) | |
74 | dc.DrawText("a bitmap", 200, 85) | |
75 | ||
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 | ||
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 | ||
87 | dc.SetPen(wxPen('RED')) | |
88 | dc.DrawEllipticArc(200, 500, 50, 75, 0, 90) | |
89 | ||
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: | |
94 | pen.SetDashes([1, 2, 3, 4, 5, 6, 7, 8]) | |
95 | pen.SetColour("RED") | |
96 | dc.SetPen(pen) | |
97 | dc.DrawLine(300, y, 400, y) | |
98 | y = y + 10 | |
99 | ||
100 | dc.SetBrush(wxTRANSPARENT_BRUSH) | |
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 | ||
111 | self.DrawSavedLines(dc) | |
112 | dc.EndDrawing() | |
113 | ||
114 | ||
115 | def DrawSavedLines(self, dc): | |
116 | dc.SetPen(wxPen('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): | |
126 | xView, yView = self.GetViewStart() | |
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 = [] | |
135 | self.CaptureMouse() | |
136 | ||
137 | elif event.Dragging(): | |
138 | dc = wxClientDC(self) | |
139 | self.PrepareDC(dc) | |
140 | dc.BeginDrawing() | |
141 | dc.SetPen(wxPen('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 = [] | |
151 | self.ReleaseMouse() | |
152 | ||
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 | ||
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 | """ |