Objective Grid stores all document-specific attributes in the parameter object. When you store the parameter object in the document, all grid views connected to the document can access the same data. In OnInitialUpdate() you will see how to pass a pointer to the parameter object to the grid.
This step adds a CGXParam object to the application's CDocument class. This enables all grid views to share the same parameter object.
Open 1stgrdoc.h
Add a public member variable to the attributes section of C1stGridDoc:
CGXGridParam* m_pParam; |
It is good practice to add clean-up code in OnNewDocument(). This makes it easier to use the document in a single document interface (SDI) application later.
Open 1stgrdoc.cpp
Replace the C1stGridDoc constructor with the following version:
C1stGridDoc::C1stGridDoc() { m_pParam = NULL; } |
Replace the C1stGridDoc destructor with the following version:
C1stGridDoc::~C1stGridDoc() { delete m_pParam; } |
Change the C1stGridDoc::Serialize() method as follows:
void C1stGridDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // Store parameter-object ar << m_pParam; } else { // Create parameter-object ar >> m_pParam; } } |
Modify C1stGridDoc::OnNewDocument() as follows:
BOOL C1stGridDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // Clean up parameter-object delete m_pParam; m_pParam = NULL; return TRUE; } |
In your OnInitialUpdate() method you need to pass the pointer to the parameter object to the grid object. If the first view for a document is opened, the parameter object must be allocated and the grid data initialized. If the end user opens additional views for the same document, no initialization is necessary. The grid data is already available in the parameter object.
bFirstView = TRUE indicates that this is the first view connected to the document and therefore the data needs to be initialized.
Next, construct the parameter object and pass the pointer to the grid view.
Your existing initialization code should be bracketed with an if statement where you should check if bFirstView is TRUE.
Add the following code to the beginning of C1stGridView::OnInitialUpdate():
BOOL bFirstView = FALSE; if (GetDocument()->m_pParam == NULL) { bFirstView = TRUE; GetDocument()->m_pParam = new CGXGridParam; } SetParam(GetDocument()->m_pParam, FALSE); |
Enclose the existing initialization code with an if statement that checks for bFirstView = TRUE:
if (bFirstView) { // ... (Existing init code) } |
Please refer to CMy1stGridView::OnInitialUpdate() in <stingray-installdir>\Samples\Grid\Tutorial\1stGrid\Step3 for a complete listing of these changes.
At this point, you can compile and run with these modifications. You can now serialize data into a document and open new views for the same data using Window|New Window. In the next section we will add support for splitting the view.
Next, override the OnCreateClient() method:
Select View|Class View.
Right click on 1stGrid and select Add, then Class.
The Add Class dialog box appears.
Select MFC in the Templates section and MFC Class in the Categories section. Then click the Add button.
The MFC Add Class Wizard appears.
In the MFC Add Class Wizard, enter CSplitterMDIChildWnd for the Name.
Specify CMDIChildWnd as the Base class.
Click Finish on the MFC Add Class Wizard dialog.
In the Class View, right click on the newly generated class and select Properties.
Click on the Overrides icon.
Select the OnCreateClient event.
Select <Add> OnCreateClient.
The function OnCreateClient(...) is added to CSplitterMDIChildWnd.
Edit the code in the file SplitterMDIChildWnd.cpp, changing OnCreateClient(…) as follows:
BOOL CSplitterMDIChildWnd::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext) { return m_wndSplitter.Create(this, 2, 2, CSize(10, 10), pContext); } |
Open SplitterMDIChildWnd.h.
Add a member variable to the attributes section of CSplitterMdiChildWnd:
CSplitterWnd m_wndSplitter; |
Close and save SplitterMDIChildWnd.h and SplitterMDIChildWnd.cpp.
Open 1stgrid.cpp.
After #include "1stgrvw.h", add:
#include "SplitterMDIChildWnd.h" |
Locate:
pDocTemplate = new CMultiDocTemplate( IDR_1STGRITYPE, RUNTIME_CLASS(C1stGridDoc), RUNTIME_CLASS(CMDIChildWnd),// standard MDI child frame RUNTIME_CLASS(C1stGridView)); AddDocTemplate(pDocTemplate); |
and replace CMDIChildWnd with CSplitterMDIChildWnd:
CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( IDR_1STGRITYPE, RUNTIME_CLASS(C1stGridDoc), RUNTIME_CLASS(CSplitterMDIChildWnd), // splitter child frame RUNTIME_CLASS(C1stGridView)); AddDocTemplate(pDocTemplate); |
At this point, you can compile and run the 1stGrid application.
Compile the application and run it.
Congratulations on completing the 1stGrid tutorial! For more information, try these other tutorials:
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.