1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/mediactrl.cpp
3 // Purpose: wxMediaCtrl common code
4 // Author: Ryan Norton <wxprojects@comcast.net>
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // TODO: Platform specific backend defaults?
14 //===========================================================================
16 //===========================================================================
18 //---------------------------------------------------------------------------
20 //---------------------------------------------------------------------------
22 #include "wx/wxprec.h"
35 #include "wx/mediactrl.h"
37 //===========================================================================
41 //===========================================================================
43 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
44 // RTTI and Event implementations
45 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
47 IMPLEMENT_CLASS(wxMediaCtrl
, wxControl
)
48 DEFINE_EVENT_TYPE(wxEVT_MEDIA_STATECHANGED
)
49 DEFINE_EVENT_TYPE(wxEVT_MEDIA_PLAY
)
50 DEFINE_EVENT_TYPE(wxEVT_MEDIA_PAUSE
)
51 IMPLEMENT_CLASS(wxMediaBackend
, wxObject
)
52 IMPLEMENT_DYNAMIC_CLASS(wxMediaEvent
, wxEvent
)
53 DEFINE_EVENT_TYPE(wxEVT_MEDIA_FINISHED
)
54 DEFINE_EVENT_TYPE(wxEVT_MEDIA_LOADED
)
55 DEFINE_EVENT_TYPE(wxEVT_MEDIA_STOP
)
57 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
61 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
63 //---------------------------------------------------------------------------
64 // wxMediaBackend Destructor
66 // This is here because the DARWIN gcc compiler badly screwed up and
67 // needs the destructor implementation in the source
68 //---------------------------------------------------------------------------
69 wxMediaBackend::~wxMediaBackend()
73 //---------------------------------------------------------------------------
74 // wxMediaCtrl::Create (file version)
75 // wxMediaCtrl::Create (URL version)
77 // Searches for a backend that is installed on the system (backends
78 // starting with lower characters in the alphabet are given priority),
79 // and creates the control from it
81 // This searches by searching the global RTTI hashtable, class by class,
82 // attempting to call CreateControl on each one found that is a derivative
83 // of wxMediaBackend - if it succeeded Create returns true, otherwise
84 // it keeps iterating through the hashmap.
85 //---------------------------------------------------------------------------
86 bool wxMediaCtrl::Create(wxWindow
* parent
, wxWindowID id
,
87 const wxString
& fileName
,
91 const wxString
& szBackend
,
92 const wxValidator
& validator
,
95 if(!szBackend
.empty())
97 wxClassInfo
* pClassInfo
= wxClassInfo::FindClass(szBackend
);
99 if(!pClassInfo
|| !DoCreate(pClassInfo
, parent
, id
,
100 pos
, size
, style
, validator
, name
))
106 if (!fileName
.empty())
116 SetInitialSize(size
);
121 wxClassInfo::const_iterator it
= wxClassInfo::begin_classinfo();
123 const wxClassInfo
* classInfo
;
125 while((classInfo
= NextBackend(&it
)) != NULL
)
127 wxLogMessage( classInfo
->GetClassName() );
128 if(!DoCreate(classInfo
, parent
, id
,
129 pos
, size
, style
, validator
, name
))
132 if (!fileName
.empty())
136 SetInitialSize(size
);
144 SetInitialSize(size
);
154 bool wxMediaCtrl::Create(wxWindow
* parent
, wxWindowID id
,
155 const wxURI
& location
,
159 const wxString
& szBackend
,
160 const wxValidator
& validator
,
161 const wxString
& name
)
163 if(!szBackend
.empty())
165 wxClassInfo
* pClassInfo
= wxClassInfo::FindClass(szBackend
);
166 if(!pClassInfo
|| !DoCreate(pClassInfo
, parent
, id
,
167 pos
, size
, style
, validator
, name
))
180 SetInitialSize(size
);
185 wxClassInfo::const_iterator it
= wxClassInfo::begin_classinfo();
187 const wxClassInfo
* classInfo
;
189 while((classInfo
= NextBackend(&it
)) != NULL
)
191 if(!DoCreate(classInfo
, parent
, id
,
192 pos
, size
, style
, validator
, name
))
197 SetInitialSize(size
);
209 //---------------------------------------------------------------------------
210 // wxMediaCtrl::DoCreate
212 // Attempts to create the control from a backend
213 //---------------------------------------------------------------------------
214 bool wxMediaCtrl::DoCreate(const wxClassInfo
* classInfo
,
215 wxWindow
* parent
, wxWindowID id
,
219 const wxValidator
& validator
,
220 const wxString
& name
)
222 m_imp
= (wxMediaBackend
*)classInfo
->CreateObject();
224 if( m_imp
->CreateControl(this, parent
, id
, pos
, size
,
225 style
, validator
, name
) )
234 //---------------------------------------------------------------------------
235 // wxMediaCtrl::NextBackend (static)
238 // Search through the RTTI hashmap one at a
239 // time, attempting to create each derivative
243 // STL isn't compatible with and will have a compilation error
244 // on a wxNode, however, wxHashTable::compatibility_iterator is
245 // incompatible with the old 2.4 stable version - but since
246 // we're in 2.5+ only we don't need to worry about the new version
247 //---------------------------------------------------------------------------
248 const wxClassInfo
* wxMediaCtrl::NextBackend(wxClassInfo::const_iterator
* it
)
250 for ( wxClassInfo::const_iterator end
= wxClassInfo::end_classinfo();
251 *it
!= end
; ++(*it
) )
253 const wxClassInfo
* classInfo
= **it
;
254 if ( classInfo
->IsKindOf(CLASSINFO(wxMediaBackend
)) &&
255 classInfo
!= CLASSINFO(wxMediaBackend
) )
262 // Nope - couldn't successfully find one... fail
268 //---------------------------------------------------------------------------
269 // wxMediaCtrl Destructor
271 // Free up the backend if it exists
272 //---------------------------------------------------------------------------
273 wxMediaCtrl::~wxMediaCtrl()
279 //---------------------------------------------------------------------------
280 // wxMediaCtrl::Load (file version)
281 // wxMediaCtrl::Load (URL version)
282 // wxMediaCtrl::Load (URL & Proxy version)
283 // wxMediaCtrl::Load (wxInputStream version)
285 // Here we call load of the backend - keeping
286 // track of whether it was successful or not - which
287 // will determine which later method calls work
288 //---------------------------------------------------------------------------
289 bool wxMediaCtrl::Load(const wxString
& fileName
)
292 return (m_bLoaded
= m_imp
->Load(fileName
));
296 bool wxMediaCtrl::Load(const wxURI
& location
)
299 return (m_bLoaded
= m_imp
->Load(location
));
303 bool wxMediaCtrl::Load(const wxURI
& location
, const wxURI
& proxy
)
306 return (m_bLoaded
= m_imp
->Load(location
, proxy
));
310 //---------------------------------------------------------------------------
312 // wxMediaCtrl::Pause
314 // wxMediaCtrl::GetPlaybackRate
315 // wxMediaCtrl::SetPlaybackRate
316 // wxMediaCtrl::Seek --> SetPosition
317 // wxMediaCtrl::Tell --> GetPosition
318 // wxMediaCtrl::Length --> GetDuration
319 // wxMediaCtrl::GetState
320 // wxMediaCtrl::DoGetBestSize
321 // wxMediaCtrl::SetVolume
322 // wxMediaCtrl::GetVolume
323 // wxMediaCtrl::ShowInterface
324 // wxMediaCtrl::GetDownloadProgress
325 // wxMediaCtrl::GetDownloadTotal
327 // 1) Check to see whether the backend exists and is loading
328 // 2) Call the backend's version of the method, returning success
329 // if the backend's version succeeds
330 //---------------------------------------------------------------------------
331 bool wxMediaCtrl::Play()
333 if(m_imp
&& m_bLoaded
)
334 return m_imp
->Play();
338 bool wxMediaCtrl::Pause()
340 if(m_imp
&& m_bLoaded
)
341 return m_imp
->Pause();
345 bool wxMediaCtrl::Stop()
347 if(m_imp
&& m_bLoaded
)
348 return m_imp
->Stop();
352 double wxMediaCtrl::GetPlaybackRate()
354 if(m_imp
&& m_bLoaded
)
355 return m_imp
->GetPlaybackRate();
359 bool wxMediaCtrl::SetPlaybackRate(double dRate
)
361 if(m_imp
&& m_bLoaded
)
362 return m_imp
->SetPlaybackRate(dRate
);
366 wxFileOffset
wxMediaCtrl::Seek(wxFileOffset where
, wxSeekMode mode
)
376 offset
= Length() - where
;
378 // case wxFromCurrent:
380 offset
= Tell() + where
;
384 if(m_imp
&& m_bLoaded
&& m_imp
->SetPosition(offset
))
386 return wxInvalidOffset
;
389 wxFileOffset
wxMediaCtrl::Tell()
391 if(m_imp
&& m_bLoaded
)
392 return (wxFileOffset
) m_imp
->GetPosition().ToLong();
393 return wxInvalidOffset
;
396 wxFileOffset
wxMediaCtrl::Length()
398 if(m_imp
&& m_bLoaded
)
399 return (wxFileOffset
) m_imp
->GetDuration().ToLong();
400 return wxInvalidOffset
;
403 wxMediaState
wxMediaCtrl::GetState()
405 if(m_imp
&& m_bLoaded
)
406 return m_imp
->GetState();
407 return wxMEDIASTATE_STOPPED
;
410 wxSize
wxMediaCtrl::DoGetBestSize() const
413 return m_imp
->GetVideoSize();
417 double wxMediaCtrl::GetVolume()
419 if(m_imp
&& m_bLoaded
)
420 return m_imp
->GetVolume();
424 bool wxMediaCtrl::SetVolume(double dVolume
)
426 if(m_imp
&& m_bLoaded
)
427 return m_imp
->SetVolume(dVolume
);
431 bool wxMediaCtrl::ShowPlayerControls(wxMediaCtrlPlayerControls flags
)
434 return m_imp
->ShowPlayerControls(flags
);
438 wxFileOffset
wxMediaCtrl::GetDownloadProgress()
440 if(m_imp
&& m_bLoaded
)
441 return (wxFileOffset
) m_imp
->GetDownloadProgress().ToLong();
442 return wxInvalidOffset
;
445 wxFileOffset
wxMediaCtrl::GetDownloadTotal()
447 if(m_imp
&& m_bLoaded
)
448 return (wxFileOffset
) m_imp
->GetDownloadTotal().ToLong();
449 return wxInvalidOffset
;
452 //---------------------------------------------------------------------------
453 // wxMediaCtrl::DoMoveWindow
455 // 1) Call parent's version so that our control's window moves where
457 // 2) If the backend exists and is loaded, move the video
458 // of the media to where our control's window is now located
459 //---------------------------------------------------------------------------
460 void wxMediaCtrl::DoMoveWindow(int x
, int y
, int w
, int h
)
462 wxControl::DoMoveWindow(x
,y
,w
,h
);
465 m_imp
->Move(x
, y
, w
, h
);
468 //---------------------------------------------------------------------------
469 // wxMediaCtrl::MacVisibilityChanged
470 //---------------------------------------------------------------------------
472 void wxMediaCtrl::MacVisibilityChanged()
474 wxControl::MacVisibilityChanged();
477 m_imp
->MacVisibilityChanged();
481 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
483 // wxMediaBackendCommonBase
485 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
487 void wxMediaBackendCommonBase::NotifyMovieSizeChanged()
489 // our best size changed after opening a new file
490 m_ctrl
->InvalidateBestSize();
491 m_ctrl
->SetSize(m_ctrl
->GetSize());
493 // if the parent of the control has a sizer ask it to refresh our size
494 wxWindow
* const parent
= m_ctrl
->GetParent();
495 if ( parent
->GetSizer() )
497 m_ctrl
->GetParent()->Layout();
498 m_ctrl
->GetParent()->Refresh();
499 m_ctrl
->GetParent()->Update();
503 void wxMediaBackendCommonBase::NotifyMovieLoaded()
505 NotifyMovieSizeChanged();
507 // notify about movie being fully loaded
508 QueueEvent(wxEVT_MEDIA_LOADED
);
511 bool wxMediaBackendCommonBase::SendStopEvent()
513 wxMediaEvent
theEvent(wxEVT_MEDIA_STOP
, m_ctrl
->GetId());
515 return !m_ctrl
->ProcessEvent(theEvent
) || theEvent
.IsAllowed();
518 void wxMediaBackendCommonBase::QueueEvent(wxEventType evtType
)
520 wxMediaEvent
theEvent(evtType
, m_ctrl
->GetId());
521 m_ctrl
->AddPendingEvent(theEvent
);
524 void wxMediaBackendCommonBase::QueuePlayEvent()
526 QueueEvent(wxEVT_MEDIA_STATECHANGED
);
527 QueueEvent(wxEVT_MEDIA_PLAY
);
530 void wxMediaBackendCommonBase::QueuePauseEvent()
532 QueueEvent(wxEVT_MEDIA_STATECHANGED
);
533 QueueEvent(wxEVT_MEDIA_PAUSE
);
536 void wxMediaBackendCommonBase::QueueStopEvent()
538 QueueEvent(wxEVT_MEDIA_STATECHANGED
);
539 QueueEvent(wxEVT_MEDIA_STOP
);
544 // Force link default backends in -
545 // see http://wiki.wxwidgets.org/wiki.pl?RTTI
547 #include "wx/html/forcelnk.h"
549 #ifdef __WXMSW__ // MSW has huge backends so we do it seperately
550 FORCE_LINK(wxmediabackend_am
)
551 FORCE_LINK(wxmediabackend_wmp10
)
553 FORCE_LINK(basewxmediabackends
)
556 #endif //wxUSE_MEDIACTRL