]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/mediactrl.mm
first version of osx_cocoa implementation
[wxWidgets.git] / src / osx / cocoa / mediactrl.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/mediactrl.cpp
3 // Purpose: Built-in Media Backends for Cocoa
4 // Author: Ryan Norton <wxprojects@comcast.net>
5 // Modified by:
6 // Created: 02/03/05
7 // RCS-ID: $Id: mediactrl.mm 39285 2006-05-23 11:04:37Z ABX $
8 // Copyright: (c) 2004-2005 Ryan Norton, (c) 2005 David Elliot
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 //===========================================================================
13 // DECLARATIONS
14 //===========================================================================
15
16 //---------------------------------------------------------------------------
17 // Pre-compiled header stuff
18 //---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 //---------------------------------------------------------------------------
28 // Compilation guard
29 //---------------------------------------------------------------------------
30 #if wxUSE_MEDIACTRL
31
32 #include "wx/mediactrl.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/timer.h"
36 #endif
37
38 #include "wx/osx/private.h"
39
40 //===========================================================================
41 // BACKEND DECLARATIONS
42 //===========================================================================
43
44 //---------------------------------------------------------------------------
45 //
46 // wxQTMediaBackend
47 //
48 //---------------------------------------------------------------------------
49
50 //---------------------------------------------------------------------------
51 // QT Includes
52 //---------------------------------------------------------------------------
53 #include <QTKit/QTKit.h>
54
55 #include "wx/cocoa/autorelease.h"
56 #include "wx/cocoa/string.h"
57
58 #import <AppKit/NSMovie.h>
59 #import <AppKit/NSMovieView.h>
60
61
62 class WXDLLIMPEXP_MEDIA wxQTMediaBackend : public wxMediaBackend
63 {
64 public:
65
66 wxQTMediaBackend();
67 ~wxQTMediaBackend();
68
69 virtual bool CreateControl(wxControl* ctrl, wxWindow* parent,
70 wxWindowID id,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxValidator& validator,
75 const wxString& name);
76
77 virtual bool Play();
78 virtual bool Pause();
79 virtual bool Stop();
80
81 virtual bool Load(const wxString& fileName);
82 virtual bool Load(const wxURI& location);
83
84 virtual wxMediaState GetState();
85
86 virtual bool SetPosition(wxLongLong where);
87 virtual wxLongLong GetPosition();
88 virtual wxLongLong GetDuration();
89
90 virtual void Move(int x, int y, int w, int h);
91 wxSize GetVideoSize() const;
92
93 virtual double GetPlaybackRate();
94 virtual bool SetPlaybackRate(double dRate);
95
96 virtual double GetVolume();
97 virtual bool SetVolume(double dVolume);
98
99 void Cleanup();
100 void FinishLoad();
101
102 virtual bool ShowPlayerControls(wxMediaCtrlPlayerControls flags);
103 private:
104 void DoShowPlayerControls(wxMediaCtrlPlayerControls flags);
105
106 wxSize m_bestSize; //Original movie size
107 QTMovie* m_movie; //QTMovie handle/instance
108 QTMovieView* m_movieview; //QTMovieView instance
109 wxControl* m_ctrl; //Parent control
110
111 wxMediaCtrlPlayerControls m_interfaceflags; // Saved interface flags
112
113 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend);
114 };
115
116
117 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
118 //
119 // wxQTMediaBackend
120 //
121 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
122
123 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend, wxMediaBackend);
124
125 //Time between timer calls
126 #define MOVIE_DELAY 100
127
128 // --------------------------------------------------------------------------
129 // wxQTTimer - Handle Asyncronous Playing
130 // --------------------------------------------------------------------------
131 class _wxQTTimer : public wxTimer
132 {
133 public:
134 _wxQTTimer(QTMovie movie, wxQTMediaBackend* parent) :
135 m_movie(movie), m_bPaused(false), m_parent(parent)
136 {
137 }
138
139 ~_wxQTTimer()
140 {
141 }
142
143 bool GetPaused() {return m_bPaused;}
144 void SetPaused(bool bPaused) {m_bPaused = bPaused;}
145
146 //-----------------------------------------------------------------------
147 // _wxQTTimer::Notify
148 //
149 // 1) Checks to see if the movie is done, and if not continues
150 // streaming the movie
151 // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
152 // the movie.
153 //-----------------------------------------------------------------------
154 void Notify()
155 {
156 #if 0
157 if (!m_bPaused)
158 {
159 if(!IsMovieDone(m_movie))
160 MoviesTask(m_movie, MOVIE_DELAY);
161 else
162 {
163 wxMediaEvent theEvent(wxEVT_MEDIA_STOP,
164 m_parent->m_ctrl->GetId());
165 m_parent->m_ctrl->GetEventHandler()->ProcessEvent(theEvent);
166
167 if(theEvent.IsAllowed())
168 {
169 Stop();
170 m_parent->Stop();
171 wxASSERT(::GetMoviesError() == noErr);
172
173 //send the event to our child
174 wxMediaEvent theEvent(wxEVT_MEDIA_FINISHED,
175 m_parent->m_ctrl->GetId());
176 m_parent->m_ctrl->GetEventHandler()->ProcessEvent(theEvent);
177 }
178 }
179 }
180 #endif
181 }
182
183 protected:
184 QTMovie m_movie; //Our movie instance
185 bool m_bPaused; //Whether we are paused or not
186 wxQTMediaBackend* m_parent; //Backend pointer
187 };
188
189 //---------------------------------------------------------------------------
190 // wxQTMediaBackend Constructor
191 //
192 // Sets m_timer to NULL signifying we havn't loaded anything yet
193 //---------------------------------------------------------------------------
194 wxQTMediaBackend::wxQTMediaBackend() :
195 m_interfaceflags(wxMEDIACTRLPLAYERCONTROLS_NONE),
196 m_movie(nil), m_movieview(nil), m_ctrl(NULL)
197 {
198 }
199
200 //---------------------------------------------------------------------------
201 // wxQTMediaBackend Destructor
202 //
203 // 1) Cleans up the QuickTime movie instance
204 // 2) Decrements the QuickTime reference counter - if this reaches
205 // 0, QuickTime shuts down
206 // 3) Decrements the QuickTime Windows Media Layer reference counter -
207 // if this reaches 0, QuickTime shuts down the Windows Media Layer
208 //---------------------------------------------------------------------------
209 wxQTMediaBackend::~wxQTMediaBackend()
210 {
211 [m_movie release];
212 }
213
214 //---------------------------------------------------------------------------
215 // wxQTMediaBackend::CreateControl
216 //
217 // 1) Intializes QuickTime
218 // 2) Creates the control window
219 //---------------------------------------------------------------------------
220 bool wxQTMediaBackend::CreateControl(wxControl* inctrl, wxWindow* parent,
221 wxWindowID wid,
222 const wxPoint& pos,
223 const wxSize& size,
224 long style,
225 const wxValidator& validator,
226 const wxString& name)
227 {
228 wxMediaCtrl* mediactrl = (wxMediaCtrl*) inctrl;
229
230 //
231 // Create window
232 // By default wxWindow(s) is created with a border -
233 // so we need to get rid of those
234 //
235 // Since we don't have a child window like most other
236 // backends, we don't need wxCLIP_CHILDREN
237 //
238 if ( !mediactrl->wxControl::Create(
239 parent, wid, pos, size,
240 wxWindow::MacRemoveBordersFromStyle(style),
241 validator, name))
242 {
243 return false;
244 }
245
246 NSRect r = wxOSXGetFrameForControl( mediactrl, pos , size ) ;
247 QTMovieView* theView = [[QTMovieView alloc] initWithFrame: r];
248
249 wxWidgetCocoaImpl* impl = new wxWidgetCocoaImpl(mediactrl,theView);
250 mediactrl->SetPeer(impl);
251
252 m_movieview = theView;
253 // will be set up after load
254 [theView setControllerVisible:NO];
255
256 m_ctrl = mediactrl;
257 return true;
258 }
259
260 bool wxQTMediaBackend::Load(const wxString& fileName)
261 {
262 return Load(
263 wxURI(
264 wxString( wxT("file://") ) + fileName
265 )
266 );
267 }
268
269 bool wxQTMediaBackend::Load(const wxURI& location)
270 {
271 wxString theURI = location.BuildURI();
272
273 QTMovie* movie = [[QTMovie alloc] initWithURL: [NSURL URLWithString: wxNSStringWithWxString(theURI)] error: nil ];
274
275 [m_movie release];
276 m_movie = movie;
277
278 [m_movieview setMovie:movie];
279
280 DoShowPlayerControls(m_interfaceflags);
281
282 FinishLoad();
283
284 return movie != nil;
285 }
286
287 void wxQTMediaBackend::FinishLoad()
288 {
289 NSRect r =[m_movieview movieBounds];
290 m_bestSize.x = r.size.width;
291 m_bestSize.y = r.size.height;
292
293 m_ctrl->InvalidateBestSize();
294 m_ctrl->GetParent()->Layout();
295 m_ctrl->GetParent()->Refresh();
296 m_ctrl->GetParent()->Update();
297 }
298
299 bool wxQTMediaBackend::Play()
300 {
301 [m_movieview play:nil];
302 return true;
303 }
304
305 bool wxQTMediaBackend::Pause()
306 {
307 [m_movieview pause:nil];
308 return true;
309 }
310
311 bool wxQTMediaBackend::Stop()
312 {
313 [m_movieview pause:nil];
314 [m_movieview gotoBeginning:nil];
315 return true;
316 }
317
318 double wxQTMediaBackend::GetVolume()
319 {
320 return [m_movie volume];
321 }
322
323 bool wxQTMediaBackend::SetVolume(double dVolume)
324 {
325 [m_movie setVolume:dVolume];
326 return true;
327 }
328 double wxQTMediaBackend::GetPlaybackRate()
329 {
330 return [m_movie rate];
331 }
332
333 bool wxQTMediaBackend::SetPlaybackRate(double dRate)
334 {
335 [m_movie setRate:dRate];
336 return true;
337 }
338
339 bool wxQTMediaBackend::SetPosition(wxLongLong where)
340 {
341 QTTime position;
342 position = [m_movie currentTime];
343 position.timeValue = (where.GetValue() / 1000.0) * position.timeScale;
344 [m_movie setCurrentTime:position];
345 return true;
346 }
347
348 wxLongLong wxQTMediaBackend::GetPosition()
349 {
350 QTTime position = [m_movie currentTime];
351 return ((double) position.timeValue) / position.timeScale * 1000;
352 }
353
354 wxLongLong wxQTMediaBackend::GetDuration()
355 {
356 QTTime duration = [m_movie duration];
357 return ((double) duration.timeValue) / duration.timeScale * 1000;
358 }
359
360 wxMediaState wxQTMediaBackend::GetState()
361 {
362 /*
363 if ( !m_timer || (m_timer->IsRunning() == false &&
364 m_timer->GetPaused() == false) )
365 return wxMEDIASTATE_STOPPED;
366
367 if( m_timer->IsRunning() == true )
368 return wxMEDIASTATE_PLAYING;
369 else
370 */
371 return wxMEDIASTATE_PAUSED;
372 }
373
374 void wxQTMediaBackend::Cleanup()
375 {
376 [[m_movieview movie] release];
377 [m_movieview setMovie:NULL];
378 }
379
380 wxSize wxQTMediaBackend::GetVideoSize() const
381 {
382 return m_bestSize;
383 }
384
385 void wxQTMediaBackend::Move(int x, int y, int w, int h)
386 {
387 }
388
389 bool wxQTMediaBackend::ShowPlayerControls(wxMediaCtrlPlayerControls flags)
390 {
391 if ( m_interfaceflags != flags )
392 DoShowPlayerControls(flags);
393
394 m_interfaceflags = flags;
395 return true;
396 }
397
398 void wxQTMediaBackend::DoShowPlayerControls(wxMediaCtrlPlayerControls flags)
399 {
400 if (flags == wxMEDIACTRLPLAYERCONTROLS_NONE )
401 {
402 [m_movieview setControllerVisible:NO];
403 }
404 else
405 {
406 [m_movieview setStepButtonsVisible:(flags & wxMEDIACTRLPLAYERCONTROLS_STEP) ? YES:NO];
407 [m_movieview setVolumeButtonVisible:(flags & wxMEDIACTRLPLAYERCONTROLS_VOLUME) ? YES:NO];
408 }
409 }
410
411 //in source file that contains stuff you don't directly use
412 #include "wx/html/forcelnk.h"
413 FORCE_LINK_ME(basewxmediabackends);
414
415 #endif //wxUSE_MEDIACTRL