]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxDragImage.py
   2 from wxPython
.wx 
import * 
   6 #---------------------------------------------------------------------- 
   9     def __init__(self
, bmp
): 
  11         self
.pos 
= wxPoint(0,0) 
  14         self
.fullscreen 
= false
 
  17     def HitTest(self
, pt
): 
  19         return rect
.Inside(pt
.x
, pt
.y
) 
  23         return wxRect(self
.pos
.x
, self
.pos
.y
, 
  24                       self
.bmp
.GetWidth(), self
.bmp
.GetHeight()) 
  27     def Draw(self
, dc
, op 
= wxCOPY
): 
  30             memDC
.SelectObject(self
.bmp
) 
  32             dc
.Blit(self
.pos
.x
, self
.pos
.y
, 
  33                     self
.bmp
.GetWidth(), self
.bmp
.GetHeight(), 
  34                     memDC
, 0, 0, op
, true
) 
  42 #---------------------------------------------------------------------- 
  44 class DragCanvas(wxScrolledWindow
): 
  45     def __init__(self
, parent
, ID
): 
  46         wxScrolledWindow
.__init
__(self
, parent
, ID
) 
  50         self
.hiliteShape 
= None 
  52         self
.SetCursor(wxStockCursor(wxCURSOR_ARROW
)) 
  53         self
.bg_bmp 
= images
.getBackgroundBitmap() 
  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
) 
  65         # Make a shape from some text 
  67         bg_colour 
= wxColour(57, 115, 57)  # matches the bg image 
  68         font 
= wxFont(15, wxROMAN
, wxNORMAL
, wxBOLD
) 
  69         textExtent 
= self
.GetFullTextExtent(text
, font
) 
  70         bmp 
= wxEmptyBitmap(textExtent
[0], textExtent
[1]) 
  73         dc
.SetBackground(wxBrush(bg_colour
, wxSOLID
)) 
  75         dc
.SetTextForeground(wxRED
) 
  77         dc
.DrawText(text
, 0, 0) 
  78         dc
.SelectObject(wxNullBitmap
) 
  79         mask 
= wxMaskColour(bmp
, bg_colour
) 
  81         shape 
= DragShape(bmp
) 
  82         shape
.pos 
= wxPoint(5, 100) 
  83         shape
.text 
= "Some dragging text" 
  84         self
.shapes
.append(shape
) 
  87         # Make some shapes from some playing card images. 
  89         for card 
in ['_01c_', '_12h_', '_13d_', '_10s_']: 
  90             bmpFunc 
= getattr(images
, "get%sBitmap" % card
) 
  92             shape 
= DragShape(bmp
) 
  93             shape
.pos 
= wxPoint(x
, 5) 
  94             self
.shapes
.append(shape
) 
  98         EVT_ERASE_BACKGROUND(self
, self
.OnEraseBackground
) 
  99         EVT_PAINT(self
, self
.OnPaint
) 
 100         EVT_LEFT_DOWN(self
, self
.OnLeftDown
) 
 101         EVT_LEFT_UP(self
, self
.OnLeftUp
) 
 102         EVT_MOTION(self
, self
.OnMotion
) 
 103         EVT_LEAVE_WINDOW(self
, self
.OnLeaveWindow
) 
 106     def OnLeaveWindow(self
, evt
): 
 110     def TileBackground(self
, dc
): 
 111         # tile the background bitmap 
 112         sz 
= self
.GetClientSize() 
 113         w 
= self
.bg_bmp
.GetWidth() 
 114         h 
= self
.bg_bmp
.GetHeight() 
 120                 dc
.DrawBitmap(self
.bg_bmp
, x
, y
) 
 125     def DrawShapes(self
, dc
): 
 126         for shape 
in self
.shapes
: 
 131     def FindShape(self
, pt
): 
 132         for shape 
in self
.shapes
: 
 133             if shape
.HitTest(pt
): 
 138     def EraseShape(self
, shape
, dc
): 
 140         dc
.SetClippingRegion(r
.x
, r
.y
, r
.width
, r
.height
) 
 141         self
.TileBackground(dc
) 
 143         dc
.DestroyClippingRegion() 
 146     def OnEraseBackground(self
, evt
): 
 149             dc 
= wxClientDC(self
) 
 150             rect 
= self
.GetUpdateRegion().GetBox() 
 151             dc
.SetClippingRegion(rect
.x
, rect
.y
, rect
.width
, rect
.height
) 
 152         self
