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

  1. // formvw1.cpp : implementation file
  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. #include "formvw1.h"
  18.  
  19. #include "gridsdoc.h"
  20.  
  21. #ifndef _GXCTRL_H_
  22. #include "gxctrl.h"
  23. #endif
  24.  
  25. #ifndef _GXPRPDLG_H_
  26. #include "gxprpdlg.h"
  27. #endif
  28.  
  29. #ifndef _GXVW_H_
  30. #include "gxvw.h"
  31. #endif
  32.  
  33.  
  34. #ifdef _DEBUG
  35. #undef THIS_FILE
  36. static char BASED_CODE THIS_FILE[] = __FILE__;
  37. #endif
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CSample1FormGridWnd grid control
  41.  
  42. // validation routine
  43.  
  44. BOOL CSample1FormGridWnd::OnValidateCell(ROWCOL nRow, ROWCOL nCol)
  45. {
  46.     CString s;
  47.  
  48.     // retrieve text from current cell
  49.     CGXControl* pControl = GetControl(nRow, nCol);
  50.     pControl->GetCurrentText(s);
  51.  
  52.     if (_ttol(s) < 0 || _ttol(s) > 100)
  53.     {
  54.         SetWarningText(_T("Please enter a value between 0 and 100!"));
  55.         return FALSE;
  56.     }
  57.  
  58.     return TRUE;
  59. }
  60.  
  61. void CSample1FormGridWnd::OnInitialUpdate()
  62. {
  63.     CGXGridWnd::OnInitialUpdate();  // Creates all objects and links them to the grid
  64.  
  65.     GetParam()->EnableUndo(FALSE);
  66.  
  67.     SetRowCount(256);
  68.     SetColCount(52);
  69.  
  70.     SetStyleRange(CGXRange(2, 2), CGXStyle()
  71.             .SetControl(GX_IDS_CTRL_TEXTFIT)
  72.             .SetChoiceList(_T("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight"))
  73.         );
  74.  
  75.     GetParam()->EnableUndo(TRUE);
  76. }
  77.  
  78. BEGIN_MESSAGE_MAP(CSample1FormGridWnd, CGXGridWnd)
  79.     //{{AFX_MSG_MAP(CSample1FormGridWnd)
  80.     //}}AFX_MSG_MAP
  81. END_MESSAGE_MAP()
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CSample1FormView
  85.  
  86. IMPLEMENT_DYNCREATE(CSample1FormView, CFormView)
  87.  
  88. CSample1FormView::CSample1FormView()
  89.     : CFormView(CSample1FormView::IDD)
  90. {
  91.     //{{AFX_DATA_INIT(CSample1FormView)
  92.         // NOTE: the ClassWizard will add member initialization here
  93.     //}}AFX_DATA_INIT
  94. }
  95.  
  96. CSample1FormView::~CSample1FormView()
  97. {
  98. }
  99.  
  100. void CSample1FormView::DoDataExchange(CDataExchange* pDX)
  101. {
  102.     CFormView::DoDataExchange(pDX);
  103.     //{{AFX_DATA_MAP(CSample1FormView)
  104.         // NOTE: the ClassWizard will add DDX and DDV calls here
  105.     //}}AFX_DATA_MAP
  106.  
  107.     // validation routine for CGXGridWnd controls
  108.     DDV_GXGridWnd(pDX, &m_wndGrid);
  109. }
  110.  
  111.  
  112. BEGIN_MESSAGE_MAP(CSample1FormView, CFormView)
  113.     //{{AFX_MSG_MAP(CSample1FormView)
  114.     ON_COMMAND(ID_FILE_HEADERFOOTER, OnFileHeaderfooter)
  115.     ON_COMMAND(ID_FILE_PAGE_SETUP, OnFilePageSetup)
  116.     ON_COMMAND(ID_VIEW_PROPERTIES, OnViewProperties)
  117.     ON_COMMAND(ID_EDIT_FIND, OnEditFind)
  118.     ON_COMMAND(ID_EDIT_REPLACE, OnEditReplace)
  119.     ON_COMMAND(ID_EDIT_REPEAT, OnEditRepeat)
  120.     //}}AFX_MSG_MAP
  121.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  122.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  123. END_MESSAGE_MAP()
  124.  
  125.  
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CSample1FormView diagnostics
  129.  
  130. #ifdef _DEBUG
  131. void CSample1FormView::AssertValid() const
  132. {
  133.     CFormView::AssertValid();
  134. }
  135.  
  136. void CSample1FormView::Dump(CDumpContext& dc) const
  137. {
  138.     CFormView::Dump(dc);
  139. }
  140. #endif //_DEBUG
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // CSample1FormView message handlers
  144.  
  145. //
  146. // Initialization of the grid
  147. //
  148.  
  149. void CSample1FormView::OnInitialUpdate()
  150. {
  151.     CFormView::OnInitialUpdate();
  152.  
  153.     m_wndGrid.SubclassDlgItem(IDC_FORMVW1_GRIDWND, this);
  154.  
  155.     m_wndGrid.Initialize();
  156.  
  157.     m_wndGrid.SetFocus();
  158. }
  159.  
  160. // When a CFormView is deactivated, it stores a handle to the
  161. // control which did have the focus.  This can cause problems
  162. // when the current cell of the CGXGridWnd was active and the
  163. // CFormView is reactivated.  On Activation, CFormView passes
  164. // the focus directly to the current cell without notifying the
  165. // grid.
  166.  
  167. // The following methods ensure that the CFormView stores a
  168. // handle to the parent grid window when the form view is
  169. // deactivated and the current cell was active.
  170.  
  171. void CSample1FormView::OnActivateView(
  172.     BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
  173. {
  174.     if (SaveFocusControl())
  175.         return;     // don't call base class when focus is already set
  176.  
  177.     // Note: I do call the CView base class member instead
  178.     // of the CFormView member
  179.     CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  180. }
  181.  
  182. void CSample1FormView::OnActivateFrame(UINT nState, CFrameWnd* /*pFrameWnd*/)
  183. {
  184.     if (nState == WA_INACTIVE)
  185.         SaveFocusControl();     // save focus when frame loses activation
  186. }
  187.  
  188. BOOL CSample1FormView::SaveFocusControl()
  189. {
  190.     // save focus window if focus is on this window's controls
  191.     HWND hWndFocus = ::GetFocus();
  192.     if (hWndFocus != NULL && ::IsChild(m_hWnd, hWndFocus))
  193.     {
  194.         // if hWndFocus is a child of a CGXGridWnd, store
  195.         // the handle for the parent grid window
  196.  
  197.         CWnd* pWnd = CWnd::FromHandle(hWndFocus);
  198.         while (pWnd && !pWnd->IsKindOf(RUNTIME_CLASS(CGXGridWnd)))
  199.             pWnd = pWnd->GetParent();
  200.  
  201.         if (pWnd)
  202.             m_hWndFocus = pWnd->GetSafeHwnd();
  203.         else
  204.             m_hWndFocus = hWndFocus;
  205.  
  206.         return TRUE;
  207.     }
  208.     return FALSE;
  209. }
  210.  
  211. //
  212. // Support for printing the grid
  213. //
  214.  
  215. BOOL CSample1FormView::OnPreparePrinting(CPrintInfo* pInfo)
  216. {
  217.     pInfo->SetMaxPage(0xffff);
  218.  
  219.     pInfo->m_pPD->m_pd.Flags &= ~PD_NOSELECTION;
  220.     pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle();
  221.  
  222.     // default preparation
  223.     return DoPreparePrinting(pInfo);
  224. }
  225.  
  226. void CSample1FormView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
  227. {
  228.     m_wndGrid.OnBeginPrinting(pDC, pInfo);
  229. }
  230.  
  231. void CSample1FormView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
  232. {
  233.     m_wndGrid.OnEndPrinting(pDC, pInfo);
  234. }
  235.  
  236. void CSample1FormView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
  237. {
  238.     m_wndGrid.OnPrint(pDC, pInfo);
  239. }
  240.  
  241.  
  242. void CSample1FormView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo /* = NULL */)
  243. {
  244.     if (pInfo)
  245.     {
  246.         // Adjust printer DC
  247.         pDC->SetMapMode(MM_ANISOTROPIC);
  248.         pDC->SetWindowExt(GXGetFontState()->m_nLogPixelsX, GXGetFontState()->m_nLogPixelsY);
  249.  
  250.         int nlogx = pDC->GetDeviceCaps(LOGPIXELSX);
  251.         int nlogy = pDC->GetDeviceCaps(LOGPIXELSY);
  252.  
  253.         pDC->SetViewportExt(nlogx, nlogy);
  254.  
  255.         // Mapping: 72 pixels/unit (like MM_TEXT)
  256.     }
  257. }
  258.  
  259. /////////////////////////////////////////////////////////////////////////////
  260. // View menu handlers
  261.  
  262. void CSample1FormView::OnViewProperties()
  263. {
  264.     BOOL bSaveDefault = FALSE;
  265.  
  266.     CGXProperties* pPropertyObj = m_wndGrid.GetParam()->GetProperties();
  267.     ASSERT(pPropertyObj->IsKindOf(RUNTIME_CLASS(CGXProperties)));
  268.  
  269.     CGXProperties* pNewSettings = new CGXProperties;
  270.     *pNewSettings = *pPropertyObj;
  271.  
  272.     CGXDisplayPropertiesDialog dlg(pNewSettings, m_wndGrid.GetParam()->GetStylesMap(), &bSaveDefault, FALSE);
  273.  
  274.     int result = dlg.DoModal();
  275.     if (result == IDOK)
  276.     {
  277.         *pPropertyObj = *pNewSettings;
  278.         if (bSaveDefault)
  279.             pPropertyObj->WriteProfile();
  280.  
  281.         // SetModifiedFlag();
  282.         m_wndGrid.Redraw();
  283.     }
  284.  
  285.     delete pNewSettings;
  286. }
  287.  
  288. /////////////////////////////////////////////////////////////////////////////
  289. // File menu handlers
  290.  
  291. void CSample1FormView::OnFilePageSetup()
  292. {
  293.     BOOL bSaveDefault = FALSE;
  294.  
  295.     CGXProperties* pPropertyObj = m_wndGrid.GetParam()->GetProperties();
  296.     ASSERT(pPropertyObj->IsKindOf(RUNTIME_CLASS(CGXProperties)));
  297.  
  298.     CGXProperties* pNewSettings = new CGXProperties;
  299.     *pNewSettings = *pPropertyObj;
  300.  
  301.     CGXPrintPropertiesDialog  dlg(pNewSettings, m_wndGrid.GetParam()->GetStylesMap(), &bSaveDefault, FALSE);
  302.  
  303.     int result = dlg.DoModal();
  304.     if (result == IDOK)
  305.     {
  306.         *pPropertyObj = *pNewSettings;
  307.         if (bSaveDefault)
  308.             pPropertyObj->WriteProfile();
  309.  
  310.         // SetModifiedFlag();
  311.     }
  312.  
  313.     delete pNewSettings;
  314. }
  315.  
  316. void CSample1FormView::OnFileHeaderfooter()
  317. {
  318.     BOOL bSaveDefault = FALSE;
  319.  
  320.     CGXProperties* pPropertyObj = m_wndGrid.GetParam()->GetProperties();
  321.     ASSERT(pPropertyObj->IsKindOf(RUNTIME_CLASS(CGXProperties)));
  322.  
  323.     CGXProperties* pNewSettings = new CGXProperties;
  324.     *pNewSettings = *pPropertyObj;
  325.  
  326.     CGXHeaderFooterDialog dlg(pNewSettings, &bSaveDefault);
  327.  
  328.     int result = dlg.DoModal();
  329.     if (result == IDOK)
  330.     {
  331.         *pPropertyObj = *pNewSettings;
  332.         if (bSaveDefault)
  333.             pPropertyObj->WriteProfile();
  334.  
  335.         // SetModifiedFlag();
  336.     }
  337.  
  338.     delete pNewSettings;
  339. }
  340.  
  341. /////////////////////////////////////////////////////////////////////////////
  342. // Edit menu handlers
  343.  
  344. void CSample1FormView::OnEditFind()
  345. {
  346.     m_wndGrid.OnShowFindReplaceDialog(TRUE);
  347. }
  348.  
  349. void CSample1FormView::OnEditReplace()
  350. {
  351.     m_wndGrid.OnShowFindReplaceDialog(FALSE);
  352. }
  353.  
  354. void CSample1FormView::OnEditRepeat()
  355. {
  356.     if (!m_wndGrid.FindText())
  357.         m_wndGrid.OnTextNotFound(GXGetLastFRState()->strFind);
  358. }
  359.  
  360.