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