oleobject.cpp 10.2 KB
Newer Older
1 2 3
/*****************************************************************************
 * oleobject.cpp: ActiveX control for VLC
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include "plugin.h"
#include "oleobject.h"

#include "utils.h"

#include <docobj.h>

using namespace std;

VLCOleObject::VLCOleObject(VLCPlugin *p_instance) :
_p_clientsite(NULL), _p_instance(p_instance) 
{
    CreateOleAdviseHolder(&_p_advise_holder);
};

VLCOleObject::~VLCOleObject()
{
    _p_advise_holder->Release();
    SetClientSite(NULL); 
};

STDMETHODIMP VLCOleObject::Advise(IAdviseSink *pAdvSink, DWORD *dwConnection)
{
    return _p_advise_holder->Advise(pAdvSink, dwConnection);
};

STDMETHODIMP VLCOleObject::Close(DWORD dwSaveOption)
{
    _p_advise_holder->SendOnClose();
    OleFlushClipboard();
    return _p_instance->onClose(dwSaveOption);
};

STDMETHODIMP VLCOleObject::DoVerb(LONG iVerb, LPMSG lpMsg, LPOLECLIENTSITE pActiveSite,
                                    LONG lIndex, HWND hwndParent, LPCRECT lprcPosRect)
{
    switch( iVerb )
    {
        case OLEIVERB_PRIMARY:
        case OLEIVERB_SHOW:
        case OLEIVERB_OPEN:
        case OLEIVERB_INPLACEACTIVATE:
65
            return doInPlaceActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect);
66 67 68 69 70 71 72 73 74 75 76 77

        case OLEIVERB_HIDE:
            _p_instance->setVisible(FALSE);
            return S_OK;

        case OLEIVERB_UIACTIVATE:
            return doUIActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect);

        case OLEIVERB_DISCARDUNDOSTATE:
            return S_OK;

        default:
78
            return OLEOBJ_S_INVALIDVERB;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    }
};

HRESULT VLCOleObject::doInPlaceActivate(LPMSG lpMsg, LPOLECLIENTSITE pActiveSite, HWND hwndParent, LPCRECT lprcPosRect)
{
    RECT posRect;
    RECT clipRect;
    LPCRECT lprcClipRect = lprcPosRect;

    if( NULL != pActiveSite )
    {
        // check if already activated
        if( _p_instance->isInPlaceActive() )
        {
            // just attempt to show object then
            pActiveSite->ShowObject();
            _p_instance->setVisible(TRUE);
            return S_OK;
        }

        LPOLEINPLACESITE p_inPlaceSite;

        if( SUCCEEDED(pActiveSite->QueryInterface(IID_IOleInPlaceSite, (void**)&p_inPlaceSite)) )
        {
            if( S_OK != p_inPlaceSite->CanInPlaceActivate() )
104
            {
105
                return OLEOBJ_S_CANNOT_DOVERB_NOW;
106
            }
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

            LPOLEINPLACEFRAME p_inPlaceFrame;
            LPOLEINPLACEUIWINDOW p_inPlaceUIWindow;
            OLEINPLACEFRAMEINFO oleFrameInfo;

            if( SUCCEEDED(p_inPlaceSite->GetWindowContext(&p_inPlaceFrame, &p_inPlaceUIWindow, &posRect, &clipRect, &oleFrameInfo)) )
            {
                lprcPosRect = &posRect;
                lprcClipRect = &clipRect;

                if( NULL != p_inPlaceFrame )
                    p_inPlaceFrame->Release();
                if( NULL != p_inPlaceUIWindow )
                    p_inPlaceUIWindow->Release();
            }
122 123 124 125 126 127

            if( (NULL == hwndParent) && FAILED(p_inPlaceSite->GetWindow(&hwndParent)) )
            {
                p_inPlaceSite->Release();
                return OLEOBJ_S_INVALIDHWND;
            }
128
        }
129
        else if( NULL == hwndParent )
130
        {
131
            return OLEOBJ_S_INVALIDHWND;
132
        }
133 134

        if( FAILED(_p_instance->onActivateInPlace(lpMsg, hwndParent, lprcPosRect, lprcClipRect)) )
135 136 137 138 139
        {
            if( NULL != p_inPlaceSite )
                p_inPlaceSite->Release();
            return OLEOBJ_S_CANNOT_DOVERB_NOW;
        }
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

        if( NULL != p_inPlaceSite )
            p_inPlaceSite->OnPosRectChange(lprcPosRect);

        pActiveSite->ShowObject();
        _p_instance->setVisible(TRUE);

        if( NULL != p_inPlaceSite )
        {
            p_inPlaceSite->OnInPlaceActivate();
            p_inPlaceSite->Release();
        }

        if( NULL != lpMsg )
        {
            switch( lpMsg->message )
            {
                case WM_LBUTTONDOWN:
                case WM_LBUTTONDBLCLK:
                    doUIActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect);
                    break;
                default:
                    break;
            }
        }
        return S_OK;
    }
167
    return OLEOBJ_S_CANNOT_DOVERB_NOW;
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
};

HRESULT VLCOleObject::doUIActivate(LPMSG lpMsg, LPOLECLIENTSITE pActiveSite, HWND hwndParent, LPCRECT lprcPosRect)
{
    if( NULL != pActiveSite )
    {
        // check if already activated
        if( ! _p_instance->isInPlaceActive() )
            return OLE_E_NOT_INPLACEACTIVE;

        LPOLEINPLACESITE p_inPlaceSite;

        if( SUCCEEDED(pActiveSite->QueryInterface(IID_IOleInPlaceSite, (void**)&p_inPlaceSite)) )
        {
            p_inPlaceSite->OnUIActivate();

            if( NULL != lprcPosRect )
            {
                p_inPlaceSite->OnPosRectChange(lprcPosRect);
            }
            p_inPlaceSite->Release();
        }

        pActiveSite->ShowObject();
        _p_instance->setVisible(TRUE);
        _p_instance->setFocus(TRUE);

        return S_OK;
    }
    return E_FAIL;
};

STDMETHODIMP VLCOleObject::EnumAdvise(IEnumSTATDATA **ppEnumAdvise)
{
    return _p_advise_holder->EnumAdvise(ppEnumAdvise);
};

STDMETHODIMP VLCOleObject::EnumVerbs(IEnumOleVerb **ppEnumOleVerb)
{
    return OLE_S_USEREG;
};

STDMETHODIMP VLCOleObject::GetClientSite(LPOLECLIENTSITE *ppClientSite)
{
    if( NULL == ppClientSite )
        return E_POINTER;
 
    if( NULL != _p_clientsite )
        _p_clientsite->AddRef(); 

218
    *ppClientSite = _p_clientsite;
219 220 221 222 223 224 225 226 227 228
    return S_OK;
};

STDMETHODIMP VLCOleObject::GetClipboardData(DWORD dwReserved, LPDATAOBJECT *ppDataObject)
{
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::GetExtent(DWORD dwDrawAspect, SIZEL *pSizel)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
229 230 231 232 233 234 235 236 237 238
    if( NULL == pSizel )
        return E_POINTER;

    if( dwDrawAspect & DVASPECT_CONTENT )
    {
        *pSizel = _p_instance->getExtent();
        return S_OK;
    }
    pSizel->cx= 0L;
    pSizel->cy= 0L;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::GetMiscStatus(DWORD dwAspect, DWORD *pdwStatus)
{
    if( NULL != pdwStatus )
        return E_POINTER;

    switch( dwAspect )
    {
        case DVASPECT_CONTENT:
            *pdwStatus = OLEMISC_RECOMPOSEONRESIZE
                | OLEMISC_CANTLINKINSIDE
                | OLEMISC_INSIDEOUT
                | OLEMISC_ACTIVATEWHENVISIBLE
                | OLEMISC_SETCLIENTSITEFIRST;
            break;
        default:
            *pdwStatus = 0;
    }

    return S_OK;
};

STDMETHODIMP VLCOleObject::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, LPMONIKER *ppMoniker)
{
    if( NULL != _p_clientsite )
        return _p_clientsite->GetMoniker(dwAssign,dwWhichMoniker, ppMoniker);
 
    return E_UNEXPECTED;
};

STDMETHODIMP VLCOleObject::GetUserClassID(LPCLSID pClsid)
{
    if( NULL == pClsid )
        return E_POINTER;
 
    pClsid = const_cast<LPCLSID>(&_p_instance->getClassID()); 
    return S_OK;
};

STDMETHODIMP VLCOleObject::GetUserType(DWORD dwFormOfType, LPOLESTR *pszUserType)
{
    return OLE_S_USEREG;
};

STDMETHODIMP VLCOleObject::InitFromData(LPDATAOBJECT pDataObject, BOOL fCreation, DWORD dwReserved)
{
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::IsUpToDate(void)
{
    return S_OK;
};

STDMETHODIMP VLCOleObject::SetClientSite(LPOLECLIENTSITE pClientSite)
{
 
    if( NULL != pClientSite )
    {
        pClientSite->AddRef();

        /*
        ** retrieve container ambient properties
        */
        VARIANT v;
        VariantInit(&v);
        V_VT(&v) = VT_I4;
        if( SUCCEEDED(GetObjectProperty(pClientSite, DISPID_AMBIENT_CODEPAGE, v)) )
        {
            _p_instance->setCodePage(V_I4(&v));
            VariantClear(&v);
        }
    }
