]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/CustomDragAndDrop.py
2 from wxPython
. wx
import *
6 #----------------------------------------------------------------------
9 class DoodlePad ( wxWindow
):
10 def __init__ ( self
, parent
, log
):
11 wxWindow
.__ init
__ ( self
, parent
, - 1 , style
= wxSUNKEN_BORDER
)
13 self
. SetBackgroundColour ( wxWHITE
)
16 self
. SetCursor ( wxStockCursor ( wxCURSOR_PENCIL
))
18 EVT_LEFT_DOWN ( self
, self
. OnLeftDown
)
19 EVT_LEFT_UP ( self
, self
. OnLeftUp
)
20 EVT_RIGHT_UP ( self
, self
. OnRightUp
)
21 EVT_MOTION ( self
, self
. OnMotion
)
24 def OnPaint ( self
, event
):
26 self
. DrawSavedLines ( dc
)
28 def DrawSavedLines ( self
, dc
):
30 dc
. SetPen ( wxPen ( wxBLUE
, 3 ))
31 for line
in self
. lines
:
33 apply ( dc
. DrawLine
, coords
)
37 def OnLeftDown ( self
, event
):
38 if event
. ControlDown ():
39 self
. StartDragOpperation ()
42 self
. x
, self
. y
= event
. GetPositionTuple ()
46 def OnLeftUp ( self
, event
):
47 self
. lines
. append ( self
. curLine
)
51 def OnRightUp ( self
, event
):
55 def OnMotion ( self
, event
):
56 if event
. Dragging () and not event
. ControlDown ():
59 dc
. SetPen ( wxPen ( wxBLUE
, 3 ))
60 coords
= ( self
. x
, self
. y
) + event
. GetPositionTuple ()
61 self
. curLine
. append ( coords
)
62 apply ( dc
. DrawLine
, coords
)
63 self
. x
, self
. y
= event
. GetPositionTuple ()
67 def StartDragOpperation ( self
):
68 # pickle the lines list
69 linesdata
= cPickle
. dumps ( self
. lines
, 1 )
71 # create our own data format and use it in a
73 ldata
= wxCustomDataObject ( wxCustomDataFormat ( "DoodleLines" ))
74 ldata
. SetData ( linesdata
)
76 # Also create a Bitmap version of the drawing
78 bmp
= wxEmptyBitmap ( size
. width
, size
. height
)
81 dc
. SetBackground ( wxWHITE_BRUSH
)
83 self
. DrawSavedLines ( dc
)
84 dc
. SelectObject ( wxNullBitmap
)
86 # Now make a data object for the bitmap and also a composite
87 # data object holding both of the others.
88 bdata
= wxBitmapDataObject ( bmp
)
89 data
= wxDataObjectComposite ()
93 # And finally, create the drop source and begin the drag
95 dropSource
= wxDropSource ( self
)
96 dropSource
. SetData ( data
)
97 self
. log
. WriteText ( "Begining DragDrop \n " )
98 result
= dropSource
. DoDragDrop ()
99 self
. log
. WriteText ( "DragDrop completed: %d \n " % result
)
101 #----------------------------------------------------------------------
104 class DoodleDropTarget ( wxPyDropTarget
):
105 def __init__ ( self
, window
, log
):
106 wxPyDropTarget
.__ init
__ ( self
)
109 self
. data
= wxCustomDataObject ( wxCustomDataFormat ( "DoodleLines" ))
110 self
. SetDataObject ( self
. data
)
112 def OnEnter ( self
, x
, y
, d
):
113 self
. log
. WriteText ( "OnEnter: %d , %d , %d \n " % ( x
, y
, d
))
117 self
. log
. WriteText ( "OnLeave \n " )
119 def OnDrop ( self
, x
, y
):
120 self
. log
. WriteText ( "OnDrop: %d %d \n " % ( x
, y
))
123 def OnData ( self
, x
, y
, d
):
124 self
. log
. WriteText ( "OnData: %d , %d , %d \n " % ( x
, y
, d
))
126 linesdata
= self
. data
. GetData ()
127 lines
= cPickle
. loads ( linesdata
)
128 self
. dv
. SetLines ( lines
)
131 #def OnDragOver(self, x, y, d):
132 # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
137 class DoodleViewer ( wxWindow
):
138 def __init__ ( self
, parent
, log
):
139 wxWindow
.__ init
__ ( self
, parent
, - 1 , style
= wxSUNKEN_BORDER
)
141 self
. SetBackgroundColour ( wxWHITE
)
144 dt
= DoodleDropTarget ( self
, log
)
145 self
. SetDropTarget ( dt
)
147 def SetLines ( self
, lines
):
151 def OnPaint ( self
, event
):
153 self
. DrawSavedLines ( dc
)
155 def DrawSavedLines ( self
, dc
):
157 dc
. SetPen ( wxPen ( wxRED
, 3 ))
158 for line
in self
. lines
:
160 apply ( dc
. DrawLine
, coords
)
163 #----------------------------------------------------------------------
165 class CustomDnDPanel ( wxPanel
):
166 def __init__ ( self
, parent
, log
):
167 wxPanel
.__ init
__ ( self
, parent
, - 1 )
169 self
. SetFont ( wxFont ( 10 , wxSWISS
, wxNORMAL
, wxBOLD
, false
))
171 sizer
= wxBoxSizer ( wxHORIZONTAL
)
172 sizer
. Add ( wxStaticText ( self
, - 1 ,
173 "Draw a little picture in this window \n "
174 "then Ctrl-Drag it to the lower \n "
175 "window or to another application \n "
176 "that accepts BMP's as a drop target." ),
177 1 , wxEXPAND|wxALL
, 10 )
179 insizer
= wxBoxSizer ( wxVERTICAL
)
180 insizer
. Add ( DoodlePad ( self
, log
), 1 , wxEXPAND|wxALL
, 5 )
181 insizer
. Add ( DoodleViewer ( self
, log
), 1 , wxEXPAND|wxALL
, 5 )
183 sizer
. Add ( insizer
, 1 , wxEXPAND
)
184 self
. SetAutoLayout ( true
)
190 #----------------------------------------------------------------------
191 #----------------------------------------------------------------------
193 class TestPanel ( wxPanel
):
194 def __init__ ( self
, parent
, log
):
195 wxPanel
.__ init
__ ( self
, parent
, - 1 )
197 self
. SetAutoLayout ( true
)
198 sizer
= wxBoxSizer ( wxVERTICAL
)
200 msg
= "Custom Drag-And-Drop"
201 text
= wxStaticText ( self
, - 1 , "" , style
= wxALIGN_CENTRE
)
202 text
. SetFont ( wxFont ( 24 , wxSWISS
, wxNORMAL
, wxBOLD
, false
))
204 w
, h
= text
. GetTextExtent ( msg
)
205 text
. SetSize ( wxSize ( w
, h
+ 1 ))
206 text
. SetForegroundColour ( wxBLUE
)
207 sizer
. Add ( text
, 0 , wxEXPAND|wxALL
, 5 )
208 sizer
. Add ( wxStaticLine ( self
, - 1 ), 0 , wxEXPAND
)
210 sizer
. Add ( CustomDnDPanel ( self
, log
), 1 , wxEXPAND
)
215 #----------------------------------------------------------------------
217 def runTest ( frame
, nb
, log
):
218 win
= TestPanel ( nb
, log
)
221 #----------------------------------------------------------------------
235 This demo shows Drag and Drop using a custom data type and a custom data object. A type called "DoodleLines" is created and a Python Pickle of a list is actually transfered in the drag and drop opperation.
237 A second data object is also created containing a bitmap of the image and is made available to any drop target that accepts bitmaps, such as MS Word.
239 The two data objects are combined in a wxDataObjectComposite and the rest is handled by the framework.