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

  1. // gridsvw.cpp : implementation of the CGridSampleView 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. #include "gridsvw.h"
  20. #include "dlguser.h"
  21. #include "mainfrm.h"
  22. #include "gridfrms.h"
  23.  
  24. #ifdef _DEBUG
  25. #undef THIS_FILE
  26. static char BASED_CODE THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CGridSampleView
  31. //
  32. // CGridSampleView can be used as standalone, splitter or worksheet gridview.
  33. //
  34. // As described in the overview of the programmers guide,
  35. // it is necessary to attach a parameter-object to the grid
  36. // and attach several other objects as a property-object, a data-object,
  37. // a printdevice-object and stylesmap-object to the parameter-object.
  38. //
  39. // CGridSampleDoc maintains an array with parameter objects.
  40. // CGridSampleView initializes m_nViewID with its worksheet id
  41. // or 0 if it is used as standalone view.
  42. // m_nViewID is used as index for the parameters stored in CGridSampleDoc.
  43. //
  44. // CGridSampleView checks if parameter-object exists for the m_nViewID index
  45. // and connects this parameter-object with the gridview. If the parameter
  46. // object is not existing, one is created on the heap.
  47. //
  48. // Next, CGridSampleView calls OnInitialUpdate to initialize all other objects.
  49. // When the view is closed, all these objects will be destroyed.
  50. //
  51. // CGridSampleView enables hint creation by calling EnableHints().
  52. // To avoid setting the document dirty when initial commands are executed,
  53. // EnableHints() is called as last command in OnInitialUpdate.
  54. //
  55. // To prevent the user undoing the initial commands, OnInitialUpdate calls
  56. // GetParam()->EnableUndo(FALSE) and after executing inital commands
  57. // GetParam()->EnableUndo(TRUE).
  58.  
  59. // CGridSampleView also illustrates how to apply cell some simple formattings.
  60. //
  61.  
  62. static TCHAR BASED_CODE szInstruct[] =
  63.         _T("This is a simple gridview. You can move the current cell, ")
  64.         _T("edit the cell contents, zoom the view, change the printers settings, ")
  65.         _T("and ...");
  66.  
  67. IMPLEMENT_DYNCREATE(CGridSampleView, CMyGridView)
  68.  
  69. #define new DEBUG_NEW
  70.  
  71. BEGIN_MESSAGE_MAP(CGridSampleView, CMyGridView)
  72.     //{{AFX_MSG_MAP(CGridSampleView)
  73.     ON_COMMAND(ID_VIEW_USERACTIONS, OnViewUseractions)
  74.     ON_COMMAND(ID_VIEW_SPLITTERVIEW, OnViewSplitterview)
  75.     //}}AFX_MSG_MAP
  76. END_MESSAGE_MAP()
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CGridSampleView construction/destruction
  80.  
  81. CGridSampleView::CGridSampleView()
  82. {
  83. }
  84.  
  85. CGridSampleView::~CGridSampleView()
  86. {
  87. }
  88.  
  89. BOOL CGridSampleView::ConnectParam()
  90. {
  91.     BOOL bNew = FALSE;
  92.  
  93.     // Retrive the zero-based worksheet-id if used as worksheet
  94.     if (GetParentTabWnd(this, TRUE))
  95.         m_nViewID = GetParentTabViewID(this);
  96.  
  97.     // check if it is a new pane in a splitter window
  98.     CSplitterWnd* pSplitterWnd = GetParentDynamicSplitter(this, TRUE);
  99.     if (pSplitterWnd != NULL)
  100.     {
  101.         CGXGridView *pView = (CGXGridView *) pSplitterWnd->GetPane(0, 0);
  102.         if (pView != this)
  103.             m_nViewID = pView->GetViewID();
  104.     }
  105.  
  106.     // check if parameter-object exist (in document)
  107.     if (GetDocument()->GetParam(m_nViewID) == NULL)
  108.     {
  109.         // create a parameter-object on the heap
  110.         GetDocument()->SetParam(m_nViewID, new CGXGridParam);
  111.  
  112.         bNew = TRUE;    // this view needs initializing
  113.     }
  114.  
  115.     // connect parameter-object with grid
  116.     SetParam((CGXGridParam*) GetDocument()->GetParam(m_nViewID), FALSE);
  117.  
  118.     return bNew;
  119. }
  120.  
  121. void CGridSampleView::OnInitialUpdate()
  122. {
  123.     BOOL bNew = ConnectParam();
  124.  
  125.     CMyGridView::OnInitialUpdate(); // Creates all objects and links them to the grid
  126.  
  127.     // Now, all objects are connected:
  128.     ASSERT(GetParam() != NULL);                     // parameter object
  129.     ASSERT(GetParam()->GetProperties() != NULL);    // properties object (with header-/footer, colors,...)
  130.     ASSERT(GetParam()->GetStylesMap() != NULL);     // stylesmap object (with base styles)
  131.     ASSERT(GetParam()->GetData() != NULL);          // data object (with cell's data)
  132.     ASSERT(GetParam()->GetPrintDevice() != NULL);   // printdevice object (with printer settings)
  133.  
  134.     // ... and you can execute commands on the grid
  135.  
  136.     if (bNew)
  137.     {
  138.         // Don't create undo-information for the following commands
  139.         GetParam()->EnableUndo(FALSE);
  140.         // (at the end of this procedure, I will reenable it)
  141.  
  142.         // Change default background color
  143.         //   StandardStyle() returns a reference to the "Standard-Style".
  144.         //   All cells in the grid inherit their attributes from this Standard-Style
  145.         //   The user can change the Standard-Style with menu Format|Styles.
  146.  
  147.         StandardStyle()
  148.                 .SetInterior(RGB(255,255,255));
  149.  
  150.         // Turn off row-numbers
  151.         //   RowHeaderStyle() returns a reference to the "Row Header-Style".
  152.         RowHeaderStyle()
  153.                 .SetValue(_T(""));
  154.  
  155.         // Number of rows and columns
  156.         SetRowCount(100);
  157.         SetColCount(20);
  158.  
  159.         // Apply some formattings to the cells
  160.         SetValueRange(CGXRange(5,1), _T("Hello"));
  161.  
  162.         // You can call SetStyleRange several times on the same cell
  163.         SetValueRange(CGXRange(6,1), _T("world!"));
  164.         // you could also call:
  165.         //     SetStyleRange(CGXRange(6,1), CGXStyle().SetValue(_T("world!")));
  166.  
  167.         // .. right alignment is added to the cell's formatting
  168.         SetStyleRange(CGXRange(6,1), CGXStyle().SetHorizontalAlignment(DT_RIGHT));
  169.  
  170.         // ... or you can call SetStyleRange with several attributes.
  171.         SetStyleRange(
  172.             CGXRange(8,2,12,3),
  173.             CGXStyle()
  174.                 .SetValue(_T("ABC"))
  175.                 .SetTextColor(RGB(255,255,0))   // Bright Yellow
  176.                 .SetDraw3dFrame(gxFrameInset)   // "inset" 3d-effect
  177.                 .SetInterior(
  178.                     CGXBrush()
  179.                         .SetPattern(14)         // "////" - Pattern, red lines, blue background
  180.                         .SetColor(RGB(255,0,0))
  181.                         .SetBkColor(RGB(0,0,255)) )
  182.                 .SetMaxLength(4)                // Limit input to 4 chars
  183.                 .SetBorders(gxBorderAll,
  184.                     CGXPen()
  185.                         .SetColor(RGB(192,192,192)) )   // grey border
  186.                 .SetHorizontalAlignment(DT_CENTER));
  187.  
  188.         // Automatically adapt row heights to the previous block of cell
  189.         ResizeRowHeightsToFit( CGXRange(8,2,12,3) );
  190.  
  191.         // apply a style to rows
  192.         SetStyleRange(CGXRange().SetRows(7, 13),
  193.             CGXStyle()
  194.                 .SetAutoSize(TRUE)              // automatically resize cells when user enters text
  195.                 .SetInterior(RGB(192,192,192))  // grey background
  196.             );
  197.  
  198.         // Instructions
  199.         SetCoveredCellsRowCol(1, 1, 3, 5);
  200.         SetStyleRange(CGXRange(1, 1),
  201.             CGXStyle()
  202.                 .SetWrapText(TRUE)              // wrap text
  203.                 .SetEnabled(FALSE)              // inhibit usage as current cell
  204.                 .SetFont(
  205.                     CGXFont()
  206.                         .SetFaceName(_T("Times New Roman")) )
  207.                 .SetInterior(RGB(255,251,240))   // Off-white
  208.                 .SetHorizontalAlignment(DT_CENTER)
  209.                 .SetVerticalAlignment(DT_VCENTER)
  210.                 .SetControl(GX_IDS_CTRL_STATIC) // Static Text
  211.                 .SetBorders(gxBorderAll,
  212.                     CGXPen()
  213.                         .SetWidth(2))           // black border, 2-pixels thick
  214.                 .SetValue(szInstruct));
  215.  
  216.         // Enable ceration of undo-information for user interactions
  217.         GetParam()->EnableUndo(TRUE);
  218.  
  219.         // Draw border in current cell
  220.         CGXProperties* pProp = GetParam()->GetProperties();
  221.         pProp->SetUserProperty(GX_IDS_OUTLINECURRENTCELL,
  222.              (CGXStyle)(pProp->sInvertDrawBorder)); // need cast to compile
  223.     }
  224.  
  225.     // Position the current cell
  226.  
  227.     SetCurrentCell(4, 1, FALSE /* avoid immediate updating */);
  228.  
  229.     // Enable Update-Hint-Mechanism
  230.     // You should put this line as last command into OnInitialUpdate,
  231.     // becaus as long as EnableHints is not called, the modified flag
  232.     // of the document will not be changed.
  233.  
  234.     EnableHints();
  235. }
  236.  
  237. /////////////////////////////////////////////////////////////////////////////
  238. // CGridSampleView diagnostics
  239.  
  240. #ifdef _DEBUG
  241. void CGridSampleView::AssertValid() const
  242. {
  243.     CMyGridView::AssertValid();
  244. }
  245.  
  246. void CGridSampleView::Dump(CDumpContext& dc) const
  247. {
  248.     CMyGridView::Dump(dc);
  249. }
  250.  
  251. CGridSampleDoc* CGridSampleView::GetDocument() // non-debug version is inline
  252. {
  253.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGridSampleDoc)));
  254.     return (CGridSampleDoc*) m_pDocument;
  255. }
  256.  
  257. #endif //_DEBUG
  258.  
  259.  
  260. /////////////////////////////////////////////////////////////////////////////
  261. // CGridSampleView message handlers
  262.  
  263. // Menu handler for View->Splitter View
  264.  
  265. void CGridSampleView::OnViewSplitterview()
  266. {
  267.     // Creates a new CSplitterMDIChildWnd frame with a CGridSampleView
  268.     // and initializes it with the same view id.
  269.  
  270.     CDocument* pDoc = GetDocument();
  271.  
  272.     CMyMultiDocTemplate* pTemplate
  273.         = (CMyMultiDocTemplate*) ((CGridSampleApp*) AfxGetApp())->m_pSplitterTemplate;
  274.  
  275.     pTemplate->SetViewClass(GetRuntimeClass());
  276.  
  277.     CMDIChildWnd* pNewFrame
  278.         = (CMDIChildWnd*) pTemplate->CreateNewFrame(GetDocument(), NULL);
  279.  
  280.     if (pNewFrame == NULL)
  281.         return;     // not created
  282.  
  283.     ASSERT(pNewFrame->IsKindOf(RUNTIME_CLASS(CSplitterMDIChildWnd)));
  284.  
  285.     CSplitterWnd& splitter = (CSplitterWnd&)
  286.         ((CSplitterMDIChildWnd *) pNewFrame)->m_wndSplitter;
  287.  
  288.     CGridSampleView* pView = (CGridSampleView*)
  289.         splitter.GetPane(0, 0);
  290.  
  291.     // Set view id to active tab view id
  292.     pView->m_nViewID = m_nViewID;
  293.  
  294.     pTemplate->InitialUpdateFrame(pNewFrame, pDoc);
  295.  
  296.     pNewFrame->GetActiveView();
  297.     ASSERT(pView);
  298. }
  299.  
  300. // Menu handler for View->User Actions...
  301.  
  302. void CGridSampleView::OnViewUseractions()
  303. {
  304. /*
  305. //  just some performance checking
  306.  
  307.     ((CMDIChildWnd*) GetParentFrame())->MDIMaximize();
  308.  
  309.     OnViewZoomin();
  310.     OnViewZoomin();
  311.     OnViewZoomin();
  312.  
  313.     OnViewZoomout();
  314.     OnViewZoomout();
  315.     OnViewZoomout();
  316.     OnViewZoomout();
  317.     OnViewZoomout();
  318.  
  319.     OnViewZoomin();
  320.     OnViewZoomin();
  321.     OnViewZoomin();
  322.     OnViewZoomin();
  323.     OnViewZoomin();
  324.  
  325.     OnViewZoomout();
  326.     OnViewZoomout();
  327.     OnViewZoomout();
  328.  
  329.     ProcessKeys(this, WM_CHAR, 20, 1, 0);
  330.     return;
  331. */
  332.  
  333.     // Shows a dialog with some attributes of the parameter-object
  334.     // where you can experiment with some attributes
  335.     // such as allowing the user to track columns, select cells
  336.     // or use the grid as a listbox.
  337.  
  338.     // Transfer Current Cell's Data to grid
  339.     if (!TransferCurrentCell())
  340.         return;
  341.  
  342.     CUserActionsDialog dlg(GetParam());
  343.  
  344.     if (dlg.DoModal() == IDOK)
  345.     {
  346.         // Redraw the grid
  347.         Redraw();
  348.     }
  349. }
  350.  
  351.