]>
Commit | Line | Data |
---|---|---|
cf694132 RD |
1 | |
2 | from wxPython.wx import * | |
3 | ||
4 | #--------------------------------------------------------------------------- | |
5 | ||
6 | class MyCanvas(wxScrolledWindow): | |
e166644c RD |
7 | def __init__(self, parent, id = -1, size = wxDefaultSize): |
8 | wxScrolledWindow.__init__(self, parent, id, wxPoint(0, 0), size, wxSUNKEN_BORDER) | |
cf694132 RD |
9 | |
10 | self.lines = [] | |
bb0054cd RD |
11 | self.maxWidth = 1000 |
12 | self.maxHeight = 1000 | |
cf694132 RD |
13 | |
14 | self.SetBackgroundColour(wxNamedColor("WHITE")) | |
f6bcfd97 BP |
15 | EVT_LEFT_DOWN(self, self.OnLeftButtonEvent) |
16 | EVT_LEFT_UP(self, self.OnLeftButtonEvent) | |
17 | EVT_MOTION(self, self.OnLeftButtonEvent) | |
18 | ||
19 | EVT_PAINT(self, self.OnPaint) | |
20 | ||
cf694132 RD |
21 | |
22 | self.SetCursor(wxStockCursor(wxCURSOR_PENCIL)) | |
bb0054cd | 23 | bmp = wxBitmap('bitmaps/test2.bmp', wxBITMAP_TYPE_BMP) |
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 | ||
30 | def getWidth(self): | |
31 | return self.maxWidth | |
32 | ||
33 | def getHeight(self): | |
34 | return self.maxHeight | |
cf694132 RD |
35 | |
36 | ||
37 | def OnPaint(self, event): | |
38 | dc = wxPaintDC(self) | |
39 | self.PrepareDC(dc) | |
40 | self.DoDrawing(dc) | |
41 | ||
42 | ||
43 | def DoDrawing(self, dc): | |
44 | dc.BeginDrawing() | |
cf694132 RD |
45 | pen1 = wxPen(wxNamedColour('RED')) |
46 | dc.SetPen(pen1) | |
47 | dc.DrawRectangle(5, 5, 50, 50) | |
48 | ||
49 | dc.SetBrush(wxLIGHT_GREY_BRUSH) | |
50 | dc.SetPen(wxPen(wxNamedColour('BLUE'), 4)) | |
51 | dc.DrawRectangle(15, 15, 50, 50) | |
52 | ||
53 | font = wxFont(14, wxSWISS, wxNORMAL, wxNORMAL) | |
54 | dc.SetFont(font) | |
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 | ||
c368d904 RD |
84 | dc.SetPen(pen1) |
85 | dc.DrawEllipticArc(200, 500, 50, 75, 0, 90) | |
86 | ||
eec92d76 RD |
87 | #from wxPython import dch |
88 | #dch.FillRect(dc, wxRect(50, 400, 50, 50), wxBLACK) | |
89 | ||
cf694132 RD |
90 | self.DrawSavedLines(dc) |
91 | dc.EndDrawing() | |
92 | ||
93 | ||
eec92d76 | 94 | |
cf694132 RD |
95 | def DrawSavedLines(self, dc): |
96 | dc.SetPen(wxPen(wxNamedColour('MEDIUM FOREST GREEN'), 4)) | |
97 | for line in self.lines: | |
98 | for coords in line: | |
99 | apply(dc.DrawLine, coords) | |
100 | ||
101 | ||
102 | def SetXY(self, event): | |
103 | self.x, self.y = self.ConvertEventCoords(event) | |
104 | ||
105 | def ConvertEventCoords(self, event): | |
3ca6a5f0 | 106 | xView, yView = self.GetViewStart() |
cf694132 RD |
107 | xDelta, yDelta = self.GetScrollPixelsPerUnit() |
108 | return (event.GetX() + (xView * xDelta), | |
109 | event.GetY() + (yView * yDelta)) | |
110 | ||
111 | def OnLeftButtonEvent(self, event): | |
112 | if event.LeftDown(): | |
113 | self.SetXY(event) | |
114 | self.curLine = [] | |
714d23b4 | 115 | self.CaptureMouse() |
cf694132 RD |
116 | |
117 | elif event.Dragging(): | |
118 | dc = wxClientDC(self) | |
119 | self.PrepareDC(dc) | |
120 | dc.BeginDrawing() | |
121 | dc.SetPen(wxPen(wxNamedColour('MEDIUM FOREST GREEN'), 4)) | |
122 | coords = (self.x, self.y) + self.ConvertEventCoords(event) | |
123 | self.curLine.append(coords) | |
124 | apply(dc.DrawLine, coords) | |
125 | self.SetXY(event) | |
126 | dc.EndDrawing() | |
127 | ||
128 | elif event.LeftUp(): | |
129 | self.lines.append(self.curLine) | |
130 | self.curLine = [] | |
714d23b4 | 131 | self.ReleaseMouse() |
cf694132 RD |
132 | |
133 | #--------------------------------------------------------------------------- | |
134 | ||
135 | def runTest(frame, nb, log): | |
136 | win = MyCanvas(nb) | |
137 | return win | |
138 | ||
139 | #--------------------------------------------------------------------------- | |
140 | ||
141 | ||
142 | ||
143 | ||
144 | ||
145 | ||
146 | ||
147 | ||
148 | ||
149 | ||
150 | ||
151 | ||
152 | ||
153 | overview = """\ | |
154 | """ |