1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/moviectrl.cpp
3 // Purpose: wxMovieCtrl MSW
4 // Author: Ryan Norton <wxprojects@comcast.net>
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 //#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 //#pragma implementation "moviectrl.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/moviectrl.h"
26 #include "wx/msw/ole/oleutils.h" //for wxBasicString
30 IMPLEMENT_CLASS(wxMovieCtrl
, wxControl
);
31 IMPLEMENT_DYNAMIC_CLASS(wxMovieEvent
, wxEvent
);
32 DEFINE_EVENT_TYPE(wxEVT_MOVIE_FINISHED
);
34 #define SAFE_RELEASE(x) { if (x) x->Release(); x = NULL; }
36 #define WM_GRAPHNOTIFY WM_USER+13
39 #define wxDSVERIFY(x) wxASSERT( SUCCEEDED ((x)) )
41 #define wxDSVERIFY(x) (x)
44 //it's there someplace :)
45 extern "C" WXDLLIMPEXP_BASE HWND
46 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
48 bool wxMovieCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxString
& fileName
,
49 const wxString
& label
, const wxPoint
& pos
, const wxSize
& size
,
50 long style
, const wxString
& name
)
52 //do some window stuff - ORDER IS IMPORTANT
54 if ( !wxControl::Create(parent
, id
, pos
, size
, wxNO_BORDER
| wxCLIP_CHILDREN
, wxDefaultValidator
, name
) )
57 //Set our background color to black by default
58 SetBackgroundColour(*wxBLACK
);
60 wxControl::SetLabel(label
);
74 bool wxMovieCtrl::Load(const wxString
& fileName
)
80 IGraphBuilder
*& pGB
= (IGraphBuilder
*&) m_pGB
;
81 IMediaControl
*& pMC
= (IMediaControl
*&) m_pMC
;
82 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
83 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
84 IBasicAudio
*& pBA
= (IBasicAudio
*&) m_pBA
;
85 IBasicVideo
*& pBV
= (IBasicVideo
*&) m_pBV
;
86 IMediaSeeking
*& pMS
= (IMediaSeeking
*&) m_pMS
;
88 //create our filter graph
89 CoCreateInstance(CLSID_FilterGraph
, NULL
, CLSCTX_INPROC_SERVER
,
90 IID_IGraphBuilder
, (void**)&pGB
);
92 //load the graph & render
93 if (FAILED(pGB
->RenderFile(fileName
.wc_str(wxConvLocal
), NULL
)))
95 wxFAIL_MSG("Could not load movie!");
99 //get the interfaces, all of them
100 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaControl
, (void**)&pMC
) );
101 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaEventEx
, (void**)&pME
) );
102 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaSeeking
, (void**)&pMS
) );
103 wxDSVERIFY( pGB
->QueryInterface(IID_IVideoWindow
, (void**)&pVW
) );
104 wxDSVERIFY( pGB
->QueryInterface(IID_IBasicAudio
, (void**)&pBA
) );
105 wxDSVERIFY( pGB
->QueryInterface(IID_IBasicVideo
, (void**)&pBV
) );
107 //get the _actual_ size of the movie & remember it
108 long nX
, nY
, nSX
, nSY
;
109 if (FAILED(pVW
->GetWindowPosition(&nX
,&nY
,&nSX
,&nSY
)))
115 this->Connect( wxID_ANY
,
117 (wxObjectEventFunction
) (wxEventFunction
) (wxSizeEventFunction
) &wxMovieCtrl::OnSize
);
125 wxDSVERIFY( pVW
->put_Owner((OAHWND
)this->GetHandle()) );
126 wxDSVERIFY( pVW
->put_WindowStyle(WS_CHILD
| WS_CLIPSIBLINGS
) );
127 // wxDSVERIFY( pME->SetNotifyWindow((OAHWND)this->GetHandle(), WM_GRAPHNOTIFY, 0) );
128 wxDSVERIFY( pVW
->put_Visible(OATRUE
) ); //OATRUE actually == -1 :)
131 //set the time format
132 wxDSVERIFY( pMS
->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME
) );
135 SetLabel(GetLabel());
141 void wxMovieCtrl::SetLabel(const wxString
& label
)
143 wxControl::SetLabel(label
);
145 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
147 //wxBasicString will have a null string on an
148 //empty wxString - gotta love those workarounds!!
149 if(!label
.empty() && m_bVideo
)
151 wxBasicString
theBasicString(label
.mb_str());
152 wxDSVERIFY( pVW
->put_Caption(theBasicString
.Get()) );
156 bool wxMovieCtrl::Play()
158 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Run() );
161 bool wxMovieCtrl::Pause()
163 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Pause() );
166 bool wxMovieCtrl::Stop()
168 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Stop() );
173 bool wxMovieCtrl::Seek(const wxTimeSpan
& where
)
175 //DS uses 100 nanos - so we need a 10 mult
176 LONGLONG pos
= ((size_t)where
.GetMilliseconds().ToLong()) * 10;
178 return SUCCEEDED( ((IMediaSeeking
*&)m_pMS
)->SetPositions(
180 AM_SEEKING_AbsolutePositioning
,
182 AM_SEEKING_NoPositioning
186 wxTimeSpan
wxMovieCtrl::Tell()
188 LONGLONG outCur
, outStop
;
189 wxDSVERIFY( ((IMediaSeeking
*&)m_pMS
)->GetPositions(&outCur
, &outStop
) );
194 wxTimeSpan
wxMovieCtrl::Length()
196 LONGLONG outDuration
;
197 wxDSVERIFY( ((IMediaSeeking
*&)m_pMS
)->GetDuration(&outDuration
) );
202 #endif // wxUSE_DATETIME
204 wxMovieCtrlState
wxMovieCtrl::GetState()
207 OAFilterState theState
;
208 hr
= ((IMediaControl
*&)m_pMC
)->GetState(INFINITE
, &theState
);
210 wxASSERT( SUCCEEDED(hr
) );
212 //MSW state is the same as ours
214 //State_Paused = State_Stopped + 1,
215 //State_Running = State_Paused + 1
217 return (wxMovieCtrlState
) theState
;
220 double wxMovieCtrl::GetPlaybackRate()
223 wxDSVERIFY( ((IMediaSeeking
*&)m_pMS
)->GetRate(&dRate
) );
227 bool wxMovieCtrl::SetPlaybackRate(double dRate
)
229 return SUCCEEDED( ((IMediaSeeking
*&)m_pMS
)->SetRate(dRate
) );
232 WXLRESULT
wxMovieCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
235 // IMediaControl*& pMC = (IMediaControl*&) m_pMC;
236 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
237 // IMediaSeeking*& pMS = (IMediaSeeking*&) m_pMS;
239 if (nMsg
== WM_GRAPHNOTIFY
)
241 LONG evCode
, evParam1
, evParam2
;
244 // Process all queued events
245 while(SUCCEEDED(pME
->GetEvent(&evCode
, (LONG_PTR
*) &evParam1
,
246 (LONG_PTR
*) &evParam2
, 0)
250 // Free memory associated with callback, since we're not using it
251 hr
= pME
->FreeEventParams(evCode
, evParam1
, evParam2
);
253 // If this is the end of the clip, notify handler
254 if(EC_COMPLETE
== evCode
)
256 wxMovieEvent
theEvent(wxEVT_MOVIE_FINISHED
, this->GetId());
257 GetParent()->ProcessEvent(theEvent
);
261 // Reset to first frame of movie
262 hr = pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
263 NULL, AM_SEEKING_NoPositioning);
267 // for filters that don't support seeking do a rewind
271 wxString::Format(TEXT("Failed(0x%08lx) to stop media clip!\r\n"), hr)
276 hr = hr = pMC->Run();
281 wxString::Format(TEXT("Failed(0x%08lx) to reset media clip!\r\n"), hr)
289 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
291 //pass the event to our parent
292 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
295 void wxMovieCtrl::Cleanup()
298 this->Disconnect( wxID_ANY
,
300 (wxObjectEventFunction
) (wxEventFunction
) (wxSizeEventFunction
) &wxMovieCtrl::OnSize
);
303 IGraphBuilder
*& pGB
= (IGraphBuilder
*&) m_pGB
;
304 IMediaControl
*& pMC
= (IMediaControl
*&) m_pMC
;
305 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
306 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
307 IBasicAudio
*& pBA
= (IBasicAudio
*&) m_pBA
;
308 IBasicVideo
*& pBV
= (IBasicVideo
*&) m_pBV
;
309 IMediaSeeking
*& pMS
= (IMediaSeeking
*&) m_pMS
;
311 // Hide then disown the window
314 pVW
->put_Visible(OAFALSE
); //OSFALSE == 0
315 pVW
->put_Owner(NULL
);
318 // Release and zero DirectShow interfaces
328 wxMovieCtrl::~wxMovieCtrl()
334 wxSize
wxMovieCtrl::DoGetBestSize() const
339 void wxMovieCtrl::OnSize(wxSizeEvent
& evt
)
341 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
342 wxDSVERIFY( pVW
->SetWindowPosition(0, 0, evt
.GetSize().GetWidth(), evt
.GetSize().GetHeight()) );
347 #endif //wxUSE_MOVIECTRL