]> git.saurik.com Git - wxWidgets.git/blame_incremental - utils/wxMMedia2/board/mmboard.cpp
Added a bigger wxMMedia2 demonstration: wxMultimediaBoard: it is a sort
[wxWidgets.git] / utils / wxMMedia2 / board / mmboard.cpp
... / ...
CommitLineData
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
71class MMBoardFrame : public wxFrame
72{
73public:
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
88private:
89 // any class wishing to process wxWindows events must use this macro
90 DECLARE_EVENT_TABLE()
91
92private:
93 void UpdateMMedInfo();
94 void UpdateInfoText();
95
96 MMBoardFile *m_opened_file;
97
98 wxSlider *m_positionSlider;
99 wxBitmapButton *m_playButton, *m_pauseButton, *m_stopButton, *m_ejectButton;
100 wxStaticText *m_fileType, *m_infoText;
101
102 wxTimer *m_refreshTimer;
103};
104
105// ----------------------------------------------------------------------------
106// constants
107// ----------------------------------------------------------------------------
108
109// IDs for the controls and the menu commands
110enum
111{
112 // menu items
113 MMBoard_Quit = 1,
114 MMBoard_Open,
115 MMBoard_About,
116 MMBoard_PositionSlider,
117 MMBoard_PlayButton,
118 MMBoard_PauseButton,
119 MMBoard_ResumeButton,
120 MMBoard_StopButton,
121 MMBoard_EjectButton,
122 MMBoard_RefreshInfo
123};
124
125// ----------------------------------------------------------------------------
126// event tables and other macros for wxWindows
127// ----------------------------------------------------------------------------
128
129BEGIN_EVENT_TABLE(MMBoardFrame, wxFrame)
130 EVT_MENU(MMBoard_Quit, MMBoardFrame::OnQuit)
131 EVT_MENU(MMBoard_About, MMBoardFrame::OnAbout)
132 EVT_MENU(MMBoard_Open, MMBoardFrame::OnOpen)
133 EVT_BUTTON(MMBoard_PlayButton, MMBoardFrame::OnPlay)
134 EVT_BUTTON(MMBoard_StopButton, MMBoardFrame::OnStop)
135 EVT_BUTTON(MMBoard_PauseButton, MMBoardFrame::OnPause)
136 EVT_CUSTOM(wxEVT_TIMER, MMBoard_RefreshInfo, MMBoardFrame::OnRefreshInfo)
137END_EVENT_TABLE()
138
139// ---------------------------------------------------------------------------
140// Main board application launcher
141// ---------------------------------------------------------------------------
142
143IMPLEMENT_APP(MMBoardApp)
144
145// ============================================================================
146// implementation
147// ============================================================================
148
149// ----------------------------------------------------------------------------
150// the application class
151// ----------------------------------------------------------------------------
152
153bool MMBoardApp::OnInit()
154{
155 // create the main application window
156 MMBoardFrame *frame = new MMBoardFrame("Multimedia Board",
157 wxPoint(50, 50), wxSize(450, 340));
158
159 // and show it (the frames, unlike simple controls, are not shown when
160 // created initially)
161 frame->Show(TRUE);
162
163 m_caps = TestMultimediaCaps();
164
165 if (!m_caps) {
166 wxMessageBox("Your system has no multimedia capabilities. We are exiting now.", "Major error !", wxOK | wxICON_ERROR, NULL);
167 return FALSE;
168 }
169
170 wxString msg;
171 msg.Printf("Detected : %s%s%s", (m_caps & MM_SOUND_OSS) ? "OSS " : "",
172 (m_caps & MM_SOUND_ESD) ? "ESD " : "",
173 (m_caps & MM_SOUND_WIN) ? "WIN" : "");
174
175 wxMessageBox(msg, "Good !", wxOK | wxICON_INFORMATION, NULL);
176
177 // success: wxApp::OnRun() will be called which will enter the main message
178 // loop and the application will run. If we returned FALSE here, the
179 // application would exit immediately.
180 return TRUE;
181}
182
183wxUint8 MMBoardApp::TestMultimediaCaps()
184{
185 wxSoundStream *dev;
186 wxUint8 caps;
187
188 caps = 0;
189
190#ifdef __UNIX__
191 // We test the OSS (Open Sound System) support.
192
193 dev = new wxSoundStreamOSS();
194 if (dev->GetError() == wxSOUND_NOERR)
195 caps |= MM_SOUND_OSS;
196 delete dev;
197
198 // We now test the ESD support
199
200 dev = new wxSoundStreamESD();
201 if (dev->GetError() == wxSOUND_NOERR)
202 caps |= MM_SOUND_ESD;
203 delete dev;
204#endif
205
206#ifdef __WIN32__
207 // We test the Windows sound support.
208
209 dev = new wxSoundStreamWin();
210 if (dev->GetError() == wxSOUND_NOERR)
211 caps |= MM_SOUND_WIN;
212 delete dev;
213#endif
214
215 return caps;
216}
217
218// ----------------------------------------------------------------------------
219// main frame
220// ----------------------------------------------------------------------------
221
222// frame constructor
223MMBoardFrame::MMBoardFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
224 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
225{
226#ifdef __WXMAC__
227 // we need this in order to allow the about menu relocation, since ABOUT is
228 // not the default id of the about menu
229 wxApp::s_macAboutMenuItemId = MMBoard_About;
230#endif
231
232 // set the frame icon
233 SetIcon(wxICON(mondrian));
234
235 // create a menu bar
236 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
237
238 // the "About" item should be in the help menu
239 wxMenu *helpMenu = new wxMenu;
240 helpMenu->Append(MMBoard_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
241
242 menuFile->Append(MMBoard_Open, _T("&Open\tAlt-O"), _T("Open file"));
243 menuFile->AppendSeparator();
244 menuFile->Append(MMBoard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
245
246 // now append the freshly created menu to the menu bar...
247 wxMenuBar *menuBar = new wxMenuBar();
248 menuBar->Append(menuFile, _T("&File"));
249 menuBar->Append(helpMenu, _T("&Help"));
250
251 // ... and attach this menu bar to the frame
252 SetMenuBar(menuBar);
253
254#if wxUSE_STATUSBAR
255 // create a status bar just for fun (by default with 1 pane only)
256 CreateStatusBar(3);
257 SetStatusText(_T("Welcome to wxWindows!"));
258#endif // wxUSE_STATUSBAR
259
260 // Misc variables
261 m_opened_file = NULL;
262
263 wxPanel *panel = new wxPanel(this, -1);
264
265 // Initialize main slider
266 m_positionSlider = new wxSlider( panel, MMBoard_PositionSlider, 0, 0, 60,
267 wxDefaultPosition, wxSize(300, -1),
268 wxSL_HORIZONTAL | wxSL_AUTOTICKS);
269 m_positionSlider->SetPageSize(60); // 60 secs
270
271 // Initialize info panel
272 wxPanel *infoPanel = new wxPanel( panel, -1);
273 infoPanel->SetBackgroundColour(*wxBLACK);
274 infoPanel->SetForegroundColour(*wxWHITE);
275
276 wxBoxSizer *infoSizer = new wxBoxSizer(wxVERTICAL);
277
278 m_fileType = new wxStaticText(infoPanel, -1, _T(""));
279 wxStaticLine *line = new wxStaticLine(infoPanel, -1);
280 m_infoText = new wxStaticText(infoPanel, -1, "");
281
282 UpdateInfoText();
283
284 infoSizer->Add(m_fileType, 0, wxGROW | wxALL, 1);
285 infoSizer->Add(line, 0, wxGROW | wxCENTRE, 20);
286 infoSizer->Add(m_infoText, 0, wxGROW | wxALL, 1);
287
288 infoPanel->SetSizer(infoSizer);
289 infoPanel->SetAutoLayout(TRUE);
290
291 // Bitmap button panel
292 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
293
294 wxBitmap *play_bmp = new wxBitmap(play_back_xpm);
295 wxBitmap *stop_bmp = new wxBitmap(stop_back_xpm);
296 wxBitmap *eject_bmp = new wxBitmap(eject_xpm);
297 wxBitmap *pause_bmp = new wxBitmap(pause_xpm);
298
299 m_playButton = new wxBitmapButton(panel, MMBoard_PlayButton, *play_bmp);
300 m_playButton->Enable(FALSE);
301 m_pauseButton = new wxBitmapButton(panel, MMBoard_PauseButton, *pause_bmp);
302 m_pauseButton->Enable(FALSE);
303 m_stopButton = new wxBitmapButton(panel, MMBoard_StopButton, *stop_bmp);
304 m_stopButton->Enable(FALSE);
305 m_ejectButton = new wxBitmapButton(panel, MMBoard_EjectButton, *eject_bmp);
306 m_ejectButton->Enable(FALSE);
307
308 buttonSizer->Add(m_playButton, 0, wxALL, 2);
309 buttonSizer->Add(m_pauseButton, 0, wxALL, 2);
310 buttonSizer->Add(m_stopButton, 0, wxALL, 2);
311 buttonSizer->Add(m_ejectButton, 0, wxALL, 2);
312
313 // Top sizer
314 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
315 sizer->Add(new wxStaticLine(panel, -1), 0, wxGROW | wxCENTRE, 0);
316 sizer->Add(m_positionSlider, 0, wxCENTRE | wxGROW | wxALL, 2);
317 sizer->Add(new wxStaticLine(panel, -1), 0, wxGROW | wxCENTRE, 0);
318 sizer->Add(buttonSizer, 0, wxALL, 0);
319 sizer->Add(new wxStaticLine(panel, -1), 0, wxGROW | wxCENTRE, 0);
320 sizer->Add(infoPanel, 1, wxCENTRE | wxGROW, 0);
321
322 panel->SetSizer(sizer);
323 panel->SetAutoLayout(TRUE);
324 sizer->Fit(this);
325 sizer->SetSizeHints(this);
326
327 // Timer
328 m_refreshTimer = new wxTimer(this, MMBoard_RefreshInfo);
329}
330
331MMBoardFrame::~MMBoardFrame()
332{
333 if (m_opened_file)
334 delete m_opened_file;
335
336 delete m_refreshTimer;
337}
338
339// event handlers
340
341void MMBoardFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
342{
343 // TRUE is to force the frame to close
344 Close(TRUE);
345}
346
347void MMBoardFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
348{
349 wxString msg;
350 msg.Printf( _T("wxWindows Multimedia board v1.0a, wxMMedia v2.0a:\n")
351 _T("an example of the capabilities of the wxWindows multimedia classes.\n")
352 _T("Copyright 1999, 2000, Guilhem Lavaux.\n"));
353
354 wxMessageBox(msg, "About MMBoard", wxOK | wxICON_INFORMATION, this);
355}
356
357void MMBoardFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
358{
359 wxString selected_file;
360
361 // select a file to be opened
362 selected_file = wxLoadFileSelector("multimedia", "*", NULL, this);
363 if (selected_file.IsNull())
364 return;
365
366 m_opened_file = MMBoardManager::Open(selected_file);
367
368 // Change the range values of the slider.
369 MMBoardTime length;
370
371 length = m_opened_file->GetLength();
372 m_positionSlider->SetRange(0, length.hours * 3600 + length.minutes * 60 + length.seconds);
373
374 // Update misc info
375 UpdateMMedInfo();
376
377 SetStatusText(selected_file, 2);
378
379 // Update info text
380 UpdateInfoText();
381
382 // Enable a few buttons
383 m_playButton->Enable(TRUE);
384 m_ejectButton->Enable(TRUE);
385}
386
387void MMBoardFrame::UpdateInfoText()
388{
389 wxString infotext1, infotext2;
390
391 if (m_opened_file) {
392 infotext1 = _T("File type:\n\t");
393 infotext1 += m_opened_file->GetStringType() + _T("\n");
394
395 infotext2 = _T("File informations:\n\n");
396 infotext2 += m_opened_file->GetStringInformation();
397 } else {
398 infotext1 = _T("File type: \n\tNo file opened");
399 infotext2 = _T("File informations:\nNo information\n\n\n\n\n");
400 }
401
402 m_fileType->SetLabel(infotext1);
403 m_infoText->SetLabel(infotext2);
404}
405
406void MMBoardFrame::UpdateMMedInfo()
407{
408 wxString temp_string;
409 MMBoardTime current, length;
410
411 if (m_opened_file) {
412 current = m_opened_file->GetPosition();
413 length = m_opened_file->GetLength();
414 }
415
416 // We refresh the status bar
417 temp_string.Printf("%02d:%02d / %02d:%02d", current.hours * 60 + current.minutes,
418 current.seconds, length.hours * 60 + length.minutes, length.seconds);
419 SetStatusText(temp_string, 1);
420
421 // We set the slider position
422 m_positionSlider->SetValue(current.hours * 3600 + current.minutes * 60 + current.seconds);
423}
424
425// ----------------------------------------------------------------------------
426// Playing management, refreshers, ...
427
428void MMBoardFrame::OnRefreshInfo(wxEvent& WXUNUSED(event))
429{
430 UpdateMMedInfo();
431
432 if (m_opened_file->IsStopped()) {
433 m_refreshTimer->Stop();
434 m_playButton->Enable(TRUE);
435 m_stopButton->Enable(FALSE);
436 m_pauseButton->Enable(FALSE);
437 }
438}
439
440void MMBoardFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
441{
442 m_refreshTimer->Start(1000, FALSE);
443
444 m_opened_file->Play();
445
446 m_stopButton->Enable(TRUE);
447 m_pauseButton->Enable(TRUE);
448 m_playButton->Enable(FALSE);
449}
450
451void MMBoardFrame::OnStop(wxCommandEvent& WXUNUSED(event))
452{
453 m_opened_file->Stop();
454 m_refreshTimer->Stop();
455
456 m_stopButton->Enable(FALSE);
457 m_playButton->Enable(TRUE);
458
459 UpdateMMedInfo();
460}
461
462void MMBoardFrame::OnPause(wxCommandEvent& WXUNUSED(event))
463{
464 m_opened_file->Pause();
465
466 m_playButton->Enable(TRUE);
467 m_pauseButton->Enable(FALSE);
468}