]> git.sesse.net Git - vlc/blob - activex/plugin.cpp
plugin.cpp, plugin.h: delayed initialization of VLC until activation in place in...
[vlc] / activex / plugin.cpp
1 /*****************************************************************************
2  * plugin.cpp: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #include "plugin.h"
24
25 #include "oleobject.h"
26 #include "olecontrol.h"
27 #include "oleinplaceobject.h"
28 #include "oleinplaceactiveobject.h"
29 #include "persistpropbag.h"
30 #include "persiststreaminit.h"
31 #include "persiststorage.h"
32 #include "provideclassinfo.h"
33 #include "connectioncontainer.h"
34 #include "objectsafety.h"
35 #include "vlccontrol.h"
36 #include "viewobject.h"
37 #include "dataobject.h"
38
39 #include "utils.h"
40
41 #include <string.h>
42 #include <winreg.h>
43 #include <winuser.h>
44 #include <servprov.h>
45 #include <shlwapi.h>
46 #include <wininet.h>
47
48 using namespace std;
49
50 ////////////////////////////////////////////////////////////////////////
51 //class factory
52
53 // {E23FE9C6-778E-49d4-B537-38FCDE4887D8}
54 //const GUID CLSID_VLCPlugin = 
55 //    { 0xe23fe9c6, 0x778e, 0x49d4, { 0xb5, 0x37, 0x38, 0xfc, 0xde, 0x48, 0x87, 0xd8 } };
56
57 static LRESULT CALLBACK VLCInPlaceClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
58     switch( uMsg )
59     {
60         case WM_ERASEBKGND:
61             return 1L;
62
63         case WM_PAINT:
64             PAINTSTRUCT ps;
65             if( GetUpdateRect(hWnd, NULL, FALSE) )
66             {
67                 BeginPaint(hWnd, &ps);
68                 EndPaint(hWnd, &ps);
69             }
70             return 0L;
71
72         default:
73             return DefWindowProc(hWnd, uMsg, wParam, lParam);
74     }
75 };
76
77 static LRESULT CALLBACK VLCVideoClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
78     VLCPlugin *p_instance = reinterpret_cast<VLCPlugin *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
79
80     switch( uMsg )
81     {
82         case WM_ERASEBKGND:
83             return 1L;
84
85         case WM_PAINT:
86             PAINTSTRUCT ps;
87             RECT pr;
88             if( GetUpdateRect(hWnd, &pr, FALSE) )
89             {
90                 RECT bounds;
91                 GetClientRect(hWnd, &bounds);
92                 BeginPaint(hWnd, &ps);
93                 p_instance->onPaint(ps.hdc, bounds, pr);
94                 EndPaint(hWnd, &ps);
95             }
96             return 0L;
97
98         default:
99             return DefWindowProc(hWnd, uMsg, wParam, lParam);
100     }
101 };
102
103 VLCPluginClass::VLCPluginClass(LONG *p_class_ref, HINSTANCE hInstance) :
104     _p_class_ref(p_class_ref),
105     _hinstance(hInstance),
106     _inplace_picture(NULL)
107 {
108     WNDCLASS wClass;
109
110     if( ! GetClassInfo(hInstance, getInPlaceWndClassName(), &wClass) )
111     {
112         wClass.style          = CS_NOCLOSE|CS_DBLCLKS;
113         wClass.lpfnWndProc    = VLCInPlaceClassWndProc;
114         wClass.cbClsExtra     = 0;
115         wClass.cbWndExtra     = 0;
116         wClass.hInstance      = hInstance;
117         wClass.hIcon          = NULL;
118         wClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
119         wClass.hbrBackground  = NULL;
120         wClass.lpszMenuName   = NULL;
121         wClass.lpszClassName  = getInPlaceWndClassName();
122        
123         _inplace_wndclass_atom = RegisterClass(&wClass);
124     }
125     else
126     {
127         _inplace_wndclass_atom = 0;
128     }
129
130     if( ! GetClassInfo(hInstance, getVideoWndClassName(), &wClass) )
131     {
132         wClass.style          = CS_NOCLOSE;
133         wClass.lpfnWndProc    = VLCVideoClassWndProc;
134         wClass.cbClsExtra     = 0;
135         wClass.cbWndExtra     = 0;
136         wClass.hInstance      = hInstance;
137         wClass.hIcon          = NULL;
138         wClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
139         wClass.hbrBackground  = NULL;
140         wClass.lpszMenuName   = NULL;
141         wClass.lpszClassName  = getVideoWndClassName();
142        
143         _video_wndclass_atom = RegisterClass(&wClass);
144     }
145     else
146     {
147         _video_wndclass_atom = 0;
148     }
149
150     HBITMAP hbitmap = (HBITMAP)LoadImage(getHInstance(), TEXT("INPLACE-PICT"), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
151     if( NULL != hbitmap )
152     {
153         PICTDESC pictDesc;
154
155         pictDesc.cbSizeofstruct = sizeof(PICTDESC);
156         pictDesc.picType        = PICTYPE_BITMAP;
157         pictDesc.bmp.hbitmap    = hbitmap;
158         pictDesc.bmp.hpal       = NULL;
159
160         if( FAILED(OleCreatePictureIndirect(&pictDesc, IID_IPicture, TRUE, reinterpret_cast<LPVOID*>(&_inplace_picture))) )
161             _inplace_picture = NULL;
162     }
163     AddRef();
164 };
165
166 VLCPluginClass::~VLCPluginClass()
167 {
168     if( 0 != _inplace_wndclass_atom )
169         UnregisterClass(MAKEINTATOM(_inplace_wndclass_atom), _hinstance);
170
171     if( 0 != _video_wndclass_atom )
172         UnregisterClass(MAKEINTATOM(_video_wndclass_atom), _hinstance);
173
174     if( NULL != _inplace_picture )
175         _inplace_picture->Release();
176 };
177
178 STDMETHODIMP VLCPluginClass::QueryInterface(REFIID riid, void **ppv)
179 {
180     if( NULL == ppv )
181         return E_INVALIDARG;
182
183     if( (IID_IUnknown == riid) || (IID_IClassFactory == riid) )
184     {
185         AddRef();
186         *ppv = reinterpret_cast<LPVOID>(this);
187
188         return NOERROR;
189     }
190
191     *ppv = NULL;
192
193     return E_NOINTERFACE;
194 };
195
196 STDMETHODIMP_(ULONG) VLCPluginClass::AddRef(void)
197 {
198     return InterlockedIncrement(_p_class_ref);
199 };
200
201 STDMETHODIMP_(ULONG) VLCPluginClass::Release(void)
202 {
203     ULONG refcount = InterlockedDecrement(_p_class_ref);
204     if( 0 == refcount )
205     {
206         delete this;
207         return 0;
208     }
209     return refcount;
210 };
211
212 STDMETHODIMP VLCPluginClass::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, void **ppv)
213 {
214     if( NULL == ppv )
215         return E_POINTER;
216
217     *ppv = NULL;
218
219     if( (NULL != pUnkOuter) && (IID_IUnknown != riid) ) {
220         return CLASS_E_NOAGGREGATION;
221     }
222
223     VLCPlugin *plugin = new VLCPlugin(this, pUnkOuter);
224     if( NULL != plugin )
225     {
226         HRESULT hr = plugin->QueryInterface(riid, ppv);
227         plugin->Release();
228         return hr;
229     }
230     return E_OUTOFMEMORY;
231 };
232
233 STDMETHODIMP VLCPluginClass::LockServer(BOOL fLock)
234 {
235     if( fLock )
236         AddRef();
237     else 
238         Release();
239
240     return S_OK;
241 };
242
243 ////////////////////////////////////////////////////////////////////////
244
245 VLCPlugin::VLCPlugin(VLCPluginClass *p_class, LPUNKNOWN pUnkOuter) :
246     _inplacewnd(NULL),
247     _videownd(NULL),
248     _p_class(p_class),
249     _i_ref(1UL),
250     _i_codepage(CP_ACP),
251     _b_usermode(TRUE),
252     _i_vlc(0)
253 {
254     p_class->AddRef();
255
256     vlcOleControl = new VLCOleControl(this);
257     vlcOleInPlaceObject = new VLCOleInPlaceObject(this);
258     vlcOleInPlaceActiveObject = new VLCOleInPlaceActiveObject(this);
259     vlcPersistStorage = new VLCPersistStorage(this);
260     vlcPersistStreamInit = new VLCPersistStreamInit(this);
261     vlcPersistPropertyBag = new VLCPersistPropertyBag(this);
262     vlcProvideClassInfo = new VLCProvideClassInfo(this);
263     vlcConnectionPointContainer = new VLCConnectionPointContainer(this);
264     vlcObjectSafety = new VLCObjectSafety(this);
265     vlcControl = new VLCControl(this);
266     vlcViewObject = new VLCViewObject(this);
267     vlcDataObject = new VLCDataObject(this);
268     vlcOleObject = new VLCOleObject(this);
269
270     // configure controlling IUnknown interface for implemented interfaces
271     this->pUnkOuter = (NULL != pUnkOuter) ? pUnkOuter : dynamic_cast<LPUNKNOWN>(this);
272
273     // default picure
274     _p_pict = p_class->getInPlacePict();
275
276     // make sure that persistable properties are initialized
277     onInit();
278 };
279
280 VLCPlugin::~VLCPlugin()
281 {
282     delete vlcOleObject;
283     delete vlcDataObject;
284     delete vlcViewObject;
285     delete vlcControl;
286     delete vlcConnectionPointContainer;
287     delete vlcProvideClassInfo;
288     delete vlcPersistPropertyBag;
289     delete vlcPersistStreamInit;
290     delete vlcPersistStorage;
291     delete vlcOleInPlaceActiveObject;
292     delete vlcOleInPlaceObject;
293     delete vlcObjectSafety;
294
295     delete vlcOleControl;
296     if( _p_pict )
297         _p_pict->Release();
298
299     SysFreeString(_bstr_mrl);
300
301     _p_class->Release();
302 };
303
304 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
305 {
306     if( NULL == ppv )
307         return E_INVALIDARG;
308
309     if( IID_IUnknown == riid )
310         *ppv = reinterpret_cast<LPVOID>(this);
311     else if( IID_IOleObject == riid )
312         *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
313     else if( IID_IOleControl == riid )
314         *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
315     else if( IID_IOleWindow == riid )
316         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
317     else if( IID_IOleInPlaceObject == riid )
318         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
319     else if( IID_IOleInPlaceActiveObject == riid )
320         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
321     else if( IID_IPersist == riid )
322         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
323     else if( IID_IPersistStreamInit == riid )
324         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
325     else if( IID_IPersistStorage == riid )
326         *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
327     else if( IID_IPersistPropertyBag == riid )
328         *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
329     else if( IID_IProvideClassInfo == riid )
330         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
331     else if( IID_IProvideClassInfo2 == riid )
332         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
333     else if( IID_IConnectionPointContainer == riid )
334         *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
335     else if( IID_IObjectSafety == riid )
336         *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
337     else if( IID_IDispatch == riid )
338         *ppv = reinterpret_cast<LPVOID>(vlcControl);
339     else if( IID_IVLCControl == riid )
340         *ppv = reinterpret_cast<LPVOID>(vlcControl);
341     else if( IID_IViewObject == riid )
342         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
343     else if( IID_IViewObject2 == riid )
344         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
345     else if( IID_IDataObject == riid )
346         *ppv = reinterpret_cast<LPVOID>(vlcDataObject);
347     else
348     {
349         *ppv = NULL;
350         return E_NOINTERFACE;
351     }
352     ((LPUNKNOWN)*ppv)->AddRef();
353     return NOERROR;
354 };
355
356 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
357 {
358     return InterlockedIncrement((LONG *)&_i_ref);
359 };
360
361 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
362 {
363     if( ! InterlockedDecrement((LONG *)&_i_ref) )
364     {
365         delete this;
366         return 0;
367     }
368     return _i_ref;
369 };
370
371 //////////////////////////////////////
372
373 /*
374 ** we use a window to represent plugin viewport,
375 ** whose geometry is limited by the clipping rectangle
376 ** all drawing within this window must follow must
377 ** follow coordinates system described in lprPosRect
378 */
379
380 static void getViewportCoords(LPRECT lprPosRect, LPRECT lprClipRect)
381 {
382     RECT bounds;
383     bounds.right  = lprPosRect->right-lprPosRect->left;
384
385     if( lprClipRect->left <= lprPosRect->left )
386     {
387         // left side is not clipped out
388         bounds.left = 0;
389
390         if( lprClipRect->right >= lprPosRect->right )
391         {
392             // right side is not clipped out, no change
393         }
394         else if( lprClipRect->right >= lprPosRect->left )
395         {
396             // right side is clipped out
397             lprPosRect->right = lprClipRect->right;
398         }
399         else
400         {
401             // outside of clipping rectange, not visible
402             lprPosRect->right = lprPosRect->left;
403         }
404     }
405     else
406     {
407         // left side is clipped out
408         bounds.left = lprPosRect->left-lprClipRect->left;
409         bounds.right += bounds.left;
410
411         lprPosRect->left = lprClipRect->left;
412         if( lprClipRect->right >= lprPosRect->right )
413         {
414             // right side is not clipped out
415         }
416         else
417         {
418             // right side is clipped out
419             lprPosRect->right = lprClipRect->right;
420         }
421     }
422
423     bounds.bottom = lprPosRect->bottom-lprPosRect->top;
424
425     if( lprClipRect->top <= lprPosRect->top )
426     {
427         // top side is not clipped out
428         bounds.top = 0;
429
430         if( lprClipRect->bottom >= lprPosRect->bottom )
431         {
432             // bottom side is not clipped out, no change
433         }
434         else if( lprClipRect->bottom >= lprPosRect->top )
435         {
436             // bottom side is clipped out
437             lprPosRect->bottom = lprClipRect->bottom;
438         }
439         else
440         {
441             // outside of clipping rectange, not visible
442             lprPosRect->right = lprPosRect->left;
443         }
444     }
445     else
446     {
447         bounds.top = lprPosRect->top-lprClipRect->top;
448         bounds.bottom += bounds.top;
449
450         lprPosRect->top = lprClipRect->top;
451         if( lprClipRect->bottom >= lprPosRect->bottom )
452         {
453             // bottom side is not clipped out
454         }
455         else
456         {
457             // bottom side is clipped out
458             lprPosRect->bottom = lprClipRect->bottom;
459         }
460     }
461     *lprClipRect = *lprPosRect;
462     *lprPosRect  = bounds;
463 };
464
465 HRESULT VLCPlugin::onInit(void)
466 {
467     if( 0 == _i_vlc )
468     {
469         // initialize persistable properties
470         _bstr_mrl = NULL;
471         _b_autoplay = TRUE;
472         _b_autoloop = FALSE;
473         _b_visible = TRUE;
474         _b_mute = FALSE;
475         _i_volume = 50;
476         // set default/preferred size (320x240) pixels in HIMETRIC
477         HDC hDC = CreateDevDC(NULL);
478         _extent.cx = 320;
479         _extent.cy = 240;
480         HimetricFromDP(hDC, (LPPOINT)&_extent, 1);
481         DeleteDC(hDC);
482
483         return S_OK;
484     }
485     return CO_E_ALREADYINITIALIZED;
486 };
487
488 HRESULT VLCPlugin::onLoad(void)
489 {
490     if( SysStringLen(_bstr_mrl) > 0 )
491     {
492         /*
493         ** try to combine MRL with client site moniker, which for Internet Explorer
494         ** is the URL of the page the plugin is embedded into. Hence, if the MRL
495         ** is a relative URL, we should end up with an absolute URL
496         */
497         LPOLECLIENTSITE pClientSite;
498         if( SUCCEEDED(vlcOleObject->GetClientSite(&pClientSite)) && (NULL != pClientSite) )
499         {
500             IBindCtx *pBC = 0;
501             if( SUCCEEDED(CreateBindCtx(0, &pBC)) )
502             {
503                 LPMONIKER pContMoniker = NULL;
504                 if( SUCCEEDED(pClientSite->GetMoniker(OLEGETMONIKER_ONLYIFTHERE,
505                                 OLEWHICHMK_CONTAINER, &pContMoniker)) )
506                 {
507                     LPOLESTR base_url;
508                     if( SUCCEEDED(pContMoniker->GetDisplayName(pBC, NULL, &base_url)) )
509                     {
510                         /*
511                         ** check that the moniker name is a URL
512                         */
513                         if( UrlIsW(base_url, URLIS_URL) )
514                         {
515                             DWORD len = INTERNET_MAX_URL_LENGTH;
516                             LPOLESTR abs_url = (LPOLESTR)CoTaskMemAlloc(sizeof(OLECHAR)*len);
517                             if( NULL != abs_url )
518                             {
519                                 if( SUCCEEDED(UrlCombineW(base_url, _bstr_mrl, abs_url, &len,
520                                                 URL_ESCAPE_UNSAFE|URL_PLUGGABLE_PROTOCOL)) )
521                                 {
522                                     SysFreeString(_bstr_mrl);
523                                     _bstr_mrl = SysAllocStringLen(abs_url, len);
524                                 }
525                                 CoTaskMemFree(abs_url);
526                             }
527                         }
528                         CoTaskMemFree(base_url);
529                     }
530                     pContMoniker->Release();
531                 }
532                 pBC->Release();
533             }
534             pClientSite->Release();
535         }
536     }
537     setDirty(FALSE);
538     return S_OK;
539 };
540
541 HRESULT VLCPlugin::onRun(void)
542 {
543     if( ! isRunning() )
544     {
545         _i_vlc = VLC_Create();
546         if( _i_vlc < 0 )
547         {
548             _i_vlc = 0;
549             return E_FAIL;
550         }
551
552         /*
553         ** default initialization options
554         */
555         char *ppsz_argv[10] = { "vlc", };
556         int   ppsz_argc = 1;
557
558         HKEY h_key;
559         DWORD i_type, i_data = MAX_PATH + 1;
560         char p_data[MAX_PATH + 1];
561         if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
562                           0, KEY_READ, &h_key ) == ERROR_SUCCESS )
563         {
564              if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
565                                   (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
566              {
567                  if( i_type == REG_SZ )
568                  {
569                      strcat( p_data, "\\vlc" );
570                      ppsz_argv[0] = p_data;
571                  }
572              }
573              RegCloseKey( h_key );
574         }
575
576 #if 1
577         ppsz_argv[0] = "C:\\cygwin\\home\\Damien_Fouilleul\\dev\\videolan\\vlc-trunk\\vlc";
578 #endif
579
580         // make sure plugin isn't affected with VLC single instance mode
581         ppsz_argv[ppsz_argc++] = "--no-one-instance";
582
583         // loop mode is a configuration option only
584         if( _b_autoloop )
585             ppsz_argv[ppsz_argc++] = "--loop";
586
587         if( IsDebuggerPresent() )
588         {
589             /*
590             ** VLC default threading mechanism is designed to be as compatible
591             ** with POSIX as possible, however when debugged on win32, threads
592             ** lose signals and eventually VLC get stuck during initialization.
593             ** threading support can be configured to be more debugging friendly
594             ** but it will be less compatible with POSIX.
595             ** This is done by initializing with the following options
596             */
597             ppsz_argv[ppsz_argc++] = "--fast-mutex";
598             ppsz_argv[ppsz_argc++] = "--win9x-cv-method=1";
599         }
600
601         if( VLC_Init(_i_vlc, ppsz_argc, ppsz_argv) )
602         {
603             VLC_Destroy(_i_vlc);
604             _i_vlc = 0;
605             return E_FAIL;
606         }
607
608         VLC_VolumeSet(_i_vlc, _i_volume);
609
610         if( _b_mute )
611             VLC_VolumeMute(_i_vlc);
612
613         char *psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
614         if( NULL != psz_mrl )
615         {
616             // add default target to playlist
617             VLC_AddTarget(_i_vlc, psz_mrl, NULL, 0, PLAYLIST_APPEND, PLAYLIST_END);
618             CoTaskMemFree(psz_mrl);
619         }
620     }
621     return S_OK;
622 };
623
624 HRESULT VLCPlugin::onAmbientChanged(LPUNKNOWN pContainer, DISPID dispID)
625 {
626     VARIANT v;
627     switch( dispID )
628     {
629         case DISPID_AMBIENT_BACKCOLOR:
630             break;
631         case DISPID_AMBIENT_DISPLAYNAME:
632             break;
633         case DISPID_AMBIENT_FONT:
634             break;
635         case DISPID_AMBIENT_FORECOLOR:
636             break;
637         case DISPID_AMBIENT_LOCALEID:
638             break;
639         case DISPID_AMBIENT_MESSAGEREFLECT:
640             break;
641         case DISPID_AMBIENT_SCALEUNITS:
642             break;
643         case DISPID_AMBIENT_TEXTALIGN:
644             break;
645         case DISPID_AMBIENT_USERMODE:
646             VariantInit(&v);
647             V_VT(&v) = VT_BOOL;
648             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
649             {
650                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
651             }
652             break;
653         case DISPID_AMBIENT_UIDEAD:
654             break;
655         case DISPID_AMBIENT_SHOWGRABHANDLES:
656             break;
657         case DISPID_AMBIENT_SHOWHATCHING:
658             break;
659         case DISPID_AMBIENT_DISPLAYASDEFAULT:
660             break;
661         case DISPID_AMBIENT_SUPPORTSMNEMONICS:
662             break;
663         case DISPID_AMBIENT_AUTOCLIP:
664             break;
665         case DISPID_AMBIENT_APPEARANCE:
666             break;
667         case DISPID_AMBIENT_CODEPAGE:
668             VariantInit(&v);
669             V_VT(&v) = VT_I4;
670             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
671             {
672                 setCodePage(V_I4(&v));
673             }
674             break;
675         case DISPID_AMBIENT_PALETTE:
676             break;
677         case DISPID_AMBIENT_CHARSET:
678             break;
679         case DISPID_AMBIENT_RIGHTTOLEFT:
680             break;
681         case DISPID_AMBIENT_TOPTOBOTTOM:
682             break;
683         case DISPID_UNKNOWN:
684             /*
685             ** multiple property change, look up the ones we are interested in
686             */
687             VariantInit(&v);
688             V_VT(&v) = VT_BOOL;
689             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_USERMODE, v)) )
690             {
691                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
692             }
693             VariantInit(&v);
694             V_VT(&v) = VT_I4;
695             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_CODEPAGE, v)) )
696             {
697                 setCodePage(V_I4(&v));
698             }
699             break;
700     }
701     return S_OK;
702 };
703
704 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
705 {
706     if( isInPlaceActive() )
707     {
708         onInPlaceDeactivate();
709     }
710     if( isRunning() )
711     {
712         int i_vlc = _i_vlc;
713
714         _i_vlc = 0;
715         vlcDataObject->onClose();
716
717         VLC_CleanUp(i_vlc);
718         VLC_Destroy(i_vlc);
719     }
720     return S_OK;
721 };
722
723 BOOL VLCPlugin::isInPlaceActive(void)
724 {
725     return (NULL != _inplacewnd);
726 };
727
728 HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
729 {
730     RECT posRect = *lprcPosRect;
731     RECT clipRect = *lprcClipRect;
732
733     /*
734     ** record keeping of control geometry within container
735     */ 
736     _posRect = posRect;
737
738     /*
739     ** convert posRect & clipRect to match control viewport coordinates
740     */
741     getViewportCoords(&posRect, &clipRect);
742
743     /*
744     ** Create a window for in place activated control.
745     ** the window geometry matches the control viewport
746     ** within container so that embedded video is always
747     ** properly clipped.
748     */
749     _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
750             "VLC Plugin In-Place Window",
751             WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
752             clipRect.left,
753             clipRect.top,
754             clipRect.right-clipRect.left,
755             clipRect.bottom-clipRect.top,
756             hwndParent,
757             0,
758             _p_class->getHInstance(),
759             NULL
760            );
761
762     if( NULL == _inplacewnd )
763         return E_FAIL;
764
765     SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
766
767     /*
768     ** VLC embedded video automatically grows to cover client
769     ** area of parent window.
770     ** hence create a such a 'parent' window whose geometry
771     ** is always correct relative to the viewport bounds
772     */
773     _videownd = CreateWindow(_p_class->getVideoWndClassName(),
774             "VLC Plugin Video Window",
775             WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
776             posRect.left,
777             posRect.top,
778             posRect.right-posRect.left,
779             posRect.bottom-posRect.top,
780             _inplacewnd,
781             0,
782             _p_class->getHInstance(),
783             NULL
784            );
785
786     if( NULL == _videownd )
787     {
788         DestroyWindow(_inplacewnd);
789         return E_FAIL;
790     }
791
792     SetWindowLongPtr(_videownd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
793
794     if( getVisible() )
795         ShowWindow(_inplacewnd, SW_SHOW);
796
797     if( _b_usermode )
798     {
799         /* run vlc if not done already */
800         HRESULT result = onRun();
801         if( FAILED(result) )
802             return result;
803
804         /* set internal video width and height */
805         vlc_value_t val;
806         val.i_int = posRect.right-posRect.left;
807         VLC_VariableSet(_i_vlc, "conf::width", val);
808         val.i_int = posRect.bottom-posRect.top;
809         VLC_VariableSet(_i_vlc, "conf::height", val);
810
811         /* set internal video parent window */
812         /* horrible cast there */
813         val.i_int = reinterpret_cast<int>(_videownd);
814         VLC_VariableSet(_i_vlc, "drawable", val);
815
816         if( _b_autoplay & (VLC_PlaylistNumberOfItems(_i_vlc) > 0) )
817         {
818             VLC_Play(_i_vlc);
819             fireOnPlayEvent();
820         }
821     }
822     return S_OK;
823 };
824
825 HRESULT VLCPlugin::onInPlaceDeactivate(void)
826 {
827     if( isRunning() )
828     {
829         VLC_Stop(_i_vlc);
830         fireOnStopEvent();
831     }
832
833     DestroyWindow(_videownd);
834     _videownd = NULL;
835     DestroyWindow(_inplacewnd);
836     _inplacewnd = NULL;
837  
838     return S_OK;
839 };
840
841 void VLCPlugin::setVisible(BOOL fVisible)
842 {
843     if( fVisible != _b_visible )
844     {
845         _b_visible = fVisible;
846         if( isInPlaceActive() )
847         {
848             ShowWindow(_inplacewnd, fVisible ? SW_SHOW : SW_HIDE);
849             if( fVisible )
850                 InvalidateRect(_videownd, NULL, TRUE);
851         }
852         setDirty(TRUE);
853         firePropChangedEvent(DISPID_Visible);
854     }
855 };
856
857 void VLCPlugin::setVolume(int volume)
858 {
859     if( volume < 0 )
860         volume = 0;
861     else if( volume > 200 )
862         volume = 200;
863
864     if( volume != _i_volume )
865     {
866         _i_volume = volume;
867         if( isRunning() )
868         {
869             VLC_VolumeSet(_i_vlc, _i_volume);
870         }
871         setDirty(TRUE);
872     }
873 };
874
875 void VLCPlugin::setFocus(BOOL fFocus)
876 {
877     if( fFocus )
878         SetActiveWindow(_inplacewnd);
879 };
880
881 BOOL VLCPlugin::hasFocus(void)
882 {
883     return GetActiveWindow() == _inplacewnd;
884 };
885
886 void VLCPlugin::onDraw(DVTARGETDEVICE * ptd, HDC hicTargetDev,
887         HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds)
888 {
889     if( getVisible() )
890     {
891         long width = lprcBounds->right-lprcBounds->left;
892         long height = lprcBounds->bottom-lprcBounds->top;
893
894         RECT bounds = { lprcBounds->left, lprcBounds->top, lprcBounds->right, lprcBounds->bottom };
895         FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(WHITE_BRUSH));
896
897         LPPICTURE pict = getPicture();
898         if( NULL != pict )
899         {
900             OLE_XSIZE_HIMETRIC picWidth;
901             OLE_YSIZE_HIMETRIC picHeight;
902
903             pict->get_Width(&picWidth);
904             pict->get_Height(&picHeight);
905
906             SIZEL picSize = { picWidth, picHeight };
907
908             if( NULL != hicTargetDev )
909             {
910                 DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
911             }
912             else if( NULL != (hicTargetDev = CreateDevDC(ptd)) )
913             {
914                 DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
915                 DeleteDC(hicTargetDev);
916             }
917
918             if( picSize.cx > width-4 )
919                 picSize.cx = width-4;
920             if( picSize.cy > height-4 )
921                 picSize.cy = height-4;
922
923             LONG dstX = lprcBounds->left+(width-picSize.cx)/2;
924             LONG dstY = lprcBounds->top+(height-picSize.cy)/2;
925
926             if( NULL != lprcWBounds )
927             {
928                 RECT wBounds = { lprcWBounds->left, lprcWBounds->top, lprcWBounds->right, lprcWBounds->bottom };
929                 pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
930                         0L, picHeight, picWidth, -picHeight, &wBounds);
931             }
932             else 
933                 pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
934                         0L, picHeight, picWidth, -picHeight, NULL);
935
936             pict->Release();
937         }
938
939         SelectObject(hdcDraw, GetStockObject(BLACK_BRUSH));
940
941         MoveToEx(hdcDraw, bounds.left, bounds.top, NULL);
942         LineTo(hdcDraw, bounds.left+width-1, bounds.top);
943         LineTo(hdcDraw, bounds.left+width-1, bounds.top+height-1);
944         LineTo(hdcDraw, bounds.left, bounds.top+height-1);
945         LineTo(hdcDraw, bounds.left, bounds.top);
946     }
947 };
948
949 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &clipRect)
950 {
951     if( getVisible() )
952     {
953         /** if VLC is playing, it may not display any VIDEO content 
954         ** hence, draw control logo*/
955         HDC hdcDraw = CreateCompatibleDC(hdc);
956         if( NULL != hdcDraw )
957         {
958             SIZEL size = getExtent();
959             DPFromHimetric(hdc, (LPPOINT)&size, 1);
960             RECTL posRect = { 0, 0, size.cx, size.cy };
961
962             int width = bounds.right-bounds.left;
963             int height = bounds.bottom-bounds.top;
964
965             HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
966             if( NULL != hBitmap )
967             {
968                 HBITMAP oldBmp = (HBITMAP)SelectObject(hdcDraw, hBitmap);
969
970                 if( (size.cx != width) || (size.cy != height) )
971                 {
972                     // needs to scale canvas
973                     SetMapMode(hdcDraw, MM_ANISOTROPIC);
974                     SetWindowExtEx(hdcDraw, size.cx, size.cy, NULL);
975                     SetViewportExtEx(hdcDraw, width, height, NULL);
976                 }
977
978                 onDraw(NULL, hdc, hdcDraw, &posRect, NULL);
979
980                 SetMapMode(hdcDraw, MM_TEXT);
981                 BitBlt(hdc, bounds.left, bounds.top,
982                         width, height,
983                         hdcDraw, 0, 0,
984                         SRCCOPY);
985
986                 SelectObject(hdcDraw, oldBmp);
987                 DeleteObject(hBitmap);
988             }
989             DeleteDC(hdcDraw);
990         }
991     }
992 };
993
994 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
995 {
996     RECT clipRect = *lprcClipRect;
997     RECT posRect  = *lprcPosRect;
998
999     //RedrawWindow(GetParent(_inplacewnd), &_posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
1000
1001     /*
1002     ** record keeping of control geometry within container
1003     */
1004     _posRect = posRect;
1005
1006     /*
1007     ** convert posRect & clipRect to match control viewport coordinates
1008     */
1009     getViewportCoords(&posRect, &clipRect);
1010
1011     /*
1012     ** change in-place window geometry to match clipping region
1013     */
1014     SetWindowPos(_inplacewnd, NULL,
1015             clipRect.left,
1016             clipRect.top,
1017             clipRect.right-clipRect.left,
1018             clipRect.bottom-clipRect.top,
1019             SWP_NOACTIVATE|
1020             SWP_NOCOPYBITS|
1021             SWP_NOZORDER|
1022             SWP_NOOWNERZORDER );
1023
1024     /*
1025     ** change video window geometry to match object bounds within clipping region
1026     */
1027     SetWindowPos(_videownd, NULL,
1028             posRect.left,
1029             posRect.top,
1030             posRect.right-posRect.left,
1031             posRect.bottom-posRect.top,
1032             SWP_NOACTIVATE|
1033             SWP_NOCOPYBITS|
1034             SWP_NOZORDER|
1035             SWP_NOOWNERZORDER );
1036
1037     //RedrawWindow(_videownd, &posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
1038     vlc_value_t val;
1039     val.i_int = posRect.right-posRect.left;
1040     VLC_VariableSet(_i_vlc, "conf::width", val);
1041     val.i_int = posRect.bottom-posRect.top;
1042     VLC_VariableSet(_i_vlc, "conf::height", val);
1043 };
1044
1045 void VLCPlugin::freezeEvents(BOOL freeze)
1046 {
1047     vlcConnectionPointContainer->freezeEvents(freeze);
1048 };
1049
1050 void VLCPlugin::firePropChangedEvent(DISPID dispid)
1051 {
1052     vlcConnectionPointContainer->firePropChangedEvent(dispid); 
1053 };
1054
1055 void VLCPlugin::fireOnPlayEvent(void)
1056 {
1057     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1058     vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs); 
1059 };
1060
1061 void VLCPlugin::fireOnPauseEvent(void)
1062 {
1063     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1064     vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs); 
1065 };
1066
1067 void VLCPlugin::fireOnStopEvent(void)
1068 {
1069     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1070     vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs); 
1071 };
1072