]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/moviectrl.cpp
ba21a9a1d4cc13d540421c829908d71393dd1d0a
[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 #if wxUSE_MOVIECTRL
24
25 #include "wx/moviectrl.h"
26 #include "wx/sound.h"
27 #include "wx/timer.h"
28
29 IMPLEMENT_CLASS(wxMovieCtrl, wxControl);
30 IMPLEMENT_DYNAMIC_CLASS(wxMovieEvent, wxEvent);
31 DEFINE_EVENT_TYPE(wxEVT_MOVIE_FINISHED);
32
33 //MESSY headers
34 #ifdef __WXMAC__
35 #include "wx/mac/uma.h"
36 #include <Movies.h>
37 #include <Gestalt.h>
38 #endif
39
40 //quicktime media layer only required for mac emulation on pc
41 #ifndef __WXMAC__
42 #include <qtml.h>
43 #endif
44
45 #include <QuickTimeComponents.h>
46
47 //Time between timer calls
48 #define MOVIE_DELAY 100
49
50 // ------------------------------------------------------------------
51 // wxQTTimer - Handle Asyncronous Playing
52 // ------------------------------------------------------------------
53 class _wxQTTimer : public wxTimer
54 {
55 public:
56 _wxQTTimer(Movie movie, wxMovieCtrl* parent) :
57 m_movie(movie), m_bPaused(false), m_parent(parent)
58 {
59 }
60
61 ~_wxQTTimer()
62 {
63 }
64
65 bool GetPaused() {return m_bPaused;}
66 void SetPaused(bool bPaused) {m_bPaused = bPaused;}
67
68 void Notify()
69 {
70 if (!m_bPaused)
71 {
72 if(!IsMovieDone(m_movie))
73 MoviesTask(m_movie, MOVIE_DELAY); //Give QT time to play movie
74 else
75 {
76 Stop();
77 ::GoToBeginningOfMovie(m_movie);
78 wxASSERT( ::GetMoviesError() == noErr );
79 wxMovieEvent theEvent(wxEVT_MOVIE_FINISHED, m_parent->GetId());
80 m_parent->GetParent()->ProcessEvent(theEvent);
81 }
82 }
83 }
84
85 protected:
86 Movie m_movie;
87 bool m_bPaused;
88 wxMovieCtrl* m_parent;
89 };
90
91 //Determines whether version 3 of QT is installed
92 Boolean wxIsQuickTime3Installed (void)
93 {
94 #ifdef __WXMAC__
95 short error;
96 long result;
97
98 error = Gestalt (gestaltQuickTime, &result);
99 return (error == noErr) && (((result >> 16) & 0xffff) >= 0x0300);
100 #else
101 return true;
102 #endif
103 }
104
105 bool wxMovieCtrl::InitQT ()
106 {
107 if (wxIsQuickTime3Installed())
108 {
109 #ifndef __WXMAC__
110 int nError;
111 //-2093 no dll
112 if ((nError = InitializeQTML(0)) != noErr)
113 {
114 wxFAIL_MSG(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError));
115 }
116 #endif
117 EnterMovies();
118 return true;
119 }
120 else
121 {
122 wxFAIL_MSG(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
123 return false;
124 }
125 }
126
127 bool wxMovieCtrl::Create(wxWindow* parent, wxWindowID id, const wxString& fileName,
128 const wxString& label, const wxPoint& pos, const wxSize& size,
129 long WXUNUSED(style), const wxString& name)
130 {
131 //do some window stuff
132 if ( !wxControl::Create(parent, id, pos, size, wxNO_BORDER, wxDefaultValidator, name) )
133 return false;
134
135 //Set our background color to black by default
136 SetBackgroundColour(*wxBLACK);
137
138 if(!fileName.empty())
139 {
140 if (!Load(fileName))
141 return false;
142
143 SetLabel(label);
144
145 if(!Play())
146 return false;
147 }
148 else
149 wxControl::SetLabel(label);
150
151 return true;
152 }
153
154 bool wxMovieCtrl::Load(const wxString& fileName)
155 {
156 if(m_bLoaded)
157 Cleanup();
158
159 if ( !InitQT() )
160 return false;
161
162 OSErr err = noErr;
163 short movieResFile;
164 FSSpec sfFile;
165 #ifdef __WXMAC__
166 wxMacFilename2FSSpec( m_sndname , &sfFile ) ;
167 #else
168 int nError;
169 if ((nError = NativePathNameToFSSpec ((char*) fileName.c_str(), &sfFile, 0)) != noErr)
170 {
171 wxFAIL_MSG(wxString::Format(wxT("File:%s does not exist\nError:%i"),
172 fileName.c_str(), nError));
173 return false;
174 }
175 #endif
176 if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
177 {
178 wxFAIL_MSG(wxT("Quicktime couldn't open the file"));
179 return false;
180 }
181 short movieResID = 0;
182 Str255 movieName;
183
184 err = NewMovieFromFile (
185 &m_movie,
186 movieResFile,
187 &movieResID,
188 movieName,
189 newMovieActive,
190 NULL); //wasChanged
191
192 CloseMovieFile (movieResFile);
193
194 if (err != noErr)
195 {
196 wxFAIL_MSG(wxT("Could not create movie"));
197 return false;
198 }
199
200 m_timer = new _wxQTTimer(m_movie, (wxMovieCtrl*) this);
201 wxASSERT(m_timer);
202
203 //get the real size of the movie
204 Rect outRect;
205 ::GetMovieNaturalBoundsRect (m_movie, &outRect);
206
207 m_bestSize.x = outRect.right - outRect.left;
208 m_bestSize.y = outRect.bottom - outRect.top;
209
210 //soldier in OnSize
211 this->Connect( wxID_ANY,
212 wxEVT_SIZE,
213 (wxObjectEventFunction) (wxEventFunction) (wxSizeEventFunction) &wxMovieCtrl::OnSize );
214
215 //reparent movie
216 #ifdef __WXMSW__
217 CreatePortAssociation(this->GetHWND(), NULL, 0L);
218 #endif
219 SetMovieGWorld(m_movie, (CGrafPtr)
220
221 #ifdef __WXMSW__
222 GetNativeWindowPort(this->GetHWND())
223 #else
224 this->GetHandle()
225 #endif
226 , nil);
227
228 return true;
229 }
230
231
232 bool wxMovieCtrl::Play()
233 {
234 ::StartMovie(m_movie);
235 m_timer->SetPaused(false);
236 m_timer->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
237 return ::GetMoviesError() == noErr;
238 }
239
240 bool wxMovieCtrl::Pause()
241 {
242 ::StopMovie(m_movie);
243 m_timer->SetPaused(true);
244 return ::GetMoviesError() == noErr;
245 }
246
247 bool wxMovieCtrl::Stop()
248 {
249 m_timer->SetPaused(false);
250
251 ::StopMovie(m_movie);
252 if(::GetMoviesError() != noErr)
253 return false;
254
255 ::GoToBeginningOfMovie(m_movie);
256 return ::GetMoviesError() == noErr;
257 }
258
259 double wxMovieCtrl::GetPlaybackRate()
260 {
261 return (double) (::GetMovieTimeScale(m_movie) / 0x10000f);
262 }
263
264 bool wxMovieCtrl::SetPlaybackRate(double dRate)
265 {
266 ::SetMovieTimeScale(m_movie, (Fixed) (dRate * 0x10000));
267 return ::GetMoviesError() == noErr;
268 }
269
270 #if wxUSE_DATETIME
271
272 bool wxMovieCtrl::Seek(const wxTimeSpan& where)
273 {
274 TimeRecord theTimeRecord;
275 theTimeRecord.value.lo = ((size_t)where.GetMilliseconds().ToLong()) * 10;
276 theTimeRecord.scale = ::GetMovieTimeScale(m_movie);
277 theTimeRecord.base = ::GetMovieTimeBase(m_movie);
278 ::SetMovieTime(m_movie, &theTimeRecord);
279
280 if (::GetMoviesError() != noErr)
281 return false;
282
283 return true;
284 }
285
286 wxTimeSpan wxMovieCtrl::Tell()
287 {
288 return (wxTimeSpan) ::GetMovieTime(m_movie, NULL);
289 }
290
291 wxTimeSpan wxMovieCtrl::Length()
292 {
293 return (wxTimeSpan) ::GetMovieDuration(m_movie);
294 }
295
296 #endif // wxUSE_DATETIME
297
298 wxMovieCtrlState wxMovieCtrl::GetState()
299 {
300 if( m_timer->IsRunning() == true )
301 return wxMOVIECTRL_STOPPED;
302
303 if ( m_timer->GetPaused() == false )
304 return wxMOVIECTRL_PLAYING;
305 else
306 return wxMOVIECTRL_PAUSED;
307 }
308
309 void wxMovieCtrl::Cleanup()
310 {
311 //soldier in OnSize
312 this->Disconnect( wxID_ANY,
313 wxEVT_SIZE,
314 (wxObjectEventFunction) (wxEventFunction) (wxSizeEventFunction) &wxMovieCtrl::OnSize );
315
316 delete m_timer;
317
318 StopMovie(m_movie);
319 DisposeMovie(m_movie);
320
321 //Note that ExitMovies() is not neccessary, but
322 //the docs are fuzzy on whether or not TerminateQTML is
323 ExitMovies();
324
325 #ifndef __WXMAC__
326 TerminateQTML();
327 #endif
328 }
329
330 wxMovieCtrl::~wxMovieCtrl()
331 {
332 if(m_bLoaded)
333 Cleanup();
334 }
335
336 wxSize wxMovieCtrl::DoGetBestSize() const
337 {
338 return m_bestSize;
339 }
340
341 void wxMovieCtrl::OnSize(wxSizeEvent& evt)
342 {
343 Rect theRect;
344 theRect.left = 0;
345 theRect.top = 0;
346 theRect.bottom = evt.GetSize().y;
347 theRect.right = evt.GetSize().x;
348
349 ::SetMovieBox(m_movie, &theRect);
350
351 wxASSERT(::GetMoviesError() == noErr);
352 evt.Skip();
353 }
354
355 #endif //wxUSE_MOVIECTRL