]> git.saurik.com Git - wxWidgets.git/blame - samples/sound/sound.cpp
Hopefully fixed library names generated by wx-config for OS/2's PM port.
[wxWidgets.git] / samples / sound / sound.cpp
CommitLineData
0d9c603d
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: sound.cpp
be5a51fb 3// Purpose: Example of sound playing in wxWidgets
0d9c603d
VS
4// Author: Vaclav Slavik
5// Modified by:
6// Created: 2004/01/29
7// RCS-ID: $Id$
8// Copyright: (c) 2004 Vaclav Salvik
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18// for all others, include the necessary headers (this file is usually all you
be5a51fb 19// need because it includes almost all "standard" wxWidgets headers)
0d9c603d
VS
20#ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #include "wx/frame.h"
23 #include "wx/menu.h"
24 #include "wx/msgdlg.h"
e4f3eb42 25 #include "wx/icon.h"
aa92c398
RD
26 #include "wx/textctrl.h"
27 #include "wx/filedlg.h"
0d9c603d
VS
28#endif
29
d93966b9 30#include "wx/sound.h"
e4f3eb42 31#include "wx/numdlg.h"
0d9c603d
VS
32
33// ----------------------------------------------------------------------------
34// resources
35// ----------------------------------------------------------------------------
36
37// the application icon (under Windows and OS/2 it is in resources)
38#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
39 #include "../sample.xpm"
40#endif
41
42#define WAV_FILE _T("doggrowl.wav")
43
44// ----------------------------------------------------------------------------
45// private classes
46// ----------------------------------------------------------------------------
47
48class MyApp : public wxApp
49{
50public:
51 virtual bool OnInit();
52};
53
54
55class MyFrame : public wxFrame
56{
57public:
58 // ctor(s)
59 MyFrame(const wxString& title);
60 ~MyFrame() { delete m_sound; }
61
62 // event handlers (these functions should _not_ be virtual)
63 void OnPlaySync(wxCommandEvent& event);
64 void OnPlayAsync(wxCommandEvent& event);
65 void OnPlayAsyncOnStack(wxCommandEvent& event);
66 void OnPlayLoop(wxCommandEvent& event);
aa92c398
RD
67
68 void OnSelectFile(wxCommandEvent& event);
0d9c603d
VS
69 void OnQuit(wxCommandEvent& event);
70 void OnAbout(wxCommandEvent& event);
71
aa92c398
RD
72 void NotifyUsingFile(const wxString& name);
73
74
0d9c603d 75private:
aa92c398
RD
76 wxSound* m_sound;
77 wxString m_soundFile;
78 wxTextCtrl* m_tc;
0d9c603d 79
be5a51fb 80 // any class wishing to process wxWidgets events must use this macro
0d9c603d
VS
81 DECLARE_EVENT_TABLE()
82};
83
84// ----------------------------------------------------------------------------
85// constants
86// ----------------------------------------------------------------------------
87
88// IDs for the controls and the menu commands
89enum
90{
91 // menu items
92 Sound_Quit = wxID_EXIT,
93 Sound_About = wxID_ABOUT,
94 Sound_PlaySync = wxID_HIGHEST + 1,
95 Sound_PlayAsync,
96 Sound_PlayAsyncOnStack,
aa92c398
RD
97 Sound_PlayLoop,
98 Sound_SelectFile
0d9c603d
VS
99};
100
101// ----------------------------------------------------------------------------
be5a51fb 102// event tables and other macros for wxWidgets
0d9c603d
VS
103// ----------------------------------------------------------------------------
104
be5a51fb 105// the event tables connect the wxWidgets events with the functions (event
0d9c603d
VS
106// handlers) which process them. It can be also done at run-time, but for the
107// simple menu events like this the static method is much simpler.
108BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aa92c398 109 EVT_MENU(Sound_SelectFile, MyFrame::OnSelectFile)
0d9c603d
VS
110 EVT_MENU(Sound_Quit, MyFrame::OnQuit)
111 EVT_MENU(Sound_About, MyFrame::OnAbout)
112 EVT_MENU(Sound_PlaySync, MyFrame::OnPlaySync)
113 EVT_MENU(Sound_PlayAsync, MyFrame::OnPlayAsync)
27c2041f 114 EVT_MENU(Sound_PlayAsyncOnStack, MyFrame::OnPlayAsyncOnStack)
0d9c603d
VS
115 EVT_MENU(Sound_PlayLoop, MyFrame::OnPlayLoop)
116END_EVENT_TABLE()
117
be5a51fb 118// Create a new application object: this macro will allow wxWidgets to create
0d9c603d
VS
119// the application object during program execution (it's better than using a
120// static object for many reasons) and also implements the accessor function
121// wxGetApp() which will return the reference of the right type (i.e. MyApp and
122// not wxApp)
123IMPLEMENT_APP(MyApp)
124
125// ============================================================================
126// implementation
127// ============================================================================
128
129// ----------------------------------------------------------------------------
130// the application class
131// ----------------------------------------------------------------------------
132
133// 'Main program' equivalent: the program execution "starts" here
134bool MyApp::OnInit()
135{
136 // create the main application window
be5a51fb 137 MyFrame *frame = new MyFrame(_T("wxWidgets Sound Sample"));
0d9c603d
VS
138
139 // and show it (the frames, unlike simple controls, are not shown when
140 // created initially)
141 frame->Show(true);
142
143 // success: wxApp::OnRun() will be called which will enter the main message
144 // loop and the application will run. If we returned false here, the
145 // application would exit immediately.
146 return true;
147}
148
149// ----------------------------------------------------------------------------
150// main frame
151// ----------------------------------------------------------------------------
152
153// frame constructor
154MyFrame::MyFrame(const wxString& title)
155 : wxFrame(NULL, wxID_ANY, title)
156{
157 m_sound = NULL;
aa92c398 158 m_soundFile = WAV_FILE;
0d9c603d
VS
159
160 // set the frame icon
161 SetIcon(wxICON(sample));
162
163 wxMenu *menuFile = new wxMenu;
164 wxMenu *helpMenu = new wxMenu;
165 wxMenu *playMenu = new wxMenu;
166 helpMenu->Append(Sound_About, _T("&About...\tF1"), _T("Show about dialog"));
b320510f 167 menuFile->Append(Sound_SelectFile, _T("&Select WAV file\tCtrl+O"), _T("Select a new wav file to play"));
0d9c603d 168 menuFile->Append(Sound_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
b320510f
DE
169 playMenu->Append(Sound_PlaySync, _T("Play sound &synchronously\tCtrl+S"));
170 playMenu->Append(Sound_PlayAsync, _T("Play sound &asynchronously\tCtrl+A"));
171 playMenu->Append(Sound_PlayAsyncOnStack, _T("Play sound asynchronously (&object on stack)\tCtrl+T"));
172 playMenu->Append(Sound_PlayLoop, _T("&Loop sound\tCtrl+L"));
0d9c603d
VS
173
174 // now append the freshly created menu to the menu bar...
175 wxMenuBar *menuBar = new wxMenuBar();
176 menuBar->Append(menuFile, _T("&File"));
177 menuBar->Append(playMenu, _T("&Play"));
178 menuBar->Append(helpMenu, _T("&Help"));
179
180 // ... and attach this menu bar to the frame
181 SetMenuBar(menuBar);
aa92c398 182
7981fe9b 183 m_tc = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
86c5779a 184 wxDefaultPosition, wxDefaultSize,
aa92c398
RD
185 wxTE_MULTILINE|wxTE_READONLY);
186 NotifyUsingFile(m_soundFile);
187}
188
189
190
191void MyFrame::NotifyUsingFile(const wxString& name)
192{
193 wxString msg;
194 msg << _T("Using sound file: ") << name << _T("\n");
195 m_tc->AppendText(msg);
0d9c603d
VS
196}
197
198
199// event handlers
200
aa92c398
RD
201
202void MyFrame::OnSelectFile(wxCommandEvent& WXUNUSED(event))
203{
204 wxFileDialog dlg(this, _T("Choose a sound file"),
205 wxEmptyString, wxEmptyString,
206 _T("WAV files (*.wav)|*.wav"), wxOPEN|wxCHANGE_DIR);
207 if ( dlg.ShowModal() == wxID_OK )
208 {
209 m_soundFile = dlg.GetPath();
210 delete m_sound;
211 m_sound = NULL;
212 NotifyUsingFile(m_soundFile);
213 }
214}
215
0d9c603d
VS
216void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
217{
218 // true is to force the frame to close
219 Close(true);
220}
221
222void MyFrame::OnPlaySync(wxCommandEvent& WXUNUSED(event))
223{
224 wxBusyCursor busy;
225 if (!m_sound)
aa92c398 226 m_sound = new wxSound(m_soundFile);
0d9c603d 227 if (m_sound->IsOk())
fc57ba54 228 m_sound->Play(wxSOUND_SYNC);
0d9c603d
VS
229}
230
231void MyFrame::OnPlayAsync(wxCommandEvent& WXUNUSED(event))
232{
233 wxBusyCursor busy;
234 if (!m_sound)
aa92c398 235 m_sound = new wxSound(m_soundFile);
0d9c603d 236 if (m_sound->IsOk())
fc57ba54 237 m_sound->Play(wxSOUND_ASYNC);
0d9c603d
VS
238}
239
240void MyFrame::OnPlayAsyncOnStack(wxCommandEvent& WXUNUSED(event))
241{
242 wxBusyCursor busy;
aa92c398 243 wxSound snd(m_soundFile);
0d9c603d 244 if (snd.IsOk())
fc57ba54 245 snd.Play(wxSOUND_ASYNC);
0d9c603d
VS
246}
247
248void MyFrame::OnPlayLoop(wxCommandEvent& WXUNUSED(event))
249{
250 wxBusyCursor busy;
251 if (!m_sound)
aa92c398 252 m_sound = new wxSound(m_soundFile);
0d9c603d 253 if (m_sound->IsOk())
fc57ba54 254 m_sound->Play(wxSOUND_ASYNC | wxSOUND_LOOP);
0d9c603d
VS
255}
256
257void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
258{
259 wxString msg;
260 msg.Printf( _T("This is the About dialog of the sound sample.\n")
261 _T("Welcome to %s"), wxVERSION_STRING);
262
263 wxMessageBox(msg, _T("About"), wxOK | wxICON_INFORMATION, this);
264}