]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/DragAndDrop.py
xcode 1.2 native targets
[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 wx.TheClipboard.Open()
52 wx.TheClipboard.SetData(self.do)
53 wx.TheClipboard.Close()
54
55
56 def OnPaste(self, evt):
57 do = wx.TextDataObject()
58 wx.TheClipboard.Open()
59 success = wx.TheClipboard.GetData(do)
60 wx.TheClipboard.Close()
61
62 if success:
63 self.text.SetValue(do.GetText())
64 else:
65 wx.MessageBox(
66 "There is no data in the clipboard in the required format",
67 "Error"
68 )
69
70 def OnCopyBitmap(self, evt):
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
85 dlg.Destroy()
86
87 #----------------------------------------------------------------------
88
89 class OtherDropTarget(wx.PyDropTarget):
90 def __init__(self, window, log):
91 wx.PyDropTarget.__init__(self)
92 self.log = log
93 self.do = wx.FileDataObject()
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))
98 return wx.DragCopy
99
100 #def OnDragOver(self, x, y, d):
101 # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
102 # return wx.DragCopy
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))
109 return True
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
118 class MyFileDropTarget(wx.FileDropTarget):
119 def __init__(self, window, log):
120 wx.FileDropTarget.__init__(self)
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))
128
129 for file in filenames:
130 self.window.WriteText(file + '\n')
131
132
133 class MyTextDropTarget(wx.TextDropTarget):
134 def __init__(self, window, log):
135 wx.TextDropTarget.__init__(self)
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
142 def OnDragOver(self, x, y, d):
143 return wx.DragCopy
144
145
146 class FileDropPanel(wx.Panel):
147 def __init__(self, parent, log):
148 wx.Panel.__init__(self, parent, -1)
149
150 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
151
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 )
157
158 self.text = wx.TextCtrl(
159 self, -1, "",
160 style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY
161 )
162
163 dt = MyFileDropTarget(self, log)
164 self.text.SetDropTarget(dt)
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 )
176
177 dt = MyTextDropTarget(self.text2, log)
178 self.text2.SetDropTarget(dt)
179 sizer.Add(self.text2, 1, wx.EXPAND)
180
181 self.SetAutoLayout(True)
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
195 class TestPanel(wx.Panel):
196 def __init__(self, parent, log):
197 wx.Panel.__init__(self, parent, -1)
198
199 self.SetAutoLayout(True)
200 outsideSizer = wx.BoxSizer(wx.VERTICAL)
201
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))
205 text.SetLabel(msg)
206
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)
212
213 inSizer = wx.BoxSizer(wx.HORIZONTAL)
214 inSizer.Add(ClipTextPanel(self, log), 1, wx.EXPAND)
215 inSizer.Add(FileDropPanel(self, log), 1, wx.EXPAND)
216
217 outsideSizer.Add(inSizer, 1, wx.EXPAND)
218 self.SetSizer(outsideSizer)
219
220
221 #----------------------------------------------------------------------
222
223 def runTest(frame, nb, log):
224 win = TestPanel(nb, log)
225 return win
226
227 #----------------------------------------------------------------------
228
229
230 overview = """\
231 <html>
232 <body>
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.
239 <p>
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.
250 <p>
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.
256 </body>
257 </html>
258 """
259
260
261 if __name__ == '__main__':
262 import sys,os
263 import run
264 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
265