]>
Commit | Line | Data |
---|---|---|
f6bcfd97 BP |
1 | |
2 | from wxPython.wx import * | |
3 | ||
96bfd053 RD |
4 | import images |
5 | ||
f6bcfd97 BP |
6 | #---------------------------------------------------------------------- |
7 | ||
8 | class DragShape: | |
9 | def __init__(self, bmp): | |
10 | self.bmp = bmp | |
11 | self.pos = wxPoint(0,0) | |
12 | self.shown = true | |
13 | self.text = None | |
14 | self.fullscreen = false | |
15 | ||
16 | ||
17 | def HitTest(self, pt): | |
18 | rect = self.GetRect() | |
19 | return rect.Inside(pt.x, pt.y) | |
20 | ||
21 | ||
22 | def GetRect(self): | |
23 | return wxRect(self.pos.x, self.pos.y, | |
24 | self.bmp.GetWidth(), self.bmp.GetHeight()) | |
25 | ||
26 | ||
27 | def Draw(self, dc, op = wxCOPY): | |
28 | if self.bmp.Ok(): | |
29 | memDC = wxMemoryDC() | |
30 | memDC.SelectObject(self.bmp) | |
31 | ||
32 | dc.Blit(self.pos.x, self.pos.y, | |
33 | self.bmp.GetWidth(), self.bmp.GetHeight(), | |
34 | memDC, 0, 0, op, true) | |
35 | ||
36 | return true | |
37 | else: | |
38 | return false | |
39 | ||
40 | ||
41 | ||
42 | #---------------------------------------------------------------------- | |
43 | ||
44 | class DragCanvas(wxScrolledWindow): | |
45 | def __init__(self, parent, ID): | |
46 | wxScrolledWindow.__init__(self, parent, ID) | |
47 | self.shapes = [] | |
48 | self.dragImage = None | |
49 | self.dragShape = None | |
50 | ||
51 | self.SetCursor(wxStockCursor(wxCURSOR_ARROW)) | |
96bfd053 | 52 | self.bg_bmp = images.getBackgroundBitmap() |
f6bcfd97 BP |
53 | |
54 | ||
55 | # Make a shape from an image and mask. This one will demo | |
56 | # dragging outside the window | |
96bfd053 RD |
57 | bmp = images.getTestStarBitmap() |
58 | #mask = wxMaskColour(bmp, wxWHITE) | |
59 | #bmp.SetMask(mask) | |
f6bcfd97 BP |
60 | shape = DragShape(bmp) |
61 | shape.pos = wxPoint(5, 5) | |
62 | shape.fullscreen = true | |
63 | self.shapes.append(shape) | |
64 | ||
65 | ||
66 | # Make a shape from some text | |
67 | text = "Some Text" | |
68 | font = wxFont(15, wxROMAN, wxNORMAL, wxBOLD) | |
69 | textExtent = self.GetFullTextExtent(text, font) | |
70 | bmp = wxEmptyBitmap(textExtent[0], textExtent[1]) | |
71 | dc = wxMemoryDC() | |
72 | dc.SelectObject(bmp) | |
73 | dc.Clear() | |
74 | dc.SetTextForeground(wxRED) | |
75 | dc.SetFont(font) | |
76 | dc.DrawText(text, 0, 0) | |
77 | dc.SelectObject(wxNullBitmap) | |
78 | del dc | |
79 | mask = wxMaskColour(bmp, wxWHITE) | |
80 | bmp.SetMask(mask) | |
81 | shape = DragShape(bmp) | |
82 | shape.pos = wxPoint(5, 100) | |
83 | shape.text = "Some dragging text" | |
84 | self.shapes.append(shape) | |
85 | ||
86 | ||
87 | # Make some shapes from some playing card images. | |
88 | x = 200 | |
96bfd053 RD |
89 | for card in ['_01c_', '_12h_', '_13d_', '_10s_']: |
90 | bmpFunc = getattr(images, "get%sBitmap" % card) | |
91 | bmp = bmpFunc() | |
f6bcfd97 BP |
92 | shape = DragShape(bmp) |
93 | shape.pos = wxPoint(x, 5) | |
94 | self.shapes.append(shape) | |
95 | x = x + 80 | |
96 | ||
97 | ||
98 | EVT_ERASE_BACKGROUND(self, self.OnEraseBackground) | |
99 | EVT_PAINT(self, self.OnPaint) | |
100 | EVT_LEFT_DOWN(self, self.OnLeftDown) | |
101 | EVT_LEFT_UP(self, self.OnLeftUp) | |
102 | EVT_MOTION(self, self.OnMotion) | |
103 | ||
104 | ||
105 | ||
106 | def TileBackground(self, dc): | |
107 | # tile the background bitmap | |
108 | sz = self.GetClientSize() | |
109 | w = self.bg_bmp.GetWidth() | |
110 | h = self.bg_bmp.GetHeight() | |
111 | ||
112 | x = 0 | |
113 | while x < sz.width: | |
114 | y = 0 | |
115 | while y < sz.height: | |
116 | dc.DrawBitmap(self.bg_bmp, x, y) | |
117 | y = y + h | |
118 | x = x + w | |
119 | ||
120 | ||
121 | def DrawShapes(self, dc): | |
122 | for shape in self.shapes: | |
123 | if shape.shown: | |
124 | shape.Draw(dc) | |
125 | ||
126 | ||
127 | def FindShape(self, pt): | |
128 | for shape in self.shapes: | |
129 | if shape.HitTest(pt): | |
130 | return shape | |
131 | return None | |
132 | ||
133 | ||
134 | def EraseShape(self, shape, dc): | |
135 | r = shape.GetRect() | |
136 | dc.SetClippingRegion(r.x, r.y, r.width, r.height) | |
137 | self.TileBackground(dc) | |
138 | self.DrawShapes(dc) | |
139 | dc.DestroyClippingRegion() | |
140 | ||
141 | ||
142 | ||
143 | ||
144 | def OnEraseBackground(self, evt): | |
145 | dc = evt.GetDC() | |
146 | if not dc: | |
147 | dc = wxClientDC(self) | |
148 | self.TileBackground(dc) | |
149 | ||
150 | ||
151 | def OnPaint(self, evt): | |
152 | dc = wxPaintDC(self) | |
153 | self.PrepareDC(dc) | |
154 | self.DrawShapes(dc) | |
155 | ||
156 | ||
157 | def OnLeftDown(self, evt): | |
158 | shape = self.FindShape(evt.GetPosition()) | |
159 | if shape: | |
160 | # get ready to start dragging, but wait for the user to | |
161 | # move it a bit first | |
162 | self.dragShape = shape | |
163 | self.dragStartPos = evt.GetPosition() | |
164 | ||
165 | ||
166 | def OnLeftUp(self, evt): | |
167 | if not self.dragImage or not self.dragShape: | |
168 | self.dragImage = None | |
169 | self.dragShape = None | |
170 | return | |
171 | ||
172 | # end the dragging | |
173 | self.dragImage.Hide() | |
174 | self.dragImage.EndDrag() | |
175 | self.dragImage = None | |
176 | ||
177 | # reposition and draw the shape | |
178 | pt = evt.GetPosition() | |
179 | newPos = wxPoint(self.dragShape.pos.x + (pt.x - self.dragStartPos.x), | |
180 | self.dragShape.pos.y + (pt.y - self.dragStartPos.y)) | |
181 | ||
182 | dc = wxClientDC(self) | |
183 | self.dragShape.pos = newPos | |
184 | self.dragShape.shown = true | |
185 | self.dragShape.Draw(dc) | |
186 | self.dragShape = None | |
187 | ||
188 | ||
189 | def OnMotion(self, evt): | |
190 | if not self.dragShape or not evt.Dragging() or not evt.LeftIsDown(): | |
191 | return | |
192 | ||
193 | # if we have a shape, but havn't started dragging yet | |
194 | if self.dragShape and not self.dragImage: | |
195 | ||
196 | # only start the drag after having moved a couple pixels | |
197 | tolerance = 4 | |
198 | pt = evt.GetPosition() | |
199 | dx = abs(pt.x - self.dragStartPos.x) | |
200 | dy = abs(pt.y - self.dragStartPos.y) | |
201 | if dx <= tolerance and dy <= tolerance: | |
202 | return | |
203 | ||
204 | if self.dragShape.text: | |
205 | self.dragImage = wxDragString(self.dragShape.text, | |
206 | wxStockCursor(wxCURSOR_HAND)) | |
207 | else: | |
208 | self.dragImage = wxDragImage(self.dragShape.bmp, | |
209 | wxStockCursor(wxCURSOR_HAND)) | |
210 | ||
211 | newPos = wxPoint(self.dragShape.pos.x + (pt.x - self.dragStartPos.x), | |
212 | self.dragShape.pos.y + (pt.y - self.dragStartPos.y)) | |
213 | ||
214 | if self.dragShape.fullscreen: | |
215 | newPos = self.ClientToScreen(newPos) | |
216 | self.dragImage.BeginDrag((0,0), self, true) | |
217 | else: | |
218 | self.dragImage.BeginDrag((0,0), self) | |
219 | ||
220 | ||
221 | # erase the shape since it will be drawn independently now | |
222 | dc = wxClientDC(self) | |
223 | self.dragShape.shown = false | |
224 | self.EraseShape(self.dragShape, dc) | |
225 | ||
226 | self.dragImage.Move(newPos) | |
227 | self.dragImage.Show() | |
228 | ||
229 | ||
230 | # if we have shape and image then move it. | |
231 | elif self.dragShape and self.dragImage: | |
232 | pt = evt.GetPosition() | |
233 | newPos = wxPoint(self.dragShape.pos.x + (pt.x - self.dragStartPos.x), | |
234 | self.dragShape.pos.y + (pt.y - self.dragStartPos.y)) | |
235 | if self.dragShape.fullscreen: | |
236 | newPos = self.ClientToScreen(newPos) | |
237 | ||
238 | self.dragImage.Move(newPos) | |
239 | ||
240 | ||
241 | #---------------------------------------------------------------------- | |
242 | ||
243 | def runTest(frame, nb, log): | |
244 | win = DragCanvas(nb, -1) | |
245 | return win | |
246 | ||
247 | #---------------------------------------------------------------------- | |
248 | ||
249 | ||
250 | ||
251 | overview = """\ | |
252 | """ |