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 / chap22 / patron / iipsite.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  12KB  |  493 lines

  1. /*
  2.  * IIPSITE.CPP
  3.  * Patron Chapter 22
  4.  *
  5.  * IOleInPlaceSite interface implementation for Patron
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #include "patron.h"
  16.  
  17.  
  18. BOOL        g_fSwitchingActive=FALSE;
  19.  
  20. /*
  21.  * CImpIOleInPlaceSite::CImpIOleInPlaceSite
  22.  * CImpIOleInPlaceSite::~CImpIOleInPlaceSite
  23.  *
  24.  * Parameters (Constructor):
  25.  *  pTen            PCTenant of the tenant we're in.
  26.  *  pUnkOuter       LPUNKNOWN to which we delegate.
  27.  */
  28.  
  29. CImpIOleInPlaceSite::CImpIOleInPlaceSite(PCTenant pTen
  30.     , LPUNKNOWN pUnkOuter)
  31.     {
  32.     m_cRef=0;
  33.     m_pTen=pTen;
  34.     m_pUnkOuter=pUnkOuter;
  35.     return;
  36.     }
  37.  
  38. CImpIOleInPlaceSite::~CImpIOleInPlaceSite(void)
  39.     {
  40.     return;
  41.     }
  42.  
  43.  
  44.  
  45. /*
  46.  * CImpIOleInPlaceSite::QueryInterface
  47.  * CImpIOleInPlaceSite::AddRef
  48.  * CImpIOleInPlaceSite::Release
  49.  *
  50.  * Purpose:
  51.  *  IUnknown members for CImpIOleInPlaceSite object.
  52.  */
  53.  
  54. STDMETHODIMP CImpIOleInPlaceSite::QueryInterface(REFIID riid
  55.     , PPVOID ppv)
  56.     {
  57.     return m_pUnkOuter->QueryInterface(riid, ppv);
  58.     }
  59.  
  60.  
  61. STDMETHODIMP_(ULONG) CImpIOleInPlaceSite::AddRef(void)
  62.     {
  63.     ++m_cRef;
  64.     return m_pUnkOuter->AddRef();
  65.     }
  66.  
  67. STDMETHODIMP_(ULONG) CImpIOleInPlaceSite::Release(void)
  68.     {
  69.     --m_cRef;
  70.     return m_pUnkOuter->Release();
  71.     }
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.  * CImpIOleInPlaceActiveObject::GetWindow
  78.  *
  79.  * Purpose:
  80.  *  Retrieves the handle of the window associated with the object
  81.  *  on which this interface is implemented.
  82.  *
  83.  * Parameters:
  84.  *  phWnd           HWND * in which to store the window handle.
  85.  *
  86.  * Return Value:
  87.  *  HRESULT         NOERROR if successful, E_FAIL if there is no
  88.  *                  window.
  89.  */
  90.  
  91. STDMETHODIMP CImpIOleInPlaceSite::GetWindow(HWND *phWnd)
  92.     {
  93.     *phWnd=m_pTen->m_hWnd;
  94.     return NOERROR;
  95.     }
  96.  
  97.  
  98.  
  99.  
  100. /*
  101.  * CImpIOleInPlaceActiveObject::ContextSensitiveHelp
  102.  *
  103.  * Purpose:
  104.  *  Instructs the object on which this interface is implemented to
  105.  *  enter or leave a context-sensitive help mode.
  106.  *
  107.  * Parameters:
  108.  *  fEnterMode      BOOL TRUE to enter the mode, FALSE otherwise.
  109.  *
  110.  * Return Value:
  111.  *  HRESULT         NOERROR
  112.  */
  113.  
  114. STDMETHODIMP CImpIOleInPlaceSite::ContextSensitiveHelp
  115.     (BOOL fEnterMode)
  116.     {
  117.     return NOERROR;
  118.     }
  119.  
  120.  
  121.  
  122.  
  123. /*
  124.  * CImpIOleInPlaceSite::CanInPlaceActivate
  125.  *
  126.  * Purpose:
  127.  *  Answers the server whether or not we can currently in-place
  128.  *  activate its object.  By implementing this interface we say
  129.  *  that we support in-place activation, but through this function
  130.  *  we indicate whether the object can currently be activated
  131.  *  in-place.  Iconic aspects, for example, cannot, meaning we
  132.  *  return S_FALSE.
  133.  *
  134.  * Parameters:
  135.  *  None
  136.  *
  137.  * Return Value:
  138.  *  HRESULT         NOERROR if we can in-place activate the object
  139.  *                  in this site, S_FALSE if not.
  140.  */
  141.  
  142. STDMETHODIMP CImpIOleInPlaceSite::CanInPlaceActivate(void)
  143.     {
  144.     if (DVASPECT_CONTENT!=m_pTen->m_fe.dwAspect)
  145.         return ResultFromScode(S_FALSE);
  146.  
  147.     if (TENANTTYPE_EMBEDDEDOBJECT!=m_pTen->m_tType)
  148.         return ResultFromScode(S_FALSE);
  149.  
  150.     return NOERROR;
  151.     }
  152.  
  153.  
  154.  
  155.  
  156. /*
  157.  * CImpIOleInPlaceSite::OnInPlaceActivate
  158.  *
  159.  * Purpose:
  160.  *  Informs the container that an object is being activated in-place
  161.  *  such that the container can prepare appropriately.  The
  162.  *  container does not, however, make any user interface changes at
  163.  *  this point.  See OnUIActivate.
  164.  *
  165.  * Parameters:
  166.  *  None
  167.  *
  168.  * Return Value:
  169.  *  HRESULT         NOERROR or an appropriate error code.
  170.  */
  171.  
  172. STDMETHODIMP CImpIOleInPlaceSite::OnInPlaceActivate(void)
  173.     {
  174.     //m_pIOleIPObject is our in-place flag.
  175.     m_pTen->m_pObj->QueryInterface(IID_IOleInPlaceObject
  176.         , (PPVOID)&m_pTen->m_pIOleIPObject);
  177.     return NOERROR;
  178.     }
  179.  
  180.  
  181.  
  182.  
  183. /*
  184.  * CImpIOleInPlaceSite::OnInPlaceDeactivate
  185.  *
  186.  * Purpose:
  187.  *  Notifies the container that the object has deactivated itself
  188.  *  from an in-place state.  Opposite of OnInPlaceActivate.  The
  189.  *  container does not change any UI at this point.
  190.  *
  191.  * Parameters:
  192.  *  None
  193.  *
  194.  * Return Value:
  195.  *  HRESULT         NOERROR or an appropriate error code.
  196.  */
  197.  
  198. STDMETHODIMP CImpIOleInPlaceSite::OnInPlaceDeactivate(void)
  199.     {
  200.     /*
  201.      * Since we don't have an Undo command, we can tell the object
  202.      * right away to discard its Undo state.
  203.      */
  204.     m_pTen->Activate(OLEIVERB_DISCARDUNDOSTATE, NULL);
  205.     ReleaseInterface(m_pTen->m_pIOleIPObject);
  206.     return NOERROR;
  207.     }
  208.  
  209.  
  210.  
  211.  
  212. /*
  213.  * CImpIOleInPlaceSite::OnUIActivate
  214.  *
  215.  * Purpose:
  216.  *  Informs the container that the object is going to start munging
  217.  *  around with user interface, like replacing the menu.  The
  218.  *  container should remove any relevant UI in preparation.
  219.  *
  220.  * Parameters:
  221.  *  None
  222.  *
  223.  * Return Value:
  224.  *  HRESULT         NOERROR or an appropriate error code.
  225.  */
  226.  
  227. STDMETHODIMP CImpIOleInPlaceSite::OnUIActivate(void)
  228.     {
  229.     PCPatronDoc     pDoc;
  230.  
  231.     m_pTen->m_pPG->m_fAddUI=FALSE;
  232.  
  233.     pDoc=(PCPatronDoc)SendMessage(GetParent(m_pTen->m_hWnd)
  234.         , DOCM_PDOCUMENT, 0, 0L);
  235.  
  236.     /*
  237.      * Change the currently selected tenant in the page.  This
  238.      * will UIDeactivate the currently UI Active tenant.
  239.      */
  240.     g_fSwitchingActive=TRUE;
  241.     m_pTen->m_pPG->m_pPageCur->SwitchActiveTenant(m_pTen);
  242.     g_fSwitchingActive=FALSE;
  243.  
  244.     //Hide the frame tools if necessary.
  245.     g_pFR->ShowUIAndTools(pDoc->NoObjectFrameTools(0, FALSE), FALSE);
  246.     return NOERROR;
  247.     }
  248.  
  249.  
  250.  
  251.  
  252. /*
  253.  * CImpIOleInPlaceSite::OnUIDeactivate
  254.  *
  255.  * Purpose:
  256.  *  Informs the container that the object is deactivating its
  257.  *  in-place user interface at which time the container may
  258.  *  reinstate its own.  Opposite of OnUIActivate.
  259.  *
  260.  * Parameters:
  261.  *  fUndoable       BOOL indicating if the object will actually
  262.  *                  perform an Undo if the container calls
  263.  *                  ReactivateAndUndo.
  264.  *
  265.  * Return Value:
  266.  *  HRESULT         NOERROR or an appropriate error code.
  267.  */
  268.  
  269. STDMETHODIMP CImpIOleInPlaceSite::OnUIDeactivate(BOOL fUndoable)
  270.     {
  271.     PCDocument  pDoc;
  272.     MSG         msg;
  273.  
  274.     /*
  275.      * Ignore this notification if we're switching between
  276.      * multiple active objects.
  277.      */
  278.     if (g_fSwitchingActive)
  279.         return NOERROR;
  280.  
  281.     //If in shutdown (NULL storage), don't check messages.
  282.     if (NULL==m_pTen->m_pIStorage)
  283.         {
  284.         g_pFR->ReinstateUI();
  285.         return NOERROR;
  286.         }
  287.  
  288.     pDoc=(PCDocument)SendMessage(GetParent(m_pTen->m_hWnd)
  289.         , DOCM_PDOCUMENT, 0, 0L);
  290.  
  291.     //If there's a pending double-click, delay showing our UI
  292.     if (!PeekMessage(&msg, pDoc->Window(), WM_LBUTTONDBLCLK
  293.         , WM_LBUTTONDBLCLK, PM_NOREMOVE | PM_NOYIELD))
  294.         {
  295.         //Turn everything back on.
  296.         g_pFR->ReinstateUI();
  297.         }
  298.     else
  299.         m_pTen->m_pPG->m_fAddUI=TRUE;
  300.  
  301.     SetFocus(pDoc->Window());
  302.     return NOERROR;
  303.     }
  304.  
  305.  
  306.  
  307.  
  308. /*
  309.  * CImpIOleInPlaceSite::DeactivateAndUndo
  310.  *
  311.  * Purpose:
  312.  *  If immediately after activation the object does an Undo, the
  313.  *  action being undone is the activation itself, and this call
  314.  *  informs the container that this is, in fact, what happened.
  315.  *  The container should call IOleInPlaceObject::UIDeactivate.
  316.  *
  317.  * Parameters:
  318.  *  None
  319.  *
  320.  * Return Value:
  321.  *  HRESULT         NOERROR or an appropriate error code.
  322.  */
  323.  
  324. STDMETHODIMP CImpIOleInPlaceSite::DeactivateAndUndo(void)
  325.     {
  326.     m_pTen->m_pIOleIPObject->InPlaceDeactivate();
  327.     return NOERROR;
  328.     }
  329.  
  330.  
  331.  
  332.  
  333. /*
  334.  * CImpIOleInPlaceSite::DiscardUndoState
  335.  *
  336.  * Purpose:
  337.  *  Informs the container that something happened in the object
  338.  *  that means the container should discard any undo information
  339.  *  it currently maintains for the object.
  340.  *
  341.  * Parameters:
  342.  *  None
  343.  *
  344.  * Return Value:
  345.  *  HRESULT         NOERROR or an appropriate error code.
  346.  */
  347.  
  348. STDMETHODIMP CImpIOleInPlaceSite::DiscardUndoState(void)
  349.     {
  350.     return ResultFromScode(E_NOTIMPL);
  351.     }
  352.  
  353.  
  354.  
  355.  
  356. /*
  357.  * CImpIOleInPlaceSite::GetWindowContext
  358.  *
  359.  * Purpose:
  360.  *  Provides an in-place object with pointers to the frame and
  361.  *  document level in-place interfaces (IOleInPlaceFrame and
  362.  *  IOleInPlaceUIWindow) such that the object can do border
  363.  *  negotiation and so forth.  Also requests the position and
  364.  *  clipping rectangles of the object in the container and a
  365.  *  pointer to an OLEINPLACEFRAME info structure which contains
  366.  *  accelerator information.
  367.  *
  368.  *  Note that the two interfaces this call returns are not
  369.  *  available through QueryInterface on IOleInPlaceSite since they
  370.  *  live with the frame and document, but not the site.
  371.  *
  372.  * Parameters:
  373.  *  ppIIPFrame      LPOLEINPLACEFRAME * in which to return the
  374.  *                  AddRef'd pointer to the container's
  375.  *                  IOleInPlaceFrame.
  376.  *  ppIIPUIWindow   LPOLEINPLACEUIWINDOW * in which to return
  377.  *                  the AddRef'd pointer to the container document's
  378.  *                  IOleInPlaceUIWindow.
  379.  *  prcPos          LPRECT in which to store the object's position.
  380.  *  prcClip         LPRECT in which to store the object's visible
  381.  *                  region.
  382.  *  pFI             LPOLEINPLACEFRAMEINFO to fill with accelerator
  383.  *                  stuff.
  384.  *
  385.  * Return Value:
  386.  *  HRESULT         NOERROR
  387.  */
  388.  
  389. STDMETHODIMP CImpIOleInPlaceSite::GetWindowContext
  390.     (LPOLEINPLACEFRAME *ppIIPFrame, LPOLEINPLACEUIWINDOW
  391.     *ppIIPUIWindow, LPRECT prcPos, LPRECT prcClip
  392.     , LPOLEINPLACEFRAMEINFO pFI)
  393.     {
  394.     PCPatronDoc     pDoc;
  395.     RECTL           rcl;
  396.  
  397.     *ppIIPUIWindow=NULL;
  398.  
  399.     *ppIIPFrame=(LPOLEINPLACEFRAME)g_pFR;
  400.     g_pFR->AddRef();
  401.  
  402.     pDoc=(PCPatronDoc)SendMessage(GetParent(m_pTen->m_hWnd)
  403.         , DOCM_PDOCUMENT, 0, 0L);
  404.  
  405.     if (NULL!=pDoc)
  406.         {
  407.         pDoc->QueryInterface(IID_IOleInPlaceUIWindow
  408.             , (PPVOID)ppIIPUIWindow);
  409.         }
  410.  
  411.     //Now get the rectangles and frame information.
  412.     m_pTen->RectGet(&rcl, TRUE);
  413.     RECTFROMRECTL(*prcPos, rcl);
  414.  
  415.     //Include scroll position here.
  416.     OffsetRect(prcPos, -(int)m_pTen->m_pPG->m_xPos
  417.         , -(int)m_pTen->m_pPG->m_yPos);
  418.  
  419.     SetRect(prcClip, 0, 0, 32767, 32767);
  420.  
  421.     pFI->cb=sizeof(OLEINPLACEFRAMEINFO);
  422.    #ifdef MDI
  423.     pFI->fMDIApp=TRUE;
  424.    #else
  425.     pFI->fMDIApp=FALSE;
  426.    #endif
  427.  
  428.     pFI->hwndFrame=g_pFR->Window();
  429.  
  430.     pFI->haccel=g_pFR->m_hAccelIP;
  431.     pFI->cAccelEntries=CINPLACEACCELERATORS;
  432.  
  433.     return NOERROR;
  434.     }
  435.  
  436.  
  437.  
  438.  
  439. /*
  440.  * CImpIOleInPlaceSite::Scroll
  441.  *
  442.  * Purpose:
  443.  *  Asks the container to scroll the document, and thus the object,
  444.  *  by the given amounts in the sz parameter.
  445.  *
  446.  * Parameters:
  447.  *  sz              SIZE containing signed horizontal and vertical
  448.  *                  extents by which the container should scroll.
  449.  *                  These are in device units.
  450.  *
  451.  * Return Value:
  452.  *  HRESULT         NOERROR
  453.  */
  454.  
  455. STDMETHODIMP CImpIOleInPlaceSite::Scroll(SIZE sz)
  456.     {
  457.     int         x, y;
  458.  
  459.     x=m_pTen->m_pPG->m_xPos+sz.cx;
  460.     y=m_pTen->m_pPG->m_yPos+sz.cy;
  461.  
  462.     SendScrollPosition(m_pTen->m_hWnd, WM_HSCROLL, x);
  463.     SendScrollPosition(m_pTen->m_hWnd, WM_VSCROLL, y);
  464.     return NOERROR;
  465.     }
  466.  
  467.  
  468.  
  469.  
  470. /*
  471.  * CImpIOleInPlaceSite::OnPosRectChange
  472.  *
  473.  * Purpose:
  474.  *  Informs the container that the in-place object was resized.
  475.  *  The container must call IOleInPlaceObject::SetObjectRects.
  476.  *  This does not change the site's rectangle in any case.
  477.  *
  478.  * Parameters:
  479.  *  prcPos          LPCRECT containing the new size of the object.
  480.  *
  481.  * Return Value:
  482.  *  HRESULT         NOERROR
  483.  */
  484.  
  485. STDMETHODIMP CImpIOleInPlaceSite::OnPosRectChange(LPCRECT prcPos)
  486.     {
  487.     if (NULL!=prcPos)
  488.         m_pTen->m_rcPos=*prcPos;
  489.  
  490.     m_pTen->UpdateInPlaceObjectRects(prcPos, FALSE);
  491.     return NOERROR;
  492.     }
  493.