]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/mmedia/mmboard.cpp
Applied patch [ 1263950 ] wxConnection fixes for Unicode
[wxWidgets.git] / contrib / samples / mmedia / 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.h"
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" wxWidgets 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(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
41 #include "mondrian.xpm"
42 #endif
43
44 // include multimedia classes
45 #include "wx/mmedia/sndbase.h"
46 #ifdef __WIN32__
47 #include "wx/mmedia/sndwin.h"
48 #endif
49 #ifdef __UNIX__
50 #include "wx/mmedia/sndoss.h"
51 #include "wx/mmedia/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 OnEject(wxCommandEvent& event);
87 void OnRefreshInfo(wxEvent& event);
88 void OnSetPosition(wxCommandEvent& event);
89
90 void OpenVideoWindow();
91 void CloseVideoWindow();
92
93 private:
94 // any class wishing to process wxWidgets events must use this macro
95 DECLARE_EVENT_TABLE()
96
97 private:
98 void UpdateMMedInfo();
99 void UpdateInfoText();
100
101 MMBoardFile *m_opened_file;
102
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;
107
108 wxPanel *m_panel;
109 wxSizer *m_sizer;
110
111 wxTimer *m_refreshTimer;
112 };
113
114 // ----------------------------------------------------------------------------
115 // constants
116 // ----------------------------------------------------------------------------
117
118 // IDs for the controls and the menu commands
119 enum
120 {
121 // menu items
122 MMBoard_Quit = 1,
123 MMBoard_Open,
124 MMBoard_About,
125 MMBoard_PositionSlider,
126 MMBoard_PlayButton,
127 MMBoard_PauseButton,
128 MMBoard_ResumeButton,
129 MMBoard_StopButton,
130 MMBoard_EjectButton,
131 MMBoard_RefreshInfo
132 };
133
134 // ----------------------------------------------------------------------------
135 // event tables and other macros for wxWidgets
136 // ----------------------------------------------------------------------------
137
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)
148 END_EVENT_TABLE()
149
150 // ---------------------------------------------------------------------------
151 // Main board application launcher
152 // ---------------------------------------------------------------------------
153
154 IMPLEMENT_APP(MMBoardApp)
155
156 // ============================================================================
157 // implementation
158 // ============================================================================
159
160 // ----------------------------------------------------------------------------
161 // the application class
162 // ----------------------------------------------------------------------------
163
164 bool MMBoardApp::OnInit()
165 {
166 // create the main application window
167 MMBoardFrame *frame = new MMBoardFrame(_T("Multimedia Board"),
168 wxPoint(50, 50), wxSize(450, 340));
169
170 // and show it (the frames, unlike simple controls, are not shown when
171 // created initially)
172 frame->Show();
173
174 m_caps = TestMultimediaCaps();
175
176 if (!m_caps) {
177 wxMessageBox(_T("Your system has no multimedia capabilities. We are exiting now."), _T("Major error !"), wxOK | wxICON_ERROR, NULL);
178 return false;
179 }
180
181 wxString msg;
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(""));
185
186 wxMessageBox(msg, _T("Good !"), wxOK | wxICON_INFORMATION, NULL);
187
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.
191 return true;
192 }
193
194 wxUint8 MMBoardApp::TestMultimediaCaps()
195 {
196 wxSoundStream *dev;
197 wxUint8 caps;
198
199 caps = 0;
200
201 #ifdef __WIN32__
202 // We test the Windows sound support.
203
204 dev = new wxSoundStreamWin();
205 if (dev->GetError() == wxSOUND_NOERROR)
206 caps |= MM_SOUND_WIN;
207 delete dev;
208
209 #elif defined __UNIX__
210 // We now test the ESD support
211
212 dev = new wxSoundStreamESD();
213 if (dev->GetError() == wxSOUND_NOERROR)
214 caps |= MM_SOUND_ESD;
215 delete dev;
216
217 // We test the OSS (Open Sound System) support.
218 // WARNING: There is a conflict between ESD and ALSA. We may be interrested
219 // in disabling the auto detection of OSS is ESD has been detected.
220 #if 1
221 if (!(caps & MM_SOUND_ESD)) {
222 #endif
223
224 dev = new wxSoundStreamOSS();
225 if (dev->GetError() == wxSOUND_NOERROR)
226 caps |= MM_SOUND_OSS;
227 delete dev;
228 #if 1
229 }
230 #endif
231
232 #endif
233
234 return caps;
235 }
236
237 // ----------------------------------------------------------------------------
238 // main frame
239 // ----------------------------------------------------------------------------
240
241 // frame constructor
242 MMBoardFrame::MMBoardFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
243 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
244 {
245 #ifdef __WXMAC__
246 // we need this in order to allow the about menu relocation, since ABOUT is
247 // not the default id of the about menu
248 wxApp::s_macAboutMenuItemId = MMBoard_About;
249 #endif
250
251 // set the frame icon
252 SetIcon(wxICON(mondrian));
253
254 // create a menu bar
255 wxMenu *menuFile = new wxMenu(wxEmptyString, wxMENU_TEAROFF);
256
257 // the "About" item should be in the help menu
258 wxMenu *helpMenu = new wxMenu;
259 helpMenu->Append(MMBoard_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
260
261 menuFile->Append(MMBoard_Open, wxT("&Open\tAlt-O"), wxT("Open file"));
262 menuFile->AppendSeparator();
263 menuFile->Append(MMBoard_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
264
265 // now append the freshly created menu to the menu bar...
266 wxMenuBar *menuBar = new wxMenuBar();
267 menuBar->Append(menuFile, wxT("&File"));
268 menuBar->Append(helpMenu, wxT("&Help"));
269
270 // ... and attach this menu bar to the frame
271 SetMenuBar(menuBar);
272
273 #if wxUSE_STATUSBAR
274 // create a status bar just for fun (by default with 1 pane only)
275 CreateStatusBar(3);
276 SetStatusText(wxT("Welcome to wxWidgets!"));
277 #endif // wxUSE_STATUSBAR
278
279 // Misc variables
280 m_opened_file = NULL;
281
282 m_panel = new wxPanel(this, wxID_ANY);
283
284 // Initialize main slider
285 m_positionSlider = new wxSlider( m_panel, MMBoard_PositionSlider, 0, 0, 60,
286 wxDefaultPosition, wxSize(300, wxDefaultCoord),
287 wxSL_HORIZONTAL | wxSL_AUTOTICKS);
288 m_positionSlider->SetPageSize(60); // 60 secs
289 m_positionSlider->Disable();
290
291 // Initialize info panel
292 wxPanel *infoPanel = new wxPanel( m_panel, wxID_ANY);
293 infoPanel->SetBackgroundColour(*wxBLACK);
294 infoPanel->SetForegroundColour(*wxWHITE);
295
296 wxBoxSizer *infoSizer = new wxBoxSizer(wxVERTICAL);
297
298 m_fileType = new wxStaticText(infoPanel, wxID_ANY, wxEmptyString);
299 #if wxUSE_STATLINE
300 wxStaticLine *line = new wxStaticLine(infoPanel, wxID_ANY);
301 #endif // wxUSE_STATLINE
302 m_infoText = new wxStaticText(infoPanel, wxID_ANY, wxEmptyString);
303
304 UpdateInfoText();
305
306 infoSizer->Add(m_fileType, 0, wxGROW | wxALL, 1);
307 #if wxUSE_STATLINE
308 infoSizer->Add(line, 0, wxGROW | wxCENTRE, 20);
309 #endif // wxUSE_STATLINE
310 infoSizer->Add(m_infoText, 0, wxGROW | wxALL, 1);
311
312 infoPanel->SetSizer(infoSizer);
313
314 // Bitmap button panel
315 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
316
317 wxBitmap play_bmp(play_back_xpm);
318 wxBitmap stop_bmp(stop_back_xpm);
319 wxBitmap eject_bmp(eject_xpm);
320 wxBitmap pause_bmp(pause_xpm);
321
322 m_playButton = new wxBitmapButton(m_panel, MMBoard_PlayButton, play_bmp);
323 m_playButton->Disable();
324 m_pauseButton = new wxBitmapButton(m_panel, MMBoard_PauseButton, pause_bmp);
325 m_pauseButton->Disable();
326 m_stopButton = new wxBitmapButton(m_panel, MMBoard_StopButton, stop_bmp);
327 m_stopButton->Disable();
328 m_ejectButton = new wxBitmapButton(m_panel, MMBoard_EjectButton, eject_bmp);
329 m_ejectButton->Disable();
330
331 buttonSizer->Add(m_playButton, 0, wxALL, 2);
332 buttonSizer->Add(m_pauseButton, 0, wxALL, 2);
333 buttonSizer->Add(m_stopButton, 0, wxALL, 2);
334 buttonSizer->Add(m_ejectButton, 0, wxALL, 2);
335
336 // Top sizer
337 m_sizer = new wxBoxSizer(wxVERTICAL);
338 #if wxUSE_STATLINE
339 m_sizer->Add(new wxStaticLine(m_panel, wxID_ANY), 0, wxGROW | wxCENTRE, 0);
340 #endif // wxUSE_STATLINE
341 m_sizer->Add(m_positionSlider, 0, wxCENTRE | wxGROW | wxALL, 2);
342 #if wxUSE_STATLINE
343 m_sizer->Add(new wxStaticLine(m_panel, wxID_ANY), 0, wxGROW | wxCENTRE, 0);
344 #endif // wxUSE_STATLINE
345 m_sizer->Add(buttonSizer, 0, wxALL, 0);
346 #if wxUSE_STATLINE
347 m_sizer->Add(new wxStaticLine(m_panel, wxID_ANY), 0, wxGROW | wxCENTRE, 0);
348 #endif // wxUSE_STATLINE
349 m_sizer->Add(infoPanel, 1, wxCENTRE | wxGROW, 0);
350
351 m_panel->SetSizer(m_sizer);
352 m_sizer->Fit(this);
353 m_sizer->SetSizeHints(this);
354
355 // Timer
356 m_refreshTimer = new wxTimer(this, MMBoard_RefreshInfo);
357
358 // Video window
359 m_video_window = NULL;
360
361 // Multimedia file
362 m_opened_file = NULL;
363 }
364
365 MMBoardFrame::~MMBoardFrame()
366 {
367 if (m_opened_file)
368 delete m_opened_file;
369
370 delete m_refreshTimer;
371 }
372
373 void MMBoardFrame::OpenVideoWindow()
374 {
375 if (m_video_window)
376 return;
377
378 m_video_window = new wxWindow(m_panel, wxID_ANY, wxDefaultPosition, wxSize(200, 200));
379 m_video_window->SetBackgroundColour(*wxBLACK);
380 m_sizer->Prepend(m_video_window, 2, wxGROW | wxSHRINK | wxCENTRE, 1);
381
382 m_sizer->Fit(this);
383 }
384
385 void MMBoardFrame::CloseVideoWindow()
386 {
387 if (!m_video_window)
388 return;
389
390 m_sizer->Detach( m_video_window );
391 delete m_video_window;
392 m_video_window = NULL;
393
394 m_sizer->Fit(this);
395 }
396
397 // event handlers
398
399 void MMBoardFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
400 {
401 // true is to force the frame to close
402 Close(true);
403 }
404
405 void MMBoardFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
406 {
407 wxString msg;
408 msg.Printf( wxT("wxWidgets Multimedia board v1.0a, wxMMedia v2.0a:\n")
409 wxT("an example of the capabilities of the wxWidgets multimedia classes.\n")
410 wxT("Copyright 1999, 2000, Guilhem Lavaux.\n"));
411
412 wxMessageBox(msg, _T("About MMBoard"), wxOK | wxICON_INFORMATION, this);
413 }
414
415 void MMBoardFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
416 {
417 wxString selected_file;
418
419 if (m_opened_file) {
420 if (!m_opened_file->IsStopped()) {
421 wxCommandEvent event2;
422 OnStop(event2);
423 }
424 delete m_opened_file;
425 }
426
427 // select a file to be opened
428 #if wxUSE_FILEDLG
429 selected_file = wxLoadFileSelector(_T("multimedia"), _T("*"), NULL, this);
430 #endif // wxUSE_FILEDLG
431 if (selected_file.empty())
432 return;
433
434 m_opened_file = MMBoardManager::Open(selected_file);
435
436 // Change the range values of the slider.
437 MMBoardTime length;
438
439 length = m_opened_file->GetLength();
440 m_positionSlider->SetRange(0, length.hours * 3600 + length.minutes * 60 + length.seconds);
441
442 // Update misc info
443 UpdateMMedInfo();
444
445 #if wxUSE_STATUSBAR
446 SetStatusText(selected_file, 2);
447 #endif // wxUSE_STATUSBAR
448
449 // Update info text
450 UpdateInfoText();
451
452 // Enable a few buttons
453 m_playButton->Enable();
454 m_ejectButton->Enable();
455 m_positionSlider->Enable();
456
457 if (m_opened_file->NeedWindow()) {
458 OpenVideoWindow();
459 m_opened_file->SetWindow(m_video_window);
460 } else
461 CloseVideoWindow();
462 }
463
464 void MMBoardFrame::UpdateInfoText()
465 {
466 wxString infotext1, infotext2;
467
468 if (m_opened_file) {
469 infotext1 = wxT("File type:\n\t");
470 infotext1 += m_opened_file->GetStringType() + wxT("\n");
471
472 infotext2 = wxT("File informations:\n\n");
473 infotext2 += m_opened_file->GetStringInformation();
474 } else {
475 infotext1 = wxT("File type: \n\tNo file opened");
476 infotext2 = wxT("File informations:\nNo information\n\n\n\n\n");
477 }
478
479 m_fileType->SetLabel(infotext1);
480 m_infoText->SetLabel(infotext2);
481 }
482
483 void MMBoardFrame::UpdateMMedInfo()
484 {
485 MMBoardTime current, length;
486
487 if (m_opened_file) {
488 current = m_opened_file->GetPosition();
489 length = m_opened_file->GetLength();
490 } else {
491 current.hours = current.minutes = current.seconds = 0;
492 length = current;
493 }
494
495 #if wxUSE_STATUSBAR
496 // We refresh the status bar
497 wxString temp_string;
498 temp_string.Printf(wxT("%02d:%02d / %02d:%02d"), current.hours * 60 + current.minutes,
499 current.seconds, length.hours * 60 + length.minutes, length.seconds);
500 SetStatusText(temp_string, 1);
501 #else
502 wxUnusedVar(length);
503 #endif // wxUSE_STATUSBAR
504
505 // We set the slider position
506 m_positionSlider->SetValue(current.hours * 3600 + current.minutes * 60 + current.seconds);
507 }
508
509 // ----------------------------------------------------------------------------
510 // Playing management, refreshers, ...
511
512 void MMBoardFrame::OnRefreshInfo(wxEvent& WXUNUSED(event))
513 {
514 UpdateMMedInfo();
515
516 if (m_opened_file->IsStopped())
517 {
518 m_refreshTimer->Stop();
519 m_playButton->Enable();
520 m_stopButton->Disable();
521 m_pauseButton->Disable();
522 }
523 }
524
525 void MMBoardFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
526 {
527 m_stopButton->Enable();
528 m_pauseButton->Enable();
529 m_playButton->Disable();
530
531 if (m_opened_file->IsPaused())
532 {
533 m_opened_file->Resume();
534 return;
535 }
536
537 m_refreshTimer->Start(1000, false);
538
539 m_opened_file->Play();
540
541 m_stopButton->Enable();
542 m_pauseButton->Enable();
543 m_playButton->Disable();
544 }
545
546 void MMBoardFrame::OnStop(wxCommandEvent& WXUNUSED(event))
547 {
548 m_opened_file->Stop();
549 m_refreshTimer->Stop();
550
551 m_stopButton->Disable();
552 m_playButton->Enable();
553
554 UpdateMMedInfo();
555 }
556
557 void MMBoardFrame::OnPause(wxCommandEvent& WXUNUSED(event))
558 {
559 m_opened_file->Pause();
560
561 m_playButton->Enable();
562 m_pauseButton->Disable();
563 }
564
565 void MMBoardFrame::OnEject(wxCommandEvent& WXUNUSED(event))
566 {
567 m_opened_file->Stop();
568
569 delete m_opened_file;
570 m_opened_file = NULL;
571
572 m_playButton->Disable();
573 m_pauseButton->Disable();
574 m_stopButton->Disable();
575 m_ejectButton->Disable();
576 m_positionSlider->Disable();
577
578 UpdateInfoText();
579 UpdateMMedInfo();
580 }
581
582 void MMBoardFrame::OnSetPosition(wxCommandEvent& WXUNUSED(event))
583 {
584 wxUint32 itime;
585 MMBoardTime btime;
586
587 itime = m_positionSlider->GetValue();
588 btime.seconds = itime % 60;
589 btime.minutes = (itime / 60) % 60;
590 btime.hours = itime / 3600;
591 m_opened_file->SetPosition(btime);
592
593 UpdateMMedInfo();
594 }