]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/DragAndDrop.py
d04bdc7048d9d5e459851f91c740c54d1f181494
1 # 11/5/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
4 # o Got rid of all the hardcoded window IDs.
5 # o Fixed a bug in class TestPanel() (empty sizer Add())
7 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
9 # o Fixed a bug in the BMP file dialog; was using GetFilename()
10 # instead of GetPath() to get the file to load.
15 #----------------------------------------------------------------------
17 ID_CopyBtn
= wx
.NewId()
18 ID_PasteBtn
= wx
.NewId()
19 ID_BitmapBtn
= wx
.NewId()
21 #----------------------------------------------------------------------
23 class ClipTextPanel(wx
.Panel
):
24 def __init__(self
, parent
, log
):
25 wx
.Panel
.__init
__(self
, parent
, -1)
28 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
30 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
33 self
, -1, "Copy/Paste text to/from\n"
34 "this window and other apps"
36 0, wx
.EXPAND|wx
.ALL
, 2
39 self
.text
= wx
.TextCtrl(self
, -1, "", style
=wx
.TE_MULTILINE|wx
.HSCROLL
)
40 sizer
.Add(self
.text
, 1, wx
.EXPAND
)
42 hsz
= wx
.BoxSizer(wx
.HORIZONTAL
)
43 hsz
.Add(wx
.Button(self
, ID_CopyBtn
, " Copy "), 1, wx
.EXPAND|wx
.ALL
, 2)
44 hsz
.Add(wx
.Button(self
, ID_PasteBtn
, " Paste "), 1, wx
.EXPAND|wx
.ALL
, 2)
45 sizer
.Add(hsz
, 0, wx
.EXPAND
)
47 wx
.Button(self
, ID_BitmapBtn
, " Copy Bitmap "),
48 0, wx
.EXPAND|wx
.ALL
, 2
51 self
.Bind(wx
.EVT_BUTTON
, self
.OnCopy
, id=ID_CopyBtn
)
52 self
.Bind(wx
.EVT_BUTTON
, self
.OnPaste
, id=ID_PasteBtn
)
53 self
.Bind(wx
.EVT_BUTTON
, self
.OnCopyBitmap
, id=ID_BitmapBtn
)
55 self
.SetAutoLayout(True)
59 def OnCopy(self
, evt
):
60 self
.do
= wx
.TextDataObject()
61 self
.do
.SetText(self
.text
.GetValue())
62 wx
.TheClipboard
.Open()
63 wx
.TheClipboard
.SetData(self
.do
)
64 wx
.TheClipboard
.Close()
67 def OnPaste(self
, evt
):
68 do
= wx
.TextDataObject()
69 wx
.TheClipboard
.Open()
70 success
= wx
.TheClipboard
.GetData(do
)
71 wx
.TheClipboard
.Close()
74 self
.text
.SetValue(do
.GetText())
77 "There is no data in the clipboard in the required format",
81 def OnCopyBitmap(self
, evt
):
82 dlg
= wx
.FileDialog(self
, "Choose a bitmap to copy", wildcard
="*.bmp")
84 if dlg
.ShowModal() == wx
.ID_OK
:
85 bmp
= wx
.Bitmap(dlg
.GetPath(), wx
.BITMAP_TYPE_BMP
)
86 bmpdo
= wx
.BitmapDataObject(bmp
)
87 wx
.TheClipboard
.Open()
88 wx
.TheClipboard
.SetData(bmpdo
)
89 wx
.TheClipboard
.Close()
92 "The bitmap is now in the Clipboard. Switch to a graphics\n"
93 "editor and try pasting it in..."
98 #----------------------------------------------------------------------
100 class OtherDropTarget(wx
.PyDropTarget
):
101 def __init__(self
, window
, log
):
102 wx
.PyDropTarget
.__init
__(self
)
104 self
.do
= wx
.FileDataObject()
105 self
.SetDataObject(self
.do
)
107 def OnEnter(self
, x
, y
, d
):
108 self
.log
.WriteText("OnEnter: %d, %d, %d\n" % (x
, y
, d
))
111 #def OnDragOver(self, x, y, d):
112 # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
116 self
.log
.WriteText("OnLeave\n")
118 def OnDrop(self
, x
, y
):
119 self
.log
.WriteText("OnDrop: %d %d\n" % (x
, y
))
122 def OnData(self
, x
, y
, d
):
123 self
.log
.WriteText("OnData: %d, %d, %d\n" % (x
, y
, d
))
125 self
.log
.WriteText("%s\n" % self
.do
.GetFilenames())
129 class MyFileDropTarget(wx
.FileDropTarget
):
130 def __init__(self
, window
, log
):
131 wx
.FileDropTarget
.__init
__(self
)
135 def OnDropFiles(self
, x
, y
, filenames
):
136 self
.window
.SetInsertionPointEnd()
137 self
.window
.WriteText("\n%d file(s) dropped at %d,%d:\n" %
138 (len(filenames
), x
, y
))
140 for file in filenames
:
141 self
.window
.WriteText(file + '\n')
144 class MyTextDropTarget(wx
.TextDropTarget
):
145 def __init__(self
, window
, log
):
146 wx
.TextDropTarget
.__init
__(self
)
150 def OnDropText(self
, x
, y
, text
):
151 self
.window
.WriteText("(%d, %d)\n%s\n" % (x
, y
, text
))
153 def OnDragOver(self
, x
, y
, d
):
157 class FileDropPanel(wx
.Panel
):
158 def __init__(self
, parent
, log
):
159 wx
.Panel
.__init
__(self
, parent
, -1)
161 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
163 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
165 wx
.StaticText(self
, -1, " \nDrag some files here:"),
166 0, wx
.EXPAND|wx
.ALL
, 2
169 self
.text
= wx
.TextCtrl(
171 style
= wx
.TE_MULTILINE|wx
.HSCROLL|wx
.TE_READONLY
174 dt
= MyFileDropTarget(self
, log
)
175 self
.text
.SetDropTarget(dt
)
176 sizer
.Add(self
.text
, 1, wx
.EXPAND
)
179 wx
.StaticText(self
, -1, " \nDrag some text here:"),
180 0, wx
.EXPAND|wx
.ALL
, 2
183 self
.text2
= wx
.TextCtrl(
185 style
= wx
.TE_MULTILINE|wx
.HSCROLL|wx
.TE_READONLY
188 dt
= MyTextDropTarget(self
.text2
, log
)
189 self
.text2
.SetDropTarget(dt
)
190 sizer
.Add(self
.text2
, 1, wx
.EXPAND
)
192 self
.SetAutoLayout(True)
196 def WriteText(self
, text
):
197 self
.text
.WriteText(text
)
199 def SetInsertionPointEnd(self
):
200 self
.text
.SetInsertionPointEnd()
203 #----------------------------------------------------------------------
204 #----------------------------------------------------------------------
206 class TestPanel(wx
.Panel
):
207 def __init__(self
, parent
, log
):
208 wx
.Panel
.__init
__(self
, parent
, -1)
210 self
.SetAutoLayout(True)
211 outsideSizer
= wx
.BoxSizer(wx
.VERTICAL
)
213 msg
= "Clipboard / Drag-And-Drop"
214 text
= wx
.StaticText(self
, -1, "", style
=wx
.ALIGN_CENTRE
)
215 text
.SetFont(wx
.Font(24, wx
.SWISS
, wx
.NORMAL
, wx
.BOLD
, False))
218 w
,h
= text
.GetTextExtent(msg
)
219 text
.SetSize(wx
.Size(w
,h
+1))
220 text
.SetForegroundColour(wx
.BLUE
)
221 outsideSizer
.Add(text
, 0, wx
.EXPAND|wx
.ALL
, 5)
222 outsideSizer
.Add(wx
.StaticLine(self
, -1), 0, wx
.EXPAND
)
224 inSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
225 inSizer
.Add(ClipTextPanel(self
, log
), 1, wx
.EXPAND
)
226 inSizer
.Add(FileDropPanel(self
, log
), 1, wx
.EXPAND
)
228 outsideSizer
.Add(inSizer
, 1, wx
.EXPAND
)
229 self
.SetSizer(outsideSizer
)
232 #----------------------------------------------------------------------
234 def runTest(frame
, nb
, log
):
235 win
= TestPanel(nb
, log
)
238 #----------------------------------------------------------------------
244 This demo shows some examples of data transfer through clipboard or
245 drag and drop. In wxWindows, these two ways to transfer data (either
246 between different applications or inside one and the same) are very
247 similar which allows to implement both of them using almost the same
248 code - or, in other words, if you implement drag and drop support for
249 your application, you get clipboard support for free and vice versa.
251 At the heart of both clipboard and drag and drop operations lies the
252 wxDataObject class. The objects of this class (or, to be precise,
253 classes derived from it) represent the data which is being carried by
254 the mouse during drag and drop operation or copied to or pasted from
255 the clipboard. wxDataObject is a "smart" piece of data because it
256 knows which formats it supports (see GetFormatCount and GetAllFormats)
257 and knows how to render itself in any of them (see GetDataHere). It
258 can also receive its value from the outside in a format it supports if
259 it implements the SetData method. Please see the documentation of this
260 class for more details.
262 Both clipboard and drag and drop operations have two sides: the source
263 and target, the data provider and the data receiver. These which may
264 be in the same application and even the same window when, for example,
265 you drag some text from one position to another in a word
266 processor. Let us describe what each of them should do.
272 if __name__
== '__main__':
275 run
.main(['', os
.path
.basename(sys
.argv
[0])])