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