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

  1. // NwindDlg.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 "extbind.h"
  15. #include "nwindDlg.h"
  16. #include "modeldlg.h"
  17.  
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CNwindDialog dialog
  27.  
  28.  
  29. CNwindDialog::CNwindDialog(CWnd* pParent)
  30.     : CDialog(CNwindDialog::IDD, pParent)
  31. {
  32.     //{{AFX_DATA_INIT(CNwindDialog)
  33.     m_ShowDate = 0;
  34.     m_ShowName = 0;
  35.     //}}AFX_DATA_INIT
  36.     ASSERT(m_pParent != NULL);
  37.  
  38.     m_pParent = pParent;
  39.     m_nID = CNwindDialog::IDD;
  40. }
  41.  
  42. void CNwindDialog::DoDataExchange(CDataExchange* pDX)
  43. {
  44.     CDialog::DoDataExchange(pDX);
  45.     //{{AFX_DATA_MAP(CNwindDialog)
  46.     DDX_Radio(pDX, IDC_BIRTHDATE, m_ShowDate);
  47.     DDX_Radio(pDX, IDC_FIRSTNAME, m_ShowName);
  48.     //}}AFX_DATA_MAP
  49. }
  50.  
  51.  
  52. BEGIN_MESSAGE_MAP(CNwindDialog, CDialog)
  53.     //{{AFX_MSG_MAP(CNwindDialog)
  54.     ON_BN_CLICKED(IDC_BIRTHDATE, OnChangeDate)
  55.     ON_BN_CLICKED(IDC_FIRSTNAME, OnChangeName)
  56.     ON_WM_DESTROY()
  57.     ON_BN_CLICKED(IDC_HIREDATE, OnChangeDate)
  58.     ON_BN_CLICKED(IDC_LASTNAME, OnChangeName)
  59.     ON_WM_CREATE()
  60.     //}}AFX_MSG_MAP
  61. END_MESSAGE_MAP()
  62.  
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CNwindDialog message handlers
  66.  
  67. void CNwindDialog::OnOK()
  68. {
  69.     CMSMask* pEdit = (CMSMask*)GetDlgItem(IDC_MASKEDBOX1);
  70.     CListBox* pList = (CListBox*) (m_pParent->GetDlgItem(IDC_LIST));
  71.  
  72.     ASSERT(pList != NULL);
  73.     ASSERT(pEdit != NULL);
  74.  
  75.     if (pList != NULL && pEdit != NULL)
  76.     {
  77.         CString str=pEdit->GetText();
  78.         str=_T("NorthWind:") + str;
  79.         pList->AddString(str);
  80.     }
  81. }
  82.  
  83. BOOL CNwindDialog::Create()
  84. {
  85.     return CDialog::Create(m_nID, m_pParent);
  86. }
  87.  
  88. void CNwindDialog::OnCancel()
  89. {
  90.     ((CMainDlg*)m_pParent)->BoxDone();
  91.     DestroyWindow();
  92. }
  93.  
  94. void CNwindDialog::PostNcDestroy()
  95. {
  96.     delete this;
  97. }
  98.  
  99. BOOL CNwindDialog::OnInitDialog()
  100. {
  101.     CDialog::OnInitDialog();
  102.  
  103.     RECT r;
  104.     GetClientRect(&r);
  105.     r.bottom=r.top+180;
  106.     r.top+=40;  // top of the dialog (almost)
  107.     r.left+=10;r.right-=10; // some margins to look better
  108.  
  109.     // get the cursor from IDC_RDCNWIND created in Pubs dialog
  110.     // parent dialogRDC
  111.     LPUNKNOWN pCursor=m_pParent->GetDlgItem(IDC_RDCNWIND)->GetDSCCursor();
  112.     ASSERT(pCursor!=NULL);
  113.  
  114.     m_pGrid.Create(_T("Northwind Employees table"),WS_VISIBLE | WS_CHILD,
  115.         r,this,IDC_GRIDNWIND);
  116.     m_pGrid.SetDataSource(pCursor);
  117.     m_pGrid.BindProperty(0x9,m_pParent->GetDlgItem(IDC_RDCNWIND));
  118.     //  create the calendar control to display the Birth/Hire Date
  119.     GetClientRect(&r);
  120.     r.top=r.bottom-170; // the very bottom portion of the dialog
  121.     m_date.Create(NULL,WS_VISIBLE | WS_CHILD,
  122.         r,this,IDC_CALNWIND);
  123.     BindCalendar();
  124.     // bind the mased edit to IDC_RDCNWIND
  125.     CWnd* pMasked=GetDlgItem(IDC_MASKEDBOX1);
  126.     ASSERT(pMasked!=NULL);
  127.     // bind to the correct field: HireDate or BirthDate
  128.     pMasked->BindDefaultProperty(MASKDISPID_TEXT,VT_BSTR,
  129.         m_ShowName==0?_T("FirstName"):_T("LastName"),
  130.         m_pParent->GetDlgItem(IDC_RDCNWIND));
  131.  
  132.     return TRUE;  // return TRUE unless you set the focus to a control
  133.                   // EXCEPTION: OCX Property Pages should return FALSE
  134. }
  135.  
  136.  
  137. void CNwindDialog::BindCalendar()
  138. {
  139.     // bind the calendar to the correct field: HireDate or BirthDate
  140.     m_date.BindDefaultProperty(0xc,VT_VARIANT,m_ShowDate==0?_T("BirthDate"):_T("HireDate"),
  141.         m_pParent->GetDlgItem(IDC_RDCNWIND)); // bind to the RDC in Pubs dialog
  142. }
  143.  
  144. void CNwindDialog::OnChangeDate()
  145. {
  146.     if (!UpdateData())
  147.         return;
  148.     BindCalendar();
  149. }
  150.  
  151.  
  152. void CNwindDialog::OnChangeName()
  153. {
  154.     if (!UpdateData())
  155.         return;
  156.     CWnd* p_maskedit;
  157.  
  158.     p_maskedit=GetDlgItem(IDC_MASKEDBOX1); // retrieve the OCX control with name
  159.     ASSERT(p_maskedit!=NULL);
  160.     if (p_maskedit==NULL)
  161.         return;
  162.     p_maskedit->BindDefaultProperty(MASKDISPID_TEXT,VT_BSTR,
  163.         m_ShowName==0?_T("FirstName"):_T("LastName"),
  164.         m_pParent->GetDlgItem(IDC_RDCNWIND));
  165. }
  166.  
  167. void CNwindDialog::OnDestroy()
  168. {
  169.     CWnd* pMasked=GetDlgItem(IDC_MASKEDBOX1);
  170.     ASSERT(pMasked!=NULL);
  171.     pMasked->BindDefaultProperty(MASKDISPID_TEXT,VT_BSTR,NULL,NULL); // unbind the edit
  172.     m_date.BindDefaultProperty(0xc,VT_VARIANT,NULL,NULL);   // unbind the calendar
  173.     m_pGrid.SetDataSource(NULL);                            // unbind the GRID
  174.     m_pGrid.BindProperty(0x9,NULL);
  175.  
  176.     CDialog::OnDestroy();
  177. }
  178.  
  179. int CNwindDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
  180. {
  181.     if (CDialog::OnCreate(lpCreateStruct) == -1)
  182.         return -1;
  183.     // move the window down (do not cover the main dialog)
  184.     MoveWindow(lpCreateStruct->x+50,lpCreateStruct->y+50,
  185.         lpCreateStruct->cx,lpCreateStruct->cy,FALSE );
  186.  
  187.     return 0;
  188. }
  189.  
  190. BEGIN_EVENTSINK_MAP(CNwindDialog, CDialog)
  191.     ON_PROPNOTIFY_RANGE(CNwindDialog, IDC_MASKEDBOX1, IDC_MASKEDBOX1, MASKDISPID_TEXT, OnRequestEdit, OnChanged)
  192. END_EVENTSINK_MAP()
  193.  
  194. BOOL CNwindDialog::OnRequestEdit(UINT nCtl, BOOL* pBool)
  195. {   // this handler is never called in optimistic DataBinding architecture supplied by MFC
  196.     // but is mapped as well as OnChanged to the events produced by DSC.
  197.     // see ON_PROPNOTIFY_RANGE macro.
  198.     // You might want to implement pessimistic (Access style) DataBinding
  199.     // to make use of this function
  200.     return TRUE;
  201. }
  202.  
  203. BOOL CNwindDialog::OnChanged(UINT nCtl)
  204. {
  205.     // process the notification from nCtl control when data changed
  206.     // in optimistic DataBinding
  207.     return TRUE;
  208. }
  209.