Back to index

lightning-sunbird  0.9+nobinonly
ControlSite.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Adam Lock <adamlock@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #ifndef CONTROLSITE_H
00039 #define CONTROLSITE_H
00040 
00041 #include "IOleCommandTargetImpl.h"
00042 
00043 #include "PropertyList.h"
00044 
00045 // If you created a class derived from CControlSite, use the following macro
00046 // in the interface map of the derived class to include all the necessary
00047 // interfaces.
00048 #define CCONTROLSITE_INTERFACES() \
00049     COM_INTERFACE_ENTRY(IOleWindow) \
00050     COM_INTERFACE_ENTRY(IOleClientSite) \
00051     COM_INTERFACE_ENTRY(IOleInPlaceSite) \
00052     COM_INTERFACE_ENTRY_IID(IID_IOleInPlaceSite, IOleInPlaceSiteWindowless) \
00053     COM_INTERFACE_ENTRY_IID(IID_IOleInPlaceSiteEx, IOleInPlaceSiteWindowless) \
00054     COM_INTERFACE_ENTRY(IOleControlSite) \
00055     COM_INTERFACE_ENTRY(IDispatch) \
00056     COM_INTERFACE_ENTRY_IID(IID_IAdviseSink, IAdviseSinkEx) \
00057     COM_INTERFACE_ENTRY_IID(IID_IAdviseSink2, IAdviseSinkEx) \
00058     COM_INTERFACE_ENTRY_IID(IID_IAdviseSinkEx, IAdviseSinkEx) \
00059     COM_INTERFACE_ENTRY(IOleCommandTarget) \
00060     COM_INTERFACE_ENTRY(IServiceProvider) \
00061     COM_INTERFACE_ENTRY(IBindStatusCallback) \
00062     COM_INTERFACE_ENTRY(IWindowForBindingUI)
00063 
00064 // Temoporarily removed by bug 200680. Stops controls misbehaving and calling
00065 // windowless methods when they shouldn't.
00066 //     COM_INTERFACE_ENTRY_IID(IID_IOleInPlaceSiteWindowless, IOleInPlaceSiteWindowless) \
00067 
00068 
00069 // Class that defines the control's security policy with regards to
00070 // what controls it hosts etc.
00071 
00072 class CControlSiteSecurityPolicy
00073 {
00074 public:
00075     // Test if the class is safe to host
00076     virtual BOOL IsClassSafeToHost(const CLSID & clsid) = 0;
00077     // Test if the specified class is marked safe for scripting
00078     virtual BOOL IsClassMarkedSafeForScripting(const CLSID & clsid, BOOL &bClassExists) = 0;
00079     // Test if the instantiated object is safe for scripting on the specified interface
00080     virtual BOOL IsObjectSafeForScripting(IUnknown *pObject, const IID &iid) = 0;
00081 };
00082 
00083 //
00084 // Class for hosting an ActiveX control
00085 //
00086 // This class supports both windowed and windowless classes. The normal
00087 // steps to hosting a control are this:
00088 //
00089 //   CControlSiteInstance *pSite = NULL;
00090 //   CControlSiteInstance::CreateInstance(&pSite);
00091 //   pSite->AddRef();
00092 //   pSite->Create(clsidControlToCreate);
00093 //   pSite->Attach(hwndParentWindow, rcPosition);
00094 //
00095 // Where propertyList is a named list of values to initialise the new object
00096 // with, hwndParentWindow is the window in which the control is being created,
00097 // and rcPosition is the position in window coordinates where the control will
00098 // be rendered.
00099 //
00100 // Destruction is this:
00101 //
00102 //   pSite->Detach();
00103 //   pSite->Release();
00104 //   pSite = NULL;
00105 
00106 class CControlSite :    public CComObjectRootEx<CComSingleThreadModel>,
00107                         public CControlSiteSecurityPolicy,
00108                         public IOleClientSite,
00109                         public IOleInPlaceSiteWindowless,
00110                         public IOleControlSite,
00111                         public IAdviseSinkEx,
00112                         public IDispatch,
00113                         public IServiceProvider,
00114                         public IOleCommandTargetImpl<CControlSite>,
00115                         public IBindStatusCallback,
00116                         public IWindowForBindingUI
00117 {
00118 public:
00119 // Site management values
00120     // Handle to parent window
00121     HWND m_hWndParent;
00122     // Position of the site and the contained object
00123     RECT m_rcObjectPos;
00124     // Flag indicating if client site should be set early or late
00125     unsigned m_bSetClientSiteFirst:1;
00126     // Flag indicating whether control is visible or not
00127     unsigned m_bVisibleAtRuntime:1;
00128     // Flag indicating if control is in-place active
00129     unsigned m_bInPlaceActive:1;
00130     // Flag indicating if control is UI active
00131     unsigned m_bUIActive:1;
00132     // Flag indicating if control is in-place locked and cannot be deactivated
00133     unsigned m_bInPlaceLocked:1;
00134     // Flag indicating if the site allows windowless controls
00135     unsigned m_bSupportWindowlessActivation:1;
00136     // Flag indicating if control is windowless (after being created)
00137     unsigned m_bWindowless:1;
00138     // Flag indicating if only safely scriptable controls are allowed
00139     unsigned m_bSafeForScriptingObjectsOnly:1;
00140     // Pointer to an externally registered service provider
00141     CComPtr<IServiceProvider> m_spServiceProvider;
00142     // Pointer to the OLE container
00143     CComPtr<IOleContainer> m_spContainer;
00144     // Return the default security policy object
00145     static CControlSiteSecurityPolicy *GetDefaultControlSecurityPolicy();
00146 
00147 protected:
00148 // Pointers to object interfaces
00149     // Raw pointer to the object
00150     CComPtr<IUnknown> m_spObject;
00151     // Pointer to objects IViewObject interface
00152     CComQIPtr<IViewObject, &IID_IViewObject> m_spIViewObject;
00153     // Pointer to object's IOleObject interface
00154     CComQIPtr<IOleObject, &IID_IOleObject> m_spIOleObject;
00155     // Pointer to object's IOleInPlaceObject interface
00156     CComQIPtr<IOleInPlaceObject, &IID_IOleInPlaceObject> m_spIOleInPlaceObject;
00157     // Pointer to object's IOleInPlaceObjectWindowless interface
00158     CComQIPtr<IOleInPlaceObjectWindowless, &IID_IOleInPlaceObjectWindowless> m_spIOleInPlaceObjectWindowless;
00159     // CLSID of the control
00160     CLSID m_CLSID;
00161     // Parameter list
00162     PropertyList m_ParameterList;
00163     // Pointer to the security policy
00164     CControlSiteSecurityPolicy *m_pSecurityPolicy;
00165 
00166 // Binding variables
00167     // Flag indicating whether binding is in progress
00168     unsigned m_bBindingInProgress;
00169     // Result from the binding operation
00170     HRESULT m_hrBindResult;
00171 
00172 // Double buffer drawing variables used for windowless controls
00173     // Area of buffer
00174     RECT m_rcBuffer;
00175     // Bitmap to buffer
00176     HBITMAP m_hBMBuffer;
00177     // Bitmap to buffer
00178     HBITMAP m_hBMBufferOld;
00179     // Device context
00180     HDC m_hDCBuffer;
00181     // Clipping area of site
00182     HRGN m_hRgnBuffer;
00183     // Flags indicating how the buffer was painted
00184     DWORD m_dwBufferFlags;
00185 
00186 // Ambient properties
00187     // Locale ID
00188     LCID m_nAmbientLocale;
00189     // Foreground colour
00190     COLORREF m_clrAmbientForeColor;
00191     // Background colour
00192     COLORREF m_clrAmbientBackColor;
00193     // Flag indicating if control should hatch itself
00194     bool m_bAmbientShowHatching:1;
00195     // Flag indicating if control should have grab handles
00196     bool m_bAmbientShowGrabHandles:1;
00197     // Flag indicating if control is in edit/user mode
00198     bool m_bAmbientUserMode:1;
00199     // Flag indicating if control has a 3d border or not
00200     bool m_bAmbientAppearance:1;
00201 
00202 protected:
00203     // Notifies the attached control of a change to an ambient property
00204     virtual void FireAmbientPropertyChange(DISPID id);
00205 
00206 public:
00207 // Construction and destruction
00208     // Constructor
00209     CControlSite();
00210     // Destructor
00211     virtual ~CControlSite();
00212 
00213 BEGIN_COM_MAP(CControlSite)
00214     CCONTROLSITE_INTERFACES()
00215 END_COM_MAP()
00216 
00217 BEGIN_OLECOMMAND_TABLE()
00218 END_OLECOMMAND_TABLE()
00219 
00220     // Returns the window used when processing ole commands
00221     HWND GetCommandTargetWindow()
00222     {
00223         return NULL; // TODO
00224     }
00225 
00226 // Object creation and management functions
00227     // Creates and initialises an object
00228     virtual HRESULT Create(REFCLSID clsid, PropertyList &pl = PropertyList(),
00229         LPCWSTR szCodebase = NULL, IBindCtx *pBindContext = NULL);
00230     // Attaches the object to the site
00231     virtual HRESULT Attach(HWND hwndParent, const RECT &rcPos, IUnknown *pInitStream = NULL);
00232     // Detaches the object from the site
00233     virtual HRESULT Detach();
00234     // Returns the IUnknown pointer for the object
00235     virtual HRESULT GetControlUnknown(IUnknown **ppObject);
00236     // Sets the bounding rectangle for the object
00237     virtual HRESULT SetPosition(const RECT &rcPos);
00238     // Draws the object using the provided DC
00239     virtual HRESULT Draw(HDC hdc);
00240     // Performs the specified action on the object
00241     virtual HRESULT DoVerb(LONG nVerb, LPMSG lpMsg = NULL);
00242     // Sets an advise sink up for changes to the object
00243     virtual HRESULT Advise(IUnknown *pIUnkSink, const IID &iid, DWORD *pdwCookie);
00244     // Removes an advise sink
00245     virtual HRESULT Unadvise(const IID &iid, DWORD dwCookie);
00246     // Register an external service provider object
00247     virtual void SetServiceProvider(IServiceProvider *pSP)
00248     {
00249         m_spServiceProvider = pSP;
00250     }
00251     virtual void SetContainer(IOleContainer *pContainer)
00252     {
00253         m_spContainer = pContainer;
00254     }
00255     // Set the security policy object. Ownership of this object remains with the caller and the security
00256     // policy object is meant to exist for as long as it is set here.
00257     virtual void SetSecurityPolicy(CControlSiteSecurityPolicy *pSecurityPolicy)
00258     {
00259         m_pSecurityPolicy = pSecurityPolicy;
00260     }
00261     virtual CControlSiteSecurityPolicy *GetSecurityPolicy() const
00262     {
00263         return m_pSecurityPolicy;
00264     }
00265 
00266 // Methods to set ambient properties
00267     virtual void SetAmbientUserMode(BOOL bUser);
00268 
00269 // Inline helper methods
00270     // Returns the object's CLSID
00271     virtual const CLSID &GetObjectCLSID() const
00272     {
00273         return m_CLSID;
00274     }
00275     // Tests if the object is valid or not
00276     virtual BOOL IsObjectValid() const
00277     {
00278         return (m_spObject) ? TRUE : FALSE;
00279     }
00280     // Returns the parent window to this one
00281     virtual HWND GetParentWindow() const
00282     {
00283         return m_hWndParent;
00284     }
00285     // Returns the inplace active state of the object
00286     virtual BOOL IsInPlaceActive() const
00287     {
00288         return m_bInPlaceActive;
00289     }
00290 
00291 // CControlSiteSecurityPolicy
00292     // Test if the class is safe to host
00293     virtual BOOL IsClassSafeToHost(const CLSID & clsid);
00294     // Test if the specified class is marked safe for scripting
00295     virtual BOOL IsClassMarkedSafeForScripting(const CLSID & clsid, BOOL &bClassExists);
00296     // Test if the instantiated object is safe for scripting on the specified interface
00297     virtual BOOL IsObjectSafeForScripting(IUnknown *pObject, const IID &iid);
00298     // Test if the instantiated object is safe for scripting on the specified interface
00299     virtual BOOL IsObjectSafeForScripting(const IID &iid);
00300 
00301 // IServiceProvider
00302     virtual HRESULT STDMETHODCALLTYPE QueryService(REFGUID guidService, REFIID riid, void** ppv);
00303 
00304 // IDispatch
00305     virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(/* [out] */ UINT __RPC_FAR *pctinfo);
00306     virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(/* [in] */ UINT iTInfo, /* [in] */ LCID lcid, /* [out] */ ITypeInfo __RPC_FAR *__RPC_FAR *ppTInfo);
00307     virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(/* [in] */ REFIID riid, /* [size_is][in] */ LPOLESTR __RPC_FAR *rgszNames, /* [in] */ UINT cNames, /* [in] */ LCID lcid, /* [size_is][out] */ DISPID __RPC_FAR *rgDispId);
00308     virtual /* [local] */ HRESULT STDMETHODCALLTYPE Invoke(/* [in] */ DISPID dispIdMember, /* [in] */ REFIID riid, /* [in] */ LCID lcid, /* [in] */ WORD wFlags, /* [out][in] */ DISPPARAMS __RPC_FAR *pDispParams, /* [out] */ VARIANT __RPC_FAR *pVarResult, /* [out] */ EXCEPINFO __RPC_FAR *pExcepInfo, /* [out] */ UINT __RPC_FAR *puArgErr);
00309 
00310 // IAdviseSink implementation
00311     virtual /* [local] */ void STDMETHODCALLTYPE OnDataChange(/* [unique][in] */ FORMATETC __RPC_FAR *pFormatetc, /* [unique][in] */ STGMEDIUM __RPC_FAR *pStgmed);
00312     virtual /* [local] */ void STDMETHODCALLTYPE OnViewChange(/* [in] */ DWORD dwAspect, /* [in] */ LONG lindex);
00313     virtual /* [local] */ void STDMETHODCALLTYPE OnRename(/* [in] */ IMoniker __RPC_FAR *pmk);
00314     virtual /* [local] */ void STDMETHODCALLTYPE OnSave(void);
00315     virtual /* [local] */ void STDMETHODCALLTYPE OnClose(void);
00316 
00317 // IAdviseSink2
00318     virtual /* [local] */ void STDMETHODCALLTYPE OnLinkSrcChange(/* [unique][in] */ IMoniker __RPC_FAR *pmk);
00319 
00320 // IAdviseSinkEx implementation
00321     virtual /* [local] */ void STDMETHODCALLTYPE OnViewStatusChange(/* [in] */ DWORD dwViewStatus);
00322 
00323 // IOleWindow implementation
00324     virtual /* [input_sync] */ HRESULT STDMETHODCALLTYPE GetWindow(/* [out] */ HWND __RPC_FAR *phwnd);
00325     virtual HRESULT STDMETHODCALLTYPE ContextSensitiveHelp(/* [in] */ BOOL fEnterMode);
00326 
00327 // IOleClientSite implementation
00328     virtual HRESULT STDMETHODCALLTYPE SaveObject(void);
00329     virtual HRESULT STDMETHODCALLTYPE GetMoniker(/* [in] */ DWORD dwAssign, /* [in] */ DWORD dwWhichMoniker, /* [out] */ IMoniker __RPC_FAR *__RPC_FAR *ppmk);
00330     virtual HRESULT STDMETHODCALLTYPE GetContainer(/* [out] */ IOleContainer __RPC_FAR *__RPC_FAR *ppContainer);
00331     virtual HRESULT STDMETHODCALLTYPE ShowObject(void);
00332     virtual HRESULT STDMETHODCALLTYPE OnShowWindow(/* [in] */ BOOL fShow);
00333     virtual HRESULT STDMETHODCALLTYPE RequestNewObjectLayout(void);
00334 
00335 // IOleInPlaceSite implementation
00336     virtual HRESULT STDMETHODCALLTYPE CanInPlaceActivate(void);
00337     virtual HRESULT STDMETHODCALLTYPE OnInPlaceActivate(void);
00338     virtual HRESULT STDMETHODCALLTYPE OnUIActivate(void);
00339     virtual HRESULT STDMETHODCALLTYPE GetWindowContext(/* [out] */ IOleInPlaceFrame __RPC_FAR *__RPC_FAR *ppFrame, /* [out] */ IOleInPlaceUIWindow __RPC_FAR *__RPC_FAR *ppDoc, /* [out] */ LPRECT lprcPosRect, /* [out] */ LPRECT lprcClipRect, /* [out][in] */ LPOLEINPLACEFRAMEINFO lpFrameInfo);
00340     virtual HRESULT STDMETHODCALLTYPE Scroll(/* [in] */ SIZE scrollExtant);
00341     virtual HRESULT STDMETHODCALLTYPE OnUIDeactivate(/* [in] */ BOOL fUndoable);
00342     virtual HRESULT STDMETHODCALLTYPE OnInPlaceDeactivate(void);
00343     virtual HRESULT STDMETHODCALLTYPE DiscardUndoState(void);
00344     virtual HRESULT STDMETHODCALLTYPE DeactivateAndUndo(void);
00345     virtual HRESULT STDMETHODCALLTYPE OnPosRectChange(/* [in] */ LPCRECT lprcPosRect);
00346 
00347 // IOleInPlaceSiteEx implementation
00348     virtual HRESULT STDMETHODCALLTYPE OnInPlaceActivateEx(/* [out] */ BOOL __RPC_FAR *pfNoRedraw, /* [in] */ DWORD dwFlags);
00349     virtual HRESULT STDMETHODCALLTYPE OnInPlaceDeactivateEx(/* [in] */ BOOL fNoRedraw);
00350     virtual HRESULT STDMETHODCALLTYPE RequestUIActivate(void);
00351 
00352 // IOleInPlaceSiteWindowless implementation
00353     virtual HRESULT STDMETHODCALLTYPE CanWindowlessActivate(void);
00354     virtual HRESULT STDMETHODCALLTYPE GetCapture(void);
00355     virtual HRESULT STDMETHODCALLTYPE SetCapture(/* [in] */ BOOL fCapture);
00356     virtual HRESULT STDMETHODCALLTYPE GetFocus(void);
00357     virtual HRESULT STDMETHODCALLTYPE SetFocus(/* [in] */ BOOL fFocus);
00358     virtual HRESULT STDMETHODCALLTYPE GetDC(/* [in] */ LPCRECT pRect, /* [in] */ DWORD grfFlags, /* [out] */ HDC __RPC_FAR *phDC);
00359     virtual HRESULT STDMETHODCALLTYPE ReleaseDC(/* [in] */ HDC hDC);
00360     virtual HRESULT STDMETHODCALLTYPE InvalidateRect(/* [in] */ LPCRECT pRect, /* [in] */ BOOL fErase);
00361     virtual HRESULT STDMETHODCALLTYPE InvalidateRgn(/* [in] */ HRGN hRGN, /* [in] */ BOOL fErase);
00362     virtual HRESULT STDMETHODCALLTYPE ScrollRect(/* [in] */ INT dx, /* [in] */ INT dy, /* [in] */ LPCRECT pRectScroll, /* [in] */ LPCRECT pRectClip);
00363     virtual HRESULT STDMETHODCALLTYPE AdjustRect(/* [out][in] */ LPRECT prc);
00364     virtual HRESULT STDMETHODCALLTYPE OnDefWindowMessage(/* [in] */ UINT msg, /* [in] */ WPARAM wParam, /* [in] */ LPARAM lParam, /* [out] */ LRESULT __RPC_FAR *plResult);
00365 
00366 // IOleControlSite implementation
00367     virtual HRESULT STDMETHODCALLTYPE OnControlInfoChanged(void);
00368     virtual HRESULT STDMETHODCALLTYPE LockInPlaceActive(/* [in] */ BOOL fLock);
00369     virtual HRESULT STDMETHODCALLTYPE GetExtendedControl(/* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppDisp);
00370     virtual HRESULT STDMETHODCALLTYPE TransformCoords(/* [out][in] */ POINTL __RPC_FAR *pPtlHimetric, /* [out][in] */ POINTF __RPC_FAR *pPtfContainer, /* [in] */ DWORD dwFlags);
00371     virtual HRESULT STDMETHODCALLTYPE TranslateAccelerator(/* [in] */ MSG __RPC_FAR *pMsg, /* [in] */ DWORD grfModifiers);
00372     virtual HRESULT STDMETHODCALLTYPE OnFocus(/* [in] */ BOOL fGotFocus);
00373     virtual HRESULT STDMETHODCALLTYPE ShowPropertyFrame( void);
00374 
00375 // IBindStatusCallback
00376     virtual HRESULT STDMETHODCALLTYPE OnStartBinding(/* [in] */ DWORD dwReserved, /* [in] */ IBinding __RPC_FAR *pib);
00377     virtual HRESULT STDMETHODCALLTYPE GetPriority(/* [out] */ LONG __RPC_FAR *pnPriority);
00378     virtual HRESULT STDMETHODCALLTYPE OnLowResource(/* [in] */ DWORD reserved);
00379     virtual HRESULT STDMETHODCALLTYPE OnProgress(/* [in] */ ULONG ulProgress, /* [in] */ ULONG ulProgressMax, /* [in] */ ULONG ulStatusCode, /* [in] */ LPCWSTR szStatusText);
00380     virtual HRESULT STDMETHODCALLTYPE OnStopBinding(/* [in] */ HRESULT hresult, /* [unique][in] */ LPCWSTR szError);
00381     virtual /* [local] */ HRESULT STDMETHODCALLTYPE GetBindInfo( /* [out] */ DWORD __RPC_FAR *grfBINDF, /* [unique][out][in] */ BINDINFO __RPC_FAR *pbindinfo);
00382     virtual /* [local] */ HRESULT STDMETHODCALLTYPE OnDataAvailable(/* [in] */ DWORD grfBSCF, /* [in] */ DWORD dwSize, /* [in] */ FORMATETC __RPC_FAR *pformatetc, /* [in] */ STGMEDIUM __RPC_FAR *pstgmed);
00383     virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(/* [in] */ REFIID riid, /* [iid_is][in] */ IUnknown __RPC_FAR *punk);
00384 
00385 // IWindowForBindingUI
00386     virtual HRESULT STDMETHODCALLTYPE GetWindow(/* [in] */ REFGUID rguidReason, /* [out] */ HWND *phwnd);
00387 };
00388 
00389 typedef CComObject<CControlSite> CControlSiteInstance;
00390 
00391 
00392 
00393 #endif