Created
February 13, 2013 10:28
-
-
Save mrmikejones/4943679 to your computer and use it in GitHub Desktop.
How to add file drag and drop support to a wxWidgets application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "main.hpp" | |
#include "wx/dnd.h" | |
#include "wx/filename.h" | |
#define APP_NAME wxString("Drag 'n' Drop Example") | |
class MyApp: public wxApp | |
{ | |
virtual bool OnInit(); | |
}; | |
enum | |
{ | |
ID_Quit = 1, | |
ID_FileOpen, | |
ID_About, | |
}; | |
BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
EVT_MENU(ID_Quit, MyFrame::OnQuit) | |
EVT_MENU(ID_FileOpen, MyFrame::OnOpen) | |
EVT_MENU(ID_About, MyFrame::OnAbout) | |
END_EVENT_TABLE() | |
IMPLEMENT_APP(MyApp) | |
bool MyApp::OnInit() | |
{ | |
wxInitAllImageHandlers(); | |
MyFrame *frame = new MyFrame(APP_NAME, wxPoint(50,50), wxSize(450,340)); | |
frame->Show(TRUE); | |
SetTopWindow(frame); | |
return TRUE; | |
} | |
class MyDropTarget : public wxFileDropTarget | |
{ | |
public: | |
MyDropTarget::MyDropTarget(MyFrame* parent) | |
: wxFileDropTarget() | |
{ | |
mParent = parent; | |
} | |
bool MyDropTarget::OnDropFiles(wxCoord x, wxCoord y, const wxArrayString& filenames) | |
{ | |
mParent->OpenFile(filenames[0]); // Just open the first file if several are dropped onto the window | |
return true; | |
} | |
private: | |
MyFrame* mParent; | |
}; | |
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
: wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
{ | |
SetMinSize(size); | |
wxMenu* menuHelp = new wxMenu; | |
menuHelp->Append( ID_About, "&About..." ); | |
wxMenu *menuFile = new wxMenu; | |
menuFile->Append(ID_FileOpen, "&Open"); | |
menuFile->AppendSeparator(); | |
menuFile->Append( ID_Quit, "E&xit" ); | |
wxMenuBar *menuBar = new wxMenuBar; | |
menuBar->Append(menuFile, "&File" ); | |
menuBar->Append(menuHelp, "&Help"); | |
SetMenuBar(menuBar); | |
mDropper = new MyDropTarget(this); | |
SetDropTarget(mDropper); | |
} | |
MyFrame::~MyFrame() | |
{ | |
} | |
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
{ | |
Close(TRUE); | |
} | |
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
{ | |
wxMessageBox("Copyright (c) 2013 Mike Jones\naudioprogrammermike@gmail.com", APP_NAME, wxOK | wxICON_INFORMATION, this); | |
} | |
void MyFrame::OnOpen(wxCommandEvent& event) | |
{ | |
wxFileDialog* OpenDialog = new wxFileDialog( | |
this, _("Choose a file to open"), wxEmptyString, wxEmptyString, | |
_("All Files (*.*)|*.*"), | |
wxFD_OPEN, wxDefaultPosition); | |
if (OpenDialog->ShowModal() == wxID_OK) // if the user click "Open" instead of "Cancel" | |
{ | |
wxString CurrentDocPath = OpenDialog->GetPath(); | |
OpenFile(CurrentDocPath); | |
} | |
// Clean up after ourselves | |
OpenDialog->Destroy(); | |
} | |
void MyFrame::OpenFile(wxString path) | |
{ | |
wxFileName filename(path); | |
SetTitle(APP_NAME << " - " << filename.GetFullName()); // Set the Title to reflect the file open | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _MAIN_HPP_ | |
#define _MAIN_HPP_ | |
#include "wx/config.h" | |
#include "wx/wx.h" | |
class MyDropTarget; | |
class MyFrame: public wxFrame | |
{ | |
public: | |
MyFrame(const wxString& title, | |
const wxPoint& pos, const wxSize& size); | |
virtual ~MyFrame(); | |
void OnQuit(wxCommandEvent& event); | |
void OnAbout(wxCommandEvent& event); | |
void OnOpen(wxCommandEvent& event); | |
void OpenFile(wxString filename); | |
private: | |
MyDropTarget* mDropper; | |
DECLARE_EVENT_TABLE() | |
}; | |
#endif //_MAIN_HPP_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment