]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/demo/wxDragImage.py
mention ShouldInheritColours() change
[wxWidgets.git] / wxPython / demo / wxDragImage.py
index 19985b7dd36a3b478b6a662b0e11b4ab45b52be7..74ade7e0eaad69eca0d5a3431d5f4afaa2c2837f 100644 (file)
@@ -1,20 +1,22 @@
 
 from wxPython.wx import *
 
+import images
+
 #----------------------------------------------------------------------
 
 class DragShape:
     def __init__(self, bmp):
         self.bmp = bmp
         self.pos = wxPoint(0,0)
-        self.shown = true
+        self.shown = True
         self.text = None
-        self.fullscreen = false
+        self.fullscreen = False
 
 
     def HitTest(self, pt):
         rect = self.GetRect()
-        return rect.Inside(pt.x, pt.y)
+        return rect.InsideXY(pt.x, pt.y)
 
 
     def GetRect(self):
@@ -29,11 +31,11 @@ class DragShape:
 
             dc.Blit(self.pos.x, self.pos.y,
                     self.bmp.GetWidth(), self.bmp.GetHeight(),
-                    memDC, 0, 0, op, true)
+                    memDC, 0, 0, op, True)
 
-            return true
+            return True
         else:
-            return false
+            return False
 
 
 
@@ -45,36 +47,36 @@ class DragCanvas(wxScrolledWindow):
         self.shapes = []
         self.dragImage = None
         self.dragShape = None
+        self.hiliteShape = None
 
         self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
-        self.bg_bmp = wxBitmap('bitmaps/backgrnd.png', wxBITMAP_TYPE_PNG)
+        self.bg_bmp = images.getBackgroundBitmap()
 
 
         # Make a shape from an image and mask.  This one will demo
         # dragging outside the window
-        bmp = wxBitmap('bitmaps/test_image.png', wxBITMAP_TYPE_PNG)
-        mask = wxMaskColour(bmp, wxWHITE)
-        bmp.SetMask(mask)
+        bmp = images.getTestStarBitmap()
         shape = DragShape(bmp)
         shape.pos = wxPoint(5, 5)
-        shape.fullscreen = true
+        shape.fullscreen = True
         self.shapes.append(shape)
 
 
         # Make a shape from some text
         text = "Some Text"
+        bg_colour = wxColour(57, 115, 57)  # matches the bg image
         font = wxFont(15, wxROMAN, wxNORMAL, wxBOLD)
         textExtent = self.GetFullTextExtent(text, font)
         bmp = wxEmptyBitmap(textExtent[0], textExtent[1])
         dc = wxMemoryDC()
         dc.SelectObject(bmp)
+        dc.SetBackground(wxBrush(bg_colour, wxSOLID))
         dc.Clear()
         dc.SetTextForeground(wxRED)
         dc.SetFont(font)
         dc.DrawText(text, 0, 0)
         dc.SelectObject(wxNullBitmap)
-        del dc
-        mask = wxMaskColour(bmp, wxWHITE)
+        mask = wxMaskColour(bmp, bg_colour)
         bmp.SetMask(mask)
         shape = DragShape(bmp)
         shape.pos = wxPoint(5, 100)
@@ -84,8 +86,9 @@ class DragCanvas(wxScrolledWindow):
 
         # Make some shapes from some playing card images.
         x = 200
-        for card in ['01c.gif', '10s.gif', '12h.gif', '13d.gif']:
-            bmp = wxBitmap('bitmaps/'+card, wxBITMAP_TYPE_GIF)
+        for card in ['_01c_', '_12h_', '_13d_', '_10s_']:
+            bmpFunc = getattr(images, "get%sBitmap" % card)
+            bmp = bmpFunc()
             shape = DragShape(bmp)
             shape.pos = wxPoint(x, 5)
             self.shapes.append(shape)
@@ -97,8 +100,12 @@ class DragCanvas(wxScrolledWindow):
         EVT_LEFT_DOWN(self, self.OnLeftDown)
         EVT_LEFT_UP(self, self.OnLeftUp)
         EVT_MOTION(self, self.OnMotion)
+        EVT_LEAVE_WINDOW(self, self.OnLeaveWindow)
 
 
+    def OnLeaveWindow(self, evt):
+        pass
+
 
     def TileBackground(self, dc):
         # tile the background bitmap
@@ -136,12 +143,12 @@ class DragCanvas(wxScrolledWindow):
         dc.DestroyClippingRegion()
 
 
-
-
     def OnEraseBackground(self, evt):
         dc = evt.GetDC()
         if not dc:
             dc = wxClientDC(self)
+            rect = self.GetUpdateRegion().GetBox()
+            dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height)
         self.TileBackground(dc)
 
 
@@ -171,14 +178,14 @@ class DragCanvas(wxScrolledWindow):
         self.dragImage.EndDrag()
         self.dragImage = None
 
