home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / stingray / gridsdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-27  |  4.8 KB  |  219 lines

  1. // gridsdoc.cpp : implementation of the CGridSampleDoc class
  2. //
  3.  
  4. // This is a part of the Objective Grid C++ Library.
  5. // Copyright (C) 1995,1996 ClassWorks, Stefan Hoenig.
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to
  9. // the Objective Grid Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding
  12. // the Objective Grid product.
  13. //
  14.  
  15. #include "stdafx.h"
  16. #include "gridapp.h"
  17.  
  18. #include "gridsdoc.h"
  19.  
  20. #ifndef _GXTWND_H_
  21. #include "gxtwnd.h"
  22. #endif
  23.  
  24. #ifndef _GXVW_H_
  25. #include "gxvw.h"
  26. #endif
  27.  
  28. #ifdef _DEBUG
  29. #undef THIS_FILE
  30. static char BASED_CODE THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CGridSampleDoc
  36.  
  37. IMPLEMENT_DYNCREATE(CGridSampleDoc, CDocument)
  38.  
  39. #define new DEBUG_NEW
  40.  
  41. BEGIN_MESSAGE_MAP(CGridSampleDoc, CDocument)
  42.     //{{AFX_MSG_MAP(CGridSampleDoc)
  43.     //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CGridSampleDoc construction/destruction
  48.  
  49. CGridSampleDoc::CGridSampleDoc()
  50. {
  51. }
  52.  
  53. CGridSampleDoc::~CGridSampleDoc()
  54. {
  55.     DeleteContents();
  56. }
  57.  
  58. void CGridSampleDoc::DeleteContents()
  59. {
  60.     int nSize = m_ParamArray.GetSize();
  61.  
  62.     for (int i = 0; i < m_bOwnParamArray.GetSize(); i++)
  63.         if (m_bOwnParamArray[i])
  64.             delete m_ParamArray[i];
  65.  
  66.     m_ParamArray.RemoveAll();
  67.     m_bOwnParamArray.RemoveAll();
  68. }
  69.  
  70. BOOL CGridSampleDoc::OnNewDocument()
  71. {
  72.     if (!CDocument::OnNewDocument())
  73.         return FALSE;
  74.  
  75.     SetModifiedFlag();
  76.  
  77.     return TRUE;
  78. }
  79.  
  80. BOOL CGridSampleDoc::OnOpenDocument(LPCTSTR pszPathName)
  81. {
  82.     // TRACE1("CDocument: OnOpenDocument(%s)\n", pszPathName);
  83.  
  84.     SetModifiedFlag(FALSE);
  85.  
  86.     if (!CDocument::OnOpenDocument(pszPathName))
  87.         return FALSE;
  88.  
  89.     return TRUE;
  90. }
  91.  
  92. BOOL CGridSampleDoc::OnSaveDocument(LPCTSTR pszPathName)
  93. {
  94.     // TRACE1("CDocument: OnSaveDocument(%s)\n", pszPathName);
  95.  
  96.     // Ensure that each views current cell is stored
  97.     CGXGridHint hint(gxHintTransferCurrentCell);
  98.     hint.lParam = TRUE;
  99.  
  100.     UpdateAllViews(NULL, 0, &hint);
  101.  
  102.     // Now, I can save the document
  103.     if (!CDocument::OnSaveDocument(pszPathName))
  104.         return FALSE;
  105.  
  106.     SetModifiedFlag(FALSE);
  107.  
  108.     return TRUE;
  109. }
  110.  
  111. BOOL CGridSampleDoc::CanCloseFrame(CFrameWnd* pFrame)
  112. {
  113.     // Ensure that views can be deactivated
  114.     CView* pView = pFrame->GetActiveView();
  115.     if (pView && pView->SendMessage(WM_GX_CANACTIVATE, 0, 0))
  116.         return FALSE;
  117.  
  118.     // Is it a grid?
  119.     CGXGridCore* pGrid = NULL;
  120.     if (pView->IsKindOf(RUNTIME_CLASS(CGXGridView)))
  121.         pGrid = (CGXGridCore*) ((CGXGridView*) pView);
  122.  
  123.     else if (pView->IsKindOf(RUNTIME_CLASS(CGXGridHandleView)))
  124.         pGrid = ((CGXGridHandleView*) pView)->GetGrid();
  125.  
  126.     if (pGrid)
  127.     {
  128.         // Ensure that the current cell can be stored
  129.         if (!pGrid->TransferCurrentCell(TRUE, GX_UPDATENOW, FALSE))
  130.             // grid state is invalid, don't close the frame
  131.             return FALSE;
  132.     }
  133.  
  134.     // Now, I can close the view
  135.     return CDocument::CanCloseFrame(pFrame);
  136. }
  137.  
  138. /////////////////////////////////////////////////////////////////////////////
  139. // CGridSampleDoc serialization
  140.  
  141. void CGridSampleDoc::Serialize(CArchive& ar)
  142. {
  143.     static const WORD wVersion = 1;
  144.     WORD wActualVersion = wVersion;
  145.  
  146.     ASSERT_VALID( this );
  147.  
  148.     if (ar.IsStoring())
  149.     {
  150.         ar << wVersion;
  151.     }
  152.     else
  153.     {
  154.         // Check for version first
  155.         ar >> wActualVersion;
  156.         if( wActualVersion != wVersion )
  157.         {
  158.             // Wrong version
  159. #ifdef _DEBUG
  160.             TRACE0("Incompatible format while reading CGridSampleDoc" );
  161.             TRACE2("in %s at line %d\n", __FILE__, __LINE__);
  162. #endif
  163.             AfxThrowArchiveException(CArchiveException::badSchema);
  164.             return;
  165.         }
  166.     }
  167.  
  168.     m_ParamArray.Serialize( ar );
  169.     m_bOwnParamArray.Serialize( ar );
  170. }
  171.  
  172.  
  173. /////////////////////////////////////////////////////////////////////////////
  174. // CGridSampleDoc diagnostics
  175.  
  176. #ifdef _DEBUG
  177. void CGridSampleDoc::AssertValid() const
  178. {
  179.     CDocument::AssertValid();
  180. }
  181.  
  182. void CGridSampleDoc::Dump(CDumpContext& dc) const
  183. {
  184.     CDocument::Dump(dc);
  185. }
  186. #endif //_DEBUG
  187.  
  188. /////////////////////////////////////////////////////////////////////////////
  189. // CGridSampleDoc commands
  190.  
  191. CObject* CGridSampleDoc::GetParam(int nViewID)
  192. {
  193.     CObject* pParam = NULL;
  194.  
  195.     if (m_ParamArray.GetUpperBound() >= nViewID && m_ParamArray[nViewID])
  196.         pParam = m_ParamArray[nViewID];
  197.  
  198.     return pParam;
  199. }
  200.  
  201. void CGridSampleDoc::SetParam(int nViewID, CObject* pParam, BOOL bMustDelete)
  202. {
  203.     CObject* pDocParam = NULL;
  204.  
  205.     if (m_ParamArray.GetUpperBound() >= nViewID && m_ParamArray[nViewID])
  206.         pDocParam = m_ParamArray[nViewID];
  207.  
  208.     if (pDocParam == NULL || pParam != NULL)
  209.     {
  210.         if (pDocParam)
  211.             delete pDocParam;
  212.  
  213.         ASSERT_VALID(pParam);
  214.  
  215.         m_ParamArray.SetAtGrow(nViewID, pParam);
  216.         m_bOwnParamArray.SetAtGrow(nViewID, (BYTE) bMustDelete);
  217.     }
  218. }
  219.