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

  1. // gridsvw3.cpp : implementation of the CGridSample3View 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 "gridsvw3.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. // CGridSample3View
  31. //
  32. // Composition of styles is a very powerful feature of Objective Grid/MFC.
  33. // CGridSample3View shows you how to create and modify base styles which the
  34. // user can modify through the styles-dialog. (Menu: Format->Styles)
  35. // and change some properties, parameters
  36. //
  37. // CGridSample3View also shows you how to inhibit dragging columns
  38. // depending on context (e.g. not to move unfrozen columns to frozen colums).
  39. // See OnSelDragColsStart and OnSelDragColsMove.
  40. //
  41. // Finally, CGridSample3View illustrates how to freeze rows or columns,
  42. // use several rows or columns for headers and the possibility to use
  43. // SetCoveredCellsRowCol with Column headers.
  44. //
  45.  
  46. IMPLEMENT_DYNCREATE(CGridSample3View, CMyGridView)
  47.  
  48. static TCHAR BASED_CODE szErrorStyle[] = _T("Error Style");
  49. static TCHAR BASED_CODE szComboStyle[] = _T("Combo Style");
  50.  
  51. static TCHAR BASED_CODE szInstruct[] =
  52.         _T("This view shows the usage of base styles and the possibility to use ")
  53.         _T("several rows or columns as headers and to freeze columns. ")
  54.         _T("You can call Format->Styles to add or modify base styles.");
  55.  
  56.  
  57. #define new DEBUG_NEW
  58.  
  59. BEGIN_MESSAGE_MAP(CGridSample3View, CMyGridView)
  60.     //{{AFX_MSG_MAP(CGridSample3View)
  61.     ON_COMMAND(ID_VIEW_USERACTIONS, OnViewUseractions)
  62.     ON_COMMAND(ID_VIEW_SPLITTERVIEW, OnViewSplitterview)
  63.     //}}AFX_MSG_MAP
  64. END_MESSAGE_MAP()
  65.  
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CGridSample3View construction/destruction
  68.  
  69. CGridSample3View::CGridSample3View()
  70. {
  71.     // TODO: add construction code here
  72. }
  73.  
  74. CGridSample3View::~CGridSample3View()
  75. {
  76. }
  77.  
  78. BOOL CGridSample3View::ConnectParam()
  79. {
  80.     // Note: this method is copied from CGridSampleView
  81.     //
  82.  
  83.     BOOL bNew = FALSE;
  84.  
  85.     // Retrive the zero-based worksheet-id if used as worksheet
  86.     if (GetParentTabWnd(this, TRUE))
  87.         m_nViewID = GetParentTabViewID(this);
  88.  
  89.     // check if it is a new pane in a splitter window
  90.     CSplitterWnd* pSplitterWnd = GetParentDynamicSplitter(this, TRUE);
  91.     if (pSplitterWnd != NULL)
  92.     {
  93.         CGXGridView *pView = (CGXGridView *) pSplitterWnd->GetPane(0, 0);
  94.         if (pView != this)
  95.             m_nViewID = pView->GetViewID();
  96.     }
  97.  
  98.     // check if parameter-object exist (in document)
  99.     if (GetDocument()->GetParam(m_nViewID) == NULL)
  100.     {
  101.         // create a parameter-object on the heap
  102.         GetDocument()->SetParam(m_nViewID, new CGXGridParam);
  103.  
  104.         bNew = TRUE;    // this view needs initializing
  105.     }
  106.  
  107.     // connect parameter-object with grid
  108.     SetParam((CGXGridParam*) GetDocument()->GetParam(m_nViewID), FALSE);
  109.  
  110.     return bNew;
  111. }
  112.  
  113. void CGridSample3View::SetupBaseStyles()
  114. {
  115.     ASSERT(GetParam()->GetStylesMap() == NULL);
  116.     // ASSERTION-> a stylesmap is already connected to parameter-object ->END
  117.  
  118.     if (GetParam()->GetStylesMap() != NULL)
  119.         return;
  120.  
  121.     // create a stylesmap and connect it with the parameter-object
  122.     CGXStylesMap* pStyMap;
  123.  
  124.     GetParam()->SetStylesMap(pStyMap = new CGXStylesMap);
  125.  
  126.     // create standard styles
  127.     pStyMap->CreateStandardStyles();
  128.  
  129.     // Add some base styles
  130.  
  131.     // "Combo Style" - A style with a combo box and choice list
  132.     pStyMap->RegisterStyle(szComboStyle,
  133.             CGXStyle()
  134.                 .SetControl(GX_IDS_CTRL_CBS_DROPDOWNLIST)
  135.                 .SetChoiceList(_T("one\r\ntwo\r\nthree\r\nfour\r\nfive\r\nsix\r\n")),
  136.             TRUE    // system-style (non removable)
  137.         );
  138.  
  139.     // "Error Style" - Yellow text on red backcolor with a bold font
  140.     pStyMap->RegisterStyle(szErrorStyle,
  141.             CGXStyle()
  142.                 .SetTextColor(RGB(255,255,0))   // yellow
  143.                 .SetInterior(RGB(255,0,0))      // red
  144.                 .SetFont(
  145.                     CGXFont()
  146.                         .SetBold(TRUE)
  147.                     ),
  148.             TRUE    // system-style (non removable)
  149.         );
  150.  
  151.     // I do also want to adapt the base styles:
  152.  
  153.     // StandardStyle() calls pStyMap->LookupStyle(pStyMap->m_wStyleStandard, ...)
  154.     // and returns a reference to the Standard-Style.
  155.     //
  156.     // So, I can simply adapt this style's settings to my needs.
  157.  
  158.     StandardStyle()
  159.         .SetFont( CGXFont()
  160.             .SetFaceName(_T("Times New Roman"))
  161.             .SetSize(9) );
  162.  
  163.     RowHeaderStyle()
  164.         .SetTextColor(RGB(0,0,255) );
  165.  
  166.     // Now, read the styles from profile.
  167.     // Note that reading style from profile can override the previous settings.
  168.     // The previous settings are standard settings.
  169.  
  170.     pStyMap->SetSection(_T("My Base Styles"));  // extra profile section
  171.  
  172.     pStyMap->ReadProfile();
  173. }
  174.  
  175.  
  176. /////////////////////////////////////////////////////////////////////////////
  177. // CGridSample3View drawing
  178.  
  179. void CGridSample3View::OnInitialUpdate()
  180. {
  181.     BOOL bNew = ConnectParam();
  182.  
  183.     if (bNew)
  184.         SetupBaseStyles();  // Setup base styles and read them from profile
  185.  
  186.     CMyGridView::OnInitialUpdate(); // Creates all objects and links them to the grid
  187.  
  188.     // check out identifiers for the styles previously registered with RegisterStyle
  189.     m_wStyleCombo = GetParam()->GetStylesMap()->GetBaseStyleId(szComboStyle);
  190.     m_wStyleError = GetParam()->GetStylesMap()->GetBaseStyleId(szErrorStyle);
  191.  
  192.     // you could use these identifiers later if you want to change the style
  193.     // e.g.
  194.     //     BaseStyle(m_wStyleCombo)
  195.     //         .SetChoiceList(_T("one\ntwo\nthree"));
  196.     //
  197.     // where BaseStyle(id) is a member function of CGXGridCore which returns a
  198.     // reference to the specified base-style.
  199.     //
  200.     // You need this identifier if you want to apply this base-style to cells
  201.     // e.g.
  202.     //  SetStyleRange(
  203.     //      CGXRange(5,2,8,3),
  204.     //      CGXStyle()
  205.     //          .SetBaseStyle(m_wStyleCombo)
  206.     //      );
  207.  
  208.     if (bNew)
  209.     {
  210.         // Don't create undo-information for the following commands
  211.         GetParam()->EnableUndo(FALSE);
  212.         // (at the end of this procedure, I will reenable it)
  213.  
  214.         // Number of rows and columns
  215.         SetRowCount(100);
  216.         SetColCount(20);
  217.  
  218.         // Now you can apply base styles to cells
  219.  
  220.         SetStyleRange(
  221.             CGXRange(5,2,8,3),
  222.             CGXStyle()
  223.                 .SetBaseStyle(m_wStyleCombo)
  224.                 .SetValue(_T("one"))
  225.             );
  226.  
  227.         // Optimize row heights
  228.         ResizeRowHeightsToFit(CGXRange(5,2,8,2));
  229.  
  230.         SetStyleRange(
  231.             CGXRange(5,4,8,5),
  232.             CGXStyle()
  233.                 .SetBaseStyle(m_wStyleError)
  234.                 .SetValue(_T("ALERT!"))
  235.             );
  236.  
  237.         // ... and row
  238.         SetStyleRange(
  239.             CGXRange().SetRows(10,11),      // Rows 10 to 11
  240.             CGXStyle()
  241.                 .SetValue(szErrorStyle)
  242.                 .SetBaseStyle(m_wStyleError)
  243.             );
  244.  
  245.         // Instructions
  246.         SetCoveredCellsRowCol(1, 1, 3, 5);
  247.         SetStyleRange(CGXRange(1,1),
  248.             CGXStyle()
  249.                 .SetWrapText(TRUE)
  250.                 .SetEnabled(FALSE)
  251.                 .SetFont(CGXFont().SetFaceName(_T("Times New Roman")))
  252.                 .SetInterior(RGB(255,251,240))   // Off-white
  253.                 .SetHorizontalAlignment(DT_CENTER)
  254.                 .SetVerticalAlignment(DT_VCENTER)
  255.                 .SetControl(GX_IDS_CTRL_STATIC)
  256.                 .SetBorders(gxBorderAll, CGXPen().SetWidth(2))
  257.                 .SetValue(szInstruct));
  258.  
  259.         // freeze 3 columns, use 1 extra row as header ( + standard header at column 0)
  260.         SetFrozenCols(3, 1);
  261.  
  262.         // freeze 3 rows, use 3 extra columns as headers ( + standard header at row 0)
  263.         SetFrozenRows(3, 3);
  264.  
  265.         // Do not draw column headers pressed when moving the current cell
  266.         GetParam()->GetProperties()->SetMarkColHeader(FALSE);
  267.  
  268.         // Use covered cells even with row headers
  269.         SetCoveredCellsRowCol(5,0,7,0);
  270.  
  271.         SetStyleRange(CGXRange(5,0), _T("Covered Header"));
  272.  
  273.         // Enable ceration of undo-information for user interactions
  274.         GetParam()->EnableUndo(TRUE);
  275.     }
  276.  
  277.     // Position the current cell
  278.  
  279.     SetCurrentCell(4, 2, FALSE /* avoid immediate updating */);
  280.  
  281.     // Enable Update-Hint-Mechanism
  282.  
  283.     EnableHints();
  284. }
  285.  
  286. // Inhibit dragging frozen columns.
  287.  
  288. BOOL CGridSample3View::OnSelDragColsStart(ROWCOL nFirstCol, ROWCOL)
  289. {
  290.     return nFirstCol > GetFrozenCols();
  291.  
  292. }
  293.  
  294. // Inhibit dragging columns to nonfrozen columns.
  295. // The grid will display a "no drop"-cursor if OnSelDragColsMove returns FALSE.
  296.  
  297. BOOL CGridSample3View::OnSelDragColsMove(ROWCOL, ROWCOL, ROWCOL nDestCol)
  298. {
  299.     return nDestCol > GetFrozenCols();
  300. }
  301.  
  302. /////////////////////////////////////////////////////////////////////////////
  303. // CGridSample3View diagnostics
  304.  
  305. #ifdef _DEBUG
  306. void CGridSample3View::AssertValid() const
  307. {
  308.     CMyGridView::AssertValid();
  309. }
  310.  
  311. void CGridSample3View::Dump(CDumpContext& dc) const
  312. {
  313.     CMyGridView::Dump(dc);
  314. }
  315.  
  316. CGridSampleDoc* CGridSample3View::GetDocument() // non-debug version is inline
  317. {
  318.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGridSampleDoc)));
  319.     return (CGridSampleDoc*) m_pDocument;
  320. }
  321.  
  322. #endif //_DEBUG
  323.  
  324.  
  325. /////////////////////////////////////////////////////////////////////////////
  326. // CGridSample3View message handlers
  327.  
  328. // Menu handler for View->Splitter View
  329.  
  330. void CGridSample3View::OnViewSplitterview()
  331. {
  332.     CDocument* pDoc = GetDocument();
  333.  
  334.     CMyMultiDocTemplate* pTemplate
  335.         = (CMyMultiDocTemplate*) ((CGridSampleApp*) AfxGetApp())->m_pSplitterTemplate;
  336.  
  337.     pTemplate->SetViewClass(GetRuntimeClass());
  338.  
  339.     CMDIChildWnd* pNewFrame
  340.         = (CMDIChildWnd*) pTemplate->CreateNewFrame(GetDocument(), NULL);
  341.  
  342.     if (pNewFrame == NULL)
  343.         return;     // not created
  344.  
  345.     ASSERT(pNewFrame->IsKindOf(RUNTIME_CLASS(CSplitterMDIChildWnd)));
  346.  
  347.     CSplitterWnd& splitter = (CSplitterWnd&)
  348.         ((CSplitterMDIChildWnd *) pNewFrame)->m_wndSplitter;
  349.  
  350.     CGridSample3View* pView = (CGridSample3View*)
  351.         splitter.GetPane(0, 0);
  352.  
  353.     // Set view id to active tab view id
  354.     pView->m_nViewID = m_nViewID;
  355.  
  356.     pTemplate->InitialUpdateFrame(pNewFrame, pDoc);
  357.  
  358.     pNewFrame->GetActiveView();
  359.     ASSERT(pView);
  360. }
  361.  
  362. // Menu handler for View->User Actions...
  363.  
  364. void CGridSample3View::OnViewUseractions()
  365. {
  366.     // Note: this method is copied from CGridSampleView
  367.     //
  368.     // Shows a dialog with some attributes of the parameter-object
  369.     // where you can experiment with some attributes
  370.     // such as allowing the user to track columns, select cells
  371.     // or use the grid as a listbox.
  372.  
  373.     // Transfer Current Cell's Data to grid
  374.     if (!TransferCurrentCell())
  375.         return;
  376.  
  377.     CUserActionsDialog dlg(GetParam());
  378.  
  379.     if (dlg.DoModal() == IDOK)
  380.     {
  381.         // Redraw the grid
  382.         Redraw();
  383.     }
  384. }
  385.