home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK8 / MFC / SAMPLES / TESTCLNT / CLIPBD.CP$ / clipbd
Encoding:
Text File  |  1992-01-15  |  6.0 KB  |  301 lines

  1. // clipbd.cpp : This file contains all code which deals with the 
  2. //      clipboard.  It contains code which updates the
  3. //      menus on the clipboard contents and code which 
  4. //      copies, pastes, and paste links to/from the clipboard.
  5. //
  6. // This is a part of the Microsoft Foundation Classes C++ library.
  7. // Copyright (C) 1992 Microsoft Corporation
  8. // All rights reserved.
  9. //
  10. // This source code is only intended as a supplement to the
  11. // Microsoft Foundation Classes Reference and Microsoft
  12. // QuickHelp documentation provided with the library.
  13. // See these sources for detailed information regarding the
  14. // Microsoft Foundation Classes product.
  15.  
  16.  
  17. #include "testclnt.h"
  18. #include "resource.h"
  19. #include "defs.h"
  20.  
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. static CString GLOBAL_szProtocol = "StdFileEditing";
  24. static char szBuffer[80];
  25.  
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // OLE Paste Link
  29. //
  30. void CTestClient::OnPasteLink()
  31. {
  32.     ASSERT(m_pClient != NULL);
  33.     ASSERT(m_pItemTitle != NULL);
  34.  
  35.     if (!OpenClipboard())
  36.     {
  37.         MessageBox("Couldn't Open Clipboard", "Test Client", 
  38.             MB_ICONEXCLAMATION);
  39.         return;                    
  40.         // Couldn't open the clipboard 
  41.     }
  42.  
  43.     char szTmp[CBOBJNAMEMAX];
  44.  
  45.     CreateNewUniqueName(szTmp);
  46.  
  47.     // Here we deal with the case where there is an existing object
  48.     // on the board by deleting the current object
  49.  
  50.     if (m_pClient->m_lpObject != NULL)
  51.     {
  52.         TRY
  53.         {
  54.             m_pClient->Delete();
  55.         }
  56.         CATCH (COleException, e)
  57.         {
  58.             if (m_bLogging)
  59.             {
  60.                 LogError( THIS_FILE, e->m_status );
  61.             }
  62.             return;
  63.         }
  64.         END_CATCH
  65.     }
  66.  
  67.     TRY
  68.     {
  69.         if (!m_pClient->CreateLinkFromClipboard(szTmp))
  70.         {
  71.             // error pasting link
  72.             MessageBox("Clipboard Paste Link Failed","Test Client",
  73.                 MB_ICONEXCLAMATION);
  74.             // No delete is required here since the object is only  
  75.             // created on success
  76.             return;
  77.         }
  78.  
  79.         CloseClipboard();
  80.         if (!DrawObject())
  81.         {
  82.             MessageBox("Couldn't Draw Object!", "Test Client",
  83.                 MB_ICONEXCLAMATION);
  84.             m_pClient->Delete();        // Delete the object here
  85.         }
  86.     }
  87.     CATCH (COleException, e)
  88.     {
  89.         if (m_bLogging)
  90.         {
  91.             LogError( THIS_FILE, e->m_status );
  92.         }
  93.         CloseClipboard();
  94.         return;
  95.     }
  96.     END_CATCH
  97.  
  98.     
  99.  
  100.     // Set the information bars', object title
  101.     m_pItemTitle->SetWindowText(m_pClient->GetName());
  102.     m_bUntitled = TRUE;
  103.     m_bDirty = TRUE;
  104.     Invalidate(TRUE);
  105. }
  106.  
  107.  
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CTestClient OnPaste
  111. //
  112. void CTestClient::OnPaste()
  113. {
  114.     char        szTmp[CBOBJNAMEMAX];
  115.  
  116.     ASSERT(m_pClient != NULL);
  117.     ASSERT(m_pItemTitle != NULL);
  118.  
  119.     if (!OpenClipboard())
  120.     {
  121.         MessageBox("Couldn't Open Clipboard", "Test Client", 
  122.             MB_ICONEXCLAMATION);
  123.         return;                    
  124.         // Couldn't open the clipboard 
  125.     }
  126.  
  127.     CreateNewUniqueName(szTmp);
  128.  
  129.  
  130.     // if an object exists, delete it
  131.  
  132.     TRY
  133.     {
  134.         if (m_pClient->m_lpObject != NULL)
  135.         {
  136.             m_pClient->Delete();
  137.         }
  138.     }
  139.     CATCH (COleException, e)
  140.     {
  141.         if (m_bLogging)
  142.         {
  143.             LogError( THIS_FILE, e->m_status );
  144.         }
  145.         return;
  146.  
  147.     }
  148.     END_CATCH
  149.  
  150.  
  151.  
  152.     TRY
  153.     {
  154.         if (!m_pClient->CreateFromClipboard(szTmp)) 
  155.         {
  156.             // error in pasting object
  157.             MessageBox("Clipboard Paste Failed", "Test Client",
  158.                 MB_ICONEXCLAMATION);
  159.             // No delete is required since the object is created only
  160.             // on success, I think
  161.             return;
  162.         } 
  163.  
  164.         CloseClipboard();
  165.         if (!DrawObject())
  166.         {
  167.             MessageBox("Couldn't Draw Object!", "Test Client",
  168.                 MB_ICONEXCLAMATION);
  169.             m_pClient->Delete();        // Delete the object here
  170.         }
  171.     }
  172.     CATCH (COleException, e)
  173.     {
  174.         if (m_bLogging)
  175.         {
  176.             LogError( THIS_FILE, e->m_status );
  177.         }
  178.         CloseClipboard();
  179.         return;
  180.     }
  181.     END_CATCH
  182.  
  183.     // Set up the information bars' object title
  184.     m_pItemTitle->SetWindowText(m_pClient->GetName());
  185.     m_bUntitled = TRUE;
  186.     m_bDirty = TRUE;
  187.     Invalidate(TRUE);
  188. }
  189.  
  190.  
  191.  
  192. /////////////////////////////////////////////////////////////////////////////
  193. // CTestClient OnCopy
  194. //
  195. void CTestClient::OnCopy() 
  196. {
  197.     ASSERT(m_pClient != NULL);
  198.  
  199.     if (!OpenClipboard())
  200.     {
  201.         MessageBox("Couldn't Open Clipboard", "Test Client", 
  202.             MB_ICONEXCLAMATION);
  203.         return;
  204.     }
  205.  
  206.     EmptyClipboard();
  207.  
  208.     TRY
  209.     {
  210.         m_pClient->CopyToClipboard();
  211.     }
  212.     CATCH(COleException, e)
  213.     {
  214.         if (m_bLogging)
  215.         {
  216.             LogError( THIS_FILE, e->m_status );
  217.         }
  218.  
  219.         MessageBox("Object Not Copied To Clipboard", "Test Client",
  220.             MB_ICONEXCLAMATION);
  221.         CloseClipboard();
  222.         return;
  223.     }
  224.     END_CATCH
  225.  
  226.     CloseClipboard();
  227.     return;
  228. }
  229.  
  230.  
  231. /////////////////////////////////////////////////////////////////////////////
  232. // CTestClient OnDrawClipboard()
  233. //
  234. void CTestClient::OnDrawClipboard()
  235. {
  236.  
  237.     // Regenerate Listbox Entries for Clipboard Format
  238.     ASSERT (m_pClipFormat != NULL);
  239.  
  240.     if (!OpenClipboard())
  241.     {
  242.         return;
  243.     }
  244.     m_pClipFormat->ResetContent();
  245.     
  246.     WORD wCurrentFormat, wFormat;
  247.     wCurrentFormat = 0;
  248.  
  249.     int nFormats = CountClipboardFormats();
  250.     for (int i=0; i < nFormats; i++)
  251.     {
  252.         wFormat = EnumClipboardFormats(wCurrentFormat);
  253.         int nCnt = GetClipboardFormatName(wFormat, szBuffer, sizeof(szBuffer));
  254.         if (nCnt == 0)
  255.         {
  256.             if (wFormat & CF_OWNERDISPLAY)
  257.             {
  258.                 strcpy(szBuffer, szOwnerFormat[wFormat-CF_OWNERDISPLAY]);
  259.             }
  260.             else
  261.             {
  262.                 strcpy(szBuffer, szDefinedFormat[wFormat-CF_TEXT]);
  263.             }
  264.         }
  265.  
  266.         m_pClipFormat->AddString(szBuffer);
  267.         wCurrentFormat = wFormat;   
  268.     }
  269.     CloseClipboard();
  270.  
  271.  
  272.     if (m_hWndNextView != NULL)
  273.     {
  274.         ::SendMessage(m_hWndNextView, WM_DRAWCLIPBOARD,
  275.             (GetCurrentMessage())->wParam, (GetCurrentMessage())->lParam);
  276.     }
  277. }
  278.  
  279.  
  280.  
  281.  
  282. /////////////////////////////////////////////////////////////////////////////
  283. // CTestClient OnChangeCbChain(HWND hRemove, HWND hNew)
  284. //
  285. void CTestClient::OnChangeCbChain(HWND hRemove, HWND hNew)
  286. {
  287.     if (hRemove == m_hWndNextView)
  288.     {
  289.         m_hWndNextView = hNew;
  290.     }
  291.  
  292.     else if (m_hWndNextView != NULL)
  293.     {
  294.         ::SendMessage(m_hWndNextView, WM_CHANGECBCHAIN, 
  295.             (GetCurrentMessage())->wParam, (GetCurrentMessage())->lParam) ;
  296.     }
  297. }
  298.  
  299.  
  300.  
  301.