-        # reposition and draw the shape
-        pt = evt.GetPosition()
-        newPos = wxPoint(self.dragShape.pos.x + (pt.x - self.dragStartPos.x),
-                         self.dragShape.pos.y + (pt.y - self.dragStartPos.y))
-
         dc = wxClientDC(self)
-        self.dragShape.pos = newPos
-        self.dragShape.shown = true
+        if self.hiliteShape:
+            self.hiliteShape.Draw(dc)
+            self.hiliteShape = None
+
+        # reposition and draw the shape
+        self.dragShape.pos = self.dragShape.pos + evt.GetPosition() - self.dragStartPos
+        self.dragShape.shown = True
         self.dragShape.Draw(dc)
         self.dragShape = None
 
@@ -187,17 +194,23 @@ class DragCanvas(wxScrolledWindow):
         if not self.dragShape or not evt.Dragging() or not evt.LeftIsDown():
             return
 
-        # if we have a shape, but havn't started dragging yet
+        # if we have a shape, but haven't started dragging yet
         if self.dragShape and not self.dragImage:
 
             # only start the drag after having moved a couple pixels
-            tolerance = 4
+            tolerance = 2
             pt = evt.GetPosition()
             dx = abs(pt.x - self.dragStartPos.x)
             dy = abs(pt.y - self.dragStartPos.y)
             if dx <= tolerance and dy <= tolerance:
                 return
 
+            # erase the shape since it will be drawn independently now
+            dc = wxClientDC(self)
+            self.dragShape.shown = False
+            self.EraseShape(self.dragShape, dc)
+
+
             if self.dragShape.text:
                 self.dragImage = wxDragString(self.dragShape.text,
                                               wxStockCursor(wxCURSOR_HAND))
@@ -205,40 +218,54 @@ class DragCanvas(wxScrolledWindow):
                 self.dragImage = wxDragImage(self.dragShape.bmp,
                                              wxStockCursor(wxCURSOR_HAND))
 
-            newPos = wxPoint(self.dragShape.pos.x + (pt.x - self.dragStartPos.x),
-                             self.dragShape.pos.y + (pt.y - self.dragStartPos.y))
+            hotspot = self.dragStartPos - self.dragShape.pos
+            self.dragImage.BeginDrag(hotspot, self, self.dragShape.fullscreen)
 
-            if self.dragShape.fullscreen:
-                newPos = self.ClientToScreen(newPos)
-                self.dragImage.BeginDrag((0,0), self, true)
-            else:
-                self.dragImage.BeginDrag((0,0), self)
+            self.dragImage.Move(pt)
+            self.dragImage.Show()
 
 
-            # erase the shape since it will be drawn independently now
-            dc = wxClientDC(self)
-            self.dragShape.shown = false
-            self.EraseShape(self.dragShape, dc)
+        # if we have shape and image then move it, posibly highlighting another shape.
+        elif self.dragShape and self.dragImage:
+            onShape = self.FindShape(evt.GetPosition())
+            unhiliteOld = False
+            hiliteNew = False
 
-            self.dragImage.Move(newPos)
-            self.dragImage.Show()
+            # figure out what to hilite and what to unhilite
+            if self.hiliteShape:
+                if onShape is None or self.hiliteShape is not onShape:
+                    unhiliteOld = True
 
+            if onShape and onShape is not self.hiliteShape and onShape.shown:
+                hiliteNew = True
 
-        # if we have shape and image then move it.
-        elif self.dragShape and self.dragImage:
-            pt = evt.GetPosition()
-            newPos = wxPoint(self.dragShape.pos.x + (pt.x - self.dragStartPos.x),
-                             self.dragShape.pos.y + (pt.y - self.dragStartPos.y))
-            if self.dragShape.fullscreen:
-                newPos = self.ClientToScreen(newPos)
+            # if needed, hide the drag image so we can update the window
+            if unhiliteOld or hiliteNew:
+                self.dragImage.Hide()
+
+            if unhiliteOld:
+                dc = wxClientDC(self)
+                self.hiliteShape.Draw(dc)
+                self.hiliteShape = None
+
+            if hiliteNew:
+                dc = wxClientDC(self)
+                self.hiliteShape = onShape
+                self.hiliteShape.Draw(dc, wxINVERT)
 
-            self.dragImage.Move(newPos)
+            # now move it and show it again if needed
+            self.dragImage.Move(evt.GetPosition())
+            if unhiliteOld or hiliteNew:
+                self.dragImage.Show()
 
 
 #----------------------------------------------------------------------
 
 def runTest(frame, nb, log):
-    win = DragCanvas(nb, -1)
+    win = wxPanel(nb, -1)
+    canvas = DragCanvas(win, -1)
+    def onSize(evt, panel=win, canvas=canvas): canvas.SetSize(panel.GetSize())
+    EVT_SIZE(win, onSize)
     return win
 
 #----------------------------------------------------------------------
@@ -247,3 +274,10 @@ def runTest(frame, nb, log):
 
 overview = """\
 """
+
+
+if __name__ == '__main__':
+    import sys,os
+    import run
+    run.main(['', os.path.basename(sys.argv[0])])
+