home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c031 / 8.ddi / MFC / SAMPLES / TESTCLNT / TCOBJECT.CP$ / tcobject
Encoding:
Text File  |  1992-03-18  |  6.4 KB  |  318 lines

  1. // tcobject.cpp : This file contains code which deal with the respective
  2. //      object functions.  The draw routine is listed here as
  3. //      is the InsertNewObject, the edit, and play routines.
  4. //
  5. // This is a part of the Microsoft Foundation Classes C++ library.
  6. // Copyright (C) 1992 Microsoft Corporation
  7. // All rights reserved.
  8. //
  9. // This source code is only intended as a supplement to the
  10. // Microsoft Foundation Classes Reference and Microsoft
  11. // QuickHelp documentation provided with the library.
  12. // See these sources for detailed information regarding the
  13. // Microsoft Foundation Classes product.
  14.  
  15.  
  16. #include "resource.h"
  17. #include "testclnt.h"
  18. #include "defs.h"
  19.  
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22.  
  23. static char szPrefix[] = "Test Client # ";
  24. static int GLOBAL_nCurrentNumber;
  25. extern CStdioFile *pOleDump;
  26. static char szBuffer[80];
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CTestClient OnInsertObject()
  30. //
  31. void CTestClient::OnInsertObject()
  32. {
  33.     char szTmp[CBOBJNAMEMAX];
  34.     CString className;
  35.  
  36.     ASSERT(m_pClient != NULL);
  37.     ASSERT(m_pItemTitle != NULL);
  38.  
  39.     if (!AfxOleInsertDialog(className))
  40.     {
  41.         if (m_bLogging)
  42.         {
  43.             ASSERT(pOleDump != NULL);
  44.             wsprintf(szBuffer,
  45.                 "CTestClient::OnInsertObject:  No Server Selected\n");
  46.             pOleDump->WriteString(szBuffer);
  47.         }
  48.         return;
  49.     }
  50.  
  51.     TRY
  52.     {
  53.         if (m_pClient->m_lpObject != NULL)
  54.         {
  55.             m_pClient->Delete();
  56.         }
  57.  
  58.         if (!m_pClient->CreateNewObject(className, 
  59.             CreateNewUniqueName(szTmp)))
  60.         {
  61.             MessageBox("Failed to create object", "Test Client",
  62.                 MB_ICONEXCLAMATION);
  63.             return;
  64.         }
  65.             
  66.         if(!DrawObject())
  67.         {
  68.             MessageBox("Couldn't Draw Object!", "Test Client",
  69.                 MB_ICONEXCLAMATION);
  70.             m_pClient->Delete();        // Delete the object here
  71.         }
  72.     }
  73.     CATCH (COleException, e)
  74.     {
  75.         if (m_bLogging)
  76.         {
  77.             LogError (THIS_FILE, e->m_status);
  78.         }
  79.         return;
  80.     }
  81.     END_CATCH
  82.  
  83.     // Set up Information bar's object title
  84.     m_pItemTitle->SetWindowText(m_pClient->GetName());
  85.     m_bUntitled = TRUE;
  86.     m_bDirty = TRUE;
  87.     Invalidate(TRUE);
  88. }
  89.  
  90.  
  91.  
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CTestClient::FixObjectBounds(CRect& rect)
  95. //
  96. void CTestClient::FixObjectBounds(CRect& rect)
  97. {
  98.     CRect clientRect;
  99.     
  100.     GetClientRect(&clientRect);
  101.  
  102.     /* If we have an empty rectangle, start at default size */
  103.     if (rect.IsRectEmpty())
  104.     {
  105.         rect.SetRect(0, 0, clientRect.right, clientRect.bottom);
  106.     }
  107.     else
  108.     {
  109.         // First map from HIMETRIC back to screen coordinates
  110.         {
  111.             CClientDC screenDC(NULL);
  112.  
  113.             short oldMode = screenDC.SetMapMode(MM_HIMETRIC);
  114.             screenDC.LPtoDP(&rect);
  115.             screenDC.SetMapMode(oldMode);
  116.         }
  117.  
  118.         /* Preserve the Aspect Ratio of the picture */
  119.         DWORD xDiff = (DWORD) (rect.right - rect.left + 1);
  120.         DWORD yDiff = (DWORD) (rect.bottom - rect.top + 1);
  121.  
  122.         DWORD dwXDiff = (clientRect.right - clientRect.left + 1);
  123.         DWORD dwYDiff = (clientRect.bottom - clientRect.top + 1);
  124.  
  125.         /* Don't use *= here because of integer arithmetic */
  126.         if (xDiff >  dwXDiff || yDiff > dwYDiff)
  127.         {
  128.             if ((xDiff * dwYDiff) > (yDiff * dwXDiff))
  129.             {
  130.                 yDiff = ((yDiff * dwXDiff) / xDiff);
  131.                 xDiff = dwXDiff;
  132.             }
  133.             else
  134.             {
  135.                 xDiff = ((xDiff * dwYDiff) / yDiff);
  136.                 yDiff = dwYDiff;
  137.             }
  138.         }
  139.         rect.SetRect(0, 0, (int)xDiff - 1, (int)yDiff - 1);
  140.     }
  141. }
  142.  
  143.  
  144.  
  145. LPSTR CreateNewUniqueName(LPSTR lpstr)
  146. {
  147.     wsprintf(lpstr, "%s%04d", (LPCSTR)szPrefix, GLOBAL_nCurrentNumber++);
  148.     return(lpstr);
  149. }
  150.  
  151.  
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // CTestClient DrawObject()
  155. //
  156. BOOL CTestClient::DrawObject()
  157. {
  158.     CRect   rectBounds;
  159.     BOOL bNotBlank;
  160.     
  161.     ASSERT(m_pClient != NULL);
  162.  
  163.     TRY
  164.     {
  165.         if (!(bNotBlank = m_pClient->GetBounds(&rectBounds)))
  166.         {
  167.             rectBounds.SetRectEmpty();
  168.         }
  169.     }
  170.     CATCH (COleException, e)
  171.     {
  172.         if (m_bLogging)
  173.         {
  174.             LogError (THIS_FILE, e->m_status);
  175.         }
  176.         return FALSE;
  177.     }
  178.     END_CATCH
  179.     
  180.     FixObjectBounds(rectBounds);
  181.  
  182.     rectBounds.OffsetRect(2 * GetSystemMetrics(SM_CXFRAME),
  183.                     2 * GetSystemMetrics(SM_CYFRAME));
  184.  
  185.  
  186.     TRY
  187.     {
  188.         if (bNotBlank)
  189.         {
  190.             if (m_pClient->GetType() == OT_EMBEDDED)
  191.             {
  192.                 CString s = (m_pClient->GetName());
  193.                 m_pClient->SetHostNames(AfxGetAppName(), s);
  194.                 CDC *pDC = GetDC();
  195.                 PrepareDC(pDC);
  196.                 if (!m_pClient->Draw(pDC, rectBounds, NULL, pDC))
  197.                 {
  198.                     MessageBox("Draw Problem with Embedded Item", 
  199.                         "Test Client", MB_ICONEXCLAMATION);
  200.                     ReleaseDC(pDC);
  201.                     return FALSE;
  202.                 }
  203.                 ReleaseDC(pDC);
  204.             }
  205.             
  206.             /* If the object is a link... */
  207.             if (m_pClient->GetType() == OT_LINK)
  208.             {
  209.                 CDC *pDC = GetDC();
  210.                 PrepareDC(pDC);
  211.                 if (!m_pClient->Draw(pDC, rectBounds, NULL, pDC))
  212.                 {
  213.                     MessageBox("Draw Problem with Linked Item", 
  214.                         "Test Client", MB_ICONEXCLAMATION);
  215.                     ReleaseDC(pDC);
  216.                     return FALSE;
  217.                 }
  218.                 ReleaseDC(pDC);
  219.             }
  220.         }
  221.     }
  222.     CATCH (COleException, e)
  223.     {
  224.         if (m_bLogging)
  225.         {
  226.             LogError (THIS_FILE, e->m_status);
  227.         }
  228.         return FALSE;
  229.     }
  230.     END_CATCH
  231.     
  232.     // Regenerate Objects Clipboard Formats
  233.     
  234.     if (m_pObjectFormat == NULL)
  235.     {
  236.         return TRUE;
  237.     }
  238.  
  239.     m_pObjectFormat->ResetContent();
  240.  
  241.     OLECLIPFORMAT wCurrentFormat, wFormat;
  242.  
  243.     wCurrentFormat = 0;
  244.  
  245.     while ((wFormat = m_pClient->EnumFormats(wCurrentFormat)) != 0)
  246.     {
  247.         int nCnt = GetClipboardFormatName(wFormat, szBuffer, sizeof(szBuffer));
  248.  
  249.         if (nCnt == 0)
  250.         {
  251.             if (wFormat & CF_OWNERDISPLAY)
  252.             {
  253.                 strcpy(szBuffer, szOwnerFormat[wFormat-CF_OWNERDISPLAY]);
  254.             }
  255.             else
  256.             {
  257.                 strcpy(szBuffer, szDefinedFormat[wFormat-CF_TEXT]);
  258.             }
  259.         }
  260.         m_pObjectFormat->AddString(szBuffer);
  261.         wCurrentFormat = wFormat;
  262.     }
  263.  
  264.     return TRUE;
  265. }
  266.  
  267.  
  268.  
  269. /////////////////////////////////////////////////////////////////////////////
  270. // CTestClient OnEdit()
  271. //
  272. void CTestClient::DoVerb(UINT nVerb)
  273. {
  274.  
  275.     ASSERT (m_pClient != NULL);
  276.  
  277.     TRY
  278.     {
  279.         if (m_pClient->GetType() == OT_STATIC)
  280.         {
  281.             return;
  282.         }
  283.     }
  284.     CATCH (COleException, e)
  285.     {
  286.         if (m_bLogging)
  287.         {
  288.             LogError (THIS_FILE, e->m_status);
  289.         }
  290.         return;
  291.     }
  292.     END_CATCH
  293.  
  294.     CRect   rect;
  295.     GetClientRect(&rect);
  296.  
  297.     TRY
  298.     {
  299.         m_pClient->Activate(nVerb, TRUE, TRUE, this, &rect);
  300.     }
  301.     CATCH (COleException, e)
  302.     {
  303.         // error when activating
  304.         MessageBox("Error Activating Server to Edit", "Test Client",
  305.             MB_ICONEXCLAMATION);
  306.         if (m_bLogging)
  307.         {
  308.             LogError (THIS_FILE, e->m_status);
  309.         }
  310.         return;
  311.     }
  312.     END_CATCH
  313.     Invalidate(TRUE);
  314. }
  315.  
  316.  
  317.  
  318.