2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
import wx
|
1999-11-06 03:30:23 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
ID_CopyBtn = wx.NewId()
|
|
|
|
ID_PasteBtn = wx.NewId()
|
|
|
|
ID_BitmapBtn = wx.NewId()
|
1999-11-06 03:30:23 -05:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class ClipTextPanel(wx.Panel):
|
1999-11-06 03:30:23 -05:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.__init__(self, parent, -1)
|
1999-11-06 03:30:23 -05:00
|
|
|
self.log = log
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
#self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
|
|
|
|
|
|
|
|
sizer = wx.BoxSizer(wx.VERTICAL)
|
|
|
|
sizer.Add(
|
|
|
|
wx.StaticText(
|
|
|
|
self, -1, "Copy/Paste text to/from\n"
|
|
|
|
"this window and other apps"
|
|
|
|
),
|
|
|
|
0, wx.EXPAND|wx.ALL, 2
|
|
|
|
)
|
|
|
|
|
|
|
|
self.text = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.HSCROLL)
|
|
|
|
sizer.Add(self.text, 1, wx.EXPAND)
|
|
|
|
|
|
|
|
hsz = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
hsz.Add(wx.Button(self, ID_CopyBtn, " Copy "), 1, wx.EXPAND|wx.ALL, 2)
|
|
|
|
hsz.Add(wx.Button(self, ID_PasteBtn, " Paste "), 1, wx.EXPAND|wx.ALL, 2)
|
|
|
|
sizer.Add(hsz, 0, wx.EXPAND)
|
|
|
|
sizer.Add(
|
|
|
|
wx.Button(self, ID_BitmapBtn, " Copy Bitmap "),
|
|
|
|
0, wx.EXPAND|wx.ALL, 2
|
|
|
|
)
|
|
|
|
|
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnCopy, id=ID_CopyBtn)
|
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnPaste, id=ID_PasteBtn)
|
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnCopyBitmap, id=ID_BitmapBtn)
|
1999-11-06 03:30:23 -05:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetAutoLayout(True)
|
1999-11-06 03:30:23 -05:00
|
|
|
self.SetSizer(sizer)
|
|
|
|
|
|
|
|
|
|
|
|
def OnCopy(self, evt):
|
2003-12-08 20:23:28 -05:00
|
|
|
self.do = wx.TextDataObject()
|
1999-11-06 03:30:23 -05:00
|
|
|
self.do.SetText(self.text.GetValue())
|
2005-01-28 20:17:02 -05:00
|
|
|
if wx.TheClipboard.Open():
|
|
|
|
wx.TheClipboard.SetData(self.do)
|
|
|
|
wx.TheClipboard.Close()
|
|
|
|
else:
|
|
|
|
wx.MessageBox("Unable to open the clipboard", "Error")
|
1999-11-06 03:30:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def OnPaste(self, evt):
|
2005-01-28 20:17:02 -05:00
|
|
|
success = False
|
2003-12-08 20:23:28 -05:00
|
|
|
do = wx.TextDataObject()
|
2005-01-28 20:17:02 -05:00
|
|
|
if wx.TheClipboard.Open():
|
|
|
|
success = wx.TheClipboard.GetData(do)
|
|
|
|
wx.TheClipboard.Close()
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-11-06 03:30:23 -05:00
|
|
|
if success:
|
1999-11-25 02:08:14 -05:00
|
|
|
self.text.SetValue(do.GetText())
|
1999-11-06 03:30:23 -05:00
|
|
|
else:
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.MessageBox(
|
|
|
|
"There is no data in the clipboard in the required format",
|
|
|
|
"Error"
|
|
|
|
)
|
1999-11-06 03:30:23 -05:00
|
|
|
|
|
|
|
def OnCopyBitmap(self, evt):
|
2003-12-08 20:23:28 -05:00
|
|
|
dlg = wx.FileDialog(self, "Choose a bitmap to copy", wildcard="*.bmp")
|
|
|
|
|
|
|
|
if dlg.ShowModal() == wx.ID_OK:
|
|
|
|
bmp = wx.Bitmap(dlg.GetPath(), wx.BITMAP_TYPE_BMP)
|
|
|
|
bmpdo = wx.BitmapDataObject(bmp)
|
2005-01-28 20:17:02 -05:00
|
|
|
if wx.TheClipboard.Open():
|
|
|
|
wx.TheClipboard.SetData(bmpdo)
|
|
|
|
wx.TheClipboard.Close()
|
|
|
|
|
|
|
|
wx.MessageBox(
|
|
|
|
"The bitmap is now in the Clipboard. Switch to a graphics\n"
|
|
|
|
"editor and try pasting it in..."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
wx.MessageBox(
|
|
|
|
"There is no data in the clipboard in the required format",
|
|
|
|
"Error"
|
|
|
|
)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-11-06 03:30:23 -05:00
|
|
|
dlg.Destroy()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class OtherDropTarget(wx.PyDropTarget):
|
1999-11-06 03:30:23 -05:00
|
|
|
def __init__(self, window, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.PyDropTarget.__init__(self)
|
1999-11-06 03:30:23 -05:00
|
|
|
self.log = log
|
2003-12-08 20:23:28 -05:00
|
|
|
self.do = wx.FileDataObject()
|
1999-11-06 03:30:23 -05:00
|
|
|
self.SetDataObject(self.do)
|
|
|
|
|
|
|
|
def OnEnter(self, x, y, d):
|
|
|
|
self.log.WriteText("OnEnter: %d, %d, %d\n" % (x, y, d))
|
2003-12-08 20:23:28 -05:00
|
|
|
return wx.DragCopy
|
1999-11-06 03:30:23 -05:00
|
|
|
|
|
|
|
#def OnDragOver(self, x, y, d):
|
|
|
|
# self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
|
2003-12-08 20:23:28 -05:00
|
|
|
# return wx.DragCopy
|
1999-11-06 03:30:23 -05:00
|
|
|
|
|
|
|
def OnLeave(self):
|
|
|
|
self.log.WriteText("OnLeave\n")
|
|
|
|
|
|
|
|
def OnDrop(self, x, y):
|
|
|
|
self.log.WriteText("OnDrop: %d %d\n" % (x, y))
|
2003-03-25 01:35:27 -05:00
|
|
|
return True
|
1999-11-06 03:30:23 -05:00
|
|
|
|
|
|
|
def OnData(self, x, y, d):
|
|
|
|
self.log.WriteText("OnData: %d, %d, %d\n" % (x, y, d))
|
|
|
|
self.GetData()
|
|
|
|
self.log.WriteText("%s\n" % self.do.GetFilenames())
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class MyFileDropTarget(wx.FileDropTarget):
|
1999-11-06 03:30:23 -05:00
|
|
|
def __init__(self, window, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.FileDropTarget.__init__(self)
|
1999-11-06 03:30:23 -05:00
|
|
|
self.window = window
|
|
|
|
self.log = log
|
|
|
|
|
|
|
|
def OnDropFiles(self, x, y, filenames):
|
|
|
|
self.window.SetInsertionPointEnd()
|
|
|
|
self.window.WriteText("\n%d file(s) dropped at %d,%d:\n" %
|
|
|
|
(len(filenames), x, y))
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-11-06 03:30:23 -05:00
|
|
|
for file in filenames:
|
|
|
|
self.window.WriteText(file + '\n')
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class MyTextDropTarget(wx.TextDropTarget):
|
2001-10-25 22:13:11 -04:00
|
|
|
def __init__(self, window, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.TextDropTarget.__init__(self)
|
2001-10-25 22:13:11 -04:00
|
|
|
self.window = window
|
|
|
|
self.log = log
|
|
|
|
|
|
|
|
def OnDropText(self, x, y, text):
|
|
|
|
self.window.WriteText("(%d, %d)\n%s\n" % (x, y, text))
|
|
|
|
|
2002-01-22 15:18:42 -05:00
|
|
|
def OnDragOver(self, x, y, d):
|
2003-12-08 20:23:28 -05:00
|
|
|
return wx.DragCopy
|
2001-10-25 22:13:11 -04:00
|
|
|
|
1999-11-06 03:30:23 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class FileDropPanel(wx.Panel):
|
1999-11-06 03:30:23 -05:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.__init__(self, parent, -1)
|
|
|
|
|
|
|
|
#self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
|
1999-11-06 03:30:23 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
sizer = wx.BoxSizer(wx.VERTICAL)
|
|
|
|
sizer.Add(
|
|
|
|
wx.StaticText(self, -1, " \nDrag some files here:"),
|
|
|
|
0, wx.EXPAND|wx.ALL, 2
|
|
|
|
)
|
1999-11-06 03:30:23 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.text = wx.TextCtrl(
|
|
|
|
self, -1, "",
|
|
|
|
style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY
|
|
|
|
)
|
1999-11-06 03:30:23 -05:00
|
|
|
|
|
|
|
dt = MyFileDropTarget(self, log)
|
|
|
|
self.text.SetDropTarget(dt)
|
2003-12-08 20:23:28 -05:00
|
|
|
sizer.Add(self.text, 1, wx.EXPAND)
|
|
|
|
|
|
|
|
sizer.Add(
|
|
|
|
wx.StaticText(self, -1, " \nDrag some text here:"),
|
|
|
|
0, wx.EXPAND|wx.ALL, 2
|
|
|
|
)
|
|
|
|
|
|
|
|
self.text2 = wx.TextCtrl(
|
|
|
|
self, -1, "",
|
|
|
|
style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY
|
|
|
|
)
|
1999-11-06 03:30:23 -05:00
|
|
|
|
2001-10-25 22:13:11 -04:00
|
|
|
dt = MyTextDropTarget(self.text2, log)
|
|
|
|
self.text2.SetDropTarget(dt)
|
2003-12-08 20:23:28 -05:00
|
|
|
sizer.Add(self.text2, 1, wx.EXPAND)
|
2001-10-25 22:13:11 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetAutoLayout(True)
|
1999-11-06 03:30:23 -05:00
|
|
|
self.SetSizer(sizer)
|
|
|
|
|
|
|
|
|
|
|
|
def WriteText(self, text):
|
|
|
|
self.text.WriteText(text)
|
|
|
|
|
|
|
|
def SetInsertionPointEnd(self):
|
|
|
|
self.text.SetInsertionPointEnd()
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestPanel(wx.Panel):
|
1999-11-06 03:30:23 -05:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.__init__(self, parent, -1)
|
1999-11-06 03:30:23 -05:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetAutoLayout(True)
|
2003-12-08 20:23:28 -05:00
|
|
|
outsideSizer = wx.BoxSizer(wx.VERTICAL)
|
1999-11-06 03:30:23 -05:00
|
|
|
|
1999-11-08 01:58:32 -05:00
|
|
|
msg = "Clipboard / Drag-And-Drop"
|
2003-12-08 20:23:28 -05:00
|
|
|
text = wx.StaticText(self, -1, "", style=wx.ALIGN_CENTRE)
|
|
|
|
text.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False))
|
1999-11-08 01:58:32 -05:00
|
|
|
text.SetLabel(msg)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-11-08 01:58:32 -05:00
|
|
|
w,h = text.GetTextExtent(msg)
|
2003-12-08 20:23:28 -05:00
|
|
|
text.SetSize(wx.Size(w,h+1))
|
|
|
|
text.SetForegroundColour(wx.BLUE)
|
|
|
|
outsideSizer.Add(text, 0, wx.EXPAND|wx.ALL, 5)
|
|
|
|
outsideSizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND)
|
1999-11-06 03:30:23 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
inSizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
inSizer.Add(ClipTextPanel(self, log), 1, wx.EXPAND)
|
|
|
|
inSizer.Add(FileDropPanel(self, log), 1, wx.EXPAND)
|
1999-11-06 03:30:23 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
outsideSizer.Add(inSizer, 1, wx.EXPAND)
|
1999-11-06 03:30:23 -05:00
|
|
|
self.SetSizer(outsideSizer)
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = TestPanel(nb, log)
|
|
|
|
return win
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
overview = """\
|
|
|
|
<html>
|
|
|
|
<body>
|
2003-07-02 19:13:10 -04:00
|
|
|
This demo shows some examples of data transfer through clipboard or
|
|
|
|
drag and drop. In wxWindows, these two ways to transfer data (either
|
|
|
|
between different applications or inside one and the same) are very
|
|
|
|
similar which allows to implement both of them using almost the same
|
|
|
|
code - or, in other words, if you implement drag and drop support for
|
|
|
|
your application, you get clipboard support for free and vice versa.
|
|
|
|
<p>
|
|
|
|
At the heart of both clipboard and drag and drop operations lies the
|
|
|
|
wxDataObject class. The objects of this class (or, to be precise,
|
|
|
|
classes derived from it) represent the data which is being carried by
|
|
|
|
the mouse during drag and drop operation or copied to or pasted from
|
|
|
|
the clipboard. wxDataObject is a "smart" piece of data because it
|
|
|
|
knows which formats it supports (see GetFormatCount and GetAllFormats)
|
|
|
|
and knows how to render itself in any of them (see GetDataHere). It
|
|
|
|
can also receive its value from the outside in a format it supports if
|
|
|
|
it implements the SetData method. Please see the documentation of this
|
|
|
|
class for more details.
|
|
|
|
<p>
|
|
|
|
Both clipboard and drag and drop operations have two sides: the source
|
|
|
|
and target, the data provider and the data receiver. These which may
|
|
|
|
be in the same application and even the same window when, for example,
|
|
|
|
you drag some text from one position to another in a word
|
|
|
|
processor. Let us describe what each of them should do.
|
2003-12-08 20:23:28 -05:00
|
|
|
</body>
|
|
|
|
</html>
|
2003-07-02 19:13:10 -04:00
|
|
|
"""
|
1999-11-06 03:30:23 -05:00
|
|
|
|
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
2003-07-02 19:13:10 -04:00
|
|
|
|