diff --git a/docs/changes.txt b/docs/changes.txt index 41018d17fa..cef0b49bd5 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -101,6 +101,7 @@ All (GUI): - Fix creating/removing mode buttons in wxPG manager (Artur Wieczorek). - Harmonize wxMenuEvent handling between all major ports. - Fix wxPGChoices copy ctor (Snoits). +- Show how to handle files on command line in docview sample (Neil Mayhew). wxGTK: diff --git a/samples/docview/docview.cpp b/samples/docview/docview.cpp index 74023289b4..cf0c52477c 100644 --- a/samples/docview/docview.cpp +++ b/samples/docview/docview.cpp @@ -108,6 +108,10 @@ void MyApp::OnInitCmdLine(wxCmdLineParser& parser) "run in SDI mode: multiple documents, multiple windows"); parser.AddSwitch("", CmdLineOption::SINGLE, "run in single document mode"); + + parser.AddParam("document-file", + wxCMD_LINE_VAL_STRING, + wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL); } bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser) @@ -140,9 +144,19 @@ bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser) return false; } + // save any files given on the command line: we'll open them in OnInit() + // later, after creating the frame + for ( size_t i = 0; i != parser.GetParamCount(); ++i ) + m_filesFromCmdLine.push_back(parser.GetParam(i)); + return wxApp::OnCmdLineParsed(parser); } +void MyApp::MacNewFile() +{ + wxDocManager::GetDocumentManager()->CreateNewDocument(); +} + bool MyApp::OnInit() { if ( !wxApp::OnInit() ) @@ -229,7 +243,6 @@ bool MyApp::OnInit() { m_canvas = new MyCanvas(NULL, frame); m_menuEdit = CreateDrawingEditMenu(); - docManager->CreateNewDocument(); } CreateMenuBarForFrame(frame, menuFile, m_menuEdit); @@ -238,6 +251,16 @@ bool MyApp::OnInit() frame->Centre(); frame->Show(); + if ( m_filesFromCmdLine.empty() ) + { + docManager->CreateNewDocument(); + } + else // we have files to open on command line + { + for ( size_t i = 0; i != m_filesFromCmdLine.size(); ++i ) + docManager->CreateDocument(m_filesFromCmdLine[i], wxDOC_SILENT); + } + return true; } diff --git a/samples/docview/docview.h b/samples/docview/docview.h index e598067fb7..e4a459254f 100644 --- a/samples/docview/docview.h +++ b/samples/docview/docview.h @@ -13,6 +13,7 @@ #define _WX_SAMPLES_DOCVIEW_DOCVIEW_H_ #include "wx/docview.h" +#include "wx/vector.h" class MyCanvas; @@ -39,6 +40,8 @@ public: virtual void OnInitCmdLine(wxCmdLineParser& parser) wxOVERRIDE; virtual bool OnCmdLineParsed(wxCmdLineParser& parser) wxOVERRIDE; + virtual void MacNewFile() wxOVERRIDE; + // our specific methods Mode GetMode() const { return m_mode; } wxFrame *CreateChildFrame(wxView *view, bool isCanvas); @@ -68,6 +71,8 @@ private: // application object itself void OnAbout(wxCommandEvent& event); + // contains the file names given on the command line, possibly empty + wxVector m_filesFromCmdLine; // the currently used mode Mode m_mode;