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 / chap18 / cosmo1.0 / oledoc.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  11KB  |  417 lines

  1. /*
  2.  * OLEDOC.C
  3.  *
  4.  * Contains all callback functions in the OLESERVERDOCVTBL struture:
  5.  *      DocClose
  6.  *      DocGetObject
  7.  *      DocExecute
  8.  *      DocRelease
  9.  *      DocSave
  10.  *      DocSetColorScheme
  11.  *      DocSetDocDimensions
  12.  *      DocSetHostNames
  13.  *
  14.  * Also contains two helper functions, PDocumentAllocate and DocumentClean.
  15.  * PDocumentAllocate acts like a C++ constructor.
  16.  *
  17.  * Copyright(c) Microsoft Corp. 1992-1994 All Rights Reserved
  18.  * Win32 version, January 1994
  19.  */
  20.  
  21. #ifdef MAKEOLESERVER
  22.  
  23. #include <windows.h>
  24. #include <ole.h>
  25. #include "cosmo.h"
  26. #include "oleglobl.h"
  27.  
  28.  
  29. /*
  30.  * PDocumentAllocate
  31.  *
  32.  * Purpose:
  33.  *  Allocates a COSMODOC structure and sets the defaults in its fields.
  34.  *  Used from application initialization and various server methods that
  35.  *  create a document.
  36.  *
  37.  * Parameters:
  38.  *  pVtblDoc        LPOLESERVERDOCVTBL used to initialize the pvtbl field.
  39.  *
  40.  * Return Value:
  41.  *  LPCOSMODOC      Pointer to the allocated structure in local memory.
  42.  *                  The hMem field will contain a handle to the structure
  43.  *                  to pass to LocalFree.
  44.  *
  45.  */
  46.  
  47. LPCOSMODOC WINAPI PDocumentAllocate(LPOLESERVERDOCVTBL pVtblDoc)
  48.     {
  49.     HLOCAL      hMem;
  50.     LPCOSMODOC  pDoc;
  51.  
  52.     hMem=LocalAlloc(LPTR, CBCOSMODOC);
  53.  
  54.     if (NULL==hMem)
  55.         return NULL;
  56.  
  57.     pDoc=(LPCOSMODOC)(PSTR)hMem;
  58.  
  59.     pDoc->hMem=hMem;
  60.     pDoc->fRelease=TRUE;
  61.     pDoc->pvtbl=pVtblDoc;
  62.  
  63.     return pDoc;
  64.     }
  65.  
  66.  
  67.  
  68.  
  69. /*
  70.  * DocClose
  71.  *
  72.  * Purpose:
  73.  *  Instructs the server to unconditionally close the document.  OLESVR
  74.  *  calls DocClose when the client initiates a shutdown.  OLESVR always
  75.  *  calls this function before calling ServerRelease.
  76.  *
  77.  * Parameters:
  78.  *  pDoc            LPCOSMODOC identifying the document affected.
  79.  *
  80.  * Return Value:
  81.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  82.  */
  83.  
  84. OLESTATUS WINAPI DocClose(LPCOSMODOC pDoc)
  85.     {
  86.     OLESTATUS       os;
  87.  
  88.     /*
  89.      * Take no action to notify user--Client will take care of that.
  90.      *
  91.      * 1.   Call OleRevokeServerDoc; resources are freed when OLESVR
  92.      *      calls DocRelease.
  93.      * 2.   Return the return value of OleRevokeServerDoc, which will
  94.      *      generally be OLE_OK.
  95.      */
  96.  
  97.     os=OleRevokeServerDoc(pDoc->lh);
  98.     return os;
  99.     }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. /*
  107.  * DocGetObject
  108.  *
  109.  * Purpose:
  110.  *  Requests the server application to create an OLEOBJECT structure.
  111.  *
  112.  * Parameters:
  113.  *  pDoc            LPCOSMODOC identifying the document affected.
  114.  *  pszItem         OLE_LPCSTR specifying the name of the item in a document
  115.  *                  for which the object is to be created.  If NULL, then
  116.  *                  the entire document is requested.  This could be a
  117.  *                  range of spreadsheet cells like "R1:C3-R10:C50"
  118.  *  ppObj           LPLPOLEOBJECT at which to store a pointer to the
  119.  *                  OLEOBJECT structure.
  120.  *  pClient         LPOLECLIENT that should be associated with
  121.  *                  the object in order to notify OLECLI when the object
  122.  *                  changes.
  123.  *
  124.  * Return Value:
  125.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  126.  */
  127.  
  128. OLESTATUS WINAPI DocGetObject(LPCOSMODOC pDoc, OLE_LPCSTR pszItem
  129.     , LPLPOLEOBJECT ppObj, LPOLECLIENT pClient)
  130.     {
  131.     LPCOSMOOBJECT   pObj;
  132.  
  133.     /*
  134.      * 1.   Allocate and initialize an OLEOBJECT structure.
  135.      * 2.   Store pClient in the object's OLEOBJECT structure for use
  136.      *      in sending notifications to the client.
  137.      * 3.   Store a pointer to the new OLEOBJECT structure in ppObj.
  138.      * 4.   Return OLE_OK if successful, OLE_ERROR_NAME if pszObj is
  139.      *      not recognized, or OLE_ERROR_MEMORY if the object could not
  140.      *      be allocated.
  141.      *
  142.      * This function is called in response to a client calling
  143.      * OleGetObject.
  144.      */
  145.  
  146.     //Allocate one from local FIXED memory.
  147.     pObj=PObjectAllocate(&pOLE->vtblObj);
  148.  
  149.     if (NULL==pObj)
  150.         return OLE_ERROR_MEMORY;
  151.  
  152.     //Remember who we created for freeing in DocRelease.
  153.     pDoc->pObj=pObj;
  154.  
  155.     //Must save this for sending notifications.
  156.     pObj->pClient=pClient;
  157.  
  158.     //Pass back the pointer to this object.
  159.     *ppObj=(LPOLEOBJECT)pObj;
  160.     return OLE_OK;
  161.     }
  162.  
  163.  
  164.  
  165. /*
  166.  * DocExecute
  167.  *
  168.  * Purpose:
  169.  *  Passes DDE Execute strings related to the document to the server
  170.  *  application.
  171.  *
  172.  * Parameters:
  173.  *  pDoc            LPCOSMODOC identifying the document affected.
  174.  *  hCommands       HGLOBAL to memory containing DDE execute strings.
  175.  *
  176.  * Return Value:
  177.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  178.  */
  179.  
  180. OLESTATUS WINAPI DocExecute(LPCOSMODOC pDoc, HGLOBAL hCommands)
  181.     {
  182.     /*
  183.      * 1.   Lock the hCommands handle to access the execute strings.
  184.      * 2.   Parse and execute the commands as necessary.
  185.      * 3.   DO NOT FREE hCommands.
  186.      * 4.   Return OLE_OK if successful, OLE_ERROR_COMMAND otherwise.
  187.      */
  188.  
  189.     return OLE_ERROR_COMMAND;
  190.     }
  191.  
  192.  
  193.  
  194.  
  195.  
  196. /*
  197.  * DocRelease
  198.  *
  199.  * Purpose:
  200.  *  Notifies the server when a revoked document may be destroyed.
  201.  *
  202.  * Parameters:
  203.  *  pDoc            LPCOSMODOC identifying the document affected.
  204.  *
  205.  * Return Value:
  206.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  207.  */
  208.  
  209. OLESTATUS WINAPI DocRelease(LPCOSMODOC pDoc)
  210.     {
  211.     /*
  212.      * 1.   Free any resources allocated for this document, but
  213.      *      DO NOT free the OLESERVERDOC structure itself.  This could
  214.      *      include freeing objects, however.
  215.      * 2.   Set a flag to indicate that Release has been called.
  216.      * 3.   Return OLE_OK if successful, OLE_ERROR_GENERIC otherwise.
  217.      */
  218.  
  219.     if (0!=pDoc->aObject)
  220.         {
  221.         DeleteAtom(pDoc->aObject);
  222.         pDoc->aObject=0;
  223.         }
  224.  
  225.     if (0!=pDoc->aClient)
  226.         {
  227.         DeleteAtom(pDoc->aClient);
  228.         pDoc->aClient=0;
  229.         }
  230.  
  231.  
  232.     //Release any object stored in this document.
  233.     if (0!=pDoc->pObj)
  234.         {
  235.         if (NULL!=pDoc->pObj->hMem)
  236.             LocalFree(pDoc->pObj->hMem);
  237.         }
  238.  
  239.     pDoc->pObj=NULL;
  240.  
  241.     pDoc->fRelease=TRUE;
  242.     return OLE_OK;
  243.     }
  244.  
  245.  
  246.  
  247.  
  248.  
  249. /*
  250.  * DocSave
  251.  *
  252.  * Purpose:
  253.  *  Instructs the server application to save the document.  If DocSave is
  254.  *  called you are assumed to have a know filename to which you can save
  255.  *  since this method is only used in linking.
  256.  *
  257.  * Parameters:
  258.  *  pDoc            LPCOSMODOC identifying the document affected.
  259.  *
  260.  * Return Value:
  261.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  262.  */
  263.  
  264. OLESTATUS WINAPI DocSave(LPCOSMODOC pDoc)
  265.     {
  266.     /*
  267.      * 1.   Save the document to the known filename.  How you save
  268.      *      documents is application-specific.
  269.      * 2.   Return OLE_OK if the save is successful, OLE_ERROR_GENERIC
  270.      *      otherwise.
  271.      */
  272.  
  273.     SendMessage(pGlob->hWnd, WM_COMMAND, IDM_FILESAVE, 0L);
  274.     return OLE_OK;
  275.     }
  276.  
  277.  
  278.  
  279.  
  280.  
  281. /*
  282.  * DocSetColorScheme
  283.  *
  284.  * Purpose:
  285.  *  Provides a color scheme that the client application recommends for
  286.  *  rendering graphical data.
  287.  *
  288.  * Parameters:
  289.  *  pDoc            LPCOSMODOC identifying the document affected.
  290.  *  pPal            LPLOGPALETTE describing the recommended palette.
  291.  *
  292.  * Return Value:
  293.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  294.  */
  295.  
  296. OLESTATUS WINAPI DocSetColorScheme(LPCOSMODOC pDoc
  297.     , OLE_CONST LOGPALETTE FAR *pPal)
  298.     {
  299.     /*
  300.      * Servers are not required to use this palette.  The LPLOGPALETTE
  301.      * only is a convenient structure to contain the color scheme; IT DOES
  302.      * not REPRESENT A PALETTE IN STRICT TERMS!  Do NOT call CreatePalette
  303.      * and try to realize it.
  304.      *
  305.      * The color scheme contained in the LOGPALETTE structure contains
  306.      * a number of colors where the first color is the suggested foreground,
  307.      * the second the suggested background, then the first HALF of those
  308.      * remaining are suggested fill colors and the last half suggested
  309.      * line colors.  If there are an odd number of colors, give the extra
  310.      * color to the fill colors, that is, there is one less line color than
  311.      * fill colors.
  312.      */
  313.  
  314.     return OLE_ERROR_PALETTE;
  315.     }
  316.  
  317.  
  318.  
  319. /*
  320.  * DocSetDocDimensions
  321.  *
  322.  * Purpose:
  323.  *  Specifies the rectangle on the target device for which EMBEDDED
  324.  *  objects should be formatted.
  325.  *
  326.  * Parameters:
  327.  *  pDoc            LPCOSMODOC identifying the document affected.
  328.  *  pRect           LPRECT to the device rectangle in MM_HIMETRIC units.
  329.  *
  330.  * Return Value:
  331.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  332.  */
  333.  
  334. OLESTATUS WINAPI DocSetDocDimensions(LPCOSMODOC pDoc
  335.     , OLE_CONST RECT FAR *pRect)
  336.     {
  337.     /*
  338.      * OLESVR will call this method when the client has resized the
  339.      * object.
  340.      *
  341.      * In this case we try to make the parent window the correct size
  342.      * to just contain the object.
  343.      */
  344.  
  345.     //First, convert the rectangle to units we can deal with MM_TEXT.
  346.     RectConvertToDevice(pGlob->hWnd, (LPRECT)pRect);
  347.  
  348.     /*
  349.      * Tell the Polyline document to use this rectangle, also notifying
  350.      * the parent which will then resize itself.
  351.      */
  352.     SendMessage(pGlob->hWndPolyline, PLM_RECTSET, TRUE, (LONG)pRect);
  353.  
  354.     return OLE_OK;
  355.     }
  356.  
  357.  
  358.  
  359.  
  360.  
  361. /*
  362.  * DocSetHostNames
  363.  *
  364.  * Purpose:
  365.  *  Set names that should be used for window titles, only for
  366.  *  embedded objects; linked objects have their own titles as they
  367.  *  are loaded through the server application's usual file loading
  368.  *  mechanism.
  369.  *
  370.  * Parameters:
  371.  *  pDoc            LPCOSMODOC identifying the document affected.
  372.  *  pszClient       OLE_LPCSTR to the name of the client application.
  373.  *  pszObj          OLE_LPCSTR to the name of the embedded object.
  374.  *
  375.  * Return Value:
  376.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  377.  */
  378.  
  379. OLESTATUS WINAPI DocSetHostNames(LPCOSMODOC pDoc, OLE_LPCSTR pszClient
  380.     , OLE_LPCSTR pszObj)
  381.     {
  382.     char        szTemp[256];
  383.  
  384.     /*
  385.      * 1.   Change the title bar to reflect the embedded state with the
  386.      *      appropriate names.
  387.      * 2.   Change the File menu to reflect the embedded state and the name
  388.      *      of the client application.
  389.      * 3.   Store the object and client names in teh OLESERVERDOC structure.
  390.      *      These will be needed later for message boxes where the name of
  391.      *      the client application must be displayed.
  392.      * 4.   Return OLE_OK if successful, OLE_ERROR_GENERIC otherwise.
  393.      */
  394.  
  395.     wsprintf(szTemp, "%s in %s", pszObj, pszClient);
  396.     WindowTitleSet(pGlob->hWnd, szTemp);
  397.     MenuEmbeddingSet(pGlob->hWnd, (LPSTR)pszClient, TRUE);
  398.  
  399.     if (0!=pDoc->aObject)
  400.         DeleteAtom(pDoc->aObject);
  401.  
  402.     pDoc->aObject=AddAtom(pszObj);
  403.  
  404.     if (0!=pDoc->aClient)
  405.         DeleteAtom(pDoc->aClient);
  406.  
  407.     pDoc->aClient=AddAtom(pszClient);
  408.  
  409.     return OLE_OK;
  410.     }
  411.  
  412.  
  413.  
  414.  
  415.  
  416. #endif //MAKEOLESERVER
  417.