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