]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | import wx | |
14 | ||
15 | #---------------------------------------------------------------------- | |
16 | ||
17 | ID_CopyBtn = wx.NewId() | |
18 | ID_PasteBtn = wx.NewId() | |
19 | ID_BitmapBtn = wx.NewId() | |
20 | ||
21 | #---------------------------------------------------------------------- | |
22 | ||
23 | class ClipTextPanel(wx.Panel): | |
24 | def __init__(self, parent, log): | |
25 | wx.Panel.__init__(self, parent, -1) | |
26 | self.log = log | |
27 | ||
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) | |
54 | ||
55 | self.SetAutoLayout(True) | |
56 | self.SetSizer(sizer) | |
57 | ||
58 | ||
59 | def OnCopy(self, evt): | |
60 | self.do = wx.TextDataObject() | |
61 | self.do.SetText(self.text.GetValue()) | |
62 | wx.TheClipboard.Open() | |
63 | wx.TheClipboard.SetData(self.do) | |
64 | wx.TheClipboard.Close() | |
65 | ||
66 | ||
67 | def OnPaste(self, evt): | |
68 | do = wx.TextDataObject() | |
69 | wx.TheClipboard.Open() | |
70 | success = wx.TheClipboard.GetData(do) | |
71 | wx.TheClipboard.Close() | |
72 | ||
73 | if success: | |
74 | self.text.SetValue(do.GetText()) | |
75 | else: | |
76 | wx.MessageBox( | |
77 | "There is no data in the clipboard in the required format", | |
78 | "Error" | |
79 | ) | |
80 | ||
81 | def OnCopyBitmap(self, evt): | |
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 | ||
96 | dlg.Destroy() | |
97 | ||
98 | #---------------------------------------------------------------------- | |
99 | ||
100 | class OtherDropTarget(wx.PyDropTarget): | |
101 | def __init__(self, window, log): | |
102 | wx.PyDropTarget.__init__(self) | |
103 | self.log = log | |
104 | self.do = wx.FileDataObject() | |
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)) | |
109 | return wx.DragCopy | |
110 | ||
111 | #def OnDragOver(self, x, y, d): | |
112 | # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d)) | |
113 | # return wx.DragCopy | |
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)) | |
120 | return True | |
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 | ||
129 | class MyFileDropTarget(wx.FileDropTarget): | |
130 | def __init__(self, window, log): | |
131 | wx.FileDropTarget.__init__(self) | |
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)) | |
139 | ||
140 | for file in filenames: | |
141 | self.window.WriteText(file + '\n') | |
142 | ||
143 | ||
144 | class MyTextDropTarget(wx.TextDropTarget): | |
145 | def __init__(self, window, log): | |
146 | wx.TextDropTarget.__init__(self) | |
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 | ||
153 | def OnDragOver(self, x, y, d): | |
154 | return wx.DragCopy | |
155 | ||
156 | ||
157 | class FileDropPanel(wx.Panel): | |
158 | def __init__(self, parent, log): | |
159 | wx.Panel.__init__(self, parent, -1) | |
160 | ||
161 | #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False)) | |
162 | ||
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 | ) | |
168 | ||
169 | self.text = wx.TextCtrl( | |
170 | self, -1, "", | |
171 | style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY | |
172 | ) | |
173 | ||
174 | dt = MyFileDropTarget(self, log) | |
175 | self.text.SetDropTarget(dt) | |
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 | ) | |
187 | ||
188 | dt = MyTextDropTarget(self.text2, log) | |
189 | self.text2.SetDropTarget(dt) | |
190 | sizer.Add(self.text2, 1, wx.EXPAND) | |
191 | ||
192 | self.SetAutoLayout(True) | |
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 | ||
206 | class TestPanel(wx.Panel): | |
207 | def __init__(self, parent, log): | |
208 | wx.Panel.__init__(self, parent, -1) | |
209 | ||
210 | self.SetAutoLayout(True) | |
211 | outsideSizer = wx.BoxSizer(wx.VERTICAL) | |
212 | ||
213 | msg = "Clipboard / Drag-And-Drop" | |
214 | text = wx.StaticText(self, -1, "", style=wx.ALIGN_CENTRE) | |
215 | text.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False)) | |
216 | text.SetLabel(msg) | |
217 | ||
218 | w,h = text.GetTextExtent(msg) | |
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) | |
223 | ||
224 | inSizer = wx.BoxSizer(wx.HORIZONTAL) | |
225 | inSizer.Add(ClipTextPanel(self, log), 1, wx.EXPAND) | |
226 | inSizer.Add(FileDropPanel(self, log), 1, wx.EXPAND) | |
227 | ||
228 | outsideSizer.Add(inSizer, 1, wx.EXPAND) | |
229 | self.SetSizer(outsideSizer) | |
230 | ||
231 | ||
232 | #---------------------------------------------------------------------- | |
233 | ||
234 | def runTest(frame, nb, log): | |
235 | win = TestPanel(nb, log) | |
236 | return win | |
237 | ||
238 | #---------------------------------------------------------------------- | |
239 | ||
240 | ||
241 | overview = """\ | |
242 | <html> | |
243 | <body> | |
244 | This demo shows some examples of data transfer through clipboard or | |
245 | drag and drop. In wxWindows, these two ways to transfer data (either | |
246 | between different applications or inside one and the same) are very | |
247 | similar which allows to implement both of them using almost the same | |
248 | code - or, in other words, if you implement drag and drop support for | |
249 | your application, you get clipboard support for free and vice versa. | |
250 | <p> | |
251 | At the heart of both clipboard and drag and drop operations lies the | |
252 | wxDataObject class. The objects of this class (or, to be precise, | |
253 | classes derived from it) represent the data which is being carried by | |
254 | the mouse during drag and drop operation or copied to or pasted from | |
255 | the clipboard. wxDataObject is a "smart" piece of data because it | |
256 | knows which formats it supports (see GetFormatCount and GetAllFormats) | |
257 | and knows how to render itself in any of them (see GetDataHere). It | |
258 | can also receive its value from the outside in a format it supports if | |
259 | it implements the SetData method. Please see the documentation of this | |
260 | class for more details. | |
261 | <p> | |
262 | Both clipboard and drag and drop operations have two sides: the source | |
263 | and target, the data provider and the data receiver. These which may | |
264 | be in the same application and even the same window when, for example, | |
265 | you drag some text from one position to another in a word | |
266 | processor. Let us describe what each of them should do. | |
267 | </body> | |
268 | </html> | |
269 | """ | |
270 | ||
271 | ||
272 | if __name__ == '__main__': | |
273 | import sys,os | |
274 | import run | |
275 | run.main(['', os.path.basename(sys.argv[0])]) | |
276 |