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 / page.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  42KB  |  1,739 lines

  1. /*
  2.  * PAGE.CPP
  3.  * Patron Chapter 22
  4.  *
  5.  * Implementation of parts of the CPage class; those member
  6.  * functions dealing with mouse events are in PAGEMOUS.CPP.
  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. #include "patron.h"
  17.  
  18.  
  19. /*
  20.  * CPage::CPage
  21.  * CPage::~CPage
  22.  *
  23.  * Constructor Parameters:
  24.  *  dwID            DWORD identifier for this page.
  25.  *  hWnd            HWND of the pages window (for repaints, etc).
  26.  *  pPG             PCPages to the Pages window.
  27.  */
  28.  
  29. CPage::CPage(DWORD dwID, HWND hWnd, PCPages pPG)
  30.     {
  31.     m_dwID     =dwID;
  32.     m_pIStorage=NULL;
  33.  
  34.     m_cOpens=0;
  35.     m_hWnd=hWnd;
  36.     m_pPG=pPG;
  37.  
  38.     m_dwIDNext      =0;
  39.     m_cTenants      =0;
  40.     m_hWndTenantList=NULL;
  41.     m_iTenantCur    =NOVALUE;   //Tenants are zero indexed.
  42.     m_pTenantCur    =NULL;
  43.  
  44.     m_uHTCode=HTNOWHERE;
  45.     m_uSizingFlags=0;
  46.     m_fTracking=FALSE;
  47.     m_hDC=NULL;
  48.  
  49.     m_fDragPending=FALSE;
  50.     m_fSizePending=FALSE;
  51.     m_fTimer=FALSE;
  52.  
  53.     //Get WIN.INI distance and delay values, with OLE defaults.
  54.     m_cxyDist=GetProfileInt(TEXT("windows"), TEXT("DragMinDist")
  55.         , DD_DEFDRAGMINDIST);
  56.     m_cDelay=GetProfileInt(TEXT("windows"), TEXT("DragDelay")
  57.         , DD_DEFDRAGDELAY);
  58.  
  59.     m_fReopen=FALSE;
  60.     m_pmkFile=m_pPG->m_pmkFile;
  61.  
  62.     if (NULL!=m_pmkFile)
  63.         m_pmkFile->AddRef();
  64.  
  65.     m_cRef=0L;
  66.     m_dwRegROTWild=0L;
  67.     m_pImpIOleItemContainer=NULL;
  68.  
  69.     //CHAPTER22MOD
  70.     m_fFirstUIActivate=TRUE;  //We haven't UIActivated anyone yet
  71.     //End CHAPTER22MOD
  72.     return;
  73.     }
  74.  
  75.  
  76. CPage::~CPage(void)
  77.     {
  78.     INOLE_RevokeAsRunning(&m_dwRegROTWild);
  79.  
  80.     if (m_fTimer)
  81.         KillTimer(m_hWnd, IDTIMER_DEBOUNCE);
  82.  
  83.     m_hWnd=NULL;
  84.     Close(FALSE);
  85.     return;
  86.     }
  87.  
  88.  
  89.  
  90. /*
  91.  * CPage::QueryInterface
  92.  * CPage::AddRef
  93.  * CPage::Release
  94.  *
  95.  * Purpose:
  96.  *  IUnknown members for CPage object.
  97.  */
  98.  
  99. STDMETHODIMP CPage::QueryInterface(REFIID riid, PPVOID ppv)
  100.     {
  101.     *ppv=NULL;
  102.  
  103.     if (IID_IUnknown==riid)
  104.         *ppv=this;
  105.  
  106.     if (IID_IOleItemContainer==riid || IID_IOleContainer==riid
  107.         || IID_IParseDisplayName==riid)
  108.         *ppv=m_pImpIOleItemContainer;
  109.  
  110.     if (NULL!=*ppv)
  111.         {
  112.         ((LPUNKNOWN)*ppv)->AddRef();
  113.         return NOERROR;
  114.         }
  115.  
  116.     return ResultFromScode(E_NOINTERFACE);
  117.     }
  118.  
  119.  
  120. STDMETHODIMP_(ULONG) CPage::AddRef(void)
  121.     {
  122.     return ++m_cRef;
  123.     }
  124.  
  125. STDMETHODIMP_(ULONG) CPage::Release(void)
  126.     {
  127.     if (0!=--m_cRef)
  128.         return m_cRef;
  129.  
  130.     delete this;
  131.     return 0;
  132.     }
  133.  
  134.  
  135.  
  136.  
  137.  
  138. /*
  139.  * CPage::GetID
  140.  *
  141.  * Return Value:
  142.  *  DWORD           dwID field in this page.
  143.  */
  144.  
  145. DWORD CPage::GetID(void)
  146.     {
  147.     return m_dwID;
  148.     }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /*
  155.  * CPage::Open
  156.  *
  157.  * Purpose:
  158.  *  Retrieves the IStorage associated with this page.  The IStorage
  159.  *  is owned by the page and thus the page always holds a reference
  160.  *  count.  The caller should call Close or delete this page to
  161.  *  match this open.
  162.  *
  163.  *  This function may be called multiple times resulting in
  164.  *  additional reference counts on the storage each of which must be
  165.  *  matched with a call to Close.  The last Close can be done
  166.  *  through delete.
  167.  *
  168.  * Parameters:
  169.  *  pIStorage       LPSTORAGE in which this page lives.
  170.  *
  171.  * Return Value:
  172.  *  BOOL            TRUE if opening succeeds, FALSE otherwise.
  173.  */
  174.  
  175. BOOL CPage::Open(LPSTORAGE pIStorage)
  176.     {
  177.     HRESULT                 hr=NOERROR;
  178.     LPSTREAM                pIStream;
  179.     DWORD                   dwMode;
  180.     OLECHAR                 szTemp[32];
  181.     TCHAR                   szCap[32];
  182.     BOOL                    fNew;
  183.     BOOL                    fCreated=FALSE;
  184.     TENANTLIST              tl;
  185.     PTENANTINFO             pti;
  186.     ULONG                   cb;
  187.     LPMALLOC                pIMalloc;
  188.     UINT                    i;
  189.     PCTenant                pTenant;
  190.     UINT                    cLinks;
  191.     LPOLELINK               pIOleLink;
  192.     LPUNKNOWN               pIUnknown;
  193.     UINT                    uRet;
  194.     OLEUIEDITLINKS          el;
  195.     PCIOleUILinkContainer   pIUILinks;
  196.     HWND                    hWndDoc;
  197.  
  198.     fNew=(NULL==m_pIStorage);
  199.  
  200.     if (!fNew)
  201.         {
  202.         m_cOpens++;
  203.         m_pIStorage->AddRef();
  204.         return TRUE;
  205.         }
  206.  
  207.     if (NULL==pIStorage)
  208.         return FALSE;
  209.  
  210.     /*
  211.      * Attempt to open the storage under this ID.  If none,
  212.      * create one.  In either case, the IStorage is either
  213.      * saved in pPage or released.
  214.      */
  215.  
  216.     GetStorageName(szTemp);
  217.     dwMode=STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  218.  
  219.     hr=pIStorage->OpenStorage(szTemp, NULL, dwMode, NULL, 0
  220.         , &m_pIStorage);
  221.  
  222.     if (FAILED(hr))
  223.         {
  224.         hr=pIStorage->CreateStorage(szTemp, dwMode, 0, 0
  225.             , &m_pIStorage);
  226.         fCreated=TRUE;
  227.         }
  228.  
  229.     if (FAILED(hr))
  230.         return FALSE;
  231.  
  232.     m_cOpens++;
  233.  
  234.     if (NULL==m_hWndTenantList)
  235.         {
  236.         /*
  237.          * The first time we open this page, create the hidden
  238.          * listbox we'll use to track tenants.  We give it the
  239.          * owner-draw style so we can just store pointers in it.
  240.          */
  241.         m_hWndTenantList=CreateWindow(TEXT("listbox")
  242.             , TEXT("Tenant List"), WS_POPUP | LBS_OWNERDRAWFIXED
  243.             , 0, 0, 100, 100, HWND_DESKTOP, NULL
  244.             , m_pPG->m_hInst, NULL);
  245.  
  246.         if (NULL==m_hWndTenantList)
  247.             return FALSE;
  248.         }
  249.  
  250.     m_pImpIOleItemContainer=new CImpIOleItemContainer(this, this
  251.         , FALSE);
  252.  
  253.     if (NULL==m_pImpIOleItemContainer)
  254.         return FALSE;
  255.  
  256.     //If this is brand-new, we're done.
  257.     if (fCreated)
  258.         return TRUE;
  259.  
  260.  
  261.     /*
  262.      * Now open the stream we saved in Close and load all the
  263.      * tenants listed in there.  If there are none, then we don't
  264.      * have to load squat.
  265.      */
  266.  
  267.     hr=m_pIStorage->OpenStream(SZSTREAMTENANTLIST, NULL, STGM_DIRECT
  268.         | STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pIStream);
  269.  
  270.     if (FAILED(hr))
  271.         return FALSE;
  272.  
  273.     if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
  274.         {
  275.         pIStream->Read(&tl, sizeof(tl), NULL);
  276.         m_cTenants=tl.cTenants;
  277.         m_dwIDNext=tl.dwIDNext;
  278.         m_iTenantCur=0;
  279.  
  280.         cb=tl.cTenants*sizeof(TENANTINFO);
  281.  
  282.         if (0!=cb)
  283.             {
  284.             pti=(PTENANTINFO)pIMalloc->Alloc(cb);
  285.  
  286.             if (NULL!=pti)
  287.                 {
  288.                 pIStream->Read(pti, cb, NULL);
  289.  
  290.                 for (i=0; i < m_cTenants; i++)
  291.                     {
  292.                     if (TenantAdd(NOVALUE, (pti+i)->dwID, &pTenant))
  293.                         {
  294.                         pTenant->Load(m_pIStorage, (pti+i));
  295.  
  296.                         //Make sure it knows about the show state.
  297.                         pTenant->ShowObjectType(m_pPG->m_fShowTypes);
  298.                         }
  299.                     }
  300.  
  301.                 pIMalloc->Free(pti);
  302.                 }
  303.             }
  304.  
  305.         pIMalloc->Release();
  306.         }
  307.  
  308.     pIStream->Release();
  309.  
  310.     //Get and select the first tenant
  311.     //CHAPTER22MOD
  312.     if (TenantGet(0, &m_pTenantCur, FALSE))
  313.         m_pTenantCur->Select(TRUE, TRUE);
  314.     //End CHAPTER22MOD
  315.  
  316.     //If we just saved and closed, don't bother with updating links
  317.     if (m_fReopen)
  318.         {
  319.         m_fReopen=FALSE;
  320.         return TRUE;
  321.         }
  322.  
  323.     /*
  324.      * Update all the links in this page, showing the progress
  325.      * indicator as it's happening.  We use the same
  326.      * IOlUILinkContainer implementation as we do for the links
  327.      * dialog, passing it to OleUIUpdateLinks which does everything
  328.      * for us.
  329.      *
  330.      * We might also optimize this to not do anything if there are
  331.      * no automatic links, but it's not a big concern.
  332.      */
  333.  
  334.     //First, count the number of automatic links.
  335.     cLinks=0;
  336.  
  337.     for (i=0; i < m_cTenants; i++)
  338.         {
  339.         if (TenantGet(i, &pTenant, FALSE))
  340.             {
  341.             DWORD       dw;
  342.  
  343.             pTenant->ObjectGet(&pIUnknown);
  344.             hr=pIUnknown->QueryInterface(IID_IOleLink
  345.                 , (PPVOID)&pIOleLink);
  346.             pIUnknown->Release();
  347.  
  348.             if (FAILED(hr))
  349.                 continue;
  350.  
  351.             pIOleLink->GetUpdateOptions(&dw);
  352.             pIOleLink->Release();
  353.  
  354.             if (OLEUPDATE_ALWAYS==dw)
  355.                 cLinks++;
  356.             }
  357.         }
  358.  
  359.     //If we have any automatic links, invoke the update dialog.
  360.     if (0==cLinks)
  361.         return TRUE;
  362.  
  363.     //Create an IOleUILinkContainer instantiation.
  364.     if (!m_pPG->GetUILinkContainer(&pIUILinks))
  365.         return TRUE;    //Guess we can't update, oh well.
  366.  
  367.     hWndDoc=GetParent(m_hWnd);
  368.     LoadString(m_pPG->m_hInst, IDS_CAPTION, szCap, sizeof(szCap));
  369.  
  370.     if (!OleUIUpdateLinks(pIUILinks, hWndDoc, szCap, cLinks))
  371.         {
  372.         /*
  373.          * If updating failed, ask to show the links dialog.  NOTE:
  374.          * OleUIPromptUser has a variable wsprintf argument list
  375.          * after the hWnd parameter!  Use appropriate typecasting!
  376.          */
  377.         uRet=OleUIPromptUser(IDD_CANNOTUPDATELINK, hWndDoc, szCap);
  378.  
  379.        #ifdef IDC_PU_LINKS
  380.         if (IDC_PU_LINKS==uRet)
  381.        #else
  382.         if (ID_PU_LINKS==uRet)
  383.        #endif
  384.             {
  385.             //Throw up the links dialog.
  386.             memset(&el, 0, sizeof(el));
  387.             el.cbStruct=sizeof(el);
  388.             el.hWndOwner=hWndDoc;
  389.             el.lpOleUILinkContainer=pIUILinks;
  390.             OleUIEditLinks(&el);
  391.             }
  392.         }
  393.  
  394.     m_pPG->m_fDirty=pIUILinks->m_fDirty;
  395.     pIUILinks->Release();
  396.     return TRUE;
  397.     }
  398.  
  399.  
  400.  
  401.  
  402.  
  403. /*
  404.  * CPage::Close
  405.  *
  406.  * Purpose:
  407.  *  Possibly commits the storage, then releases it reversing the
  408.  *  reference count from Open.
  409.  *
  410.  * Parameters:
  411.  *  fCommit         BOOL indicating if we're to commit.
  412.  *
  413.  * Return Value:
  414.  *  None
  415.  */
  416.  
  417. void CPage::Close(BOOL fCommit)
  418.     {
  419.     if (NULL==m_pIStorage)
  420.         return;
  421.  
  422.     if (fCommit)
  423.         Update();
  424.  
  425.     m_pIStorage->Release();
  426.  
  427.     //If this was the last close, make all tenants loaded->passive
  428.     if (0==--m_cOpens)
  429.         {
  430.         UINT        i;
  431.         PCTenant    pTenant;
  432.  
  433.         m_pIStorage=NULL;
  434.  
  435.         for (i=0; i < m_cTenants; i++)
  436.             {
  437.             if (TenantGet(i, &pTenant, FALSE))
  438.                 {
  439.                 if (NULL!=m_hWnd)
  440.                     {
  441.                     //Open may select again, so this repaints.
  442.                     //CHAPTER22MOD
  443.                     pTenant->Select(FALSE, TRUE);
  444.                     //End CHAPTER22MOD
  445.                     }
  446.  
  447.                 pTenant->Close(FALSE);
  448.                 pTenant->Release();
  449.                 }
  450.             }
  451.  
  452.         DestroyWindow(m_hWndTenantList);
  453.         m_hWndTenantList=NULL;
  454.         m_fReopen=TRUE;
  455.  
  456.         if (NULL!=m_pmkFile)
  457.             m_pmkFile->Release();
  458.  
  459.         m_pmkFile=NULL;
  460.  
  461.         DeleteInterfaceImp(m_pImpIOleItemContainer);
  462.         }
  463.  
  464.     return;
  465.     }
  466.  
  467.  
  468.  
  469.  
  470. /*
  471.  * CPage::Update
  472.  *
  473.  * Purpose:
  474.  *  Forces a common on the page if it's open.
  475.  *
  476.  * Parameters:
  477.  *  None
  478.  *
  479.  * Return Value:
  480.  *  BOOL            TRUE if there are any open objects on this page,
  481.  *                  that is, we should remain open.
  482.  */
  483.  
  484. BOOL CPage::Update(void)
  485.     {
  486.     BOOL            fOpen=FALSE;
  487.     UINT            i;
  488.     PCTenant        pTenant;
  489.     HRESULT         hr;
  490.     LPSTREAM        pIStream;
  491.     TENANTLIST      tl;
  492.     PTENANTINFO     pti;
  493.     ULONG           cb;
  494.     LPMALLOC        pIMalloc;
  495.  
  496.     //Walk the list of objects and update them all as well.
  497.     for (i=0; i < m_cTenants; i++)
  498.         {
  499.         if (TenantGet(i, &pTenant, FALSE))
  500.             fOpen |= pTenant->Update();
  501.         }
  502.  
  503.     //Now write our own stream containing the tenant list.
  504.     hr=m_pIStorage->CreateStream(SZSTREAMTENANTLIST, STGM_CREATE
  505.         | STGM_WRITE| STGM_DIRECT | STGM_SHARE_EXCLUSIVE, 0, 0
  506.         , &pIStream);
  507.  
  508.     if (FAILED(hr))
  509.         return fOpen;
  510.  
  511.     if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
  512.         {
  513.         tl.cTenants=m_cTenants;
  514.         tl.dwIDNext=m_dwIDNext;
  515.  
  516.         pIStream->Write(&tl, sizeof(TENANTLIST), &cb);
  517.  
  518.         cb=m_cTenants*sizeof(TENANTINFO);
  519.         pti=(PTENANTINFO)pIMalloc->Alloc(cb);
  520.  
  521.         if (NULL!=pti)
  522.             {
  523.             for (i=0; i < m_cTenants; i++)
  524.                 {
  525.                 TenantGet(i, &pTenant, FALSE);
  526.                 pTenant->GetInfo((pti+i));
  527.                 }
  528.  
  529.             pIStream->Write(pti, cb, &cb);
  530.             pIMalloc->Free(pti);
  531.             }
  532.  
  533.         pIMalloc->Release();
  534.         }
  535.  
  536.     pIStream->Release();
  537.  
  538.     //Now commit the whole mess and we're done
  539.     if (NULL!=m_pIStorage)
  540.         m_pIStorage->Commit(STGC_DEFAULT);
  541.  
  542.     return fOpen;
  543.     }
  544.  
  545.  
  546.  
  547.  
  548.  
  549. /*
  550.  * CPage::Destroy
  551.  *
  552.  * Purpose:
  553.  *  Removes this page from the given storage.  The caller should
  554.  *  eventually delete this Page object to free the storage.
  555.  *
  556.  * Parameters:
  557.  *  pIStorage       LPSTORAGE contianing this page on which to call
  558.  *                  DestroyElement
  559.  *
  560.  * Return Value:
  561.  *  None
  562.  */
  563.  
  564. void CPage::Destroy(LPSTORAGE pIStorage)
  565.     {
  566.     if (NULL!=pIStorage)
  567.         {
  568.         OLECHAR szTemp[32];
  569.  
  570.         Close(FALSE);
  571.         GetStorageName(szTemp);
  572.         pIStorage->DestroyElement(szTemp);
  573.         }
  574.  
  575.     return;
  576.     }
  577.  
  578.  
  579.  
  580.  
  581. /*
  582.  * CPage::GetStorageName
  583.  *
  584.  * Parameters:
  585.  *  pszName         LPOLESTR to a buffer in which to store the
  586.  *                  storage name for this page.
  587.  *
  588.  * Return Value:
  589.  *  UINT            Number of characters stored.
  590.  */
  591.  
  592. UINT CPage::GetStorageName(LPOLESTR pszName)
  593.     {
  594.    #ifdef WIN32ANSI
  595.     char        szTemp[32];
  596.     UINT        cch;
  597.  
  598.     cch=wsprintf(szTemp, "Page %lu", m_dwID);
  599.     MultiByteToWideChar(CP_ACP, 0, szTemp, -1, pszName, 32);
  600.     return cch;
  601.    #else
  602.     return wsprintf(pszName, TEXT("Page %lu"), m_dwID);
  603.    #endif
  604.     }
  605.  
  606.  
  607.  
  608.  
  609. /*
  610.  * CPage::Draw
  611.  *
  612.  * Purpose:
  613.  *  Draws the objects on this page to the given hDC.
  614.  *
  615.  * Parameters:
  616.  *  hDC             HDC on which to draw.
  617.  *  xOff, yOff      int offsets for the page.
  618.  *  fNoColor        BOOL indicating black & white screen rendering.
  619.  *  fPrinter        BOOL indicating hDC is on the printer.
  620.  *
  621.  * Return Value:
  622.  *  None
  623.  */
  624.  
  625. void CPage::Draw(HDC hDC, int xOff, int yOff, BOOL fNoColor
  626.     , BOOL fPrinter)
  627.     {
  628.     int                 i;
  629.     PCTenant            pTenant;
  630.     HDC                 hIC=NULL;
  631.     PCOMBINEDEVICE      pcd=NULL;
  632.     DVTARGETDEVICE     *ptd=NULL;
  633.  
  634.     /*
  635.      * If printing, tell the tenant to forget the borders. Otherwise
  636.      * we leave xOff and yOff the same to account for scrolling.
  637.      */
  638.     if (fPrinter)
  639.         {
  640.         xOff=LOMETRIC_BORDER+m_pPG->m_xMarginLeft;
  641.         yOff=-LOMETRIC_BORDER-m_pPG->m_yMarginTop;
  642.  
  643.         /*
  644.          * Get device information.  If this fails, ptd is
  645.          * NULL which is acceptable.
  646.          */
  647.         if (m_pPG->DevReadConfig(&pcd, &hIC))
  648.             ptd=&(pcd->td);
  649.         }
  650.  
  651.     for (i=(int)m_cTenants-1; i >=0; i--)
  652.         {
  653.         if (TenantGet(i, &pTenant, FALSE))
  654.             {
  655.             RECT        rc, rcWin;
  656.             RECTL       rcl;
  657.  
  658.             //Paint this tenant only if visible.
  659.             pTenant->RectGet(&rcl, TRUE);
  660.             RECTFROMRECTL(rc, rcl);
  661.             OffsetRect(&rc, -(int)m_pPG->m_xPos
  662.                 , -(int)m_pPG->m_yPos);
  663.             GetClientRect(m_hWnd, &rcWin);
  664.  
  665.             if (IntersectRect(&rc, &rc, &rcWin))
  666.                 {
  667.                 pTenant->Draw(hDC, ptd, hIC, xOff, yOff
  668.                     , fNoColor, fPrinter);
  669.                 }
  670.             }
  671.         }
  672.  
  673.     //Free whatever CPages::DevReadConfig returned.
  674.     if (NULL!=pcd)
  675.         {
  676.         LPMALLOC    pIMalloc;
  677.  
  678.         if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
  679.             {
  680.             pIMalloc->Free(pcd);
  681.             pIMalloc->Release();
  682.             }
  683.         }
  684.  
  685.     if (NULL!=hIC)
  686.         DeleteDC(hIC);
  687.  
  688.     return;
  689.     }
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696. /*
  697.  * CPage::TenantCreate
  698.  *
  699.  * Purpose:
  700.  *  Creates a new tenant of a specific type.
  701.  *
  702.  * Parameters:
  703.  *  tType           TENANTTYPE to create.
  704.  *  pv              LPVOID providing information for the new
  705.  *                  object creation.
  706.  *  pFE             LPFORMATETC describing how we want this
  707.  *                  rendered.
  708.  *  ppo             PPATRONOBJECT with placement data.
  709.  *  dwData          DWORD extra data to pass to the tenant.
  710.  *
  711.  * Return Value:
  712.  *  None
  713.  */
  714.  
  715. BOOL CPage::TenantCreate(TENANTTYPE tType, LPVOID pv
  716.     , LPFORMATETC pFE, PPATRONOBJECT ppo, DWORD dwData)
  717.     {
  718.     PCTenant    pTenant;
  719.     UINT        uRet;
  720.     int         x, y;
  721.     int         h, v;
  722.     POINTL      ptl;
  723.     SIZEL       szl;
  724.     RECTL       rcl;
  725.     RECT        rc;
  726.  
  727.     //New tenants go at top of the pile; zero index to TenantAdd.
  728.     if (!TenantAdd(0, m_dwIDNext, &pTenant))
  729.         return FALSE;
  730.  
  731.     uRet=pTenant->Create(tType, pv, pFE, &ptl, &szl, m_pIStorage
  732.         , ppo, dwData);
  733.  
  734.     if (CREATE_FAILED==uRet)
  735.         {
  736.         //Reverse Create AND TenantAdd
  737.         SendMessage(m_hWndTenantList, LB_DELETESTRING, 0, 0L);
  738.         pTenant->Destroy(m_pIStorage);
  739.  
  740.         pTenant->Release();
  741.         return FALSE;
  742.         }
  743.  
  744.     m_dwIDNext++;
  745.     m_cTenants++;
  746.  
  747.     //CHAPTER22MOD
  748.     if (NULL!=m_pTenantCur)
  749.         m_pTenantCur->Select(FALSE, TRUE);
  750.     //End CHAPTER22MOD
  751.  
  752.     m_iTenantCur=0;             //First one in the list now.
  753.     m_pTenantCur=pTenant;
  754.  
  755.     //Tell the tenant where it lives, default is (0,0) in print area
  756.     x=LOMETRIC_BORDER+m_pPG->m_xMarginLeft;
  757.     y=-LOMETRIC_BORDER-m_pPG->m_yMarginTop;
  758.  
  759.     h=x;
  760.     v=y;
  761.  
  762.     if (CREATE_PLACEDOBJECT==uRet)
  763.         {
  764.         SetRect(&rc, 3*CXYHANDLE, 3*CXYHANDLE, 0, 0);
  765.         RectConvertMappings(&rc, NULL, FALSE);
  766.  
  767.         //Make sure place point is on page, otherwise go to (0,0)
  768.         if (((int)ptl.x > x)
  769.             && ((int)ptl.x < x+(int)m_pPG->m_cx-rc.left))
  770.             x=(int)ptl.x;
  771.  
  772.         //m_pPG->m_cy is absolute
  773.         if (((int)ptl.y < y)
  774.             && ((int)ptl.y > y-(int)m_pPG->m_cy-rc.top))
  775.             y=(int)ptl.y;
  776.         }
  777.  
  778.     //Bounds check size of the object and fit to page as necessary.
  779.     if (x+(int)szl.cx > (int)(h+m_pPG->m_cx))
  780.         szl.cx=h+m_pPG->m_cx-x;
  781.  
  782.     //Remember that szl we get from Create is absolute
  783.     if (y-(int)szl.cy < (int)(v-m_pPG->m_cy))
  784.         szl.cy=-(int)(v-m_pPG->m_cy-y);
  785.  
  786.     SETRECTL(rcl, x, y, x+szl.cx, y-szl.cy);
  787.     m_pTenantCur->RectSet(&rcl, FALSE, TRUE);
  788.  
  789.     //Force a repaint on this new guy
  790.     m_pTenantCur->Invalidate();
  791.     UpdateWindow(m_hWnd);
  792.  
  793.     //CHAPTER22MOD
  794.     m_pTenantCur->Select(TRUE, TRUE);
  795.     //End CHAPTER22MOD
  796.  
  797.     //Make sure this new tenant knows about showing it's type.
  798.     m_pTenantCur->ShowObjectType(m_pPG->m_fShowTypes);
  799.  
  800.     //New tenants must know the available monikers
  801.     if (NULL!=m_pmkFile)
  802.         {
  803.         LPBC        pbc;
  804.         LPMONIKER   pmkPage;
  805.         TCHAR       szTemp[32];
  806.         LPTSTR      pszFile;
  807.  
  808.        #ifdef WIN32ANSI
  809.         OLECHAR     szW[32];
  810.  
  811.         GetStorageName(szW);
  812.         WideCharToMultiByte(CP_ACP, 0, szW, -1, szTemp, 32
  813.            , NULL, NULL);
  814.        #else
  815.         GetStorageName(szTemp);
  816.        #endif
  817.         CreateItemMoniker(TEXT("!"), szTemp, &pmkPage);
  818.  
  819.         //We can get the filename from our file moniker
  820.         CreateBindCtx(0, &pbc);
  821.        #ifdef WIN32ANSI
  822.         LPOLESTR    pszTemp;
  823.         m_pmkFile->GetDisplayName(pbc, NULL, &pszTemp);
  824.         pszFile=(LPTSTR)CoTaskMemAlloc(512);
  825.         WideCharToMultiByte(CP_ACP, 0, pszTemp, -1, pszFile
  826.            , 512, NULL, NULL);
  827.         CoTaskMemFree((void *)pszTemp);
  828.        #else
  829.         m_pmkFile->GetDisplayName(pbc, NULL, &pszFile);
  830.        #endif
  831.         pbc->Release();
  832.  
  833.         pTenant->NotifyOfRename(pszFile, m_pmkFile, pmkPage);
  834.         pmkPage->Release();
  835.         CoTaskMemFree((void *)pszFile);
  836.         }
  837.  
  838.     //Activate new objects immediately and force a save on them
  839.     if (TENANTTYPE_EMBEDDEDOBJECT==tType)
  840.         {
  841.         //CHAPTER22MOD
  842.         m_pTenantCur->Activate(OLEIVERB_SHOW, NULL);
  843.         //End CHAPTER22MOD
  844.         m_pTenantCur->Update();
  845.         }
  846.  
  847.     return TRUE;
  848.     }
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855. /*
  856.  * CPage::TenantDestroy
  857.  *
  858.  * Purpose:
  859.  *  Destroys the currently selected tenant on this page.
  860.  *
  861.  * Parameters:
  862.  *  None
  863.  *
  864.  * Return Value:
  865.  *  None
  866.  */
  867.  
  868. BOOL CPage::TenantDestroy(void)
  869.     {
  870.     if (NULL==m_pTenantCur)
  871.         {
  872.         MessageBeep(0);
  873.         return FALSE;
  874.         }
  875.  
  876.     SendMessage(m_hWndTenantList, LB_DELETESTRING
  877.         , m_iTenantCur, 0L);
  878.  
  879.     m_pTenantCur->Invalidate();
  880.     m_pTenantCur->Destroy(m_pIStorage);
  881.     m_pTenantCur->Release();
  882.     m_pTenantCur=NULL;
  883.  
  884.     //Update counts, etc., and select the next tenant in the list.
  885.     if (m_iTenantCur==m_cTenants-1)
  886.         m_iTenantCur--;
  887.  
  888.     if (0==--m_cTenants)
  889.         m_pTenantCur=NULL;
  890.     else
  891.         {
  892.         TenantGet(m_iTenantCur, &m_pTenantCur, TRUE);
  893.  
  894.         //CHAPTER22MOD
  895.         m_pTenantCur->Select(TRUE, TRUE);
  896.         //End CHAPTER22MOD
  897.         }
  898.  
  899.     UpdateWindow(m_hWnd);
  900.     return TRUE;
  901.     }
  902.  
  903.  
  904.  
  905.  
  906.  
  907. /*
  908.  * CPage::TenantClip
  909.  *
  910.  * Purpose:
  911.  *  Copies or cuts the currently selected tenant to the clipoard.
  912.  *
  913.  * Parameters:
  914.  *  fCut            BOOL TRUE to cut the object, FALSE to copy.
  915.  *
  916.  * Return Value:
  917.  *  BOOL            TRUE if successful, FALSE otherwise.
  918.  */
  919.  
  920. BOOL CPage::TenantClip(BOOL fCut)
  921.     {
  922.     LPDATAOBJECT    pIDataObject;
  923.     BOOL            fRet=FALSE;
  924.  
  925.     if (NULL==m_pTenantCur)
  926.         return FALSE;
  927.  
  928.     /*
  929.      * To perform a data transfer operation, we need to create a
  930.      * data object with the selected object's data inside. To do
  931.      * this we CoCreateInstance on CLSID_DataTransferObject
  932.      * (Also implemented in this chapter), retrieve data from the
  933.      * object we have, stuff that data into the transfer object,
  934.      * then stick that object on the clipboard.
  935.      *
  936.      * Since we'll want an identical object at other times, like for
  937.      * drag-drop, we use a private function to actually create it.
  938.      */
  939.  
  940.     pIDataObject=TransferObjectCreate(NULL);
  941.  
  942.     if (NULL!=pIDataObject)
  943.         {
  944.         if (SUCCEEDED(OleSetClipboard(pIDataObject)))
  945.             {
  946.             if (fCut)
  947.                 TenantDestroy();
  948.  
  949.             fRet=TRUE;
  950.             }
  951.  
  952.         pIDataObject->Release();
  953.         }
  954.  
  955.     return fRet;
  956.     }
  957.  
  958.  
  959.  
  960.  
  961.  
  962. /*
  963.  * CPage::FQueryObjectSelected
  964.  *
  965.  * Purpose:
  966.  *  Returns whether or not there is an object selected on this
  967.  *  page for Cut, Copy, Delete functions.
  968.  *
  969.  * Parameters:
  970.  *  hMenu           HMENU of the Edit menu.
  971.  *
  972.  * Return Value:
  973.  *  BOOL            TRUE if we have an object, FALSE otherwise.
  974.  */
  975.  
  976. BOOL CPage::FQueryObjectSelected(HMENU hMenu)
  977.     {
  978.     HMENU       hMenuTemp;
  979.  
  980.     /*
  981.      * This will only be called on WM_INITMENUPOPUP, we'll also
  982.      * use this function to create the Verb menu for this object.
  983.      */
  984.     if (NULL!=m_pTenantCur)
  985.         {
  986.         m_pTenantCur->AddVerbMenu(hMenu, MENUPOS_OBJECT);
  987.         return TRUE;
  988.         }
  989.  
  990.     OleUIAddVerbMenu(NULL, NULL, hMenu, MENUPOS_OBJECT
  991.         , IDM_VERBMIN, IDM_VERBMAX, FALSE, 0, &hMenuTemp);
  992.  
  993.     return FALSE;
  994.     }
  995.  
  996.  
  997.  
  998.  
  999. //CHAPTER22MOD
  1000. /*
  1001.  * CPage::ActivateObject
  1002.  *
  1003.  * Purpose:
  1004.  *  Executes a verb on the currently selected object.
  1005.  *
  1006.  * Parameters:
  1007.  *  iVerb           LONG of the selected verb.
  1008.  *  pMSG            LPMSG that caused the invocation of the verb.
  1009.  *
  1010.  * Return Value:
  1011.  *  None
  1012.  */
  1013.  
  1014. void CPage::ActivateObject(LONG iVerb, LPMSG pMSG)
  1015.     {
  1016.     if (NULL==m_pTenantCur)
  1017.         return;
  1018.  
  1019.     m_pTenantCur->Activate(iVerb, pMSG);
  1020.     return;
  1021.     }
  1022. //End CHAPTER22MOD
  1023.  
  1024.  
  1025.  
  1026.  
  1027. /*
  1028.  * CPage::ShowObjectTypes
  1029.  *
  1030.  * Purpose:
  1031.  *  Loops through all the tenants and tells each one to turn on or
  1032.  *  off the Show Objects features.
  1033.  *
  1034.  * Parameters:
  1035.  *  fShow           BOOL indicating to show the type or not.
  1036.  *
  1037.  * Return Value:
  1038.  *  None
  1039.  */
  1040.  
  1041. void CPage::ShowObjectTypes(BOOL fShow)
  1042.     {
  1043.     PCTenant    pTenant;
  1044.     UINT        i;
  1045.  
  1046.     for (i=0; i < m_cTenants; i++)
  1047.         {
  1048.         if (TenantGet(i, &pTenant, FALSE))
  1049.             pTenant->ShowObjectType(fShow);
  1050.         }
  1051.  
  1052.     return;
  1053.     }
  1054.  
  1055.  
  1056.  
  1057.  
  1058. /*
  1059.  * CPage::NotifyTenantsOfRename
  1060.  *
  1061.  * Purpose:
  1062.  *  Loops through all the tenants and informs each of the new
  1063.  *  document name.
  1064.  *
  1065.  * Parameters:
  1066.  *  pszFile         LPTSTR of the new filename.
  1067.  *  pmk             LPMONKIER for the new filename.
  1068.  *
  1069.  * Return Value:
  1070.  *  None
  1071.  */
  1072.  
  1073. void CPage::NotifyTenantsOfRename(LPTSTR pszFile, LPMONIKER pmk)
  1074.     {
  1075.     PCTenant    pTenant;
  1076.     UINT        i;
  1077.     LPMONIKER   pmkPage;
  1078.     LPMONIKER   pmkAll;
  1079.     TCHAR       szTemp[32];
  1080.  
  1081.     //Save the file moniker
  1082.     ReleaseInterface(m_pmkFile);
  1083.     m_pmkFile=pmk;
  1084.     m_pmkFile->AddRef();
  1085.  
  1086.     //Create a page moniker to send to the tenants.
  1087.    #ifdef WIN32ANSI
  1088.     OLECHAR     szW[32];
  1089.  
  1090.     GetStorageName(szW);
  1091.     WideCharToMultiByte(CP_ACP, 0, szW, -1, szTemp, 32
  1092.        , NULL, NULL);
  1093.    #else
  1094.     GetStorageName(szTemp);
  1095.    #endif
  1096.     CreateItemMoniker(TEXT("!"), szTemp, &pmkPage);
  1097.  
  1098.     for (i=0; i < m_cTenants; i++)
  1099.         {
  1100.         if (TenantGet(i, &pTenant, FALSE))
  1101.             pTenant->NotifyOfRename(pszFile, pmk, pmkPage);
  1102.         }
  1103.  
  1104.     /*
  1105.      * Register a File!Page!"\" wildcard moniker as well.
  1106.      * Note that the page is already marked as running
  1107.      * with the document's wildcard moniker.
  1108.      */
  1109.     CreateItemMoniker(TEXT("!"), TEXT("\\"), &pmkAll);
  1110.  
  1111.     if (NULL!=pmkAll)
  1112.         {
  1113.         LPMONIKER   pmkWild=NULL;
  1114.         LPMONIKER   pmkTemp=NULL;
  1115.  
  1116.         INOLE_RevokeAsRunning(&m_dwRegROTWild);
  1117.         pmk->ComposeWith(pmkPage, FALSE, &pmkTemp);
  1118.  
  1119.         if (NULL!=pmkTemp)
  1120.             {
  1121.             pmkTemp->ComposeWith(pmkAll, FALSE, &pmkWild);
  1122.             pmkTemp->Release();
  1123.             }
  1124.  
  1125.         if (NULL!=pmkWild)
  1126.             {
  1127.             INOLE_RegisterAsRunning(this, pmk, 0
  1128.                 , &m_dwRegROTWild);
  1129.             pmkWild->Release();
  1130.             }
  1131.  
  1132.         pmkAll->Release();
  1133.         }
  1134.  
  1135.     //If anything held onto this, they AddRef'd
  1136.     pmkPage->Release();
  1137.     return;
  1138.     }
  1139.  
  1140.  
  1141. /*
  1142.  * CPage::ConvertObject
  1143.  *
  1144.  * Purpose:
  1145.  *  Invokes and handles the results of the Convert dialog
  1146.  *
  1147.  * Parameters:
  1148.  *  hWndFrame       HWND to use as the parent of the dialog.
  1149.  *  fNoServer       BOOL indicating if this was called because
  1150.  *                  ActivateObject failed.
  1151.  *
  1152.  * Return Value:
  1153.  *  None
  1154.  */
  1155.  
  1156. BOOL CPage::ConvertObject(HWND hWndFrame, BOOL fNoServer)
  1157.     {
  1158.     HRESULT         hr;
  1159.     OLEUICONVERT    ct;
  1160.     TENANTTYPE      tType;
  1161.     TENANTINFO      ti;
  1162.     UINT            uRet;
  1163.     HCURSOR         hCur;
  1164.     BOOL            fActivate=fNoServer;
  1165.     SIZEL           szl;
  1166.  
  1167.     if (NULL==m_pTenantCur)
  1168.         return FALSE;
  1169.  
  1170.     tType=m_pTenantCur->TypeGet();
  1171.  
  1172.     if (TENANTTYPE_STATIC==tType)
  1173.         {
  1174.         MessageBeep(0);
  1175.         return FALSE;
  1176.         }
  1177.  
  1178.     //Get object information we may want.
  1179.     m_pTenantCur->GetInfo(&ti);
  1180.  
  1181.     //Fill the structure.
  1182.     memset(&ct, 0, sizeof(ct));
  1183.     ct.cbStruct=sizeof(OLEUICONVERT);
  1184.     ct.hWndOwner=hWndFrame;
  1185.     ct.fIsLinkedObject=(TENANTTYPE_LINKEDOBJECT==tType);
  1186.     ct.dvAspect=ti.fe.dwAspect;
  1187.  
  1188.     m_pTenantCur->ObjectClassFormatAndIcon(&ct.clsid, &ct.wFormat
  1189.         , &ct.lpszUserType, &ct.hMetaPict, &ct.lpszDefLabel);
  1190.  
  1191.     uRet=OleUIConvert(&ct);
  1192.  
  1193.     if (OLEUI_OK==uRet)
  1194.         {
  1195.         //Potentially a long operation.
  1196.         hCur=SetCursor(LoadCursor(NULL, IDC_WAIT));
  1197.  
  1198.         //Prevent multiple repaints.
  1199.         m_pTenantCur->EnableRepaint(FALSE);
  1200.  
  1201.         //First, let's bother with the iconic aspect switch.
  1202.         if ((DVASPECT_ICON==ct.dvAspect && ct.fObjectsIconChanged)
  1203.             || ct.dvAspect!=ti.fe.dwAspect)
  1204.             {
  1205.             HGLOBAL     hMem=NULL;
  1206.  
  1207.             //Only pass non-NULL handle for icon aspects.
  1208.             if (DVASPECT_ICON==ct.dvAspect)
  1209.                 hMem=ct.hMetaPict;
  1210.  
  1211.             m_pPG->m_fDirty=m_pTenantCur->SwitchOrUpdateAspect(hMem
  1212.                 , FALSE);
  1213.             }
  1214.  
  1215.         //Now change types around.
  1216.         if ((CF_SELECTCONVERTTO & ct.dwFlags)
  1217.             && ct.clsid!=ct.clsidNew)
  1218.             {
  1219.             LPSTORAGE   pIStorage;
  1220.  
  1221.             /*
  1222.              * User selected convert, so:
  1223.              *  1.  Unload the object (back to passive state)
  1224.              *  2.  Call INOLE_DoConvert, which calls WriteClassStg,
  1225.              *      WriteFmtUserTypeStg, and SetConvertStg.
  1226.              *  3.  Reload the object and force an update.
  1227.              */
  1228.  
  1229.             //This should be the only close necessary.
  1230.             m_pTenantCur->StorageGet(&pIStorage);
  1231.             m_pTenantCur->Close(TRUE);
  1232.  
  1233.             hr=INOLE_DoConvert(pIStorage, ct.clsidNew);
  1234.  
  1235.             //Need to commit the new type and format
  1236.             pIStorage->Commit(STGC_DEFAULT);
  1237.             pIStorage->Release();
  1238.  
  1239.             if (SUCCEEDED(hr))
  1240.                 {
  1241.                 LPUNKNOWN   pObj;
  1242.                 LPOLEOBJECT pIOleObject;
  1243.  
  1244.                 //Reload and update.
  1245.                 m_pTenantCur->Load(m_pIStorage, &ti);
  1246.  
  1247.                 m_pTenantCur->ObjectGet(&pObj);
  1248.                 pObj->QueryInterface(IID_IOleObject
  1249.                     , (PPVOID)&pIOleObject);
  1250.                 pIOleObject->Update();
  1251.                 pIOleObject->Release();
  1252.                 pObj->Release();
  1253.                 }
  1254.  
  1255.             m_pPG->m_fDirty=TRUE;
  1256.             }
  1257.  
  1258.  
  1259.         if (CF_SELECTACTIVATEAS & ct.dwFlags)
  1260.             {
  1261.             /*
  1262.              * User selected Activate As, so:
  1263.              *  1.  Add the TreatAs entry in the registry
  1264.              *      through CoTreatAsClass
  1265.              *  2.  Unload all objects of the old CLSID that you
  1266.              *      have loaded.
  1267.              *  3.  Reload objects as desired
  1268.              *  4.  Activate the current object.
  1269.              */
  1270.  
  1271.             hr=CoTreatAsClass(ct.clsid, ct.clsidNew);
  1272.  
  1273.             if (SUCCEEDED(hr))
  1274.                 {
  1275.                 PCTenant    pTenant;
  1276.                 UINT        i;
  1277.  
  1278.                 for (i=0; i < m_cTenants; i++)
  1279.                     {
  1280.                     if (TenantGet(i, &pTenant, FALSE))
  1281.                         {
  1282.                         pTenant->GetInfo(&ti);
  1283.                         pTenant->Close(FALSE);
  1284.                         pTenant->Load(m_pIStorage, &ti);
  1285.                         }
  1286.                     }
  1287.  
  1288.                 fActivate=TRUE;
  1289.                 }
  1290.             }
  1291.  
  1292.         //These two steps insure the object knows of the size.
  1293.         m_pTenantCur->SizeGet(&szl, FALSE);
  1294.         m_pTenantCur->SizeSet(&szl, FALSE, TRUE);
  1295.  
  1296.         m_pTenantCur->EnableRepaint(TRUE);
  1297.         m_pTenantCur->Repaint();
  1298.  
  1299.         if (fActivate)
  1300.             //CHAPTER22MOD
  1301.             m_pTenantCur->Activate(OLEIVERB_SHOW, NULL);
  1302.             //End CHAPTER22MOD
  1303.  
  1304.         SetCursor(hCur);
  1305.         }
  1306.  
  1307.     CoTaskMemFree((void*)ct.lpszUserType);
  1308.     INOLE_MetafilePictIconFree(ct.hMetaPict);
  1309.  
  1310.     return TRUE;
  1311.     }
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319. /*
  1320.  * CPages::FQueryLinksInPage
  1321.  *
  1322.  * Purpose:
  1323.  *  Pass through to current page to see if there are any linked
  1324.  *  objects.
  1325.  *
  1326.  * Parameters:
  1327.  *  None
  1328.  *
  1329.  * Return Value:
  1330.  *  None
  1331.  */
  1332.  
  1333. BOOL CPage::FQueryLinksInPage()
  1334.     {
  1335.     PCTenant    pTenant;
  1336.     UINT        i;
  1337.     BOOL        fRet=FALSE;
  1338.  
  1339.     for (i=0; i < m_cTenants; i++)
  1340.         {
  1341.         if (TenantGet(i, &pTenant, FALSE))
  1342.             fRet |= (pTenant->TypeGet()==TENANTTYPE_LINKEDOBJECT);
  1343.         }
  1344.  
  1345.     return fRet;
  1346.     }
  1347.  
  1348.  
  1349.  
  1350.  
  1351. //CHAPTER22MOD
  1352. /*
  1353.  * CPage::ScrolledWindow
  1354.  *
  1355.  * Purpose:
  1356.  *  Instructs the page to call CTenant::UpdateInPlaceObjectRects
  1357.  *  for the current tenant when the window is scrolled.
  1358.  *
  1359.  * Parameters:
  1360.  *  None
  1361.  *
  1362.  * Return Value:
  1363.  *  None
  1364.  */
  1365.  
  1366. void CPage::ScrolledWindow(void)
  1367.     {
  1368.     UINT        i;
  1369.     PCTenant    pTenant;
  1370.  
  1371.     /*
  1372.      * Tell all tenants to update the position of in-place objects.
  1373.      * If you do not support inside-out, only the selected object
  1374.      * need be notified.
  1375.      */
  1376.     for (i=0; i < m_cTenants; i++)
  1377.         {
  1378.         if (!TenantGet(i, &pTenant, FALSE))
  1379.             continue;
  1380.  
  1381.         pTenant->UpdateInPlaceObjectRects(NULL, TRUE);
  1382.         }
  1383.  
  1384.     return;
  1385.     }
  1386.  
  1387.  
  1388. /*
  1389.  * CPage::SwitchActiveTenant
  1390.  *
  1391.  * Purpose:
  1392.  *  Changes the activation of in-place objects from the current
  1393.  *  one known to the page to the new one passed to this function.
  1394.  *  This is called from IOleInPlaceSite::OnUIDeactivate.
  1395.  *
  1396.  * Parameters:
  1397.  *  pTenant         PCTenant that is becoming active.
  1398.  *
  1399.  * Return Value:
  1400.  *  None
  1401.  */
  1402.  
  1403. void CPage::SwitchActiveTenant(PCTenant pTenant)
  1404.     {
  1405.     BOOL        fSelect;
  1406.  
  1407.     /*
  1408.      * If we're UI activating the selected tenant, don't
  1409.      * bother changing selection--just so activation.
  1410.      */
  1411.     fSelect=(pTenant!=m_pTenantCur);
  1412.  
  1413.     /*
  1414.      * The first time we UI Activate we're not switching
  1415.      * anything so avoid the whole sequence.
  1416.      */
  1417.     if (m_fFirstUIActivate)
  1418.         {
  1419.         m_fFirstUIActivate=FALSE;
  1420.         return;
  1421.         }
  1422.  
  1423.     if (NULL!=m_pTenantCur && fSelect)
  1424.         m_pTenantCur->Select(FALSE, TRUE);
  1425.  
  1426.     m_pTenantCur=pTenant;
  1427.  
  1428.     //Select the new tenant, but don't activate it again
  1429.     if (NULL!=m_pTenantCur)
  1430.         {
  1431.         UINT        i;
  1432.  
  1433.         if (fSelect)
  1434.             m_pTenantCur->Select(TRUE, FALSE);
  1435.  
  1436.         //Find the new tenant in our list and move to the top
  1437.         for (i=0; i < m_cTenants; i++)
  1438.             {
  1439.             PCTenant        pTenList;
  1440.  
  1441.             if (TenantGet(i, &pTenList, FALSE))
  1442.                 {
  1443.                 if (pTenList==m_pTenantCur)
  1444.                     {
  1445.                     HWND        hWndObj;
  1446.  
  1447.                     m_iTenantCur=0;
  1448.  
  1449.                     //Remove the tenant and add to the top again.
  1450.                     SendMessage(m_hWndTenantList, LB_DELETESTRING
  1451.                         , i, 0L);
  1452.                     SendMessage(m_hWndTenantList, LB_INSERTSTRING
  1453.                         , 0, (LONG)pTenant);
  1454.  
  1455.                     hWndObj=pTenant->ObjectWindow();
  1456.  
  1457.                     if (NULL!=hWndObj)
  1458.                         {
  1459.                         SetWindowPos(hWndObj, HWND_TOP, 0, 0, 0, 0
  1460.                             , SWP_NOMOVE | SWP_NOSIZE
  1461.                             | SWP_NOACTIVATE);
  1462.                         }
  1463.                     break;
  1464.                     }
  1465.                 }
  1466.             }
  1467.         }
  1468.  
  1469.     return;
  1470.     }
  1471. //End CHAPTER22MOD
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478. /*
  1479.  * CPage::TenantGet
  1480.  * (Protected)
  1481.  *
  1482.  * Purpose:
  1483.  *  Returns a tenant of a given index returning a BOOL so it's
  1484.  *  simple to use this function inside if statements.
  1485.  *
  1486.  * Parameters:
  1487.  *  iTenant         UINT tenant to retrieve 0 based.
  1488.  *  ppTenant        PCPage * in which to return the tenant
  1489.  *                  pointer
  1490.  *  fOpen           BOOL indicating if we should open this
  1491.  *                  tenant as well.
  1492.  *
  1493.  * Return Value:
  1494.  *  BOOL            TRUE if successful, FALSE otherwise.
  1495.  */
  1496.  
  1497. BOOL CPage::TenantGet(UINT iTenant, PCTenant *ppTenant
  1498.     , BOOL fOpen)
  1499.     {
  1500.     if (NULL==ppTenant)
  1501.         return FALSE;
  1502.  
  1503.     if (LB_ERR!=SendMessage(m_hWndTenantList, LB_GETTEXT
  1504.         , iTenant, (LONG)ppTenant))
  1505.         {
  1506.         if (fOpen)
  1507.             (*ppTenant)->Open(m_pIStorage);
  1508.  
  1509.         return TRUE;
  1510.         }
  1511.  
  1512.     return FALSE;
  1513.     }
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520. /*
  1521.  * CPage::TenantGetFromID
  1522.  * (Protected)
  1523.  *
  1524.  * Purpose:
  1525.  *  Finds a tenant pointer from an ID for use from
  1526.  *  IOleItemContainer::GetObject
  1527.  *
  1528.  * Parameters:
  1529.  *  dwID            DWORD identifier of the tenant to find.
  1530.  *  ppTenant        PCTenant * in which to return the tenant
  1531.  *                  pointer
  1532.  *  fOpen           BOOL indicating if we should open this tenant as
  1533.  *                  well.
  1534.  *
  1535.  * Return Value:
  1536.  *  BOOL            TRUE if successful, FALSE otherwise.
  1537.  */
  1538.  
  1539. BOOL CPage::TenantGetFromID(DWORD dwID, PCTenant *ppTenant
  1540.     , BOOL fOpen)
  1541.     {
  1542.     UINT        i;
  1543.     PCTenant    pTenant;
  1544.  
  1545.     if (NULL==ppTenant)
  1546.         return FALSE;
  1547.  
  1548.     for (i=0; i < m_cTenants; i++)
  1549.         {
  1550.         if (!TenantGet(i, &pTenant, FALSE))
  1551.             continue;
  1552.  
  1553.         if (pTenant->GetID()==dwID)
  1554.             {
  1555.             if (fOpen)
  1556.                 pTenant->Open(m_pIStorage);
  1557.  
  1558.             *ppTenant=pTenant;
  1559.             return TRUE;
  1560.             }
  1561.         }
  1562.  
  1563.     return FALSE;
  1564.     }
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570. /*
  1571.  * CPage::TenantAdd
  1572.  * (Protected)
  1573.  *
  1574.  * Purpose:
  1575.  *  Creates a new tenant initialized to the given values.  The new
  1576.  *  tenants's storage is created if it does not already exist.  If
  1577.  *  fOpenStorage is set the the tenants's storage is opened and left
  1578.  *  opened.
  1579.  *
  1580.  * Parameters:
  1581.  *  iTenant         UINT Location at which to insert tenant; new
  1582.  *                  tenant is inserted after this position.  NOVALUE
  1583.  *                  for the end.
  1584.  *  dwID            DWORD ID for this tenant.
  1585.  *  ppNew           PCTenant * in which to store the new tenant.
  1586.  *
  1587.  * Return Value:
  1588.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  1589.  */
  1590.  
  1591. BOOL CPage::TenantAdd(UINT iTenant, DWORD dwID
  1592.     , PCTenant *ppNew)
  1593.     {
  1594.     PCTenant    pTenant;
  1595.     LRESULT     lr;
  1596.  
  1597.     if (NULL!=ppNew)
  1598.         *ppNew=NULL;
  1599.  
  1600.     pTenant=new CTenant(dwID, m_hWnd, m_pPG);
  1601.  
  1602.     if (NULL==pTenant)
  1603.         return FALSE;
  1604.  
  1605.     //The constructor doesn't AddRef, so we need to.
  1606.     pTenant->AddRef();
  1607.  
  1608.     //Now try to add to the listbox.
  1609.     lr=SendMessage(m_hWndTenantList, LB_INSERTSTRING, iTenant
  1610.         , (LONG)pTenant);
  1611.  
  1612.     if (lr < 0)
  1613.         {
  1614.         pTenant->Release();
  1615.         return FALSE;
  1616.         }
  1617.  
  1618.     *ppNew=pTenant;
  1619.     return TRUE;
  1620.     }
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626. /*
  1627.  * CPage::TransferObjectCreate
  1628.  * (Protected)
  1629.  *
  1630.  * Purpose:
  1631.  *  Creates a DataTransferObject and stuff the current selection's
  1632.  *  data into it.
  1633.  *
  1634.  * Parameters:
  1635.  *  pptl            PPOINTL containing the pick point in device
  1636.  *                  units applicable only to drag-drop; since
  1637.  *                  drag-drop is inherently mouse oriented, we use
  1638.  *                  device units for the point.  Ignored if NULL.
  1639.  *
  1640.  * Return Value:
  1641.  *  LPDATAOBJECT    Pointer to the object created, NULL on failure
  1642.  */
  1643.  
  1644. LPDATAOBJECT CPage::TransferObjectCreate(PPOINTL pptl)
  1645.     {
  1646.     LPDATAOBJECT    pIDataObject;
  1647.     LPDATAOBJECT    pIDataT;
  1648.     PPATRONOBJECT   ppo;
  1649.     RECTL           rcl;
  1650.     LPUNKNOWN       pObj;
  1651.     FORMATETC       fe;
  1652.     STGMEDIUM       stm;
  1653.     HRESULT         hr;
  1654.     HGLOBAL         hMem;
  1655.  
  1656.     m_pTenantCur->ObjectGet(&pObj);
  1657.  
  1658.     hr=CoCreateInstance(CLSID_DataTransferObject, NULL
  1659.         , CLSCTX_INPROC_SERVER, IID_IDataObject
  1660.         , (PPVOID)&pIDataObject);
  1661.  
  1662.     if (FAILED(hr))
  1663.         return NULL;
  1664.  
  1665.     //Go get the data we should hold on to.
  1666.     hr=pObj->QueryInterface(IID_IDataObject, (PPVOID)&pIDataT);
  1667.  
  1668.     if (FAILED(hr))
  1669.         {
  1670.         pIDataObject->Release();
  1671.         pObj->Release();
  1672.         return NULL;
  1673.         }
  1674.  
  1675.     //Copy from known obj into transfer obj.  Ordering is important!
  1676.  
  1677.     //Generate placeable object structure
  1678.     stm.tymed=TYMED_HGLOBAL;
  1679.     stm.pUnkForRelease=NULL;
  1680.     stm.hGlobal=GlobalAlloc(GHND, sizeof(PATRONOBJECT));
  1681.  
  1682.     if (NULL==stm.hGlobal)
  1683.         {
  1684.         pIDataObject->Release();
  1685.         pObj->Release();
  1686.         return NULL;
  1687.         }
  1688.  
  1689.     ppo=(PPATRONOBJECT)GlobalLock(stm.hGlobal);
  1690.  
  1691.     m_pTenantCur->SizeGet(&ppo->szl, FALSE);
  1692.     ppo->szl.cy=-ppo->szl.cy; //Negate to make absolute size
  1693.  
  1694.     m_pTenantCur->RectGet(&rcl, FALSE);
  1695.     ppo->ptl.x=rcl.left;
  1696.     ppo->ptl.y=rcl.top;
  1697.  
  1698.     if (NULL==pptl)
  1699.         {
  1700.         ppo->ptlPick.x=0;
  1701.         ppo->ptlPick.y=0;
  1702.         }
  1703.     else
  1704.         ppo->ptlPick=*pptl;
  1705.  
  1706.     m_pTenantCur->FormatEtcGet(&ppo->fe, FALSE);
  1707.  
  1708.     //If this is a linked object, just copy a presentation
  1709.     if (TENANTTYPE_LINKEDOBJECT==m_pTenantCur->TypeGet())
  1710.         m_pTenantCur->FormatEtcGet(&ppo->fe, TRUE);
  1711.  
  1712.     SETDefFormatEtc(fe, m_pPG->m_cf, TYMED_HGLOBAL);
  1713.     pIDataObject->SetData(&fe, &stm, TRUE);
  1714.  
  1715.     /*
  1716.      * Here now we have to include CFSTR_EMBEDDEDOBJECT and
  1717.      * CFSTR_OBJECTDESCRIPTOR when what we have selected is, in
  1718.      * fact, a compound document object.  We'll just ask the tenant
  1719.      * to set these in pIDataObject since it knows what the object.
  1720.      * If we copy embedded object data, make sure the PATRONOBJECT
  1721.      * structure has the right format in it as well.
  1722.      */
  1723.     m_pTenantCur->CopyEmbeddedObject(pIDataObject, &ppo->fe, pptl);
  1724.     hMem=stm.hGlobal;
  1725.  
  1726.     //Copy the actual presentation.
  1727.     m_pTenantCur->FormatEtcGet(&fe, TRUE);
  1728.     pIDataT->GetData(&fe, &stm);
  1729.     pIDataObject->SetData(&fe, &stm, TRUE);
  1730.  
  1731.     //Copy a link to this tenant if it's embedded
  1732.     m_pTenantCur->CopyLinkedObject(pIDataObject, &ppo->fe, pptl);
  1733.     GlobalUnlock(hMem); //ppo
  1734.  
  1735.     pIDataT->Release();
  1736.     pObj->Release();
  1737.     return pIDataObject;    //Caller now responsible
  1738.     }
  1739.