]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/board/mmboard.cpp
Removed unnecessary code from utilsunx.cpp
[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 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 wxWindows 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 wxWindows
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("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(TRUE);
173
174 m_caps = TestMultimediaCaps();
175
176 if (!m_caps) {
177 wxMessageBox("Your system has no multimedia capabilities. We are exiting now.", "Major error !", wxOK | wxICON_ERROR, NULL);
178 return FALSE;
179 }
180
181 wxString msg;
182 msg.Printf("Detected : %s%s%s", (m_caps & MM_SOUND_OSS) ? "OSS " : "",
183 (m_caps & MM_SOUND_ESD) ? "ESD " : "",
184 (m_caps & MM_SOUND_WIN) ? "WIN" : "");
185
186 wxMessageBox(msg, "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 __UNIX__
202 // We now test the ESD support
203
204 dev = new wxSoundStreamESD();
205 if (dev->GetError() == wxSOUND_NOERR)
206 caps |= MM_SOUND_ESD;
207 delete dev;
208
209 // We test the OSS (Open Sound System) support.
210
211 #if 0
212 dev = new wxSoundStreamOSS();
213 if (dev->GetError() == wxSOUND_NOERR)
214 caps |= MM_SOUND_OSS;
215 delete dev;
216 #endif
217 #endif
218
219 #ifdef __WIN32__
220 // We test the Windows sound support.
221
222 dev = new wxSoundStreamWin();
223 if (dev->GetError() == wxSOUND_NOERR)
224 caps |= MM_SOUND_WIN;
225 delete dev;
226 #endif
227
228 return caps;
229 }
230
231 // ----------------------------------------------------------------------------
232 // main frame
233 // ----------------------------------------------------------------------------
234
235 // frame constructor
236 MMBoardFrame::MMBoardFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
237 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
238 {
239 #ifdef __WXMAC__
240 // we need this in order to allow the about menu relocation, since ABOUT is
241 // not the default id of the about menu
242 wxApp::s_macAboutMenuItemId = MMBoard_About;
243 #endif
244
245 // set the frame icon
246 SetIcon(wxICON(mondrian));
247
248 // create a menu bar
249 wxMenu *menuFile = new wxMenu(wxT(""), wxMENU_TEAROFF);
250
251 // the "About" item should be in the help menu
252 wxMenu *helpMenu = new wxMenu;
253 helpMenu->Append(MMBoard_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
254
255 menuFile->Append(MMBoard_Open, wxT("&Open\tAlt-O"), wxT("Open file"));
256 menuFile->AppendSeparator();
257 menuFile->Append(MMBoard_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
258
259 // now append the freshly created menu to the menu bar...
260 wxMenuBar *menuBar = new wxMenuBar();
261 menuBar->Append(menuFile, wxT("&File"));
262 menuBar->Append(helpMenu, wxT("&Help"));
263
264 // ... and attach this menu bar to the frame
265 SetMenuBar(menuBar);
266
267 #if wxUSE_STATUSBAR
268 // create a status bar just for fun (by default with 1 pane only)
269 CreateStatusBar(3);
270 SetStatusText(wxT("Welcome to wxWindows!"));
271 #endif // wxUSE_STATUSBAR
272
273 // Misc variables
274 m_opened_file = NULL;
275
276 m_panel = new wxPanel(this, -1);
277
278 // Initialize main slider
279 m_positionSlider = new wxSlider( m_panel, MMBoard_PositionSlider, 0, 0, 60,
280 wxDefaultPosition, wxSize(300, -1),
281 wxSL_HORIZONTAL | wxSL_AUTOTICKS);
282 m_positionSlider->SetPageSize(60); // 60 secs
283 m_positionSlider->Enable(FALSE);
284
285 // Initialize info panel
286 wxPanel *infoPanel = new wxPanel( m_panel, -1);
287 infoPanel->SetBackgroundColour(*wxBLACK);
288 infoPanel->SetForegroundColour(*wxWHITE);
289
290 wxBoxSizer *infoSizer = new wxBoxSizer(wxVERTICAL);
291
292 m_fileType = new wxStaticText(infoPanel, -1, wxT(""));
293 wxStaticLine *line = new wxStaticLine(infoPanel, -1);
294 m_infoText = new wxStaticText(infoPanel, -1, "");
295
296 UpdateInfoText();
297
298 infoSizer->Add(m_fileType, 0, wxGROW | wxALL, 1);
299 infoSizer->Add(line, 0, wxGROW | wxCENTRE, 20);
300 infoSizer->Add(m_infoText, 0, wxGROW | wxALL, 1);
301
302 infoPanel->SetSizer(infoSizer);
303 infoPanel->SetAutoLayout(TRUE);
304
305 // Bitmap button panel
306 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
307
308 wxBitmap *play_bmp = new wxBitmap(play_back_xpm);
309 wxBitmap *stop_bmp = new wxBitmap(stop_back_xpm);
310 wxBitmap *eject_bmp = new wxBitmap(eject_xpm);
311 wxBitmap *pause_bmp = new wxBitmap(pause_xpm);
312
313 m_playButton = new wxBitmapButton(m_panel, MMBoard_PlayButton, *play_bmp);
314 m_playButton->Enable(FALSE);
315 m_pauseButton = new wxBitmapButton(m_panel, MMBoard_PauseButton, *pause_bmp);
316 m_pauseButton->Enable(FALSE);
317 m_stopButton = new wxBitmapButton(m_panel, MMBoard_StopButton, *stop_bmp);
318 m_stopButton->Enable(FALSE);
319 m_ejectButton = new wxBitmapButton(m_panel, MMBoard_EjectButton, *eject_bmp);
320 m_ejectButton->Enable(FALSE);
321
322 buttonSizer->Add(m_playButton, 0, wxALL, 2);
323 buttonSizer->Add(m_pauseButton, 0, wxALL, 2);
324 buttonSizer->Add(m_stopButton, 0, wxALL, 2);
325 buttonSizer->Add(m_ejectButton, 0, wxALL, 2);
326
327 // Top sizer
328 m_sizer = new wxBoxSizer(wxVERTICAL);
329 m_sizer->Add(new wxStaticLine(m_panel, -1), 0, wxGROW | wxCENTRE, 0);
330 m_sizer->Add(m_positionSlider, 0, wxCENTRE | wxGROW | wxALL, 2);
331 m_sizer->Add(new wxStaticLine(m_panel, -1), 0, wxGROW | wxCENTRE, 0);
332 m_sizer->Add(buttonSizer, 0, wxALL, 0);
333 m_sizer->Add(new wxStaticLine(m_panel, -1), 0, wxGROW | wxCENTRE, 0);
334 m_sizer->Add(infoPanel, 1, wxCENTRE | wxGROW, 0);
335
336 m_panel->SetSizer(m_sizer);
337 m_panel->SetAutoLayout(TRUE);
338 m_sizer->Fit(this);
339 m_sizer->SetSizeHints(this);
340
341 // Timer
342 m_refreshTimer = new wxTimer(this, MMBoard_RefreshInfo);
343
344 // Video window
345 m_video_window = NULL;
346
347 // Multimedia file
348 m_opened_file = NULL;
349 }
350
351 MMBoardFrame::~MMBoardFrame()
352 {
353 if (m_opened_file)
354 delete m_opened_file;
355
356 delete m_refreshTimer;
357 }
358
359 void MMBoardFrame::OpenVideoWindow()
360 {
361 if (m_video_window)
362 return;
363
364 m_video_window = new wxWindow(m_panel, -1, wxDefaultPosition, wxSize(200, 200));
365 m_video_window->SetBackgroundColour(*wxBLACK);
366 m_sizer->Prepend(m_video_window, 2, wxGROW | wxSHRINK | wxCENTRE, 1);
367
368 m_sizer->Fit(this);
369 }
370
371 void MMBoardFrame::CloseVideoWindow()
372 {
373 if (!m_video_window)
374 return;
375
376 m_sizer->Remove(m_video_window);
377 delete m_video_window;
378 m_video_window = NULL;
379
380 m_sizer->Fit(this);
381 }
382
383 // event handlers
384
385 void MMBoardFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
386 {
387 // TRUE is to force the frame to close
388 Close(TRUE);
389 }
390
391 void MMBoardFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
392 {
393 wxString msg;
394 msg.Printf( wxT("wxWindows Multimedia board v1.0a, wxMMedia v2.0a:\n")
395 wxT("an example of the capabilities of the wxWindows multimedia classes.\n")
396 wxT("Copyright 1999, 2000, Guilhem Lavaux.\n"));
397
398 wxMessageBox(msg, "About MMBoard", wxOK | wxICON_INFORMATION, this);
399 }
400
401 void MMBoardFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
402 {
403 wxString selected_file;
404
405 if (m_opened_file) {
406 if (!m_opened_file->IsStopped()) {
407 wxCommandEvent event2;
408 OnStop(event2);
409 }
410 delete m_opened_file;
411 }
412
413 // select a file to be opened
414 selected_file = wxLoadFileSelector("multimedia", "*", NULL, this);
415 if (selected_file.IsNull())
416 return;
417
418 m_opened_file = MMBoardManager::Open(selected_file);
419
420 // Change the range values of the slider.
421 MMBoardTime length;
422
423 length = m_opened_file->GetLength();
424 m_positionSlider->SetRange(0, length.hours * 3600 + length.minutes * 60 + length.seconds);
425
426 // Update misc info
427 UpdateMMedInfo();
428
429 SetStatusText(selected_file, 2);
430
431 // Update info text
432 UpdateInfoText();
433
434 // Enable a few buttons
435 m_playButton->Enable(TRUE);
436 m_ejectButton->Enable(TRUE);
437 m_positionSlider->Enable(TRUE);
438
439 if (m_opened_file->NeedWindow()) {
440 OpenVideoWindow();
441 m_opened_file->SetWindow(m_video_window);
442 } else
443 CloseVideoWindow();
444 }
445
446 void MMBoardFrame::UpdateInfoText()
447 {
448 wxString infotext1, infotext2;
449
450 if (m_opened_file) {
451 infotext1 = wxT("File type:\n\t");
452 infotext1 += m_opened_file->GetStringType() + wxT("\n");
453
454 infotext2 = wxT("File informations:\n\n");
455 infotext2 += m_opened_file->GetStringInformation();
456 } else {
457 infotext1 = wxT("File type: \n\tNo file opened");
458 infotext2 = wxT("File informations:\nNo information\n\n\n\n\n");
459 }
460
461 m_fileType->SetLabel(infotext1);
462 m_infoText->SetLabel(infotext2);
463 }
464
465 void MMBoardFrame::UpdateMMedInfo()
466 {
467 wxString temp_string;
468 MMBoardTime current, length;
469
470 if (m_opened_file) {
471 current = m_opened_file->GetPosition();
472 length = m_opened_file->GetLength();
473 } else {
474 current.hours = current.minutes = current.seconds = 0;
475 length = current;
476 }
477
478 // We refresh the status bar
479 temp_string.Printf(wxT("%02d:%02d / %02d:%02d"), current.hours * 60 + current.minutes,
480 current.seconds, length.hours * 60 + length.minutes, length.seconds);
481 SetStatusText(temp_string, 1);
482
483 // We set the slider position
484 m_positionSlider->SetValue(current.hours * 3600 + current.minutes * 60 + current.seconds);
485 }
486
487 // ----------------------------------------------------------------------------
488 // Playing management, refreshers, ...
489
490 void MMBoardFrame::OnRefreshInfo(wxEvent& WXUNUSED(event))
491 {
492 UpdateMMedInfo();
493
494 if (m_opened_file->IsStopped()) {
495 m_refreshTimer->Stop();
496 m_playButton->Enable(TRUE);
497 m_stopButton->Enable(FALSE);
498 m_pauseButton->Enable(FALSE);
499 }
500 }
501
502 void MMBoardFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
503 {
504 m_stopButton->Enable(TRUE);
505 m_pauseButton->Enable(TRUE);
506 m_playButton->Enable(FALSE);
507
508 if (m_opened_file->IsPaused()) {
509 m_opened_file->Resume();
510 return;
511 }
512
513 m_refreshTimer->Start(1000, FALSE);
514
515 m_opened_file->Play();
516
517 m_stopButton->Enable(TRUE);
518 m_pauseButton->Enable(TRUE);
519 m_playButton->Enable(FALSE);
520 }
521
522 void MMBoardFrame::OnStop(wxCommandEvent& WXUNUSED(event))
523 {
524 m_opened_file->Stop();
525 m_refreshTimer->Stop();
526
527 m_stopButton->Enable(FALSE);
528 m_playButton->Enable(TRUE);
529
530 UpdateMMedInfo();
531 }
532
533 void MMBoardFrame::OnPause(wxCommandEvent& WXUNUSED(event))
534 {
535 m_opened_file->Pause();
536
537 m_playButton->Enable(TRUE);
538 m_pauseButton->Enable(FALSE);
539 }
540
541 void MMBoardFrame::OnEject(wxCommandEvent& WXUNUSED(event))
542 {
543 m_opened_file->Stop();
544
545 delete m_opened_file;
546 m_opened_file = NULL;
547
548 m_playButton->Enable(FALSE);
549 m_pauseButton->Enable(FALSE);
550 m_stopButton->Enable(FALSE);
551 m_ejectButton->Enable(FALSE);
552 m_positionSlider->Enable(FALSE);
553
554 UpdateInfoText();
555 UpdateMMedInfo();
556 }
557
558 void MMBoardFrame::OnSetPosition(wxCommandEvent& WXUNUSED(event))
559 {
560 wxUint32 itime;
561 MMBoardTime btime;
562
563 itime = m_positionSlider->GetValue();
564 btime.seconds = itime % 60;
565 btime.minutes = (itime / 60) % 60;
566 btime.hours = itime / 3600;
567 m_opened_file->SetPosition(btime);
568
569 UpdateMMedInfo();
570 }
571