home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / mdibind / rdcview.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  5KB  |  173 lines

  1. // RDCView.cpp : implementation of the CRDCView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "MDIBind.h"
  15. #include "rdc.h"
  16. #include "MDIDoc.h"
  17. #include "RDCView.h"
  18. #include "GridFrm.h"
  19. #include "MaskFrm.h"
  20. #include "mainfrm.h"
  21.  
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CRDCView
  30.  
  31. IMPLEMENT_DYNCREATE(CRDCView, CView)
  32.  
  33. BEGIN_MESSAGE_MAP(CRDCView, CView)
  34.     //{{AFX_MSG_MAP(CRDCView)
  35.     ON_WM_CREATE()
  36.     ON_WM_SIZE()
  37.     ON_WM_DESTROY()
  38.     //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CRDCView construction/destruction
  43.  
  44. CRDCView::CRDCView()
  45. {
  46.     m_pRDCCtl = new CRdc;
  47.  
  48. }
  49.  
  50. CRDCView::~CRDCView()
  51. {
  52.     delete m_pRDCCtl;
  53. }
  54.  
  55. BOOL CRDCView::PreCreateWindow(CREATESTRUCT& cs)
  56. {
  57.     // TODO: Modify the Window class or styles here by modifying
  58.     // the CREATESTRUCT cs
  59.  
  60.     return CView::PreCreateWindow(cs);
  61. }
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CRDCView drawing
  65.  
  66. void CRDCView::OnDraw(CDC* pDC)
  67. {
  68.     CMDIBindDoc* pDoc = GetDocument();
  69.     ASSERT_VALID(pDoc);
  70.  
  71.     // TODO: add draw code for native data here
  72. }
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CRDCView diagnostics
  76.  
  77. #ifdef _DEBUG
  78. void CRDCView::AssertValid() const
  79. {
  80.     CView::AssertValid();
  81. }
  82.  
  83. void CRDCView::Dump(CDumpContext& dc) const
  84. {
  85.     CView::Dump(dc);
  86. }
  87.  
  88. CMDIBindDoc* CRDCView::GetDocument() // non-debug version is inline
  89. {
  90.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMDIBindDoc)));
  91.     return (CMDIBindDoc*) m_pDocument;
  92. }
  93. #endif //_DEBUG
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CRDCView message handlers
  97.  
  98. int CRDCView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  99. {
  100.     if (CView::OnCreate(lpCreateStruct) == -1)
  101.         return -1;
  102.     CMDIBindDoc* pDoc=GetDocument();
  103.     if(pDoc->m_pRDC!=NULL)
  104.         return -1; // the RDC already created
  105.  
  106.     RECT r=  { 2,2,400,300 };
  107.  
  108.     m_pRDCCtl->Create(NULL,_T("NorthWind"),
  109.                        WS_VISIBLE | WS_CHILD, r, this, 1);
  110.  
  111.     m_pRDCCtl->SetDataSourceName(_T("NorthWind"));
  112.     m_pRDCCtl->SetSql(_T("SELECT FirstName, LastName, BirthDate, HireDate from Employees"));
  113.     m_pRDCCtl->SetUserName(_T("admin"));
  114.     m_pRDCCtl->SetPassword(_T(""));
  115.     m_pRDCCtl->SetCursorDriver(2); //server side cursor
  116.     pDoc->m_pRDC=m_pRDCCtl; // add newly created RDC
  117.  
  118.     return 0;
  119. }
  120.  
  121. void CRDCView::OnSize(UINT nType, int cx, int cy)
  122. {
  123.     CView::OnSize(nType, cx, cy);
  124.  
  125.     if (::IsWindow(m_pRDCCtl->m_hWnd) )
  126.         m_pRDCCtl->MoveWindow(2,2,cx-4,cy-4,FALSE);  // do not repaint
  127.  
  128. }
  129.  
  130. // sets the value of the RDC control
  131. void CRDCView::Bind(CString &DSN,CString &Sql,CString &User,CString &Pw)
  132. {
  133.     m_pRDCCtl->SetDataSourceName(DSN);
  134.     m_pRDCCtl->SetSql(Sql);
  135.     m_pRDCCtl->SetUserName(User);
  136.     m_pRDCCtl->SetPassword(Pw);
  137.     m_pRDCCtl->SetCaption(DSN);
  138.     // TODO here some bound controls might want to update
  139.     m_pRDCCtl->Refresh();  // must refresh all bound controls
  140. }
  141.  
  142.  
  143. void CRDCView::OnDestroy()
  144. { // must unbind all bound controls before destroying the object
  145.     CMDIBindDoc* pDoc;
  146.  //  how to get all documents
  147.     CMainFrame* pMainFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
  148.     ASSERT(pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame)));
  149.     // let's do it other way: iterate through all MDI windows
  150.     CMDIChildWnd* pMDIChild = pMainFrame->MDIGetActive();
  151.     if(pMDIChild != NULL)
  152.         pMDIChild = (CMDIChildWnd*) pMDIChild->GetWindow(GW_HWNDLAST);
  153.     while (pMDIChild != NULL)
  154.     {
  155.         if(pMDIChild->IsKindOf(RUNTIME_CLASS(CGridFrame)) ||
  156.             pMDIChild->IsKindOf(RUNTIME_CLASS(CMaskFrame)))
  157.         { // data bound control windows
  158.             CView* pView = pMDIChild->GetActiveView();
  159.             pDoc=(CMDIBindDoc*) pView->GetDocument();
  160.             if(pDoc->m_pRDC==m_pRDCCtl)
  161.             {    // unbind this and all other controls
  162.                 pDoc->m_pRDC=NULL;
  163.                 // TODO if you want to change the title to reflect
  164.                 // unbound state, do it here
  165.                 pDoc->UpdateAllViews(NULL);
  166.             }
  167.         }
  168.         pMDIChild =  (CMDIChildWnd*) pMDIChild->GetWindow(GW_HWNDPREV);
  169.     } //while
  170.  
  171.     CView::OnDestroy();
  172. }
  173.