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 / rdcdlg.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  171 lines

  1. // RDCDlg.cpp : implementation file
  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 "mainfrm.h"
  18. #include "RDCFrm.h"
  19. #include "RDCDlg.h"
  20. #include "rdocols.h"
  21. #include "rdocol.h"
  22. #include "rdoReslt.h"
  23.  
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // COpenControlsDlg dialog
  32.  
  33.  
  34. COpenControlsDlg::COpenControlsDlg(const CMDIBindDoc* pDocument,CWnd* pParent /*=NULL*/)
  35.     : CDialog(COpenControlsDlg::IDD, pParent)
  36. {
  37.     //{{AFX_DATA_INIT(COpenControlsDlg)
  38.     m_ColName = pDocument->m_boundCol;
  39.     //}}AFX_DATA_INIT
  40.     if(pDocument->m_pRDC!=NULL)
  41.         m_pRDCView=(CRDCView*) pDocument->m_pRDC->GetParent();
  42.         //you could change m_pRDCView to ponter to RDC itself
  43.     else
  44.         m_pRDCView=NULL;
  45.     m_CurrentSel=-1;
  46. }
  47.  
  48.  
  49. void COpenControlsDlg::DoDataExchange(CDataExchange* pDX)
  50. {
  51.     CDialog::DoDataExchange(pDX);
  52.     //{{AFX_DATA_MAP(COpenControlsDlg)
  53.     DDX_CBString(pDX, IDC_COLUMN, m_ColName);
  54.     //}}AFX_DATA_MAP
  55. }
  56.  
  57.  
  58. BEGIN_MESSAGE_MAP(COpenControlsDlg, CDialog)
  59.     //{{AFX_MSG_MAP(COpenControlsDlg)
  60.     ON_CBN_DROPDOWN(IDC_COLUMN, OnDropdownColumns)
  61.     //}}AFX_MSG_MAP
  62. END_MESSAGE_MAP()
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // COpenControlsDlg message handlers
  66.  
  67. BOOL COpenControlsDlg::OnInitDialog()
  68. {
  69.     CDialog::OnInitDialog();
  70.  
  71.     CListBox* pListBox = (CListBox*) GetDlgItem(IDC_RDCLIST);
  72.  
  73.     CMainFrame* pMainFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
  74.     ASSERT(pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame)));
  75.     // iterate through all MDI windows
  76.     CMDIChildWnd* pMDIChild = pMainFrame->MDIGetActive();
  77.     if(pMDIChild != NULL)
  78.         pMDIChild = (CRDCFrame*) pMDIChild->GetWindow(GW_HWNDLAST);
  79.     while (pMDIChild != NULL)
  80.     {
  81.         CString strTitle;
  82.         int nIndex;
  83.         if(pMDIChild->IsKindOf(RUNTIME_CLASS(CRDCFrame)))
  84.         { // only RDC windows
  85.             CRDCView* pRDCView = (CRDCView*)pMDIChild->GetActiveView();
  86.             ASSERT(pRDCView->IsKindOf(RUNTIME_CLASS(CRDCView)));
  87.             // get the window's title
  88.             pMDIChild->GetWindowText(strTitle);
  89.             strTitle=strTitle+_T(": ")+pRDCView->m_pRDCCtl->GetCaption();
  90.             // add the window to the list
  91.             nIndex = pListBox->AddString(strTitle);
  92.  
  93.             if(m_pRDCView!=NULL &&
  94.                 pRDCView==m_pRDCView) // you'd compare RDC themselves as well
  95.             {   // select nIndex item only if
  96.                 // m_pRDCCtl==the control currenty assigned
  97.                 pListBox->SetCurSel(nIndex);
  98.             }
  99.             // store a pointer to the frame
  100.             pListBox->SetItemData(nIndex, (DWORD)pMDIChild);
  101.         }
  102.         pMDIChild = (CRDCFrame*) pMDIChild->GetWindow(GW_HWNDPREV);
  103.     } // while
  104.  
  105.     int cElements = pListBox->GetCount();
  106.     pListBox->SetFocus();
  107.  
  108.     return TRUE;  // return TRUE unless you set the focus to a control
  109.                   // EXCEPTION: OCX Property Pages should return FALSE
  110. }
  111.  
  112. void COpenControlsDlg::OnOK()
  113. {
  114.     CListBox* pListBox = (CListBox*) GetDlgItem(IDC_RDCLIST);
  115.     int cElements = pListBox->GetCount();
  116.     int nSelection= pListBox->GetCurSel();  // get the selected item
  117.  
  118.     if(cElements > 0 && nSelection >= 0)
  119.     {   // can only select a window if at least one item is selected
  120.         // get the pointer to the frame
  121.         m_pMDIChild = (CRDCFrame*) pListBox->GetItemData(nSelection);
  122.         ASSERT(m_pMDIChild->IsKindOf(RUNTIME_CLASS(CRDCFrame)));
  123.         // now use ActiveView() rather than MDIActivate()
  124.         // get a pointer to the active view in the frame
  125.         m_pRDCView = (CRDCView*) m_pMDIChild->GetActiveView();
  126.         ASSERT(m_pRDCView->IsKindOf(RUNTIME_CLASS(CRDCView)));
  127.     } //if
  128.  
  129.     CDialog::OnOK();
  130. }
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // Refill the Combo List with columns names from currently selected RDC
  134. void COpenControlsDlg::OnDropdownColumns()
  135. {
  136.     CListBox* pListBox = (CListBox*) GetDlgItem(IDC_RDCLIST);
  137.     CComboBox* pComboBox = (CComboBox*) GetDlgItem(IDC_COLUMN);
  138.  
  139.     int nSelection=pListBox->GetCurSel();
  140.     if(nSelection<0 || nSelection==m_CurrentSel)
  141.         return; // nothing to change in Combo box
  142.     m_CurrentSel=nSelection;
  143.     pComboBox->ResetContent( ); // clear old columns
  144.  
  145.     m_pMDIChild = (CRDCFrame*) pListBox->GetItemData(nSelection);
  146.     ASSERT(m_pMDIChild->IsKindOf(RUNTIME_CLASS(CRDCFrame)));
  147.     // now use ActiveView() rather than MDIActivate()
  148.     // get a pointer to the active view in the frame
  149.     m_pRDCView = (CRDCView*) m_pMDIChild->GetActiveView();
  150.     ASSERT(m_pRDCView->IsKindOf(RUNTIME_CLASS(CRDCView)));
  151.  
  152.     CRdc* pRDC=m_pRDCView->m_pRDCCtl;
  153.     ASSERT(pRDC!=NULL);
  154.  
  155.     CrdoResultset pResult;  // resultset from RDC
  156.     CrdoColumns pColumns;   // columns from resultset
  157.  
  158.     pResult=pRDC->GetResultset();
  159.     if(pResult!=NULL && (pColumns=pResult.GetRdoColumns())!=NULL)
  160.     {
  161.         long ncol=pColumns.GetCount();
  162.         CrdoColumn pCol; // element from columns collection
  163.         for(long icol=0; icol<ncol; icol++)
  164.         {
  165.             pCol=pColumns.GetItem(COleVariant(icol)); //get i-column
  166.             pComboBox->AddString(pCol.GetName());//add its name to the list
  167.         }
  168.         pComboBox->SetCurSel(0); // no old element valid if new RDC
  169.     }
  170. }
  171.