Objective Grid : PART II Programmer’s Guide : Chapter 20 Page Break and Scaled Printing Support : Adding Page Break and Scaled Printing Support
Adding Page Break and Scaled Printing Support
You can add a page break and/or scaled printing support to an Objective Grid-based application quickly and easily.
1. After you create an Objective Grid application, add a pointer to the CGXPageBreakPlugin class variable in your view class. Ensure that your view class is derived from the CGXGridView class.
 
class CPageBreakUIView : public CGXGridView
{
public:
CGXPageBreakPlugin* m_pPlugin;
2. Create the plugin in the constructor for your view class.
 
CPageBreakUIView::CPageBreakUIView()
{
m_pPlugin = new CGXPageBreakPlugin;
3. Destroy the plugin in the destructor method of the view:
 
CPageBreakUIView::~CPageBreakUIView()
{
delete m_pPlugin;
}
4. Call the Plugin() method of the plugin during the OnInitialUpdate() method of the view, before any other method calls:
 
void CPageBreakUIView::OnInitialUpdate()
{
if(m_pPlugin != NULL)
m_pPlugin->Plugin(this);
...
 
}
5. Override the OnBeginPrinting() method of your view class. In the overriding method, you need to call the DoBeginPrinting() method and then call CGXGridView::OnBeginPrinting() if DoBeginPrinting() returns FALSE. The DoBeginPrinting() method prepares the mapping mode for the given pDC. After you edit this method, the device context is ready for use in printing or in the print preview.
 
void CPageBreakUIView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
BOOL bRet = FALSE;
if( m_pPlugin != NULL )
bRet = m_pPlugin->DoBeginPrinting(pDC, pInfo);
if( !bRet )
CGXGridView::OnBeginPrinting(pDC, pInfo);
}
6. Override the OnGridPrepareDC() method of CGXGridView class in your view class. In this method, you need to delegate the call to the DoPrepareDC() in our plugin.
 
void CPageBreakUIView::OnGridPrepareDC(CDC* pDC,
CPrintInfo* pInfo)
{
if( m_pPlugin != NULL )
m_pPlugin->DoPrepareDC(pDC, pInfo);
else
CGXGridView::OnGridPrepareDC(pDC, pInfo);
}
7. Override the WindowProc() method of your view class. Generally, this is how you add a plugin.
 
LRESULT CPageBreakUIView::WindowProc(UINT message,
WPARAM wParam, LPARAM lParam)
{
LRESULT lResult;
if(m_pPlugin != NULL)
{
m_pPlugin->HandleMessage(message, wParam,
lParam, &lResult);
if( m_pPlugin->m_bExitMessage )
return lResult;
}
return CGXGridView::WindowProc(message, wParam, lParam);
}
8. In the OnDraw() method of your view class, add plugin drawing code similar to the following code. Generally, you should draw the page break line last, so the DoDraw() method of the plugin is called last.
 
void CPageBreakUIView::OnDraw(CDC* pDC)
{
CGXGridView::OnDraw(pDC);
if( m_pPlugin != NULL )
m_pPlugin->DoDraw(pDC);
}
9. You may need to add a menu item in your project to show or hide the page break lines.
 
void CPageBreakUIView::OnChangePageBreakMode()
{
if (m_pPlugin != NULL )
{
m_bIsPageBreakMode = !m_bIsPageBreakMode;
m_pPlugin->SetPageBreakMode(m_bIsPageBreakMode);
}
}
10. For scaled printing, you could provide a dialog to your users where they can specify their desired page break dimensions. You could then use the SetPageGrid API to scale the grid to the specified dimensions.
 
void CPageBreakUIView::OnFitToPages()
{
if( m_pPlugin !=NULL )
{
CPageConfigDlg dlg;
dlg.m_bFitToPages = m_pPlugin->m_bFitToPages;
dlg.m_nColPages = m_pPlugin->m_nColPages;
dlg.m_nRowPages = m_pPlugin->m_nRowPages;
if( dlg.DoModal() == IDOK )
m_pPlugin->SetPageGrid(dlg.m_nRowPages,
dlg.m_nColPages, dlg.m_bFitToPages);
}
}
11. To change the color of the page label text and page break lines, you can assign new COLORREF values to the following public members of CGXPageBreakPlugin:
m_clrLabel (Color for the page label text.)
m_clrDefaultBreakLine (Color for default break lines.)
m_clrCustomBreakLine (Color for custom break lines.)
12. To change the font, size, or visibility of the page label text, override CGXPageBreakPlugin::DrawPageBreak(). Currently, the font is hardcoded and the visibility of the page labels and page break lines are toggled simultaneously when CGXPageBreakPlugin::SetPageBreakMode() is called.
13. For scaled printing, you could provide a dialog to your users where they can specify their desired page break dimensions. You could then use the SetPageGrid API to scale the grid to the specified dimensions.
 
void CPageBreakUIView::OnFitToPages()
{
if( m_pPlugin != NULL )
{
CPageConfigDlg dlg;
dlg.m_bFitToPages = m_pPlugin->m_bFitToPages;
dlg.m_nColPages = m_pPlugin->m_nColPages;
dlg.m_nRowPages = m_pPlugin->m_nRowPages;
if( dlg.DoModal() == IDOK )
m_pPlugin->SetPageGrid(dlg.m_nRowPages,
dlg.m_nColPages, dlg.m_bFitToPages);
}
}