314 315 316 317

    if( NULL != _p_clientsite )
        _p_clientsite->Release();

318
    _p_clientsite = pClientSite;
319
    _p_instance->onClientSiteChanged(pClientSite);
320

321 322 323 324 325 326 327 328 329 330
    return S_OK;
};

STDMETHODIMP VLCOleObject::SetColorScheme(LOGPALETTE *pLogpal)
{
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::SetExtent(DWORD dwDrawAspect, SIZEL *pSizel)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
331 332 333 334 335 336 337 338 339 340 341 342 343
    if( NULL == pSizel )
        return E_POINTER;

    if( dwDrawAspect & DVASPECT_CONTENT )
    {
        _p_instance->setExtent(*pSizel);

        if( _p_instance->isInPlaceActive() )
        {
            LPOLEINPLACESITE p_inPlaceSite;

            if( SUCCEEDED(_p_clientsite->QueryInterface(IID_IOleInPlaceSite, (void**)&p_inPlaceSite)) )
            {
344
                HWND hwnd;
Damien Fouilleul's avatar
Damien Fouilleul committed
345

346
                if( SUCCEEDED(p_inPlaceSite->GetWindow(&hwnd)) )
Damien Fouilleul's avatar
Damien Fouilleul committed
347
                {
348 349 350
                    // use HIMETRIC to pixel transform 
                    RECT posRect = _p_instance->getPosRect();
                    HDC hDC = GetDC(hwnd);
Damien Fouilleul's avatar
Damien Fouilleul committed
351 352 353
                    posRect.right = (pSizel->cx*GetDeviceCaps(hDC, LOGPIXELSX)/2540L)+posRect.left;
                    posRect.bottom = (pSizel->cy*GetDeviceCaps(hDC, LOGPIXELSY)/2540L)+posRect.top;
                    DeleteDC(hDC);
354
                    p_inPlaceSite->OnPosRectChange(&posRect);
Damien Fouilleul's avatar
Damien Fouilleul committed
355 356 357 358 359 360
                }
                p_inPlaceSite->Release();
            }
        }
        return S_OK;
    }
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::SetHostNames(LPCOLESTR szContainerApp, LPCOLESTR szContainerObj)
{
    return S_OK;
};

STDMETHODIMP VLCOleObject::SetMoniker(DWORD dwWhichMoniker, LPMONIKER pMoniker)
{
    return _p_advise_holder->SendOnRename(pMoniker);
};

STDMETHODIMP VLCOleObject::Unadvise(DWORD dwConnection)
{
    return _p_advise_holder->Unadvise(dwConnection);
};

STDMETHODIMP VLCOleObject::Update(void)
{
    return S_OK;
};