]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/DragAndDrop.py
added missing button state
[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())
54563c57
RD
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")
b1462dfa
RD
56
57
58 def OnPaste(self, evt):
54563c57 59 success = False
8fa876ca 60 do = wx.TextDataObject()
54563c57
RD
61 if wx.TheClipboard.Open():
62 success = wx.TheClipboard.GetData(do)
63 wx.TheClipboard.Close()
8fa876ca 64
b1462dfa 65 if success:
694759cf 66 self.text.SetValue(do.GetText())
b1462dfa 67 else:
8fa876ca
RD
68 wx.MessageBox(
69 "There is no data in the clipboard in the required format",
70 "Error"
71 )
b1462dfa
RD
72
73 def OnCopyBitmap(self, evt):
8fa876ca
RD
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)
54563c57
RD
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 )
8fa876ca 92
b1462dfa
RD
93 dlg.Destroy()
94
95#----------------------------------------------------------------------
96
8fa876ca 97class OtherDropTarget(wx.PyDropTarget):
b1462dfa 98 def __init__(self, window, log):
8fa876ca 99 wx.PyDropTarget.__init__(self)
b1462dfa 100 self.log = log
8fa876ca 101 self.do = wx.FileDataObject()
b1462dfa
RD
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))
8fa876ca 106 return wx.DragCopy
b1462dfa
RD
107
108 #def OnDragOver(self, x, y, d):
109 # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
8fa876ca 110 # return wx.DragCopy
b1462dfa
RD
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))
1e4a197e 117 return True
b1462dfa
RD
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
8fa876ca 126class MyFileDropTarget(wx.FileDropTarget):
b1462dfa 127 def __init__(self, window, log):
8fa876ca 128 wx.FileDropTarget.__init__(self)
b1462dfa
RD
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))
8fa876ca 136
b1462dfa
RD
137 for file in filenames:
138 self.window.WriteText(file + '\n')
139
140
8fa876ca 141class MyTextDropTarget(wx.TextDropTarget):
846ec2f9 142 def __init__(self, window, log):
8fa876ca 143 wx.TextDropTarget.__init__(self)
846ec2f9
RD
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
e00a2cc7 150 def OnDragOver(self, x, y, d):
8fa876ca 151 return wx.DragCopy
846ec2f9 152
b1462dfa 153
8fa876ca 154class FileDropPanel(wx.Panel):
b1462dfa 155 def __init__(self, parent, log):
8fa876ca
RD
156 wx.Panel.__init__(self, parent, -1)
157
158 #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False))
b1462dfa 159
8fa876ca
RD
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 )
b1462dfa 165
8fa876ca
RD
166 self.text = wx.TextCtrl(
167 self, -1, "",
168 style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY
169 )
b1462dfa 170
b1462dfa
RD
171 dt = MyFileDropTarget(self, log)
172 self.text.SetDropTarget(dt)
8fa876ca
RD
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 )
b1462dfa 184
846ec2f9
RD
185 dt = MyTextDropTarget(self.text2, log)
186 self.text2.SetDropTarget(dt)
8fa876ca 187 sizer.Add(self.text2, 1, wx.EXPAND)
846ec2f9 188
1e4a197e 189 self.SetAutoLayout(True)
b1462dfa
RD
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
8fa876ca 203class TestPanel(wx.Panel):
b1462dfa 204 def __init__(self, parent, log):
8fa876ca 205 wx.Panel.__init__(self, parent, -1)
b1462dfa 206
1e4a197e 207 self.SetAutoLayout(True)
8fa876ca 208 outsideSizer = wx.BoxSizer(wx.VERTICAL)
b1462dfa 209
4120ef2b 210 msg = "Clipboard / Drag-And-Drop"
8fa876ca
RD
211 text = wx.StaticText(self, -1, "", style=wx.ALIGN_CENTRE)
212 text.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False))
4120ef2b 213 text.SetLabel(msg)
8fa876ca 214
4120ef2b 215 w,h = text.GetTextExtent(msg)
8fa876ca
RD
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)
b1462dfa 220
8fa876ca
RD
221 inSizer = wx.BoxSizer(wx.HORIZONTAL)
222 inSizer.Add(ClipTextPanel(self, log), 1, wx.EXPAND)
223 inSizer.Add(FileDropPanel(self, log), 1, wx.EXPAND)
b1462dfa 224
8fa876ca 225 outsideSizer.Add(inSizer, 1, wx.EXPAND)
b1462dfa
RD
226 self.SetSizer(outsideSizer)
227
228
229#----------------------------------------------------------------------
230
231def runTest(frame, nb, log):
232 win = TestPanel(nb, log)
233 return win
234
235#----------------------------------------------------------------------
236
237
8fa876ca
RD
238overview = """\
239<html>
240<body>
1fded56b
RD
241This demo shows some examples of data transfer through clipboard or
242drag and drop. In wxWindows, these two ways to transfer data (either
243between different applications or inside one and the same) are very
244similar which allows to implement both of them using almost the same
245code - or, in other words, if you implement drag and drop support for
246your application, you get clipboard support for free and vice versa.
247<p>
248At the heart of both clipboard and drag and drop operations lies the
249wxDataObject class. The objects of this class (or, to be precise,
250classes derived from it) represent the data which is being carried by
251the mouse during drag and drop operation or copied to or pasted from
252the clipboard. wxDataObject is a "smart" piece of data because it
253knows which formats it supports (see GetFormatCount and GetAllFormats)
254and knows how to render itself in any of them (see GetDataHere). It
255can also receive its value from the outside in a format it supports if
256it implements the SetData method. Please see the documentation of this
257class for more details.
258<p>
259Both clipboard and drag and drop operations have two sides: the source
260and target, the data provider and the data receiver. These which may
261be in the same application and even the same window when, for example,
262you drag some text from one position to another in a word
263processor. Let us describe what each of them should do.
8fa876ca
RD
264</body>
265</html>
1fded56b 266"""
b1462dfa 267
b1462dfa 268
1fded56b
RD
269if __name__ == '__main__':
270 import sys,os
271 import run
8eca4fef 272 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
1fded56b 273