]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/DragAndDrop.py
Added new MaskedEditControl code from Will Sadkin. The modules are
[wxWidgets.git] / wxPython / demo / DragAndDrop.py
CommitLineData
8fa876ca
RD
1
2import wx
b1462dfa 3
8fa876ca
RD
4#----------------------------------------------------------------------
5
6ID_CopyBtn = wx.NewId()
7ID_PasteBtn = wx.NewId()
8ID_BitmapBtn = wx.NewId()
b1462dfa
RD
9
10#----------------------------------------------------------------------
11
8fa876ca 12class ClipTextPanel(wx.Panel):
b1462dfa 13 def __init__(self, parent, log):
8fa876ca 14 wx.Panel.__init__(self, parent, -1)
b1462dfa
RD
15 self.log = log
16
8fa876ca
RD
17 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
18
19 sizer = wx.BoxSizer(wx.VERTICAL)
20 sizer.Add(
21 wx.StaticText(
22 self, -1, "Copy/Paste text to/from\n"
23 "this window and other apps"
24 ),
25 0, wx.EXPAND|wx.ALL, 2
26 )
27
28 self.text = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.HSCROLL)
29 sizer.Add(self.text, 1, wx.EXPAND)
30
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)
35 sizer.Add(
36 wx.Button(self, ID_BitmapBtn, " Copy Bitmap "),
37 0, wx.EXPAND|wx.ALL, 2
38 )
39
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)
b1462dfa 43
1e4a197e 44 self.SetAutoLayout(True)
b1462dfa
RD
45 self.SetSizer(sizer)
46
47
48 def OnCopy(self, evt):
8fa876ca 49 self.do = wx.TextDataObject()
b1462dfa 50 self.do.SetText(self.text.GetValue())
8fa876ca
RD
51 wx.TheClipboard.Open()
52 wx.TheClipboard.SetData(self.do)
53 wx.TheClipboard.Close()
b1462dfa
RD
54
55
56 def OnPaste(self, evt):
8fa876ca
RD
57 do = wx.TextDataObject()
58 wx.TheClipboard.Open()
59 success = wx.TheClipboard.GetData(do)
60 wx.TheClipboard.Close()
61
b1462dfa 62 if success:
694759cf 63 self.text.SetValue(do.GetText())
b1462dfa 64 else:
8fa876ca
RD
65 wx.MessageBox(
66 "There is no data in the clipboard in the required format",
67 "Error"
68 )
b1462dfa
RD
69
70 def OnCopyBitmap(self, evt):
8fa876ca
RD
71 dlg = wx.FileDialog(self, "Choose a bitmap to copy", wildcard="*.bmp")
72
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()
79
80 wx.MessageBox(
81 "The bitmap is now in the Clipboard. Switch to a graphics\n"
82 "editor and try pasting it in..."
83 )
84
b1462dfa
RD
85 dlg.Destroy()
86
87#----------------------------------------------------------------------
88
8fa876ca 89class OtherDropTarget(wx.PyDropTarget):
b1462dfa 90 def __init__(self, window, log):
8fa876ca 91 wx.PyDropTarget.__init__(self)
b1462dfa 92 self.log = log
8fa876ca 93 self.do = wx.FileDataObject()
b1462dfa
RD
94 self.SetDataObject(self.do)
95
96 def OnEnter(self, x, y, d):
97 self.log.WriteText("OnEnter: %d, %d, %d\n" % (x, y, d))
8fa876ca 98 return wx.DragCopy
b1462dfa
RD
99
100 #def OnDragOver(self, x, y, d):
101 # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
8fa876ca 102 # return wx.DragCopy
b1462dfa
RD
103
104 def OnLeave(self):
105 self.log.WriteText("OnLeave\n")
106
107 def OnDrop(self, x, y):
108 self.log.WriteText("OnDrop: %d %d\n" % (x, y))
1e4a197e 109 return True
b1462dfa
RD
110
111 def OnData(self, x, y, d):
112 self.log.WriteText("OnData: %d, %d, %d\n" % (x, y, d))
113 self.GetData()
114 self.log.WriteText("%s\n" % self.do.GetFilenames())
115 return d
116
117
8fa876ca 118class MyFileDropTarget(wx.FileDropTarget):
b1462dfa 119 def __init__(self, window, log):
8fa876ca 120 wx.FileDropTarget.__init__(self)
b1462dfa
RD
121 self.window = window
122 self.log = log
123
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))
8fa876ca 128
b1462dfa
RD
129 for file in filenames:
130 self.window.WriteText(file + '\n')
131
132
8fa876ca 133class MyTextDropTarget(wx.TextDropTarget):
846ec2f9 134 def __init__(self, window, log):
8fa876ca 135 wx.TextDropTarget.__init__(self)
846ec2f9
RD
136 self.window = window
137 self.log = log
138
139 def OnDropText(self, x, y, text):
140 self.window.WriteText("(%d, %d)\n%s\n" % (x, y, text))
141
e00a2cc7 142 def OnDragOver(self, x, y, d):
8fa876ca 143 return wx.DragCopy
846ec2f9 144
b1462dfa 145
8fa876ca 146class FileDropPanel(wx.Panel):
b1462dfa 147 def __init__(self, parent, log):
8fa876ca
RD
148 wx.Panel.__init__(self, parent, -1)
149
150 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
b1462dfa 151
8fa876ca
RD
152 sizer = wx.BoxSizer(wx.VERTICAL)
153 sizer.Add(
154 wx.StaticText(self, -1, " \nDrag some files here:"),
155 0, wx.EXPAND|wx.ALL, 2
156 )
b1462dfa 157
8fa876ca
RD
158 self.text = wx.TextCtrl(
159 self, -1, "",
160 style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY
161 )
b1462dfa 162
b1462dfa
RD
163 dt = MyFileDropTarget(self, log)
164 self.text.SetDropTarget(dt)
8fa876ca
RD
165 sizer.Add(self.text, 1, wx.EXPAND)
166
167 sizer.Add(
168 wx.StaticText(self, -1, " \nDrag some text here:"),
169 0, wx.EXPAND|wx.ALL, 2
170 )
171
172 self.text2 = wx.TextCtrl(
173 self, -1, "",
174 style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY
175 )
b1462dfa 176
846ec2f9
RD
177 dt = MyTextDropTarget(self.text2, log)
178 self.text2.SetDropTarget(dt)
8fa876ca 179 sizer.Add(self.text2, 1, wx.EXPAND)
846ec2f9 180
1e4a197e 181 self.SetAutoLayout(True)
b1462dfa
RD
182 self.SetSizer(sizer)
183
184
185 def WriteText(self, text):
186 self.text.WriteText(text)
187
188 def SetInsertionPointEnd(self):
189 self.text.SetInsertionPointEnd()
190
191
192#----------------------------------------------------------------------
193#----------------------------------------------------------------------
194
8fa876ca 195class TestPanel(wx.Panel):
b1462dfa 196 def __init__(self, parent, log):
8fa876ca 197 wx.Panel.__init__(self, parent, -1)
b1462dfa 198
1e4a197e 199 self.SetAutoLayout(True)
8fa876ca 200 outsideSizer = wx.BoxSizer(wx.VERTICAL)
b1462dfa 201
4120ef2b 202 msg = "Clipboard / Drag-And-Drop"
8fa876ca
RD
203 text = wx.StaticText(self, -1, "", style=wx.ALIGN_CENTRE)
204 text.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False))
4120ef2b 205 text.SetLabel(msg)
8fa876ca 206
4120ef2b 207 w,h = text.GetTextExtent(msg)
8fa876ca
RD
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)
b1462dfa 212
8fa876ca
RD
213 inSizer = wx.BoxSizer(wx.HORIZONTAL)
214 inSizer.Add(ClipTextPanel(self, log), 1, wx.EXPAND)
215 inSizer.Add(FileDropPanel(self, log), 1, wx.EXPAND)
b1462dfa 216
8fa876ca 217 outsideSizer.Add(inSizer, 1, wx.EXPAND)
b1462dfa
RD
218 self.SetSizer(outsideSizer)
219
220
221#----------------------------------------------------------------------
222
223def runTest(frame, nb, log):
224 win = TestPanel(nb, log)
225 return win
226
227#----------------------------------------------------------------------
228
229
8fa876ca
RD
230overview = """\
231<html>
232<body>
1fded56b
RD
233This demo shows some examples of data transfer through clipboard or
234drag and drop. In wxWindows, these two ways to transfer data (either
235between different applications or inside one and the same) are very
236similar which allows to implement both of them using almost the same
237code - or, in other words, if you implement drag and drop support for
238your application, you get clipboard support for free and vice versa.
239<p>
240At the heart of both clipboard and drag and drop operations lies the
241wxDataObject class. The objects of this class (or, to be precise,
242classes derived from it) represent the data which is being carried by
243the mouse during drag and drop operation or copied to or pasted from
244the clipboard. wxDataObject is a "smart" piece of data because it
245knows which formats it supports (see GetFormatCount and GetAllFormats)
246and knows how to render itself in any of them (see GetDataHere). It
247can also receive its value from the outside in a format it supports if
248it implements the SetData method. Please see the documentation of this
249class for more details.
250<p>
251Both clipboard and drag and drop operations have two sides: the source
252and target, the data provider and the data receiver. These which may
253be in the same application and even the same window when, for example,
254you drag some text from one position to another in a word
255processor. Let us describe what each of them should do.
8fa876ca
RD
256</body>
257</html>
1fded56b 258"""
b1462dfa 259
b1462dfa 260
1fded56b
RD
261if __name__ == '__main__':
262 import sys,os
263 import run
8eca4fef 264 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
1fded56b 265