]> git.sesse.net Git - vlc/blob - activex/plugin.cpp
FSF address change.
[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     _p_class(p_class),
248     _i_ref(1UL),
249     _i_codepage(CP_ACP),
250     _b_usermode(TRUE),
251     _bstr_mrl(NULL),
252     _b_autoplay(TRUE),
253     _b_autoloop(FALSE),
254     _b_visible(TRUE),
255     _b_mute(FALSE),
256     _i_vlc(0)
257 {
258     p_class->AddRef();
259
260     vlcOleControl = new VLCOleControl(this);
261     vlcOleInPlaceObject = new VLCOleInPlaceObject(this);
262     vlcOleInPlaceActiveObject = new VLCOleInPlaceActiveObject(this);
263     vlcPersistStorage = new VLCPersistStorage(this);
264     vlcPersistStreamInit = new VLCPersistStreamInit(this);
265     vlcPersistPropertyBag = new VLCPersistPropertyBag(this);
266     vlcProvideClassInfo = new VLCProvideClassInfo(this);
267     vlcConnectionPointContainer = new VLCConnectionPointContainer(this);
268     vlcObjectSafety = new VLCObjectSafety(this);
269     vlcControl = new VLCControl(this);
270     vlcViewObject = new VLCViewObject(this);
271     vlcDataObject = new VLCDataObject(this);
272     vlcOleObject = new VLCOleObject(this);
273
274     // configure controlling IUnknown interface for implemented interfaces
275     this->pUnkOuter = (NULL != pUnkOuter) ? pUnkOuter : dynamic_cast<LPUNKNOWN>(this);
276
277     // default picure
278     _p_pict = p_class->getInPlacePict();
279
280     // set default/preferred size (320x240) pixels in HIMETRIC
281     HDC hDC = CreateDevDC(NULL);
282     _extent.cx = 320;
283     _extent.cy = 240;
284     HimetricFromDP(hDC, (LPPOINT)&_extent, 1);
285     DeleteDC(hDC);
286 };
287
288 VLCPlugin::~VLCPlugin()
289 {
290     delete vlcOleObject;
291     delete vlcDataObject;
292     delete vlcViewObject;
293     delete vlcControl;
294     delete vlcConnectionPointContainer;
295     delete vlcProvideClassInfo;
296     delete vlcPersistPropertyBag;
297     delete vlcPersistStreamInit;
298     delete vlcPersistStorage;
299     delete vlcOleInPlaceActiveObject;
300     delete vlcOleInPlaceObject;
301     delete vlcObjectSafety;
302
303     delete vlcOleControl;
304     if( _p_pict )
305         _p_pict->Release();
306
307     SysFreeString(_bstr_mrl);
308
309     _p_class->Release();
310 };
311
312 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
313 {
314     if( NULL == ppv )
315         return E_INVALIDARG;
316
317     if( IID_IUnknown == riid )
318         *ppv = reinterpret_cast<LPVOID>(this);
319     else if( IID_IOleObject == riid )
320         *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
321     else if( IID_IOleControl == riid )
322         *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
323     else if( IID_IOleWindow == riid )
324         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
325     else if( IID_IOleInPlaceObject == riid )
326         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
327     else if( IID_IOleInPlaceActiveObject == riid )
328         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
329     else if( IID_IPersist == riid )
330         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
331     else if( IID_IPersistStreamInit == riid )
332         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
333     else if( IID_IPersistStorage == riid )
334         *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
335     else if( IID_IPersistPropertyBag == riid )
336         *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
337     else if( IID_IProvideClassInfo == riid )
338         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
339     else if( IID_IProvideClassInfo2 == riid )
340         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
341     else if( IID_IConnectionPointContainer == riid )
342         *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
343     else if( IID_IObjectSafety == riid )
344         *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
345     else if( IID_IDispatch == riid )
346         *ppv = reinterpret_cast<LPVOID>(vlcControl);
347     else if( IID_IVLCControl == riid )
348         *ppv = reinterpret_cast<LPVOID>(vlcControl);
349     else if( IID_IViewObject == riid )
350         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
351     else if( IID_IViewObject2 == riid )
352         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
353     else if( IID_IDataObject == riid )
354         *ppv = reinterpret_cast<LPVOID>(vlcDataObject);
355     else
356     {
357         *ppv = NULL;
358         return E_NOINTERFACE;
359     }
360     ((LPUNKNOWN)*ppv)->AddRef();
361     return NOERROR;
362 };
363
364 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
365 {
366     return InterlockedIncrement((LONG *)&_i_ref);
367 };
368
369 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
370 {
371     if( ! InterlockedDecrement((LONG *)&_i_ref) )
372     {
373         delete this;
374         return 0;
375     }
376     return _i_ref;
377 };
378
379 //////////////////////////////////////
380
381 /*
382 ** we use a window to represent plugin viewport,
383 ** whose geometry is limited by the clipping rectangle
384 ** all drawing within this window must follow must
385 ** follow coordinates system described in lprPosRect
386 */
387
388 static void getViewportCoords(LPRECT lprPosRect, LPRECT lprClipRect)
389 {
390     RECT bounds;
391     bounds.right  = lprPosRect->right-lprPosRect->left;
392
393     if( lprClipRect->left <= lprPosRect->left )
394     {
395         // left side is not clipped out
396         bounds.left = 0;
397
398         if( lprClipRect->right >= lprPosRect->right )
399         {
400             // right side is not clipped out, no change
401         }
402         else if( lprClipRect->right >= lprPosRect->left )
403         {
404             // right side is clipped out
405             lprPosRect->right = lprClipRect->right;
406         }
407         else
408         {
409             // outside of clipping rectange, not visible
410             lprPosRect->right = lprPosRect->left;
411         }
412     }
413     else
414     {
415         // left side is clipped out
416         bounds.left = lprPosRect->left-lprClipRect->left;
417         bounds.right += bounds.left;
418
419         lprPosRect->left = lprClipRect->left;
420         if( lprClipRect->right >= lprPosRect->right )
421         {
422             // right side is not clipped out
423         }
424         else
425         {
426             // right side is clipped out
427             lprPosRect->right = lprClipRect->right;
428         }
429     }
430
431     bounds.bottom = lprPosRect->bottom-lprPosRect->top;
432
433     if( lprClipRect->top <= lprPosRect->top )
434     {
435         // top side is not clipped out
436         bounds.top = 0;
437
438         if( lprClipRect->bottom >= lprPosRect->bottom )
439         {
440             // bottom side is not clipped out, no change
441         }
442         else if( lprClipRect->bottom >= lprPosRect->top )
443         {
444             // bottom side is clipped out
445             lprPosRect->bottom = lprClipRect->bottom;
446         }
447         else
448         {
449             // outside of clipping rectange, not visible
450             lprPosRect->right = lprPosRect->left;
451         }
452     }
453     else
454     {
455         bounds.top = lprPosRect->top-lprClipRect->top;
456         bounds.bottom += bounds.top;
457
458         lprPosRect->top = lprClipRect->top;
459         if( lprClipRect->bottom >= lprPosRect->bottom )
460         {
461             // bottom side is not clipped out
462         }
463         else
464         {
465             // bottom side is clipped out
466             lprPosRect->bottom = lprClipRect->bottom;
467         }
468     }
469     *lprClipRect = *lprPosRect;
470     *lprPosRect  = bounds;
471 };
472
473 HRESULT VLCPlugin::onInit(void)
474 {
475     if( 0 == _i_vlc )
476     {
477         _i_vlc = VLC_Create();
478         if( _i_vlc < 0 )
479         {
480             _i_vlc = 0;
481             return E_FAIL;
482         }
483
484         /*
485         ** default initialization options
486         */
487         char *ppsz_argv[10] = { "vlc", "-vv" };
488         int   ppsz_argc = 2;
489
490         HKEY h_key;
491         DWORD i_type, i_data = MAX_PATH + 1;
492         char p_data[MAX_PATH + 1];
493         if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
494                           0, KEY_READ, &h_key ) == ERROR_SUCCESS )
495         {
496              if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
497                                   (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
498              {
499                  if( i_type == REG_SZ )
500                  {
501                      strcat( p_data, "\\vlc" );
502                      ppsz_argv[0] = p_data;
503                  }
504              }
505              RegCloseKey( h_key );
506         }
507
508 #if 0
509         ppsz_argv[0] = "C:\\cygwin\\home\\Damien_Fouilleul\\dev\\videolan\\vlc-trunk\\vlc";
510 #endif
511
512         if( IsDebuggerPresent() )
513         {
514             /*
515             ** VLC default threading mechanism is designed to be as compatible
516             ** with POSIX as possible, however when debugged on win32, threads
517             ** lose signals and eventually VLC get stuck during initialization.
518             ** threading support can be configured to be more debugging friendly
519             ** but it will be less compatible with POSIX.
520             ** This is done by initializing with the following options
521             */
522             ppsz_argv[ppsz_argc++] = "--fast-mutex";
523             ppsz_argv[ppsz_argc++] = "--win9x-cv-method=1";
524         }
525
526         if( VLC_Init(_i_vlc, ppsz_argc, ppsz_argv) )
527         {
528             VLC_Destroy(_i_vlc);
529             _i_vlc = 0;
530             return E_FAIL;
531         }
532         return S_OK;
533     }
534     return CO_E_ALREADYINITIALIZED;
535 };
536
537 HRESULT VLCPlugin::onLoad(void)
538 {
539     if( _b_mute )
540         VLC_VolumeMute(_i_vlc);
541
542     if( SysStringLen(_bstr_mrl) > 0 )
543     {
544         /*
545         ** try to combine MRL with client site moniker, which for Internet Explorer
546         ** is the URL of the page the plugin is embedded into. Hence, if the MRL
547         ** is a relative URL, we should end up with an absolute URL
548         */
549         IOleClientSite *pClientSite;
550         if( SUCCEEDED(vlcOleObject->GetClientSite(&pClientSite)) && (NULL != pClientSite) )
551         {
552             IBindCtx *pBC = 0;
553             if( SUCCEEDED(CreateBindCtx(0, &pBC)) )
554             {
555                 LPMONIKER pContMoniker = NULL;
556                 if( SUCCEEDED(pClientSite->GetMoniker(OLEGETMONIKER_ONLYIFTHERE,
557                                 OLEWHICHMK_CONTAINER, &pContMoniker)) )
558                 {
559                     LPOLESTR base_url;
560                     if( SUCCEEDED(pContMoniker->GetDisplayName(pBC, NULL, &base_url)) )
561                     {
562                         /*
563                         ** check that the moniker name is a URL
564                         */
565                         if( UrlIsW(base_url, URLIS_URL) )
566                         {
567                             DWORD len = INTERNET_MAX_URL_LENGTH;
568                             LPOLESTR abs_url = (LPOLESTR)CoTaskMemAlloc(sizeof(OLECHAR)*len);
569                             if( NULL != abs_url )
570                             {
571                                 if( SUCCEEDED(UrlCombineW(base_url, _bstr_mrl, abs_url, &len,
572                                                 URL_ESCAPE_UNSAFE|URL_PLUGGABLE_PROTOCOL)) )
573                                 {
574                                     SysFreeString(_bstr_mrl);
575                                     _bstr_mrl = SysAllocStringLen(abs_url, len);
576                                 }
577                                 CoTaskMemFree(abs_url);
578                             }
579                         }
580                         CoTaskMemFree(base_url);
581                     }
582                     pContMoniker->Release();
583                 }
584                 pBC->Release();
585             }
586             pClientSite->Release();
587         }
588
589         char *psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
590         if( NULL != psz_mrl )
591         {
592             // add default target to playlist
593             char *cOptions[1];
594             int  cOptionsCount = 0;
595
596             if( _b_autoloop )
597             {
598                 cOptions[cOptionsCount++] = "loop";
599             }
600             VLC_AddTarget(_i_vlc, psz_mrl, (const char **)&cOptions, cOptionsCount, PLAYLIST_APPEND, PLAYLIST_END);
601             CoTaskMemFree(psz_mrl);
602         }
603     }
604     setDirty(FALSE);
605     return S_OK;
606 };
607
608 HRESULT VLCPlugin::onAmbientChanged(LPUNKNOWN pContainer, DISPID dispID)
609 {
610     VARIANT v;
611     switch( dispID )
612     {
613         case DISPID_AMBIENT_BACKCOLOR:
614             break;
615         case DISPID_AMBIENT_DISPLAYNAME:
616             break;
617         case DISPID_AMBIENT_FONT:
618             break;
619         case DISPID_AMBIENT_FORECOLOR:
620             break;
621         case DISPID_AMBIENT_LOCALEID:
622             break;
623         case DISPID_AMBIENT_MESSAGEREFLECT:
624             break;
625         case DISPID_AMBIENT_SCALEUNITS:
626             break;
627         case DISPID_AMBIENT_TEXTALIGN:
628             break;
629         case DISPID_AMBIENT_USERMODE:
630             VariantInit(&v);
631             V_VT(&v) = VT_BOOL;
632             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
633             {
634                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
635             }
636             break;
637         case DISPID_AMBIENT_UIDEAD:
638             break;
639         case DISPID_AMBIENT_SHOWGRABHANDLES:
640             break;
641         case DISPID_AMBIENT_SHOWHATCHING:
642             break;
643         case DISPID_AMBIENT_DISPLAYASDEFAULT:
644             break;
645         case DISPID_AMBIENT_SUPPORTSMNEMONICS:
646             break;
647         case DISPID_AMBIENT_AUTOCLIP:
648             break;
649         case DISPID_AMBIENT_APPEARANCE:
650             break;
651         case DISPID_AMBIENT_CODEPAGE:
652             VariantInit(&v);
653             V_VT(&v) = VT_I4;
654             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
655             {
656                 setCodePage(V_I4(&v));
657             }
658             break;
659         case DISPID_AMBIENT_PALETTE:
660             break;
661         case DISPID_AMBIENT_CHARSET:
662             break;
663         case DISPID_AMBIENT_RIGHTTOLEFT:
664             break;
665         case DISPID_AMBIENT_TOPTOBOTTOM:
666             break;
667         case DISPID_UNKNOWN:
668         /*
669         ** multiple property change, look up the ones we are interested in
670         */
671             VariantInit(&v);
672             V_VT(&v) = VT_BOOL;
673             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_USERMODE, v)) )
674             {
675                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
676             }
677             VariantInit(&v);
678             V_VT(&v) = VT_I4;
679             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_CODEPAGE, v)) )
680             {
681                 setCodePage(V_I4(&v));
682             }
683             break;
684     }
685     return S_OK;
686 };
687
688 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
689 {
690     if( _i_vlc )
691     {
692         int i_vlc = _i_vlc;
693
694         _i_vlc = 0;
695         if( isInPlaceActive() )
696         {
697             onInPlaceDeactivate();
698         }
699         vlcDataObject->onClose();
700
701         VLC_CleanUp(i_vlc);
702         VLC_Destroy(i_vlc);
703     }
704     return S_OK;
705 };
706
707 BOOL VLCPlugin::isInPlaceActive(void)
708 {
709     return (NULL != _inplacewnd);
710 };
711
712 HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
713 {
714     RECT posRect = *lprcPosRect;
715     RECT clipRect = *lprcClipRect;
716
717     /*
718     ** record keeping of control geometry within container
719     */ 
720     _posRect = posRect;
721
722     /*
723     ** convert posRect & clipRect to match control viewport coordinates
724     */
725     getViewportCoords(&posRect, &clipRect);
726
727     /*
728     ** Create a window for in place activated control.
729     ** the window geometry matches the control viewport
730     ** within container so that embedded video is always
731     ** properly clipped.
732     */
733     _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
734             "VLC Plugin In-Place Window",
735             WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
736             clipRect.left,
737             clipRect.top,
738             clipRect.right-clipRect.left,
739             clipRect.bottom-clipRect.top,
740             hwndParent,
741             0,
742             _p_class->getHInstance(),
743             NULL
744            );
745
746     if( NULL == _inplacewnd )
747         return E_FAIL;
748
749     SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
750
751     /*
752     ** VLC embedded video automatically grows to cover client
753     ** area of parent window.
754     ** hence create a such a 'parent' window whose geometry
755     ** is always correct relative to the viewport bounds
756     */
757     _videownd = CreateWindow(_p_class->getVideoWndClassName(),
758             "VLC Plugin Video Window",
759             WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
760             posRect.left,
761             posRect.top,
762             posRect.right-posRect.left,
763             posRect.bottom-posRect.top,
764             _inplacewnd,
765             0,
766             _p_class->getHInstance(),
767             NULL
768            );
769
770     if( NULL == _videownd )
771     {
772         DestroyWindow(_inplacewnd);
773         return E_FAIL;
774     }
775
776     SetWindowLongPtr(_videownd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
777
778     if( getVisible() )
779         ShowWindow(_inplacewnd, SW_SHOWNORMAL);
780
781     /* set internal video width and height */
782     vlc_value_t val;
783     val.i_int = posRect.right-posRect.left;
784     VLC_VariableSet(_i_vlc, "conf::width", val);
785     val.i_int = posRect.bottom-posRect.top;
786     VLC_VariableSet(_i_vlc, "conf::height", val);
787
788     /* set internal video parent window */
789     /* horrible cast there */
790     val.i_int = reinterpret_cast<int>(_videownd);
791     VLC_VariableSet(_i_vlc, "drawable", val);
792
793     if( _b_usermode && _b_autoplay & (VLC_PlaylistNumberOfItems(_i_vlc) > 0) )
794     {
795         VLC_Play(_i_vlc);
796         fireOnPlayEvent();
797     }
798     return S_OK;
799 };
800
801 HRESULT VLCPlugin::onInPlaceDeactivate(void)
802 {
803     VLC_Stop(_i_vlc);
804     fireOnStopEvent();
805
806     DestroyWindow(_videownd);
807     _videownd = NULL;
808     DestroyWindow(_inplacewnd);
809     _inplacewnd = NULL;
810  
811     return S_OK;
812 };
813
814 void VLCPlugin::setVisible(BOOL fVisible)
815 {
816     _b_visible = fVisible;
817     if( isInPlaceActive() )
818         ShowWindow(_inplacewnd, fVisible ? SW_SHOWNORMAL : SW_HIDE);
819     firePropChangedEvent(DISPID_Visible);
820 };
821
822 void VLCPlugin::setFocus(BOOL fFocus)
823 {
824     if( fFocus )
825         SetActiveWindow(_inplacewnd);
826 };
827
828 BOOL VLCPlugin::hasFocus(void)
829 {
830     return GetActiveWindow() == _inplacewnd;
831 };
832
833 void VLCPlugin::onDraw(DVTARGETDEVICE * ptd, HDC hicTargetDev,
834         HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds)
835 {
836     if( getVisible() )
837     {
838         long width = lprcBounds->right-lprcBounds->left;
839         long height = lprcBounds->bottom-lprcBounds->top;
840
841         RECT bounds = { lprcBounds->left, lprcBounds->top, lprcBounds->right, lprcBounds->bottom };
842         FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(WHITE_BRUSH));
843
844         LPPICTURE pict = getPicture();
845         if( NULL != pict )
846         {
847             OLE_XSIZE_HIMETRIC picWidth;
848             OLE_YSIZE_HIMETRIC picHeight;
849
850             pict->get_Width(&picWidth);
851             pict->get_Height(&picHeight);
852
853             SIZEL picSize = { picWidth, picHeight };
854
855             if( NULL != hicTargetDev )
856             {
857                 DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
858             }
859             else if( NULL != (hicTargetDev = CreateDevDC(ptd)) )
860             {
861                 DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
862                 DeleteDC(hicTargetDev);
863             }
864
865             if( picSize.cx > width-4 )
866                 picSize.cx = width-4;
867             if( picSize.cy > height-4 )
868                 picSize.cy = height-4;
869
870             LONG dstX = lprcBounds->left+(width-picSize.cx)/2;
871             LONG dstY = lprcBounds->top+(height-picSize.cy)/2;
872
873             if( NULL != lprcWBounds )
874             {
875                 RECT wBounds = { lprcWBounds->left, lprcWBounds->top, lprcWBounds->right, lprcWBounds->bottom };
876                 pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
877                         0L, picHeight, picWidth, -picHeight, &wBounds);
878             }
879             else 
880                 pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
881                         0L, picHeight, picWidth, -picHeight, NULL);
882
883             pict->Release();
884         }
885
886         SelectObject(hdcDraw, GetStockObject(BLACK_BRUSH));
887
888         MoveToEx(hdcDraw, bounds.left, bounds.top, NULL);
889         LineTo(hdcDraw, bounds.left+width-1, bounds.top);
890         LineTo(hdcDraw, bounds.left+width-1, bounds.top+height-1);
891         LineTo(hdcDraw, bounds.left, bounds.top+height-1);
892         LineTo(hdcDraw, bounds.left, bounds.top);
893     }
894 };
895
896 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &clipRect)
897 {
898     if( getVisible() )
899     {
900         /** if VLC is playing, it may not display any VIDEO content 
901         ** hence, draw control logo*/
902         HDC hdcDraw = CreateCompatibleDC(hdc);
903         if( NULL != hdcDraw )
904         {
905             SIZEL size = getExtent();
906             DPFromHimetric(hdc, (LPPOINT)&size, 1);
907             RECTL posRect = { 0, 0, size.cx, size.cy };
908
909             int width = bounds.right-bounds.left;
910             int height = bounds.bottom-bounds.top;
911
912             HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
913             if( NULL != hBitmap )
914             {
915                 HBITMAP oldBmp = (HBITMAP)SelectObject(hdcDraw, hBitmap);
916
917                 if( (size.cx != width) || (size.cy != height) )
918                 {
919                     // needs to scale canvas
920                     SetMapMode(hdcDraw, MM_ANISOTROPIC);
921                     SetWindowExtEx(hdcDraw, size.cx, size.cy, NULL);
922                     SetViewportExtEx(hdcDraw, width, height, NULL);
923                 }
924
925                 onDraw(NULL, hdc, hdcDraw, &posRect, NULL);
926
927                 SetMapMode(hdcDraw, MM_TEXT);
928                 BitBlt(hdc, bounds.left, bounds.top,
929                         width, height,
930                         hdcDraw, 0, 0,
931                         SRCCOPY);
932
933                 SelectObject(hdcDraw, oldBmp);
934                 DeleteObject(hBitmap);
935             }
936             DeleteDC(hdcDraw);
937         }
938     }
939 };
940
941 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
942 {
943     RECT clipRect = *lprcClipRect;
944     RECT posRect  = *lprcPosRect;
945
946     //RedrawWindow(GetParent(_inplacewnd), &_posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
947
948     /*
949     ** record keeping of control geometry within container
950     */
951     _posRect = posRect;
952
953     /*
954     ** convert posRect & clipRect to match control viewport coordinates
955     */
956     getViewportCoords(&posRect, &clipRect);
957
958     /*
959     ** change in-place window geometry to match clipping region
960     */
961     SetWindowPos(_inplacewnd, NULL,
962             clipRect.left,
963             clipRect.top,
964             clipRect.right-clipRect.left,
965             clipRect.bottom-clipRect.top,
966             SWP_NOACTIVATE|
967             SWP_NOCOPYBITS|
968             SWP_NOZORDER|
969             SWP_NOOWNERZORDER );
970
971     /*
972     ** change video window geometry to match object bounds within clipping region
973     */
974     SetWindowPos(_videownd, NULL,
975             posRect.left,
976             posRect.top,
977             posRect.right-posRect.left,
978             posRect.bottom-posRect.top,
979             SWP_NOACTIVATE|
980             SWP_NOCOPYBITS|
981             SWP_NOZORDER|
982             SWP_NOOWNERZORDER );
983
984     //RedrawWindow(_videownd, &posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
985     vlc_value_t val;
986     val.i_int = posRect.right-posRect.left;
987     VLC_VariableSet(_i_vlc, "conf::width", val);
988     val.i_int = posRect.bottom-posRect.top;
989     VLC_VariableSet(_i_vlc, "conf::height", val);
990 };
991
992 void VLCPlugin::freezeEvents(BOOL freeze)
993 {
994     vlcConnectionPointContainer->freezeEvents(freeze);
995 };
996
997 void VLCPlugin::firePropChangedEvent(DISPID dispid)
998 {
999     vlcConnectionPointContainer->firePropChangedEvent(dispid); 
1000 };
1001
1002 void VLCPlugin::fireOnPlayEvent(void)
1003 {
1004     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1005     vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs); 
1006 };
1007
1008 void VLCPlugin::fireOnPauseEvent(void)
1009 {
1010     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1011     vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs); 
1012 };
1013
1014 void VLCPlugin::fireOnStopEvent(void)
1015 {
1016     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1017     vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs); 
1018 };
1019