]>
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 text
= 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. \n\n "
177 "The lower window is accepting a \n "
178 "custom data type that is a pickled \n "
179 "Python list of lines data." )
180 sizer
. Add ( text
, 1 , wxALL
, 10 )
182 insizer
= wxBoxSizer ( wxVERTICAL
)
183 insizer
. Add ( DoodlePad ( self
, log
), 1 , wxEXPAND|wxALL
, 5 )
184 insizer
. Add ( DoodleViewer ( self
, log
), 1 , wxEXPAND|wxALL
, 5 )
186 sizer
. Add ( insizer
, 1 , wxEXPAND
)
187 self
. SetAutoLayout ( true
)
193 #----------------------------------------------------------------------
194 #----------------------------------------------------------------------
196 class TestPanel ( wxPanel
):
197 def __init__ ( self
, parent
, log
):
198 wxPanel
.__ init
__ ( self
, parent
, - 1 )
200 self
. SetAutoLayout ( true
)
201 sizer
= wxBoxSizer ( wxVERTICAL
)
203 msg
= "Custom Drag-And-Drop"
204 text
= wxStaticText ( self
, - 1 , "" , style
= wxALIGN_CENTRE
)
205 text
. SetFont ( wxFont ( 24 , wxSWISS
, wxNORMAL
, wxBOLD
, false
))
207 w
, h
= text
. GetTextExtent ( msg
)
208 text
. SetSize ( wxSize ( w
, h
+ 1 ))
209 text
. SetForegroundColour ( wxBLUE
)
210 sizer
. Add ( text
, 0 , wxEXPAND|wxALL
, 5 )
211 sizer
. Add ( wxStaticLine ( self
, - 1 ), 0 , wxEXPAND
)
213 sizer
. Add ( CustomDnDPanel ( self
, log
), 1 , wxEXPAND
)
218 #----------------------------------------------------------------------
220 def runTest ( frame
, nb
, log
):
221 win
= TestPanel ( nb
, log
)
225 if __name__
== '__main__' :
228 def WriteText ( self
, text
):
229 sys
. stdout
. write ( text
)
231 class TestApp ( wxApp
):
236 def MakeFrame ( self
, event
= None ):
237 frame
= wxFrame ( None , - 1 , "Custom Drag and Drop" , size
=( 550 , 400 ))
239 menu
. Append ( 6543 , "Window" )
241 mb
. Append ( menu
, "New" )
243 EVT_MENU ( frame
, 6543 , self
. MakeFrame
)
244 panel
= TestPanel ( frame
, DummyLog ())
246 self
. SetTopWindow ( frame
)
253 #----------------------------------------------------------------------
267 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.
269 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.
271 The two data objects are combined in a wxDataObjectComposite and the rest is handled by the framework.