2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
import wx
|
|
|
|
import images
|
2001-04-09 15:36:36 -04:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class DragShape:
|
|
|
|
def __init__(self, bmp):
|
|
|
|
self.bmp = bmp
|
2003-12-08 20:23:28 -05:00
|
|
|
self.pos = (0,0)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.shown = True
|
2000-07-15 15:51:35 -04:00
|
|
|
self.text = None
|
2003-03-25 01:35:27 -05:00
|
|
|
self.fullscreen = False
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
def HitTest(self, pt):
|
|
|
|
rect = self.GetRect()
|
2003-03-25 01:35:27 -05:00
|
|
|
return rect.InsideXY(pt.x, pt.y)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
def GetRect(self):
|
2003-12-08 20:23:28 -05:00
|
|
|
return wx.Rect(self.pos[0], self.pos[1],
|
2000-07-15 15:51:35 -04:00
|
|
|
self.bmp.GetWidth(), self.bmp.GetHeight())
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
def Draw(self, dc, op = wx.COPY):
|
2000-07-15 15:51:35 -04:00
|
|
|
if self.bmp.Ok():
|
2003-12-08 20:23:28 -05:00
|
|
|
memDC = wx.MemoryDC()
|
2000-07-15 15:51:35 -04:00
|
|
|
memDC.SelectObject(self.bmp)
|
|
|
|
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.Blit(self.pos[0], self.pos[1],
|
|
|
|
self.bmp.GetWidth(), self.bmp.GetHeight(),
|
|
|
|
memDC, 0, 0, op, True)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
return True
|
2000-07-15 15:51:35 -04:00
|
|
|
else:
|
2003-03-25 01:35:27 -05:00
|
|
|
return False
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class DragCanvas(wx.ScrolledWindow):
|
2000-07-15 15:51:35 -04:00
|
|
|
def __init__(self, parent, ID):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.ScrolledWindow.__init__(self, parent, ID)
|
2000-07-15 15:51:35 -04:00
|
|
|
self.shapes = []
|
|
|
|
self.dragImage = None
|
|
|
|
self.dragShape = None
|
2001-05-18 19:46:30 -04:00
|
|
|
self.hiliteShape = None
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
|
2001-04-09 15:36:36 -04:00
|
|
|
self.bg_bmp = images.getBackgroundBitmap()
|
2006-01-21 02:07:28 -05:00
|
|
|
self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
# Make a shape from an image and mask. This one will demo
|
|
|
|
# dragging outside the window
|
2001-04-09 15:36:36 -04:00
|
|
|
bmp = images.getTestStarBitmap()
|
2005-05-24 16:55:42 -04:00
|
|
|
#bmp = wx.Bitmap('bitmaps/toucan.png')
|
2000-07-15 15:51:35 -04:00
|
|
|
shape = DragShape(bmp)
|
2003-12-08 20:23:28 -05:00
|
|
|
shape.pos = (5, 5)
|
2003-03-25 01:35:27 -05:00
|
|
|
shape.fullscreen = True
|
2000-07-15 15:51:35 -04:00
|
|
|
self.shapes.append(shape)
|
|
|
|
|
2007-02-07 11:24:56 -05:00
|
|
|
bmp = images.getTheKidBitmap()
|
|
|
|
shape = DragShape(bmp)
|
|
|
|
shape.pos = (200, 5)
|
|
|
|
self.shapes.append(shape)
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
# Make a shape from some text
|
|
|
|
text = "Some Text"
|
2003-12-08 20:23:28 -05:00
|
|
|
bg_colour = wx.Colour(57, 115, 57) # matches the bg image
|
|
|
|
font = wx.Font(15, wx.ROMAN, wx.NORMAL, wx.BOLD)
|
2000-07-15 15:51:35 -04:00
|
|
|
textExtent = self.GetFullTextExtent(text, font)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
# create a bitmap the same size as our text
|
|
|
|
bmp = wx.EmptyBitmap(textExtent[0], textExtent[1])
|
|
|
|
|
|
|
|
# 'draw' the text onto the bitmap
|
|
|
|
dc = wx.MemoryDC()
|
2000-07-15 15:51:35 -04:00
|
|
|
dc.SelectObject(bmp)
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetBackground(wx.Brush(bg_colour, wx.SOLID))
|
2000-07-15 15:51:35 -04:00
|
|
|
dc.Clear()
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetTextForeground(wx.RED)
|
2000-07-15 15:51:35 -04:00
|
|
|
dc.SetFont(font)
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawText(text, 0, 0)
|
2003-12-11 14:55:48 -05:00
|
|
|
dc.SelectObject(wx.NullBitmap)
|
2004-05-01 22:41:33 -04:00
|
|
|
mask = wx.Mask(bmp, bg_colour)
|
2000-07-15 15:51:35 -04:00
|
|
|
bmp.SetMask(mask)
|
|
|
|
shape = DragShape(bmp)
|
2003-12-08 20:23:28 -05:00
|
|
|
shape.pos = (5, 100)
|
2000-07-15 15:51:35 -04:00
|
|
|
shape.text = "Some dragging text"
|
|
|
|
self.shapes.append(shape)
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
|
|
|
|
self.Bind(wx.EVT_PAINT, self.OnPaint)
|
|
|
|
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
|
|
|
|
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
|
|
|
|
self.Bind(wx.EVT_MOTION, self.OnMotion)
|
|
|
|
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
# We're not doing anything here, but you might have reason to.
|
|
|
|
# for example, if you were dragging something, you might elect to
|
|
|
|
# 'drop it' when the cursor left the window.
|
2002-05-27 13:53:27 -04:00
|
|
|
def OnLeaveWindow(self, evt):
|
|
|
|
pass
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# tile the background bitmap
|
2000-07-15 15:51:35 -04:00
|
|
|
def TileBackground(self, dc):
|
|
|
|
sz = self.GetClientSize()
|
|
|
|
w = self.bg_bmp.GetWidth()
|
|
|
|
h = self.bg_bmp.GetHeight()
|
|
|
|
|
|
|
|
x = 0
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
while x < sz.width:
|
|
|
|
y = 0
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
while y < sz.height:
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawBitmap(self.bg_bmp, x, y)
|
2000-07-15 15:51:35 -04:00
|
|
|
y = y + h
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
x = x + w
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Go through our list of shapes and draw them in whatever place they are.
|
2000-07-15 15:51:35 -04:00
|
|
|
def DrawShapes(self, dc):
|
|
|
|
for shape in self.shapes:
|
|
|
|
if shape.shown:
|
|
|
|
shape.Draw(dc)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# This is actually a sophisticated 'hit test', but in this
|
|
|
|
# case we're also determining which shape, if any, was 'hit'.
|
2000-07-15 15:51:35 -04:00
|
|
|
def FindShape(self, pt):
|
|
|
|
for shape in self.shapes:
|
|
|
|
if shape.HitTest(pt):
|
|
|
|
return shape
|
|
|
|
return None
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Remove a shape from the display
|
2000-07-15 15:51:35 -04:00
|
|
|
def EraseShape(self, shape, dc):
|
|
|
|
r = shape.GetRect()
|
2003-11-28 13:42:44 -05:00
|
|
|
dc.SetClippingRect(r)
|
2000-07-15 15:51:35 -04:00
|
|
|
self.TileBackground(dc)
|
|
|
|
self.DrawShapes(dc)
|
|
|
|
dc.DestroyClippingRegion()
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Clears the background, then redraws it. If the DC is passed, then
|
|
|
|
# we only do so in the area so designated. Otherwise, it's the whole thing.
|
2000-07-15 15:51:35 -04:00
|
|
|
def OnEraseBackground(self, evt):
|
|
|
|
dc = evt.GetDC()
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
if not dc:
|
2006-07-24 14:20:18 -04:00
|
|
|
dc = wx.ClientDC(self)
|
2001-07-05 13:27:59 -04:00
|
|
|
rect = self.GetUpdateRegion().GetBox()
|
2003-11-28 13:42:44 -05:00
|
|
|
dc.SetClippingRect(rect)
|
2000-07-15 15:51:35 -04:00
|
|
|
self.TileBackground(dc)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Fired whenever a paint event occurs
|
2000-07-15 15:51:35 -04:00
|
|
|
def OnPaint(self, evt):
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.PaintDC(self)
|
2000-07-15 15:51:35 -04:00
|
|
|
self.PrepareDC(dc)
|
|
|
|
self.DrawShapes(dc)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Left mouse button is down.
|
2000-07-15 15:51:35 -04:00
|
|
|
def OnLeftDown(self, evt):
|
2003-12-08 20:23:28 -05:00
|
|
|
# Did the mouse go down on one of our shapes?
|
2000-07-15 15:51:35 -04:00
|
|
|
shape = self.FindShape(evt.GetPosition())
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
# If a shape was 'hit', then set that as the shape we're going to
|
|
|
|
# drag around. Get our start position. Dragging has not yet started.
|
|
|
|
# That will happen once the mouse moves, OR the mouse is released.
|
2000-07-15 15:51:35 -04:00
|
|
|
if shape:
|
|
|
|
self.dragShape = shape
|
|
|
|
self.dragStartPos = evt.GetPosition()
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Left mouse button up.
|
2000-07-15 15:51:35 -04:00
|
|
|
def OnLeftUp(self, evt):
|
|
|
|
if not self.dragImage or not self.dragShape:
|
|
|
|
self.dragImage = None
|
|
|
|
self.dragShape = None
|
|
|
|
return
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Hide the image, end dragging, and nuke out the drag image.
|
2000-07-15 15:51:35 -04:00
|
|
|
self.dragImage.Hide()
|
|
|
|
self.dragImage.EndDrag()
|
|
|
|
self.dragImage = None
|
|
|
|
|
2001-05-18 19:46:30 -04:00
|
|
|
if self.hiliteShape:
|
2006-07-24 14:20:18 -04:00
|
|
|
self.RefreshRect(self.hiliteShape.GetRect())
|
2001-05-18 19:46:30 -04:00
|
|
|
self.hiliteShape = None
|
|
|
|
|
|
|
|
# reposition and draw the shape
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
# Note by jmg 11/28/03
|
|
|
|
# Here's the original:
|
|
|
|
#
|
|
|
|
# self.dragShape.pos = self.dragShape.pos + evt.GetPosition() - self.dragStartPos
|
|
|
|
#
|
|
|
|
# So if there are any problems associated with this, use that as
|
|
|
|
# a starting place in your investigation. I've tried to simulate the
|
|
|
|
# wx.Point __add__ method here -- it won't work for tuples as we
|
|
|
|
# have now from the various methods
|
|
|
|
#
|
|
|
|
# There must be a better way to do this :-)
|
|
|
|
#
|
|
|
|
|
|
|
|
self.dragShape.pos = (
|
|
|
|
self.dragShape.pos[0] + evt.GetPosition()[0] - self.dragStartPos[0],
|
|
|
|
self.dragShape.pos[1] + evt.GetPosition()[1] - self.dragStartPos[1]
|
|
|
|
)
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.dragShape.shown = True
|
2006-07-24 14:20:18 -04:00
|
|
|
self.RefreshRect(self.dragShape.GetRect())
|
2000-07-15 15:51:35 -04:00
|
|
|
self.dragShape = None
|
|
|
|
|
2006-07-24 14:20:18 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# The mouse is moving
|
2000-07-15 15:51:35 -04:00
|
|
|
def OnMotion(self, evt):
|
2003-12-08 20:23:28 -05:00
|
|
|
# Ignore mouse movement if we're not dragging.
|
2000-07-15 15:51:35 -04:00
|
|
|
if not self.dragShape or not evt.Dragging() or not evt.LeftIsDown():
|
|
|
|
return
|
|
|
|
|
2003-10-01 20:58:06 -04:00
|
|
|
# if we have a shape, but haven't started dragging yet
|
2000-07-15 15:51:35 -04:00
|
|
|
if self.dragShape and not self.dragImage:
|
|
|
|
|
|
|
|
# only start the drag after having moved a couple pixels
|
2001-05-18 19:46:30 -04:00
|
|
|
tolerance = 2
|
2000-07-15 15:51:35 -04:00
|
|
|
pt = evt.GetPosition()
|
|
|
|
dx = abs(pt.x - self.dragStartPos.x)
|
|
|
|
dy = abs(pt.y - self.dragStartPos.y)
|
|
|
|
if dx <= tolerance and dy <= tolerance:
|
|
|
|
return
|
|
|
|
|
2001-05-18 19:46:30 -04:00
|
|
|
# erase the shape since it will be drawn independently now
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.ClientDC(self)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.dragShape.shown = False
|
2001-05-18 19:46:30 -04:00
|
|
|
self.EraseShape(self.dragShape, dc)
|
|
|
|
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
if self.dragShape.text:
|
2003-12-08 20:23:28 -05:00
|
|
|
self.dragImage = wx.DragString(self.dragShape.text,
|
|
|
|
wx.StockCursor(wx.CURSOR_HAND))
|
2000-07-15 15:51:35 -04:00
|
|
|
else:
|
2003-12-08 20:23:28 -05:00
|
|
|
self.dragImage = wx.DragImage(self.dragShape.bmp,
|
|
|
|
wx.StockCursor(wx.CURSOR_HAND))
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2001-05-18 19:46:30 -04:00
|
|
|
hotspot = self.dragStartPos - self.dragShape.pos
|
|
|
|
self.dragImage.BeginDrag(hotspot, self, self.dragShape.fullscreen)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2001-05-18 19:46:30 -04:00
|
|
|
self.dragImage.Move(pt)
|
2000-07-15 15:51:35 -04:00
|
|
|
self.dragImage.Show()
|
|
|
|
|
|
|
|
|
2001-05-18 19:46:30 -04:00
|
|
|
# if we have shape and image then move it, posibly highlighting another shape.
|
2000-07-15 15:51:35 -04:00
|
|
|
elif self.dragShape and self.dragImage:
|
2001-05-18 19:46:30 -04:00
|
|
|
onShape = self.FindShape(evt.GetPosition())
|
2003-03-25 01:35:27 -05:00
|
|
|
unhiliteOld = False
|
|
|
|
hiliteNew = False
|
2001-05-18 19:46:30 -04:00
|
|
|
|
|
|
|
# figure out what to hilite and what to unhilite
|
|
|
|
if self.hiliteShape:
|
|
|
|
if onShape is None or self.hiliteShape is not onShape:
|
2003-03-25 01:35:27 -05:00
|
|
|
unhiliteOld = True
|
2001-05-18 19:46:30 -04:00
|
|
|
|
|
|
|
if onShape and onShape is not self.hiliteShape and onShape.shown:
|
2003-03-25 01:35:27 -05:00
|
|
|
hiliteNew = True
|
2001-05-18 19:46:30 -04:00
|
|
|
|
|
|
|
# if needed, hide the drag image so we can update the window
|
|
|
|
if unhiliteOld or hiliteNew:
|
|
|
|
self.dragImage.Hide()
|
|
|
|
|
|
|
|
if unhiliteOld:
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.ClientDC(self)
|
2001-05-18 19:46:30 -04:00
|
|
|
self.hiliteShape.Draw(dc)
|
|
|
|
self.hiliteShape = None
|
|
|
|
|
|
|
|
if hiliteNew:
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.ClientDC(self)
|
2001-05-18 19:46:30 -04:00
|
|
|
self.hiliteShape = onShape
|
2003-12-08 20:23:28 -05:00
|
|
|
self.hiliteShape.Draw(dc, wx.INVERT)
|
2001-05-18 19:46:30 -04:00
|
|
|
|
|
|
|
# now move it and show it again if needed
|
|
|
|
self.dragImage.Move(evt.GetPosition())
|
|
|
|
if unhiliteOld or hiliteNew:
|
|
|
|
self.dragImage.Show()
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
win = wx.Panel(nb, -1)
|
2002-07-06 14:13:59 -04:00
|
|
|
canvas = DragCanvas(win, -1)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
def onSize(evt, panel=win, canvas=canvas):
|
|
|
|
canvas.SetSize(panel.GetSize())
|
|
|
|
|
|
|
|
win.Bind(wx.EVT_SIZE, onSize)
|
2000-07-15 15:51:35 -04:00
|
|
|
return win
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overview = """\
|
2003-12-08 20:23:28 -05:00
|
|
|
DragImage is used when you wish to drag an object on the screen, and a simple
|
|
|
|
cursor is not enough.
|
|
|
|
|
|
|
|
On Windows, the WIN32 API is used to do achieve smooth dragging. On other
|
|
|
|
platforms, <code>GenericDragImage</code> is used. Applications may also prefer to use
|
|
|
|
<code>GenericDragImage</code> on Windows, too.
|
|
|
|
|
|
|
|
<b>wxPython note</b>: wxPython uses <code>GenericDragImage</code> on all
|
|
|
|
platforms, but uses the <code>DragImage</code> name.
|
|
|
|
|
|
|
|
To use this class, when you wish to start dragging an image, create a
|
|
|
|
<code>DragImage</code> object and store it somewhere you can access it as the
|
|
|
|
drag progresses. Call BeginDrag to start, and EndDrag to stop the drag. To move
|
|
|
|
the image, initially call Show and then Move. If you wish to update the screen
|
|
|
|
contents during the drag (for example, highlight an item as in the example), first
|
|
|
|
call Hide, update the screen, call Move, and then call Show.
|
|
|
|
|
|
|
|
You can drag within one window, or you can use full-screen dragging either across
|
|
|
|
the whole screen, or just restricted to one area of the screen to save resources.
|
|
|
|
If you want the user to drag between two windows, then you will need to use
|
|
|
|
full-screen dragging.
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
"""
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
2003-03-25 01:35:27 -05:00
|
|
|
|