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 / browsdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-27  |  6.9 KB  |  276 lines

  1. // browsdoc.cpp : implementation of the CDBaseBrowserDoc 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. #include "browsdoc.h"
  18.  
  19. #ifndef _GXTWND_H_
  20. #include "gxtwnd.h"
  21. #endif
  22.  
  23. #ifndef _GXVW_H_
  24. #include "gxvw.h"
  25. #endif
  26.  
  27. #ifdef _DEBUG
  28. #undef THIS_FILE
  29. static char BASED_CODE THIS_FILE[] = __FILE__;
  30. #endif
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CDBaseBrowserDoc
  34.  
  35. IMPLEMENT_DYNCREATE(CDBaseBrowserDoc, CDocument)
  36.  
  37. static TCHAR BASED_CODE szNumeric[]  = _T("Numeric");
  38. static TCHAR BASED_CODE szText[]     = _T("Text");
  39. static TCHAR BASED_CODE szDate[]     = _T("Date");
  40. static TCHAR BASED_CODE szLogical[]  = _T("Logical");
  41.  
  42. CDBaseBrowserDoc::CDBaseBrowserDoc()
  43. {
  44.     // Standard styles
  45.     m_stylesMap.CreateStandardStyles();
  46.  
  47.     // New styles
  48.     m_wStyleNumeric = m_stylesMap.RegisterStyle(szNumeric, CGXStyle()
  49.             .SetHorizontalAlignment(DT_RIGHT)
  50.             .SetMaxLength(20));
  51.  
  52.     m_wStyleText = m_stylesMap.RegisterStyle(szText, CGXStyle()
  53.             .SetHorizontalAlignment(DT_LEFT),
  54.             TRUE /* system style */);
  55.  
  56.     m_wStyleDate = m_stylesMap.RegisterStyle(szDate, CGXStyle()
  57.             .SetHorizontalAlignment(DT_LEFT)
  58.             .SetMaxLength(12),
  59.             TRUE /* system style */);
  60.  
  61.     m_wStyleLogical = m_stylesMap.RegisterStyle(szLogical, CGXStyle()
  62.             .SetHorizontalAlignment(DT_RIGHT)
  63.             .SetMaxLength(6),
  64.             TRUE /* system style */);
  65.  
  66.     // Change default styles
  67.  
  68.     // Don't allow Enter-Key to enter several lines
  69.     CGXStyle* pStyle;
  70.     m_stylesMap.LookupStyle(m_stylesMap.m_wStyleStandard, pStyle);
  71.     pStyle->SetAllowEnter(FALSE);
  72.     // if you would use AddStyle, the previous style would get deleted
  73.  
  74.     m_stylesMap.ReadProfile();  // read user defined standard settings
  75.  
  76.     m_param.SetStylesMap(&m_stylesMap, FALSE /* do not destroy */);
  77.  
  78.     // here you could set default settings
  79.     // m_properties.SetDisplayVertLines(FALSE);
  80.  
  81.     m_properties.ReadProfile(); // read user defined standard settings
  82.  
  83.     m_param.SetProperties(&m_properties, FALSE /* do not destroy */);
  84.  
  85.     m_param.EnableTrackRowHeight(GX_TRACK_ALL);
  86.     m_param.EnableMoveRows(FALSE);
  87.     m_param.EnableSelection(GX_SELROW | GX_SELCOL | GX_SELTABLE | GX_SELSHIFT);
  88.  
  89.     // Normally, the objects are created in CGXGridCore::OnGridInitialUpdate.
  90.     // CGXGridCore::OnGridInitialUpdate checks each pointer in CGXGridParam.
  91.     // If there exists no valid pointer yet, the object is created on the heap.
  92.     // If CGXGridParam creates an object, it sets a flag that indicates
  93.     // that the object has to be destroyed when the CGXGridParam-object gets
  94.     // destroyed.
  95.  
  96.     // In the above calls to SetProperties, SetStylesMap, ... the FALSE parameter
  97.     // indicates that the object must not be destroyed by the CGXGridParam-object.
  98.  
  99.     // See CDBaseBrowserView::OnInitialUpdate for linking the parameter-object
  100.     // to the gridview.
  101. }
  102.  
  103. BOOL CDBaseBrowserDoc::OnNewDocument()
  104. {
  105.     if (!CDocument::OnNewDocument())
  106.         return FALSE;
  107.  
  108.     // Ask for Dbase-File
  109.     CFileDialog dlg(TRUE, _T("dsf"), NULL,
  110.         0 /* OFN_HIDEREADONLY */, _T("DBase-Files (*.dbf)|*.dbf||"));
  111.  
  112.     if (dlg.DoModal() != IDOK)
  113.         return FALSE;
  114.  
  115.     BOOL b = m_dbfile.Open(dlg.GetPathName(), dlg.GetReadOnlyPref());
  116.  
  117.     if (b)
  118.     {
  119.         for (int i = 1; i <= m_dbfile.nFieldCount; i++)
  120.             m_columnIdMap[i] = i-1;
  121.     }
  122.  
  123.     SetModifiedFlag();
  124.  
  125.     // initialize header
  126.     {
  127.         CGXData& header = m_properties.GetDataHeader();
  128.  
  129.         header.DeleteContents();
  130.         header.StoreRowCount(10);
  131.         header.StoreColCount(3);
  132.         header.StoreStyleRowCol(1, 2, CGXStyle()
  133.             .SetValue(_T("GridApp DBase Browser"))
  134.             .SetFont(CGXFont().SetBold(TRUE).SetSize(12)),
  135.             gxOverride);
  136.         header.StoreStyleRowCol(2, 2, CGXStyle()
  137.             .SetValue(dlg.GetFileName())
  138.             .SetFont(CGXFont().SetBold(TRUE).SetSize(16)),
  139.             gxOverride);
  140.     }
  141.  
  142.     return b;
  143. }
  144.  
  145. CDBaseBrowserDoc::~CDBaseBrowserDoc()
  146. {
  147. }
  148.  
  149.  
  150. BEGIN_MESSAGE_MAP(CDBaseBrowserDoc, CDocument)
  151.     //{{AFX_MSG_MAP(CDBaseBrowserDoc)
  152.         // NOTE - the ClassWizard will add and remove mapping macros here.
  153.     //}}AFX_MSG_MAP
  154. END_MESSAGE_MAP()
  155.  
  156.  
  157.  
  158. /////////////////////////////////////////////////////////////////////////////
  159. // CDBaseBrowserDoc diagnostics
  160.  
  161. #ifdef _DEBUG
  162. void CDBaseBrowserDoc::AssertValid() const
  163. {
  164.     CDocument::AssertValid();
  165. }
  166.  
  167. void CDBaseBrowserDoc::Dump(CDumpContext& dc) const
  168. {
  169.     CDocument::Dump(dc);
  170. }
  171. #endif //_DEBUG
  172.  
  173. /////////////////////////////////////////////////////////////////////////////
  174. // CDBaseBrowserDoc serialization
  175.  
  176. void CDBaseBrowserDoc::Serialize(CArchive& ar)
  177. {
  178.     static const WORD wVersion = 1;
  179.     WORD wActualVersion = wVersion;
  180.  
  181.     ASSERT_VALID( this );
  182.  
  183.     if (ar.IsStoring())
  184.     {
  185.         ar << wVersion;
  186.     }
  187.     else
  188.     {
  189.         // Check for version first
  190.         ar >> wActualVersion;
  191.         if( wActualVersion != wVersion )
  192.         {
  193.             // Wrong version
  194. #ifdef _DEBUG
  195.             TRACE0("Incompatible format while reading CDBaseBrowserDoc");
  196.             TRACE2("in %s at line %d\n", __FILE__, __LINE__);
  197. #endif
  198.             AfxThrowArchiveException(CArchiveException::badSchema);
  199.             return;
  200.         }
  201.     }
  202.  
  203.     if (ar.IsStoring())
  204.     {
  205.         ar << m_dbfile.sFileName;
  206.  
  207.         ar << (BYTE) m_dbfile.bReadOnly;
  208.  
  209.         m_columnIdMap.Serialize(ar);
  210.  
  211.         m_param.Serialize(ar);
  212.  
  213.         // Column widths
  214.         for (int i = 0; i < m_dbfile.nFieldCount; i++)
  215.             ar << (WORD) m_dbfile.GetField(i)->width;
  216.     }
  217.     else
  218.     {
  219.         BYTE bReadOnly;
  220.         CString sFileName;
  221.  
  222.         ar >> sFileName;
  223.  
  224.         ar >> bReadOnly;
  225.  
  226.         m_dbfile.Open(sFileName, (BOOL) bReadOnly);
  227.  
  228.         m_columnIdMap.Serialize(ar);
  229.  
  230.         m_param.Serialize(ar);
  231.  
  232.         // Column widths
  233.         WORD w;
  234.         for (int i = 0; i < m_dbfile.nFieldCount; i++)
  235.             ar >> w, m_dbfile.GetField(i)->width = (short) w;
  236.     }
  237. }
  238.  
  239. /////////////////////////////////////////////////////////////////////////////
  240. // CDBaseBrowserDoc commands
  241.  
  242. BOOL CDBaseBrowserDoc::OnSaveDocument(LPCTSTR pszPathName)
  243. {
  244.     // TRACE1("CDocument: OnSaveDocument(%s)\n", pszPathName);
  245.  
  246.     // Ensure that each views current cell is stored
  247.     CGXGridHint hint(gxHintTransferCurrentCell);
  248.     hint.lParam = TRUE;
  249.  
  250.     UpdateAllViews(NULL, 0, &hint);
  251.  
  252.     // Now, I can save the document
  253.     if (!CDocument::OnSaveDocument(pszPathName))
  254.         return FALSE;
  255.  
  256.     SetModifiedFlag(FALSE);
  257.  
  258.     return TRUE;
  259. }
  260.  
  261. BOOL CDBaseBrowserDoc::CanCloseFrame(CFrameWnd* pFrame)
  262. {
  263.     // Ensure that views can be deactivated
  264.     CView* pView = pFrame->GetActiveView();
  265.     if (pView && pView->SendMessage(WM_GX_CANACTIVATE, 0, 0))
  266.         return FALSE;
  267.  
  268.     // Ensure that the current cell is stored
  269.     if (pView->IsKindOf(RUNTIME_CLASS(CGXGridView))
  270.         && !((CGXGridView*) pView)->TransferCurrentCell())
  271.         return FALSE;
  272.  
  273.     // Now, I can close the view
  274.     return CDocument::CanCloseFrame(pFrame);
  275. }
  276.