]>
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 wx
.TheClipboard
.Open()
52 wx
.TheClipboard
.SetData(self
.do
)
53 wx
.TheClipboard
.Close()
56 def OnPaste(self
, evt
):
57 do
= wx
.TextDataObject()
58 wx
.TheClipboard
.Open()
59 success
= wx
.TheClipboard
.GetData(do
)
60 wx
.TheClipboard
.Close()
63 self
.text
.SetValue(do
.GetText())
66 "There is no data in the clipboard in the required format",
70 def OnCopyBitmap(self
, evt
):
71 dlg
= wx
.FileDialog(self
, "Choose a bitmap to copy", wildcard
="*.bmp")
73 if dlg
.ShowModal() == wx
.ID_OK
:
74 bmp
= wx
.Bitmap(dlg
.GetPath(), wx
.BITMAP_TYPE_BMP
)
75 bmpdo
= wx
.BitmapDataObject(bmp
)
76 wx
.TheClipboard
.Open()
77 wx
.TheClipboard
.SetData(bmpdo
)
78 wx
.TheClipboard
.Close()
81 "The bitmap is now in the Clipboard. Switch to a graphics\n"
82 "editor and try pasting it in..."
87 #----------------------------------------------------------------------
89 class OtherDropTarget(wx
.PyDropTarget
):
90 def __init__(self
, window
, log
):
91 wx
.PyDropTarget
.__init
__(self
)
93 self
.do
= wx
.FileDataObject()
94 self
.SetDataObject(self
.do
)
96 def OnEnter(self
, x
, y
, d
):
97 self
.log
.WriteText("OnEnter: %d, %d, %d\n" % (x
, y
, d
))
100 #def OnDragOver(self, x, y, d):
101 # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
105 self
.log
.WriteText("OnLeave\n")
107 def OnDrop(self
, x
, y
):
108 self
.log
.WriteText("OnDrop: %d %d\n" % (x
, y
))
111 def OnData(self
, x
, y
, d
):
112 self
.log
.WriteText("OnData: %d, %d, %d\n" % (x
, y
, d
))
114 self
.log
.WriteText("%s\n" % self
.do
.GetFilenames())
118 class MyFileDropTarget(wx
.FileDropTarget
):
119 def __init__(self
, window
, log
):
120 wx
.FileDropTarget
.__init
__(self
)
124 def OnDropFiles(self
, x
, y
, filenames
):
125 self
.window
.SetInsertionPointEnd()
126 self
.window
.WriteText("\n%d file(s) dropped at %d,%d:\n" %
127 (len(filenames
), x
, y
))
129 for file in filenames
:
130 self
.window
.WriteText(file + '\n')
133 class MyTextDropTarget(wx
.TextDropTarget
):
134 def __init__(self
, window
, log
):
135 wx
.TextDropTarget
.__init
__(self
)
139 def OnDropText(self
, x
, y
, text
):
140 self
.window
.WriteText("(%d, %d)\n%s\n" % (x
, y
, text
))
142 def OnDragOver(self
, x
, y
, d
):
146 class FileDropPanel(wx
.Panel
):
147 def __init__(self
, parent
, log
):
148 wx
.Panel
.__init
__(self
, parent
, -1)
150 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
152 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
154 wx
.StaticText(self
, -1, " \nDrag some files here:"),
155 0, wx
.EXPAND|wx
.ALL
, 2
158 self
.text
= wx
.TextCtrl(
160 style
= wx
.TE_MULTILINE|wx
.HSCROLL|wx
.TE_READONLY
163 dt
= MyFileDropTarget(self
, log
)
164 self
.text
.SetDropTarget(dt
)
165 sizer
.Add(self
.text
, 1, wx
.EXPAND
)
168 wx
.StaticText(self
, -1, " \nDrag some text here:"),
169 0, wx
.EXPAND|wx
.ALL
, 2
172 self
.text2
= wx
.TextCtrl(
174 style
= wx
.TE_MULTILINE|wx
.HSCROLL|wx
.TE_READONLY
177 dt
= MyTextDropTarget(self
.text2
, log
)
178 self
.text2
.SetDropTarget(dt
)
179 sizer
.Add(self
.text2
, 1, wx
.EXPAND
)
181 self
.SetAutoLayout(True)
185 def WriteText(self
, text
):
186 self
.text
.WriteText(text
)
188 def SetInsertionPointEnd(self
):
189 self
.text
.SetInsertionPointEnd()
192 #----------------------------------------------------------------------
193 #----------------------------------------------------------------------
195 class TestPanel(wx
.Panel
):
196 def __init__(self
, parent
, log
):
197 wx
.Panel
.__init
__(self
, parent
, -1)
199 self
.SetAutoLayout(True)
200 outsideSizer
= wx
.BoxSizer(wx
.VERTICAL
)
202 msg
= "Clipboard / Drag-And-Drop"
203 text
= wx
.StaticText(self
, -1, "", style
=wx
.ALIGN_CENTRE
)
204 text
.SetFont(wx
.Font(24, wx
.SWISS
, wx
.NORMAL
, wx
.BOLD
, False))
207 w
,h
= text
.GetTextExtent(msg
)
208 text
.SetSize(wx
.Size(w
,h
+1))
209 text
.SetForegroundColour(wx
.BLUE
)
210 outsideSizer
.Add(text
, 0, wx
.EXPAND|wx
.ALL
, 5)
211 outsideSizer
.Add(wx
.StaticLine(self
, -1), 0, wx
.EXPAND
)
213 inSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
214 inSizer
.Add(ClipTextPanel(self
, log
), 1, wx
.EXPAND
)
215 inSizer
.Add(FileDropPanel(self
, log
), 1, wx
.EXPAND
)
217 outsideSizer
.Add(inSizer
, 1, wx
.EXPAND
)
218 self
.SetSizer(outsideSizer
)
221 #----------------------------------------------------------------------
223 def runTest(frame
, nb
, log
):
224 win
= TestPanel(nb
, log
)
227 #----------------------------------------------------------------------
233 This demo shows some examples of data transfer through clipboard or
234 drag and drop. In wxWindows, these two ways to transfer data (either
235 between different applications or inside one and the same) are very
236 similar which allows to implement both of them using almost the same
237 code - or, in other words, if you implement drag and drop support for
238 your application, you get clipboard support for free and vice versa.
240 At the heart of both clipboard and drag and drop operations lies the
241 wxDataObject class. The objects of this class (or, to be precise,
242 classes derived from it) represent the data which is being carried by
243 the mouse during drag and drop operation or copied to or pasted from
244 the clipboard. wxDataObject is a "smart" piece of data because it
245 knows which formats it supports (see GetFormatCount and GetAllFormats)
246 and knows how to render itself in any of them (see GetDataHere). It
247 can also receive its value from the outside in a format it supports if
248 it implements the SetData method. Please see the documentation of this
249 class for more details.
251 Both clipboard and drag and drop operations have two sides: the source
252 and target, the data provider and the data receiver. These which may
253 be in the same application and even the same window when, for example,
254 you drag some text from one position to another in a word
255 processor. Let us describe what each of them should do.
261 if __name__
== '__main__':
264 run
.main(['', os
.path
.basename(sys
.argv
[0])])