]> git.saurik.com Git - wxWidgets.git/blob - samples/sound/sound.cpp
update from Jonas for 2.5.1 (patch 901921)
[wxWidgets.git] / samples / sound / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sound.cpp
3 // Purpose: Example of sound playing in wxWindows
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" wxWindows 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 #endif
27
28 #include "wx/sound.h"
29 #include "wx/numdlg.h"
30
31 // ----------------------------------------------------------------------------
32 // resources
33 // ----------------------------------------------------------------------------
34
35 // the application icon (under Windows and OS/2 it is in resources)
36 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
37 #include "../sample.xpm"
38 #endif
39
40 #define WAV_FILE _T("doggrowl.wav")
41
42 // ----------------------------------------------------------------------------
43 // private classes
44 // ----------------------------------------------------------------------------
45
46 class MyApp : public wxApp
47 {
48 public:
49 virtual bool OnInit();
50 };
51
52
53 class MyFrame : public wxFrame
54 {
55 public:
56 // ctor(s)
57 MyFrame(const wxString& title);
58 ~MyFrame() { delete m_sound; }
59
60 // event handlers (these functions should _not_ be virtual)
61 void OnPlaySync(wxCommandEvent& event);
62 void OnPlayAsync(wxCommandEvent& event);
63 void OnPlayAsyncOnStack(wxCommandEvent& event);
64 void OnPlayLoop(wxCommandEvent& event);
65
66 void OnQuit(wxCommandEvent& event);
67 void OnAbout(wxCommandEvent& event);
68
69 private:
70 wxSound *m_sound;
71
72 // any class wishing to process wxWindows events must use this macro
73 DECLARE_EVENT_TABLE()
74 };
75
76 // ----------------------------------------------------------------------------
77 // constants
78 // ----------------------------------------------------------------------------
79
80 // IDs for the controls and the menu commands
81 enum
82 {
83 // menu items
84 Sound_Quit = wxID_EXIT,
85 Sound_About = wxID_ABOUT,
86 Sound_PlaySync = wxID_HIGHEST + 1,
87 Sound_PlayAsync,
88 Sound_PlayAsyncOnStack,
89 Sound_PlayLoop
90 };
91
92 // ----------------------------------------------------------------------------
93 // event tables and other macros for wxWindows
94 // ----------------------------------------------------------------------------
95
96 // the event tables connect the wxWindows events with the functions (event
97 // handlers) which process them. It can be also done at run-time, but for the
98 // simple menu events like this the static method is much simpler.
99 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
100 EVT_MENU(Sound_Quit, MyFrame::OnQuit)
101 EVT_MENU(Sound_About, MyFrame::OnAbout)
102 EVT_MENU(Sound_PlaySync, MyFrame::OnPlaySync)
103 EVT_MENU(Sound_PlayAsync, MyFrame::OnPlayAsync)
104 EVT_MENU(Sound_PlayAsyncOnStack, MyFrame::OnPlayAsync)
105 EVT_MENU(Sound_PlayLoop, MyFrame::OnPlayLoop)
106 END_EVENT_TABLE()
107
108 // Create a new application object: this macro will allow wxWindows to create
109 // the application object during program execution (it's better than using a
110 // static object for many reasons) and also implements the accessor function
111 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
112 // not wxApp)
113 IMPLEMENT_APP(MyApp)
114
115 // ============================================================================
116 // implementation
117 // ============================================================================
118
119 // ----------------------------------------------------------------------------
120 // the application class
121 // ----------------------------------------------------------------------------
122
123 // 'Main program' equivalent: the program execution "starts" here
124 bool MyApp::OnInit()
125 {
126 // create the main application window
127 MyFrame *frame = new MyFrame(_T("wxWindows Sound Sample"));
128
129 // and show it (the frames, unlike simple controls, are not shown when
130 // created initially)
131 frame->Show(true);
132
133 // success: wxApp::OnRun() will be called which will enter the main message
134 // loop and the application will run. If we returned false here, the
135 // application would exit immediately.
136 return true;
137 }
138
139 // ----------------------------------------------------------------------------
140 // main frame
141 // ----------------------------------------------------------------------------
142
143 // frame constructor
144 MyFrame::MyFrame(const wxString& title)
145 : wxFrame(NULL, wxID_ANY, title)
146 {
147 m_sound = NULL;
148
149 // set the frame icon
150 SetIcon(wxICON(sample));
151
152 wxMenu *menuFile = new wxMenu;
153 wxMenu *helpMenu = new wxMenu;
154 wxMenu *playMenu = new wxMenu;
155 helpMenu->Append(Sound_About, _T("&About...\tF1"), _T("Show about dialog"));
156 menuFile->Append(Sound_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
157 playMenu->Append(Sound_PlaySync, _T("Play sound &synchronously"));
158 playMenu->Append(Sound_PlayAsync, _T("Play sound &asynchronously"));
159 playMenu->Append(Sound_PlayAsync, _T("Play sound asynchronously (&object on stack)"));
160 playMenu->Append(Sound_PlayLoop, _T("&Loop sound"));
161
162 // now append the freshly created menu to the menu bar...
163 wxMenuBar *menuBar = new wxMenuBar();
164 menuBar->Append(menuFile, _T("&File"));
165 menuBar->Append(playMenu, _T("&Play"));
166 menuBar->Append(helpMenu, _T("&Help"));
167
168 // ... and attach this menu bar to the frame
169 SetMenuBar(menuBar);
170 }
171
172
173 // event handlers
174
175 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
176 {
177 // true is to force the frame to close
178 Close(true);
179 }
180
181 void MyFrame::OnPlaySync(wxCommandEvent& WXUNUSED(event))
182 {
183 wxBusyCursor busy;
184 if (!m_sound)
185 m_sound = new wxSound(WAV_FILE);
186 if (m_sound->IsOk())
187 m_sound->Play(wxSOUND_SYNC);
188 }
189
190 void MyFrame::OnPlayAsync(wxCommandEvent& WXUNUSED(event))
191 {
192 wxBusyCursor busy;
193 if (!m_sound)
194 m_sound = new wxSound(WAV_FILE);
195 if (m_sound->IsOk())
196 m_sound->Play(wxSOUND_ASYNC);
197 }
198
199 void MyFrame::OnPlayAsyncOnStack(wxCommandEvent& WXUNUSED(event))
200 {
201 wxBusyCursor busy;
202 wxSound snd(WAV_FILE);
203 if (snd.IsOk())
204 snd.Play(wxSOUND_ASYNC);
205 }
206
207 void MyFrame::OnPlayLoop(wxCommandEvent& WXUNUSED(event))
208 {
209 wxBusyCursor busy;
210 if (!m_sound)
211 m_sound = new wxSound(WAV_FILE);
212 if (m_sound->IsOk())
213 m_sound->Play(wxSOUND_ASYNC | wxSOUND_LOOP);
214 }
215
216 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
217 {
218 wxString msg;
219 msg.Printf( _T("This is the About dialog of the sound sample.\n")
220 _T("Welcome to %s"), wxVERSION_STRING);
221
222 wxMessageBox(msg, _T("About"), wxOK | wxICON_INFORMATION, this);
223 }