]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/DragAndDrop.py
corrections to dynamic loading for Darwin
[wxWidgets.git] / wxPython / demo / DragAndDrop.py
1
2 from wxPython.wx import *
3
4 #----------------------------------------------------------------------
5
6 class ClipTextPanel(wxPanel):
7 def __init__(self, parent, log):
8 wxPanel.__init__(self, parent, -1)
9 self.log = log
10
11 #self.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD, false))
12
13 sizer = wxBoxSizer(wxVERTICAL)
14 sizer.Add(wxStaticText(self, -1,
15 "Copy/Paste text to/from\n"
16 "this window and other apps"), 0, wxEXPAND|wxALL, 2)
17
18 self.text = wxTextCtrl(self, -1, "", style=wxTE_MULTILINE|wxHSCROLL)
19 sizer.Add(self.text, 1, wxEXPAND)
20
21 hsz = wxBoxSizer(wxHORIZONTAL)
22 hsz.Add(wxButton(self, 6050, " Copy "), 1, wxEXPAND|wxALL, 2)
23 hsz.Add(wxButton(self, 6051, " Paste "), 1, wxEXPAND|wxALL, 2)
24 sizer.Add(hsz, 0, wxEXPAND)
25 sizer.Add(wxButton(self, 6052, " Copy Bitmap "), 0, wxEXPAND|wxALL, 2)
26
27 EVT_BUTTON(self, 6050, self.OnCopy)
28 EVT_BUTTON(self, 6051, self.OnPaste)
29 EVT_BUTTON(self, 6052, self.OnCopyBitmap)
30
31 self.SetAutoLayout(true)
32 self.SetSizer(sizer)
33
34
35 def OnCopy(self, evt):
36 self.do = wxTextDataObject()
37 self.do.SetText(self.text.GetValue())
38 wxTheClipboard.Open()
39 wxTheClipboard.SetData(self.do)
40 wxTheClipboard.Close()
41
42
43 def OnPaste(self, evt):
44 do = wxTextDataObject()
45 wxTheClipboard.Open()
46 success = wxTheClipboard.GetData(do)
47 wxTheClipboard.Close()
48 if success:
49 self.text.SetValue(do.GetText())
50 else:
51 wxMessageBox("There is no data in the clipboard in the required format",
52 "Error")
53
54 def OnCopyBitmap(self, evt):
55 dlg = wxFileDialog(self, "Choose a bitmap to copy", wildcard="*.bmp")
56 if dlg.ShowModal() == wxID_OK:
57 bmp = wxBitmap(dlg.GetFilename(), wxBITMAP_TYPE_BMP)
58 bmpdo = wxBitmapDataObject(bmp)
59 wxTheClipboard.Open()
60 wxTheClipboard.SetData(bmpdo)
61 wxTheClipboard.Close()
62
63 wxMessageBox("The bitmap is now in the Clipboard. Switch to a graphics\n"
64 "editor and try pasting it in...")
65 dlg.Destroy()
66
67 #----------------------------------------------------------------------
68
69 class OtherDropTarget(wxPyDropTarget):
70 def __init__(self, window, log):
71 wxPyDropTarget.__init__(self)
72 self.log = log
73 self.do = wxFileDataObject()
74 self.SetDataObject(self.do)
75
76 def OnEnter(self, x, y, d):
77 self.log.WriteText("OnEnter: %d, %d, %d\n" % (x, y, d))
78 return wxDragCopy
79
80 #def OnDragOver(self, x, y, d):
81 # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d))
82 # return wxDragCopy
83
84 def OnLeave(self):
85 self.log.WriteText("OnLeave\n")
86
87 def OnDrop(self, x, y):
88 self.log.WriteText("OnDrop: %d %d\n" % (x, y))
89 return true
90
91 def OnData(self, x, y, d):
92 self.log.WriteText("OnData: %d, %d, %d\n" % (x, y, d))
93 self.GetData()
94 self.log.WriteText("%s\n" % self.do.GetFilenames())
95 return d
96
97
98
99
100 class MyFileDropTarget(wxFileDropTarget):
101 def __init__(self, window, log):
102 wxFileDropTarget.__init__(self)
103 self.window = window
104 self.log = log
105
106 def OnDropFiles(self, x, y, filenames):
107 self.window.SetInsertionPointEnd()
108 self.window.WriteText("\n%d file(s) dropped at %d,%d:\n" %
109 (len(filenames), x, y))
110 for file in filenames:
111 self.window.WriteText(file + '\n')
112
113
114 class MyTextDropTarget(wxTextDropTarget):
115 def __init__(self, window, log):
116 wxTextDropTarget.__init__(self)
117 self.window = window
118 self.log = log
119
120 def OnDropText(self, x, y, text):
121 self.window.WriteText("(%d, %d)\n%s\n" % (x, y, text))
122
123 def OnDragOver(self, x, y, d):
124 return wxDragCopy
125
126
127 class FileDropPanel(wxPanel):
128 def __init__(self, parent, log):
129 wxPanel.__init__(self, parent, -1)
130
131 #self.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD, false))
132
133 sizer = wxBoxSizer(wxVERTICAL)
134 sizer.Add(wxStaticText(self, -1, " \nDrag some files here:"),
135 0, wxEXPAND|wxALL, 2)
136
137 self.text = wxTextCtrl(self, -1, "",
138 style = wxTE_MULTILINE|wxHSCROLL|wxTE_READONLY)
139 dt = MyFileDropTarget(self, log)
140 self.text.SetDropTarget(dt)
141 sizer.Add(self.text, 1, wxEXPAND)
142
143 sizer.Add(wxStaticText(self, -1, " \nDrag some text here:"),
144 0, wxEXPAND|wxALL, 2)
145 self.text2 = wxTextCtrl(self, -1, "",
146 style = wxTE_MULTILINE|wxHSCROLL|wxTE_READONLY)
147 dt = MyTextDropTarget(self.text2, log)
148 self.text2.SetDropTarget(dt)
149 sizer.Add(self.text2, 1, wxEXPAND)
150
151 self.SetAutoLayout(true)
152 self.SetSizer(sizer)
153
154
155 def WriteText(self, text):
156 self.text.WriteText(text)
157
158 def SetInsertionPointEnd(self):
159 self.text.SetInsertionPointEnd()
160
161
162 #----------------------------------------------------------------------
163 #----------------------------------------------------------------------
164
165 class TestPanel(wxPanel):
166 def __init__(self, parent, log):
167 wxPanel.__init__(self, parent, -1)
168
169 self.SetAutoLayout(true)
170 outsideSizer = wxBoxSizer(wxVERTICAL)
171
172 msg = "Clipboard / Drag-And-Drop"
173 text = wxStaticText(self, -1, "", style=wxALIGN_CENTRE)
174 text.SetFont(wxFont(24, wxSWISS, wxNORMAL, wxBOLD, false))
175 text.SetLabel(msg)
176 w,h = text.GetTextExtent(msg)
177 text.SetSize(wxSize(w,h+1))
178 text.SetForegroundColour(wxBLUE)
179 outsideSizer.Add(text, 0, wxEXPAND|wxALL, 5)
180 outsideSizer.Add(wxStaticLine(self, -1), 0, wxEXPAND)
181
182 inSizer = wxBoxSizer(wxHORIZONTAL)
183 inSizer.Add(ClipTextPanel(self, log), 1, wxEXPAND)
184 inSizer.Add(FileDropPanel(self, log), 1, wxEXPAND)
185
186 outsideSizer.Add(inSizer, 1, wxEXPAND)
187 self.SetSizer(outsideSizer)
188
189
190 #----------------------------------------------------------------------
191
192 def runTest(frame, nb, log):
193 win = TestPanel(nb, log)
194 return win
195
196 #----------------------------------------------------------------------
197
198
199
200
201
202
203
204
205
206
207
208
209 overview = """\
210 This demo shows some examples of data transfer through clipboard or drag and drop. In wxWindows, these two ways to transfer data (either between different applications or inside one and the same) are very similar which allows to implement both of them using almost the same code - or, in other words, if you implement drag and drop support for your application, you get clipboard support for free and vice versa.
211
212 At the heart of both clipboard and drag and drop operations lies the wxDataObject class. The objects of this class (or, to be precise, classes derived from it) represent the data which is being carried by the mouse during drag and drop operation or copied to or pasted from the clipboard. wxDataObject is a "smart" piece of data because it knows which formats it supports (see GetFormatCount and GetAllFormats) and knows how to render itself in any of them (see GetDataHere). It can also receive its value from the outside in a format it supports if it implements the SetData method. Please see the documentation of this class for more details.
213
214 Both clipboard and drag and drop operations have two sides: the source and target, the data provider and the data receiver. These which may be in the same application and even the same window when, for example, you drag some text from one position to another in a word processor. Let us describe what each of them should do.
215
216 """