]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/board/mmboard.cpp
Major changes in wxVidXANIM (support for output filtering)
[wxWidgets.git] / utils / wxMMedia2 / board / mmboard.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mmboard.cpp
3 // Purpose: Multimedia Library sample
4 // Author: Guilhem Lavaux (created from minimal by J. Smart)
5 // Modified by:
6 // Created: 13/02/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19 #ifdef __GNUG__
20 #pragma implementation "mmboard.cpp"
21 #endif
22
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 // for all others, include the necessary headers (this file is usually all you
31 // need because it includes almost all "standard" wxWindows headers
32 #ifndef WX_PRECOMP
33 #include "wx/wx.h"
34 #endif
35
36 // ----------------------------------------------------------------------------
37 // ressources
38 // ----------------------------------------------------------------------------
39 // the application icon
40 #if defined(__WXGTK__) || defined(__WXMOTIF__)
41 #include "mondrian.xpm"
42 #endif
43
44 // include multimedia classes
45 #include "sndbase.h"
46 #ifdef __WIN32__
47 #include "sndwin.h"
48 #endif
49 #ifdef __UNIX__
50 #include "sndoss.h"
51 #include "sndesd.h"
52 #endif
53
54 #include "wx/statline.h"
55 #include "wx/stattext.h"
56
57 // include personnal classes
58 #include "mmboard.h"
59 #include "mmbman.h"
60
61 #include "play.xpm"
62 #include "stop.xpm"
63 #include "eject.xpm"
64 #include "pause.xpm"
65
66 // ----------------------------------------------------------------------------
67 // private classes
68 // ----------------------------------------------------------------------------
69
70 // Main Multimedia Board frame
71 class MMBoardFrame : public wxFrame
72 {
73 public:
74 // ctor(s)
75 MMBoardFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
76 // dtor
77 ~MMBoardFrame();
78
79 // event handlers
80 void OnQuit(wxCommandEvent& event);
81 void OnAbout(wxCommandEvent& event);
82 void OnOpen(wxCommandEvent& event);
83 void OnPlay(wxCommandEvent& event);
84 void OnStop(wxCommandEvent& event);
85 void OnPause(wxCommandEvent& event);
86 void OnRefreshInfo(wxEvent& event);
87
88 void OpenVideoWindow();
89 void CloseVideoWindow();
90
91 private:
92 // any class wishing to process wxWindows events must use this macro
93 DECLARE_EVENT_TABLE()
94
95 private:
96 void UpdateMMedInfo();
97 void UpdateInfoText();
98
99 MMBoardFile *m_opened_file;
100
101 wxSlider *m_positionSlider;
102 wxBitmapButton *m_playButton, *m_pauseButton, *m_stopButton, *m_ejectButton;
103 wxStaticText *m_fileType, *m_infoText;
104 wxWindow *m_video_window;
105
106 wxPanel *m_panel;
107 wxSizer *m_sizer;
108
109 wxTimer *m_refreshTimer;
110
111 };
112
113 // ----------------------------------------------------------------------------
114 // constants
115 // ----------------------------------------------------------------------------
116
117 // IDs for the controls and the menu commands
118 enum
119 {
120 // menu items
121 MMBoard_Quit = 1,
122 MMBoard_Open,
123 MMBoard_About,
124 MMBoard_PositionSlider,
125 MMBoard_PlayButton,
126 MMBoard_PauseButton,
127 MMBoard_ResumeButton,
128 MMBoard_StopButton,
129 MMBoard_EjectButton,
130 MMBoard_RefreshInfo
131 };
132
133 // ----------------------------------------------------------------------------
134 // event tables and other macros for wxWindows
135 // ----------------------------------------------------------------------------
136
137 BEGIN_EVENT_TABLE(MMBoardFrame, wxFrame)
138 EVT_MENU(MMBoard_Quit, MMBoardFrame::OnQuit)
139 EVT_MENU(MMBoard_About, MMBoardFrame::OnAbout)
140 EVT_MENU(MMBoard_Open, MMBoardFrame::OnOpen)
141 EVT_BUTTON(MMBoard_PlayButton, MMBoardFrame::OnPlay)
142 EVT_BUTTON(MMBoard_StopButton, MMBoardFrame::OnStop)
143 EVT_BUTTON(MMBoard_PauseButton, MMBoardFrame::OnPause)
144 EVT_CUSTOM(wxEVT_TIMER, MMBoard_RefreshInfo, MMBoardFrame::OnRefreshInfo)
145 END_EVENT_TABLE()
146
147 // ---------------------------------------------------------------------------
148 // Main board application launcher
149 // ---------------------------------------------------------------------------
150
151 IMPLEMENT_APP(MMBoardApp)
152
153 // ============================================================================
154 // implementation
155 // ============================================================================
156
157 // ----------------------------------------------------------------------------
158 // the application class
159 // ----------------------------------------------------------------------------
160
161 bool MMBoardApp::OnInit()
162 {
163 // create the main application window
164 MMBoardFrame *frame = new MMBoardFrame("Multimedia Board",
165 wxPoint(50, 50), wxSize(450, 340));
166
167 // and show it (the frames, unlike simple controls, are not shown when
168 // created initially)
169 frame->Show(TRUE);
170
171 m_caps = TestMultimediaCaps();
172
173 if (!m_caps) {
174 wxMessageBox("Your system has no multimedia capabilities. We are exiting now.", "Major error !", wxOK | wxICON_ERROR, NULL);
175 return FALSE;
176 }
177
178 wxString msg;
179 msg.Printf("Detected : %s%s%s", (m_caps & MM_SOUND_OSS) ? "OSS " : "",
180 (m_caps & MM_SOUND_ESD) ? "ESD " : "",
181 (m_caps & MM_SOUND_WIN) ? "WIN" : "");
182
183 wxMessageBox(msg, "Good !", wxOK | wxICON_INFORMATION, NULL);
184
185 // success: wxApp::OnRun() will be called which will enter the main message
186 // loop and the application will run. If we returned FALSE here, the
187 // application would exit immediately.
188 return TRUE;
189 }
190
191 wxUint8 MMBoardApp::TestMultimediaCaps()
192 {
193 wxSoundStream *dev;
194 wxUint8 caps;
195
196 caps = 0;
197
198 #ifdef __UNIX__
199 // We now test the ESD support
200
201 dev = new wxSoundStreamESD();
202 if (dev->GetError() == wxSOUND_NOERR)
203 caps |= MM_SOUND_ESD;
204 delete dev;
205
206 // We test the OSS (Open Sound System) support.
207
208 #if 0
209 dev = new wxSoundStreamOSS();
210 if (dev->GetError() == wxSOUND_NOERR)
211 caps |= MM_SOUND_OSS;
212 delete dev;
213 #endif
214 #endif
215
216 #ifdef __WIN32__
217 // We test the Windows sound support.
218
219 dev = new wxSoundStreamWin();
220 if (dev->GetError() == wxSOUND_NOERR)
221 caps |= MM_SOUND_WIN;
222 delete dev;
223 #endif
224
225 return caps;
226 }
227
228 // ----------------------------------------------------------------------------
229 // main frame
230 // ----------------------------------------------------------------------------
231
232 // frame constructor
233 MMBoardFrame::MMBoardFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
234 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
235 {
236 #ifdef __WXMAC__
237 // we need this in order to allow the about menu relocation, since ABOUT is
238 // not the default id of the about menu
239 wxApp::s_macAboutMenuItemId = MMBoard_About;
240 #endif
241
242 // set the frame icon
243 SetIcon(wxICON(mondrian));
244
245 // create a menu bar
246 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
247
248 // the "About" item should be in the help menu
249 wxMenu *helpMenu = new wxMenu;
250 helpMenu->Append(MMBoard_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
251
252 menuFile->Append(MMBoard_Open, _T("&Open\tAlt-O"), _T("Open file"));
253 menuFile->AppendSeparator();
254 menuFile->Append(MMBoard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
255
256 // now append the freshly created menu to the menu bar...
257 wxMenuBar *menuBar = new wxMenuBar();
258 menuBar->Append(menuFile, _T("&File"));
259 menuBar->Append(helpMenu, _T("&Help"));
260
261 // ... and attach this menu bar to the frame
262 SetMenuBar(menuBar);
263
264 #if wxUSE_STATUSBAR
265 // create a status bar just for fun (by default with 1 pane only)
266 CreateStatusBar(3);
267 SetStatusText(_T("Welcome to wxWindows!"));
268 #endif // wxUSE_STATUSBAR
269
270 // Misc variables
271 m_opened_file = NULL;
272
273 m_panel = new wxPanel(this, -1);
274
275 // Initialize main slider
276 m_positionSlider = new wxSlider( m_panel, MMBoard_PositionSlider, 0, 0, 60,
277 wxDefaultPosition, wxSize(300, -1),
278 wxSL_HORIZONTAL | wxSL_AUTOTICKS);
279 m_positionSlider->SetPageSize(60); // 60 secs
280
281 // Initialize info panel
282 wxPanel *infoPanel = new wxPanel( m_panel, -1);
283 infoPanel->SetBackgroundColour(*wxBLACK);
284 infoPanel->SetForegroundColour(*wxWHITE);
285
286 wxBoxSizer *infoSizer = new wxBoxSizer(wxVERTICAL);
287
288 m_fileType = new wxStaticText(infoPanel, -1, _T(""));
289 wxStaticLine *line = new wxStaticLine(infoPanel, -1);
290 m_infoText = new wxStaticText(infoPanel, -1, "");
291
292 UpdateInfoText();
293
294 infoSizer->Add(m_fileType, 0, wxGROW | wxALL, 1);
295 infoSizer->Add(line, 0, wxGROW | wxCENTRE, 20);
296 infoSizer->Add(m_infoText, 0, wxGROW | wxALL, 1);
297
298 infoPanel->SetSizer(infoSizer);
299 infoPanel->SetAutoLayout(TRUE);
300
301 // Bitmap button panel
302 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
303
304 wxBitmap *play_bmp = new wxBitmap(play_back_xpm);
305 wxBitmap *stop_bmp = new wxBitmap(stop_back_xpm);
306 wxBitmap *eject_bmp = new wxBitmap(eject_xpm);
307 wxBitmap *pause_bmp = new wxBitmap(pause_xpm);
308
309 m_playButton = new wxBitmapButton(m_panel, MMBoard_PlayButton, *play_bmp);
310 m_playButton->Enable(FALSE);
311 m_pauseButton = new wxBitmapButton(m_panel, MMBoard_PauseButton, *pause_bmp);
312 m_pauseButton->Enable(FALSE);
313 m_stopButton = new wxBitmapButton(m_panel, MMBoard_StopButton, *stop_bmp);
314 m_stopButton->Enable(FALSE);
315 m_ejectButton = new wxBitmapButton(m_panel, MMBoard_EjectButton, *eject_bmp);
316 m_ejectButton->Enable(FALSE);
317
318 buttonSizer->Add(m_playButton, 0, wxALL, 2);
319 buttonSizer->Add(m_pauseButton, 0, wxALL, 2);
320 buttonSizer->Add(m_stopButton, 0, wxALL, 2);
321 buttonSizer->Add(m_ejectButton, 0, wxALL, 2);
322
323 // Top sizer
324 m_sizer = new wxBoxSizer(wxVERTICAL);
325 m_sizer->Add(new wxStaticLine(m_panel, -1), 0, wxGROW | wxCENTRE, 0);
326 m_sizer->Add(m_positionSlider, 0, wxCENTRE | wxGROW | wxALL, 2);
327 m_sizer->Add(new wxStaticLine(m_panel, -1), 0, wxGROW | wxCENTRE, 0);
328 m_sizer->Add(buttonSizer, 0, wxALL, 0);
329 m_sizer->Add(new wxStaticLine(m_panel, -1), 0, wxGROW | wxCENTRE, 0);
330 m_sizer->Add(infoPanel, 1, wxCENTRE | wxGROW, 0);
331
332 m_panel->SetSizer(m_sizer);
333 m_panel->SetAutoLayout(TRUE);
334 m_sizer->Fit(this);
335 m_sizer->SetSizeHints(this);
336
337 // Timer
338 m_refreshTimer = new wxTimer(this, MMBoard_RefreshInfo);
339
340 // Video window
341 m_video_window = NULL;
342
343 // Multimedia file
344 m_opened_file = NULL;
345 }
346
347 MMBoardFrame::~MMBoardFrame()
348 {
349 if (m_opened_file)
350 delete m_opened_file;
351
352 delete m_refreshTimer;
353 }
354
355 void MMBoardFrame::OpenVideoWindow()
356 {
357 if (m_video_window)
358 return;
359
360 m_video_window = new wxWindow(m_panel, -1, wxDefaultPosition, wxSize(200, 200));
361 m_video_window->SetBackgroundColour(*wxBLACK);
362 m_sizer->Prepend(m_video_window, 2, wxGROW | wxSHRINK | wxCENTRE, 1);
363
364 m_sizer->Fit(this);
365 }
366
367 void MMBoardFrame::CloseVideoWindow()
368 {
369 if (!m_video_window)
370 return;
371
372 m_sizer->Remove(m_video_window);
373 delete m_video_window;
374 m_video_window = NULL;
375
376 m_sizer->Fit(this);
377 }
378
379 // event handlers
380
381 void MMBoardFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
382 {
383 // TRUE is to force the frame to close
384 Close(TRUE);
385 }
386
387 void MMBoardFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
388 {
389 wxString msg;
390 msg.Printf( _T("wxWindows Multimedia board v1.0a, wxMMedia v2.0a:\n")
391 _T("an example of the capabilities of the wxWindows multimedia classes.\n")
392 _T("Copyright 1999, 2000, Guilhem Lavaux.\n"));
393
394 wxMessageBox(msg, "About MMBoard", wxOK | wxICON_INFORMATION, this);
395 }
396
397 void MMBoardFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
398 {
399 wxString selected_file;
400
401 if (m_opened_file) {
402 if (!m_opened_file->IsStopped()) {
403 wxCommandEvent event2;
404 OnStop(event2);
405 }
406 delete m_opened_file;
407 }
408
409 // select a file to be opened
410 selected_file = wxLoadFileSelector("multimedia", "*", NULL, this);
411 if (selected_file.IsNull())
412 return;
413
414 m_opened_file = MMBoardManager::Open(selected_file);
415
416 // Change the range values of the slider.
417 MMBoardTime length;
418
419 length = m_opened_file->GetLength();
420 m_positionSlider->SetRange(0, length.hours * 3600 + length.minutes * 60 + length.seconds);
421
422 // Update misc info
423 UpdateMMedInfo();
424
425 SetStatusText(selected_file, 2);
426
427 // Update info text
428 UpdateInfoText();
429
430 // Enable a few buttons
431 m_playButton->Enable(TRUE);
432 m_ejectButton->Enable(TRUE);
433
434 if (m_opened_file->NeedWindow()) {
435 OpenVideoWindow();
436 m_opened_file->SetWindow(m_video_window);
437 } else
438 CloseVideoWindow();
439 }
440
441 void MMBoardFrame::UpdateInfoText()
442 {
443 wxString infotext1, infotext2;
444
445 if (m_opened_file) {
446 infotext1 = _T("File type:\n\t");
447 infotext1 += m_opened_file->GetStringType() + _T("\n");
448
449 infotext2 = _T("File informations:\n\n");
450 infotext2 += m_opened_file->GetStringInformation();
451 } else {
452 infotext1 = _T("File type: \n\tNo file opened");
453 infotext2 = _T("File informations:\nNo information\n\n\n\n\n");
454 }
455
456 m_fileType->SetLabel(infotext1);
457 m_infoText->SetLabel(infotext2);
458 }
459
460 void MMBoardFrame::UpdateMMedInfo()
461 {
462 wxString temp_string;
463 MMBoardTime current, length;
464
465 if (m_opened_file) {
466 current = m_opened_file->GetPosition();
467 length = m_opened_file->GetLength();
468 }
469
470 // We refresh the status bar
471 temp_string.Printf("%02d:%02d / %02d:%02d", current.hours * 60 + current.minutes,
472 current.seconds, length.hours * 60 + length.minutes, length.seconds);
473 SetStatusText(temp_string, 1);
474
475 // We set the slider position
476 m_positionSlider->SetValue(current.hours * 3600 + current.minutes * 60 + current.seconds);
477 }
478
479 // ----------------------------------------------------------------------------
480 // Playing management, refreshers, ...
481
482 void MMBoardFrame::OnRefreshInfo(wxEvent& WXUNUSED(event))
483 {
484 UpdateMMedInfo();
485
486 if (m_opened_file->IsStopped()) {
487 m_refreshTimer->Stop();
488 m_playButton->Enable(TRUE);
489 m_stopButton->Enable(FALSE);
490 m_pauseButton->Enable(FALSE);
491 }
492 }
493
494 void MMBoardFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
495 {
496 m_stopButton->Enable(TRUE);
497 m_pauseButton->Enable(TRUE);
498 m_playButton->Enable(FALSE);
499
500 if (m_opened_file->IsPaused()) {
501 m_opened_file->Resume();
502 return;
503 }
504
505 m_refreshTimer->Start(1000, FALSE);
506
507 m_opened_file->Play();
508
509 m_stopButton->Enable(TRUE);
510 m_pauseButton->Enable(TRUE);
511 m_playButton->Enable(FALSE);
512 }
513
514 void MMBoardFrame::OnStop(wxCommandEvent& WXUNUSED(event))
515 {
516 m_opened_file->Stop();
517 m_refreshTimer->Stop();
518
519 m_stopButton->Enable(FALSE);
520 m_playButton->Enable(TRUE);
521
522 UpdateMMedInfo();
523 }
524
525 void MMBoardFrame::OnPause(wxCommandEvent& WXUNUSED(event))
526 {
527 m_opened_file->Pause();
528
529 m_playButton->Enable(TRUE);
530 m_pauseButton->Enable(FALSE);
531 }