]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/DragAndDrop.py
4 #----------------------------------------------------------------------
6 ID_CopyBtn
= wx
.NewId()
7 ID_PasteBtn
= wx
.NewId()
8 ID_BitmapBtn
= wx
.NewId()
10 #----------------------------------------------------------------------
12 class ClipTextPanel(wx
.Panel
):
13 def __init__(self
, parent
, log
):
14 wx
.Panel
.__init
__(self
, parent
, -1)
17 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
19 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
22 self
, -1, "Copy/Paste text to/from\n"
23 "this window and other apps"
25 0, wx
.EXPAND|wx
.ALL
, 2
28 self
.text
= wx
.TextCtrl(self
, -1, "", style
=wx
.TE_MULTILINE|wx
.HSCROLL
)
29 sizer
.Add(self
.text
, 1, wx
.EXPAND
)
31 hsz
= wx
.BoxSizer(wx
.HORIZONTAL
)
32 hsz
.Add(wx
.Button(self
, ID_CopyBtn
, " Copy "), 1, wx
.EXPAND|wx
.ALL
, 2)
33 hsz
.Add(wx
.Button(self
, ID_PasteBtn
, " Paste "), 1, wx
.EXPAND|wx
.ALL
, 2)
34 sizer
.Add(hsz
, 0, wx
.EXPAND
)
36 wx
.Button(self
, ID_BitmapBtn
, " Copy Bitmap "),
37 0, wx
.EXPAND|wx
.ALL
, 2
40 self
.Bind(wx
.EVT_BUTTON
, self
.OnCopy
, id=ID_CopyBtn
)
41 self
.Bind(wx
.EVT_BUTTON
, self
.OnPaste
, id=ID_PasteBtn
)
42 self
.Bind(wx
.EVT_BUTTON
, self
.OnCopyBitmap
, id=ID_BitmapBtn
)
44 self
.SetAutoLayout(True)
48 def OnCopy(self
, evt
):
49 self
.do
= wx
.TextDataObject()
50 self
.do
.SetText(self
.text
.GetValue())
51 if wx
.TheClipboard
.Open():
52 wx
.TheClipboard
.SetData(self
.do
)
53 wx
.TheClipboard
.Close()
55 wx
.MessageBox("Unable to open the clipboard", "Error")
58 def OnPaste(self
, evt
):
60 do
= wx
.TextDataObject()
61 if wx
.TheClipboard
.Open():
62 success
= wx
.TheClipboard
.GetData(do
)
63 wx
.TheClipboard
.Close()
66 self
.text
.SetValue(do
.GetText())
69 "There is no data in the clipboard in the required format",
73 def OnCopyBitmap(self
, evt
):
74 dlg
= wx
.FileDialog(self
, "Choose a bitmap to copy", wildcard
="*.bmp")
76 if dlg
.ShowModal() == wx
.ID_OK
:
77 bmp
= wx
.Bitmap(dlg
.GetPath(), wx
.BITMAP_TYPE_BMP
)
78 bmpdo
= wx
.BitmapDataObject(bmp
)
79 if wx
.TheClipboard
.Open():
80 wx
.TheClipboard
.SetData(bmpdo
)
81 wx
.TheClipboard
.Close()
84 "The bitmap is now in the Clipboard. Switch to a graphics\n"
85 "editor and try pasting it in..."
89 "There is no data in the clipboard in the required format",
95 #----------------------------------------------------------------------
97 class OtherDropTarget(wx
.PyDropTarget
):
98 def __init__(self
, window
, log
):
99 wx
.PyDropTarget
.__init
__(self
)
101 self
.do
= wx
.FileDataObject()
102 self
.SetDataObject(self
.do
)
104 def OnEnter(self
, x
, y
, d
):
105 self
.log
.WriteText("OnEnter: %d, %d, %d\n" % (x
, y
, d
))
108 #def OnDragOver(self, x, y, d):
109 # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
113 self
.log
.WriteText("OnLeave\n")
115 def OnDrop(self
, x
, y
):
116 self
.log
.WriteText("OnDrop: %d %d\n" % (x
, y
))
119 def OnData(self
, x
, y
, d
):
120 self
.log
.WriteText("OnData: %d, %d, %d\n" % (x
, y
, d
))
122 self
.log
.WriteText("%s\n" % self
.do
.GetFilenames())
126 class MyFileDropTarget(wx
.FileDropTarget
):
127 def __init__(self
, window
, log
):
128 wx
.FileDropTarget
.__init
__(self
)
132 def OnDropFiles(self
, x
, y
, filenames
):
133 self
.window
.SetInsertionPointEnd()
134 self
.window
.WriteText("\n%d file(s) dropped at %d,%d:\n" %
135 (len(filenames
), x
, y
))
137 for file in filenames
:
138 self
.window
.WriteText(file + '\n')
141 class MyTextDropTarget(wx
.TextDropTarget
):
142 def __init__(self
, window
, log
):
143 wx
.TextDropTarget
.__init
__(self
)
147 def OnDropText(self
, x
, y
, text
):
148 self
.window
.WriteText("(%d, %d)\n%s\n" % (x
, y
, text
))
150 def OnDragOver(self
, x
, y
, d
):
154 class FileDropPanel(wx
.Panel
):
155 def __init__(self
, parent
, log
):
156 wx
.Panel
.__init
__(self
, parent
, -1)
158 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
160 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
162 wx
.StaticText(self
, -1, " \nDrag some files here:"),
163 0, wx
.EXPAND|wx
.ALL
, 2
166 self
.text
= wx
.TextCtrl(
168 style
= wx
.TE_MULTILINE|wx
.HSCROLL|wx
.TE_READONLY
171 dt
= MyFileDropTarget(self
, log
)
172 self
.text
.SetDropTarget(dt
)
173 sizer
.Add(self
.text
, 1, wx
.EXPAND
)
176 wx
.StaticText(self
, -1, " \nDrag some text here:"),
177 0, wx
.EXPAND|wx
.ALL
, 2
180 self
.text2
= wx
.TextCtrl(
182 style
= wx
.TE_MULTILINE|wx
.HSCROLL|wx
.TE_READONLY
185 dt
= MyTextDropTarget(self
.text2
, log
)
186 self
.text2
.SetDropTarget(dt
)
187 sizer
.Add(self
.text2
, 1, wx
.EXPAND
)
189 self
.SetAutoLayout(True)
193 def WriteText(self
, text
):
194 self
.text
.WriteText(text
)
196 def SetInsertionPointEnd(self
):
197 self
.text
.SetInsertionPointEnd()
200 #----------------------------------------------------------------------
201 #----------------------------------------------------------------------
203 class TestPanel(wx
.Panel
):
204 def __init__(self
, parent
, log
):
205 wx
.Panel
.__init
__(self
, parent
, -1)
207 self
.SetAutoLayout(True)
208 outsideSizer
= wx
.BoxSizer(wx
.VERTICAL
)
210 msg
= "Clipboard / Drag-And-Drop"
211 text
= wx
.StaticText(self
, -1, "", style
=wx
.ALIGN_CENTRE
)
212 text
.SetFont(wx
.Font(24, wx
.SWISS
, wx
.NORMAL
, wx
.BOLD
, False))
215 w
,h
= text
.GetTextExtent(msg
)
216 text
.SetSize(wx
.Size(w
,h
+1))
217 text
.SetForegroundColour(wx
.BLUE
)
218 outsideSizer
.Add(text
, 0, wx
.EXPAND|wx
.ALL
, 5)
219 outsideSizer
.Add(wx
.StaticLine(self
, -1), 0, wx
.EXPAND
)
221 inSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
222 inSizer
.Add(ClipTextPanel(self
, log
), 1, wx
.EXPAND
)
223 inSizer
.Add(FileDropPanel(self
, log
), 1, wx
.EXPAND
)
225 outsideSizer
.Add(inSizer
, 1, wx
.EXPAND
)
226 self
.SetSizer(outsideSizer
)
229 #----------------------------------------------------------------------
231 def runTest(frame
, nb
, log
):
232 win
= TestPanel(nb
, log
)
235 #----------------------------------------------------------------------
241 This demo shows some examples of data transfer through clipboard or
242 drag and drop. In wxWindows, these two ways to transfer data (either
243 between different applications or inside one and the same) are very
244 similar which allows to implement both of them using almost the same
245 code - or, in other words, if you implement drag and drop support for
246 your application, you get clipboard support for free and vice versa.
248 At the heart of both clipboard and drag and drop operations lies the
249 wxDataObject class. The objects of this class (or, to be precise,
250 classes derived from it) represent the data which is being carried by
251 the mouse during drag and drop operation or copied to or pasted from
252 the clipboard. wxDataObject is a "smart" piece of data because it
253 knows which formats it supports (see GetFormatCount and GetAllFormats)
254 and knows how to render itself in any of them (see GetDataHere). It
255 can also receive its value from the outside in a format it supports if
256 it implements the SetData method. Please see the documentation of this
257 class for more details.
259 Both clipboard and drag and drop operations have two sides: the source
260 and target, the data provider and the data receiver. These which may
261 be in the same application and even the same window when, for example,
262 you drag some text from one position to another in a word
263 processor. Let us describe what each of them should do.
269 if __name__
== '__main__':
272 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])