Show how to handle files on command line in docview sample.

This is not totally obvious, so show how to do it, including the Mac-specific
MacNewFile() overriding part.

Closes #16816.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@78498 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2015-02-15 18:35:13 +00:00
parent 0e6af4ac39
commit ad527c5468
3 changed files with 30 additions and 1 deletions

View File

@@ -101,6 +101,7 @@ All (GUI):
- Fix creating/removing mode buttons in wxPG manager (Artur Wieczorek). - Fix creating/removing mode buttons in wxPG manager (Artur Wieczorek).
- Harmonize wxMenuEvent handling between all major ports. - Harmonize wxMenuEvent handling between all major ports.
- Fix wxPGChoices copy ctor (Snoits). - Fix wxPGChoices copy ctor (Snoits).
- Show how to handle files on command line in docview sample (Neil Mayhew).
wxGTK: wxGTK:

View File

@@ -108,6 +108,10 @@ void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
"run in SDI mode: multiple documents, multiple windows"); "run in SDI mode: multiple documents, multiple windows");
parser.AddSwitch("", CmdLineOption::SINGLE, parser.AddSwitch("", CmdLineOption::SINGLE,
"run in single document mode"); "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) bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
@@ -140,9 +144,19 @@ bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
return false; 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); return wxApp::OnCmdLineParsed(parser);
} }
void MyApp::MacNewFile()
{
wxDocManager::GetDocumentManager()->CreateNewDocument();
}
bool MyApp::OnInit() bool MyApp::OnInit()
{ {
if ( !wxApp::OnInit() ) if ( !wxApp::OnInit() )
@@ -229,7 +243,6 @@ bool MyApp::OnInit()
{ {
m_canvas = new MyCanvas(NULL, frame); m_canvas = new MyCanvas(NULL, frame);
m_menuEdit = CreateDrawingEditMenu(); m_menuEdit = CreateDrawingEditMenu();
docManager->CreateNewDocument();
} }
CreateMenuBarForFrame(frame, menuFile, m_menuEdit); CreateMenuBarForFrame(frame, menuFile, m_menuEdit);
@@ -238,6 +251,16 @@ bool MyApp::OnInit()
frame->Centre(); frame->Centre();
frame->Show(); 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; return true;
} }

View File

@@ -13,6 +13,7 @@
#define _WX_SAMPLES_DOCVIEW_DOCVIEW_H_ #define _WX_SAMPLES_DOCVIEW_DOCVIEW_H_
#include "wx/docview.h" #include "wx/docview.h"
#include "wx/vector.h"
class MyCanvas; class MyCanvas;
@@ -39,6 +40,8 @@ public:
virtual void OnInitCmdLine(wxCmdLineParser& parser) wxOVERRIDE; virtual void OnInitCmdLine(wxCmdLineParser& parser) wxOVERRIDE;
virtual bool OnCmdLineParsed(wxCmdLineParser& parser) wxOVERRIDE; virtual bool OnCmdLineParsed(wxCmdLineParser& parser) wxOVERRIDE;
virtual void MacNewFile() wxOVERRIDE;
// our specific methods // our specific methods
Mode GetMode() const { return m_mode; } Mode GetMode() const { return m_mode; }
wxFrame *CreateChildFrame(wxView *view, bool isCanvas); wxFrame *CreateChildFrame(wxView *view, bool isCanvas);
@@ -68,6 +71,8 @@ private:
// application object itself // application object itself
void OnAbout(wxCommandEvent& event); void OnAbout(wxCommandEvent& event);
// contains the file names given on the command line, possibly empty
wxVector<wxString> m_filesFromCmdLine;
// the currently used mode // the currently used mode
Mode m_mode; Mode m_mode;