]>
git.saurik.com Git - wxWidgets.git/blob - samples/dnd/dnd.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Drag and drop sample
4 // Author: Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
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
34 DnDText(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
36 virtual bool OnDropText(long x
, long y
, const char *psz
);
42 class DnDFile
: public wxFileDropTarget
45 DnDFile(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
47 virtual bool OnDropFiles(long x
, long y
,
48 size_t nFiles
, const char * const aszFiles
[]);
54 // ----------------------------------------------------------------------------
55 // Define a new application type
56 // ----------------------------------------------------------------------------
57 class DnDApp
: public wxApp
63 IMPLEMENT_APP(DnDApp
);
65 // ----------------------------------------------------------------------------
66 // Define a new frame type
67 // ----------------------------------------------------------------------------
68 class DnDFrame
: public wxFrame
71 DnDFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
74 void OnPaint(wxPaintEvent
& event
);
75 void OnQuit (wxCommandEvent
& event
);
76 void OnAbout(wxCommandEvent
& event
);
77 void OnDrag (wxCommandEvent
& event
);
78 void OnHelp (wxCommandEvent
& event
);
79 void OnLogClear(wxCommandEvent
& event
);
81 void OnMouseBtnDown(wxMouseEvent
& event
);
88 wxListBox
*m_ctrlFile
,
90 wxTextCtrl
*m_ctrlLog
;
92 wxLog
*m_pLog
, *m_pLogPrev
;
97 // ----------------------------------------------------------------------------
98 // IDs for the menu commands
99 // ----------------------------------------------------------------------------
109 BEGIN_EVENT_TABLE(DnDFrame
, wxFrame
)
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
)
116 EVT_LEFT_DOWN(OnMouseBtnDown
)
117 EVT_RIGHT_DOWN(OnMouseBtnDown
)
118 EVT_MIDDLE_DOWN(OnMouseBtnDown
)
121 // `Main program' equivalent, creating windows and returning main app frame
122 bool DnDApp::OnInit(void)
124 // create the main frame window
125 DnDFrame
*frame
= new DnDFrame(NULL
, "Drag & Drop wxWindows App",
136 DnDFrame::DnDFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
)
137 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
138 m_strText("wxWindows drag & drop works :-)")
143 // frame icon and status bar
144 SetIcon(wxIcon("mondrian"));
147 const int widths
[] = { -1 };
151 wxMenu
*file_menu
= new wxMenu
;
152 file_menu
->Append(Menu_Drag
, "&Test drag...");
153 file_menu
->AppendSeparator();
154 file_menu
->Append(Menu_Quit
, "E&xit");
156 wxMenu
*log_menu
= new wxMenu
;
157 log_menu
->Append(Menu_Clear
, "Clear");
159 wxMenu
*help_menu
= new wxMenu
;
160 help_menu
->Append(Menu_Help
, "&Help...");
161 help_menu
->AppendSeparator();
162 help_menu
->Append(Menu_About
, "&About");
164 wxMenuBar
*menu_bar
= new wxMenuBar
;
165 menu_bar
->Append(file_menu
, "&File");
166 menu_bar
->Append(log_menu
, "&Log");
167 menu_bar
->Append(help_menu
, "&Help");
169 SetMenuBar(menu_bar
);
171 // make a panel with 3 subwindows
173 wxSize
size(400, 200);
175 wxString
strFile("Drop files here!"), strText("Drop text on me");
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
);
180 m_ctrlLog
= new wxTextCtrl(this, -1, "", pos
, size
,
181 wxTE_MULTILINE
| wxTE_READONLY
|
182 wxSUNKEN_BORDER
| wxHSCROLL
);
184 // redirect log messages to the text window (don't forget to delete it!)
185 m_pLog
= new wxLogTextCtrl(m_ctrlLog
);
186 m_pLogPrev
= wxLog::SetActiveTarget(m_pLog
);
188 // associate drop targets with 2 text controls
189 // m_ctrlFile->SetDropTarget(new DnDFile(m_ctrlFile));
190 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
192 wxLayoutConstraints
*c
;
195 c
= new wxLayoutConstraints
;
196 c
->left
.SameAs (this, wxLeft
);
197 c
->top
.SameAs (this, wxTop
);
198 c
->right
.PercentOf(this, wxRight
, 50);
199 c
->height
.PercentOf(this, wxHeight
, 40);
200 m_ctrlFile
->SetConstraints(c
);
203 c
= new wxLayoutConstraints
;
204 c
->left
.SameAs (m_ctrlFile
, wxRight
);
205 c
->top
.SameAs (this, wxTop
);
206 c
->right
.SameAs (this, wxRight
);
207 c
->height
.PercentOf(this, wxHeight
, 40);
208 m_ctrlText
->SetConstraints(c
);
210 // Lower text control
211 c
= new wxLayoutConstraints
;
212 c
->left
.SameAs (this, wxLeft
);
213 c
->right
.SameAs (this, wxRight
);
214 c
->height
.PercentOf(this, wxHeight
, 40);
215 c
->top
.SameAs(m_ctrlText
, wxBottom
);
217 m_ctrlLog
->SetConstraints(c
);
222 void DnDFrame::OnQuit(wxCommandEvent
& /* event */)
227 void DnDFrame::OnDrag(wxCommandEvent
& /* event */)
229 wxString strText
= wxGetTextFromUser
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
241 void DnDFrame::OnAbout(wxCommandEvent
& /* event */)
243 wxMessageDialog
dialog(this,
245 "Please see \"Help|Help...\" for details\n"
246 "Copyright (c) 1998 Vadim Zeitlin",
252 void DnDFrame::OnHelp(wxCommandEvent
& /* event */)
254 wxMessageDialog
dialog(this,
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."
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. "
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."
275 "Please send all questions/bug reports/suggestions &c to "
276 "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>",
282 void DnDFrame::OnLogClear(wxCommandEvent
& /* event */ )
287 bool DnDFrame::OnClose()
292 void DnDFrame::OnMouseBtnDown(wxMouseEvent
& /* event */ )
294 if ( !m_strText
.IsEmpty() ) {
295 // start drag operation
296 wxTextDataObject
data(m_strText
);
297 wxDropSource
dragSource(data
, this);
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;
309 SetStatusText(wxString("Drag result: ") + pc
);
313 DnDFrame::~DnDFrame()
315 if ( m_pLog
!= NULL
) {
316 if ( wxLog::SetActiveTarget(m_pLogPrev
) == m_pLog
)
321 // ----------------------------------------------------------------------------
322 // Notifications called by the base class
323 // ----------------------------------------------------------------------------
324 bool DnDText::OnDropText(long, long, const char *psz
)
326 m_pOwner
->Append(psz
);
331 bool DnDFile::OnDropFiles(long, long, size_t nFiles
,
332 const char * const aszFiles
[])
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
]);