Fix command line parsing in media player sample.

It was broken since the addition of the call to wxApp::OnInit() to
wxMediaPlayerApp::OnInit() -- as the base class parses the command line
itself, we need to use OnInitCmdLine() and OnCmdLineParsed() instead of doing
it manually if we call it.

Also don't load the play list saved in wxConfig if any command line parameters
were given as otherwise the saved files would be used first (this seems to
have been always broken...).

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71926 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2012-07-02 00:16:12 +00:00
parent 69bacfaacf
commit fcbf733155

View File

@@ -66,6 +66,7 @@
#include "wx/dnd.h" // drag and drop for the playlist #include "wx/dnd.h" // drag and drop for the playlist
#include "wx/filename.h" // For wxFileName::GetName() #include "wx/filename.h" // For wxFileName::GetName()
#include "wx/config.h" // for native wxConfig #include "wx/config.h" // for native wxConfig
#include "wx/vector.h"
// Under MSW we have several different backends but when linking statically // Under MSW we have several different backends but when linking statically
// they may be discarded by the linker (this definitely happens with MSVC) so // they may be discarded by the linker (this definitely happens with MSVC) so
@@ -146,6 +147,14 @@ public:
virtual void MacOpenFiles(const wxArrayString & fileNames ); virtual void MacOpenFiles(const wxArrayString & fileNames );
#endif #endif
#if wxUSE_CMDLINE_PARSER
virtual void OnInitCmdLine(wxCmdLineParser& parser);
virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
// Files specified on the command line, if any.
wxVector<wxString> m_params;
#endif // wxUSE_CMDLINE_PARSER
virtual bool OnInit(); virtual bool OnInit();
protected: protected:
@@ -408,6 +417,34 @@ const wxChar* wxGetMediaStateText(int nState)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
IMPLEMENT_APP(wxMediaPlayerApp) IMPLEMENT_APP(wxMediaPlayerApp)
// ----------------------------------------------------------------------------
// wxMediaPlayerApp command line parsing
// ----------------------------------------------------------------------------
#if wxUSE_CMDLINE_PARSER
void wxMediaPlayerApp::OnInitCmdLine(wxCmdLineParser& parser)
{
wxApp::OnInitCmdLine(parser);
parser.AddParam("input files",
wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE);
}
bool wxMediaPlayerApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
if ( !wxApp::OnCmdLineParsed(parser) )
return false;
for (size_t paramNr=0; paramNr < parser.GetParamCount(); ++paramNr)
m_params.push_back(parser.GetParam(paramNr));
return true;
}
#endif // wxUSE_CMDLINE_PARSER
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxMediaPlayerApp::OnInit // wxMediaPlayerApp::OnInit
// //
@@ -429,34 +466,15 @@ bool wxMediaPlayerApp::OnInit()
frame->Show(true); frame->Show(true);
#if wxUSE_CMDLINE_PARSER #if wxUSE_CMDLINE_PARSER
// if ( !m_params.empty() )
// What this does is get all the command line arguments
// and treat each one as a file to put to the initial playlist
//
wxCmdLineEntryDesc cmdLineDesc[2];
cmdLineDesc[0].kind = wxCMD_LINE_PARAM;
cmdLineDesc[0].shortName = NULL;
cmdLineDesc[0].longName = NULL;
cmdLineDesc[0].description = "input files";
cmdLineDesc[0].type = wxCMD_LINE_VAL_STRING;
cmdLineDesc[0].flags = wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE;
cmdLineDesc[1].kind = wxCMD_LINE_NONE;
// gets the passed media files from cmd line
wxCmdLineParser parser (cmdLineDesc, argc, argv);
// get filenames from the commandline
if (parser.Parse() == 0)
{ {
for (size_t paramNr=0; paramNr < parser.GetParamCount(); ++paramNr) for ( size_t n = 0; n < m_params.size(); n++ )
{ frame->AddToPlayList(m_params[n]);
frame->AddToPlayList((parser.GetParam (paramNr)));
}
wxCommandEvent theEvent(wxEVT_COMMAND_MENU_SELECTED, wxID_NEXT); wxCommandEvent theEvent(wxEVT_COMMAND_MENU_SELECTED, wxID_NEXT);
frame->AddPendingEvent(theEvent); frame->AddPendingEvent(theEvent);
} }
#endif #endif // wxUSE_CMDLINE_PARSER
return true; return true;
} }
@@ -702,24 +720,34 @@ wxMediaPlayerFrame::wxMediaPlayerFrame(const wxString& title)
wxT(""), wxT(""),
true); true);
//
// Here we load the our configuration - // Don't load previous files if we have some specified on the command line,
// in our case we load all the files that were left in // we wouldn't play them otherwise (they'd have to be inserted into the
// the playlist the last time the user closed our application // play list at the beginning instead of being appended but we don't
// // support this).
// As an exercise to the reader try modifying it so that #if wxUSE_CMDLINE_PARSER
// it properly loads the playlist for each page without if ( wxGetApp().m_params.empty() )
// conflicting (loading the same data) with the other ones. #endif // wxUSE_CMDLINE_PARSER
//
wxConfig conf;
wxString key, outstring;
for(int i = 0; ; ++i)
{ {
key.clear(); //
key << i; // Here we load the our configuration -
if(!conf.Read(key, &outstring)) // in our case we load all the files that were left in
break; // the playlist the last time the user closed our application
page->m_playlist->AddToPlayList(outstring); //
// As an exercise to the reader try modifying it so that
// it properly loads the playlist for each page without
// conflicting (loading the same data) with the other ones.
//
wxConfig conf;
wxString key, outstring;
for(int i = 0; ; ++i)
{
key.clear();
key << i;
if(!conf.Read(key, &outstring))
break;
page->m_playlist->AddToPlayList(outstring);
}
} }
// //