]> git.saurik.com Git - wxWidgets.git/blame - src/msw/sound.cpp
Compilation fix for !wxUSE_OWNER_DRAWN.
[wxWidgets.git] / src / msw / sound.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/msw/sound.cpp
315ebf68 3// Purpose: wxSound
2bda0e17 4// Author: Julian Smart
ddc5c471 5// Modified by: 2005-07-29: Vadim Zeitlin: redesign
2bda0e17 6// Created: 04/01/98
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
ddc5c471
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
2bda0e17
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#if defined(__BORLANDC__)
ddc5c471 23 #pragma hdrstop
2bda0e17
KB
24#endif
25
315ebf68 26#if wxUSE_SOUND
1e6feb95 27
315ebf68 28#include "wx/sound.h"
ee4f8c2a 29#include "wx/msw/private.h"
2bda0e17 30
2cef71bc 31#include <mmsystem.h>
2bda0e17 32
ddc5c471
VZ
33// ----------------------------------------------------------------------------
34// wxSoundData
35// ----------------------------------------------------------------------------
36
37// ABC for different sound data representations
38class wxSoundData
2bda0e17 39{
ddc5c471
VZ
40public:
41 wxSoundData() { }
2bda0e17 42
ddc5c471
VZ
43 // return true if we had been successfully initialized
44 virtual bool IsOk() const = 0;
45
46 // get the flag corresponding to our content for PlaySound()
47 virtual DWORD GetSoundFlag() const = 0;
48
49 // get the data to be passed to PlaySound()
50 virtual LPCTSTR GetSoundData() const = 0;
51
52 virtual ~wxSoundData() { }
53};
54
55// class for in-memory sound data
56class wxSoundDataMemory : public wxSoundData
2bda0e17 57{
ddc5c471
VZ
58public:
59 // we copy the data
c559c4b3 60 wxSoundDataMemory(size_t size, const void* buf);
ddc5c471
VZ
61
62 void *GetPtr() const { return m_waveDataPtr; }
63
64 virtual bool IsOk() const { return GetPtr() != NULL; }
65 virtual DWORD GetSoundFlag() const { return SND_MEMORY; }
66 virtual LPCTSTR GetSoundData() const { return (LPCTSTR)GetPtr(); }
2bda0e17 67
ddc5c471
VZ
68private:
69 GlobalPtr m_waveData;
70 GlobalPtrLock m_waveDataPtr;
71
c0c133e1 72 wxDECLARE_NO_COPY_CLASS(wxSoundDataMemory);
ddc5c471
VZ
73};
74
75// class for sound files and resources
76class wxSoundDataFile : public wxSoundData
77{
78public:
79 wxSoundDataFile(const wxString& filename, bool isResource);
80
81 virtual bool IsOk() const { return !m_name.empty(); }
82 virtual DWORD GetSoundFlag() const
83 {
84 return m_isResource ? SND_RESOURCE : SND_FILENAME;
85 }
86 virtual LPCTSTR GetSoundData() const { return m_name.c_str(); }
87
88private:
89 const wxString m_name;
90 const bool m_isResource;
91
c0c133e1 92 wxDECLARE_NO_COPY_CLASS(wxSoundDataFile);
ddc5c471
VZ
93};
94
95// ============================================================================
96// implementation
97// ============================================================================
98
99// ----------------------------------------------------------------------------
100// wxSoundData-derived classes
101// ----------------------------------------------------------------------------
102
c559c4b3 103wxSoundDataMemory::wxSoundDataMemory(size_t size, const void* buf)
ddc5c471
VZ
104 : m_waveData(size),
105 m_waveDataPtr(m_waveData)
321db4b6 106{
ddc5c471
VZ
107 if ( IsOk() )
108 ::CopyMemory(m_waveDataPtr, buf, size);
321db4b6 109}
2bda0e17 110
ddc5c471
VZ
111wxSoundDataFile::wxSoundDataFile(const wxString& filename, bool isResource)
112 : m_name(filename),
113 m_isResource(isResource)
2bda0e17 114{
ddc5c471 115 // check for file/resource existence?
2bda0e17
KB
116}
117
ddc5c471
VZ
118// ----------------------------------------------------------------------------
119// wxSound
120// ----------------------------------------------------------------------------
121
122wxSound::wxSound()
2bda0e17 123{
ddc5c471
VZ
124 Init();
125}
2bda0e17 126
ddc5c471
VZ
127wxSound::wxSound(const wxString& filename, bool isResource)
128{
129 Init();
130 Create(filename, isResource);
131}
2bda0e17 132
c559c4b3 133wxSound::wxSound(size_t size, const void* data)
ddc5c471
VZ
134{
135 Init();
136 Create(size, data);
137}
2bda0e17 138
ddc5c471
VZ
139wxSound::~wxSound()
140{
141 Free();
142}
2bda0e17 143
ddc5c471
VZ
144void wxSound::Free()
145{
5276b0a5 146 wxDELETE(m_data);
ddc5c471 147}
2bda0e17 148
ddc5c471
VZ
149bool wxSound::CheckCreatedOk()
150{
151 if ( m_data && !m_data->IsOk() )
152 Free();
2bda0e17 153
ddc5c471
VZ
154 return m_data != NULL;
155}
2bda0e17 156
ddc5c471
VZ
157bool wxSound::Create(const wxString& filename, bool isResource)
158{
159 Free();
2bda0e17 160
ddc5c471 161 m_data = new wxSoundDataFile(filename, isResource);
2bda0e17 162
ddc5c471 163 return CheckCreatedOk();
2bda0e17
KB
164}
165
c559c4b3 166bool wxSound::Create(size_t size, const void* data)
321db4b6 167{
ddc5c471
VZ
168 Free();
169
170 m_data = new wxSoundDataMemory(size, data);
171
172 return CheckCreatedOk();
321db4b6
JS
173}
174
315ebf68 175bool wxSound::DoPlay(unsigned flags) const
2bda0e17 176{
ddc5c471
VZ
177 if ( !IsOk() || !m_data->IsOk() )
178 return false;
2bda0e17 179
ddc5c471
VZ
180 DWORD flagsMSW = m_data->GetSoundFlag();
181 HMODULE hmod = flagsMSW == SND_RESOURCE ? wxGetInstance() : NULL;
2bda0e17 182
ddc5c471
VZ
183 // we don't want replacement default sound
184 flagsMSW |= SND_NODEFAULT;
185
186 // NB: wxSOUND_SYNC is 0, don't test for it
187 flagsMSW |= (flags & wxSOUND_ASYNC) ? SND_ASYNC : SND_SYNC;
188 if ( flags & wxSOUND_LOOP )
189 {
190 // looping only works with async flag
191 flagsMSW |= SND_LOOP | SND_ASYNC;
2bda0e17 192 }
ddc5c471
VZ
193
194 return ::PlaySound(m_data->GetSoundData(), hmod, flagsMSW) != FALSE;
315ebf68
VS
195}
196
ddc5c471
VZ
197/* static */
198void wxSound::Stop()
315ebf68
VS
199{
200 ::PlaySound(NULL, NULL, 0);
2bda0e17
KB
201}
202
315ebf68 203#endif // wxUSE_SOUND
ddc5c471 204