fixed id values to be more standard
[wxWidgets.git] / samples / sound / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sound.cpp
3 // Purpose: Example of sound playing in wxWidgets
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
19 // need because it includes almost all "standard" wxWidgets headers)
20 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #include "wx/frame.h"
23 #include "wx/menu.h"
24 #include "wx/msgdlg.h"
25 #include "wx/icon.h"
26 #include "wx/textctrl.h"
27 #include "wx/filedlg.h"
28 #endif
29
30 #include "wx/sound.h"
31 #include "wx/numdlg.h"
32 #include "wx/textdlg.h"
33
34 // ----------------------------------------------------------------------------
35 // resources
36 // ----------------------------------------------------------------------------
37
38 // the application icon (under Windows and OS/2 it is in resources)
39 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
40 #include "../sample.xpm"
41 #endif
42
43 #define WAV_FILE _T("doggrowl.wav")
44
45 // ----------------------------------------------------------------------------
46 // private classes
47 // ----------------------------------------------------------------------------
48
49 class MyApp : public wxApp
50 {
51 public:
52 virtual bool OnInit();
53 };
54
55
56 class MyFrame : public wxFrame
57 {
58 public:
59 // ctor(s)
60 MyFrame(const wxString& title);
61 ~MyFrame() { delete m_sound; }
62
63 // event handlers (these functions should _not_ be virtual)
64 void OnPlaySync(wxCommandEvent& event);
65 void OnPlayAsync(wxCommandEvent& event);
66 void OnPlayAsyncOnStack(wxCommandEvent& event);
67 void OnPlayLoop(wxCommandEvent& event);
68
69 void OnSelectFile(wxCommandEvent& event);
70 #ifdef __WXMSW__
71 void OnSelectResource(wxCommandEvent& event);
72 #endif // __WXMSW__
73 void OnQuit(wxCommandEvent& event);
74 void OnAbout(wxCommandEvent& event);
75
76 void NotifyUsingFile(const wxString& name);
77
78
79 private:
80 wxSound *CreateSound() const;
81
82 wxSound* m_sound;
83 wxString m_soundFile;
84 #ifdef __WXMSW__
85 wxString m_soundRes;
86 #endif // __WXMSW__
87 wxTextCtrl* m_tc;
88
89 // any class wishing to process wxWidgets events must use this macro
90 DECLARE_EVENT_TABLE()
91 };
92
93 // ----------------------------------------------------------------------------
94 // constants
95 // ----------------------------------------------------------------------------
96
97 // IDs for the controls and the menu commands
98 enum
99 {
100 // menu items
101 Sound_SelectFile = wxID_HIGHEST + 1,
102 #ifdef __WXMSW__
103 Sound_SelectResource,
104 #endif // __WXMSW__
105
106 Sound_PlaySync,
107 Sound_PlayAsync,
108 Sound_PlayAsyncOnStack,
109 Sound_PlayLoop,
110
111 Sound_Quit = wxID_EXIT,
112 Sound_About = wxID_ABOUT
113 };
114
115 // ----------------------------------------------------------------------------
116 // event tables and other macros for wxWidgets
117 // ----------------------------------------------------------------------------
118
119 // the event tables connect the wxWidgets events with the functions (event
120 // handlers) which process them. It can be also done at run-time, but for the
121 // simple menu events like this the static method is much simpler.
122 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
123 EVT_MENU(Sound_SelectFile, MyFrame::OnSelectFile)
124 #ifdef __WXMSW__
125 EVT_MENU(Sound_SelectResource, MyFrame::OnSelectResource)
126 #endif // __WXMSW__
127 EVT_MENU(Sound_Quit, MyFrame::OnQuit)
128 EVT_MENU(Sound_About, MyFrame::OnAbout)
129 EVT_MENU(Sound_PlaySync, MyFrame::OnPlaySync)
130 EVT_MENU(Sound_PlayAsync, MyFrame::OnPlayAsync)
131 EVT_MENU(Sound_PlayAsyncOnStack, MyFrame::OnPlayAsyncOnStack)
132 EVT_MENU(Sound_PlayLoop, MyFrame::OnPlayLoop)
133 END_EVENT_TABLE()
134
135 // Create a new application object: this macro will allow wxWidgets to create
136 // the application object during program execution (it's better than using a
137 // static object for many reasons) and also implements the accessor function
138 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
139 // not wxApp)
140 IMPLEMENT_APP(MyApp)
141
142 // ============================================================================
143 // implementation
144 // ============================================================================
145
146 // ----------------------------------------------------------------------------
147 // the application class
148 // ----------------------------------------------------------------------------
149
150 // 'Main program' equivalent: the program execution "starts" here
151 bool MyApp::OnInit()
152 {
153 // create the main application window
154 MyFrame *frame = new MyFrame(_T("wxWidgets Sound Sample"));
155
156 // and show it (the frames, unlike simple controls, are not shown when
157 // created initially)
158 frame->Show(true);
159
160 // success: wxApp::OnRun() will be called which will enter the main message
161 // loop and the application will run. If we returned false here, the
162 // application would exit immediately.
163 return true;
164 }
165
166 // ----------------------------------------------------------------------------
167 // main frame
168 // ----------------------------------------------------------------------------
169
170 // frame constructor
171 MyFrame::MyFrame(const wxString& title)
172 : wxFrame(NULL, wxID_ANY, title)
173 {
174 m_sound = NULL;
175 m_soundFile = WAV_FILE;
176
177 // set the frame icon
178 SetIcon(wxICON(sample));
179
180 wxMenu *menuFile = new wxMenu;
181 wxMenu *helpMenu = new wxMenu;
182 wxMenu *playMenu = new wxMenu;
183 helpMenu->Append(Sound_About, _T("&About...\tF1"), _T("Show about dialog"));
184 menuFile->Append(Sound_SelectFile, _T("Select WAV &file\tCtrl+O"), _T("Select a new wav file to play"));
185 #ifdef __WXMSW__
186 menuFile->Append(Sound_SelectResource, _T("Select WAV &resource\tCtrl+R"), _T("Select a new resource to play"));
187 #endif // __WXMSW__
188 menuFile->Append(Sound_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
189 playMenu->Append(Sound_PlaySync, _T("Play sound &synchronously\tCtrl+S"));
190 playMenu->Append(Sound_PlayAsync, _T("Play sound &asynchronously\tCtrl+A"));
191 playMenu->Append(Sound_PlayAsyncOnStack, _T("Play sound asynchronously (&object on stack)\tCtrl+T"));
192 playMenu->Append(Sound_PlayLoop, _T("&Loop sound\tCtrl+L"));
193
194 // now append the freshly created menu to the menu bar...
195 wxMenuBar *menuBar = new wxMenuBar();
196 menuBar->Append(menuFile, _T("&File"));
197 menuBar->Append(playMenu, _T("&Play"));
198 menuBar->Append(helpMenu, _T("&Help"));
199
200 // ... and attach this menu bar to the frame
201 SetMenuBar(menuBar);
202
203 m_tc = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
204 wxDefaultPosition, wxDefaultSize,
205 wxTE_MULTILINE|wxTE_READONLY);
206 NotifyUsingFile(m_soundFile);
207 }
208
209
210 wxSound *MyFrame::CreateSound() const
211 {
212 #ifdef __WXMSW__
213 if ( !m_soundRes.empty() )
214 {
215 return new wxSound(m_soundRes, true);
216 }
217 #endif // __WXMSW__
218
219 return new wxSound(m_soundFile);
220 }
221
222
223 void MyFrame::NotifyUsingFile(const wxString& name)
224 {
225 wxString msg;
226 msg << _T("Using sound file: ") << name << _T("\n");
227 m_tc->AppendText(msg);
228 }
229
230
231 // event handlers
232
233
234 void MyFrame::OnSelectFile(wxCommandEvent& WXUNUSED(event))
235 {
236 wxFileDialog dlg(this, _T("Choose a sound file"),
237 wxEmptyString, wxEmptyString,
238 _T("WAV files (*.wav)|*.wav"), wxOPEN|wxCHANGE_DIR);
239 if ( dlg.ShowModal() == wxID_OK )
240 {
241 m_soundFile = dlg.GetPath();
242 #ifdef __WXMSW__
243 m_soundRes.clear();
244 #endif // __WXMSW__
245
246 delete m_sound;
247 m_sound = NULL;
248 NotifyUsingFile(m_soundFile);
249 }
250 }
251
252 #ifdef __WXMSW__
253
254 void MyFrame::OnSelectResource(wxCommandEvent& WXUNUSED(event))
255 {
256 m_soundRes = wxGetTextFromUser
257 (
258 _T("Enter resource name:"),
259 _T("wxWidgets Sound Sample"),
260 _T("FromResource"),
261 this
262 );
263 if ( m_soundRes.empty() )
264 return;
265
266 m_soundFile.clear();
267 delete m_sound;
268 m_sound = NULL;
269
270 NotifyUsingFile(_T("Windows WAV resource"));
271 }
272
273 #endif // __WXMSW__
274
275 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
276 {
277 // true is to force the frame to close
278 Close(true);
279 }
280
281 void MyFrame::OnPlaySync(wxCommandEvent& WXUNUSED(event))
282 {
283 wxBusyCursor busy;
284 if (!m_sound)
285 m_sound = CreateSound();
286 if (m_sound->IsOk())
287 m_sound->Play(wxSOUND_SYNC);
288 }
289
290 void MyFrame::OnPlayAsync(wxCommandEvent& WXUNUSED(event))
291 {
292 wxBusyCursor busy;
293 if (!m_sound)
294 m_sound = CreateSound();
295 if (m_sound->IsOk())
296 m_sound->Play(wxSOUND_ASYNC);
297 }
298
299 void MyFrame::OnPlayAsyncOnStack(wxCommandEvent& WXUNUSED(event))
300 {
301 wxBusyCursor busy;
302 wxSound snd(m_soundFile);
303 if (snd.IsOk())
304 snd.Play(wxSOUND_ASYNC);
305 }
306
307 void MyFrame::OnPlayLoop(wxCommandEvent& WXUNUSED(event))
308 {
309 wxBusyCursor busy;
310 if (!m_sound)
311 m_sound = CreateSound();
312 if (m_sound->IsOk())
313 m_sound->Play(wxSOUND_ASYNC | wxSOUND_LOOP);
314 }
315
316 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
317 {
318 wxString msg;
319 msg.Printf( _T("This is the About dialog of the sound sample.\n")
320 _T("Welcome to %s"), wxVERSION_STRING);
321
322 wxMessageBox(msg, _T("About"), wxOK | wxICON_INFORMATION, this);
323 }