home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap21 / patron / patron.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  11.6 KB  |  405 lines

  1. /*
  2.  * PATRON.H
  3.  * Patron Chapter 21
  4.  *
  5.  * Single include file that pulls in everything needed for other
  6.  * source files in the application.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #ifndef _PATRON_H_
  17. #define _PATRON_H_
  18.  
  19. #define INC_CLASSLIB
  20. #define INC_OLEUI
  21. //CHAPTER21MOD
  22. #define CHAPTER21
  23. //End CHAPTER21MOD
  24. #include <inole.h>
  25. #include "resource.h"
  26.  
  27. //Get editor window information
  28. #include "pages.h"
  29.  
  30.  
  31. /*
  32.  * UINT value such that adding one produces zero.  Portable to Win32.
  33.  * This is used to represent a non-existent zero-based UINT value
  34.  */
  35. #define NOVALUE                     ((UINT)-1)
  36.  
  37.  
  38. //PATRON.CPP:  Frame object that creates a main window
  39.  
  40. class CPatronFrame : public CFrame
  41.     {
  42.     //CHAPTER21MOD
  43.     //For access to m_pCL for document creation
  44.     friend class CLinkClassFactory;
  45.     //End CHAPTER21MOD
  46.  
  47.     private:
  48.         BOOL            m_fInitialized;     //OleInitialize worked
  49.         LPCLASSFACTORY  m_pIClassDataTran;  //For locking.
  50.  
  51.         //CHAPTER21MOD
  52.         PCDocument      m_pDocCreated;      //What class factory makes
  53.         BOOL            m_fEmbedding;       //-Embedding on command line?
  54.         DWORD           m_dwRegCO;          //From CoRegisterClassObject
  55.         LPCLASSFACTORY  m_pIClassFactory;
  56.         //End CHAPTER21MOD
  57.  
  58.     protected:
  59.         //Overridable for creating a CPatronClient
  60.         virtual PCClient    CreateCClient(void);
  61.  
  62.         virtual BOOL        FMessageHook(HWND, UINT, WPARAM, LPARAM
  63.                                 , LRESULT *);
  64.  
  65.         //CHAPTER21MOD
  66.         virtual BOOL        PreShowInit(void);
  67.         //End CHAPTER21MOD
  68.  
  69.         virtual BOOL        RegisterAllClasses(void);
  70.         virtual UINT        CreateToolbar(void);
  71.         virtual LRESULT     OnCommand(HWND, WPARAM, LPARAM);
  72.  
  73.     public:
  74.         CPatronFrame(HINSTANCE, HINSTANCE, LPSTR, int);
  75.         virtual ~CPatronFrame(void);
  76.  
  77.         //Overrides
  78.         virtual BOOL        Init(PFRAMEINIT);
  79.  
  80.         virtual void        UpdateMenus(HMENU, UINT);
  81.         virtual void        UpdateToolbar(void);
  82.  
  83.     };
  84.  
  85.  
  86. typedef CPatronFrame *PCPatronFrame;
  87.  
  88.  
  89. //CHAPTER21MOD
  90.  
  91. //These are global for simplification of object implementation.
  92. extern ULONG g_cObj;
  93. extern ULONG g_cLock;
  94. extern HWND  g_hWnd;
  95.  
  96. //Function for the object to notify on destruction.
  97. void ObjectDestroyed(void);
  98.  
  99.  
  100. //ICLASSF.CPP
  101.  
  102. class CLinkClassFactory : public IClassFactory
  103.     {
  104.     protected:
  105.         ULONG           m_cRef;
  106.         PCPatronFrame   m_pFR;          //For document creation.
  107.  
  108.     public:
  109.         CLinkClassFactory(PCPatronFrame);
  110.         ~CLinkClassFactory(void);
  111.  
  112.         //IUnknown members
  113.         STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  114.         STDMETHODIMP_(ULONG) AddRef(void);
  115.         STDMETHODIMP_(ULONG) Release(void);
  116.  
  117.         //IClassFactory members
  118.         STDMETHODIMP         CreateInstance(LPUNKNOWN, REFIID
  119.                                  , PPVOID);
  120.         STDMETHODIMP         LockServer(BOOL);
  121.     };
  122.  
  123. typedef CLinkClassFactory *PCLinkClassFactory;
  124.  
  125. //End CHAPTER21MOD
  126.  
  127.  
  128.  
  129.  
  130. //CLIENT.CPP
  131.  
  132. /*
  133.  * The only reason we have a derived class here is to override
  134.  * CreateCDocument so we can create our own type as well as
  135.  * overriding NewDocument to perform one other piece of work once
  136.  * the document's been created.
  137.  */
  138.  
  139. class CPatronClient : public CClient
  140.     {
  141.     protected:
  142.         //Overridable for creating a new CDocument
  143.         virtual PCDocument CreateCDocument(void);
  144.  
  145.     public:
  146.         CPatronClient(HINSTANCE, PCFrame);
  147.         virtual ~CPatronClient(void);
  148.     };
  149.  
  150.  
  151. typedef CPatronClient *PCPatronClient;
  152.  
  153.  
  154.  
  155.  
  156. //DOCUMENT.CPP
  157.  
  158. //Constant ID for the pages window that lives in a document window
  159. #define ID_PAGES            723
  160.  
  161.  
  162. //CHAPTER21MOD
  163. class CImpIPersistFile;
  164. typedef class CImpIPersistFile *PCImpIPersistFile;
  165. class CImpIOleItemContainer;
  166. typedef class CImpIOleItemContainer *PCImpIOleItemContainer;
  167.  
  168.  
  169. /*
  170.  * To support linking to embeddings, the document needs to have
  171.  * IPersistFile and IOleItemContainer interfaces, so we multiply
  172.  * inherit from IUnknown and provide two interface implementations.
  173.  */
  174.  
  175. class CPatronDoc : public CDocument, public IUnknown
  176. //End CHAPTER21MOD
  177.     {
  178.     //These need access to FQueryPasteFromData, PasteFromData
  179.     friend class CDropTarget;
  180.     friend class CDropSource;
  181.  
  182.     //CHAPTER21MOD
  183.     friend class CImpIPersistFile;
  184.     friend class CImpIOleItemContainer;
  185.     //End CHAPTER21MOD
  186.  
  187.     protected:
  188.         LONG            m_lVer;         //Loaded data version
  189.         PCPages         m_pPG;          //Pages window in us
  190.         LPSTORAGE       m_pIStorage;    //Root storage for document
  191.         BOOL            m_fPrintSetup;
  192.  
  193.         class CDropTarget *m_pDropTarget;   //Registered target
  194.  
  195.         UINT            m_cfEmbeddedObject; //Clipboard formats
  196.         UINT            m_cfObjectDescriptor;
  197.         UINT            m_cfLinkSource;
  198.         UINT            m_cfLinkSrcDescriptor;
  199.         BOOL            m_fShowTypes;       //Show Objects active?
  200.  
  201.         //CHAPTER21MOD
  202.         ULONG               m_cRef;
  203.         BOOL                m_fRename;          //Rename on Save?
  204.         DWORD               m_dwRegROT;
  205.         DWORD               m_dwRegROTWild;
  206.  
  207.         PCImpIPersistFile       m_pImpIPersistFile;
  208.         PCImpIOleItemContainer  m_pImpIOleItemContainer;
  209.         //End CHAPTER21MOD
  210.  
  211.     protected:
  212.         virtual BOOL    FMessageHook(HWND, UINT, WPARAM, LPARAM
  213.             , LRESULT *);
  214.  
  215.         BOOL            FQueryPasteFromData(LPDATAOBJECT
  216.                             , LPFORMATETC, PTENANTTYPE);
  217.         BOOL            FQueryPasteLinkFromData(LPDATAOBJECT
  218.                             , LPFORMATETC, PTENANTTYPE);
  219.         BOOL            PasteFromData(LPDATAOBJECT, LPFORMATETC
  220.                             , TENANTTYPE, PPATRONOBJECT, DWORD
  221.                             , BOOL);
  222.  
  223.     public:
  224.         CPatronDoc(HINSTANCE, PCFrame, PCDocumentAdviseSink);
  225.         virtual ~CPatronDoc(void);
  226.  
  227.         //CHAPTER21MOD
  228.         //We now need our ubiquitous set of IUnknown members.
  229.         STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  230.         STDMETHODIMP_(ULONG) AddRef(void);
  231.         STDMETHODIMP_(ULONG) Release(void);
  232.         //End CHAPTER21MOD
  233.  
  234.         virtual BOOL    Init(PDOCUMENTINIT);
  235.         virtual void    Clear(void);
  236.  
  237.         //CHAPTER21MOD
  238.         virtual BOOL    FDirtySet(BOOL);
  239.         //End CHAPTER21MOD
  240.         virtual BOOL    FDirtyGet(void);
  241.         virtual void    Delete(void);
  242.         virtual BOOL    FQueryPrinterSetup(void);
  243.         virtual BOOL    FQueryObjectSelected(HMENU);
  244.  
  245.         virtual UINT    Load(BOOL, LPTSTR);
  246.         virtual UINT    Save(UINT, LPTSTR);
  247.  
  248.         virtual BOOL    Print(HWND);
  249.         virtual UINT    PrinterSetup(HWND, BOOL);
  250.  
  251.         virtual BOOL    Clip(HWND, BOOL);
  252.         virtual BOOL    FQueryPaste(void);
  253.         virtual BOOL    Paste(HWND);
  254.         virtual BOOL    PasteSpecial(HWND);
  255.         virtual BOOL    FQueryEnableEditLinks(void);
  256.         virtual BOOL    EditLinks(HWND);
  257.         virtual BOOL    ShowOrQueryObjectTypes(BOOL, BOOL);
  258.  
  259.         virtual UINT    NewPage(void);
  260.         virtual UINT    DeletePage(void);
  261.         virtual UINT    NextPage(void);
  262.         virtual UINT    PreviousPage(void);
  263.         virtual UINT    FirstPage(void);
  264.         virtual UINT    LastPage(void);
  265.  
  266.         virtual void    Rename(LPTSTR);
  267.         virtual BOOL    InsertObject(HWND);
  268.         virtual void    ActivateObject(LONG);
  269.         virtual BOOL    ConvertObject(HWND);
  270.     };
  271.  
  272. typedef CPatronDoc *PCPatronDoc;
  273.  
  274. //Hook for Print Dialog to hide Setup... button
  275. UINT CALLBACK PrintDlgHook(HWND, UINT, WPARAM, LPARAM);
  276.  
  277.  
  278. //CHAPTER21MOD
  279.  
  280. //IPersistFile implementation for CPatronDoc
  281.  
  282. class CImpIPersistFile : public IPersistFile
  283.     {
  284.     protected:
  285.         ULONG               m_cRef;      //Interface reference count
  286.         PCPatronDoc         m_pDoc;      //Back pointer to the object
  287.         LPUNKNOWN           m_pUnkOuter; //Controlling unknown
  288.  
  289.     public:
  290.         CImpIPersistFile(PCPatronDoc, LPUNKNOWN);
  291.         ~CImpIPersistFile(void);
  292.  
  293.         STDMETHODIMP QueryInterface(REFIID, PPVOID);
  294.         STDMETHODIMP_(ULONG) AddRef(void);
  295.         STDMETHODIMP_(ULONG) Release(void);
  296.  
  297.         STDMETHODIMP GetClassID(LPCLSID);
  298.  
  299.         STDMETHODIMP IsDirty(void);
  300.         STDMETHODIMP Load(LPCOLESTR, DWORD);
  301.         STDMETHODIMP Save(LPCOLESTR, BOOL);
  302.         STDMETHODIMP SaveCompleted(LPCOLESTR);
  303.         STDMETHODIMP GetCurFile(LPOLESTR *);
  304.     };
  305.  
  306.  
  307.  
  308. /*
  309.  * IOleItemContainer implementation for both CPatronDoc and CPage,
  310.  * therefore have two back pointers.
  311.  */
  312.  
  313. class CImpIOleItemContainer : public IOleItemContainer
  314.     {
  315.     protected:
  316.         ULONG               m_cRef;
  317.         class CPage        *m_pPage;
  318.         PCPatronDoc         m_pDoc;      //Convenient naming & types
  319.         LPUNKNOWN           m_pUnkOuter;
  320.         BOOL                m_fDoc;      //Document or page?
  321.  
  322.     private:
  323.         BOOL         TenantFromName(LPOLESTR, PCTenant *);
  324.  
  325.     public:
  326.         CImpIOleItemContainer(LPVOID, LPUNKNOWN, BOOL);
  327.         ~CImpIOleItemContainer(void);
  328.  
  329.         STDMETHODIMP QueryInterface(REFIID, PPVOID);
  330.         STDMETHODIMP_(ULONG) AddRef(void);
  331.         STDMETHODIMP_(ULONG) Release(void);
  332.  
  333.         STDMETHODIMP ParseDisplayName(LPBC, LPOLESTR, ULONG *
  334.             , LPMONIKER *);
  335.         STDMETHODIMP EnumObjects(DWORD, LPENUMUNKNOWN *);
  336.         STDMETHODIMP LockContainer(BOOL);
  337.         STDMETHODIMP GetObject(LPOLESTR, DWORD, LPBINDCTX, REFIID
  338.             , PPVOID);
  339.         STDMETHODIMP GetObjectStorage(LPOLESTR, LPBINDCTX, REFIID
  340.             , PPVOID);
  341.         STDMETHODIMP IsRunning(LPOLESTR);
  342.     };
  343.  
  344. //End CHAPTER21MOD
  345.  
  346.  
  347.  
  348. //Drag-drop objects we need in the document
  349.  
  350. class CDropTarget : public IDropTarget
  351.     {
  352.     protected:
  353.         ULONG               m_cRef;
  354.         PCPatronDoc         m_pDoc;
  355.  
  356.         LPDATAOBJECT        m_pIDataObject;  //From DragEnter
  357.         BOOL                m_fPendingRepaint;
  358.         POINTL              m_ptPick;        //Pick-up offsets
  359.         POINTL              m_ptLast;        //Last drag point
  360.         SIZEL               m_szl;           //Object size
  361.         BOOL                m_fFeedback;     //Draw feedback?
  362.         FORMATETC           m_fe;            //Real dropping format
  363.  
  364.     public:
  365.         CDropTarget(PCPatronDoc);
  366.         ~CDropTarget(void);
  367.  
  368.         //IDropTarget interface members
  369.         STDMETHODIMP QueryInterface(REFIID, PPVOID);
  370.         STDMETHODIMP_(ULONG) AddRef(void);
  371.         STDMETHODIMP_(ULONG) Release(void);
  372.  
  373.         STDMETHODIMP DragEnter(LPDATAOBJECT, DWORD, POINTL,LPDWORD);
  374.         STDMETHODIMP DragOver(DWORD, POINTL, LPDWORD);
  375.         STDMETHODIMP DragLeave(void);
  376.         STDMETHODIMP Drop(LPDATAOBJECT, DWORD, POINTL, LPDWORD);
  377.     };
  378.  
  379.  
  380. typedef CDropTarget *PCDropTarget;
  381.  
  382.  
  383. class CDropSource : public IDropSource
  384.     {
  385.     protected:
  386.         ULONG               m_cRef;
  387.  
  388.     public:
  389.         CDropSource(void);
  390.         ~CDropSource(void);
  391.  
  392.         //IDropSource interface members
  393.         STDMETHODIMP QueryInterface(REFIID, PPVOID);
  394.         STDMETHODIMP_(ULONG) AddRef(void);
  395.         STDMETHODIMP_(ULONG) Release(void);
  396.  
  397.         STDMETHODIMP QueryContinueDrag(BOOL, DWORD);
  398.         STDMETHODIMP GiveFeedback(DWORD);
  399.     };
  400.  
  401. typedef CDropSource *PCDropSource;
  402.  
  403.  
  404. #endif //_PATRON_H_
  405.