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