]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxDragImage.py
2 from wxPython
.wx
import *
4 #----------------------------------------------------------------------
7 def __init__(self
, bmp
):
9 self
.pos
= wxPoint(0,0)
12 self
.fullscreen
= false
15 def HitTest(self
, pt
):
17 return rect
.Inside(pt
.x
, pt
.y
)
21 return wxRect(self
.pos
.x
, self
.pos
.y
,
22 self
.bmp
.GetWidth(), self
.bmp
.GetHeight())
25 def Draw(self
, dc
, op
= wxCOPY
):
28 memDC
.SelectObject(self
.bmp
)
30 dc
.Blit(self
.pos
.x
, self
.pos
.y
,
31 self
.bmp
.GetWidth(), self
.bmp
.GetHeight(),
32 memDC
, 0, 0, op
, true
)
40 #----------------------------------------------------------------------
42 class DragCanvas(wxScrolledWindow
):
43 def __init__(self
, parent
, ID
):
44 wxScrolledWindow
.__init
__(self
, parent
, ID
)
49 self
.SetCursor(wxStockCursor(wxCURSOR_ARROW
))
50 self
.bg_bmp
= wxBitmap('bitmaps/backgrnd.png', wxBITMAP_TYPE_PNG
)
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
)
58 shape
= DragShape(bmp
)
59 shape
.pos
= wxPoint(5, 5)
60 shape
.fullscreen
= true
61 self
.shapes
.append(shape
)
64 # Make a shape from some text
66 font
= wxFont(15, wxROMAN
, wxNORMAL
, wxBOLD
)
67 textExtent
= self
.GetFullTextExtent(text
, font
)
68 bmp
= wxEmptyBitmap(textExtent
[0], textExtent
[1])
72 dc
.SetTextForeground(wxRED
)
74 dc
.DrawText(text
, 0, 0)
75 dc
.SelectObject(wxNullBitmap
)
77 mask
= wxMaskColour(bmp
, wxWHITE
)
79 shape
= DragShape(bmp
)
80 shape
.pos
= wxPoint(5, 100)
81 shape
.text
= "Some dragging text"
82 self
.shapes
.append(shape
)
85 # Make some shapes from some playing card images.
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
)
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
)
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()
113 dc
.DrawBitmap(self
.bg_bmp
, x
, y
)
118 def DrawShapes(self
, dc
):
119 for shape
in self
.shapes
:
124 def FindShape(self
, pt
):
125 for shape
in self
.shapes
:
126 if shape
.HitTest(pt
):
131 def EraseShape(self
, shape
, dc
):
133 dc
.SetClippingRegion(r
.x
, r
.y
, r
.width
, r
.height
)
134 self
.TileBackground(dc
)
136 dc
.DestroyClippingRegion()
141 def OnEraseBackground(self
, evt
):
144 dc
= wxClientDC(self
)
145 self
.TileBackground(dc
)
148 def OnPaint(self
, evt
):
154 def OnLeftDown(self
, evt
):
155 shape
= self
.FindShape(evt
.GetPosition())
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()
163 def OnLeftUp(self
, evt
):
164 if not self
.dragImage
or not self
.dragShape
:
165 self
.dragImage
= None
166 self
.dragShape
= None
170 self
.dragImage
.Hide()
171 self
.dragImage
.EndDrag()
172 self
.dragImage
= None
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
))
179 dc
= wxClientDC(self
)
180 self
.dragShape
.pos
= newPos
181 self
.dragShape
.shown
= true
182 self
.dragShape
.Draw(dc
)
183 self
.dragShape
= None
186 def OnMotion(self
, evt
):
187 if not self
.dragShape
or not evt
.Dragging() or not evt
.LeftIsDown():
190 # if we have a shape, but havn't started dragging yet
191 if self
.dragShape
and not self
.dragImage
:
193 # only start the drag after having moved a couple pixels
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
:
201 if self
.dragShape
.text
:
202 self
.dragImage
= wxDragString(self
.dragShape
.text
,
203 wxStockCursor(wxCURSOR_HAND
))
205 self
.dragImage
= wxDragImage(self
.dragShape
.bmp
,
206 wxStockCursor(wxCURSOR_HAND
))
208 newPos
= wxPoint(self
.dragShape
.pos
.x
+ (pt
.x
- self
.dragStartPos
.x
),
209 self
.dragShape
.pos
.y
+ (pt
.y
- self
.dragStartPos
.y
))
211 if self
.dragShape
.fullscreen
:
212 newPos
= self
.ClientToScreen(newPos
)
213 self
.dragImage
.BeginDrag((0,0), self
, true
)
215 self
.dragImage
.BeginDrag((0,0), self
)
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
)
223 self
.dragImage
.Move(newPos
)
224 self
.dragImage
.Show()
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
)
235 self
.dragImage
.Move(newPos
)
238 #----------------------------------------------------------------------
240 def runTest(frame
, nb
, log
):
241 win
= DragCanvas(nb
, -1)
244 #----------------------------------------------------------------------