]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/DragAndDrop.py
reformat the overview string
[wxWidgets.git] / wxPython / demo / DragAndDrop.py
1
2 import wx
3
4 #----------------------------------------------------------------------
5
6 ID_CopyBtn = wx.NewId()
7 ID_PasteBtn = wx.NewId()
8 ID_BitmapBtn = wx.NewId()
9
10 #----------------------------------------------------------------------
11
12 class ClipTextPanel(wx.Panel):
13 def __init__(self, parent, log):
14 wx.Panel.__init__(self, parent, -1)
15 self.log = log
16
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)
43
44 self.SetAutoLayout(True)
45 self.SetSizer(sizer)
46
47
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()
54 else:
55 wx.MessageBox("Unable to open the clipboard", "Error")
56
57
58 def OnPaste(self, evt):
59 success = False
60 do = wx.TextDataObject()
61 if wx.TheClipboard.Open():
62 success = wx.TheClipboard.GetData(do)
63 wx.TheClipboard.Close()
64
65 if success:
66 self.text.SetValue(do.GetText())
67 else:
68 wx.MessageBox(
69 "There is no data in the clipboard in the required format",
70 "Error"
71 )
72
73 def OnCopyBitmap(self, evt):
74 dlg = wx.FileDialog(self, "Choose a bitmap to copy", wildcard="*.bmp")
75
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()
82
83 wx.MessageBox(
84 "The bitmap is now in the Clipboard. Switch to a graphics\n"
85 "editor and try pasting it in..."
86 )
87 else:
88 wx.MessageBox(
89 "There is no data in the clipboard in the required format",
90 "Error"
91 )
92
93 dlg.Destroy()
94
95 #----------------------------------------------------------------------
96
97 class OtherDropTarget(wx.PyDropTarget):
98 def __init__(self, window, log):
99 wx.PyDropTarget.__init__(self)
100 self.log = log
101 self.do = wx.FileDataObject()
102 self.SetDataObject(self.do)
103
104 def OnEnter(self, x, y, d):
105 self.log.WriteText("OnEnter: %d, %d, %d\n" % (x, y, d))
106 return wx.DragCopy
107
108 #def OnDragOver(self, x, y, d):
109 # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
110 # return wx.DragCopy
111
112 def OnLeave(self):
113 self.log.WriteText("OnLeave\n")
114
115 def OnDrop(self, x, y):
116 self.log.WriteText("OnDrop: %d %d\n" % (x, y))
117 return True
118
119 def OnData(self, x, y, d):
120 self.log.WriteText("OnData: %d, %d, %d\n" % (x, y, d))
121 self.GetData()
122 self.log.WriteText("%s\n" % self.do.GetFilenames())
123 return d
124
125
126 class MyFileDropTarget(wx.FileDropTarget):
127 def __init__(self, window, log):
128 wx.FileDropTarget.__init__(self)
129 self.window = window
130 self.log = log
131
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))
136
137 for file in filenames:
138 self.window.WriteText(file + '\n')
139
140
141 class MyTextDropTarget(wx.TextDropTarget):
142 def __init__(self, window, log):
143 wx.TextDropTarget.__init__(self)
144 self.window = window
145 self.log = log
146
147 def OnDropText(self, x, y, text):
148 self.window.WriteText("(%d, %d)\n%s\n" % (x, y, text))
149
150 def OnDragOver(self, x, y, d):
151 return wx.DragCopy
152
153
154 class FileDropPanel(wx.Panel):
155 def __init__(self, parent, log):
156 wx.Panel.__init__(self, parent, -1)
157
158 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
159
160 sizer = wx.BoxSizer(wx.VERTICAL)
161 sizer.Add(
162 wx.StaticText(self, -1, " \nDrag some files here:"),
163 0, wx.EXPAND|wx.ALL, 2
164 )
165
166 self.text = wx.TextCtrl(
167 self, -1, "",
168 style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY
169 )
170
171 dt = MyFileDropTarget(self, log)
172 self.text.SetDropTarget(dt)
173 sizer.Add(self.text, 1, wx.EXPAND)
174
175 sizer.Add(
176 wx.StaticText(self, -1, " \nDrag some text here:"),
177 0, wx.EXPAND|wx.ALL, 2
178 )
179
180 self.text2 = wx.TextCtrl(
181 self, -1, "",
182 style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY
183 )
184
185 dt = MyTextDropTarget(self.text2, log)
186 self.text2.SetDropTarget(dt)
187 sizer.Add(self.text2, 1, wx.EXPAND)
188
189 self.SetAutoLayout(True)
190 self.SetSizer(sizer)
191
192
193 def WriteText(self, text):
194 self.text.WriteText(text)
195
196 def SetInsertionPointEnd(self):
197 self.text.SetInsertionPointEnd()
198
199
200 #----------------------------------------------------------------------
201 #----------------------------------------------------------------------
202
203 class TestPanel(wx.Panel):
204 def __init__(self, parent, log):
205 wx.Panel.__init__(self, parent, -1)
206
207 self.SetAutoLayout(True)
208 outsideSizer = wx.BoxSizer(wx.VERTICAL)
209
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))
213 text.SetLabel(msg)
214
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)
220
221 inSizer = wx.BoxSizer(wx.HORIZONTAL)
222 inSizer.Add(ClipTextPanel(self, log), 1, wx.EXPAND)
223 inSizer.Add(FileDropPanel(self, log), 1, wx.EXPAND)
224
225 outsideSizer.Add(inSizer, 1, wx.EXPAND)
226 self.SetSizer(outsideSizer)
227
228
229 #----------------------------------------------------------------------
230
231 def runTest(frame, nb, log):
232 win = TestPanel(nb, log)
233 return win
234
235 #----------------------------------------------------------------------
236
237
238 overview = """\
239 <html>
240 <body>
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.
247 <p>
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.
258 <p>
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.
264 </body>
265 </html>
266 """
267
268
269 if __name__ == '__main__':
270 import sys,os
271 import run
272 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
273