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

  1. // ipdrivw.cpp : implementation of the CDriverView 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 "ipdrive.h"
  15.  
  16. #include "ipdridoc.h"
  17. #include "ipdrivw.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDriverView
  26.  
  27. IMPLEMENT_DYNCREATE(CDriverView, CFormView)
  28.  
  29. BEGIN_MESSAGE_MAP(CDriverView, CFormView)
  30.     //{{AFX_MSG_MAP(CDriverView)
  31.     ON_BN_CLICKED(ID_LOOKUP, OnLookup)
  32.     ON_BN_CLICKED(ID_REMOVE, OnRemove)
  33.     ON_BN_CLICKED(ID_ADD, OnAdd)
  34.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  35.     ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  36.     //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CDriverView construction/destruction
  41.  
  42. CDriverView::CDriverView()
  43.     : CFormView(CDriverView::IDD)
  44. {
  45.     //{{AFX_DATA_INIT(CDriverView)
  46.         // NOTE: the ClassWizard will add member initialization here
  47.     //}}AFX_DATA_INIT
  48.  
  49.     m_bInitialized = FALSE;
  50. }
  51.  
  52. CDriverView::~CDriverView()
  53. {
  54. }
  55.  
  56. void CDriverView::DoDataExchange(CDataExchange* pDX)
  57. {
  58.     CFormView::DoDataExchange(pDX);
  59.     //{{AFX_DATA_MAP(CDriverView)
  60.     DDX_Control(pDX, IDC_EDIT2, m_toValue);
  61.     DDX_Control(pDX, IDC_COMBO3, m_toType);
  62.     DDX_Control(pDX, IDC_EDIT1, m_ctrlValue);
  63.     DDX_Control(pDX, IDC_COMBO2, m_ctrlType);
  64.     //}}AFX_DATA_MAP
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CDriverView diagnostics
  69.  
  70. #ifdef _DEBUG
  71. void CDriverView::AssertValid() const
  72. {
  73.     CFormView::AssertValid();
  74. }
  75.  
  76. void CDriverView::Dump(CDumpContext& dc) const
  77. {
  78.     CFormView::Dump(dc);
  79. }
  80.  
  81. CDriverDoc* CDriverView::GetDocument() // non-debug version is inline
  82. {
  83.     return STATIC_DOWNCAST(CDriverDoc, m_pDocument);
  84. }
  85. #endif //_DEBUG
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CDriverView message handlers
  89.  
  90. void CDriverView::OnInitialUpdate()
  91. {
  92.     GetParentFrame()->RecalcLayout();
  93.     ResizeParentToFit(FALSE);
  94.  
  95.     CFormView::OnInitialUpdate();
  96.  
  97.     if (!m_bInitialized)
  98.     {
  99.         m_bInitialized = TRUE;
  100.         GetDlgItem(IDC_CURRENT_COUNT)->GetWindowText(m_strCurrentCount);
  101.     }
  102.  
  103.     // initialize the form
  104.     m_ctrlType.SetCurSel(0);
  105.     UpdateToNothing();
  106. }
  107.  
  108. void CDriverView::UpdateToNothing()
  109. {
  110.     UpdateCurrentCount();
  111.     m_toType.SetCurSel(-1);
  112.  
  113.     CString str;
  114.     str.LoadString(IDS_NOTHING);
  115.     m_toValue.SetWindowText(str);
  116. }
  117.  
  118. void CDriverView::UpdateCurrentCount()
  119. {
  120.     CDriverDoc* pDoc = GetDocument();
  121.     ASSERT_VALID(pDoc);
  122.  
  123.     CString str;
  124. #if (_MFC_VER > 0x300)
  125.     str.Format(m_strCurrentCount, pDoc->m_map.GetCount());
  126. #else
  127.     wsprintf(str.GetBuffer(256), m_strCurrentCount,
  128.         pDoc->m_map.GetCount());
  129.     str.ReleaseBuffer();
  130. #endif
  131.     SetDlgItemText(IDC_CURRENT_COUNT, str);
  132. }
  133.  
  134. VARIANT CDriverView::GetFromVariant()
  135. {
  136.     CString strValue;
  137.     m_ctrlValue.GetWindowText(strValue);
  138.  
  139.     // we can just throw it in as a string, then coerce to chosen type
  140.     VARIANT va;
  141.     VariantInit(&va);
  142.     va.vt = VT_BSTR;
  143.     va.bstrVal = strValue.AllocSysString();
  144.  
  145.     VARTYPE vt = max(0, m_ctrlType.GetCurSel());
  146.     VARIANT vaTemp;
  147.     VariantInit(&vaTemp);
  148.     if (FAILED(VariantChangeType(&vaTemp, &va, 0, vt)))
  149.     {
  150.         AfxMessageBox(IDP_INCOMPATIBLE_TYPE_VALUE);
  151.         AfxThrowUserException();
  152.     }
  153.     VariantClear(&va);
  154.     return vaTemp;
  155. }
  156.  
  157. VARIANT CDriverView::GetToVariant()
  158. {
  159.     CString strValue;
  160.     m_toValue.GetWindowText(strValue);
  161.  
  162.     // we can just throw it in as a string, then coerce to chosen type
  163.     VARIANT va;
  164.     VariantInit(&va);
  165.     va.vt = VT_BSTR;
  166.     va.bstrVal = strValue.AllocSysString();
  167.  
  168.     VARTYPE vt = max(0, m_toType.GetCurSel());
  169.     VARIANT vaTemp;
  170.     VariantInit(&vaTemp);
  171.     if (FAILED(VariantChangeType(&vaTemp, &va, 0, vt)))
  172.     {
  173.         AfxMessageBox(IDP_INCOMPATIBLE_TYPE_VALUE);
  174.         AfxThrowUserException();
  175.     }
  176.     VariantClear(&va);
  177.     return vaTemp;
  178. }
  179.  
  180. void CDriverView::UpdateCurrentVariant(VARIANT& va)
  181. {
  182.     m_toType.SetCurSel(va.vt);
  183.  
  184.     VARIANT vaTemp;
  185.     VariantInit(&vaTemp);
  186.     CString str;
  187.     if (FAILED(VariantChangeType(&vaTemp, &va, 0, VT_BSTR)))
  188.         str.LoadString(IDS_UNKNOWN);
  189.     else
  190.         str = vaTemp.bstrVal;
  191.     m_toValue.SetWindowText(str);
  192.     VariantClear(&vaTemp);
  193. }
  194.  
  195. void CDriverView::OnLookup()
  196. {
  197.     CDriverDoc* pDoc = GetDocument();
  198.     ASSERT_VALID(pDoc);
  199.  
  200.     VARIANT vaFrom = GetFromVariant();
  201.     VARIANT vaTo = pDoc->m_map.GetItem(vaFrom);
  202.     UpdateCurrentVariant(vaTo);
  203.  
  204.     VariantClear(&vaTo);
  205.     VariantClear(&vaFrom);
  206. }
  207.  
  208. void CDriverView::OnRemove()
  209. {
  210.     CDriverDoc* pDoc = GetDocument();
  211.     ASSERT_VALID(pDoc);
  212.  
  213.     VARIANT vaFrom = GetFromVariant();
  214.     pDoc->m_map.RemoveKey(vaFrom);
  215.     VariantClear(&vaFrom);
  216.  
  217.     UpdateToNothing();
  218.     UpdateCurrentCount();
  219. }
  220.  
  221. void CDriverView::OnAdd()
  222. {
  223.     CDriverDoc* pDoc = GetDocument();
  224.     ASSERT_VALID(pDoc);
  225.  
  226.     VARIANT vaFrom = GetFromVariant();
  227.     VARIANT vaTo = GetToVariant();
  228.     pDoc->m_map.SetAt(vaFrom, vaTo);
  229.     VariantClear(&vaFrom);
  230.     VariantClear(&vaTo);
  231.  
  232.     OnLookup();
  233.     UpdateCurrentCount();
  234. }
  235.  
  236. void CDriverView::OnButton1()
  237. {
  238.     CDriverDoc* pDoc = GetDocument();
  239.     ASSERT_VALID(pDoc);
  240.  
  241.     long nCount = 0;
  242.     DWORD dwThen = GetTickCount();
  243.  
  244.     if (GetKeyState(VK_CONTROL) >= 0)
  245.     {
  246.         while (GetTickCount() - dwThen < 5000)
  247.         {
  248.             pDoc->m_map.SetStr1(_T("Hello, World"));
  249.             ++nCount;
  250.         }
  251.     }
  252.     else
  253.     {
  254.         while (GetTickCount() - dwThen < 5000)
  255.         {
  256.             pDoc->m_map.SetI1(1);
  257.             ++nCount;
  258.         }
  259.     }
  260.     CString str, strFormat;
  261.     strFormat.LoadString(IDP_TIMING_RESULT);
  262. #if (_MFC_VER > 0x300)
  263.     str.Format(strFormat, nCount);
  264. #else
  265.     wsprintf(str.GetBuffer(256), strFormat, nCount);
  266.     str.ReleaseBuffer();
  267. #endif
  268.     AfxMessageBox(str);
  269. }
  270.  
  271. void CDriverView::OnButton2()
  272. {
  273.     CDriverDoc* pDoc = GetDocument();
  274.     ASSERT_VALID(pDoc);
  275.  
  276.     long nCount = 0;
  277.     DWORD dwThen = GetTickCount();
  278.  
  279.     if (GetKeyState(VK_CONTROL) >= 0)
  280.     {
  281.         while (GetTickCount() - dwThen < 5000)
  282.         {
  283.             pDoc->m_map.SetStr2(_T("Hello, World"));
  284.             ++nCount;
  285.         }
  286.     }
  287.     else
  288.     {
  289.         while (GetTickCount() - dwThen < 5000)
  290.         {
  291.             pDoc->m_map.SetI2(1);
  292.             ++nCount;
  293.         }
  294.     }
  295.     CString str, strFormat;
  296.     strFormat.LoadString(IDP_TIMING_RESULT);
  297. #if (_MFC_VER > 0x300)
  298.     str.Format(strFormat, nCount);
  299. #else
  300.     wsprintf(str.GetBuffer(256), strFormat, nCount);
  301.     str.ReleaseBuffer();
  302. #endif
  303.     AfxMessageBox(str);
  304. }
  305.