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"
23 #define wxUSE_MOVIECTRL 1
27 #include "wx/moviectrl.h"
28 #include "wx/msw/ole/oleutils.h" //for wxBasicString
32 IMPLEMENT_CLASS(wxMovieCtrl
, wxControl
);
33 IMPLEMENT_DYNAMIC_CLASS(wxMovieEvent
, wxEvent
);
34 DEFINE_EVENT_TYPE(wxEVT_MOVIE_FINISHED
);
36 #define SAFE_RELEASE(x) { if (x) x->Release(); x = NULL; }
38 #define WM_GRAPHNOTIFY WM_USER+13
41 #define wxDSVERIFY(x) wxASSERT( SUCCEEDED ((x)) )
43 #define wxDSVERIFY(x) (x)
46 //it's there someplace :)
47 extern "C" WXDLLIMPEXP_BASE HWND
48 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
50 bool wxMovieCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxString
& fileName
,
51 const wxString
& label
, const wxPoint
& pos
, const wxSize
& size
,
52 long style
, const wxString
& name
)
54 //do some window stuff - ORDER IS IMPORTANT
56 if ( !wxControl::Create(parent
, id
, pos
, size
, wxNO_BORDER
| wxCLIP_CHILDREN
, wxDefaultValidator
, name
) )
59 //Set our background color to black by default
60 SetBackgroundColour(*wxBLACK
);
73 wxControl::SetLabel(label
);
78 bool wxMovieCtrl::Load(const wxString
& fileName
)
84 IGraphBuilder
*& pGB
= (IGraphBuilder
*&) m_pGB
;
85 IMediaControl
*& pMC
= (IMediaControl
*&) m_pMC
;
86 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
87 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
88 IBasicAudio
*& pBA
= (IBasicAudio
*&) m_pBA
;
89 IBasicVideo
*& pBV
= (IBasicVideo
*&) m_pBV
;
90 IMediaSeeking
*& pMS
= (IMediaSeeking
*&) m_pMS
;
92 //create our filter graph
93 CoCreateInstance(CLSID_FilterGraph
, NULL
, CLSCTX_INPROC_SERVER
,
94 IID_IGraphBuilder
, (void**)&pGB
);
96 //load the graph & render
97 if (FAILED(pGB
->RenderFile(fileName
.wc_str(wxConvLocal
), NULL
)))
99 wxFAIL_MSG("Could not load movie!");
103 //get the interfaces, all of them
104 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaControl
, (void**)&pMC
) );
105 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaEventEx
, (void**)&pME
) );
106 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaSeeking
, (void**)&pMS
) );
107 wxDSVERIFY( pGB
->QueryInterface(IID_IVideoWindow
, (void**)&pVW
) );
108 wxDSVERIFY( pGB
->QueryInterface(IID_IBasicAudio
, (void**)&pBA
) );
109 wxDSVERIFY( pGB
->QueryInterface(IID_IBasicVideo
, (void**)&pBV
) );
111 //get the _actual_ size of the movie & remember it
112 long nX
, nY
, nSX
, nSY
;
113 if (FAILED(pVW
->GetWindowPosition(&nX
,&nY
,&nSX
,&nSY
)))
119 this->Connect( wxID_ANY
,
121 (wxObjectEventFunction
) (wxEventFunction
) (wxSizeEventFunction
) &wxMovieCtrl::OnSize
);
129 wxDSVERIFY( pVW
->put_Owner((OAHWND
)this->GetHandle()) );
130 wxDSVERIFY( pVW
->put_WindowStyle(WS_CHILD
| WS_CLIPSIBLINGS
) );
131 // wxDSVERIFY( pME->SetNotifyWindow((OAHWND)this->GetHandle(), WM_GRAPHNOTIFY, 0) );
132 wxDSVERIFY( pVW
->put_Visible(OATRUE
) ); //OATRUE actually == -1 :)
135 //set the time format
136 wxDSVERIFY( pMS
->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME
) );
142 void wxMovieCtrl::SetLabel(const wxString
& label
)
144 wxControl::SetLabel(label
);
146 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
148 //wxBasicString will have a null string on an
149 //empty wxString - gotta love those workarounds!!
150 if(!label
.empty() && m_bVideo
)
152 wxBasicString
theBasicString(label
.mb_str());
153 wxDSVERIFY( pVW
->put_Caption(theBasicString
.Get()) );
157 bool wxMovieCtrl::Play()
159 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Run() );
162 bool wxMovieCtrl::Pause()
164 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Pause() );
167 bool wxMovieCtrl::Stop()
169 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Stop() );
174 bool wxMovieCtrl::Seek(const wxTimeSpan
& where
)
176 //DS uses 100 nanos - so we need a 10 mult
177 LONGLONG pos
= ((size_t)where
.GetMilliseconds().ToLong()) * 10;
179 return SUCCEEDED( ((IMediaSeeking
*&)m_pMS
)->SetPositions(
181 AM_SEEKING_AbsolutePositioning
,
183 AM_SEEKING_NoPositioning
187 wxTimeSpan
wxMovieCtrl::Tell()
189 LONGLONG outCur
, outStop
;
190 wxDSVERIFY( ((IMediaSeeking
*&)m_pMS
)->GetPositions(&outCur
, &outStop
) );
195 wxTimeSpan
wxMovieCtrl::Length()
197 LONGLONG outDuration
;
198 wxDSVERIFY( ((IMediaSeeking
*&)m_pMS
)->GetDuration(&outDuration
) );
203 #endif // wxUSE_DATETIME
205 wxMovieCtrlState
wxMovieCtrl::GetState()
208 OAFilterState theState
;
209 hr
= ((IMediaControl
*&)m_pMC
)->GetState(INFINITE
, &theState
);
211 wxASSERT( SUCCEEDED(hr
) );
213 //MSW state is the same as ours
215 //State_Paused = State_Stopped + 1,
216 //State_Running = State_Paused + 1
218 return (wxMovieCtrlState
) theState
;
221 double wxMovieCtrl::GetPlaybackRate()
224 wxDSVERIFY( ((IMediaSeeking
*&)m_pMS
)->GetRate(&dRate
) );
228 bool wxMovieCtrl::SetPlaybackRate(double dRate
)
230 return SUCCEEDED( ((IMediaSeeking
*&)m_pMS
)->SetRate(dRate
) );
233 WXLRESULT
wxMovieCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
236 // IMediaControl*& pMC = (IMediaControl*&) m_pMC;
237 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
238 // IMediaSeeking*& pMS = (IMediaSeeking*&) m_pMS;
240 if (nMsg
== WM_GRAPHNOTIFY
)
242 LONG evCode
, evParam1
, evParam2
;
245 // Process all queued events
246 while(SUCCEEDED(pME
->GetEvent(&evCode
, (LONG_PTR
*) &evParam1
,
247 (LONG_PTR
*) &evParam2
, 0)
251 // Free memory associated with callback, since we're not using it
252 hr
= pME
->FreeEventParams(evCode
, evParam1
, evParam2
);
254 // If this is the end of the clip, notify handler
255 if(EC_COMPLETE
== evCode
)
257 wxMovieEvent
theEvent(wxEVT_MOVIE_FINISHED
, this->GetId());
258 GetParent()->ProcessEvent(theEvent
);
262 // Reset to first frame of movie
263 hr = pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
264 NULL, AM_SEEKING_NoPositioning);
268 // for filters that don't support seeking do a rewind
272 wxString::Format(TEXT("Failed(0x%08lx) to stop media clip!\r\n"), hr)
277 hr = hr = pMC->Run();
282 wxString::Format(TEXT("Failed(0x%08lx) to reset media clip!\r\n"), hr)
290 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
292 //pass the event to our parent
293 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
296 void wxMovieCtrl::Cleanup()
299 this->Disconnect( wxID_ANY
,
301 (wxObjectEventFunction
) (wxEventFunction
) (wxSizeEventFunction
) &wxMovieCtrl::OnSize
);
304 IGraphBuilder
*& pGB
= (IGraphBuilder
*&) m_pGB
;
305 IMediaControl
*& pMC
= (IMediaControl
*&) m_pMC
;
306 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
307 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
308 IBasicAudio
*& pBA
= (IBasicAudio
*&) m_pBA
;
309 IBasicVideo
*& pBV
= (IBasicVideo
*&) m_pBV
;
310 IMediaSeeking
*& pMS
= (IMediaSeeking
*&) m_pMS
;
312 // Hide then disown the window
315 pVW
->put_Visible(OAFALSE
); //OSFALSE == 0
316 pVW
->put_Owner(NULL
);
319 // Release and zero DirectShow interfaces
329 wxMovieCtrl::~wxMovieCtrl()
335 wxSize
wxMovieCtrl::DoGetBestSize() const
340 void wxMovieCtrl::OnSize(wxSizeEvent
& evt
)
342 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
343 wxDSVERIFY( pVW
->SetWindowPosition(0, 0, evt
.GetSize().GetWidth(), evt
.GetSize().GetHeight()) );
348 #endif //wxUSE_MOVIECTRL