]> git.saurik.com Git - wxWidgets.git/blob - src/msw/wave.cpp
Added wxWindowBase::CentreOnParent to allow top level windows to be
[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 #ifndef __GNUWIN32__
35 #include <mmsystem.h>
36 #endif
37
38 #ifndef __TWIN32__
39 #ifdef __GNUWIN32__
40 #include "wx/msw/gnuwin32/extra.h"
41 #endif
42 #endif
43
44 wxWave::wxWave()
45 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
46 {
47 }
48
49 wxWave::wxWave(const wxString& sFileName, bool isResource)
50 : m_waveData(NULL), m_waveLength(0), m_isResource(isResource)
51 {
52 Create(sFileName, isResource);
53 }
54
55 wxWave::wxWave(int size, const wxByte* data)
56 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
57 {
58 Create(size, data);
59 }
60
61 wxWave::~wxWave()
62 {
63 Free();
64 }
65
66 bool wxWave::Create(const wxString& fileName, bool isResource)
67 {
68 Free();
69
70 if (isResource)
71 {
72 m_isResource = TRUE;
73
74 HRSRC hresInfo;
75 #if defined(__WIN32__) && !defined(__TWIN32__)
76 hresInfo = ::FindResourceA((HMODULE) wxhInstance, fileName, "WAVE");
77 #else
78 hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, "WAVE");
79 #endif
80 if (!hresInfo)
81 return FALSE;
82
83 HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo);
84
85 if (waveData)
86 {
87 m_waveData= (wxByte*)::LockResource(waveData);
88 m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo);
89 }
90
91 return (m_waveData ? TRUE : FALSE);
92 }
93 else
94 {
95 m_isResource = FALSE;
96
97 wxFile fileWave;
98 if (!fileWave.Open(fileName, wxFile::read))
99 return FALSE;
100
101 m_waveLength = (int) fileWave.Length();
102
103 m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
104 if (!m_waveData)
105 return FALSE;
106
107 fileWave.Read(m_waveData, m_waveLength);
108
109 return TRUE;
110 }
111 }
112
113 bool wxWave::Create(int size, const wxByte* data)
114 {
115 Free();
116 m_isResource = FALSE;
117 m_waveLength=size;
118 m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
119 if (!m_waveData)
120 return FALSE;
121
122 for (int i=0; i<size; i++) m_waveData[i] = data[i];
123 return TRUE;
124 }
125
126 bool wxWave::Play(bool async, bool looped) const
127 {
128 if (!IsOk())
129 return FALSE;
130
131 #ifdef __WIN32__
132 return ( ::PlaySound((LPCSTR)m_waveData, NULL, SND_MEMORY |
133 SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 );
134 #else
135 return ( ::sndPlaySound((LPCSTR)m_waveData, SND_MEMORY |
136 SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 );
137 #endif
138 }
139
140 bool wxWave::Free()
141 {
142 if (m_waveData)
143 {
144 #ifdef __WIN32__
145 HGLOBAL waveData = ::GlobalHandle(m_waveData);
146 #else
147 HGLOBAL waveData = GlobalPtrHandle(m_waveData);
148 #endif
149
150 if (waveData)
151 {
152 if (m_isResource)
153 ::FreeResource(waveData);
154 else
155 {
156 ::GlobalUnlock(waveData);
157 ::GlobalFree(waveData);
158 }
159
160 m_waveData = NULL;
161 m_waveLength = 0;
162 return TRUE;
163 }
164 }
165 return FALSE;
166 }
167
168