]>
git.saurik.com Git - wxWidgets.git/blob - contrib/samples/mmedia/mmboard.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Multimedia Library sample
4 // Author: Guilhem Lavaux (created from minimal by J. Smart)
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "mmboard.cpp"
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include "wx/wxprec.h"
30 // for all others, include the necessary headers (this file is usually all you
31 // need because it includes almost all "standard" wxWidgets headers
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
39 // the application icon
40 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
41 #include "mondrian.xpm"
44 // include multimedia classes
45 #include "wx/mmedia/sndbase.h"
47 #include "wx/mmedia/sndwin.h"
50 #include "wx/mmedia/sndoss.h"
51 #include "wx/mmedia/sndesd.h"
54 #include "wx/statline.h"
55 #include "wx/stattext.h"
57 // include personnal classes
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // Main Multimedia Board frame
71 class MMBoardFrame
: public wxFrame
75 MMBoardFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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 OnEject(wxCommandEvent
& event
);
87 void OnRefreshInfo(wxEvent
& event
);
88 void OnSetPosition(wxCommandEvent
& event
);
90 void OpenVideoWindow();
91 void CloseVideoWindow();
94 // any class wishing to process wxWidgets events must use this macro
98 void UpdateMMedInfo();
99 void UpdateInfoText();
101 MMBoardFile
*m_opened_file
;
103 wxSlider
*m_positionSlider
;
104 wxBitmapButton
*m_playButton
, *m_pauseButton
, *m_stopButton
, *m_ejectButton
;
105 wxStaticText
*m_fileType
, *m_infoText
;
106 wxWindow
*m_video_window
;
111 wxTimer
*m_refreshTimer
;
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // IDs for the controls and the menu commands
125 MMBoard_PositionSlider
,
128 MMBoard_ResumeButton
,
134 // ----------------------------------------------------------------------------
135 // event tables and other macros for wxWidgets
136 // ----------------------------------------------------------------------------
138 BEGIN_EVENT_TABLE(MMBoardFrame
, wxFrame
)
139 EVT_MENU(MMBoard_Quit
, MMBoardFrame::OnQuit
)
140 EVT_MENU(MMBoard_About
, MMBoardFrame::OnAbout
)
141 EVT_MENU(MMBoard_Open
, MMBoardFrame::OnOpen
)
142 EVT_BUTTON(MMBoard_PlayButton
, MMBoardFrame::OnPlay
)
143 EVT_BUTTON(MMBoard_StopButton
, MMBoardFrame::OnStop
)
144 EVT_BUTTON(MMBoard_PauseButton
, MMBoardFrame::OnPause
)
145 EVT_BUTTON(MMBoard_EjectButton
, MMBoardFrame::OnEject
)
146 EVT_SLIDER(MMBoard_PositionSlider
, MMBoardFrame::OnSetPosition
)
147 EVT_CUSTOM(wxEVT_TIMER
, MMBoard_RefreshInfo
, MMBoardFrame::OnRefreshInfo
)
150 // ---------------------------------------------------------------------------
151 // Main board application launcher
152 // ---------------------------------------------------------------------------
154 IMPLEMENT_APP(MMBoardApp
)
156 // ============================================================================
158 // ============================================================================
160 // ----------------------------------------------------------------------------
161 // the application class
162 // ----------------------------------------------------------------------------
164 bool MMBoardApp::OnInit()
166 // create the main application window
167 MMBoardFrame
*frame
= new MMBoardFrame(_T("Multimedia Board"),
168 wxPoint(50, 50), wxSize(450, 340));
170 // and show it (the frames, unlike simple controls, are not shown when
171 // created initially)
174 m_caps
= TestMultimediaCaps();
177 wxMessageBox(_T("Your system has no multimedia capabilities. We are exiting now."), _T("Major error !"), wxOK
| wxICON_ERROR
, NULL
);
182 msg
.Printf(_T("Detected : %s%s%s"), (m_caps
& MM_SOUND_OSS
) ? _T("OSS ") : _T(""),
183 (m_caps
& MM_SOUND_ESD
) ? _T("ESD ") : _T(""),
184 (m_caps
& MM_SOUND_WIN
) ? _T("WIN") : _T(""));
186 wxMessageBox(msg
, _T("Good !"), wxOK
| wxICON_INFORMATION
, NULL
);
188 // success: wxApp::OnRun() will be called which will enter the main message
189 // loop and the application will run. If we returned false here, the
190 // application would exit immediately.
194 wxUint8
MMBoardApp::TestMultimediaCaps()
202 // We now test the ESD support
204 dev
= new wxSoundStreamESD();
205 if (dev
->GetError() == wxSOUND_NOERROR
)
206 caps
|= MM_SOUND_ESD
;
209 // We test the OSS (Open Sound System) support.
210 // WARNING: There is a conflict between ESD and ALSA. We may be interrested
211 // in disabling the auto detection of OSS is ESD has been detected.
213 if (!(caps
& MM_SOUND_ESD
)) {
216 dev
= new wxSoundStreamOSS();
217 if (dev
->GetError() == wxSOUND_NOERROR
)
218 caps
|= MM_SOUND_OSS
;
227 // We test the Windows sound support.
229 dev
= new wxSoundStreamWin();
230 if (dev
->GetError() == wxSOUND_NOERROR
)
231 caps
|= MM_SOUND_WIN
;
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
243 MMBoardFrame::MMBoardFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
244 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
247 // we need this in order to allow the about menu relocation, since ABOUT is
248 // not the default id of the about menu
249 wxApp::s_macAboutMenuItemId
= MMBoard_About
;
252 // set the frame icon
253 SetIcon(wxICON(mondrian
));
256 wxMenu
*menuFile
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
258 // the "About" item should be in the help menu
259 wxMenu
*helpMenu
= new wxMenu
;
260 helpMenu
->Append(MMBoard_About
, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
262 menuFile
->Append(MMBoard_Open
, wxT("&Open\tAlt-O"), wxT("Open file"));
263 menuFile
->AppendSeparator();
264 menuFile
->Append(MMBoard_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
266 // now append the freshly created menu to the menu bar...
267 wxMenuBar
*menuBar
= new wxMenuBar();
268 menuBar
->Append(menuFile
, wxT("&File"));
269 menuBar
->Append(helpMenu
, wxT("&Help"));
271 // ... and attach this menu bar to the frame
275 // create a status bar just for fun (by default with 1 pane only)
277 SetStatusText(wxT("Welcome to wxWidgets!"));
278 #endif // wxUSE_STATUSBAR
281 m_opened_file
= NULL
;
283 m_panel
= new wxPanel(this, wxID_ANY
);
285 // Initialize main slider
286 m_positionSlider
= new wxSlider( m_panel
, MMBoard_PositionSlider
, 0, 0, 60,
287 wxDefaultPosition
, wxSize(300, wxDefaultCoord
),
288 wxSL_HORIZONTAL
| wxSL_AUTOTICKS
);
289 m_positionSlider
->SetPageSize(60); // 60 secs
290 m_positionSlider
->Disable();
292 // Initialize info panel
293 wxPanel
*infoPanel
= new wxPanel( m_panel
, wxID_ANY
);
294 infoPanel
->SetBackgroundColour(*wxBLACK
);
295 infoPanel
->SetForegroundColour(*wxWHITE
);
297 wxBoxSizer
*infoSizer
= new wxBoxSizer(wxVERTICAL
);
299 m_fileType
= new wxStaticText(infoPanel
, wxID_ANY
, wxEmptyString
);
301 wxStaticLine
*line
= new wxStaticLine(infoPanel
, wxID_ANY
);
302 #endif // wxUSE_STATLINE
303 m_infoText
= new wxStaticText(infoPanel
, wxID_ANY
, wxEmptyString
);
307 infoSizer
->Add(m_fileType
, 0, wxGROW
| wxALL
, 1);
309 infoSizer
->Add(line
, 0, wxGROW
| wxCENTRE
, 20);
310 #endif // wxUSE_STATLINE
311 infoSizer
->Add(m_infoText
, 0, wxGROW
| wxALL
, 1);
313 infoPanel
->SetSizer(infoSizer
);
315 // Bitmap button panel
316 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
318 wxBitmap
play_bmp(play_back_xpm
);
319 wxBitmap
stop_bmp(stop_back_xpm
);
320 wxBitmap
eject_bmp(eject_xpm
);
321 wxBitmap
pause_bmp(pause_xpm
);
323 m_playButton
= new wxBitmapButton(m_panel
, MMBoard_PlayButton
, play_bmp
);
324 m_playButton
->Disable();
325 m_pauseButton
= new wxBitmapButton(m_panel
, MMBoard_PauseButton
, pause_bmp
);
326 m_pauseButton
->Disable();
327 m_stopButton
= new wxBitmapButton(m_panel
, MMBoard_StopButton
, stop_bmp
);
328 m_stopButton
->Disable();
329 m_ejectButton
= new wxBitmapButton(m_panel
, MMBoard_EjectButton
, eject_bmp
);
330 m_ejectButton
->Disable();
332 buttonSizer
->Add(m_playButton
, 0, wxALL
, 2);
333 buttonSizer
->Add(m_pauseButton
, 0, wxALL
, 2);
334 buttonSizer
->Add(m_stopButton
, 0, wxALL
, 2);
335 buttonSizer
->Add(m_ejectButton
, 0, wxALL
, 2);
338 m_sizer
= new wxBoxSizer(wxVERTICAL
);
340 m_sizer
->Add(new wxStaticLine(m_panel
, wxID_ANY
), 0, wxGROW
| wxCENTRE
, 0);
341 #endif // wxUSE_STATLINE
342 m_sizer
->Add(m_positionSlider
, 0, wxCENTRE
| wxGROW
| wxALL
, 2);
344 m_sizer
->Add(new wxStaticLine(m_panel
, wxID_ANY
), 0, wxGROW
| wxCENTRE
, 0);
345 #endif // wxUSE_STATLINE
346 m_sizer
->Add(buttonSizer
, 0, wxALL
, 0);
348 m_sizer
->Add(new wxStaticLine(m_panel
, wxID_ANY
), 0, wxGROW
| wxCENTRE
, 0);
349 #endif // wxUSE_STATLINE
350 m_sizer
->Add(infoPanel
, 1, wxCENTRE
| wxGROW
, 0);
352 m_panel
->SetSizer(m_sizer
);
354 m_sizer
->SetSizeHints(this);
357 m_refreshTimer
= new wxTimer(this, MMBoard_RefreshInfo
);
360 m_video_window
= NULL
;
363 m_opened_file
= NULL
;
366 MMBoardFrame::~MMBoardFrame()
369 delete m_opened_file
;
371 delete m_refreshTimer
;
374 void MMBoardFrame::OpenVideoWindow()
379 m_video_window
= new wxWindow(m_panel
, wxID_ANY
, wxDefaultPosition
, wxSize(200, 200));
380 m_video_window
->SetBackgroundColour(*wxBLACK
);
381 m_sizer
->Prepend(m_video_window
, 2, wxGROW
| wxSHRINK
| wxCENTRE
, 1);
386 void MMBoardFrame::CloseVideoWindow()
391 m_sizer
->Detach( m_video_window
);
392 delete m_video_window
;
393 m_video_window
= NULL
;
400 void MMBoardFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
402 // true is to force the frame to close
406 void MMBoardFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
409 msg
.Printf( wxT("wxWidgets Multimedia board v1.0a, wxMMedia v2.0a:\n")
410 wxT("an example of the capabilities of the wxWidgets multimedia classes.\n")
411 wxT("Copyright 1999, 2000, Guilhem Lavaux.\n"));
413 wxMessageBox(msg
, _T("About MMBoard"), wxOK
| wxICON_INFORMATION
, this);
416 void MMBoardFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
418 wxString selected_file
;
421 if (!m_opened_file
->IsStopped()) {
422 wxCommandEvent event2
;
425 delete m_opened_file
;
428 // select a file to be opened
429 selected_file
= wxLoadFileSelector(_T("multimedia"), _T("*"), NULL
, this);
430 if (selected_file
.IsNull())
433 m_opened_file
= MMBoardManager::Open(selected_file
);
435 // Change the range values of the slider.
438 length
= m_opened_file
->GetLength();
439 m_positionSlider
->SetRange(0, length
.hours
* 3600 + length
.minutes
* 60 + length
.seconds
);
445 SetStatusText(selected_file
, 2);
446 #endif // wxUSE_STATUSBAR
451 // Enable a few buttons
452 m_playButton
->Enable();
453 m_ejectButton
->Enable();
454 m_positionSlider
->Enable();
456 if (m_opened_file
->NeedWindow()) {
458 m_opened_file
->SetWindow(m_video_window
);
463 void MMBoardFrame::UpdateInfoText()
465 wxString infotext1
, infotext2
;
468 infotext1
= wxT("File type:\n\t");
469 infotext1
+= m_opened_file
->GetStringType() + wxT("\n");
471 infotext2
= wxT("File informations:\n\n");
472 infotext2
+= m_opened_file
->GetStringInformation();
474 infotext1
= wxT("File type: \n\tNo file opened");
475 infotext2
= wxT("File informations:\nNo information\n\n\n\n\n");
478 m_fileType
->SetLabel(infotext1
);
479 m_infoText
->SetLabel(infotext2
);
482 void MMBoardFrame::UpdateMMedInfo()
484 MMBoardTime current
, length
;
487 current
= m_opened_file
->GetPosition();
488 length
= m_opened_file
->GetLength();
490 current
.hours
= current
.minutes
= current
.seconds
= 0;
495 // We refresh the status bar
496 wxString temp_string
;
497 temp_string
.Printf(wxT("%02d:%02d / %02d:%02d"), current
.hours
* 60 + current
.minutes
,
498 current
.seconds
, length
.hours
* 60 + length
.minutes
, length
.seconds
);
499 SetStatusText(temp_string
, 1);
502 #endif // wxUSE_STATUSBAR
504 // We set the slider position
505 m_positionSlider
->SetValue(current
.hours
* 3600 + current
.minutes
* 60 + current
.seconds
);
508 // ----------------------------------------------------------------------------
509 // Playing management, refreshers, ...
511 void MMBoardFrame::OnRefreshInfo(wxEvent
& WXUNUSED(event
))
515 if (m_opened_file
->IsStopped())
517 m_refreshTimer
->Stop();
518 m_playButton
->Enable();
519 m_stopButton
->Disable();
520 m_pauseButton
->Disable();
524 void MMBoardFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
526 m_stopButton
->Enable();
527 m_pauseButton
->Enable();
528 m_playButton
->Disable();
530 if (m_opened_file
->IsPaused())
532 m_opened_file
->Resume();
536 m_refreshTimer
->Start(1000, false);
538 m_opened_file
->Play();
540 m_stopButton
->Enable();
541 m_pauseButton
->Enable();
542 m_playButton
->Disable();
545 void MMBoardFrame::OnStop(wxCommandEvent
& WXUNUSED(event
))
547 m_opened_file
->Stop();
548 m_refreshTimer
->Stop();
550 m_stopButton
->Disable();
551 m_playButton
->Enable();
556 void MMBoardFrame::OnPause(wxCommandEvent
& WXUNUSED(event
))
558 m_opened_file
->Pause();
560 m_playButton
->Enable();
561 m_pauseButton
->Disable();
564 void MMBoardFrame::OnEject(wxCommandEvent
& WXUNUSED(event
))
566 m_opened_file
->Stop();
568 delete m_opened_file
;
569 m_opened_file
= NULL
;
571 m_playButton
->Disable();
572 m_pauseButton
->Disable();
573 m_stopButton
->Disable();
574 m_ejectButton
->Disable();
575 m_positionSlider
->Disable();
581 void MMBoardFrame::OnSetPosition(wxCommandEvent
& WXUNUSED(event
))
586 itime
= m_positionSlider
->GetValue();
587 btime
.seconds
= itime
% 60;
588 btime
.minutes
= (itime
/ 60) % 60;
589 btime
.hours
= itime
/ 3600;
590 m_opened_file
->SetPosition(btime
);