]>
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 :-)")
141 // frame icon and status bar
142 SetIcon(wxIcon("mondrian"));
143 const int widths
[] = { -1 };
147 wxMenu
*file_menu
= new wxMenu
;
148 file_menu
->Append(Menu_Drag
, "&Test drag...");
149 file_menu
->AppendSeparator();
150 file_menu
->Append(Menu_Quit
, "E&xit");
152 wxMenu
*log_menu
= new wxMenu
;
153 log_menu
->Append(Menu_Clear
, "Clear");
155 wxMenu
*help_menu
= new wxMenu
;
156 help_menu
->Append(Menu_Help
, "&Help...");
157 help_menu
->AppendSeparator();
158 help_menu
->Append(Menu_About
, "&About");
160 wxMenuBar
*menu_bar
= new wxMenuBar
;
161 menu_bar
->Append(file_menu
, "&File");
162 menu_bar
->Append(log_menu
, "&Log");
163 menu_bar
->Append(help_menu
, "&Help");
165 SetMenuBar(menu_bar
);
167 // make a panel with 3 subwindows
169 wxSize
size(400, 200);
171 wxString
strFile("Drop files here!"), strText("Drop text on me");
173 m_ctrlFile
= new wxListBox(this, -1, pos
, size
, 1, &strFile
, wxLB_HSCROLL
);
174 m_ctrlText
= new wxListBox(this, -1, pos
, size
, 1, &strText
, wxLB_HSCROLL
);
175 m_ctrlLog
= new wxTextCtrl(this, -1, "", pos
, size
,
176 wxTE_MULTILINE
| wxTE_READONLY
|
177 wxSUNKEN_BORDER
| wxHSCROLL
);
179 // redirect log messages to the text window (don't forget to delete it!)
180 m_pLog
= new wxLogTextCtrl(m_ctrlLog
);
181 m_pLogPrev
= wxLog::SetActiveTarget(m_pLog
);
183 // associate drop targets with 2 text controls
184 m_ctrlFile
->SetDropTarget(new DnDFile(m_ctrlFile
));
185 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
187 wxLayoutConstraints
*c
;
190 c
= new wxLayoutConstraints
;
191 c
->left
.SameAs (this, wxLeft
);
192 c
->top
.SameAs (this, wxTop
);
193 c
->right
.PercentOf(this, wxRight
, 50);
194 c
->height
.PercentOf(this, wxHeight
, 40);
195 m_ctrlFile
->SetConstraints(c
);
198 c
= new wxLayoutConstraints
;
199 c
->left
.SameAs (m_ctrlFile
, wxRight
);
200 c
->top
.SameAs (this, wxTop
);
201 c
->right
.SameAs (this, wxRight
);
202 c
->height
.PercentOf(this, wxHeight
, 40);
203 m_ctrlText
->SetConstraints(c
);
205 // Lower text control
206 c
= new wxLayoutConstraints
;
207 c
->left
.SameAs (this, wxLeft
);
208 c
->right
.SameAs (this, wxRight
);
209 c
->height
.PercentOf(this, wxHeight
, 40);
210 c
->top
.SameAs(m_ctrlText
, wxBottom
);
212 m_ctrlLog
->SetConstraints(c
);
217 void DnDFrame::OnQuit(wxCommandEvent
& /* event */)
222 void DnDFrame::OnDrag(wxCommandEvent
& /* event */)
224 wxString strText
= wxGetTextFromUser
226 "After you enter text in this dialog, press any mouse\n"
227 "button in the bottom (empty) part of the frame and \n"
228 "drag it anywhere - you will be in fact dragging the\n"
229 "text object containing this text",
230 "Please enter some text", m_strText
, this
236 void DnDFrame::OnAbout(wxCommandEvent
& /* event */)
238 wxMessageDialog
dialog(this,
240 "Please see \"Help|Help...\" for details\n"
241 "Copyright (c) 1998 Vadim Zeitlin",
247 void DnDFrame::OnHelp(wxCommandEvent
& /* event */)
249 wxMessageDialog
dialog(this,
250 "This small program demonstrates drag & drop support in wxWindows. "
251 "The program window consists of 3 parts: the bottom pane is for "
252 "debug messages, so that you can see what's going on inside. "
253 "The top part is split into 2 listboxes, the left one accepts "
254 "files and the right one accepts text."
256 "To test wxDropTarget: open wordpad (write.exe), select some text in "
257 "it and drag it to the right listbox (you'll notice the usual visual "
258 "feedback, i.e. the cursor will change). Also, try dragging some "
259 "files (you can select several at once) from Windows Explorer (or "
260 "File Manager) to the left pane. Hold down Ctrl/Shift keys when "
261 "you drop text (doesn't work with files) and see what changes. "
263 "To test wxDropSource: just press any mouse button on the empty zone of "
264 "the window and drag it to wordpad or any other droptarget accepting "
265 "text (and of course you can just drag it to the right pane). Due to "
266 "a lot of trace messages, the cursor might take some time to change, "
267 "don't release the mouse button until it does. You can change the "
268 "string being dragged in in \"File|Test drag...\" dialog."
270 "Please send all questions/bug reports/suggestions &c to "
271 "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>",
277 void DnDFrame::OnLogClear(wxCommandEvent
& event
)
282 bool DnDFrame::OnClose()
287 void DnDFrame::OnMouseBtnDown(wxMouseEvent
& event
)
289 if ( !m_strText
.IsEmpty() ) {
290 // start drag operation
291 wxTextDataObject
data(m_strText
);
292 wxDropSource
dragSource(data
);
295 switch ( dragSource
.DoDragDrop(TRUE
) ) {
296 case wxDropSource::Error
: pc
= "Error!"; break;
297 case wxDropSource::None
: pc
= "Nothing"; break;
298 case wxDropSource::Copy
: pc
= "Copied"; break;
299 case wxDropSource::Move
: pc
= "Moved"; break;
300 case wxDropSource::Cancel
: pc
= "Cancelled"; break;
301 default: pc
= "Huh?"; break;
304 SetStatusText(wxString("Drag result: ") + pc
);
308 DnDFrame::~DnDFrame()
310 if ( m_pLog
!= NULL
) {
311 if ( wxLog::SetActiveTarget(m_pLogPrev
) == m_pLog
)
316 // ----------------------------------------------------------------------------
317 // Notifications called by the base class
318 // ----------------------------------------------------------------------------
319 bool DnDText::OnDropText(long, long, const char *psz
)
321 m_pOwner
->Append(psz
);
326 bool DnDFile::OnDropFiles(long, long, size_t nFiles
,
327 const char * const aszFiles
[])
330 str
.Printf("%d files dropped", nFiles
);
331 m_pOwner
->Append(str
);
332 for ( size_t n
= 0; n
< nFiles
; n
++ ) {
333 m_pOwner
->Append(aszFiles
[n
]);