1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mediactrltest.
3 // Purpose: wxMediaCtrl sample
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // the application icon (under Windows and OS/2 it is in resources and even
38 // though we could still include the XPM here it would be unused)
39 #if !defined(__WXMSW__) && !defined(__WXPM__)
40 // #include "../sample.xpm"
43 #include "wx/mediactrl.h"
44 #include "wx/filedlg.h"
45 #include "wx/slider.h"
52 #error "wxUSE_MEDIACTRL must be enabled to use this sample!"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp
: public wxApp
63 // override base class virtuals
64 // ----------------------------
66 // this one is called on application startup and is a good place for the app
67 // initialization (doing it here and not in the ctor allows to have an error
68 // return: if OnInit() returns false, the application terminates)
69 virtual bool OnInit();
74 // Define a new frame type: this is going to be our main frame
75 class MyFrame
: public wxFrame
79 MyFrame(const wxString
& title
);
82 // event handlers (these functions should _not_ be virtual)
83 void OnQuit(wxCommandEvent
& event
);
84 void OnAbout(wxCommandEvent
& event
);
85 void OnLoop(wxCommandEvent
& event
);
87 void OnOpenFile(wxCommandEvent
& event
);
88 void OnOpenURL(wxCommandEvent
& event
);
90 void OnPlay(wxCommandEvent
& event
);
91 void OnPause(wxCommandEvent
& event
);
92 void OnStop(wxCommandEvent
& event
);
94 void OnSeek(wxCommandEvent
& event
);
96 void OnMediaFinished(wxMediaEvent
& event
);
101 m_basestatus
= wxString::Format(_T("Size(x,y):%i,%i Length(Seconds):%u Speed:%1.1fx"),
102 m_movie
->GetBestSize().x
,
103 m_movie
->GetBestSize().y
,
104 m_movie
->GetDuration() / 1000,
105 m_movie
->GetPlaybackRate()
108 m_slider
->SetRange(0, m_movie
->GetDuration() / 1000);
111 wxMediaCtrl
* m_movie
;
114 class MyTimer
* m_timer
;
115 friend class MyTimer
;
116 wxString m_basestatus
;
120 // any class wishing to process wxWidgets events must use this macro
121 DECLARE_EVENT_TABLE()
124 const wxChar
* wxGetMediaStateText(int nState
)
128 case wxMEDIASTATE_PLAYING
:
129 return wxT("Playing");
130 case wxMEDIASTATE_STOPPED
:
131 return wxT("Stopped");
132 ///case wxMEDIASTATE_PAUSED:
134 return wxT("Paused");
138 class MyTimer
: public wxTimer
141 MyTimer(MyFrame
* frame
) {m_frame
= frame
;}
145 long lPosition
= m_frame
->m_movie
->GetPosition() / 1000;
146 m_frame
->m_slider
->SetValue(lPosition
);
149 m_frame
->SetStatusText(wxString::Format(_T("%s Pos:%u State:%s"),
150 m_frame
->m_basestatus
.c_str(),
152 wxGetMediaStateText(m_frame
->m_movie
->GetState())
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 // IDs for the controls and the menu commands
170 Minimal_Quit
= wxID_EXIT
,
172 // it is important for the id corresponding to the "About" command to have
173 // this standard value as otherwise it won't be handled properly under Mac
174 // (where it is special and put into the "Apple" menu)
175 Minimal_About
= wxID_ABOUT
,
187 // ----------------------------------------------------------------------------
188 // event tables and other macros for wxWidgets
189 // ----------------------------------------------------------------------------
191 // the event tables connect the wxWidgets events with the functions (event
192 // handlers) which process them. It can be also done at run-time, but for the
193 // simple menu events like this the static method is much simpler.
194 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
195 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
196 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
197 EVT_MENU(Minimal_Loop
, MyFrame::OnLoop
)
198 EVT_MENU(Minimal_OpenFile
, MyFrame::OnOpenFile
)
199 EVT_MENU(Minimal_OpenURL
, MyFrame::OnOpenURL
)
200 EVT_MENU(Minimal_Play
, MyFrame::OnPlay
)
201 EVT_MENU(Minimal_Pause
, MyFrame::OnPause
)
202 EVT_MENU(Minimal_Stop
, MyFrame::OnStop
)
203 EVT_SLIDER(Minimal_Slider
, MyFrame::OnSeek
)
204 EVT_MEDIA_FINISHED(Minimal_Media
, MyFrame::OnMediaFinished
)
207 // Create a new application object: this macro will allow wxWidgets to create
208 // the application object during program execution (it's better than using a
209 // static object for many reasons) and also implements the accessor function
210 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
214 // ============================================================================
216 // ============================================================================
218 // ----------------------------------------------------------------------------
219 // the application class
220 // ----------------------------------------------------------------------------
222 // 'Main program' equivalent: the program execution "starts" here
225 // create the main application window
226 MyFrame
*frame
= new MyFrame(_T("Minimal wxWidgets App"));
228 // and show it (the frames, unlike simple controls, are not shown when
229 // created initially)
232 // success: wxApp::OnRun() will be called which will enter the main message
233 // loop and the application will run. If we returned false here, the
234 // application would exit immediately.
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
243 MyFrame::MyFrame(const wxString
& title
)
244 : wxFrame(NULL
, wxID_ANY
, title
), m_timer(NULL
)
246 // set the frame icon
247 // SetIcon(wxICON(sample));
251 wxMenu
*menuFile
= new wxMenu
;
253 // the "About" item should be in the help menu
254 wxMenu
*helpMenu
= new wxMenu
;
255 helpMenu
->Append(Minimal_About
, _T("&About...\tF1"), _T("Show about dialog"));
257 menuFile
->Append(Minimal_OpenFile
, _T("&Open File"), _T("Open a File"));
258 menuFile
->Append(Minimal_OpenURL
, _T("Open &URL"), _T("Open a URL"));
259 menuFile
->AppendSeparator();
260 menuFile
->Append(Minimal_Play
, _T("&Play"), _T("Resume playback"));
261 menuFile
->Append(Minimal_Pause
, _T("P&ause"), _T("Pause playback"));
262 menuFile
->Append(Minimal_Stop
, _T("&Stop"), _T("Stop playback"));
263 menuFile
->AppendSeparator();
264 menuFile
->AppendCheckItem(Minimal_Loop
, _T("&Loop"), _T("Loop Selected Media"));
265 menuFile
->AppendSeparator();
266 menuFile
->Append(Minimal_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
268 // now append the freshly created menu to the menu bar...
269 wxMenuBar
*menuBar
= new wxMenuBar();
270 menuBar
->Append(menuFile
, _T("&File"));
271 menuBar
->Append(helpMenu
, _T("&Help"));
273 // ... and attach this menu bar to the frame
275 #endif // wxUSE_MENUS
277 m_sizer
= new wxBoxSizer(wxVERTICAL
);
278 this->SetSizer(m_sizer
);
279 this->SetAutoLayout(true);
281 // m_sizer->SetSizeHints(this);
282 // m_sizer->Fit(this);
284 m_movie
= new wxMediaCtrl(this, Minimal_Media
, wxT(""));
285 m_sizer
->Add(m_movie
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
287 m_slider
= new wxSlider(this, Minimal_Slider
, 0, //init
290 wxDefaultPosition
, wxDefaultSize
,
292 m_sizer
->Add(m_slider
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
|wxEXPAND
, 5);
294 wxBoxSizer
* horzsizer
= new wxBoxSizer(wxHORIZONTAL
);
295 m_sizer
->Add(horzsizer
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
299 m_timer
= new MyTimer(this);
303 // create a status bar just for fun (by default with 1 pane only)
306 SetStatusText(m_basestatus
);
307 #endif // wxUSE_STATUSBAR
318 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
320 // true is to force the frame to close
324 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
327 msg
.Printf( _T("This is a test of wxMediaCtrl.\n")
328 _T("Welcome to %s"), wxVERSION_STRING
);
330 wxMessageBox(msg
, _T("About wxMediaCtrl test"), wxOK
| wxICON_INFORMATION
, this);
333 void MyFrame::OnLoop(wxCommandEvent
& WXUNUSED(event
))
338 void MyFrame::OnOpenFile(wxCommandEvent
& WXUNUSED(event
))
340 wxFileDialog
fd(this);
342 if(fd
.ShowModal() == wxID_OK
)
344 if( !m_movie
->Load(fd
.GetPath()) )
345 wxMessageBox(wxT("Couldn't load file!"));
347 if( !m_movie
->Play() )
348 wxMessageBox(wxT("Couldn't play movie!"));
354 #include "wx/textdlg.h"
356 void MyFrame::OnOpenURL(wxCommandEvent
& WXUNUSED(event
))
358 wxString theURL
= wxGetTextFromUser(wxT("Enter the URL that has the movie to play"));
362 if( !m_movie
->Load(wxURI(theURL
)) )
363 wxMessageBox(wxT("Couldn't load URL!"));
365 if( !m_movie
->Play() )
366 wxMessageBox(wxT("Couldn't play movie!"));
372 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
374 if( !m_movie
->Play() )
375 wxMessageBox(wxT("Couldn't play movie!"));
378 void MyFrame::OnPause(wxCommandEvent
& WXUNUSED(event
))
380 if( !m_movie
->Pause() )
381 wxMessageBox(wxT("Couldn't pause movie!"));
384 void MyFrame::OnStop(wxCommandEvent
& WXUNUSED(event
))
386 if( !m_movie
->Stop() )
387 wxMessageBox(wxT("Couldn't stop movie!"));
390 void MyFrame::OnSeek(wxCommandEvent
& WXUNUSED(event
))
392 if( !m_movie
->SetPosition( m_slider
->GetValue() * 1000 ) )
393 wxMessageBox(wxT("Couldn't seek in movie!"));
396 void MyFrame::OnMediaFinished(wxMediaEvent
& WXUNUSED(event
))
400 if ( !m_movie
->SetPosition(0) || !m_movie
->Play() )
401 wxMessageBox(wxT("Couldn't seek or play to loop movie!"));