]> git.saurik.com Git - wxWidgets.git/blame - samples/dnd/dnd.cpp
*** empty log message ***
[wxWidgets.git] / samples / dnd / dnd.cpp
CommitLineData
457814b5
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: dnd.cpp
3// Purpose: Drag and drop sample
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 13.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/wx.h"
25#endif
26
27#include "wx/intl.h"
28#include "wx/log.h"
29
30#include "wx/dnd.h"
31
32// ----------------------------------------------------------------------------
33// Derive 2 simple classes which just put in the listbox the strings (text or
34// file names) we drop on them
35// ----------------------------------------------------------------------------
36class DnDText : public wxTextDropTarget
37{
38public:
39 DnDText(wxListBox *pOwner) { m_pOwner = pOwner; }
40
41 virtual bool OnDropText(long x, long y, const char *psz);
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,
53 size_t nFiles, const char * const aszFiles[]);
54
55private:
56 wxListBox *m_pOwner;
57};
58
59// ----------------------------------------------------------------------------
60// Define a new application type
61// ----------------------------------------------------------------------------
62class DnDApp : public wxApp
63{
64public:
65 bool OnInit();
66};
67
68IMPLEMENT_APP(DnDApp);
69
70// ----------------------------------------------------------------------------
71// Define a new frame type
72// ----------------------------------------------------------------------------
73class DnDFrame : public wxFrame
74{
75public:
76 DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
77 ~DnDFrame();
78
79 void OnPaint(wxPaintEvent& event);
80 void OnQuit (wxCommandEvent& event);
81 void OnAbout(wxCommandEvent& event);
82 void OnHelp (wxCommandEvent& event);
83
84 bool OnClose();
85
86 DECLARE_EVENT_TABLE()
87
88private:
89 wxListBox *m_ctrlFile,
90 *m_ctrlText;
91 wxTextCtrl *m_ctrlLog;
92
93 wxLogTarget *m_pLog, *m_pLogPrev;
94};
95
96// ----------------------------------------------------------------------------
97// IDs for the menu commands
98// ----------------------------------------------------------------------------
99enum
100{
101 Menu_Quit = 1,
102 Menu_About = 101,
103 Menu_Help,
104};
105
106BEGIN_EVENT_TABLE(DnDFrame, wxFrame)
107 EVT_MENU(Menu_Quit, DnDFrame::OnQuit)
108 EVT_MENU(Menu_About, DnDFrame::OnAbout)
109 EVT_MENU(Menu_Help, DnDFrame::OnHelp)
110
111 //EVT_PAINT(DnDFrame::OnPaint)
112END_EVENT_TABLE()
113
114// `Main program' equivalent, creating windows and returning main app frame
115bool DnDApp::OnInit(void)
116{
117 // create the main frame window
118 DnDFrame *frame = new DnDFrame(NULL, "Drag & Drop wxWindows App",
119 50, 50, 450, 340);
120
121 // activate it
122 frame->Show(TRUE);
123
124 SetTopWindow(frame);
125
126 return TRUE;
127}
128
129DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
130 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
131{
132 SetIcon(wxIcon("mondrian"));
133
134 // construct menu
135 wxMenu *file_menu = new wxMenu;
136 file_menu->Append(Menu_Help, "&Help");
137 file_menu->Append(Menu_About, "&About");
138 file_menu->AppendSeparator();
139 file_menu->Append(Menu_Quit, "E&xit");
140 wxMenuBar *menu_bar = new wxMenuBar;
141 menu_bar->Append(file_menu, "&File");
142 SetMenuBar(menu_bar);
143
144 // make a panel with 3 subwindows
145 wxPoint pos(0, 0);
146 wxSize size(400, 200);
147
148 wxString strFile("Drop files here!"), strText("Drop text on me");
149
150 m_ctrlFile = new wxListBox(this, -1, pos, size, 1, &strFile, wxLB_HSCROLL);
151 m_ctrlText = new wxListBox(this, -1, pos, size, 1, &strText, wxLB_HSCROLL);
152 m_ctrlLog = new wxTextCtrl(this, -1, "", pos, size,
153 wxTE_MULTILINE | wxTE_READONLY |
154 wxSUNKEN_BORDER| wxHSCROLL);
155
156 // redirect log messages to the text window (don't forget to delete it!)
157// m_pLog = new wxLogTextCtrl(m_ctrlLog);
158 m_pLog = NULL;
159 m_pLogPrev = wxLogTarget::SetActiveTarget(m_pLog);
160
161 // associate drop targets with 2 text controls
162 m_ctrlFile->SetDropTarget(new DnDFile(m_ctrlFile));
163 m_ctrlText->SetDropTarget(new DnDText(m_ctrlText));
164
165 wxLayoutConstraints *c;
166
167 c = new wxLayoutConstraints;
168 c->left.SameAs (this, wxLeft);
169 c->top.SameAs (this, wxTop);
170 c->right.PercentOf(this, wxRight, 50);
171 c->height.PercentOf(this, wxHeight, 50);
172 m_ctrlFile->SetConstraints(c);
173
174 c = new wxLayoutConstraints;
175 c->left.SameAs (m_ctrlFile, wxRight);
176 c->top.SameAs (this, wxTop);
177 c->right.SameAs (this, wxRight);
178 c->height.PercentOf(this, wxHeight, 50);
179 m_ctrlText->SetConstraints(c);
180
181 c = new wxLayoutConstraints;
182 c->left.SameAs (this, wxLeft);
183 c->right.SameAs (this, wxRight);
184 c->height.PercentOf(this, wxHeight, 50);
185 c->bottom.SameAs(this, wxBottom);
186
187 m_ctrlLog->SetConstraints(c);
188
189 SetAutoLayout(TRUE);
190}
191
192void DnDFrame::OnQuit(wxCommandEvent& /* event */)
193{
194 Close(TRUE);
195}
196
197void DnDFrame::OnAbout(wxCommandEvent& /* event */)
198{
199 wxMessageDialog dialog(this,
200 "Drag-&-Drop Demo\n"
201 "Please see File|Help for details",
202 "About wxDnD");
203
204 dialog.ShowModal();
205}
206
207void DnDFrame::OnHelp(wxCommandEvent& /* event */)
208{
209 wxMessageDialog dialog(this,
210"This small program demonstrates drag & drop support in wxWindows.\n"
211"The program window consists of 3 parts: the bottom pane is for\n"
212"debug messages, so that you can see what's going on inside.\n"
213"The top part is split into 2 listboxes, the left one accepts\n"
214"files and the right one accepts text.\n"
215"\n"
216"To test it: open wordpad (write.exe), select some text in it and\n"
217"drag it to the right listbox (you'll notice the usual visual\n"
218"feedback, i.e. the cursor will change). Also, try dragging some\n"
219"files (you can select several at once) from Windows Explorer (or\n"
220"File Manager) to the left pane. Hold down Ctrl/Shift keys when\n"
221"you drop text (doesn't work with files) and see what changes.\n"
222"\n"
223"Please address any questions/bug reports/suggestions &c to\n"
224"Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>",
225 "wxDnD Help");
226
227 dialog.ShowModal();
228}
229
230bool DnDFrame::OnClose()
231{
232 return TRUE;
233}
234
235DnDFrame::~DnDFrame()
236{
237 if ( m_pLog != NULL ) {
238 if ( wxLogTarget::SetActiveTarget(m_pLogPrev) == m_pLog )
239 delete m_pLog;
240 }
241}
242
243// ----------------------------------------------------------------------------
244// Notifications called by the base class
245// ----------------------------------------------------------------------------
246bool DnDText::OnDropText(long, long, const char *psz)
247{
248 m_pOwner->Append(psz);
249
250 return TRUE;
251}
252
253bool DnDFile::OnDropFiles(long, long, size_t nFiles,
254 const char * const aszFiles[])
255{
256 wxString str;
257 str.Printf("%d files dropped", nFiles);
258 m_pOwner->Append(str);
259 for ( size_t n = 0; n < nFiles; n++ ) {
260 m_pOwner->Append(aszFiles[n]);
261 }
262
263 return TRUE;
264}