]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/moviectrl.cpp
db687e759ca1eb9055bcf002e84dec33119a93ea
[wxWidgets.git] / src / mac / carbon / moviectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/carbon/moviectrl.cpp
3 // Purpose: wxMovieCtrl MAC CARBON QT
4 // Author: Ryan Norton <wxprojects@comcast.net>
5 // Modified by:
6 // Created: 11/07/04
7 // RCS-ID: $Id$
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 //#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 //#pragma implementation "moviectrl.h"
14 //#endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #define wxUSE_MOVIECTRL 1
24
25 #if wxUSE_MOVIECTRL
26
27 #include "wx/moviectrl.h"
28 #include "wx/sound.h"
29 #include "wx/timer.h"
30
31 IMPLEMENT_CLASS(wxMovieCtrl, wxControl);
32
33 BEGIN_EVENT_TABLE(wxMovieCtrl, wxControl)
34 EVT_SIZE(wxMovieCtrl::OnSize)
35 END_EVENT_TABLE()
36
37 //MESSY headers
38 #ifdef __WXMAC__
39 #include "wx/mac/uma.h"
40 #include <Movies.h>
41 #include <Gestalt.h>
42 #endif
43
44 //quicktime media layer only required for mac emulation on pc
45 #ifndef __WXMAC__
46 #include <qtml.h>
47 #endif
48
49 #include <QuickTimeComponents.h>
50
51 //Time between timer calls
52 #define MOVIE_DELAY 100
53
54 // ------------------------------------------------------------------
55 // wxQTTimer - Handle Asyncronous Playing
56 // ------------------------------------------------------------------
57 class _wxQTTimer : public wxTimer
58 {
59 public:
60 _wxQTTimer(Movie movie) :
61 m_movie(movie), m_bPaused(false)
62 {
63 }
64
65 ~_wxQTTimer()
66 {
67 }
68
69 bool GetPaused() {return m_bPaused;}
70 void SetPaused(bool bPaused) {m_bPaused = bPaused;}
71
72 void Notify()
73 {
74 if (!m_bPaused)
75 {
76 if(!IsMovieDone(m_movie))
77 MoviesTask(m_movie, MOVIE_DELAY); //Give QT time to play movie
78 else
79 Stop();
80 }
81 }
82
83 protected:
84 Movie m_movie;
85 bool m_bPaused;
86 };
87
88 //Determines whether version 3 of QT is installed
89 Boolean wxIsQuickTime3Installed (void)
90 {
91 #ifdef __WXMAC__
92 short error;
93 long result;
94
95 error = Gestalt (gestaltQuickTime, &result);
96 return (error == noErr) && (((result >> 16) & 0xffff) >= 0x0300);
97 #else
98 return true;
99 #endif
100 }
101
102 bool wxMovieCtrl::InitQT ()
103 {
104 if (wxIsQuickTime3Installed())
105 {
106 #ifndef __WXMAC__
107 int nError;
108 //-2093 no dll
109 if ((nError = InitializeQTML(0)) != noErr)
110 {
111 wxFAIL_MSG(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError));
112 }
113 #endif
114 EnterMovies();
115 return true;
116 }
117 else
118 {
119 wxFAIL_MSG(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
120 return false;
121 }
122 }
123
124 bool wxMovieCtrl::Create(wxWindow* parent, wxWindowID id, const wxString& fileName,
125 const wxString& label, const wxPoint& pos, const wxSize& size,
126 long WXUNUSED(style), const wxString& name)
127 {
128 if ( !InitQT() )
129 return false;
130
131 OSErr err = noErr;
132 short movieResFile;
133 FSSpec sfFile;
134 #ifdef __WXMAC__
135 wxMacFilename2FSSpec( m_sndname , &sfFile ) ;
136 #else
137 int nError;
138 if ((nError = NativePathNameToFSSpec ((char*) fileName.c_str(), &sfFile, 0)) != noErr)
139 {
140 wxFAIL_MSG(wxString::Format(wxT("File:%s does not exist\nError:%i"),
141 fileName.c_str(), nError));
142 return false;
143 }
144 #endif
145 if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
146 {
147 wxFAIL_MSG(wxT("Quicktime couldn't open the file"));
148 return false;
149 }
150 short movieResID = 0;
151 Str255 movieName;
152
153 err = NewMovieFromFile (
154 &m_movie,
155 movieResFile,
156 &movieResID,
157 movieName,
158 newMovieActive,
159 NULL); //wasChanged
160
161 CloseMovieFile (movieResFile);
162
163 if (err != noErr)
164 {
165 wxFAIL_MSG(wxT("Could not create movie"));
166 return false;
167 }
168
169 m_timer = new _wxQTTimer(m_movie);
170 wxASSERT(m_timer);
171
172 //get the real size of the movie
173 Rect outRect;
174 ::GetMovieNaturalBoundsRect (m_movie, &outRect);
175
176 m_bestSize.x = outRect.right - outRect.left;
177 m_bestSize.y = outRect.bottom - outRect.top;
178
179 //do some window stuff
180 if ( !wxControl::Create(parent, id, pos, size, wxNO_BORDER, wxDefaultValidator, name) )
181 return false;
182
183 //Set our background color to black by default
184 SetBackgroundColour(*wxBLACK);
185
186 //reparent movie
187 #ifdef __WXMSW__
188 CreatePortAssociation(this->GetHWND(), NULL, 0L);
189 #endif
190 SetMovieGWorld(m_movie, (CGrafPtr)
191
192 #ifdef __WXMSW__
193 GetNativeWindowPort(this->GetHWND())
194 #else
195 this->GetHandle()
196 #endif
197 , nil);
198
199 //go!
200 SetLabel(label);
201 Play();
202
203 return true;
204 }
205
206 void wxMovieCtrl::SetLabel(const wxString& label)
207 {
208 wxControl::SetLabel(label);
209 }
210
211 bool wxMovieCtrl::Play()
212 {
213 ::StartMovie(m_movie);
214 m_timer->SetPaused(false);
215 m_timer->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
216 return ::GetMoviesError() == noErr;
217 }
218
219 bool wxMovieCtrl::Pause()
220 {
221 ::StopMovie(m_movie);
222 m_timer->SetPaused(true);
223 return ::GetMoviesError() == noErr;
224 }
225
226 bool wxMovieCtrl::Stop()
227 {
228 m_timer->SetPaused(false);
229
230 ::StopMovie(m_movie);
231 if(::GetMoviesError() != noErr)
232 return false;
233
234 ::GoToEndOfMovie(m_movie);
235 return ::GetMoviesError() == noErr;
236 }
237
238 #if wxUSE_DATETIME
239
240 bool wxMovieCtrl::Seek(const wxTimeSpan& where)
241 {
242 TimeRecord theTimeRecord;
243 theTimeRecord.value.lo = ((size_t)where.GetMilliseconds().ToLong()) * 10;
244 theTimeRecord.scale = ::GetMovieTimeScale(m_movie);
245 theTimeRecord.base = ::GetMovieTimeBase(m_movie);
246 ::SetMovieTime(m_movie, &theTimeRecord);
247
248 if (::GetMoviesError() != noErr)
249 return false;
250
251 return true;
252 }
253
254 #endif // wxUSE_DATETIME
255
256 wxMovieCtrlState wxMovieCtrl::GetState()
257 {
258 if( m_timer->IsRunning() == true )
259 return wxMOVIECTRL_STOPPED;
260
261 if ( m_timer->GetPaused() == false )
262 return wxMOVIECTRL_PLAYING;
263 else
264 return wxMOVIECTRL_PAUSED;
265 }
266
267 wxMovieCtrl::~wxMovieCtrl()
268 {
269 if (m_timer)
270 {
271 delete m_timer;
272
273 StopMovie(m_movie);
274 DisposeMovie(m_movie);
275
276 //Note that ExitMovies() is not neccessary, but
277 //the docs are fuzzy on whether or not TerminateQTML is
278 ExitMovies();
279
280 #ifndef __WXMAC__
281 TerminateQTML();
282 #endif
283 }
284 }
285
286 wxSize wxMovieCtrl::DoGetBestSize() const
287 {
288 return m_bestSize;
289 }
290
291 void wxMovieCtrl::OnSize(wxSizeEvent& evt)
292 {
293 Rect theRect;
294 theRect.left = 0;
295 theRect.top = 0;
296 theRect.bottom = evt.GetSize().y;
297 theRect.right = evt.GetSize().x;
298
299 ::SetMovieBox(m_movie, &theRect);
300
301 wxASSERT(::GetMoviesError() == noErr);
302 evt.Skip();
303 }
304 #endif //wxUSE_MOVIECTRL