]>
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")) | |
15 | self.Connect(-1, -1, wxEVT_LEFT_DOWN, self.OnLeftButtonEvent) | |
16 | self.Connect(-1, -1, wxEVT_LEFT_UP, self.OnLeftButtonEvent) | |
17 | self.Connect(-1, -1, wxEVT_MOTION, self.OnLeftButtonEvent) | |
18 | ||
19 | self.SetCursor(wxStockCursor(wxCURSOR_PENCIL)) | |
bb0054cd | 20 | bmp = wxBitmap('bitmaps/test2.bmp', wxBITMAP_TYPE_BMP) |
5ed3dab2 RD |
21 | mask = wxMaskColour(bmp, wxBLUE) |
22 | bmp.SetMask(mask) | |
bb0054cd | 23 | self.bmp = bmp |
cf694132 | 24 | |
bb0054cd RD |
25 | self.SetScrollbars(20, 20, self.maxWidth/20, self.maxHeight/20) |
26 | ||
27 | def getWidth(self): | |
28 | return self.maxWidth | |
29 | ||
30 | def getHeight(self): | |
31 | return self.maxHeight | |
cf694132 RD |
32 | |
33 | ||
34 | def OnPaint(self, event): | |
35 | dc = wxPaintDC(self) | |
36 | self.PrepareDC(dc) | |
37 | self.DoDrawing(dc) | |
38 | ||
39 | ||
40 | def DoDrawing(self, dc): | |
41 | dc.BeginDrawing() | |
cf694132 RD |
42 | pen1 = wxPen(wxNamedColour('RED')) |
43 | dc.SetPen(pen1) | |
44 | dc.DrawRectangle(5, 5, 50, 50) | |
45 | ||
46 | dc.SetBrush(wxLIGHT_GREY_BRUSH) | |
47 | dc.SetPen(wxPen(wxNamedColour('BLUE'), 4)) | |
48 | dc.DrawRectangle(15, 15, 50, 50) | |
49 | ||
50 | font = wxFont(14, wxSWISS, wxNORMAL, wxNORMAL) | |
51 | dc.SetFont(font) | |
52 | dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF)) | |
53 | te = dc.GetTextExtent("Hello World") | |
54 | dc.DrawText("Hello World", 60, 65) | |
55 | ||
56 | dc.SetPen(wxPen(wxNamedColour('VIOLET'), 4)) | |
57 | dc.DrawLine(5, 65+te[1], 60+te[0], 65+te[1]) | |
58 | ||
59 | lst = [(100,110), (150,110), (150,160), (100,160)] | |
60 | dc.DrawLines(lst, -60) | |
61 | dc.SetPen(wxGREY_PEN) | |
62 | dc.DrawPolygon(lst, 75) | |
63 | dc.SetPen(wxGREEN_PEN) | |
64 | dc.DrawSpline(lst+[(100,100)]) | |
65 | ||
5ed3dab2 | 66 | dc.DrawBitmap(self.bmp, 200, 20, true) |
bb0054cd RD |
67 | dc.SetTextForeground(wxColour(0, 0xFF, 0x80)) |
68 | dc.DrawText("a bitmap", 200, 85) | |
cf694132 | 69 | |
6999b0d8 RD |
70 | font = wxFont(20, wxSWISS, wxNORMAL, wxNORMAL) |
71 | dc.SetFont(font) | |
72 | dc.SetTextForeground(wxBLACK) | |
73 | for a in range(0, 360, 45): | |
74 | dc.DrawRotatedText("Rotated text...", 300, 300, a) | |
75 | ||
eec92d76 RD |
76 | dc.SetPen(wxTRANSPARENT_PEN) |
77 | dc.SetBrush(wxBLUE_BRUSH) | |
78 | dc.DrawRectangle(50,500,50,50) | |
79 | dc.DrawRectangle(100,500,50,50) | |
80 | ||
81 | #from wxPython import dch | |
82 | #dch.FillRect(dc, wxRect(50, 400, 50, 50), wxBLACK) | |
83 | ||
cf694132 RD |
84 | self.DrawSavedLines(dc) |
85 | dc.EndDrawing() | |
86 | ||
87 | ||
eec92d76 | 88 | |
cf694132 RD |
89 | def DrawSavedLines(self, dc): |
90 | dc.SetPen(wxPen(wxNamedColour('MEDIUM FOREST GREEN'), 4)) | |
91 | for line in self.lines: | |
92 | for coords in line: | |
93 | apply(dc.DrawLine, coords) | |
94 | ||
95 | ||
96 | def SetXY(self, event): | |
97 | self.x, self.y = self.ConvertEventCoords(event) | |
98 | ||
99 | def ConvertEventCoords(self, event): | |
100 | xView, yView = self.ViewStart() | |
101 | xDelta, yDelta = self.GetScrollPixelsPerUnit() | |
102 | return (event.GetX() + (xView * xDelta), | |
103 | event.GetY() + (yView * yDelta)) | |
104 | ||
105 | def OnLeftButtonEvent(self, event): | |
106 | if event.LeftDown(): | |
107 | self.SetXY(event) | |
108 | self.curLine = [] | |
714d23b4 | 109 | self.CaptureMouse() |
cf694132 RD |
110 | |
111 | elif event.Dragging(): | |
714d23b4 | 112 | print event.GetPosition() |
cf694132 RD |
113 | dc = wxClientDC(self) |
114 | self.PrepareDC(dc) | |
115 | dc.BeginDrawing() | |
116 | dc.SetPen(wxPen(wxNamedColour('MEDIUM FOREST GREEN'), 4)) | |
117 | coords = (self.x, self.y) + self.ConvertEventCoords(event) | |
118 | self.curLine.append(coords) | |
119 | apply(dc.DrawLine, coords) | |
120 | self.SetXY(event) | |
121 | dc.EndDrawing() | |
122 | ||
123 | elif event.LeftUp(): | |
124 | self.lines.append(self.curLine) | |
125 | self.curLine = [] | |
714d23b4 | 126 | self.ReleaseMouse() |
cf694132 RD |
127 | |
128 | #--------------------------------------------------------------------------- | |
129 | ||
130 | def runTest(frame, nb, log): | |
131 | win = MyCanvas(nb) | |
132 | return win | |
133 | ||
134 | #--------------------------------------------------------------------------- | |
135 | ||
136 | ||
137 | ||
138 | ||
139 | ||
140 | ||
141 | ||
142 | ||
143 | ||
144 | ||
145 | ||
146 | ||
147 | ||
148 | overview = """\ | |
149 | """ |