]>
Commit | Line | Data |
---|---|---|
43d811ea | 1 | ///////////////////////////////////////////////////////////////////////////// |
457814b5 JS |
2 | // Name: dnd.cpp |
3 | // Purpose: Drag and drop sample | |
4 | // Author: Vadim Zeitlin | |
43d811ea JS |
5 | // Modified by: |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: | |
8 | // Copyright: | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
457814b5 JS |
12 | #include "wx/wxprec.h" |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/wx.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/intl.h" | |
23 | #include "wx/log.h" | |
24 | ||
25 | #include "wx/dnd.h" | |
26 | ||
27 | // ---------------------------------------------------------------------------- | |
28 | // Derive 2 simple classes which just put in the listbox the strings (text or | |
29 | // file names) we drop on them | |
30 | // ---------------------------------------------------------------------------- | |
31 | class DnDText : public wxTextDropTarget | |
32 | { | |
33 | public: | |
34 | DnDText(wxListBox *pOwner) { m_pOwner = pOwner; } | |
35 | ||
36 | virtual bool OnDropText(long x, long y, const char *psz); | |
37 | ||
38 | private: | |
39 | wxListBox *m_pOwner; | |
40 | }; | |
41 | ||
42 | class DnDFile : public wxFileDropTarget | |
43 | { | |
44 | public: | |
45 | DnDFile(wxListBox *pOwner) { m_pOwner = pOwner; } | |
46 | ||
47 | virtual bool OnDropFiles(long x, long y, | |
48 | size_t nFiles, const char * const aszFiles[]); | |
49 | ||
50 | private: | |
51 | wxListBox *m_pOwner; | |
52 | }; | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // Define a new application type | |
56 | // ---------------------------------------------------------------------------- | |
57 | class DnDApp : public wxApp | |
58 | { | |
59 | public: | |
60 | bool OnInit(); | |
61 | }; | |
62 | ||
63 | IMPLEMENT_APP(DnDApp); | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // Define a new frame type | |
67 | // ---------------------------------------------------------------------------- | |
68 | class DnDFrame : public wxFrame | |
69 | { | |
70 | public: | |
71 | DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h); | |
72 | ~DnDFrame(); | |
73 | ||
74 | void OnPaint(wxPaintEvent& event); | |
75 | void OnQuit (wxCommandEvent& event); | |
76 | void OnAbout(wxCommandEvent& event); | |
43d811ea | 77 | void OnDrag (wxCommandEvent& event); |
457814b5 | 78 | void OnHelp (wxCommandEvent& event); |
43d811ea JS |
79 | void OnLogClear(wxCommandEvent& event); |
80 | ||
81 | void OnMouseBtnDown(wxMouseEvent& event); | |
457814b5 JS |
82 | |
83 | bool OnClose(); | |
84 | ||
85 | DECLARE_EVENT_TABLE() | |
86 | ||
87 | private: | |
88 | wxListBox *m_ctrlFile, | |
89 | *m_ctrlText; | |
90 | wxTextCtrl *m_ctrlLog; | |
91 | ||
43d811ea JS |
92 | wxLog *m_pLog, *m_pLogPrev; |
93 | ||
94 | wxString m_strText; | |
457814b5 JS |
95 | }; |
96 | ||
97 | // ---------------------------------------------------------------------------- | |
98 | // IDs for the menu commands | |
99 | // ---------------------------------------------------------------------------- | |
100 | enum | |
101 | { | |
102 | Menu_Quit = 1, | |
43d811ea | 103 | Menu_Drag, |
457814b5 JS |
104 | Menu_About = 101, |
105 | Menu_Help, | |
43d811ea | 106 | Menu_Clear, |
457814b5 JS |
107 | }; |
108 | ||
109 | BEGIN_EVENT_TABLE(DnDFrame, wxFrame) | |
43d811ea JS |
110 | EVT_MENU(Menu_Quit, DnDFrame::OnQuit) |
111 | EVT_MENU(Menu_About, DnDFrame::OnAbout) | |
112 | EVT_MENU(Menu_Drag, DnDFrame::OnDrag) | |
113 | EVT_MENU(Menu_Help, DnDFrame::OnHelp) | |
114 | EVT_MENU(Menu_Clear, DnDFrame::OnLogClear) | |
115 | ||
116 | EVT_LEFT_DOWN(OnMouseBtnDown) | |
117 | EVT_RIGHT_DOWN(OnMouseBtnDown) | |
118 | EVT_MIDDLE_DOWN(OnMouseBtnDown) | |
457814b5 JS |
119 | END_EVENT_TABLE() |
120 | ||
121 | // `Main program' equivalent, creating windows and returning main app frame | |
122 | bool DnDApp::OnInit(void) | |
123 | { | |
124 | // create the main frame window | |
125 | DnDFrame *frame = new DnDFrame(NULL, "Drag & Drop wxWindows App", | |
126 | 50, 50, 450, 340); | |
127 | ||
128 | // activate it | |
129 | frame->Show(TRUE); | |
130 | ||
131 | SetTopWindow(frame); | |
132 | ||
133 | return TRUE; | |
134 | } | |
135 | ||
136 | DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) | |
43d811ea JS |
137 | : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)), |
138 | m_strText("wxWindows drag & drop works :-)") | |
139 | ||
457814b5 | 140 | { |
e3e65dac RR |
141 | |
142 | #ifdef __WXMSW__ | |
43d811ea | 143 | // frame icon and status bar |
457814b5 | 144 | SetIcon(wxIcon("mondrian")); |
e3e65dac RR |
145 | #endif |
146 | ||
43d811ea JS |
147 | const int widths[] = { -1 }; |
148 | CreateStatusBar(); | |
457814b5 JS |
149 | |
150 | // construct menu | |
151 | wxMenu *file_menu = new wxMenu; | |
43d811ea | 152 | file_menu->Append(Menu_Drag, "&Test drag..."); |
457814b5 JS |
153 | file_menu->AppendSeparator(); |
154 | file_menu->Append(Menu_Quit, "E&xit"); | |
43d811ea JS |
155 | |
156 | wxMenu *log_menu = new wxMenu; | |
157 | log_menu->Append(Menu_Clear, "Clear"); | |
158 | ||
159 | wxMenu *help_menu = new wxMenu; | |
160 | help_menu->Append(Menu_Help, "&Help..."); | |
161 | help_menu->AppendSeparator(); | |
162 | help_menu->Append(Menu_About, "&About"); | |
163 | ||
457814b5 JS |
164 | wxMenuBar *menu_bar = new wxMenuBar; |
165 | menu_bar->Append(file_menu, "&File"); | |
43d811ea JS |
166 | menu_bar->Append(log_menu, "&Log"); |
167 | menu_bar->Append(help_menu, "&Help"); | |
168 | ||
457814b5 JS |
169 | SetMenuBar(menu_bar); |
170 | ||
171 | // make a panel with 3 subwindows | |
172 | wxPoint pos(0, 0); | |
173 | wxSize size(400, 200); | |
174 | ||
175 | wxString strFile("Drop files here!"), strText("Drop text on me"); | |
176 | ||
177 | m_ctrlFile = new wxListBox(this, -1, pos, size, 1, &strFile, wxLB_HSCROLL); | |
178 | m_ctrlText = new wxListBox(this, -1, pos, size, 1, &strText, wxLB_HSCROLL); | |
e3e65dac | 179 | |
457814b5 JS |
180 | m_ctrlLog = new wxTextCtrl(this, -1, "", pos, size, |
181 | wxTE_MULTILINE | wxTE_READONLY | | |
182 | wxSUNKEN_BORDER| wxHSCROLL); | |
183 | ||
184 | // redirect log messages to the text window (don't forget to delete it!) | |
43d811ea JS |
185 | m_pLog = new wxLogTextCtrl(m_ctrlLog); |
186 | m_pLogPrev = wxLog::SetActiveTarget(m_pLog); | |
457814b5 JS |
187 | |
188 | // associate drop targets with 2 text controls | |
e3e65dac RR |
189 | // m_ctrlFile->SetDropTarget(new DnDFile(m_ctrlFile)); |
190 | m_ctrlText->SetDropTarget(new DnDText(m_ctrlText)); | |
457814b5 | 191 | |
e3e65dac | 192 | wxLayoutConstraints *c; |
457814b5 | 193 | |
40e1a9c0 | 194 | // Top-left listbox |
457814b5 JS |
195 | c = new wxLayoutConstraints; |
196 | c->left.SameAs (this, wxLeft); | |
197 | c->top.SameAs (this, wxTop); | |
198 | c->right.PercentOf(this, wxRight, 50); | |
43d811ea | 199 | c->height.PercentOf(this, wxHeight, 40); |
457814b5 JS |
200 | m_ctrlFile->SetConstraints(c); |
201 | ||
40e1a9c0 | 202 | // Top-right listbox |
457814b5 JS |
203 | c = new wxLayoutConstraints; |
204 | c->left.SameAs (m_ctrlFile, wxRight); | |
205 | c->top.SameAs (this, wxTop); | |
206 | c->right.SameAs (this, wxRight); | |
43d811ea | 207 | c->height.PercentOf(this, wxHeight, 40); |
457814b5 JS |
208 | m_ctrlText->SetConstraints(c); |
209 | ||
40e1a9c0 | 210 | // Lower text control |
457814b5 JS |
211 | c = new wxLayoutConstraints; |
212 | c->left.SameAs (this, wxLeft); | |
213 | c->right.SameAs (this, wxRight); | |
43d811ea JS |
214 | c->height.PercentOf(this, wxHeight, 40); |
215 | c->top.SameAs(m_ctrlText, wxBottom); | |
457814b5 JS |
216 | |
217 | m_ctrlLog->SetConstraints(c); | |
218 | ||
219 | SetAutoLayout(TRUE); | |
220 | } | |
221 | ||
222 | void DnDFrame::OnQuit(wxCommandEvent& /* event */) | |
223 | { | |
224 | Close(TRUE); | |
225 | } | |
226 | ||
43d811ea JS |
227 | void DnDFrame::OnDrag(wxCommandEvent& /* event */) |
228 | { | |
229 | wxString strText = wxGetTextFromUser | |
230 | ( | |
231 | "After you enter text in this dialog, press any mouse\n" | |
232 | "button in the bottom (empty) part of the frame and \n" | |
233 | "drag it anywhere - you will be in fact dragging the\n" | |
234 | "text object containing this text", | |
235 | "Please enter some text", m_strText, this | |
236 | ); | |
237 | ||
238 | m_strText = strText; | |
239 | } | |
240 | ||
457814b5 JS |
241 | void DnDFrame::OnAbout(wxCommandEvent& /* event */) |
242 | { | |
243 | wxMessageDialog dialog(this, | |
244 | "Drag-&-Drop Demo\n" | |
43d811ea JS |
245 | "Please see \"Help|Help...\" for details\n" |
246 | "Copyright (c) 1998 Vadim Zeitlin", | |
457814b5 JS |
247 | "About wxDnD"); |
248 | ||
249 | dialog.ShowModal(); | |
250 | } | |
251 | ||
252 | void DnDFrame::OnHelp(wxCommandEvent& /* event */) | |
253 | { | |
254 | wxMessageDialog dialog(this, | |
43d811ea JS |
255 | "This small program demonstrates drag & drop support in wxWindows. " |
256 | "The program window consists of 3 parts: the bottom pane is for " | |
257 | "debug messages, so that you can see what's going on inside. " | |
258 | "The top part is split into 2 listboxes, the left one accepts " | |
259 | "files and the right one accepts text." | |
260 | "\n\n" | |
261 | "To test wxDropTarget: open wordpad (write.exe), select some text in " | |
262 | "it and drag it to the right listbox (you'll notice the usual visual " | |
263 | "feedback, i.e. the cursor will change). Also, try dragging some " | |
264 | "files (you can select several at once) from Windows Explorer (or " | |
265 | "File Manager) to the left pane. Hold down Ctrl/Shift keys when " | |
266 | "you drop text (doesn't work with files) and see what changes. " | |
267 | "\n\n" | |
268 | "To test wxDropSource: just press any mouse button on the empty zone of " | |
269 | "the window and drag it to wordpad or any other droptarget accepting " | |
270 | "text (and of course you can just drag it to the right pane). Due to " | |
271 | "a lot of trace messages, the cursor might take some time to change, " | |
272 | "don't release the mouse button until it does. You can change the " | |
273 | "string being dragged in in \"File|Test drag...\" dialog." | |
274 | "\n\n" | |
275 | "Please send all questions/bug reports/suggestions &c to " | |
457814b5 JS |
276 | "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>", |
277 | "wxDnD Help"); | |
278 | ||
279 | dialog.ShowModal(); | |
280 | } | |
281 | ||
e3e65dac | 282 | void DnDFrame::OnLogClear(wxCommandEvent& /* event */ ) |
43d811ea JS |
283 | { |
284 | m_ctrlLog->Clear(); | |
285 | } | |
286 | ||
457814b5 JS |
287 | bool DnDFrame::OnClose() |
288 | { | |
289 | return TRUE; | |
290 | } | |
291 | ||
e3e65dac | 292 | void DnDFrame::OnMouseBtnDown(wxMouseEvent& /* event */ ) |
43d811ea JS |
293 | { |
294 | if ( !m_strText.IsEmpty() ) { | |
295 | // start drag operation | |
296 | wxTextDataObject data(m_strText); | |
e3e65dac | 297 | wxDropSource dragSource(data, this); |
43d811ea JS |
298 | const char *pc; |
299 | ||
300 | switch ( dragSource.DoDragDrop(TRUE) ) { | |
301 | case wxDropSource::Error: pc = "Error!"; break; | |
302 | case wxDropSource::None: pc = "Nothing"; break; | |
303 | case wxDropSource::Copy: pc = "Copied"; break; | |
304 | case wxDropSource::Move: pc = "Moved"; break; | |
305 | case wxDropSource::Cancel: pc = "Cancelled"; break; | |
306 | default: pc = "Huh?"; break; | |
307 | } | |
308 | ||
309 | SetStatusText(wxString("Drag result: ") + pc); | |
310 | } | |
311 | } | |
312 | ||
457814b5 JS |
313 | DnDFrame::~DnDFrame() |
314 | { | |
315 | if ( m_pLog != NULL ) { | |
43d811ea | 316 | if ( wxLog::SetActiveTarget(m_pLogPrev) == m_pLog ) |
457814b5 JS |
317 | delete m_pLog; |
318 | } | |
319 | } | |
320 | ||
321 | // ---------------------------------------------------------------------------- | |
322 | // Notifications called by the base class | |
323 | // ---------------------------------------------------------------------------- | |
324 | bool DnDText::OnDropText(long, long, const char *psz) | |
325 | { | |
326 | m_pOwner->Append(psz); | |
327 | ||
328 | return TRUE; | |
329 | } | |
330 | ||
331 | bool DnDFile::OnDropFiles(long, long, size_t nFiles, | |
332 | const char * const aszFiles[]) | |
333 | { | |
334 | wxString str; | |
335 | str.Printf("%d files dropped", nFiles); | |
336 | m_pOwner->Append(str); | |
337 | for ( size_t n = 0; n < nFiles; n++ ) { | |
338 | m_pOwner->Append(aszFiles[n]); | |
339 | } | |
340 | ||
341 | return TRUE; | |
342 | } |