]>
git.saurik.com Git - wxWidgets.git/blob - samples/dnd/dnd.cpp
8b6fb25f47981c7831a95ca460951c4ff034bc25
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Drag and drop sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
32 // ----------------------------------------------------------------------------
33 // Derive 2 simple classes which just put in the listbox the strings (text or
34 // file names) we drop on them
35 // ----------------------------------------------------------------------------
36 class DnDText
: public wxTextDropTarget
39 DnDText(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
41 virtual bool OnDropText(long x
, long y
, const char *psz
);
47 class DnDFile
: public wxFileDropTarget
50 DnDFile(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
52 virtual bool OnDropFiles(long x
, long y
,
53 size_t nFiles
, const char * const aszFiles
[]);
59 // ----------------------------------------------------------------------------
60 // Define a new application type
61 // ----------------------------------------------------------------------------
62 class DnDApp
: public wxApp
68 IMPLEMENT_APP(DnDApp
);
70 // ----------------------------------------------------------------------------
71 // Define a new frame type
72 // ----------------------------------------------------------------------------
73 class DnDFrame
: public wxFrame
76 DnDFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
79 void OnPaint(wxPaintEvent
& event
);
80 void OnQuit (wxCommandEvent
& event
);
81 void OnAbout(wxCommandEvent
& event
);
82 void OnHelp (wxCommandEvent
& event
);
89 wxListBox
*m_ctrlFile
,
91 wxTextCtrl
*m_ctrlLog
;
93 wxLogTarget
*m_pLog
, *m_pLogPrev
;
96 // ----------------------------------------------------------------------------
97 // IDs for the menu commands
98 // ----------------------------------------------------------------------------
106 BEGIN_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
)
111 //EVT_PAINT(DnDFrame::OnPaint)
114 // `Main program' equivalent, creating windows and returning main app frame
115 bool DnDApp::OnInit(void)
117 // create the main frame window
118 DnDFrame
*frame
= new DnDFrame(NULL
, "Drag & Drop wxWindows App",
129 DnDFrame::DnDFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
)
130 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
132 SetIcon(wxIcon("mondrian"));
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
);
144 // make a panel with 3 subwindows
146 wxSize
size(400, 200);
148 wxString
strFile("Drop files here!"), strText("Drop text on me");
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
);
156 // redirect log messages to the text window (don't forget to delete it!)
157 // m_pLog = new wxLogTextCtrl(m_ctrlLog);
159 m_pLogPrev
= wxLogTarget::SetActiveTarget(m_pLog
);
161 // associate drop targets with 2 text controls
162 m_ctrlFile
->SetDropTarget(new DnDFile(m_ctrlFile
));
163 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
165 wxLayoutConstraints
*c
;
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
);
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
);
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
);
187 m_ctrlLog
->SetConstraints(c
);
192 void DnDFrame::OnQuit(wxCommandEvent
& /* event */)
197 void DnDFrame::OnAbout(wxCommandEvent
& /* event */)
199 wxMessageDialog
dialog(this,
201 "Please see File|Help for details",
207 void DnDFrame::OnHelp(wxCommandEvent
& /* event */)
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"
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"
223 "Please address any questions/bug reports/suggestions &c to\n"
224 "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>",
230 bool DnDFrame::OnClose()
235 DnDFrame::~DnDFrame()
237 if ( m_pLog
!= NULL
) {
238 if ( wxLogTarget::SetActiveTarget(m_pLogPrev
) == m_pLog
)
243 // ----------------------------------------------------------------------------
244 // Notifications called by the base class
245 // ----------------------------------------------------------------------------
246 bool DnDText::OnDropText(long, long, const char *psz
)
248 m_pOwner
->Append(psz
);
253 bool DnDFile::OnDropFiles(long, long, size_t nFiles
,
254 const char * const aszFiles
[])
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
]);