home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / internet / stockticker / containermfc / containermfcdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  6.8 KB  |  258 lines

  1. // containerMFCDoc.cpp : implementation of the CContainerMFCDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "containerMFC.h"
  16.  
  17. #include "containerMFCDoc.h"
  18. #include "CntrItem.h"
  19. #include <io.h>
  20.  
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CContainerMFCDoc
  29.  
  30. IMPLEMENT_DYNCREATE(CContainerMFCDoc, COleDocument)
  31.  
  32. BEGIN_MESSAGE_MAP(CContainerMFCDoc, COleDocument)
  33.     //{{AFX_MSG_MAP(CContainerMFCDoc)
  34.         // NOTE - the ClassWizard will add and remove mapping macros here.
  35.         //    DO NOT EDIT what you see in these blocks of generated code!
  36.     //}}AFX_MSG_MAP
  37.     // Enable default OLE container implementation
  38.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, COleDocument::OnUpdatePasteMenu)
  39.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, COleDocument::OnUpdatePasteLinkMenu)
  40.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CONVERT, COleDocument::OnUpdateObjectVerbMenu)
  41.     ON_COMMAND(ID_OLE_EDIT_CONVERT, COleDocument::OnEditConvert)
  42.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, COleDocument::OnUpdateEditLinksMenu)
  43.     ON_COMMAND(ID_OLE_EDIT_LINKS, COleDocument::OnEditLinks)
  44.     ON_UPDATE_COMMAND_UI(ID_OLE_VERB_FIRST, COleDocument::OnUpdateObjectVerbMenu)
  45. END_MESSAGE_MAP()
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CContainerMFCDoc construction/destruction
  49.  
  50. CContainerMFCDoc::CContainerMFCDoc()
  51. : m_bNewFile(false), m_bStayOnTop(true), m_bNoSaveOnExit(false)
  52. {
  53.     // Use OLE compound files
  54.     EnableCompoundFile();
  55.  
  56.     m_strSaveFileName.LoadString(IDS_SAVE_FILE_NAME_MFC);
  57. }
  58.  
  59. CContainerMFCDoc::~CContainerMFCDoc()
  60. {
  61. }
  62.  
  63. BOOL CContainerMFCDoc::OnNewDocument()
  64. {
  65.     if (!COleDocument::OnNewDocument())
  66.         return FALSE;
  67.     return TRUE;
  68. }
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CContainerMFCDoc serialization
  72.  
  73. // Persist window position and control.
  74. //
  75. void CContainerMFCDoc::Serialize(CArchive& ar)
  76. {
  77.     CRect           r;
  78.     if (ar.IsStoring())
  79.     {
  80.         ((CFrameWnd*)AfxGetMainWnd())->GetWindowRect(&r);
  81.         ar << r;
  82.         ar << (WORD)(m_bStayOnTop ? 1 : 0);
  83.         if (m_stockTickerCtrl.GetSafeHwnd())
  84.             m_stockTickerCtrl.WriteControl(ar);
  85.     }
  86.     else
  87.     {
  88.         ar >> m_windowRect;
  89.         WORD        w;
  90.         ar >> w;
  91.         m_bStayOnTop = w == 1 ? true : false;
  92.         DWORD       dwCtlDataSize;
  93.  
  94.         ar >> dwCtlDataSize;
  95.  
  96.         LPBYTE      pBytes = new BYTE[dwCtlDataSize];
  97.         ar.Read(pBytes, dwCtlDataSize);
  98.         CMemFile    fileCtlData(pBytes, dwCtlDataSize);
  99.  
  100.         POSITION    pos = GetFirstViewPosition();
  101.         CView*      pFirstView = GetNextView(pos);
  102.         pFirstView->GetClientRect(&r);
  103.         BOOL b = m_stockTickerCtrl.CreateControl(m_stockTickerCtrl.GetClsid(),
  104.                                                  NULL, WS_VISIBLE, r, pFirstView, 0,
  105.                                                  &fileCtlData, TRUE);
  106.         delete[] pBytes;
  107.         if (!b)
  108.         {
  109.             CreateControlFailed();
  110.             return;
  111.         }
  112.     }
  113.  
  114.     // Calling the base class COleDocument enables serialization
  115.     //  of the container document's COleClientItem objects.
  116.     COleDocument::Serialize(ar);
  117. }
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CContainerMFCDoc diagnostics
  121.  
  122. #ifdef _DEBUG
  123. void CContainerMFCDoc::AssertValid() const
  124. {
  125.     COleDocument::AssertValid();
  126. }
  127.  
  128. void CContainerMFCDoc::Dump(CDumpContext& dc) const
  129. {
  130.     COleDocument::Dump(dc);
  131. }
  132. #endif //_DEBUG
  133.  
  134. /////////////////////////////////////////////////////////////////////////////
  135. // CContainerMFCDoc commands
  136.  
  137. // Create control and move window to previously saved position. If no data
  138. // file was found then create from scratch.
  139. //
  140. void CContainerMFCDoc::CreateControl(CView* pView, const CRect & r)
  141. {
  142.     bool    bResize = true;     // does the app need to be positioned and sized?
  143.  
  144.     if (!m_stockTickerCtrl.GetSafeHwnd())
  145.     {
  146.         CFrameWnd*  pMF = (CFrameWnd*)AfxGetMainWnd();
  147.         ASSERT_VALID(pMF);
  148.         pMF->ShowWindow(SW_HIDE);
  149.         if (_access(m_strSaveFileName, 0) != 0)
  150.         {
  151.             m_bNewFile = true;
  152.             pMF->GetWindowRect(&m_windowRect);
  153.             BOOL b = m_stockTickerCtrl.Create(NULL, WS_VISIBLE, r, pView, 0);
  154.             ASSERT(b);
  155.             if (!b)
  156.             {
  157.                 CreateControlFailed();
  158.                 return;
  159.             }
  160.             bResize = false;
  161.         }
  162.         else
  163.         {
  164.             // More than one instance can't use the same data file.
  165.             //
  166.             if (!OnOpenDocument(m_strSaveFileName))
  167.             {
  168.                 OpenDocFileFailed();
  169.                 return;
  170.             }
  171.         }
  172.     }
  173.     FinishCreate(bResize);
  174.     UpdateAllViews(NULL);
  175. }
  176.  
  177. CStockTickerCtrl* CContainerMFCDoc::GetControl()
  178. {
  179.     return &m_stockTickerCtrl;
  180. }
  181.  
  182. // Save everything to file.
  183. //
  184. void CContainerMFCDoc::Save()
  185. {
  186.     if (m_bNoSaveOnExit)
  187.         return;
  188.  
  189.     if (m_bNewFile)
  190.         DoSave(m_strSaveFileName);
  191.     else
  192.         SaveToStorage(0);
  193. }
  194.  
  195. // Toggle the stay on top feature on and off.
  196. //
  197. void CContainerMFCDoc::SetStayOnTop(CFrameWnd* pFrameWnd, bool bStayOnTop, UINT flags)
  198. {
  199.     m_bStayOnTop = bStayOnTop;
  200.  
  201.     pFrameWnd->SetWindowPos(m_bStayOnTop ? &CWnd::wndTopMost : &CWnd::wndNoTopMost,
  202.                             m_windowRect.left, m_windowRect.top,
  203.                             m_windowRect.Width(), m_windowRect.Height(),
  204.                             flags);
  205. }
  206.  
  207. // Are we currently in the stay on top state?
  208. //
  209. bool CContainerMFCDoc::IsStayOnTopSet()
  210. {
  211.     return m_bStayOnTop;
  212. }
  213.  
  214. // Display error messages.
  215. //
  216. void CContainerMFCDoc::CreateControlFailed()
  217. {
  218.     CString     strError;
  219.     CString     strTitle;
  220.     VERIFY(strError.LoadString(IDS_CREATE_CONTROL_FAILED));
  221.     VERIFY(strTitle.LoadString(AFX_IDS_APP_TITLE));
  222.     AfxGetMainWnd()->MessageBox(strError, strTitle, MB_OK|MB_ICONERROR);
  223.     m_bNoSaveOnExit = true;
  224.     ::PostQuitMessage(1);
  225. }
  226.  
  227. void CContainerMFCDoc::OpenDocFileFailed()
  228. {
  229.     CString     strError;
  230.     CString     strTitle;
  231.     VERIFY(strError.LoadString(IDS_OPEN_DOC_FILE_FAILED));
  232.     VERIFY(strTitle.LoadString(AFX_IDS_APP_TITLE));
  233.     AfxGetMainWnd()->MessageBox(strError, strTitle, MB_OK|MB_ICONERROR);
  234.     m_bNoSaveOnExit = true;
  235.     ::PostQuitMessage(1);
  236. }
  237.  
  238. // Check that window is still visible. Set stay on top state, also sets window
  239. // position.
  240. //
  241. void CContainerMFCDoc::FinishCreate(bool bResize)
  242. {
  243.     int     dtX = ::GetSystemMetrics(SM_CXSCREEN);
  244.     int     dtY = ::GetSystemMetrics(SM_CYSCREEN);
  245.  
  246.     if (m_windowRect.right > dtX)
  247.         m_windowRect.OffsetRect(-m_windowRect.Width(), 0);
  248.     if (m_windowRect.bottom > dtY)
  249.         m_windowRect.OffsetRect(0, -m_windowRect.Height());
  250.  
  251.     UINT    uWPFlags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE;
  252.  
  253.     if (bResize)
  254.         uWPFlags = SWP_SHOWWINDOW;
  255.  
  256.     SetStayOnTop((CFrameWnd*)AfxGetMainWnd(), m_bStayOnTop, uWPFlags);
  257. }
  258.