]> git.saurik.com Git - wxWidgets.git/blob - src/msw/wave.cpp
wxMenuBarBase for MSW (untested)
[wxWidgets.git] / src / msw / wave.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wave.cpp
3 // Purpose: wxWave
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wave.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if defined(__BORLANDC__)
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/file.h"
28 #include "wx/msw/wave.h"
29 #include "wx/msw/private.h"
30
31 #include <windows.h>
32 #include <windowsx.h>
33
34 #if !defined( __GNUWIN32__ ) || defined(wxUSE_NORLANDER_HEADERS)
35 #include <mmsystem.h>
36 #endif
37
38 #ifndef __TWIN32__
39 #ifdef __GNUWIN32__
40 #ifndef wxUSE_NORLANDER_HEADERS
41 #include "wx/msw/gnuwin32/extra.h"
42 #endif
43 #endif
44 #endif
45
46 wxWave::wxWave()
47 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
48 {
49 }
50
51 wxWave::wxWave(const wxString& sFileName, bool isResource)
52 : m_waveData(NULL), m_waveLength(0), m_isResource(isResource)
53 {
54 Create(sFileName, isResource);
55 }
56
57 wxWave::wxWave(int size, const wxByte* data)
58 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
59 {
60 Create(size, data);
61 }
62
63 wxWave::~wxWave()
64 {
65 Free();
66 }
67
68 bool wxWave::Create(const wxString& fileName, bool isResource)
69 {
70 Free();
71
72 if (isResource)
73 {
74 m_isResource = TRUE;
75
76 HRSRC hresInfo;
77 #if defined(__WIN32__) && !defined(__TWIN32__)
78 #ifdef _UNICODE
79 hresInfo = ::FindResourceW((HMODULE) wxhInstance, fileName, wxT("WAVE"));
80 #else
81 hresInfo = ::FindResourceA((HMODULE) wxhInstance, fileName, wxT("WAVE"));
82 #endif
83 #else
84 hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, wxT("WAVE"));
85 #endif
86 if (!hresInfo)
87 return FALSE;
88
89 HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo);
90
91 if (waveData)
92 {
93 m_waveData= (wxByte*)::LockResource(waveData);
94 m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo);
95 }
96
97 return (m_waveData ? TRUE : FALSE);
98 }
99 else
100 {
101 m_isResource = FALSE;
102
103 wxFile fileWave;
104 if (!fileWave.Open(fileName, wxFile::read))
105 return FALSE;
106
107 m_waveLength = (int) fileWave.Length();
108
109 m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
110 if (!m_waveData)
111 return FALSE;
112
113 fileWave.Read(m_waveData, m_waveLength);
114
115 return TRUE;
116 }
117 }
118
119 bool wxWave::Create(int size, const wxByte* data)
120 {
121 Free();
122 m_isResource = FALSE;
123 m_waveLength=size;
124 m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
125 if (!m_waveData)
126 return FALSE;
127
128 for (int i=0; i<size; i++) m_waveData[i] = data[i];
129 return TRUE;
130 }
131
132 bool wxWave::Play(bool async, bool looped) const
133 {
134 if (!IsOk())
135 return FALSE;
136
137 #ifdef __WIN32__
138 return ( ::PlaySound((LPCTSTR)m_waveData, NULL, SND_MEMORY |
139 SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 );
140 #else
141 return ( ::sndPlaySound((LPCSTR)m_waveData, SND_MEMORY |
142 SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 );
143 #endif
144 }
145
146 bool wxWave::Free()
147 {
148 if (m_waveData)
149 {
150 #ifdef __WIN32__
151 HGLOBAL waveData = ::GlobalHandle(m_waveData);
152 #else
153 HGLOBAL waveData = GlobalPtrHandle(m_waveData);
154 #endif
155
156 if (waveData)
157 {
158 if (m_isResource)
159 ::FreeResource(waveData);
160 else
161 {
162 ::GlobalUnlock(waveData);
163 ::GlobalFree(waveData);
164 }
165
166 m_waveData = NULL;
167 m_waveLength = 0;
168 return TRUE;
169 }
170 }
171 return FALSE;
172 }
173
174