X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/10a535200df45d933e52501c304600c1f1e602ad..94709f191133613e115fdbf913eba5a81ddd207e:/samples/mediaplayer/mediaplayer.cpp diff --git a/samples/mediaplayer/mediaplayer.cpp b/samples/mediaplayer/mediaplayer.cpp index a56bd6fadc..fd9915f74b 100644 --- a/samples/mediaplayer/mediaplayer.cpp +++ b/samples/mediaplayer/mediaplayer.cpp @@ -12,12 +12,12 @@ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // MediaPlayer // -// This is a simple example of how to use all the funtionality of -// the wxMediaCtrl class in wxWidgets. +// This is a somewhat comprehensive example of how to use all the funtionality +// of the wxMediaCtrl class in wxWidgets. // // To use this sample, simply select Open File from the file menu, -// select the file you want to play - and MediaPlayer will play the file in a -// new notebook page, showing video if neccessary. +// select the file you want to play - and MediaPlayer will play the file in a +// the current notebook page, showing video if necessary. // // You can select one of the menu options, or move the slider around // to manipulate what is playing. @@ -25,12 +25,15 @@ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // Known bugs with wxMediaCtrl: -// -// 1) Not available on Unix :\. -// 2) Certain backends can't play the same media file at the same time (MCI, -// Cocoa NSMovieView/Quicktime). -// 3) Positioning on Mac Carbon is messed up if put in a sub-control like a -// Notebook (like this sample does). +// +// 1) Certain backends can't play the same media file at the same time (MCI, +// Cocoa NSMovieView-Quicktime). +// 2) Positioning on Mac Carbon is messed up if put in a sub-control like a +// Notebook (like this sample does) on OS versions < 10.2. +// 3) On unix the video may not work - it only checks for a few video +// sinks - xvimagesink, ximagesink and whatever gnome preferences has - +// if gnome preferences is not available or you have a different video +// sink then those two (such as sdlvideosink) then you'll get black video // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // ============================================================================ @@ -60,34 +63,28 @@ #include "wx/slider.h" //for a slider for seeking within media #include "wx/sizer.h" //for positioning controls/wxBoxSizer #include "wx/timer.h" //timer for updating status bar -#include "wx/textdlg.h" //for getting user text from OpenURL +#include "wx/textdlg.h" //for getting user text from OpenURL/Debug #include "wx/notebook.h" //for wxNotebook and putting movies in pages - -// Use some stuff that's not part of the current API, such as loading -// media from a URL, etc. -#define wxUSE_UNOFFICIALSTUFF 0 - -//Libraries for MSVC with optional backends -#ifdef _MSC_VER - #if wxUSE_DIRECTSHOW - #pragma comment(lib,"strmiids.lib") - #endif - #if wxUSE_QUICKTIME - #pragma comment(lib,"qtmlClient.lib") - #endif -#endif +#include "wx/cmdline.h" //for wxCmdLineParser (optional) +#include "wx/listctrl.h" //for wxListCtrl +#include "wx/dnd.h" //drag and drop for the playlist +#include "wx/filename.h" //For wxFileName::GetName() +#include "wx/config.h" //for native wxConfig // ---------------------------------------------------------------------------- // Bail out if the user doesn't want one of the // things we need // ---------------------------------------------------------------------------- +// RN: I'm not sure why this is here - even minimal doesn't check for +// wxUSE_GUI. I may have added it myself though... #if !wxUSE_GUI #error "This is a GUI sample" #endif -#if !wxUSE_MEDIACTRL || !wxUSE_MENUS || !wxUSE_SLIDER || !wxUSE_TIMER || !wxUSE_NOTEBOOK -#error "menus, slider, mediactrl, notebook, and timers must all be enabled for this sample!" +#if !wxUSE_MEDIACTRL || !wxUSE_MENUS || !wxUSE_SLIDER || !wxUSE_TIMER || \ + !wxUSE_NOTEBOOK || !wxUSE_LISTCTRL || !wxUSE_DRAG_AND_DROP +#error "Not all required elements are enabled. Please modify setup.h!" #endif // ============================================================================ @@ -101,7 +98,7 @@ // IDs for the controls and the menu commands enum { - // menu items + // Menu event IDs wxID_LOOP = 1, wxID_OPENFILESAMEPAGE, wxID_OPENFILENEWPAGE, @@ -110,45 +107,61 @@ enum wxID_CLOSECURRENTPAGE, wxID_PLAY, wxID_PAUSE, + wxID_NEXT, + wxID_PREV, + wxID_SELECTBACKEND, + wxID_SHOWINTERFACE, // wxID_STOP, [built-in to wxWidgets] // wxID_ABOUT, [built-in to wxWidgets] // wxID_EXIT, [built-in to wxWidgets] - - // event id for our slider - wxID_SLIDER, - - // event id for our notebook - wxID_NOTEBOOK, - - // event id for our wxMediaCtrl - wxID_MEDIACTRL + // Control event IDs + wxID_SLIDER, + wxID_NOTEBOOK, + wxID_MEDIACTRL, + wxID_BUTTONNEXT, + wxID_BUTTONPREV, + wxID_BUTTONSTOP, + wxID_BUTTONPLAY, + wxID_BUTTONVD, + wxID_BUTTONVU, + wxID_LISTCTRL, + wxID_GAUGE }; // ---------------------------------------------------------------------------- -// MyApp +// wxMediaPlayerApp // ---------------------------------------------------------------------------- -class MyApp : public wxApp +class wxMediaPlayerApp : public wxApp { public: +#ifdef __WXMAC__ + virtual void MacOpenFile(const wxString & fileName ) + { + //Called when a user drags a file over our app + m_frame->DoOpenFile(fileName); + } +#endif virtual bool OnInit(); + +protected: + class wxMediaPlayerFrame* m_frame; }; // ---------------------------------------------------------------------------- -// MyFrame +// wxMediaPlayerFrame // ---------------------------------------------------------------------------- -class MyFrame : public wxFrame +class wxMediaPlayerFrame : public wxFrame { public: // Ctor/Dtor - MyFrame(const wxString& title); - ~MyFrame(); + wxMediaPlayerFrame(const wxString& title); + ~wxMediaPlayerFrame(); // Menu event handlers void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); - void OnLoop(wxCommandEvent& event); void OnOpenFileSamePage(wxCommandEvent& event); void OnOpenFileNewPage(wxCommandEvent& event); @@ -159,10 +172,34 @@ public: void OnPlay(wxCommandEvent& event); void OnPause(wxCommandEvent& event); void OnStop(wxCommandEvent& event); + void OnNext(wxCommandEvent& event); + void OnPrev(wxCommandEvent& event); + void OnVolumeDown(wxCommandEvent& event); + void OnVolumeUp(wxCommandEvent& event); + + void OnLoop(wxCommandEvent& event); + void OnShowInterface(wxCommandEvent& event); + + void OnSelectBackend(wxCommandEvent& event); // Notebook event handlers void OnPageChange(wxNotebookEvent& event); + // Key event handlers + void OnKeyDown(wxKeyEvent& event); + + // Quickie for playing from command line + void AddToPlayList(const wxString& szString); + + // ListCtrl event handlers + void OnChangeSong(wxListEvent& event); + + // Media event handlers + void OnMediaLoaded(wxMediaEvent& event); + + // Close event handlers + void OnClose(wxCloseEvent& event); + private: // Rebuild base status string (see Implementation) void ResetStatus(); @@ -170,56 +207,154 @@ private: // Common open file code void OpenFile(bool bNewPage); void OpenURL(bool bNewPage); - - // Get the media control and slider of current notebook page + void DoOpenFile(const wxString& path, bool bNewPage); + void DoPlayFile(const wxString& path); + + // Get the controls of current notebook page wxMediaCtrl* GetCurrentMediaCtrl(); wxSlider* GetCurrentSlider(); + wxGauge* GetCurrentGauge(); - class MyTimer* m_timer; //Timer to write info to status bar + int m_nLastFileId; //List ID of played file in listctrl + wxString m_szFile; //Name of currently playing file/location + class wxMediaPlayerTimer* m_timer; //Timer to write info to status bar wxString m_basestatus; //Base status string (see ResetStatus()) - wxNotebook* m_notebook; + wxNotebook* m_notebook; //Notebook containing our pages - // So that mytimer can access MyFrame's members - friend class MyTimer; + // Maybe I should use more accessors, but for simplicity + // I'll allow the other classes access to our members + friend class wxMediaPlayerApp; + friend class wxMediaPlayerNotebookPage; + friend class wxMediaPlayerTimer; }; // ---------------------------------------------------------------------------- -// MyNotebookPage +// wxMediaPlayerNotebookPage // ---------------------------------------------------------------------------- -class MyNotebookPage : public wxPanel +class wxMediaPlayerNotebookPage : public wxPanel { - MyNotebookPage(wxNotebook* book); - + wxMediaPlayerNotebookPage(wxMediaPlayerFrame* parentFrame, + wxNotebook* book, const wxString& be = wxEmptyString); + // Slider event handlers - void OnSeek(wxCommandEvent& event); + void OnBeginSeek(wxScrollEvent& event); + void OnEndSeek(wxScrollEvent& event); // Media event handlers - void OnMediaStop(wxMediaEvent& event); - + void OnMediaFinished(wxMediaEvent& event); + public: - friend class MyFrame; //make MyFrame able to access private members + bool IsBeingDragged(); //accessor for m_bIsBeingDragged + + //make wxMediaPlayerFrame able to access the private members + friend class wxMediaPlayerFrame; + wxMediaCtrl* m_mediactrl; //Our media control + class wxMediaPlayerListCtrl* m_playlist; //Our playlist wxSlider* m_slider; //The slider below our media control int m_nLoops; //Number of times media has looped + bool m_bLoop; //Whether we are looping or not + bool m_bIsBeingDragged; //Whether the user is dragging the scroll bar + wxMediaPlayerFrame* m_parentFrame; //Main wxFrame of our sample + wxButton* m_prevButton; //Go to previous file button + wxButton* m_playButton; //Play/pause file button + wxButton* m_stopButton; //Stop playing file button + wxButton* m_nextButton; //Next file button + wxButton* m_vdButton; //Volume down button + wxButton* m_vuButton; //Volume up button + wxGauge* m_gauge; //Gauge to keep in line with slider }; // ---------------------------------------------------------------------------- -// MyTimer +// wxMediaPlayerTimer // ---------------------------------------------------------------------------- -class MyTimer : public wxTimer +class wxMediaPlayerTimer : public wxTimer { public: //Ctor - MyTimer(MyFrame* frame) {m_frame = frame;} + wxMediaPlayerTimer(wxMediaPlayerFrame* frame) {m_frame = frame;} //Called each time the timer's timeout expires void Notify(); - MyFrame* m_frame; //The MyFrame + wxMediaPlayerFrame* m_frame; //The wxMediaPlayerFrame +}; + +// ---------------------------------------------------------------------------- +// wxMediaPlayerListCtrl +// ---------------------------------------------------------------------------- +class wxMediaPlayerListCtrl : public wxListCtrl +{ +public: + void AddToPlayList(const wxString& szString) + { + wxListItem kNewItem; + kNewItem.SetAlign(wxLIST_FORMAT_LEFT); + + int nID; + + kNewItem.SetId(nID = this->GetItemCount()); + kNewItem.SetMask(wxLIST_MASK_DATA); + kNewItem.SetData(new wxString(szString)); + + this->InsertItem(kNewItem); + this->SetItem(nID, 0, _T("*")); + this->SetItem(nID, 1, wxFileName(szString).GetName()); + + if (nID % 2) + { + kNewItem.SetBackgroundColour(wxColour(192,192,192)); + this->SetItem(kNewItem); + } + } + + void GetSelectedItem(wxListItem& listitem) + { + listitem.SetMask(wxLIST_MASK_TEXT | wxLIST_MASK_DATA); + int nLast = -1, nLastSelected = -1; + while ((nLast = this->GetNextItem(nLast, + wxLIST_NEXT_ALL, + wxLIST_STATE_SELECTED)) != -1) + { + listitem.SetId(nLast); + this->GetItem(listitem); + if ((listitem.GetState() & wxLIST_STATE_FOCUSED) ) + break; + nLastSelected = nLast; + } + if (nLast == -1 && nLastSelected == -1) + return; + listitem.SetId(nLastSelected == -1 ? nLast : nLastSelected); + this->GetItem(listitem); + } + +}; + +// ---------------------------------------------------------------------------- +// wxPlayListDropTarget +// +// Drop target for playlist (i.e. user drags a file from explorer unto +// playlist it adds the file) +// ---------------------------------------------------------------------------- +class wxPlayListDropTarget : public wxFileDropTarget +{ +public: + wxPlayListDropTarget(wxMediaPlayerListCtrl& list) : m_list(list) {} + ~wxPlayListDropTarget(){} + virtual bool OnDropFiles(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), + const wxArrayString& files) + { + for (size_t i = 0; i < files.GetCount(); ++i) + { + m_list.AddToPlayList(files[i]); + } + return true; + } + wxMediaPlayerListCtrl& m_list; }; // ============================================================================ @@ -256,7 +391,7 @@ const wxChar* wxGetMediaStateText(int nState) // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // -// MyApp +// wxMediaPlayerApp // // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -271,33 +406,64 @@ const wxChar* wxGetMediaStateText(int nState) // routine, such as main or WinMain(). Use IMPLEMENT_APP_NO_MAIN if you do // not desire this behavior. // ---------------------------------------------------------------------------- -IMPLEMENT_APP(MyApp) - +IMPLEMENT_APP(wxMediaPlayerApp) // ---------------------------------------------------------------------------- -// MyApp::OnInit +// wxMediaPlayerApp::OnInit // // Where execution starts - akin to a main or WinMain. // 1) Create the frame and show it to the user -// 2) return true specifying that we want execution to continue past OnInit +// 2) Process filenames from the commandline +// 3) return true specifying that we want execution to continue past OnInit // ---------------------------------------------------------------------------- -bool MyApp::OnInit() +bool wxMediaPlayerApp::OnInit() { - MyFrame *frame = new MyFrame(_T("MediaPlayer wxWidgets Sample")); + wxMediaPlayerFrame *frame = + new wxMediaPlayerFrame(_T("MediaPlayer wxWidgets Sample")); frame->Show(true); +#if wxUSE_CMDLINE_PARSER + // + // 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 = wxT("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) + { + frame->AddToPlayList((parser.GetParam (paramNr))); + } + wxCommandEvent emptyevt; + frame->OnNext(emptyevt); + } +#endif + return true; } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // -// MyFrame +// wxMediaPlayerFrame // // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // ---------------------------------------------------------------------------- -// MyFrame Constructor +// wxMediaPlayerFrame Constructor // // 1) Create our menus // 2) Create our notebook control and add it to the frame @@ -306,53 +472,67 @@ bool MyApp::OnInit() // 5) Start our timer // ---------------------------------------------------------------------------- -MyFrame::MyFrame(const wxString& title) - : wxFrame(NULL, wxID_ANY, title) +wxMediaPlayerFrame::wxMediaPlayerFrame(const wxString& title) + : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600,600)), + m_nLastFileId(-1) { // // Create Menus // - wxMenu *menuFile = new wxMenu; - + wxMenu *fileMenu = new wxMenu; + wxMenu *controlsMenu = new wxMenu; + wxMenu *optionsMenu = new wxMenu; wxMenu *helpMenu = new wxMenu; - helpMenu->Append(wxID_ABOUT, - _T("&About...\tF1"), - _T("Show about dialog")); + wxMenu *debugMenu = new wxMenu; - menuFile->Append(wxID_OPENFILESAMEPAGE, _T("&Open File"), + fileMenu->Append(wxID_OPENFILESAMEPAGE, _T("&Open File\tCtrl-Shift-O"), _T("Open a File in the current notebook page")); - menuFile->Append(wxID_OPENFILENEWPAGE, _T("&Open File in a new page"), + fileMenu->Append(wxID_OPENFILENEWPAGE, _T("&Open File in a new page"), _T("Open a File in a new notebook page")); -#if wxUSE_UNOFFICIALSTUFF - menuFile->Append(wxID_OPENURLSAMEPAGE, _T("&Open URL"), + fileMenu->Append(wxID_OPENURLSAMEPAGE, _T("&Open URL"), _T("Open a URL in the current notebook page")); - menuFile->Append(wxID_OPENURLNEWPAGE, _T("&Open URL in a new page"), + fileMenu->Append(wxID_OPENURLNEWPAGE, _T("&Open URL in a new page"), _T("Open a URL in a new notebook page")); -#endif - menuFile->AppendSeparator(); - menuFile->Append(wxID_CLOSECURRENTPAGE, _T("&Close Current Page"), + fileMenu->AppendSeparator(); + fileMenu->Append(wxID_CLOSECURRENTPAGE, _T("&Close Current Page\tCtrl-C"), _T("Close current notebook page")); - menuFile->AppendSeparator(); - menuFile->Append(wxID_PLAY, _T("&Play"), _T("Resume playback")); - menuFile->Append(wxID_PAUSE, _T("P&ause"), _T("Pause playback")); - menuFile->Append(wxID_STOP, _T("&Stop"), _T("Stop playback")); - menuFile->AppendSeparator(); - menuFile->AppendCheckItem(wxID_LOOP, - _T("&Loop"), - _T("Loop Selected Media")); - menuFile->AppendSeparator(); - menuFile->Append(wxID_EXIT, + fileMenu->AppendSeparator(); + fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit this program")); + controlsMenu->Append(wxID_PLAY, _T("&Play/Pause\tCtrl-P"), _T("Resume/Pause playback")); + controlsMenu->Append(wxID_STOP, _T("&Stop\tCtrl-S"), _T("Stop playback")); + controlsMenu->AppendSeparator(); + controlsMenu->Append(wxID_PREV, _T("&Previous\tCtrl-B"), _T("Go to previous track")); + controlsMenu->Append(wxID_NEXT, _T("&Next\tCtrl-N"), _T("Skip to next track")); + + optionsMenu->AppendCheckItem(wxID_LOOP, + _T("&Loop\tCtrl-L"), + _T("Loop Selected Media")); + optionsMenu->AppendCheckItem(wxID_SHOWINTERFACE, + _T("&Show Interface\tCtrl-I"), + _T("Show wxMediaCtrl native controls")); + + debugMenu->Append(wxID_SELECTBACKEND, + _T("&Select Backend...\tCtrl-D"), + _T("Select a backend manually")); + + helpMenu->Append(wxID_ABOUT, + _T("&About...\tF1"), + _T("Show about dialog")); + + wxMenuBar *menuBar = new wxMenuBar(); - menuBar->Append(menuFile, _T("&File")); + menuBar->Append(fileMenu, _T("&File")); + menuBar->Append(controlsMenu, _T("&Controls")); + menuBar->Append(optionsMenu, _T("&Options")); + menuBar->Append(debugMenu, _T("&Debug")); menuBar->Append(helpMenu, _T("&Help")); - SetMenuBar(menuBar); // - // Create our notebook - using wxNotebook is luckily pretty + // Create our notebook - using wxNotebook is luckily pretty // simple and self-explanatory in most cases // m_notebook = new wxNotebook(this, wxID_NOTEBOOK); @@ -373,14 +553,14 @@ MyFrame::MyFrame(const wxString& title) // // Message Maps are implemented by putting // DECLARE_MESSAGE_MAP in your wxEvtHandler-derived - // class you want to use for events, such as MyFrame. + // class you want to use for events, such as wxMediaPlayerFrame. // // Then after your class declaration you put - // BEGIN_EVENT_TABLE(MyFrame, wxFrame) + // BEGIN_EVENT_TABLE(wxMediaPlayerFrame, wxFrame) // EVT_XXX(XXX)... // END_EVENT_TABLE() // - // Where MyFrame is the class with the DECLARE_MESSAGE_MAP + // Where wxMediaPlayerFrame is the class with the DECLARE_MESSAGE_MAP // in it. EVT_XXX(XXX) are each of your handlers, such // as EVT_MENU for menu events and the XXX inside // is the parameters to the event macro - in the case @@ -411,11 +591,17 @@ MyFrame::MyFrame(const wxString& title) // type, then to a wxEventFunction, then to a // wxObjectEventFunction - I.E. // (wxObjectEventFunction)(wxEventFunction) - // (wxCommandEventFunction) &MyFrame::MyHandler + // (wxCommandEventFunction) &wxMediaPlayerFrame::MyHandler + // + // Or, you can use the new (2.5.5+) event handler + // conversion macros - for instance the above could + // be done as + // wxCommandEventHandler(wxMediaPlayerFrame::MyHandler) + // pretty simple, eh? // // The fourth is an optional userdata param - // this is of historical relevance only and is - // there only for backwards compatability. + // there only for backwards compatibility. // // The fifth is the context in which to call the // handler - by default (this param is optional) @@ -432,85 +618,156 @@ MyFrame::MyFrame(const wxString& title) // Menu events // this->Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnQuit); + wxCommandEventHandler(wxMediaPlayerFrame::OnQuit)); this->Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnAbout); + wxCommandEventHandler(wxMediaPlayerFrame::OnAbout)); this->Connect(wxID_LOOP, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnLoop); + wxCommandEventHandler(wxMediaPlayerFrame::OnLoop)); + + this->Connect(wxID_SHOWINTERFACE, wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(wxMediaPlayerFrame::OnShowInterface)); this->Connect(wxID_OPENFILENEWPAGE, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnOpenFileNewPage); + wxCommandEventHandler(wxMediaPlayerFrame::OnOpenFileNewPage)); this->Connect(wxID_OPENFILESAMEPAGE, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnOpenFileSamePage); + wxCommandEventHandler(wxMediaPlayerFrame::OnOpenFileSamePage)); this->Connect(wxID_OPENURLNEWPAGE, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnOpenURLNewPage); + wxCommandEventHandler(wxMediaPlayerFrame::OnOpenURLNewPage)); this->Connect(wxID_OPENURLSAMEPAGE, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnOpenURLSamePage); + wxCommandEventHandler(wxMediaPlayerFrame::OnOpenURLSamePage)); this->Connect(wxID_CLOSECURRENTPAGE, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnCloseCurrentPage); + wxCommandEventHandler(wxMediaPlayerFrame::OnCloseCurrentPage)); this->Connect(wxID_PLAY, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnPlay); - - this->Connect(wxID_PAUSE, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnPause); + wxCommandEventHandler(wxMediaPlayerFrame::OnPlay)); this->Connect(wxID_STOP, wxEVT_COMMAND_MENU_SELECTED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyFrame::OnStop); + wxCommandEventHandler(wxMediaPlayerFrame::OnStop)); + + this->Connect(wxID_NEXT, wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(wxMediaPlayerFrame::OnNext)); + + this->Connect(wxID_PREV, wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(wxMediaPlayerFrame::OnPrev)); + + this->Connect(wxID_SELECTBACKEND, wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(wxMediaPlayerFrame::OnSelectBackend)); // // Notebook events // this->Connect(wxID_NOTEBOOK, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, - (wxObjectEventFunction) (wxEventFunction) - (wxNotebookEventFunction) &MyFrame::OnPageChange); - + wxNotebookEventHandler(wxMediaPlayerFrame::OnPageChange)); + + // + // Key events + // + wxTheApp->Connect(wxID_ANY, wxEVT_KEY_DOWN, + wxKeyEventHandler(wxMediaPlayerFrame::OnKeyDown), + (wxObject*)0, this); + + // + // Close events + // + this->Connect(wxID_ANY, wxEVT_CLOSE_WINDOW, + wxCloseEventHandler(wxMediaPlayerFrame::OnClose)); + // // End of Events // + // + // Create an initial notebook page so the user has something + // to work with without having to go file->open every time :). + // + m_notebook->AddPage(new wxMediaPlayerNotebookPage(this, m_notebook), + wxT(""), + true); + // // Create a timer to update our status bar // - m_timer = new MyTimer(this); + m_timer = new wxMediaPlayerTimer(this); m_timer->Start(100); } // ---------------------------------------------------------------------------- -// MyFrame Destructor +// wxMediaPlayerFrame Destructor // // 1) Deletes child objects implicitly // 2) Delete our timer explicitly // ---------------------------------------------------------------------------- -MyFrame::~MyFrame() +wxMediaPlayerFrame::~wxMediaPlayerFrame() { delete m_timer; + } // ---------------------------------------------------------------------------- -// MyFrame::ResetStatus +// wxMediaPlayerFrame::OnClose +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::OnClose(wxCloseEvent& event) +{ + // + // Here we save our info to the registry or whatever + // mechanism the OS uses. + // + // This makes it so that when mediaplayer loads up again + // it restores the same files that were in the playlist + // this time, rather than the user manually re-adding them. + // + // We need to do conf->DeleteAll() here because by default + // the config still contains the same files as last time + // so we need to clear it before writing our new ones. + // + // TODO: Maybe you could add a menu option to the + // options menu to delete the configuration on exit - + // all you'd need to do is just remove everything after + // conf->DeleteAll() here + // + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + + wxConfigBase* conf = wxConfigBase::Get(); + conf->DeleteAll(); + + for(int i = 0; i < m_playlist->GetItemCount(); ++i) + { + wxString* pData = (wxString*) m_playlist->GetItemData(i); + wxString s; + s << i; + conf->Write(s, *(pData)); + delete pData; + } + + event.Skip(); //really close the frame +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::AddToPlayList +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::AddToPlayList(const wxString& szString) +{ + wxMediaPlayerNotebookPage* currentpage = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage()); + + currentpage->m_playlist->AddToPlayList(szString); +} + + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::ResetStatus // // Here we just make a simple status string with some useful info about // the media that we won't change later - such as the length of the media. // -// We then append some other info that changes in MyTimer::Notify, then +// We then append some other info that changes in wxMediaPlayerTimer::Notify, then // set the status bar to this text. // // In real applications, you'd want to find a better way to do this, @@ -521,7 +778,7 @@ MyFrame::~MyFrame() // // We also reset our loop counter here. // ---------------------------------------------------------------------------- -void MyFrame::ResetStatus() +void wxMediaPlayerFrame::ResetStatus() { wxMediaCtrl* currentMediaCtrl = GetCurrentMediaCtrl(); @@ -535,48 +792,55 @@ void MyFrame::ResetStatus() } // ---------------------------------------------------------------------------- -// MyFrame::GetCurrentMediaCtrl +// wxMediaPlayerFrame::GetCurrentMediaCtrl // // Obtains the media control of the current page, or NULL if there are no // pages open // ---------------------------------------------------------------------------- -wxMediaCtrl* MyFrame::GetCurrentMediaCtrl() +wxMediaCtrl* wxMediaPlayerFrame::GetCurrentMediaCtrl() { - wxASSERT(m_notebook->GetCurrentPage() != NULL); - return ((MyNotebookPage*)m_notebook->GetCurrentPage())->m_mediactrl; + return ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_mediactrl; } // ---------------------------------------------------------------------------- -// MyFrame::GetCurrentSlider +// wxMediaPlayerFrame::GetCurrentSlider // -// Obtains the slider of the current page, or NULL if there are no -// pages open +// Obtains the slider of the current page +// ---------------------------------------------------------------------------- +wxSlider* wxMediaPlayerFrame::GetCurrentSlider() +{ + return ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_slider; +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::GetCurrentGauge +// +// Obtains the gauge of the current page // ---------------------------------------------------------------------------- -wxSlider* MyFrame::GetCurrentSlider() +wxGauge* wxMediaPlayerFrame::GetCurrentGauge() { - wxASSERT(m_notebook->GetCurrentPage() != NULL); - return ((MyNotebookPage*)m_notebook->GetCurrentPage())->m_slider; + return ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_gauge; } // ---------------------------------------------------------------------------- -// MyFrame::OnQuit +// wxMediaPlayerFrame::OnQuit // // Called from file->quit. // Closes this application. // ---------------------------------------------------------------------------- -void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) +void wxMediaPlayerFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) { // true is to force the frame to close Close(true); } // ---------------------------------------------------------------------------- -// MyFrame::OnAbout +// wxMediaPlayerFrame::OnAbout // // Called from help->about. // Gets some info about this application. // ---------------------------------------------------------------------------- -void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) +void wxMediaPlayerFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { wxString msg; msg.Printf( _T("This is a test of wxMediaCtrl.\n") @@ -586,138 +850,302 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) } // ---------------------------------------------------------------------------- -// MyFrame::OnLoop +// wxMediaPlayerFrame::OnLoop // // Called from file->loop. // Changes the state of whether we want to loop or not. // ---------------------------------------------------------------------------- -void MyFrame::OnLoop(wxCommandEvent& WXUNUSED(event)) +void wxMediaPlayerFrame::OnLoop(wxCommandEvent& WXUNUSED(event)) { if(!m_notebook->GetCurrentPage()) { wxMessageBox(wxT("No files are currently open!")); return; } - GetCurrentMediaCtrl()->Loop( !GetCurrentMediaCtrl()->IsLooped() ); + + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_bLoop = + !((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_bLoop; } // ---------------------------------------------------------------------------- -// MyFrame::OnOpenFileSamePage +// wxMediaPlayerFrame::OnLoop +// +// Called from file->loop. +// Changes the state of whether we want to loop or not. +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::OnShowInterface(wxCommandEvent& event) +{ + if(!m_notebook->GetCurrentPage()) + { + wxMessageBox(wxT("No files are currently open!")); + return; + } + + GetCurrentMediaCtrl()->ShowPlayerControls(event.IsChecked() ? + wxMEDIACTRLPLAYERCONTROLS_DEFAULT : + wxMEDIACTRLPLAYERCONTROLS_NONE); +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::OnOpenFileSamePage // // Called from file->openfile. // Opens and plays a media file in the current notebook page // ---------------------------------------------------------------------------- -void MyFrame::OnOpenFileSamePage(wxCommandEvent& WXUNUSED(event)) -{ - OpenFile(false); +void wxMediaPlayerFrame::OnOpenFileSamePage(wxCommandEvent& WXUNUSED(event)) +{ + OpenFile(false); } // ---------------------------------------------------------------------------- -// MyFrame::OnOpenFileNewPage +// wxMediaPlayerFrame::OnOpenFileNewPage // // Called from file->openfileinnewpage. // Opens and plays a media file in a new notebook page // ---------------------------------------------------------------------------- -void MyFrame::OnOpenFileNewPage(wxCommandEvent& WXUNUSED(event)) -{ - OpenFile(true); +void wxMediaPlayerFrame::OnOpenFileNewPage(wxCommandEvent& WXUNUSED(event)) +{ + OpenFile(true); } // ---------------------------------------------------------------------------- -// MyFrame::OpenFile -// -// Code to actually open the media file +// wxMediaPlayerFrame::OpenFile // -// 1) Create file dialog and ask the user for input file -// 2) If the user didn't want anything, break out -// 3) Create a new page if the user wanted one or there isn't a current page -// 4) Load the media -// 5) Play the media -// 6) Reset the text on the status bar -// 7) Set the slider of the current page to accurately reflect media length +// Opens a file dialog asking the user for a filename, then +// calls DoOpenFile which will add the file to the playlist and play it // ---------------------------------------------------------------------------- -void MyFrame::OpenFile(bool bNewPage) +void wxMediaPlayerFrame::OpenFile(bool bNewPage) { wxFileDialog fd(this); if(fd.ShowModal() == wxID_OK) { - if(bNewPage || !m_notebook->GetCurrentPage()) - m_notebook->AddPage(new MyNotebookPage(m_notebook), fd.GetPath(), true); - - if( !GetCurrentMediaCtrl()->Load(fd.GetPath()) ) + DoOpenFile(fd.GetPath(), bNewPage); + } +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::DoOpenFile +// +// Adds the file to our playlist, selects it in the playlist, +// and then calls DoPlayFile to play it +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::DoOpenFile(const wxString& path, bool bNewPage) +{ + if(bNewPage) + { + m_notebook->AddPage( + new wxMediaPlayerNotebookPage(this, m_notebook), + path, + true); + } + + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + + if(m_nLastFileId != -1) + m_playlist->SetItemState(m_nLastFileId, 0, wxLIST_STATE_SELECTED); + + wxListItem newlistitem; + newlistitem.SetAlign(wxLIST_FORMAT_LEFT); + + int nID; + + newlistitem.SetId(nID = m_playlist->GetItemCount()); + newlistitem.SetMask(wxLIST_MASK_DATA | wxLIST_MASK_STATE); + newlistitem.SetState(wxLIST_STATE_SELECTED); + newlistitem.SetData(new wxString(path)); + + m_playlist->InsertItem(newlistitem); + m_playlist->SetItem(nID, 0, _T("*")); + m_playlist->SetItem(nID, 1, wxFileName(path).GetName()); + + if (nID % 2) + { + newlistitem.SetBackgroundColour(wxColour(192,192,192)); + m_playlist->SetItem(newlistitem); + } + + DoPlayFile(path); + // m_playlist->Focus(nID); +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::DoPlayFile +// +// Pauses the file if its the currently playing file, +// otherwise it plays the file +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::DoPlayFile(const wxString& path) +{ + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + + wxListItem listitem; + m_playlist->GetSelectedItem(listitem); + + if(listitem.GetData() != NULL && + m_szFile.compare(path) == 0 && + m_nLastFileId == listitem.GetId()) + { + if(GetCurrentMediaCtrl()->GetState() == wxMEDIASTATE_PLAYING) + { + if( !GetCurrentMediaCtrl()->Pause() ) + wxMessageBox(wxT("Couldn't pause movie!")); + else + m_playlist->SetItem(listitem.GetId(), 0, _T("||")); + } + else + { + if( !GetCurrentMediaCtrl()->Play() ) + wxMessageBox(wxT("Couldn't pause movie!")); + else + m_playlist->SetItem(listitem.GetId(), 0, _T(">")); + } + } + else + { + m_notebook->SetPageText(m_notebook->GetSelection(), + wxFileName(path).GetName()); + + if(m_nLastFileId != -1) + m_playlist->SetItem(m_nLastFileId, 0, _T("*")); + + wxURI uripath(path); + if( uripath.IsReference() ) + { + if( !GetCurrentMediaCtrl()->Load(path) ) + { + wxMessageBox(wxT("Couldn't load file!")); + m_playlist->SetItem(listitem.GetId(), 0, _T("E")); + } + else + { + m_playlist->SetItem(listitem.GetId(), 0, _T("O")); + } + } + else + { + if( !GetCurrentMediaCtrl()->Load(uripath) ) + { wxMessageBox(wxT("Couldn't load file!")); + m_playlist->SetItem(listitem.GetId(), 0, _T("E")); + } + else + { + m_playlist->SetItem(listitem.GetId(), 0, _T("O")); + } + } + + m_nLastFileId = listitem.GetId(); + m_szFile = path; + m_playlist->SetItem(m_nLastFileId, 1, wxFileName(path).GetName()); + m_playlist->SetItem(m_nLastFileId, 2, wxT("")); + } +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::OnMediaLoaded +// +// Called when the media is ready to be played - and does +// so, also gets the length of media and shows that in the list control +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::OnMediaLoaded(wxMediaEvent& WXUNUSED(evt)) +{ + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + wxListItem listitem; + m_playlist->GetSelectedItem(listitem); if( !GetCurrentMediaCtrl()->Play() ) + { wxMessageBox(wxT("Couldn't play movie!")); + m_playlist->SetItem(listitem.GetId(), 0, _T("E")); + } + else + { + m_playlist->SetItem(listitem.GetId(), 0, _T(">")); + } + + m_playlist->SetItem(listitem.GetId(), 2, wxString::Format(wxT("%u"), + (unsigned) GetCurrentMediaCtrl()->Length() / 1000) ); ResetStatus(); - - GetCurrentSlider()->SetRange(0, + + GetCurrentSlider()->SetRange(0, (int)(GetCurrentMediaCtrl()->Length() / 1000)); + GetCurrentGauge()->SetRange((int)(GetCurrentMediaCtrl()->Length() / 1000)); +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::OnSelectBackend +// +// Little debugging routine - enter the class name of a backend and it +// will use that instead of letting wxMediaCtrl search the wxMediaBackend +// RTTI class list. +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::OnSelectBackend(wxCommandEvent& WXUNUSED(evt)) +{ + wxString sBackend = wxGetTextFromUser(wxT("Enter backend to use")); + if(sBackend.empty() == false) //could have been cancelled by the user + { + int sel = m_notebook->GetSelection(); + + if (sel != wxNOT_FOUND) + { + m_notebook->DeletePage(sel); + } + + m_notebook->AddPage(new wxMediaPlayerNotebookPage(this, m_notebook, + sBackend + ), wxT(""), true); + DoOpenFile(m_szFile, false); } } // ---------------------------------------------------------------------------- -// MyFrame::OnOpenURLSamePage +// wxMediaPlayerFrame::OnOpenURLSamePage // // Called from file->openurl. // Opens and plays a media file from a URL in the current notebook page // ---------------------------------------------------------------------------- -void MyFrame::OnOpenURLSamePage(wxCommandEvent& WXUNUSED(event)) -{ - OpenURL(false); +void wxMediaPlayerFrame::OnOpenURLSamePage(wxCommandEvent& WXUNUSED(event)) +{ + OpenURL(false); } // ---------------------------------------------------------------------------- -// MyFrame::OnOpenURLNewPage +// wxMediaPlayerFrame::OnOpenURLNewPage // // Called from file->openurlinnewpage. // Opens and plays a media file from a URL in a new notebook page // ---------------------------------------------------------------------------- -void MyFrame::OnOpenURLNewPage(wxCommandEvent& WXUNUSED(event)) -{ - OpenURL(true); +void wxMediaPlayerFrame::OnOpenURLNewPage(wxCommandEvent& WXUNUSED(event)) +{ + OpenURL(true); } // ---------------------------------------------------------------------------- -// MyFrame::OpenURL -// -// Code to actually open the media file from a URL +// wxMediaPlayerFrame::OpenURL // -// 1) Create text input dialog and ask the user for an input URL -// 2) If the user didn't want anything, break out -// 3) Create a new page if the user wanted one or there isn't a current page -// 4) Load the media -// 5) Play the media -// 6) Reset the text on the status bar -// 7) Set the slider of the current page to accurately reflect media length +// Just calls DoOpenFile with the url path - which calls DoPlayFile +// which handles the real dirty work // ---------------------------------------------------------------------------- -void MyFrame::OpenURL(bool bNewPage) +void wxMediaPlayerFrame::OpenURL(bool bNewPage) { - wxString theURL = wxGetTextFromUser(wxT("Enter the URL that has the movie to play")); + wxString sUrl = wxGetTextFromUser( + wxT("Enter the URL that has the movie to play") + ); - if(!theURL.empty()) + if(sUrl.empty() == false) //could have been cancelled by user { - if(bNewPage || !m_notebook->GetCurrentPage()) - m_notebook->AddPage(new MyNotebookPage(m_notebook), theURL, true); - - if( !GetCurrentMediaCtrl()->Load(wxURI(theURL)) ) - wxMessageBox(wxT("Couldn't load URL!")); - - if( !GetCurrentMediaCtrl()->Play() ) - wxMessageBox(wxT("Couldn't play movie!")); - - ResetStatus(); - - GetCurrentSlider()->SetRange(0, - (int)(GetCurrentMediaCtrl()->Length() / 1000)); + DoOpenFile(sUrl, bNewPage); } } // ---------------------------------------------------------------------------- -// MyFrame::OnCloseCurrentPage +// wxMediaPlayerFrame::OnCloseCurrentPage // // Called when the user wants to close the current notebook page // @@ -725,54 +1153,98 @@ void MyFrame::OpenURL(bool bNewPage) // 2) If there is no current page, break out // 3) Delete the current page // ---------------------------------------------------------------------------- -void MyFrame::OnCloseCurrentPage(wxCommandEvent& WXUNUSED(event)) +void wxMediaPlayerFrame::OnCloseCurrentPage(wxCommandEvent& WXUNUSED(event)) { + if( m_notebook->GetPageCount() > 1 ) + { int sel = m_notebook->GetSelection(); if (sel != wxNOT_FOUND) { m_notebook->DeletePage(sel); - } + } + } + else + { + wxMessageBox(wxT("Cannot close main page")); + } } // ---------------------------------------------------------------------------- -// MyFrame::OnPlay +// wxMediaPlayerFrame::OnPlay // // Called from file->play. // Resumes the media if it is paused or stopped. // ---------------------------------------------------------------------------- -void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event)) +void wxMediaPlayerFrame::OnPlay(wxCommandEvent& WXUNUSED(event)) { - if(!m_notebook->GetCurrentPage()) + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + + wxListItem listitem; + m_playlist->GetSelectedItem(listitem); + if (listitem.GetData() == NULL) { - wxMessageBox(wxT("No files are currently open!")); + int nLast = -1; + if ((nLast = m_playlist->GetNextItem(nLast, + wxLIST_NEXT_ALL, + wxLIST_STATE_DONTCARE)) == -1) + { + //no items in list + wxMessageBox(_T("No items in playlist!")); return; } - - if( !GetCurrentMediaCtrl()->Play() ) - wxMessageBox(wxT("Couldn't play movie!")); + wxListItem listitem; + listitem.SetId(nLast); + m_playlist->GetItem(listitem); + listitem.SetMask(listitem.GetMask() | wxLIST_MASK_STATE); + listitem.SetState(listitem.GetState() | wxLIST_STATE_SELECTED); + m_playlist->SetItem(listitem); + wxListEvent event; + OnChangeSong(event); + } + else + { + wxListEvent event; + OnChangeSong(event); + } } // ---------------------------------------------------------------------------- -// MyFrame::OnPause +// wxMediaPlayerFrame::OnKeyDown // -// Called from file->pause. -// Pauses the media in-place. +// Deletes all selected files from the playlist if the backspace key is pressed // ---------------------------------------------------------------------------- -void MyFrame::OnPause(wxCommandEvent& WXUNUSED(event)) +void wxMediaPlayerFrame::OnKeyDown(wxKeyEvent& event) { - if(!m_notebook->GetCurrentPage()) + if(event.GetKeyCode() == WXK_BACK/*DELETE*/) { - wxMessageBox(wxT("No files are currently open!")); - return; + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + //delete all selected items + while(true) + { + wxInt32 nSelectedItem = m_playlist->GetNextItem( + -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); + if (nSelectedItem == -1) + break; + + wxListItem listitem; + listitem.SetId(nSelectedItem); + m_playlist->GetItem(listitem); + delete (wxString*) listitem.GetData(); + + m_playlist->DeleteItem(nSelectedItem); + } } - if( !GetCurrentMediaCtrl()->Pause() ) - wxMessageBox(wxT("Couldn't pause movie!")); + //Could be wxGetTextFromUser or something else important + if(event.GetEventObject() != this) + event.Skip(); } // ---------------------------------------------------------------------------- -// MyFrame::OnStop +// wxMediaPlayerFrame::OnStop // // Called from file->stop. // Where it stops depends on whether you can seek in the @@ -780,8 +1252,15 @@ void MyFrame::OnPause(wxCommandEvent& WXUNUSED(event)) // otherwise it will appear to be at the end - but it will start over again // when Play() is called // ---------------------------------------------------------------------------- -void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event)) +void wxMediaPlayerFrame::OnStop(wxCommandEvent& WXUNUSED(evt)) { + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + + wxListItem listitem; + m_playlist->GetSelectedItem(listitem); + m_playlist->SetItem(listitem.GetId(), 0, _T("[]")); + if(!m_notebook->GetCurrentPage()) { wxMessageBox(wxT("No files are currently open!")); @@ -792,84 +1271,342 @@ void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event)) wxMessageBox(wxT("Couldn't stop movie!")); } + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::OnChangeSong +// +// Routine that plays the currently selected file in the playlist. +// Called when the user actives the song from the playlist, +// and from other various places in the sample +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::OnChangeSong(wxListEvent& WXUNUSED(evt)) +{ + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + + wxListItem listitem; + m_playlist->GetSelectedItem(listitem); + DoPlayFile((*((wxString*) listitem.GetData()))); +} + // ---------------------------------------------------------------------------- -// MyFrame::OnCloseCurrentPage +// wxMediaPlayerFrame::OnPrev +// +// Tedious wxListCtrl stuff. Goes to prevous song in list, or if at the +// beginning goes to the last in the list. +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::OnPrev(wxCommandEvent& WXUNUSED(event)) +{ + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + + if (m_playlist->GetItemCount() == 0) + return; + + wxInt32 nLastSelectedItem = -1; + while(true) + { + wxInt32 nSelectedItem = m_playlist->GetNextItem(nLastSelectedItem, + wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); + if (nSelectedItem == -1) + break; + nLastSelectedItem = nSelectedItem; + m_playlist->SetItemState(nSelectedItem, 0, wxLIST_STATE_SELECTED); + } + + if (nLastSelectedItem <= 0) + nLastSelectedItem = m_playlist->GetItemCount() - 1; + else + nLastSelectedItem -= 1; + + wxListItem listitem; + listitem.SetId(nLastSelectedItem); + m_playlist->GetItem(listitem); + listitem.SetMask(listitem.GetMask() | wxLIST_MASK_STATE); + listitem.SetState(listitem.GetState() | wxLIST_STATE_SELECTED); + m_playlist->SetItem(listitem); + + wxListEvent emptyEvent; + OnChangeSong(emptyEvent); +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::OnNext +// +// Tedious wxListCtrl stuff. Goes to next song in list, or if at the +// end goes to the first in the list. +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::OnNext(wxCommandEvent& WXUNUSED(event)) +{ + wxMediaPlayerListCtrl* m_playlist = + ((wxMediaPlayerNotebookPage*)m_notebook->GetCurrentPage())->m_playlist; + + if (m_playlist->GetItemCount() == 0) + return; + + wxInt32 nLastSelectedItem = -1; + while(true) + { + wxInt32 nSelectedItem = m_playlist->GetNextItem(nLastSelectedItem, + wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); + if (nSelectedItem == -1) + break; + nLastSelectedItem = nSelectedItem; + m_playlist->SetItemState(nSelectedItem, 0, wxLIST_STATE_SELECTED); + } + + if (nLastSelectedItem == -1) + nLastSelectedItem = 0; + else + { + if (nLastSelectedItem == m_playlist->GetItemCount() - 1) + nLastSelectedItem = 0; + else + nLastSelectedItem += 1; + } + + wxListItem listitem; + listitem.SetId(nLastSelectedItem); + m_playlist->GetItem(listitem); + listitem.SetMask(listitem.GetMask() | wxLIST_MASK_STATE); + listitem.SetState(listitem.GetState() | wxLIST_STATE_SELECTED); + m_playlist->SetItem(listitem); + + wxListEvent emptyEvent; + OnChangeSong(emptyEvent); +} + + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::OnVolumeDown +// +// Lowers the volume of the media control by 10% +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::OnVolumeDown(wxCommandEvent& WXUNUSED(event)) +{ + double dVolume = GetCurrentMediaCtrl()->GetVolume(); + GetCurrentMediaCtrl()->SetVolume(dVolume < 0.1 ? 0.0 : dVolume - .1); +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::OnVolumeUp +// +// Increases the volume of the media control by 10% +// ---------------------------------------------------------------------------- +void wxMediaPlayerFrame::OnVolumeUp(wxCommandEvent& WXUNUSED(event)) +{ + double dVolume = GetCurrentMediaCtrl()->GetVolume(); + GetCurrentMediaCtrl()->SetVolume(dVolume > 0.9 ? 1.0 : dVolume + .1); +} + +// ---------------------------------------------------------------------------- +// wxMediaPlayerFrame::OnCloseCurrentPage // // Called when the user wants to closes the current notebook page // ---------------------------------------------------------------------------- -void MyFrame::OnPageChange(wxNotebookEvent& WXUNUSED(event)) +void wxMediaPlayerFrame::OnPageChange(wxNotebookEvent& WXUNUSED(event)) { ResetStatus(); } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // -// MyTimer +// wxMediaPlayerTimer // // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // ---------------------------------------------------------------------------- -// MyTimer::Notify +// wxMediaPlayerTimer::Notify // // 1) Update our slider with the position were are in in the media -// 2) Update our status bar with the base text from MyFrame::ResetStatus, +// 2) Update our status bar with the base text from wxMediaPlayerFrame::ResetStatus, // append some non-static (changing) info to it, then set the // status bar text to that result // ---------------------------------------------------------------------------- -void MyTimer::Notify() +void wxMediaPlayerTimer::Notify() { - if (!m_frame->m_notebook->GetCurrentPage()) return; - wxMediaCtrl* m_mediactrl = ((MyNotebookPage*)m_frame->m_notebook->GetCurrentPage())->m_mediactrl; - wxSlider* m_slider = ((MyNotebookPage*)m_frame->m_notebook->GetCurrentPage())->m_slider; - if (!m_mediactrl) return; - - long lPosition = (long)( m_mediactrl->Tell() / 1000 ); - m_slider->SetValue(lPosition); - + if(m_frame->m_notebook->GetCurrentPage()) + { + // get some control pointers from current notebook page + wxMediaCtrl* mediactrl = + ((wxMediaPlayerNotebookPage*)m_frame->m_notebook->GetCurrentPage())->m_mediactrl; + wxSlider* slider = + ((wxMediaPlayerNotebookPage*)m_frame->m_notebook->GetCurrentPage())->m_slider; + wxGauge* gauge = + ((wxMediaPlayerNotebookPage*)m_frame->m_notebook->GetCurrentPage())->m_gauge; + + // if the slider is being dragged then update it with the song position + if(((wxMediaPlayerNotebookPage*)m_frame->m_notebook->GetCurrentPage())->IsBeingDragged() == false) + { + long lPosition = (long)( mediactrl->Tell() / 1000 ); + slider->SetValue(lPosition); + } + + // update guage with value from slider + gauge->SetValue(slider->GetValue()); #if wxUSE_STATUSBAR - m_frame->SetStatusText(wxString::Format( - _T("%s Pos:%u State:%s Loops:%i"), - m_frame->m_basestatus.c_str(), - (unsigned int)lPosition, - wxGetMediaStateText(m_mediactrl->GetState()), - ((MyNotebookPage*)m_frame->m_notebook->GetCurrentPage())->m_nLoops - - ) - ); -#endif - + m_frame->SetStatusText(wxString::Format( + wxT("%s Pos:%u State:%s Loops:%i D/T:[%i]/[%i] V:%i%%"), + m_frame->m_basestatus.c_str(), + slider->GetValue(), + wxGetMediaStateText(mediactrl->GetState()), + ((wxMediaPlayerNotebookPage*)m_frame->m_notebook->GetCurrentPage())->m_nLoops, + (int)mediactrl->GetDownloadProgress(), + (int)mediactrl->GetDownloadTotal(), + (int)(mediactrl->GetVolume() * 100))); +#endif // wxUSE_STATUSBAR + } } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // -// MyNotebookPage +// wxMediaPlayerNotebookPage // // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // ---------------------------------------------------------------------------- -// MyNotebookPage Constructor +// wxMediaPlayerNotebookPage Constructor // // Creates a media control and slider and adds it to this panel, // along with some sizers for positioning // ---------------------------------------------------------------------------- -MyNotebookPage::MyNotebookPage(wxNotebook* theBook) : - wxPanel(theBook, wxID_ANY), m_nLoops(0) +wxMediaPlayerNotebookPage::wxMediaPlayerNotebookPage(wxMediaPlayerFrame* parentFrame, + wxNotebook* theBook, + const wxString& szBackend) : + wxPanel(theBook, wxID_ANY), m_bIsBeingDragged(false), + m_nLoops(0), m_bLoop(false), m_parentFrame(parentFrame) { + + // + // Layout // - // Create and attach the first/main sizer + // [wxMediaCtrl] + // [playlist] + // [5 control buttons] + // [slider] + // [gauge] + // + + // + // Create and attach the sizer // - wxBoxSizer* vertsizer = new wxBoxSizer(wxVERTICAL); - this->SetSizer(vertsizer); + wxFlexGridSizer* sizer = new wxFlexGridSizer(2, 1, 0, 0); + this->SetSizer(sizer); this->SetAutoLayout(true); + sizer->AddGrowableRow(0); + sizer->AddGrowableCol(0); // // Create our media control // - m_mediactrl = new wxMediaCtrl(this, wxID_MEDIACTRL); - vertsizer->Add(m_mediactrl, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5); + m_mediactrl = new wxMediaCtrl(); + + // Make sure creation was successful + bool bOK = m_mediactrl->Create(this, wxID_MEDIACTRL, wxEmptyString, + wxDefaultPosition, wxDefaultSize, 0, +//you could specify a macrod backend here like +//wxMEDIABACKEND_QUICKTIME); + szBackend); +//you could change the cursor here like +// m_mediactrl->SetCursor(wxCURSOR_BLANK); +//note that this may not effect it if SetPlayerControls +//is set to something else than wxMEDIACTRLPLAYERCONTROLS_NONE + wxASSERT_MSG(bOK, wxT("Could not create media control!")); + wxUnusedVar(bOK); + + sizer->Add(m_mediactrl, 0, wxALIGN_CENTER_HORIZONTAL|wxALL|wxEXPAND, 5); + + // + // Create the playlist/listctrl + // + m_playlist = new wxMediaPlayerListCtrl(); + m_playlist->Create(this, wxID_LISTCTRL, wxDefaultPosition, + wxDefaultSize, + wxLC_REPORT //wxLC_LIST + | wxSUNKEN_BORDER); + + // Set the background of our listctrl to white + m_playlist->SetBackgroundColour(wxColour(255,255,255)); + + // The layout of the headers of the listctrl are like + // | | File | Length + // + // Where Column one is a character representing the state the file is in: + // * - not the current file + // E - Error has occured + // > - Currently Playing + // [] - Stopped + // || - Paused + // (( - Volume Down 10% + // )) - Volume Up 10% + // + // Column two is the name of the file + // + // Column three is the length in seconds of the file + m_playlist->InsertColumn(0,_(""), wxLIST_FORMAT_CENTER, 20); + m_playlist->InsertColumn(1,_("File"), wxLIST_FORMAT_LEFT, /*wxLIST_AUTOSIZE_USEHEADER*/305); + m_playlist->InsertColumn(2,_("Length"), wxLIST_FORMAT_CENTER, 75); + + m_playlist->SetDropTarget(new wxPlayListDropTarget(*m_playlist)); + sizer->Add(m_playlist, 0, wxALIGN_CENTER_HORIZONTAL|wxALL|wxEXPAND, 5); + + // + // Here we load the our configuration - + // in our case we load all the files that were left in + // the playlist the last time the user closed our application + // + // TODO: This is probably not the best practice since + // the user will load multiple notebook pages with multiple + // wxMediaCtrl elements. + // + // 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. + // + wxConfigBase* conf = wxConfigBase::Get(); + wxString key, outstring; + for(int i = 0; ; ++i) + { + key.clear(); + key << i; + if(!conf->Read(key, &outstring)) + break; + m_playlist->AddToPlayList(outstring); + } + + // + // Create the control buttons + // TODO/FIXME/HACK: This part about sizers is really a nice hack + // and probably isn't proper + // + wxBoxSizer* horsizer1 = new wxBoxSizer(wxHORIZONTAL); + wxBoxSizer* vertsizer = new wxBoxSizer(wxHORIZONTAL); + + m_prevButton = new wxButton(); + m_playButton = new wxButton(); + m_stopButton = new wxButton(); + m_nextButton = new wxButton(); + m_vdButton = new wxButton(); + m_vuButton = new wxButton(); + + m_prevButton->Create(this, wxID_BUTTONPREV, _T("|<")); + m_playButton->Create(this, wxID_BUTTONPLAY, _T(">")); + m_stopButton->Create(this, wxID_BUTTONSTOP, _T("[]")); + m_nextButton->Create(this, wxID_BUTTONNEXT, _T(">|")); + m_vdButton->Create(this, wxID_BUTTONVD, _T("((")); + m_vuButton->Create(this, wxID_BUTTONVU, _T("))")); + vertsizer->Add(m_prevButton, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); + vertsizer->Add(m_playButton, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); + vertsizer->Add(m_stopButton, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); + vertsizer->Add(m_nextButton, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); + vertsizer->Add(m_vdButton, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); + vertsizer->Add(m_vuButton, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); + horsizer1->Add(vertsizer, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); + sizer->Add(horsizer1, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); + // // Create our slider @@ -879,60 +1616,125 @@ MyNotebookPage::MyNotebookPage(wxNotebook* theBook) : 0, //end wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL ); - vertsizer->Add(m_slider, 0, wxALIGN_CENTER_HORIZONTAL|wxALL|wxEXPAND , 5); + sizer->Add(m_slider, 0, wxALIGN_CENTER_HORIZONTAL|wxALL|wxEXPAND , 5); // - // Create the second sizer which will position things - // vertically - + // Create the gauge // - // -------Menu---------- - // [m_mediactrl] + m_gauge = new wxGauge(); + m_gauge->Create(this, wxID_GAUGE, 0, wxDefaultPosition, wxDefaultSize, + wxGA_HORIZONTAL | wxGA_SMOOTH); + sizer->Add(m_gauge, 0, wxALIGN_CENTER_HORIZONTAL|wxALL|wxEXPAND , 5); + // - // [m_slider] + // ListCtrl events // - wxBoxSizer* horzsizer = new wxBoxSizer(wxHORIZONTAL); - vertsizer->Add(horzsizer, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5); + this->Connect( wxID_LISTCTRL, wxEVT_COMMAND_LIST_ITEM_ACTIVATED, + wxListEventHandler(wxMediaPlayerFrame::OnChangeSong), + (wxObject*)0, parentFrame); // // Slider events // - this->Connect(wxID_SLIDER, wxEVT_COMMAND_SLIDER_UPDATED, - (wxObjectEventFunction) (wxEventFunction) - (wxCommandEventFunction) &MyNotebookPage::OnSeek); + this->Connect(wxID_SLIDER, wxEVT_SCROLL_THUMBTRACK, + wxScrollEventHandler(wxMediaPlayerNotebookPage::OnBeginSeek)); + this->Connect(wxID_SLIDER, wxEVT_SCROLL_THUMBRELEASE, + wxScrollEventHandler(wxMediaPlayerNotebookPage::OnEndSeek)); // // Media Control events // - this->Connect(wxID_MEDIACTRL, wxEVT_MEDIA_STOP, - (wxObjectEventFunction) (wxEventFunction) - (wxMediaEventFunction) &MyNotebookPage::OnMediaStop); + this->Connect(wxID_MEDIACTRL, wxEVT_MEDIA_FINISHED, + wxMediaEventHandler(wxMediaPlayerNotebookPage::OnMediaFinished)); + this->Connect(wxID_MEDIACTRL, wxEVT_MEDIA_LOADED, + wxMediaEventHandler(wxMediaPlayerFrame::OnMediaLoaded), + (wxObject*)0, parentFrame); + + // + // Button events + // + this->Connect( wxID_BUTTONPREV, wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxMediaPlayerFrame::OnPrev), + (wxObject*)0, parentFrame); + this->Connect( wxID_BUTTONPLAY, wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxMediaPlayerFrame::OnPlay), + (wxObject*)0, parentFrame); + this->Connect( wxID_BUTTONSTOP, wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxMediaPlayerFrame::OnStop), + (wxObject*)0, parentFrame); + this->Connect( wxID_BUTTONNEXT, wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxMediaPlayerFrame::OnNext), + (wxObject*)0, parentFrame); + this->Connect( wxID_BUTTONVD, wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxMediaPlayerFrame::OnVolumeDown), + (wxObject*)0, parentFrame); + this->Connect( wxID_BUTTONVU, wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxMediaPlayerFrame::OnVolumeUp), + (wxObject*)0, parentFrame); +} + +// ---------------------------------------------------------------------------- +// MyNotebook::OnBeginSeek +// +// Sets m_bIsBeingDragged to true to stop the timer from changing the position +// of our slider +// ---------------------------------------------------------------------------- +void wxMediaPlayerNotebookPage::OnBeginSeek(wxScrollEvent& WXUNUSED(event)) +{ + m_bIsBeingDragged = true; } // ---------------------------------------------------------------------------- -// MyNotebook::OnSeek +// MyNotebook::OnEndSeek // // Called from file->seek. // Called when the user moves the slider - // seeks to a position within the media +// then sets m_bIsBeingDragged to false to ok the timer to change the position // ---------------------------------------------------------------------------- -void MyNotebookPage::OnSeek(wxCommandEvent& WXUNUSED(event)) +void wxMediaPlayerNotebookPage::OnEndSeek(wxScrollEvent& WXUNUSED(event)) { - if( m_mediactrl->Seek( - m_slider->GetValue() * 1000 + if( m_mediactrl->Seek( + m_slider->GetValue() * 1000 ) == wxInvalidOffset ) wxMessageBox(wxT("Couldn't seek in movie!")); + + m_bIsBeingDragged = false; } // ---------------------------------------------------------------------------- -// MyNotebookPage::OnMediaStop +// wxMediaPlayerNotebookPage::IsBeingDragged // -// Called when the media is about to stop playing. +// Returns true if the user is dragging the slider // ---------------------------------------------------------------------------- -void MyNotebookPage::OnMediaStop(wxMediaEvent& WXUNUSED(event)) +bool wxMediaPlayerNotebookPage::IsBeingDragged() +{ + return m_bIsBeingDragged; +} + +// ---------------------------------------------------------------------------- +// OnMediaFinished +// +// Called when the media stops playing. +// Here we loop it if the user wants to (has been selected from file menu) +// ---------------------------------------------------------------------------- +void wxMediaPlayerNotebookPage::OnMediaFinished(wxMediaEvent& WXUNUSED(event)) { - if(m_mediactrl->IsLooped()) - ++m_nLoops; + if(m_bLoop) + { + if ( !m_mediactrl->Play() ) + { + wxMessageBox(wxT("Couldn't loop movie!")); + m_playlist->SetItem(m_parentFrame->m_nLastFileId, 0, _T("E")); + } + else + ++m_nLoops; + } + else + { + m_playlist->SetItem(m_parentFrame->m_nLastFileId, 0, _T("[]")); + } } //