]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxDragImage.py
toplevel code transferred to wxTopLevelWindow
[wxWidgets.git] / wxPython / demo / wxDragImage.py
1
2 from wxPython.wx import *
3
4 import images
5
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 self.hiliteShape = None
51
52 self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
53 self.bg_bmp = images.getBackgroundBitmap()
54
55
56 # Make a shape from an image and mask. This one will demo
57 # dragging outside the window
58 bmp = images.getTestStarBitmap()
59 shape = DragShape(bmp)
60 shape.pos = wxPoint(5, 5)
61 shape.fullscreen = true
62 self.shapes.append(shape)
63
64
65 # Make a shape from some text
66 text = "Some Text"
67 font = wxFont(15, wxROMAN, wxNORMAL, wxBOLD)
68 textExtent = self.GetFullTextExtent(text, font)
69 bmp = wxEmptyBitmap(textExtent[0], textExtent[1])
70 dc = wxMemoryDC()
71 dc.SelectObject(bmp)
72 dc.Clear()
73 dc.SetTextForeground(wxRED)
74 dc.SetFont(font)
75 dc.DrawText(text, 0, 0)
76 dc.SelectObject(wxNullBitmap)
77 del dc
78 mask = wxMaskColour(bmp, wxWHITE)
79 bmp.SetMask(mask)
80 shape = DragShape(bmp)
81 shape.pos = wxPoint(5, 100)
82 shape.text = "Some dragging text"
83 self.shapes.append(shape)
84
85
86 # Make some shapes from some playing card images.
87 x = 200
88 for card in ['_01c_', '_12h_', '_13d_', '_10s_']:
89 bmpFunc = getattr(images, "get%sBitmap" % card)
90 bmp = bmpFunc()
91 shape = DragShape(bmp)
92 shape.pos = wxPoint(x, 5)
93 self.shapes.append(shape)
94 x = x + 80
95
96
97 EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
98 EVT_PAINT(self, self.OnPaint)
99 EVT_LEFT_DOWN(self, self.OnLeftDown)
100 EVT_LEFT_UP(self, self.OnLeftUp)
101 EVT_MOTION(self, self.OnMotion)
102
103
104
105 def TileBackground(self, dc):
106 # tile the background bitmap
107 sz = self.GetClientSize()
108 w = self.bg_bmp.GetWidth()
109 h = self.bg_bmp.GetHeight()
110
111 x = 0
112 while x < sz.width:
113 y = 0
114 while y < sz.height:
115 dc.DrawBitmap(self.bg_bmp, x, y)
116 y = y + h
117 x = x + w
118
119
120 def DrawShapes(self, dc):
121 for shape in self.shapes:
122 if shape.shown:
123 shape.Draw(dc)
124
125
126 def FindShape(self, pt):
127 for shape in self.shapes:
128 if shape.HitTest(pt):
129 return shape
130 return None
131
132
133 def EraseShape(self, shape, dc):
134 r = shape.GetRect()
135 dc.SetClippingRegion(r.x, r.y, r.width, r.height)
136 self.TileBackground(dc)
137 self.DrawShapes(dc)
138 dc.DestroyClippingRegion()
139
140
141 def OnEraseBackground(self, evt):
142 dc = evt.GetDC()
143 if not dc:
144 dc = wxClientDC(self)
145 rect = self.GetUpdateRegion().GetBox()
146 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height)
147 self.TileBackground(dc)
148
149
150 def OnPaint(self, evt):
151 dc = wxPaintDC(self)
152 self.PrepareDC(dc)
153 self.DrawShapes(dc)
154
155
156 def OnLeftDown(self, evt):
157 shape = self.FindShape(evt.GetPosition())
158 if shape:
159 # get ready to start dragging, but wait for the user to
160 # move it a bit first
161 self.dragShape = shape
162 self.dragStartPos = evt.GetPosition()
163
164
165 def OnLeftUp(self, evt):
166 if not self.dragImage or not self.dragShape:
167 self.dragImage = None
168 self.dragShape = None
169 return
170
171 # end the dragging
172 self.dragImage.Hide()
173 self.dragImage.EndDrag()
174 self.dragImage = None
175
176 dc = wxClientDC(self)
177 if self.hiliteShape:
178 self.hiliteShape.Draw(dc)
179 self.hiliteShape = None
180
181 # reposition and draw the shape
182 self.dragShape.pos = self.dragShape.pos + evt.GetPosition() - self.dragStartPos
183 self.dragShape.shown = true
184 self.dragShape.Draw(dc)
185 self.dragShape = None
186
187
188 def OnMotion(self, evt):
189 if not self.dragShape or not evt.Dragging() or not evt.LeftIsDown():
190 return
191
192 # if we have a shape, but havn't started dragging yet
193 if self.dragShape and not self.dragImage:
194
195 # only start the drag after having moved a couple pixels
196 tolerance = 2
197 pt = evt.GetPosition()
198 dx = abs(pt.x - self.dragStartPos.x)
199 dy = abs(pt.y - self.dragStartPos.y)
200 if dx <= tolerance and dy <= tolerance:
201 return
202
203 # erase the shape since it will be drawn independently now
204 dc = wxClientDC(self)
205 self.dragShape.shown = false
206 self.EraseShape(self.dragShape, dc)
207
208
209 if self.dragShape.text:
210 self.dragImage = wxDragString(self.dragShape.text,
211 wxStockCursor(wxCURSOR_HAND))
212 else:
213 self.dragImage = wxDragImage(self.dragShape.bmp,
214 wxStockCursor(wxCURSOR_HAND))
215
216 hotspot = self.dragStartPos - self.dragShape.pos
217 self.dragImage.BeginDrag(hotspot, self, self.dragShape.fullscreen)
218
219 self.dragImage.Move(pt)
220 self.dragImage.Show()
221
222
223 # if we have shape and image then move it, posibly highlighting another shape.
224 elif self.dragShape and self.dragImage:
225 onShape = self.FindShape(evt.GetPosition())
226 unhiliteOld = false
227 hiliteNew = false
228
229 # figure out what to hilite and what to unhilite
230 if self.hiliteShape:
231 if onShape is None or self.hiliteShape is not onShape:
232 unhiliteOld = true
233
234 if onShape and onShape is not self.hiliteShape and onShape.shown:
235 hiliteNew = TRUE
236
237 # if needed, hide the drag image so we can update the window
238 if unhiliteOld or hiliteNew:
239 self.dragImage.Hide()
240
241 if unhiliteOld:
242 dc = wxClientDC(self)
243 self.hiliteShape.Draw(dc)
244 self.hiliteShape = None
245
246 if hiliteNew:
247 dc = wxClientDC(self)
248 self.hiliteShape = onShape
249 self.hiliteShape.Draw(dc, wxINVERT)
250
251 # now move it and show it again if needed
252 self.dragImage.Move(evt.GetPosition())
253 if unhiliteOld or hiliteNew:
254 self.dragImage.Show()
255
256
257 #----------------------------------------------------------------------
258
259 def runTest(frame, nb, log):
260 win = DragCanvas(nb, -1)
261 return win
262
263 #----------------------------------------------------------------------
264
265
266
267 overview = """\
268 """