home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / oleimpl2.h < prev    next >
C/C++ Source or Header  |  1998-06-16  |  11KB  |  326 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. // Note: Must include AFXOLE.H first
  12.  
  13. #undef AFX_DATA
  14. #define AFX_DATA AFX_OLE_DATA
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // COleFrameHook - AFX_INTERNAL
  18.  
  19. class COleFrameHook : public CCmdTarget
  20. {
  21. // Construction & Destruction
  22. public:
  23.     COleFrameHook(CFrameWnd* pFrameWnd, COleClientItem* pItem);
  24.  
  25. // Implementation
  26. public:
  27.     ~COleFrameHook();
  28.  
  29.     CFrameWnd* m_pFrameWnd;
  30.     LPOLEINPLACEACTIVEOBJECT m_lpActiveObject;
  31.     COleClientItem* m_pActiveItem;  // item this COleFrameHook is for
  32.     HWND m_hWnd;            // actual HWND this hook is attached to
  33.     BOOL m_bInModalState;   // TRUE if EnableModeless(FALSE) has been called
  34.     BOOL m_bToolBarHidden;  // TRUE if toolbar needs to be shown OnUIDeactivate
  35.     HACCEL m_hAccelTable;   // accelerator to be used while in-place object active
  36.     UINT m_nModelessCount;  // !0 if server's EnableModeless has been called
  37.     CString m_strObjName;   // name of the active in-place object
  38.  
  39. // Overrides for implementation
  40. public:
  41.     virtual void OnRecalcLayout();  // for border space re-negotiation
  42.     virtual BOOL OnPreTranslateMessage(MSG* pMsg);
  43.     virtual void OnActivate(BOOL bActive); // for OnFrameWindowActivate
  44.     virtual BOOL OnDocActivate(BOOL bActive);   // for OnDocWindowActivate
  45.     virtual BOOL OnContextHelp(BOOL bEnter);
  46.     virtual void OnEnableModeless(BOOL bEnable);
  47.     virtual BOOL OnUpdateFrameTitle();
  48.     virtual void OnPaletteChanged(CWnd* pFocusWnd);
  49.     virtual BOOL OnQueryNewPalette();
  50.     virtual BOOL OnInitMenuPopup(CMenu* pMenu, int nIndex, BOOL bSysMenu);
  51.     virtual void OnInitMenu(CMenu* pMenu);
  52.     virtual BOOL OnMenuSelect(UINT nItemID, UINT nFlags, HMENU hSysMenu);
  53.  
  54.     // implementation helpers
  55.     BOOL NotifyAllInPlace(
  56.         BOOL bParam, BOOL (COleFrameHook::*pNotifyFunc)(BOOL bParam));
  57.     BOOL DoContextSensitiveHelp(BOOL bEnter);
  58.     BOOL DoEnableModeless(BOOL bEnable);
  59.  
  60. // Interface Maps
  61. public:
  62. #ifdef _AFXDLL
  63.     virtual LPUNKNOWN GetInterfaceHook(const void*);
  64. #endif
  65.  
  66.     BEGIN_INTERFACE_PART(OleInPlaceFrame, IOleInPlaceFrame)
  67.         INIT_INTERFACE_PART(COleFrameHook, OleInPlaceFrame)
  68.         STDMETHOD(GetWindow)(HWND*);
  69.         STDMETHOD(ContextSensitiveHelp)(BOOL);
  70.         STDMETHOD(GetBorder)(LPRECT);
  71.         STDMETHOD(RequestBorderSpace)(LPCBORDERWIDTHS);
  72.         STDMETHOD(SetBorderSpace)(LPCBORDERWIDTHS);
  73.         STDMETHOD(SetActiveObject)(LPOLEINPLACEACTIVEOBJECT, LPCOLESTR);
  74.         STDMETHOD(InsertMenus)(HMENU, LPOLEMENUGROUPWIDTHS);
  75.         STDMETHOD(SetMenu)(HMENU, HOLEMENU, HWND);
  76.         STDMETHOD(RemoveMenus)(HMENU);
  77.         STDMETHOD(SetStatusText)(LPCOLESTR);
  78.         STDMETHOD(EnableModeless)(BOOL);
  79.         STDMETHOD(TranslateAccelerator)(LPMSG, WORD);
  80.     END_INTERFACE_PART(OleInPlaceFrame)
  81.  
  82.     //WINBUG: this interface is here because some applications
  83.     // are broken and require IOleCommandTarget to be
  84.     // implemented before they'll properly activate
  85.     BEGIN_INTERFACE_PART(OleCommandTarget, IOleCommandTarget)
  86.         INIT_INTERFACE_PART(COleFrameHook, OleCommandTarget)
  87.         STDMETHOD(Exec)(const GUID*, DWORD, DWORD,
  88.            VARIANTARG*, VARIANTARG*);
  89.         STDMETHOD(QueryStatus)(const GUID*, ULONG, OLECMD[], OLECMDTEXT*);
  90.     END_INTERFACE_PART(OleCommandTarget)
  91.  
  92.     DECLARE_INTERFACE_MAP()
  93.  
  94.     friend COleClientItem;
  95. };
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. // Helper for implementing OLE enumerators
  99.  
  100. // Note: the following interface is not an actual OLE interface, but is useful
  101. //  for describing an abstract (not typesafe) enumerator.
  102.  
  103. #undef  INTERFACE
  104. #define INTERFACE   IEnumVOID
  105.  
  106. DECLARE_INTERFACE_(IEnumVOID, IUnknown)
  107. {
  108.     STDMETHOD(QueryInterface)(REFIID, LPVOID*) PURE;
  109.     STDMETHOD_(ULONG,AddRef)()  PURE;
  110.     STDMETHOD_(ULONG,Release)() PURE;
  111.     STDMETHOD(Next)(ULONG, void*, ULONG*) PURE;
  112.     STDMETHOD(Skip)(ULONG) PURE;
  113.     STDMETHOD(Reset)() PURE;
  114.     STDMETHOD(Clone)(IEnumVOID**) PURE;
  115. };
  116.  
  117. class CEnumArray : public CCmdTarget
  118. {
  119. // Constructors
  120. public:
  121.     CEnumArray(size_t nSize,
  122.         const void* pvEnum, UINT nCount, BOOL bNeedFree = FALSE);
  123.  
  124. // Implementation
  125. public:
  126.     virtual ~CEnumArray();
  127.  
  128. protected:
  129.     size_t m_nSizeElem;     // size of each item in the array
  130.     CCmdTarget* m_pClonedFrom;  // used to keep original alive for clones
  131.  
  132.     BYTE* m_pvEnum;     // pointer data to enumerate
  133.     UINT m_nCurPos;     // current position in m_pvEnum
  134.     UINT m_nSize;       // total number of items in m_pvEnum
  135.     BOOL m_bNeedFree;   // free on release?
  136.  
  137.     virtual BOOL OnNext(void* pv);
  138.     virtual BOOL OnSkip();
  139.     virtual void OnReset();
  140.     virtual CEnumArray* OnClone();
  141.  
  142. // Interface Maps
  143. public:
  144.     BEGIN_INTERFACE_PART(EnumVOID, IEnumVOID)
  145.         INIT_INTERFACE_PART(CEnumArray, EnumVOID)
  146.         STDMETHOD(Next)(ULONG, void*, ULONG*);
  147.         STDMETHOD(Skip)(ULONG);
  148.         STDMETHOD(Reset)();
  149.         STDMETHOD(Clone)(IEnumVOID**);
  150.     END_INTERFACE_PART(EnumVOID)
  151. };
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // COleDispatchImpl - IDispatch implementation
  155.  
  156. // Note: This class is only designed to be used as a CCmdTarget member
  157. //  (at the offset specified by CCmdTarget::m_xDispatch))
  158. // It WILL NOT work in other classes or at different offsets!
  159.  
  160. class COleDispatchImpl : public IDispatch
  161. {
  162. public:
  163. #ifndef _AFX_NO_NESTED_DERIVATION
  164.     // required for METHOD_PROLOGUE_EX
  165.     size_t m_nOffset;
  166.     COleDispatchImpl::COleDispatchImpl()
  167.         { m_nOffset = offsetof(CCmdTarget, m_xDispatch); }
  168. #endif
  169.  
  170.     STDMETHOD_(ULONG, AddRef)();
  171.     STDMETHOD_(ULONG, Release)();
  172.     STDMETHOD(QueryInterface)(REFIID, LPVOID*);
  173.  
  174.     STDMETHOD(GetTypeInfoCount)(UINT*);
  175.     STDMETHOD(GetTypeInfo)(UINT, LCID, LPTYPEINFO*);
  176.     STDMETHOD(GetIDsOfNames)(REFIID, LPOLESTR*, UINT, LCID, DISPID*);
  177.     STDMETHOD(Invoke)(DISPID, REFIID, LCID, WORD, DISPPARAMS*, LPVARIANT,
  178.         LPEXCEPINFO, UINT*);
  179.  
  180.     // special method for disconnect
  181.     virtual void Disconnect();
  182. };
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185. // OLE data (like AUX_DATA)
  186.  
  187. struct OLE_DATA
  188. {
  189.     // OLE 1.0 clipboard formats
  190.     UINT    cfNative, cfOwnerLink, cfObjectLink;
  191.  
  192.     // OLE 2.0 clipboard formats
  193.     UINT    cfEmbeddedObject, cfEmbedSource, cfLinkSource;
  194.     UINT    cfObjectDescriptor, cfLinkSourceDescriptor;
  195.     UINT    cfFileName, cfFileNameW;
  196.  
  197.     //RichEdit formats
  198.     UINT    cfRichTextFormat;
  199.     UINT    cfRichTextAndObjects;
  200.  
  201.     OLE_DATA();
  202. };
  203.  
  204. extern OLE_DATA _oleData;
  205.  
  206. /////////////////////////////////////////////////////////////////////////////
  207. // _AFX_OLE_STATE
  208.  
  209. #undef AFX_DATA
  210. #define AFX_DATA
  211.  
  212. class _AFX_OLE_STATE : public CNoTrackObject
  213. {
  214. public:
  215.     _AFX_OLE_STATE();
  216.     virtual ~_AFX_OLE_STATE();
  217.  
  218.     CView* m_pActivateView;         // activation view
  219.     COleDataSource* m_pClipboardSource;
  220.  
  221.     DWORD m_dwReserved;             // was "parking space" window
  222. #ifdef _AFXDLL
  223.     HINSTANCE m_hInstOLE;       // handle of OLE32.DLL
  224.     HINSTANCE m_hInstOLEAUT;    // handle of OLEAUT32.DLL
  225.     HINSTANCE m_hInstOLEDLG;    // handle of OLEDLG.DLL
  226.     HINSTANCE m_hInstUrlMon;
  227. #endif
  228.     long m_nReserved;           // was reference count on parking window
  229. };
  230.  
  231. EXTERN_PROCESS_LOCAL(_AFX_OLE_STATE, _afxOleState)
  232.  
  233. /////////////////////////////////////////////////////////////////////////////
  234. // Global helper functions
  235.  
  236. // menu merging/unmerging
  237. HMENU AFXAPI AfxMergeMenus(HMENU hMenuShared, HMENU hMenuSource,
  238.     LONG* lpMenuWidths, int iWidthIndex, BOOL bMergeHelpMenus = FALSE);
  239. void AFXAPI AfxUnmergeMenus(HMENU hMenuShared, HMENU hMenuSource,
  240.     HMENU hHelpMenuPopup = NULL);
  241.  
  242. // helpers for IOleCommandTarget
  243.  
  244. HRESULT AFXAPI _AfxQueryStatusOleCommandHelper(CCmdTarget* pTarget,
  245.     const GUID* pguidCmdGroup, ULONG cCmds, OLECMD rgCmds[],
  246.    OLECMDTEXT* pcmdtext);
  247. HRESULT AFXAPI _AfxExecOleCommandHelper(CCmdTarget* pTarget,
  248.    const GUID* pguidCmdGroup, DWORD nCmdID, DWORD nCmdExecOpt,
  249.    VARIANTARG* pvarargIn, VARIANTARG* pvarargOut);
  250.  
  251. // helpers for exceptions
  252. void AFXAPI _AfxFillOleFileException(CFileException*, SCODE sc);
  253. void AFXAPI _AfxThrowOleFileException(SCODE sc);
  254.  
  255. // helper used during object creation
  256. LPFORMATETC AFXAPI _AfxFillFormatEtc(LPFORMATETC lpFormatEtc,
  257.     CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtcFill);
  258.  
  259. // helper to copy clipboard data
  260. BOOL AFXAPI _AfxCopyStgMedium(
  261.     CLIPFORMAT cfFormat, LPSTGMEDIUM lpDest, LPSTGMEDIUM lpSource);
  262.  
  263. // helper for reliable and small Release calls
  264. DWORD AFXAPI _AfxRelease(LPUNKNOWN* plpUnknown);
  265. #ifndef _DEBUG
  266. // generate smaller code in release build
  267. #define RELEASE(lpUnk) _AfxRelease((LPUNKNOWN*)&lpUnk)
  268. #else
  269. // generate larger but typesafe code in debug build
  270. #define RELEASE(lpUnk) do \
  271.     { if ((lpUnk) != NULL) { (lpUnk)->Release(); (lpUnk) = NULL; } } while (0)
  272. #endif
  273.  
  274. // helpers from OLESTD.C (from original OLE2UI sample)
  275. HGLOBAL AFXAPI _AfxOleGetObjectDescriptorData(CLSID clsid, DWORD dwDrawAspect,
  276.     SIZEL sizel, POINTL pointl, DWORD dwStatus, LPCOLESTR lpszFullUserTypeName,
  277.     LPCOLESTR lpszSrcOfCopy);
  278. HGLOBAL AFXAPI _AfxOleGetObjectDescriptorData(LPOLEOBJECT lpOleObj,
  279.     LPCOLESTR lpszSrcOfCopy, DWORD dwDrawAspect, POINTL pointl, LPSIZEL lpSizelHim);
  280. SCODE AFXAPI _AfxOleDoConvert(LPSTORAGE lpStg, REFCLSID rClsidNew);
  281. SCODE AFXAPI _AfxOleDoTreatAsClass(
  282.     LPCTSTR lpszUserType, REFCLSID rclsid, REFCLSID rclsidNew);
  283. DVTARGETDEVICE* AFXAPI _AfxOleCreateTargetDevice(LPPRINTDLG lpPrintDlg);
  284. DVTARGETDEVICE* AFXAPI _AfxOleCreateTargetDevice(LPDEVNAMES pDN, LPDEVMODE pDM);
  285. UINT AFXAPI _AfxOleGetUserTypeOfClass(
  286.     REFCLSID rclsid, LPTSTR lpszUserType, UINT cch, HKEY hKey);
  287. DWORD AFXAPI _AfxOleGetLenFilePrefixOfMoniker(LPMONIKER lpmk);
  288. DVTARGETDEVICE* AFXAPI _AfxOleCopyTargetDevice(DVTARGETDEVICE* ptdSrc);
  289. void AFXAPI _AfxOleCopyFormatEtc(LPFORMATETC petcDest, LPFORMATETC petcSrc);
  290. HDC AFXAPI _AfxOleCreateDC(DVTARGETDEVICE* ptd);
  291. void AFXAPI _AfxDeleteMetafilePict(HGLOBAL hMetaPict);
  292. BOOL AFXAPI _AfxOlePropertiesEnabled();
  293.  
  294. // helper(s) for reliable and small QueryInterface calls
  295. LPUNKNOWN AFXAPI _AfxQueryInterface(LPUNKNOWN lpUnknown, REFIID riid);
  296. #define QUERYINTERFACE(lpUnknown, iface) \
  297.     (iface*)_AfxQueryInterface(lpUnknown, IID_##iface)
  298.  
  299. // helpers for conversion between himetric and pixels
  300. #define HIMETRIC_PER_INCH   2540
  301. #define MAP_PIX_TO_LOGHIM(x,ppli)   MulDiv(HIMETRIC_PER_INCH, (x), (ppli))
  302. #define MAP_LOGHIM_TO_PIX(x,ppli)   MulDiv((ppli), (x), HIMETRIC_PER_INCH)
  303.  
  304. // helper for GUID comparison
  305. inline BOOL _AfxIsEqualGUID(REFGUID guid1, REFGUID guid2)
  306. {
  307.     return ((DWORD*)&guid1)[0] == ((DWORD*)&guid2)[0] &&
  308.         ((DWORD*)&guid1)[1] == ((DWORD*)&guid2)[1] &&
  309.         ((DWORD*)&guid1)[2] == ((DWORD*)&guid2)[2] &&
  310.         ((DWORD*)&guid1)[3] == ((DWORD*)&guid2)[3];
  311. }
  312.  
  313. HRESULT AFXAPI _AfxReadFromStream(LPSTREAM pStream, void* lpBuf, UINT nCount, DWORD& nRead);
  314.  
  315. /////////////////////////////////////////////////////////////////////////////
  316. // implementation types and constants
  317.  
  318. #define OLE_MAXITEMNAME (_countof("Embedding ")+_countof("4294967295")-_countof(""))
  319.  
  320. typedef LPVOID* LPLP;
  321.  
  322. #undef AFX_DATA
  323. #define AFX_DATA
  324.  
  325. /////////////////////////////////////////////////////////////////////////////
  326.