home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c10 / query / queryview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.9 KB  |  207 lines

  1. // QueryView.cpp : implementation of the CQueryView class
  2. //
  3. #include "stdafx.h"
  4. #include "Query.h"
  5.  
  6. #include "QueryDoc.h"
  7. #include "QueryView.h"
  8. #include "DialogLatLon.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CQueryView
  18.  
  19. IMPLEMENT_DYNCREATE(CQueryView, CScrollView)
  20.  
  21. BEGIN_MESSAGE_MAP(CQueryView, CScrollView)
  22.     //{{AFX_MSG_MAP(CQueryView)
  23.     ON_COMMAND(ID_VIEW_CLEAR, OnViewClear)
  24.     ON_COMMAND(ID_VIEW_QUERY, OnViewDynamicQuery)
  25.     ON_COMMAND(ID_VIEW_CANNED, OnViewStoredQuery)
  26.     ON_COMMAND(ID_VIEW_LATLON, OnViewLatlon)
  27.     //}}AFX_MSG_MAP
  28. END_MESSAGE_MAP()
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CQueryView construction/destruction
  32.  
  33. CQueryView::CQueryView()
  34. {
  35.     // TODO: add construction code here
  36.     m_WestLongitude= 125.0;
  37.     m_EastLongitude= 116.0;
  38.     m_SouthLatitude= 39.0;
  39.     m_NorthLatitude= 45.0;
  40.  
  41. }
  42.  
  43. CQueryView::~CQueryView()
  44. {
  45. }
  46.  
  47. BOOL CQueryView::PreCreateWindow(CREATESTRUCT& cs)
  48. {
  49.     // TODO: Modify the Window class or styles here by modifying
  50.     //  the CREATESTRUCT cs
  51.  
  52.     return CScrollView::PreCreateWindow(cs);
  53. }
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CQueryView drawing
  57.  
  58. void CQueryView::OnDraw(CDC* pDC)
  59. {
  60.     CQueryDoc* pDoc = GetDocument();
  61.     ASSERT_VALID(pDoc);
  62.  
  63.  
  64.     CSize sizeTotal;
  65.     sizeTotal.cx = 100;
  66.     TEXTMETRIC tm;
  67.     pDC->GetTextMetrics(&tm);
  68.     sizeTotal.cy = tm.tmHeight*m_strings.GetSize();
  69.     SetScrollSizes(MM_TEXT, sizeTotal);
  70.  
  71.     for (int idx=0; idx<m_strings.GetSize(); idx++) {
  72.         CString str;
  73.         str.Format("%d: %s", idx, m_strings.GetAt(idx));
  74.         pDC->TabbedTextOut(0,idx*tm.tmHeight,str,0,NULL,0);
  75.     }
  76.  
  77. }
  78.  
  79. void CQueryView::OnInitialUpdate()
  80. {
  81.     CScrollView::OnInitialUpdate();
  82.     CSize sizeTotal;
  83.     // TODO: calculate the total size of this view
  84.     sizeTotal.cx = sizeTotal.cy = 100;
  85.     SetScrollSizes(MM_TEXT, sizeTotal);
  86.  
  87.     // database creation
  88.     CString dbName= GetDefaultDBName();
  89.     m_db.Open(dbName);
  90.  
  91. }
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CQueryView diagnostics
  95.  
  96. #ifdef _DEBUG
  97. void CQueryView::AssertValid() const
  98. {
  99.     CScrollView::AssertValid();
  100. }
  101.  
  102. void CQueryView::Dump(CDumpContext& dc) const
  103. {
  104.     CScrollView::Dump(dc);
  105. }
  106.  
  107. CQueryDoc* CQueryView::GetDocument() // non-debug version is inline
  108. {
  109.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CQueryDoc)));
  110.     return (CQueryDoc*)m_pDocument;
  111. }
  112. #endif //_DEBUG
  113. CString CQueryView::GetDefaultDBName()
  114. {
  115.     CFileStatus status;
  116.     CString strFileName= _T("..\\..\\States.mdb");
  117.     if( !CFile::GetStatus( strFileName, status ) )
  118.     {
  119.         strFileName= _T("..\\States.mdb");
  120.         if( !CFile::GetStatus( strFileName, status ) )
  121.         {
  122.             strFileName= _T("");
  123.         }
  124.     }
  125.     return strFileName;
  126. }
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CQueryView message handlers
  129.  
  130. void CQueryView::OnViewClear() 
  131. {
  132.     m_strings.RemoveAll();
  133.     Invalidate();
  134. }
  135.  
  136. void CQueryView::OnViewDynamicQuery() 
  137. {
  138.     // query def CREATION
  139.     CDaoQueryDef query(&m_db);
  140.  
  141.     CString sql= "SELECT state FROM States WHERE ";
  142.     CString whereClause;
  143.     whereClause.Format(
  144.     "((wlon<=%lg) Or (elon>%lg) Or (nlat<=%lg) Or (slat>%lg)) = False;",
  145.         m_EastLongitude, m_WestLongitude, m_SouthLatitude, m_NorthLatitude);
  146.     sql += whereClause;
  147.     query.Create(NULL, sql);
  148.     // recordset creation
  149.     CDaoRecordset rs(&m_db);
  150.     rs.Open(&query);
  151.     while (!rs.IsEOF())        // Iterate throught the recordset
  152.     {
  153.         COleVariant state(rs.GetFieldValue("state"));
  154.         CString name(state.pbVal);
  155.         m_strings.Add(name);
  156.         rs.MoveNext();
  157.     }
  158.     Invalidate();
  159.     
  160. }
  161.  
  162. void CQueryView::OnViewStoredQuery() 
  163. {
  164.     // query def OPEN on existing stored query
  165.     CDaoQueryDef query(&m_db);
  166.     query.Open("SpatialQuery");    // Name of the stored query.
  167.     COleVariant eastlon(m_EastLongitude);
  168.     COleVariant westlon(m_WestLongitude);
  169.     COleVariant southlat(m_SouthLatitude);
  170.     COleVariant northlat(m_NorthLatitude);
  171.     query.SetParamValue("WestLongitude", westlon);
  172.     query.SetParamValue("EastLongitude", eastlon);
  173.     query.SetParamValue("SouthLatitude", southlat);
  174.     query.SetParamValue("NorthLatitude", northlat);
  175.  
  176.     // recordset creation
  177.     CDaoRecordset rs(&m_db);
  178.     rs.Open(&query);
  179.     while (!rs.IsEOF())        // Iterate throught the recordset
  180.     {
  181.         COleVariant state(rs.GetFieldValue("state"));
  182.         CString name(state.pbVal);
  183.         m_strings.Add(name);
  184.         rs.MoveNext();
  185.     }
  186.     Invalidate();
  187. }
  188.  
  189.  
  190. void CQueryView::OnViewLatlon() 
  191. {
  192.     // TODO: Add your command handler code here
  193.     CDialogLatLon dlg;
  194.     dlg.m_EastLongitude= m_EastLongitude;
  195.     dlg.m_NorthLatitude= m_NorthLatitude;
  196.     dlg.m_SouthLatitude= m_SouthLatitude;
  197.     dlg.m_WestLongitude= m_WestLongitude;
  198.     if (IDOK == dlg.DoModal())
  199.     {
  200.         m_EastLongitude= dlg.m_EastLongitude;
  201.         m_NorthLatitude= dlg.m_NorthLatitude;
  202.         m_SouthLatitude= dlg.m_SouthLatitude;
  203.         m_WestLongitude= dlg.m_WestLongitude;
  204.     }
  205.  
  206. }
  207.