home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK11 / MFC / SAMPLES / TESTSERV / TSOBJECT.CP$ / tsobject
Encoding:
Text File  |  1992-03-16  |  6.7 KB  |  313 lines

  1. // tsobject.cpp : This file contains code which implements the pure
  2. //      virtual functions for CServItem which inherits from COleServerItem
  3. //
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and Microsoft
  10. // QuickHelp documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "testserv.h"
  15. #include "resource.h"
  16.  
  17. LPSTR CreateNewUniqueName(LPSTR lpstr);
  18. extern void OutputLog(const char* pText);
  19. static char szPrefix[] = "Test Server # ";
  20. static int GLOBAL_nCurrentNumber;
  21.  
  22.  
  23. IMPLEMENT_SERIAL(CServItem,COleServerItem,0)
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // Edit Object dialog
  27. CEditObjectDlg::CEditObjectDlg(CWnd* pWnd, CString stringText)
  28.             : CModalDialog(IDDT_EDIT, pWnd)
  29. {
  30.     if (!stringText.IsEmpty())
  31.     {
  32.         m_Text = stringText;
  33.     }
  34. }
  35.  
  36. BOOL CEditObjectDlg::OnInitDialog()
  37. {
  38.     pWnd = (CEdit *)GetDlgItem(IDC_EDIT);
  39.     pWnd->SetSel(-1);       // Select entire box
  40.     pWnd->ReplaceSel(m_Text);
  41.     pWnd->SetFocus();
  42.     return TRUE;
  43. }
  44.  
  45.  
  46.  
  47. void CEditObjectDlg::OnOK()
  48. {
  49.     char szBuf[40];
  50.     pWnd = (CEdit*)GetDlgItem(IDC_EDIT);
  51.     pWnd->GetWindowText(szBuf, sizeof(szBuf)-1);
  52.     m_Text = szBuf;
  53.     EndDialog(IDOK);
  54. }
  55.  
  56.  
  57.  
  58. void CEditObjectDlg::OnCancel()
  59. {
  60.     EndDialog(IDCANCEL);
  61. }
  62.  
  63.  
  64.  
  65. BEGIN_MESSAGE_MAP(CEditObjectDlg, CModalDialog)
  66.     ON_COMMAND(IDC_OK, OnOK)
  67.     ON_COMMAND(IDC_CANCEL, OnCancel)
  68. END_MESSAGE_MAP()
  69.  
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CServItem::default constructor
  73. //
  74. CServItem::CServItem()
  75. {
  76.     m_pOwner = NULL;
  77. }
  78.  
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CServItem::CServItem (CTestServer* pDoc, CServDoc* pDoc, LPOLECLIENT 
  82. //          lpOleClient, LPCSTR lpszItemName, 
  83. //          LPCSTR lpszItemName)
  84. //
  85. CServItem::CServItem(CTestServer *pTest, CServDoc* pDoc,
  86.             LPCSTR lpszDocName, LPCSTR lpszItemName) 
  87. {
  88.     char szBuffer[80];
  89.     ASSERT(pTest != NULL);
  90.     ASSERT(pDoc != NULL);
  91.     
  92.     m_DocName = lpszDocName;
  93.     m_ItemName = lpszItemName;
  94.  
  95.     if (pTest->m_bLogging)
  96.     {
  97.         wsprintf(szBuffer, 
  98.             "Creating new CServItem with Parameters:  %x \t %x\n",
  99.                 pTest, pDoc);
  100.         OutputLog(szBuffer);
  101.     }
  102.  
  103.     m_pOwner = pTest;   // used to display window when embedded
  104.     m_Text = "";
  105.     m_pOwner->m_bDirty = TRUE;
  106.     m_pOwner->m_pItem = this;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. /////////////////////////////////////////////////////////////////////////////
  113. // CServItem::Serialize(CArchive &ar)
  114. //
  115. void CServItem::Serialize(CArchive &ar)
  116. {
  117.     if (ar.IsStoring())
  118.     {
  119.         ar << m_Text;
  120.     }
  121.     else
  122.     {
  123.         TRACE("Now loading the data\n");
  124.         ar >> m_Text;
  125.         char szBuf[80];
  126.         m_ItemName = CreateNewUniqueName(szBuf);
  127.     }
  128. }
  129.  
  130.  
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CServItem::OnShow (BOOL bTakeFocus)
  134. //
  135. OLESTATUS CServItem::OnShow (BOOL bTakeFocus)
  136. {
  137.     // if we are to take the focus, then we should bring up an edit
  138.     // control with the text.  We assume here that we will be edited.
  139.     // Otherwise we just draw the text with a particular rectangle
  140.  
  141.     m_pOwner->ShowWindow(SW_SHOWNORMAL);
  142.  
  143.     if (bTakeFocus != 0)
  144.     {
  145.         // Bring up Edit Box
  146.         CEditObjectDlg editObject(NULL, m_Text);
  147.  
  148.         if (editObject.DoModal() == IDOK)
  149.         {
  150.             m_Text = editObject.m_Text;
  151.         }
  152.  
  153.         m_pOwner->m_pList->ResetContent();
  154.         m_pOwner->m_pList->AddString(m_Text);
  155.         m_pOwner->Invalidate(TRUE);
  156.         m_pOwner->m_bDirty = TRUE;
  157.         char szBuf[80];
  158.         m_ItemName = CreateNewUniqueName(szBuf);
  159.         NotifyChanged();
  160.     }
  161.     return OLE_OK;
  162. }
  163.  
  164.  
  165.  
  166. /////////////////////////////////////////////////////////////////////////////
  167. // CServItem::OnSetBounds (LPRECT lpRect)
  168. //
  169. OLESTATUS CServItem::OnSetBounds (LPRECT lpRect)
  170. {
  171.     ASSERT_VALID (this);
  172.     ASSERT (lpRect != NULL);
  173.  
  174.     m_Rect.top = lpRect->top;
  175.     m_Rect.bottom = lpRect->bottom;
  176.     m_Rect.left = lpRect->left;
  177.     m_Rect.right = lpRect->right;
  178.     return OLE_OK;
  179. }
  180.  
  181.  
  182.  
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185. // CServItem::OnDraw (CMetaFile* pDC)
  186. //
  187. BOOL CServItem::OnDraw(CMetaFileDC* pDC)
  188. {
  189.     ASSERT(pDC != NULL);
  190.     BOOL bPass;
  191.     pDC->SetMapMode(MM_TEXT);
  192.     bPass = pDC->TextOut(10, 10, m_Text);
  193.  
  194.     return bPass;
  195. }
  196.     
  197.  
  198.  
  199.  
  200. LPSTR CreateNewUniqueName(LPSTR lpstr)
  201. {
  202.     wsprintf(lpstr, "%s%04d", (LPCSTR)szPrefix, GLOBAL_nCurrentNumber++);
  203.     return(lpstr);
  204. }
  205.  
  206.  
  207.  
  208.  
  209.  
  210. /////////////////////////////////////////////////////////////////////////////
  211. // CServItem::OnGetTextData()
  212. //
  213. BOOL CServItem::OnGetTextData(CString &stringText)
  214. {
  215.     stringText = m_Text;
  216.     return TRUE;
  217. }
  218.     
  219.  
  220.  
  221.  
  222. /////////////////////////////////////////////////////////////////////////////
  223. // CServItem::GetLinkData()
  224. //
  225. HANDLE CServItem::GetLinkData()
  226. {
  227.     LPSTR       lpLink;
  228.     HANDLE      hLink;
  229.     int         len;
  230.     CString     linkData, className;
  231.  
  232.     ASSERT_VALID(this);
  233.     linkData = "TestServ";
  234.  
  235.     len = linkData.GetLength() + m_DocName.GetLength() + 
  236.         m_ItemName.GetLength() + 4;
  237.  
  238.     hLink = GlobalAlloc(GMEM_DDESHARE | GHND, len + 1);
  239.     lpLink = (LPSTR)GlobalLock(hLink);
  240.  
  241.     TRACE("linkData is: %s\n", (const char*)linkData);
  242.     TRACE("m_DocName is:  %s\n", (const char*)m_DocName);
  243.     TRACE("m_ItemName is:  %s\n", (const char*)m_ItemName);
  244.  
  245.     len = linkData.GetLength();
  246.     lstrcpy(lpLink, linkData);
  247.     lpLink += (len + 1);
  248.  
  249.     len = m_DocName.GetLength();
  250.     lstrcpy(lpLink, m_DocName);
  251.     lpLink += (len + 1);
  252.  
  253.     lstrcpy(lpLink, m_ItemName);
  254.     len = linkData.GetLength();
  255.     lpLink += (len + 1);
  256.     *lpLink = '\0';     // Add a 2nd null terminating string
  257.  
  258.     GlobalUnlock(hLink);
  259.  
  260.     return hLink;
  261. }
  262.  
  263.  
  264.  
  265.  
  266.  
  267. /////////////////////////////////////////////////////////////////////////////
  268. // CServItem::OnRelease
  269. //
  270. OLESTATUS CServItem::OnRelease()
  271. {
  272.     OLESTATUS status;
  273.     status = COleServerItem::OnRelease();
  274.     if (status != OLE_OK)
  275.     {
  276.         MessageBox(NULL, "Error in Deleting Item", "Test Server",
  277.             MB_ICONEXCLAMATION);
  278.     }
  279.  
  280.     delete this;
  281.     return status;
  282. }
  283.  
  284.  
  285.  
  286. #ifdef _DEBUG
  287. /////////////////////////////////////////////////////////////////////////////
  288. // CServItem::AssertValid() const
  289. //
  290. void CServItem::AssertValid() const
  291. {
  292.     COleServerItem::AssertValid();
  293.     ASSERT(m_pOwner != NULL);
  294. }
  295.  
  296.  
  297.  
  298.  
  299. /////////////////////////////////////////////////////////////////////////////
  300. // CServItem::Dump (CDumpContext &dc)
  301. //
  302. void CServItem::Dump (CDumpContext &dc) const
  303. {
  304.     dc << "Beginning CServItem Dump\n";
  305.     COleServerItem::Dump(dc);
  306.     dc << "\nContains Text:  " << m_Text << "\n";
  307. }
  308. #endif
  309.  
  310.  
  311.  
  312. /////////////////////////////////////////////////////////////////////////////
  313.