home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / OLESRVR.ZIP / OLESRVR.H < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  12.4 KB  |  433 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. /* application "OWL OLE Server" which demonstrates a multiple instances (i.e.
  4.    non-MDI) OLE server supporting linking and embedding */
  5.  
  6. //
  7. // "File" menu items
  8. //
  9. #define IDM_NEW         100
  10. #define IDM_OPEN        101
  11. #define IDM_SAVE        102
  12. #define IDM_SAVEAS      103
  13. #define IDM_UPDATE      104
  14.  
  15. //
  16. // "Edit" menu items
  17. //
  18. #define IDM_COPY        200
  19.  
  20. //
  21. // "Shape" menu items
  22. //
  23. #define IDM_ELLIPSE     300
  24. #define IDM_RECTANGLE   301
  25. #define IDM_TRIANGLE    302
  26.  
  27. //
  28. // "Help" menu items
  29. //
  30. #define IDM_ABOUT       400
  31.  
  32. #define szAppName       "OWL OLE Server"
  33. #define szClassKey      "OWLOLEServer"
  34. #define szClassValue    "OWL OLE Object"
  35. #define szExeName       "olesrvr"
  36. #define szFileExt       "oos"
  37.  
  38. const int  MAXPATHLENGTH = 256;
  39. const int  MAXLINKS = 10;
  40.  
  41. const int  OBJX = 75;
  42. const int  OBJY = 50;
  43. const int  OBJWIDTH = 100;
  44. const int  OBJHEIGHT = 100;
  45.  
  46. _CLASSDEF (TOLEServer)
  47. _CLASSDEF (TOLEApp)
  48.  
  49.  
  50. /*
  51.     enum DOCTYPE
  52.     ---- -------
  53.     document type
  54. */
  55. enum DOCTYPE {doctypeNew,        // document is untitled
  56.               doctypeFromFile,   // document exists in a file and may be linked
  57.               doctypeEmbedded};  // document is an embedded document
  58.  
  59.  
  60. /*
  61.     typedef VERSION
  62.     ------- -------
  63.  
  64.     server applications should store version numbers in their Native data
  65.     formats
  66.  
  67.     this way a client application may embed data from one version of a server
  68.     and later request a newer version to edit that data
  69. */
  70. typedef int VERSION;
  71.  
  72.  
  73. /*
  74.     enum NATIVETYPE
  75.     ---- ----------
  76.  
  77.     native data type
  78. */
  79. enum NATIVETYPE {objEllipse = IDM_ELLIPSE,
  80.                  objRect = IDM_RECTANGLE,
  81.                  objTriangle = IDM_TRIANGLE};
  82.  
  83.  
  84. /*
  85.     struct NATIVE
  86.     ------ ------
  87.  
  88.     native data format
  89. */
  90. struct NATIVE {
  91.   NATIVETYPE  type;
  92.   VERSION     version;
  93. };
  94.  
  95. typedef struct NATIVE  FAR *LPNATIVE;
  96.  
  97.  
  98. /*
  99.     enum VERB
  100.     ---- ----
  101.  
  102.     the types of actions that the server can perform on an object
  103. */
  104. enum VERB {verbEdit, verbPlay};
  105.  
  106.  
  107. /*
  108.     enum FILEIOSTATUS
  109.     ---- ------------
  110.  
  111.     used when prompting the user to save changes before performing a file
  112.     I/O command
  113.  
  114.     if the user chooses "Cancel" then "fiCancel" is returned; otherwise
  115.     "fiExecute" is returned
  116. */
  117. enum FILEIOSTATUS {fiCancel, fiExecute};
  118.  
  119.  
  120. /*
  121.     class TOLEObject
  122.     ----- ----------
  123.  
  124.     although we have embedded the native data in the ole object, you might
  125.     not want to do this.
  126.  
  127.     rather than integrate OLE with your app you should treat OLE as a protocol
  128.     that sits on top of your app and allows other applications access to your
  129.     server's data.
  130.  
  131.     instead of embedding the data in the OLE object have the OLE object contain
  132.     a pointer to the native data.
  133. */
  134. _CLASSDEF (TOLEObject)
  135.  
  136. class TOLEObject : public OLEOBJECT {
  137.   public:
  138.     NATIVE       native;
  139.     BOOL         fRelease;  // TRUE if Release method has been called
  140.     LPOLECLIENT  lpClients[MAXLINKS + 1];  // null terminated list of client(s)
  141.                                            // we are linked to
  142.  
  143.   TOLEObject ();
  144.  
  145.     NATIVETYPE    GetType () {return native.type;}
  146.  
  147.     //
  148.     // sets instance variable "type" and calls ObjectChanged()
  149.     //
  150.     void          SetType (NATIVETYPE);
  151.  
  152.     //
  153.     // marks the document as "dirty" and sends each of the clients the receiver
  154.     // is linked to an OLE_CHANGED message
  155.     //
  156.     void          ObjectChanged ();
  157.  
  158.     //
  159.     // add "lpOleClient" to the list of clients that should be notified when
  160.     // the receivber changes
  161.     //
  162.     void          AddClientLink (LPOLECLIENT lpOleClient);
  163.  
  164.     void          Draw (HDC);
  165.  
  166.     //
  167.     // routines to build the various clipboard formats that are required for
  168.     // an OLE server
  169.     //
  170.     // your routine might provide routines for additional formats such as TEXT,
  171.     // RTF, and DIB
  172.     //
  173.     HANDLE        GetNativeData ();
  174.     HANDLE        GetLinkData ();
  175.     HBITMAP       GetBitmapData ();
  176.     HANDLE        GetMetafilePicture ();
  177.  
  178.     //
  179.     // vtable initialization
  180.     //
  181.     static BOOL   InitVTBL (HINSTANCE);
  182.  
  183.     //
  184.     // vtable entries
  185.     //
  186.     static LPVOID          FAR PASCAL _export QueryProtocol (LPOLEOBJECT, LPSTR);
  187.     static OLESTATUS     FAR PASCAL _export Release (LPOLEOBJECT);
  188.     static OLESTATUS     FAR PASCAL _export Show (LPOLEOBJECT, BOOL);
  189.     static OLESTATUS     FAR PASCAL _export DoVerb (LPOLEOBJECT, UINT, BOOL, BOOL);
  190.     static OLESTATUS     FAR PASCAL _export GetData (LPOLEOBJECT, WORD, LPHANDLE);
  191.     static OLESTATUS     FAR PASCAL _export SetData (LPOLEOBJECT, OLECLIPFORMAT, HANDLE);
  192.     static OLESTATUS     FAR PASCAL _export SetTargetDevice (LPOLEOBJECT, HANDLE);
  193.     static OLESTATUS     FAR PASCAL _export SetBounds (LPOLEOBJECT, LPRECT);
  194.     static OLECLIPFORMAT FAR PASCAL _export EnumFormats (LPOLEOBJECT, OLECLIPFORMAT);
  195.     static OLESTATUS     FAR PASCAL _export SetColorScheme (LPOLEOBJECT, LPLOGPALETTE);
  196.  
  197.   private:
  198.     static OLEOBJECTVTBL  _vtbl;
  199. };
  200.  
  201.  
  202. /*
  203.     class TOLEDocument
  204.     ----- ------------
  205.  
  206.     NOTE: we only have one object per document. if your app allowed multiple
  207.           objects per document then you would have a list of objects...
  208. */
  209. _CLASSDEF (TOLEDocument)
  210.  
  211. class TOLEDocument  : public OLESERVERDOC {
  212.   public:
  213.     LHSERVERDOC    lhDoc;     // registration handle returned by server library
  214.     DOCTYPE        type;
  215.     char          *szName;
  216.     TOLEObject    *pObject;
  217.     BOOL           fDirty;
  218.     BOOL           fRelease;  // TRUE if Release method has been called
  219.  
  220.     //
  221.     // if "szPath" is NULL then we create an untitled document and default object
  222.     //
  223.     // the type is "doctypeNew" if "lhDoc" is NULL and "doctypeEmbedded" if
  224.     // "lhDoc" is non NULL
  225.     //
  226.     // if "szPath" is non NULL we create a document of type "doctypeFromFile"
  227.     // and initialize it from file "szPath"
  228.     //
  229.     // if "lhDoc" is NULL then we call OleRegisterServerDoc() otherwise we
  230.     // just use "lhDoc" as our registration handle
  231.     //
  232.     // you specify if the document is initially marked as "dirty"
  233.     //
  234.       TOLEDocument (TOLEServer &server,
  235.                   LHSERVERDOC lhDoc = 0,
  236.                   LPSTR       szPath = 0,
  237.                   BOOL        dirty = FALSE);
  238.  
  239.     void          SaveDoc ();  // save doc to "name"
  240.     void          SaveAs ();   // prompt user for file to save to...
  241.  
  242.     //
  243.     // if "szPath" is NULL then resets document to "doctypeNew"; otherwise
  244.     // streams in the contents of "file" and sets the type to "doctypeFile"
  245.     //
  246.     // marks the document as not modified
  247.     //
  248.     void          Reset (LPSTR szPath);
  249.  
  250.     //
  251.     // changes "szName" and also changes the window's title bar (caption) if
  252.     // "changeCaption" is TRUE...
  253.     //
  254.     void          SetDocumentName (LPSTR, BOOL changeCaption = TRUE);
  255.  
  256.     //
  257.     // prompts user for filename to open and returns the string in "szPath"
  258.     //
  259.     static BOOL   PromptForOpenFileName (char *szPath);
  260.  
  261.     //
  262.     // vtable initialization
  263.     //
  264.     static BOOL   InitVTBL (HINSTANCE);
  265.  
  266.     //
  267.     // vtable entries
  268.     //
  269.     static OLESTATUS FAR PASCAL _export Save (LPOLESERVERDOC);
  270.     static OLESTATUS FAR PASCAL _export Close (LPOLESERVERDOC);
  271.     static OLESTATUS FAR PASCAL _export SetHostNames (LPOLESERVERDOC, LPSTR, LPSTR);
  272.     static OLESTATUS FAR PASCAL _export SetDocDimensions (LPOLESERVERDOC, LPRECT);
  273.     static OLESTATUS FAR PASCAL _export GetObject (LPOLESERVERDOC, LPSTR, LPOLEOBJECT FAR *, LPOLECLIENT);
  274.     static OLESTATUS FAR PASCAL _export Release (LPOLESERVERDOC);
  275.     static OLESTATUS FAR PASCAL _export SetColorScheme (LPOLESERVERDOC, LPLOGPALETTE);
  276.     static OLESTATUS FAR PASCAL _export Execute (LPOLESERVERDOC, HANDLE);
  277.  
  278.   private:
  279.     static OLESERVERDOCVTBL  _vtbl;
  280.  
  281.     BOOL          LoadFromFile (LPSTR);
  282. };
  283.  
  284.  
  285. /*
  286.     class TOLEServer
  287.     ----- ----------
  288.  
  289.     NOTE: we only have one document per server. if your app was MDI then you
  290.           would have a list of documents...
  291. */
  292. class TOLEServer : public OLESERVER {
  293.   public:
  294.     LHSERVER       lhServer;  // registration handle returned by server library
  295.     TOLEDocument  *pDocument;
  296.     BOOL           fRelease;  // TRUE if Release method has been called
  297.  
  298.     //
  299.     // registers with the server library
  300.     //
  301.     // if "embedded" is FALSE, creates an untitled document; otherwise it
  302.     // waits for the server library to request document creation
  303.     //
  304.       TOLEServer (TOLEApp &, BOOL embedded);
  305.  
  306.     //
  307.     // registers with the server library
  308.     //
  309.     // creates a document of type "objtypeFromFile" which is initialized from
  310.     // file "szPath"
  311.     //
  312.     TOLEServer (TOLEApp &, LPSTR szPath);
  313.  
  314.     //
  315.     // vtable initialization
  316.     //
  317.     static BOOL   InitVTBL (HINSTANCE);
  318.  
  319.     //
  320.     // vtable entries
  321.     //
  322.     static OLESTATUS FAR PASCAL _export Open (LPOLESERVER, LHSERVERDOC, LPSTR, LPOLESERVERDOC FAR *);
  323.     static OLESTATUS FAR PASCAL _export Create (LPOLESERVER, LHSERVERDOC, LPSTR,LPSTR, LPOLESERVERDOC FAR *);
  324.     static OLESTATUS FAR PASCAL _export CreateFromTemplate (LPOLESERVER, LHSERVERDOC, LPSTR, LPSTR, LPSTR, LPOLESERVERDOC FAR *);
  325.     static OLESTATUS FAR PASCAL _export Edit (LPOLESERVER, LHSERVERDOC, LPSTR, LPSTR, LPOLESERVERDOC FAR *);
  326.     static OLESTATUS FAR PASCAL _export Exit (LPOLESERVER);
  327.     static OLESTATUS FAR PASCAL _export Release (LPOLESERVER);
  328.     static OLESTATUS FAR PASCAL _export Execute (LPOLESERVER, HANDLE);
  329.  
  330.   private:
  331.     static OLESERVERVTBL  _vtbl;
  332.  
  333.     BOOL          Init (TOLEApp &);
  334.     BOOL          RegisterWithDatabase ();
  335.     BOOL          WantsToRegister ();
  336. };
  337.  
  338.  
  339. /*
  340.     class TWindowServer
  341.     ----- -------------
  342. */
  343. _CLASSDEF (TWindowServer)
  344.  
  345. class TWindowServer: public TWindow {
  346.   public:
  347.     TWindowServer (PTWindowsObject, LPSTR);
  348.  
  349.     //
  350.     // handler for "Help" menu items
  351.     //
  352.     void           About (RTMessage) = [CM_FIRST + IDM_ABOUT];
  353.  
  354.     //
  355.     // handlers for "File" menu items
  356.     //
  357.     void           New (RTMessage) = [CM_FIRST + IDM_NEW];
  358.     void           Open (RTMessage) = [CM_FIRST + IDM_OPEN];
  359.     void           Save (RTMessage) = [CM_FIRST + IDM_SAVE];
  360.     void           Update (RTMessage) = [CM_FIRST + IDM_UPDATE];
  361.     void           SaveAs (RTMessage) = [CM_FIRST + IDM_SAVEAS];
  362.  
  363.     //
  364.     // handler for "Edit" menu items
  365.     //
  366.     void           Copy (RTMessage) = [CM_FIRST + IDM_COPY];
  367.  
  368.     //
  369.     // routines to handle switching between embedding mode and stand-alone
  370.     // mode
  371.     //
  372.     void           BeginEmbedding ();
  373.     void           EndEmbedding ();
  374.  
  375.     //
  376.     // alter File/Save and File/Exit to reflect the name of the client's
  377.     // application document (only called for embedded objects)
  378.     //
  379.     void           UpdateFileMenu (LPSTR szDocName);
  380.  
  381.     void           WMCommand (RTMessage);
  382.     BOOL           CanClose();
  383.     //
  384.     // gets called by WMCommand() when the user selects a menu item from the
  385.     // "Shape" menu
  386.     //
  387.     void           ShapeChange (NATIVETYPE);
  388.  
  389.     void           Paint (HDC, PAINTSTRUCT _FAR &);
  390.  
  391.   protected:
  392.     //
  393.     // routine to prompt the user to save changes in a document before creating
  394.     // a new one, opening a new file, or exiting the application
  395.     //
  396.     // - choosing "Yes" causes the document to be saved and returns "fiExecute"
  397.     // - choosing "No" discards the document and also returns "fiExecute"
  398.     // - choosing "Cancel" discards the document and returns "fiCancel"
  399.     //
  400.     FILEIOSTATUS   SaveChangesPrompt ();
  401. };
  402.  
  403.  
  404. /*
  405.     class TOLEApp
  406.     ----- -------
  407. */
  408. class TOLEApp : public TApplication {
  409.   public:
  410.     TOLEServer    *pServer;
  411.     OLECLIPFORMAT  cfNative, cfOwnerLink, cfObjectLink;
  412.  
  413.     TOLEApp (LPSTR      name,
  414.              HINSTANCE  hInstance,
  415.              HINSTANCE  hPrevInstance,
  416.              LPSTR      lpCmdLine,
  417.              int        nCmdShow) : TApplication (name,
  418.                                                   hInstance,
  419.                                                   hPrevInstance,
  420.                                                   lpCmdLine,
  421.                                                   nCmdShow) {}
  422.  
  423.     void     InitInstance ();
  424.  
  425.     void     Wait (BOOL &waitFlag);
  426.  
  427.   protected:
  428.     BOOL     RegisterClipboardFormats ();
  429.     void     CreateServer ();
  430.     void     Error (int ErrorCode);
  431. };
  432.  
  433.