.TileBackground(dc
) 
 155     def OnPaint(self
, evt
): 
 161     def OnLeftDown(self
, evt
): 
 162         shape 
= self
.FindShape(evt
.GetPosition()) 
 164             # get ready to start dragging, but wait for the user to 
 165             # move it a bit first 
 166             self
.dragShape 
= shape
 
 167             self
.dragStartPos 
= evt
.GetPosition() 
 170     def OnLeftUp(self
, evt
): 
 171         if not self
.dragImage 
or not self
.dragShape
: 
 172             self
.dragImage 
= None 
 173             self
.dragShape 
= None 
 177         self
.dragImage
.Hide() 
 178         self
.dragImage
.EndDrag() 
 179         self
.dragImage 
= None 
 181         dc 
= wxClientDC(self
) 
 183             self
.hiliteShape
.Draw(dc
) 
 184             self
.hiliteShape 
= None 
 186         # reposition and draw the shape 
 187         self
.dragShape
.pos 
= self
.dragShape
.pos 
+ evt
.GetPosition() - self
.dragStartPos
 
 188         self
.dragShape
.shown 
= true
 
 189         self
.dragShape
.Draw(dc
) 
 190         self
.dragShape 
= None 
 193     def OnMotion(self
, evt
): 
 194         if not self
.dragShape 
or not evt
.Dragging() or not evt
.LeftIsDown(): 
 197         # if we have a shape, but havn't started dragging yet 
 198         if self
.dragShape 
and not self
.dragImage
: 
 200             # only start the drag after having moved a couple pixels 
 202             pt 
= evt
.GetPosition() 
 203             dx 
= abs(pt
.x 
- self
.dragStartPos
.x
) 
 204             dy 
= abs(pt
.y 
- self
.dragStartPos
.y
) 
 205             if dx 
<= tolerance 
and dy 
<= tolerance
: 
 208             # erase the shape since it will be drawn independently now 
 209             dc 
= wxClientDC(self
) 
 210             self
.dragShape
.shown 
= false
 
 211             self
.EraseShape(self
.dragShape
, dc
) 
 214             if self
.dragShape
.text
: 
 215                 self
.dragImage 
= wxDragString(self
.dragShape
.text
, 
 216                                               wxStockCursor(wxCURSOR_HAND
)) 
 218                 self
.dragImage 
= wxDragImage(self
.dragShape
.bmp
, 
 219                                              wxStockCursor(wxCURSOR_HAND
)) 
 221             hotspot 
= self
.dragStartPos 
- self
.dragShape
.pos
 
 222             self
.dragImage
.BeginDrag(hotspot
, self
, self
.dragShape
.fullscreen
) 
 224             self
.dragImage
.Move(pt
) 
 225             self
.dragImage
.Show() 
 228         # if we have shape and image then move it, posibly highlighting another shape. 
 229         elif self
.dragShape 
and self
.dragImage
: 
 230             onShape 
= self
.FindShape(evt
.GetPosition()) 
 234             # figure out what to hilite and what to unhilite 
 236                 if onShape 
is None or self
.hiliteShape 
is not onShape
: 
 239             if onShape 
and onShape 
is not self
.hiliteShape 
and onShape
.shown
: 
 242             # if needed, hide the drag image so we can update the window 
 243             if unhiliteOld 
or hiliteNew
: 
 244                 self
.dragImage
.Hide() 
 247                 dc 
= wxClientDC(self
) 
 248                 self
.hiliteShape
.Draw(dc
) 
 249                 self
.hiliteShape 
= None 
 252                 dc 
= wxClientDC(self
) 
 253                 self
.hiliteShape 
= onShape
 
 254                 self
.hiliteShape
.Draw(dc
, wxINVERT
) 
 256             # now move it and show it again if needed 
 257             self
.dragImage
.Move(evt
.GetPosition()) 
 258             if unhiliteOld 
or hiliteNew
: 
 259                 self
.dragImage
.Show() 
 262 #---------------------------------------------------------------------- 
 264 def runTest(frame
, nb
, log
): 
 265     win 
= wxPanel(nb
, -1) 
 266     canvas 
= DragCanvas(win
, -1) 
 267     def onSize(evt
, panel
=win
, canvas
=canvas
): canvas
.SetSize(panel
.GetSize()) 
 268     EVT_SIZE(win
, onSize
) 
 271 #----------------------------------------------------------------------