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 / chap14 / cosmo / autoapp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-22  |  11.6 KB  |  529 lines

  1. /*
  2.  * AUTOAPP.CPP
  3.  * Cosmo Chapter 14
  4.  *
  5.  * "Application" object for Cosmo's OLE Automation support, based
  6.  * on CAutoBase which handles the type information and IDispatch
  7.  * part of the object.  What is here are the specific properties
  8.  * and methods for the "Application" object.
  9.  *
  10.  * Copyright (c)1993-1995 Microsoft Corporation, All Right Reserved.
  11.  *
  12.  * Kraig Brockschmidt, Microsoft
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  INTERNET>kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "cosmo.h"
  19.  
  20.  
  21. /*
  22.  * CAutoApp::CAutoApp
  23.  *
  24.  * Constructor Parameters:
  25.  *  pFR             PCCosmoFrame to the frame object that we use
  26.  *                  to implement much of this interface.
  27.  */
  28.  
  29. CAutoApp::CAutoApp(PCCosmoFrame pFR)
  30.     : CAutoBase(pFR, pFR->m_hInst, IID_ICosmoApplication
  31.     , DIID_DICosmoApplication, ObjectDestroyed)
  32.     {
  33.     m_fQuitCalled=FALSE;
  34.     return;
  35.     }
  36.  
  37.  
  38. /*
  39.  * CAutoApp::QueryInterface
  40.  * CAutoApp::AddRef
  41.  * CAutoApp::Release
  42.  */
  43.  
  44. STDMETHODIMP CAutoApp::QueryInterface(REFIID riid, PPVOID ppv)
  45.     {
  46.     *ppv=NULL;
  47.  
  48.     if (IID_IUnknown==riid || IID_ICosmoApplication==riid)
  49.         *ppv=(IUnknown *)this;
  50.  
  51.     if (IID_IDispatch==riid || m_diid==riid)
  52.         *ppv=m_pImpIDispatch;
  53.  
  54.     if (IID_IExternalConnection==riid)
  55.         *ppv=m_pImpIExtConn;
  56.  
  57.     if (NULL!=*ppv)
  58.         {
  59.         ((LPUNKNOWN)*ppv)->AddRef();
  60.         return NOERROR;
  61.         }
  62.  
  63.     return ResultFromScode(E_NOINTERFACE);
  64.     }
  65.  
  66. STDMETHODIMP_(ULONG) CAutoApp::AddRef(void)
  67.     {
  68.     return ++m_cRef;
  69.     }
  70.  
  71.  
  72. STDMETHODIMP_(ULONG) CAutoApp::Release(void)
  73.     {
  74.     if (0L!=--m_cRef)
  75.         return m_cRef;
  76.  
  77.     if (!m_fQuitCalled && NULL!=m_pfnDestroy)
  78.         (*m_pfnDestroy)();
  79.  
  80.     delete this;
  81.     return 0L;
  82.     }
  83.  
  84.  
  85. /*
  86.  * CAutoApp::VTableInterface
  87.  *
  88.  * Purpose:
  89.  *  Returns the right vtable pointer to use when calling
  90.  *  ITypeInfo::Invoke (see CImpIDispatch::Invoke in AUTOBASE.CPP).
  91.  */
  92.  
  93. void *CAutoApp::VTableInterface(void)
  94.     {
  95.     return (ICosmoApplication *)this;
  96.     }
  97.  
  98.  
  99. //All that follows is the ICosmoApplication implementation
  100.  
  101. /*
  102.  * CAutoApp::Application
  103.  * Property, read-only
  104.  *
  105.  * Returns our own IDispatch for use with DISPID_VALUE.
  106.  */
  107.  
  108. STDMETHODIMP_(IDispatch *) CAutoApp::get_Application(void)
  109.     {
  110.     void       *pv;
  111.  
  112.     //This will set pv to NULL on failure
  113.     QueryInterface(IID_IDispatch, &pv);
  114.     return (IDispatch *)pv;
  115.     }
  116.  
  117.  
  118.  
  119. /*
  120.  * CAutoApp::ActiveFigure
  121.  * Property, read-only
  122.  *
  123.  * Returns the active document's IDispatch
  124.  */
  125.  
  126. STDMETHODIMP_(IDispatch *) CAutoApp::get_ActiveFigure(void)
  127.     {
  128.     void           *pv=NULL;
  129.     PCCosmoDoc      pDoc;
  130.     PCAutoFigure    pFig;
  131.  
  132.     pDoc=(PCCosmoDoc)((PCCosmoClient)m_pFR->m_pCL)->ActiveDocument();
  133.  
  134.     if (NULL==pDoc)
  135.         return NULL;
  136.  
  137.     pFig=pDoc->AutoFigure();
  138.  
  139.     if (NULL!=pFig)
  140.         pFig->QueryInterface(IID_IDispatch, &pv);
  141.  
  142.     return (IDispatch *)pv;
  143.     }
  144.  
  145.  
  146.  
  147. /*
  148.  * CAutoApp::Caption
  149.  * Property, read-only
  150.  *
  151.  * Retrieve the application's caption bar
  152.  */
  153.  
  154. STDMETHODIMP_(BSTR) CAutoApp::get_Caption(void)
  155.     {
  156.     const int   cch=256;
  157.     TCHAR       szCaption[cch];
  158.  
  159.     GetWindowText(m_pFR->Window(), szCaption, cch);
  160.  
  161.    #ifdef WIN32ANSI
  162.     OLECHAR     szTemp[cch];
  163.  
  164.     MultiByteToWideChar(CP_ACP, 0, szCaption, -1, szTemp, cch);
  165.     return SysAllocString(szTemp);
  166.    #else
  167.     return SysAllocString(szCaption);
  168.    #endif
  169.     }
  170.  
  171.  
  172.  
  173.  
  174. /*
  175.  * CAutoApp::Figures
  176.  * Property, read-only
  177.  *
  178.  * Retrieve the collection object for figures (documents)
  179.  */
  180.  
  181. STDMETHODIMP_(IDispatch *) CAutoApp::get_Figures(void)
  182.     {
  183.     void            *pv=NULL;
  184.     PCAutoFigures    pFigs;
  185.  
  186.     pFigs=((PCCosmoClient)m_pFR->m_pCL)->AutoFigures();
  187.  
  188.     if (NULL!=pFigs)
  189.         pFigs->QueryInterface(IID_IDispatch, &pv);
  190.  
  191.     return (IDispatch *)pv;
  192.     }
  193.  
  194.  
  195.  
  196. /*
  197.  * CAutoApp::FullName, Name, Path
  198.  * Properties, read-only
  199.  *
  200.  * Retrieve the full pathname of the application module (FullName),
  201.  * just the module name (Name), or just the path (Path).
  202.  */
  203.  
  204. STDMETHODIMP_(BSTR) CAutoApp::get_FullName(void)
  205.     {
  206.     const int   cch=512;
  207.     TCHAR       szModule[cch];
  208.  
  209.     GetModuleFileName(m_pFR->Instance(), szModule, cch);
  210.  
  211.    #ifdef WIN32ANSI
  212.     OLECHAR     szTemp[cch];
  213.     MultiByteToWideChar(CP_ACP, 0, szModule, -1, szTemp, cch);
  214.     return SysAllocString(szTemp);
  215.    #else
  216.     return SysAllocString(szModule);
  217.    #endif
  218.     }
  219.  
  220.  
  221. STDMETHODIMP_(BSTR) CAutoApp::get_Name(void)
  222.     {
  223.     BSTR        bstrFull=NULL;
  224.     BSTR        bstrName=NULL;
  225.     const int   cch=256;
  226.     TCHAR       szName[256];
  227.  
  228.     //Get the full path
  229.     bstrFull=get_FullName();
  230.  
  231.     if (NULL==bstrFull)
  232.         return NULL;
  233.  
  234.     //Now retrieve just the filename
  235.    #ifdef WIN32ANSI
  236.     char        szTemp[cch];
  237.     OLECHAR     szTempW[cch];
  238.  
  239.     WideCharToMultiByte(CP_ACP, 0, bstrFull, -1, szTemp
  240.         , cch, NULL, NULL);
  241.     if (0==GetFileTitle(szTemp, szName, cch))
  242.         {
  243.         MultiByteToWideChar(CP_ACP, 0, szName, -1, szTempW, cch);
  244.         bstrName=SysAllocString(szTempW);
  245.         }
  246.    #else
  247.     if (0==GetFileTitle(bstrFull, szName, cch))
  248.         bstrName=SysAllocString(szName);
  249.    #endif
  250.  
  251.     SysFreeString(bstrFull);
  252.     return bstrName;
  253.     }
  254.  
  255.  
  256. STDMETHODIMP_(BSTR) CAutoApp::get_Path(void)
  257.     {
  258.     BSTR        bstrFull=NULL;
  259.     BSTR        bstrName=NULL;
  260.     BSTR        bstrPath=NULL;
  261.  
  262.     bstrFull=get_FullName();
  263.  
  264.     if (NULL==bstrFull)
  265.         return NULL;
  266.  
  267.     bstrName=get_Name();
  268.  
  269.     if (NULL!=bstrName)
  270.         {
  271.         LPOLESTR    psz;
  272.  
  273.         /*
  274.          * Find the position of bstrName in bstrFull then copy
  275.          * only charaters up to that point into bstrPath.
  276.          */
  277.        #ifdef WIN32ANSI
  278.         psz=wcsstr(bstrFull, bstrName);
  279.        #else
  280.         psz=_tcsstr(bstrFull, bstrName);
  281.        #endif
  282.  
  283.         //The -1 accounts for the \ before the filename
  284.         bstrPath=SysAllocStringLen(bstrFull
  285.             , ((DWORD)psz-(DWORD)bstrFull)/sizeof(OLECHAR)-1);
  286.         SysFreeString(bstrName);
  287.         }
  288.  
  289.     SysFreeString(bstrFull);
  290.     return bstrPath;
  291.     }
  292.  
  293.  
  294.  
  295. /*
  296.  * CAutoApp::Left, Top, Width, Height
  297.  * Properties, read-write
  298.  *
  299.  * Horizontal (Left) and vertical (Top) positions of the frame
  300.  * window from the left and top edges of the screen; horizontal
  301.  * (Width) and  vertical (Height) dimensions of the frame
  302.  * window.  All of these functions call our private member
  303.  * MoveSize, a helper function.
  304.  */
  305.  
  306. STDMETHODIMP_(long) CAutoApp::get_Left(void)
  307.     {
  308.     return MoveSize(MOVESIZEACTION_GETLEFT, 0, 0, 0, 0);
  309.     }
  310.  
  311. STDMETHODIMP_(void) CAutoApp::put_Left(long x)
  312.     {
  313.     MoveSize(MOVESIZEACTION_LEFT, x, 0, 0, 0);
  314.     return;
  315.     }
  316.  
  317. STDMETHODIMP_(long) CAutoApp::get_Top(void)
  318.     {
  319.     return MoveSize(MOVESIZEACTION_GETTOP, 0, 0, 0, 0);
  320.     }
  321.  
  322. STDMETHODIMP_(void) CAutoApp::put_Top(long y)
  323.     {
  324.     MoveSize(MOVESIZEACTION_TOP, 0, y, 0, 0);
  325.     return;
  326.     }
  327.  
  328. STDMETHODIMP_(long) CAutoApp::get_Width(void)
  329.     {
  330.     return MoveSize(MOVESIZEACTION_GETWIDTH, 0, 0, 0, 0);
  331.     }
  332.  
  333. STDMETHODIMP_(void) CAutoApp::put_Width(long cx)
  334.     {
  335.     MoveSize(MOVESIZEACTION_WIDTH, 0, 0, cx, 0);
  336.     return;
  337.     }
  338.  
  339. STDMETHODIMP_(long) CAutoApp::get_Height(void)
  340.     {
  341.     return MoveSize(MOVESIZEACTION_GETHEIGHT, 0, 0, 0, 0);
  342.     }
  343.  
  344. STDMETHODIMP_(void) CAutoApp::put_Height(long cy)
  345.     {
  346.     MoveSize(MOVESIZEACTION_HEIGHT, 0, 0, 0, cy);
  347.     return;
  348.     }
  349.  
  350.  
  351.  
  352.  
  353. /*
  354.  * CAutoApp::Visible
  355.  * Properties, read-write
  356.  *
  357.  * Controls visibility of the frame window (which is hidden by
  358.  * default when the application is launched for Automation.
  359.  */
  360.  
  361. STDMETHODIMP_(VARIANT_BOOL) CAutoApp::get_Visible(void)
  362.     {
  363.     return (VARIANT_BOOL)IsWindowVisible(m_pFR->Window());
  364.     }
  365.  
  366. STDMETHODIMP_(void) CAutoApp::put_Visible(VARIANT_BOOL fShow)
  367.     {
  368.     ShowWindow(m_pFR->Window(), fShow ? SW_SHOW : SW_HIDE);
  369.     return;
  370.     }
  371.  
  372.  
  373.  
  374. /*
  375.  * CAutoApp::StatusBar
  376.  * Properties, read-write
  377.  *
  378.  * Retrieve or modify the status line text.
  379.  * default when the application is launched for Automation.
  380.  */
  381.  
  382. STDMETHODIMP_(BSTR) CAutoApp::get_StatusBar(void)
  383.     {
  384.     const int   cch=256;
  385.     TCHAR       szText[cch];
  386.     BSTR        bstrText=NULL;
  387.  
  388.    #ifdef WIN32ANSI
  389.     if (0!=m_pFR->StatusLine()->MessageGet(szText, cch))
  390.         {
  391.         OLECHAR szTemp[cch];
  392.  
  393.         MultiByteToWideChar(CP_ACP, 0, szText, -1, szTemp, cch);
  394.         bstrText=SysAllocString(szTemp);
  395.         }
  396.    #else
  397.     if (0!=m_pFR->StatusLine()->MessageGet(szText, cch))
  398.         bstrText=SysAllocString(szText);
  399.    #endif
  400.  
  401.     return bstrText;
  402.     }
  403.  
  404. STDMETHODIMP_(void) CAutoApp::put_StatusBar(BSTR bstrText)
  405.     {
  406.     if (NULL==bstrText)
  407.         return;
  408.  
  409.    #ifdef WIN32ANSI
  410.     char    szTemp[256];
  411.  
  412.     WideCharToMultiByte(CP_ACP, 0, bstrText, -1, szTemp, 256
  413.         , NULL, NULL);
  414.     m_pFR->StatusLine()->MessageSet(szTemp);
  415.    #else
  416.     m_pFR->StatusLine()->MessageSet(bstrText);
  417.    #endif
  418.     return;
  419.     }
  420.  
  421.  
  422.  
  423. /*
  424.  * CAutoApp::Quit
  425.  * Method
  426.  *
  427.  * Instructs the application to terminate.  This behaves as
  428.  * if the user closes the app directly.
  429.  */
  430.  
  431. STDMETHODIMP_(void) CAutoApp::Quit(void)
  432.     {
  433.     ObjectDestroyed();
  434.     m_fQuitCalled=TRUE;
  435.     return;
  436.     }
  437.  
  438.  
  439. /*
  440.  * CAutoApp::MoveSize
  441.  *
  442.  * Purpose:
  443.  *  Helper function for the Left, Top, Width, and Height properties
  444.  *  that centralizes calls to GetWindowRect and SetWindowPos,
  445.  *  reducing overall code somewhat.
  446.  *
  447.  * Parameters:
  448.  *  iAction         MOVESIZEACTION enum value to indicate what
  449.  *                  type of moving/sizing to perform
  450.  *  x, y            long position coordinates;  x is only meaningful
  451.  *                  with MOVESIZEACTION_LEFT, y only with _TOP
  452.  *  cx, cy          long extents;  cx is only meaningful
  453.  *                  with MOVESIZEACTION_WIDTH, cy only with _HEIGHT
  454.  *
  455.  * Return Value:
  456.  *  long            The current x, y, cx, or cy value depending on
  457.  *                  iAction being _GETLEFT, _GETTOP, _GETWIDTH, or
  458.  *                  _GETHEIGHT.
  459.  */
  460.  
  461. long CAutoApp::MoveSize(MOVESIZEACTION iAction, long x, long y
  462.     , long cx, long cy)
  463.     {
  464.     RECT        rc;
  465.     long        x1, y1, cx1, cy1;
  466.     UINT        uFlags;
  467.  
  468.     GetWindowRect(m_pFR->Window(), &rc);
  469.  
  470.     //By default we'll do nothing
  471.     x1=rc.left;
  472.     y1=rc.top;
  473.     cx1=rc.right-rc.left;
  474.     cy1=rc.bottom-rc.top;
  475.     uFlags=0L;
  476.  
  477.     switch (iAction)
  478.         {
  479.         /*
  480.          * Each individual property modifies the appropriate
  481.          * variable x1, y1, cx1, cy1, as well as uFlags to set
  482.          * up SetWindowPos call.
  483.          */
  484.         case MOVESIZEACTION_LEFT:
  485.             x1=x;
  486.             uFlags=SWP_NOSIZE;
  487.             break;
  488.  
  489.         case MOVESIZEACTION_TOP:
  490.             y1=y;
  491.             uFlags=SWP_NOSIZE;
  492.             break;
  493.  
  494.         case MOVESIZEACTION_WIDTH:
  495.             cx1=cx;
  496.             uFlags=SWP_NOMOVE;
  497.             break;
  498.  
  499.         case MOVESIZEACTION_HEIGHT:
  500.             cy1=cy;
  501.             uFlags=SWP_NOMOVE;
  502.             break;
  503.  
  504.  
  505.         case MOVESIZEACTION_GETLEFT:
  506.             return rc.left;
  507.  
  508.         case MOVESIZEACTION_GETTOP:
  509.             return rc.top;
  510.  
  511.         case MOVESIZEACTION_GETWIDTH:
  512.             return rc.right-rc.left;
  513.  
  514.         case MOVESIZEACTION_GETHEIGHT:
  515.             return rc.bottom-rc.top;
  516.  
  517.         default:
  518.             return 0;
  519.         }
  520.  
  521.     //We only get here on propety changes
  522.     SetWindowPos(m_pFR->Window(), NULL
  523.         , (int)x1, (int)y1, (int)cx1, (int)cy1
  524.         , SWP_NOZORDER | SWP_NOACTIVATE | uFlags);
  525.  
  526.     //Irrelevant for property changes.
  527.     return 0;
  528.     }
  529.