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

  1. // gridsvw2.cpp : implementation of the CGridSample2View 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 "gridsvw2.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. // CGridSample2View
  31. //
  32. // CGridSample2View can be used as standalone, splitter or worksheet gridview
  33. // as you have already seen with CGridSample2View
  34. //
  35. // CGridSample2View illustrates how to apply cell formattings
  36. //
  37.  
  38. IMPLEMENT_DYNCREATE(CGridSample2View, CMyGridView)
  39.  
  40. static TCHAR BASED_CODE szInstruct[] =
  41.         _T("This view shows some simple cell formattings. ")
  42.         _T("You can call Format->Cells to change the formattings for the selected range of cells.");
  43.  
  44. #define new DEBUG_NEW
  45.  
  46. BEGIN_MESSAGE_MAP(CGridSample2View, CMyGridView)
  47.     //{{AFX_MSG_MAP(CGridSample2View)
  48.     ON_COMMAND(ID_VIEW_USERACTIONS, OnViewUseractions)
  49.     ON_COMMAND(ID_VIEW_SPLITTERVIEW, OnViewSplitterview)
  50.     //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CGridSample2View construction/destruction
  55.  
  56. CGridSample2View::CGridSample2View()
  57. {
  58.     // TODO: add construction code here
  59. }
  60.  
  61. CGridSample2View::~CGridSample2View()
  62. {
  63. }
  64.  
  65. BOOL CGridSample2View::ConnectParam()
  66. {
  67.     // Note: this method is copied from CGridSampleView
  68.     //
  69.  
  70.     BOOL bNew = FALSE;
  71.  
  72.     // Retrive the zero-based worksheet-id if used as worksheet
  73.     if (GetParentTabWnd(this, TRUE))
  74.         m_nViewID = GetParentTabViewID(this);
  75.  
  76.     // check if it is a new pane in a splitter window
  77.     CSplitterWnd* pSplitterWnd = GetParentDynamicSplitter(this, TRUE);
  78.     if (pSplitterWnd != NULL)
  79.     {
  80.         CGXGridView *pView = (CGXGridView *) pSplitterWnd->GetPane(0, 0);
  81.         if (pView != this)
  82.             m_nViewID = pView->GetViewID();
  83.     }
  84.  
  85.     // check if parameter-object exist (in document)
  86.     if (GetDocument()->GetParam(m_nViewID) == NULL)
  87.     {
  88.         // create a parameter-object on the heap
  89.         GetDocument()->SetParam(m_nViewID, new CGXGridParam);
  90.  
  91.         bNew = TRUE;    // this view needs initializing
  92.     }
  93.  
  94.     // connect parameter-object with grid
  95.     SetParam((CGXGridParam*) GetDocument()->GetParam(m_nViewID), FALSE);
  96.  
  97.     return bNew;
  98. }
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CGridSample2View drawing
  102.  
  103. void CGridSample2View::OnInitialUpdate()
  104. {
  105.     BOOL bNew = ConnectParam();
  106.  
  107.     CMyGridView::OnInitialUpdate(); // Creates all objects and links them to the grid
  108.  
  109.     // Now, all objects are connected:
  110.     ASSERT(GetParam() != NULL);                     // parameter object
  111.     ASSERT(GetParam()->GetProperties() != NULL);    // properties object (with header-/footer, colors,...)
  112.     ASSERT(GetParam()->GetStylesMap() != NULL);     // stylesmap object (with base styles)
  113.     ASSERT(GetParam()->GetData() != NULL);          // data object (with cell's data)
  114.     ASSERT(GetParam()->GetPrintDevice() != NULL);   // printdevice object (with printer settings)
  115.  
  116.     // ... and you can execute commands on the grid
  117.  
  118.     if (bNew)
  119.     {
  120.         // Don't create undo-information for the following commands
  121.         GetParam()->EnableUndo(FALSE);
  122.         // (at the end of this procedure, I will reenable it)
  123.  
  124.         // Number of rows and columns
  125.         SetRowCount(100);
  126.         SetColCount(20);
  127.  
  128.         // You can create one big cell which spans several other cells
  129.         SetCoveredCellsRowCol(5,2, 10, 4);
  130.  
  131.         // ... and apply a big bold "times new roman" font and a border
  132.         SetStyleRange(
  133.             CGXRange(5,2),
  134.             CGXStyle()
  135.                 .SetValue(_T("Welcome!"))
  136.                 .SetWrapText(FALSE)         // use single line edit control: wrap text = FALSE and allow enter = FALSE
  137.                 .SetAllowEnter(FALSE)
  138.                 .SetHorizontalAlignment(DT_CENTER)
  139.                 .SetVerticalAlignment(DT_VCENTER)
  140.                 .SetInterior(CGXBrush().SetPattern(5))
  141.                 .SetDraw3dFrame(gxFrameRaised)
  142.                 // ... a big bold "times new roman" font
  143.                 .SetFont(
  144.                     CGXFont()
  145.                         .SetSize(24)
  146.                         .SetBold(TRUE)
  147.                         .SetFaceName(_T("Times New Roman")))
  148.                 // ... and a blue border
  149.                 .SetBorders(gxBorderAll, CGXPen().SetWidth(2).SetColor(RGB(0,0,255)))
  150.             );
  151.  
  152.         // You can apply formattings to all cells of a row or column
  153.  
  154.         // ... set rows strike-out
  155.         SetStyleRange(
  156.             CGXRange().SetRows(11, 15),     // Rows 9 to 13
  157.             CGXStyle()
  158.                 .SetValue(_T("YYY"))
  159.                 .SetFont( CGXFont().SetStrikeOut(TRUE) )
  160.             );
  161.  
  162.         // ... and columns light blue
  163.         SetStyleRange(
  164.             CGXRange().SetCols(5,6),        // Columns 5 to 6
  165.             CGXStyle()
  166.                 .SetValue(_T("XXX"))
  167.                 .SetInterior(RGB(166,202,240))   // Light blue
  168.             );
  169.  
  170.         // Take a look at the cell  11,5: It combines both the formattings
  171.         // of the row and the columns. They are Light Blue and StrikeOut
  172.  
  173.         // Turn off StrikeOut for cells 13,5 to 13,6
  174.         SetStyleRange(
  175.             CGXRange().SetCells(13,5,13,6),     // equal to CGXRange(19,8,20,9)
  176.             CGXStyle()
  177.                 .SetFont( CGXFont().SetStrikeOut(FALSE) )
  178.             );
  179.  
  180.         // Add a selection
  181.         POSITION area = GetParam()->GetRangeList()->AddTail(new CGXRange);
  182.         SetSelection(area, 13, 2, 18, 5);
  183.  
  184.         SetStyleRange(CGXRange().SetCols(1), CGXStyle()
  185.             .SetUserAttribute(GX_IDS_UA_TOOLTIPTEXT, _T("Column 1")));
  186.  
  187.         SetStyleRange(CGXRange().SetCols(2), CGXStyle()
  188.             .SetUserAttribute(GX_IDS_UA_TOOLTIPTEXT, _T("Column 2")));
  189.  
  190.         SetStyleRange(CGXRange().SetCols(3), CGXStyle()
  191.             .SetUserAttribute(GX_IDS_UA_TOOLTIPTEXT, _T("Column 3")));
  192.  
  193.         // Composition of styles is a very powerful feature of Objective Grid/MFC.
  194.         // In CGridSample3View, I will show you how to create the base styles which the
  195.         // user can modify through the styles-dialog. (Menu: Format->Styles)
  196.  
  197.         // Instructions
  198.         SetCoveredCellsRowCol(1, 1, 3, 5);
  199.         SetStyleRange(CGXRange(1,1),
  200.             CGXStyle()
  201.                 .SetWrapText(TRUE)
  202.                 .SetEnabled(FALSE)
  203.                 .SetFont(CGXFont().SetFaceName(_T("Times New Roman")))
  204.                 .SetInterior(RGB(255,251,240))   // Off-white
  205.                 .SetHorizontalAlignment(DT_CENTER)
  206.                 .SetVerticalAlignment(DT_VCENTER)
  207.                 .SetControl(GX_IDS_CTRL_STATIC)
  208.                 .SetBorders(gxBorderAll, CGXPen().SetWidth(2))
  209.                 .SetValue(szInstruct));
  210.  
  211.         // Enable ceration of undo-information for user interactions
  212.         GetParam()->EnableUndo(TRUE);
  213.     }
  214.  
  215.     // Position the current cell
  216.  
  217.     SetCurrentCell(4, 1, FALSE /* avoid immediate updating */);
  218.  
  219.     // Enable Update-Hint-Mechanism
  220.  
  221.     EnableHints();
  222. }
  223.  
  224. /////////////////////////////////////////////////////////////////////////////
  225. // CGridSample2View diagnostics
  226.  
  227. #ifdef _DEBUG
  228. void CGridSample2View::AssertValid() const
  229. {
  230.     CMyGridView::AssertValid();
  231. }
  232.  
  233. void CGridSample2View::Dump(CDumpContext& dc) const
  234. {
  235.     CMyGridView::Dump(dc);
  236. }
  237.  
  238. CGridSampleDoc* CGridSample2View::GetDocument() // non-debug version is inline
  239. {
  240.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGridSampleDoc)));
  241.     return (CGridSampleDoc*) m_pDocument;
  242. }
  243.  
  244. #endif //_DEBUG
  245.  
  246.  
  247. /////////////////////////////////////////////////////////////////////////////
  248. // CGridSample2View message handlers
  249.  
  250. // Menu handler for View->Splitter View
  251.  
  252. void CGridSample2View::OnViewSplitterview()
  253. {
  254.     CDocument* pDoc = GetDocument();
  255.  
  256.     CMyMultiDocTemplate* pTemplate
  257.         = (CMyMultiDocTemplate*) ((CGridSampleApp*) AfxGetApp())->m_pSplitterTemplate;
  258.  
  259.     pTemplate->SetViewClass(GetRuntimeClass());
  260.  
  261.     CMDIChildWnd* pNewFrame
  262.         = (CMDIChildWnd*) pTemplate->CreateNewFrame(GetDocument(), NULL);
  263.  
  264.     if (pNewFrame == NULL)
  265.         return;     // not created
  266.  
  267.     ASSERT(pNewFrame->IsKindOf(RUNTIME_CLASS(CSplitterMDIChildWnd)));
  268.  
  269.     CSplitterWnd& splitter = (CSplitterWnd&)
  270.         ((CSplitterMDIChildWnd *) pNewFrame)->m_wndSplitter;
  271.  
  272.     CGridSample2View* pView = (CGridSample2View*)
  273.         splitter.GetPane(0, 0);
  274.  
  275.     // Set view id to active tab view id
  276.     pView->m_nViewID = m_nViewID;
  277.  
  278.     pTemplate->InitialUpdateFrame(pNewFrame, pDoc);
  279.  
  280.     pNewFrame->GetActiveView();
  281.     ASSERT(pView);
  282. }
  283.  
  284. // Menu handler for View->User Actions...
  285.  
  286. void CGridSample2View::OnViewUseractions()
  287. {
  288.     // Note: this method is copied from CGridSampleView
  289.     //
  290.     // Shows a dialog with some attributes of the parameter-object
  291.     // where you can experiment with some attributes
  292.     // such as allowing the user to track columns, select cells
  293.     // or use the grid as a listbox.
  294.  
  295.     // Transfer Current Cell's Data to grid
  296.     if (!TransferCurrentCell())
  297.         return;
  298.  
  299.     CUserActionsDialog dlg(GetParam());
  300.  
  301.     if (dlg.DoModal() == IDOK)
  302.     {
  303.         // Redraw the grid
  304.         Redraw();
  305.     }
  306. }
  307.  
  308.