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

  1.  
  2. // gridsvw4.cpp : implementation of the CGridSample4View class
  3. //
  4.  
  5. // This is a part of the Objective Grid C++ Library.
  6. // Copyright (C) 1995,1996 ClassWorks, Stefan Hoenig.
  7. // All rights reserved.
  8. //
  9. // This source code is only intended as a supplement to
  10. // the Objective Grid Classes Reference and related
  11. // electronic documentation provided with the library.
  12. // See these sources for detailed information regarding
  13. // the Objective Grid product.
  14. //
  15.  
  16. #include "stdafx.h"
  17. #include "gridapp.h"
  18.  
  19. #include "gridsdoc.h"
  20. #include "gridsvw4.h"
  21. #include "dlguser.h"
  22. #include "mainfrm.h"
  23. #include "gridfrms.h"
  24.  
  25. #ifdef _DEBUG
  26. #undef THIS_FILE
  27. static char BASED_CODE THIS_FILE[] = __FILE__;
  28. #endif
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CGridSample4View
  32. //
  33. // CGridSample4View illustrates how to override CGXPrintProperties,
  34. // use own tokens for header and footers and connect the derived
  35. // properties object with the grid.
  36. //
  37. // CGridSample4View also illustrates how to register a bitmap control
  38. // and display it in a covered cell
  39. //
  40.  
  41. IMPLEMENT_DYNCREATE(CGridSample4View, CMyGridView)
  42. IMPLEMENT_SERIAL(CMyProperties, CGXProperties, 0)
  43.  
  44. static TCHAR BASED_CODE szInstruct[] =
  45.         _T("This view shows a bitmap, some 3d-effects and has registered some header/footer tokens. ")
  46.         _T("Call File->Header/Footer and File->Print Preview to see the tokens. ")
  47.         _T("The bitmap can be applied to cells with Format->Cells->Control.");
  48.  
  49. #define new DEBUG_NEW
  50.  
  51. CMyProperties::CMyProperties()
  52. {
  53.     m_nTokDate = AddToken(_T("#DATE#"));
  54.     m_nTokText = AddToken(_T("$T"));
  55.  
  56.     CGXData& mapDataHeader = GetDataHeader();
  57.     CGXData& mapDataFooter = GetDataFooter();
  58.  
  59.     mapDataHeader.DeleteContents();
  60.  
  61.     mapDataHeader.StoreStyleRowCol(1, 2, CGXStyle()
  62.         .SetValue(_T("Header"))
  63.         .SetFont(CGXFont().SetSize(10))
  64.         , gxCopy
  65.         );
  66.  
  67.     mapDataHeader.StoreStyleRowCol(2, 2, CGXStyle()
  68.         .SetValue(_T("$R"))
  69.         .SetFont(CGXFont()
  70.             .SetBold(TRUE)
  71.             .SetSize(16))
  72.         , gxCopy
  73.         );
  74.  
  75.     mapDataHeader.StoreStyleRowCol(2, 3, CGXStyle()
  76.         .SetValue(_T("Datum: #DATE#{%x, %X}"))  // see  GXIntlStrFTime for date formatting
  77.         .SetFont(CGXFont().SetSize(10))
  78.         , gxCopy
  79.         );
  80.  
  81.  
  82.     mapDataFooter.DeleteContents();
  83.  
  84.     mapDataFooter.StoreStyleRowCol(1, 2, CGXStyle()
  85.         .SetValue(_T("Footer"))
  86.         .SetFont(CGXFont().SetSize(10).SetBold(TRUE))
  87.         , gxCopy
  88.         );
  89.     mapDataFooter.StoreStyleRowCol(2, 2, CGXStyle()
  90.         .SetValue(_T("- $T -"))
  91.         , gxCopy
  92.         );
  93. }
  94.  
  95. CString CMyProperties::SubstTokenText(int nToken, CString& sRest)
  96. {
  97.     if (nToken == m_nTokDate)
  98.     {
  99.         CString args = GetTokenArgs(sRest);
  100.         if (args.IsEmpty())
  101.             args = _T("%c");
  102.  
  103.         TCHAR szBuffer[255];
  104.  
  105.         // GetCurrentTime
  106.         time_t ti = time(NULL);
  107.         struct tm* ptmTemp = localtime(&ti);
  108.         ASSERT(ptmTemp != NULL); // make sure the time has been initialized!
  109.  
  110.         if (!GXIntlStrFtime(AfxGetResourceHandle(), GX_IDS_TIME_ADAY1,
  111.                 szBuffer, sizeof(szBuffer) / sizeof(TCHAR), args, ptmTemp))
  112.             szBuffer[0] = _T('\0');
  113.         return szBuffer;
  114.     }
  115.     else if (nToken == m_nTokText)
  116.     {
  117.         return _T("Text-Token");
  118.     }
  119.  
  120.     return CGXProperties::SubstTokenText(nToken, sRest);
  121. }
  122.  
  123. BEGIN_MESSAGE_MAP(CGridSample4View, CMyGridView)
  124.     //{{AFX_MSG_MAP(CGridSample4View)
  125.     ON_COMMAND(ID_VIEW_USERACTIONS, OnViewUseractions)
  126.     ON_COMMAND(ID_VIEW_SPLITTERVIEW, OnViewSplitterview)
  127.     //}}AFX_MSG_MAP
  128. END_MESSAGE_MAP()
  129.  
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CGridSample4View construction/destruction
  132.  
  133. CGridSample4View::CGridSample4View()
  134. {
  135.     // TODO: add construction code here
  136. }
  137.  
  138. CGridSample4View::~CGridSample4View()
  139. {
  140. }
  141.  
  142. BOOL CGridSample4View::ConnectParam()
  143. {
  144.     // Note: this method is copied from CGridSampleView
  145.     //
  146.  
  147.     BOOL bNew = FALSE;
  148.  
  149.     // Retrive the zero-based worksheet-id if used as worksheet
  150.     if (GetParentTabWnd(this, TRUE))
  151.         m_nViewID = GetParentTabViewID(this);
  152.  
  153.     // check if it is a new pane in a splitter window
  154.     CSplitterWnd* pSplitterWnd = GetParentDynamicSplitter(this, TRUE);
  155.     if (pSplitterWnd != NULL)
  156.     {
  157.         CGXGridView *pView = (CGXGridView *) pSplitterWnd->GetPane(0, 0);
  158.         if (pView != this)
  159.             m_nViewID = pView->GetViewID();
  160.     }
  161.  
  162.     // check if parameter-object exist (in document)
  163.     if (GetDocument()->GetParam(m_nViewID) == NULL)
  164.     {
  165.         // create a parameter-object on the heap
  166.         GetDocument()->SetParam(m_nViewID, new CGXGridParam);
  167.  
  168.         bNew = TRUE;    // this view needs initializing
  169.     }
  170.  
  171.     // connect parameter-object with grid
  172.     SetParam((CGXGridParam*) GetDocument()->GetParam(m_nViewID), FALSE);
  173.  
  174.     return bNew;
  175. }
  176.  
  177. void CGridSample4View::SetupProperties()
  178. {
  179.     ASSERT(GetParam()->GetProperties() == NULL);
  180.     // ASSERTION-> a property object is already connected to parameter-object ->END
  181.  
  182.     if (GetParam()->GetProperties() != NULL)
  183.         return;
  184.  
  185.     // create a property object and connect it with the parameter-object
  186.     CMyProperties* pProp;
  187.  
  188.     GetParam()->SetProperties(pProp = new CMyProperties);
  189.     pProp->AddDefaultUserProperties();
  190.  
  191.     pProp->SetCenterHorizontal(TRUE);
  192.     pProp->SetDisplayHorzLines(FALSE);
  193.     pProp->SetPrintHorzLines(FALSE);
  194.     pProp->SetDisplayVertLines(FALSE);
  195.     pProp->SetPrintVertLines(FALSE);
  196.     pProp->SetBlackWhite(TRUE);
  197.     pProp->SetPrintFrame(FALSE);
  198.  
  199.     // Now, read the properties from profile.
  200.     // Note that reading properties from profile can override the previous settings.
  201.     // The previous settings are standard settings.
  202.  
  203.     pProp->SetSection(_T("My Properties")); // extra profile section
  204.  
  205.     pProp->ReadProfile();
  206. }
  207.  
  208.  
  209. /////////////////////////////////////////////////////////////////////////////
  210. // CGridSample4View drawing
  211.  
  212. void CGridSample4View::OnInitialUpdate()
  213. {
  214.     BOOL bNew = ConnectParam();
  215.  
  216.     if (bNew)
  217.         SetupProperties();  // Setup base styles and read them from profile
  218.  
  219.     CMyGridView::OnInitialUpdate(); // Creates all objects and links them to the grid
  220.  
  221.     // Register the welcome bitmap
  222.     // Use CGXDIBitmapButton instead of CGXBitmapButton
  223.     RegisterControl(IDS_CTRL_BITMAP, new CGXDIBitmapButton(this, IDB_WELCOME));
  224.     // You can apply this bitmap to cells with Format->Cells->Control
  225.  
  226.     if (bNew)
  227.     {
  228.         GetParam()->EnableUndo(FALSE);
  229.  
  230.         SetRowCount(100);
  231.         SetColCount(20);
  232.  
  233.         // Hide the row header
  234.         SetRowHeight(0, 0, 0);
  235.  
  236.         // Hide the column header
  237.         SetColWidth(0, 0, 0);
  238.  
  239.         // Disable tracking column header size
  240.         GetParam()->EnableTrackRowHeight(GX_TRACK_INDIVIDUAL | GX_TRACK_EXTHITTEST | GX_TRACK_NOTHEADER);
  241.             // you can drop GX_TRACK_EXTHITTEST if you don't want the extended size feature
  242.  
  243.         GetParam()->EnableTrackColWidth(GX_TRACK_INDIVIDUAL | GX_TRACK_EXTHITTEST | GX_TRACK_NOTHEADER);
  244.  
  245.         // disable selecting cells
  246.         GetParam()->EnableSelection(GX_SELNONE);
  247.  
  248.         // give the cells some 3d inset effect and a pattern
  249.         StandardStyle()
  250.             .SetDraw3dFrame(gxFrameInset)
  251.             .SetInterior(
  252.                 CGXBrush()
  253.                     .SetPattern(4)
  254.                     .SetColor(RGB(0,0,255)) )   // blue dots
  255.             .SetBorders(gxBorderAll, CGXPen(PS_SOLID, 3, RGB(192,192,192)));
  256.  
  257.         // ... add the extra width used by borders to the default row-height
  258.         SetDefaultRowHeight(GetDefaultRowHeight()+6);
  259.  
  260.         // Display the welcome bitmap
  261.         SetCoveredCellsRowCol(5, 2, 16, 6);
  262.         SetStyleRange(CGXRange(5, 2), CGXStyle()
  263.                 .SetControl(IDS_CTRL_BITMAP)
  264.                 .SetInterior(RGB(192,192,192))
  265.             );
  266.  
  267.         // Instructions
  268.         SetCoveredCellsRowCol(1, 1, 3, 5);
  269.         SetStyleRange(CGXRange(1, 1),
  270.             CGXStyle()
  271.                 .SetWrapText(TRUE)
  272.                 .SetEnabled(FALSE)
  273.                 .SetFont(CGXFont().SetFaceName(_T("Times New Roman")))
  274.                 .SetInterior(RGB(255,251,240))   // Off-white
  275.                 .SetHorizontalAlignment(DT_CENTER)
  276.                 .SetVerticalAlignment(DT_VCENTER)
  277.                 .SetControl(GX_IDS_CTRL_STATIC)
  278.                 .SetBorders(gxBorderAll, CGXPen().SetWidth(2))
  279.                 .SetValue(szInstruct));
  280.  
  281.         // Enable ceration of undo-information for user interactions
  282.         GetParam()->EnableUndo(TRUE);
  283.     }
  284.  
  285.     // Position the current cell
  286.  
  287.     SetCurrentCell(4, 1, FALSE /* avoid immediate updating */);
  288.  
  289.     // Enable Update-Hint-Mechanism
  290.  
  291.     EnableHints();
  292. }
  293.  
  294. /////////////////////////////////////////////////////////////////////////////
  295. // CGridSample4View diagnostics
  296.  
  297. #ifdef _DEBUG
  298. void CGridSample4View::AssertValid() const
  299. {
  300.     CMyGridView::AssertValid();
  301. }
  302.  
  303. void CGridSample4View::Dump(CDumpContext& dc) const
  304. {
  305.     CMyGridView::Dump(dc);
  306. }
  307.  
  308. CGridSampleDoc* CGridSample4View::GetDocument() // non-debug version is inline
  309. {
  310.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGridSampleDoc)));
  311.     return (CGridSampleDoc*) m_pDocument;
  312. }
  313.  
  314. #endif //_DEBUG
  315.  
  316.  
  317. /////////////////////////////////////////////////////////////////////////////
  318. // CGridSample4View message handlers
  319.  
  320. // Menu handler for View->Splitter View
  321.  
  322. void CGridSample4View::OnViewSplitterview()
  323. {
  324.     CDocument* pDoc = GetDocument();
  325.  
  326.     CMyMultiDocTemplate* pTemplate
  327.         = (CMyMultiDocTemplate*) ((CGridSampleApp*) AfxGetApp())->m_pSplitterTemplate;
  328.  
  329.     pTemplate->SetViewClass(GetRuntimeClass());
  330.  
  331.     CMDIChildWnd* pNewFrame
  332.         = (CMDIChildWnd*) pTemplate->CreateNewFrame(GetDocument(), NULL);
  333.  
  334.     if (pNewFrame == NULL)
  335.         return;     // not created
  336.  
  337.     ASSERT(pNewFrame->IsKindOf(RUNTIME_CLASS(CSplitterMDIChildWnd)));
  338.  
  339.     CSplitterWnd& splitter = (CSplitterWnd&)
  340.         ((CSplitterMDIChildWnd *) pNewFrame)->m_wndSplitter;
  341.  
  342.     CGridSample4View* pView = (CGridSample4View*)
  343.         splitter.GetPane(0, 0);
  344.  
  345.     // Set view id to active tab view id
  346.     pView->m_nViewID = m_nViewID;
  347.  
  348.     pTemplate->InitialUpdateFrame(pNewFrame, pDoc);
  349.  
  350.     pNewFrame->GetActiveView();
  351.     ASSERT(pView);
  352. }
  353.  
  354. // Menu handler for View->User Actions...
  355.  
  356. void CGridSample4View::OnViewUseractions()
  357. {
  358.     // Note: this method is copied from CGridSampleView
  359.     //
  360.     // Shows a dialog with some attributes of the parameter-object
  361.     // where you can experiment with some attributes
  362.     // such as allowing the user to track columns, select cells
  363.     // or use the grid as a listbox.
  364.  
  365.     // Transfer Current Cell's Data to grid
  366.     if (!TransferCurrentCell())
  367.         return;
  368.  
  369.     CUserActionsDialog dlg(GetParam());
  370.  
  371.     if (dlg.DoModal() == IDOK)
  372.     {
  373.         // Redraw the grid
  374.         Redraw();
  375.     }
  376. }
  377.