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

  1. // catalog2View.cpp : implementation of the CCatalog2View 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 "catalog2.h"
  15.  
  16. #include "catsets.h"
  17. #include "cat2Doc.h"
  18. #include "cat2View.h"
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CCatalog2View
  27.  
  28. IMPLEMENT_DYNCREATE(CCatalog2View, CListView)
  29.  
  30. BEGIN_MESSAGE_MAP(CCatalog2View, CListView)
  31.     //{{AFX_MSG_MAP(CCatalog2View)
  32.     ON_COMMAND(ID_VIEW_TABLES, OnViewTablelevel)
  33.     ON_COMMAND(ID_VIEW_COLUMNINFO, OnViewColumnlevel)
  34.     ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  35.     ON_UPDATE_COMMAND_UI(ID_VIEW_COLUMNINFO, OnUpdateViewColumnlevel)
  36.     ON_UPDATE_COMMAND_UI(ID_VIEW_TABLES, OnUpdateViewTablelevel)
  37.     //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CCatalog2View construction/destruction
  42.  
  43. CCatalog2View::CCatalog2View()
  44. {
  45.     m_strTableName = _T("");
  46. }
  47.  
  48. CCatalog2View::~CCatalog2View()
  49. {
  50. }
  51.  
  52. BOOL CCatalog2View::PreCreateWindow(CREATESTRUCT& cs)
  53. {
  54.     // set list view control to report, single selection
  55.     cs.style &= ~(LVS_LIST | LVS_ICON | LVS_SMALLICON);
  56.     cs.style |= LVS_REPORT;
  57.     cs.style |= LVS_SINGLESEL;
  58.  
  59.     return CListView::PreCreateWindow(cs);
  60. }
  61.  
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CCatalog2View drawing
  64.  
  65. void CCatalog2View::OnDraw(CDC* pDC)
  66. {
  67.     CCatalog2Doc* pDoc = GetDocument();
  68.     ASSERT_VALID(pDoc);
  69.  
  70.     // TODO: add draw code for native data here
  71. }
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CCatalog2View diagnostics
  75.  
  76. #ifdef _DEBUG
  77. void CCatalog2View::AssertValid() const
  78. {
  79.     CListView::AssertValid();
  80. }
  81.  
  82. void CCatalog2View::Dump(CDumpContext& dc) const
  83. {
  84.     CListView::Dump(dc);
  85. }
  86.  
  87. CCatalog2Doc* CCatalog2View::GetDocument() // non-debug version is inline
  88. {
  89.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCatalog2Doc)));
  90.     return (CCatalog2Doc*)m_pDocument;
  91. }
  92. #endif //_DEBUG
  93.  
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CCatalog2View message handlers
  96.  
  97. void CCatalog2View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
  98. {
  99.     CCatalog2Doc*   pDoc = GetDocument();
  100.     ASSERT_VALID(pDoc);
  101.  
  102.     // delete all items and columns
  103.     CListCtrl& control = GetListCtrl();
  104.     control.DeleteAllItems();
  105.     while (control.DeleteColumn(0));
  106.  
  107.     // set up view based on the document's level
  108.     switch (pDoc->m_nLevel)
  109.     {
  110.         case CCatalog2Doc::levelNone:
  111.  
  112.             // set the document title
  113.             pDoc->SetTitle(pDoc->GetDSN());
  114.             break;
  115.  
  116.         case CCatalog2Doc::levelTable:
  117.         {
  118.             // set the document title
  119.             CString strDataSource = pDoc->GetDSN();
  120.             strDataSource += _T(" [Tables]");
  121.             pDoc->SetTitle(strDataSource);
  122.  
  123.             // add columns to display
  124.             control.InsertColumn(0,_T("Name"),LVCFMT_LEFT,100,-1);
  125.             control.InsertColumn(1,_T("Type"),LVCFMT_LEFT,100,1);
  126.             control.InsertColumn(2,_T("Qualifier"),LVCFMT_LEFT,100,2);
  127.             control.InsertColumn(3,_T("Owner"),LVCFMT_LEFT,100,3);
  128.             control.InsertColumn(4,_T("Remarks"),LVCFMT_LEFT,100,4);
  129.  
  130.             // traverse the table recordset
  131.             // displaying the table information
  132.             int item = 0;
  133.             pDoc->m_pTableset->MoveFirst();
  134.             while (!pDoc->m_pTableset->IsEOF())
  135.             {
  136.                 control.InsertItem(item,
  137.                     pDoc->m_pTableset->m_strTableName);
  138.                 control.SetItem(item,1,LVIF_TEXT,
  139.                     pDoc->m_pTableset->m_strTableType,0,0,0,0);
  140.                 control.SetItem(item,2,LVIF_TEXT,
  141.                     pDoc->m_pTableset->m_strTableQualifier,0,0,0,0);
  142.                 control.SetItem(item,3,LVIF_TEXT,
  143.                     pDoc->m_pTableset->m_strTableOwner,0,0,0,0);
  144.                 control.SetItem(item,4,LVIF_TEXT,
  145.                     pDoc->m_pTableset->m_strRemarks,0,0,0,0);
  146.                 item++;
  147.                 pDoc->m_pTableset->MoveNext();
  148.             }
  149.             break;
  150.         }
  151.  
  152.         case CCatalog2Doc::levelColumn:
  153.         {
  154.             int column;
  155.  
  156.             // set the document title
  157.             CString strDataSource = pDoc->GetDSN();
  158.             strDataSource += _T(" - ");
  159.             strDataSource += m_strTableName;
  160.             strDataSource += _T(" [Column Info]");
  161.             pDoc->SetTitle(strDataSource);
  162.  
  163.             // add columns to display
  164.             // respect the column info settings values
  165.             column = 0;
  166.             control.InsertColumn(column++,_T("Name"),LVCFMT_LEFT,100,-1);
  167.             control.InsertColumn(column,_T("Type"),LVCFMT_LEFT,100,column++);
  168.             if (pDoc->m_bLength)
  169.                 control.InsertColumn(column,_T("Length"),LVCFMT_LEFT,80,column++);
  170.             if (pDoc->m_bPrecision)
  171.             {
  172.                 control.InsertColumn(column,_T("Precision"),LVCFMT_LEFT,80,column++);
  173.                 control.InsertColumn(column,_T("Scale"),LVCFMT_LEFT,50,column++);
  174.                 control.InsertColumn(column,_T("Radix"),LVCFMT_LEFT,50,column++);
  175.             }
  176.             if (pDoc->m_bNullability)
  177.                 control.InsertColumn(column,_T("Nullable"),LVCFMT_LEFT,50,column++);
  178.  
  179.             // traverse the column info recordset
  180.             // respect the column info settings values
  181.             int item = 0;
  182.             pDoc->m_pColumnset->MoveFirst();
  183.             while (!pDoc->m_pColumnset->IsEOF())
  184.             {
  185.                 CString strValue;
  186.  
  187.                 // always insert the column name
  188.                 control.InsertItem(item,
  189.                     pDoc->m_pColumnset->m_strColumnName);
  190.  
  191.                 // always insert the column type
  192.                 column = 1;
  193.                 control.SetItem(item,column++,LVIF_TEXT,
  194.                     pDoc->m_pColumnset->m_strTypeName,0,0,0,0);
  195.  
  196.                 // only show type if requested
  197.                 if (pDoc->m_bLength)
  198.                 {
  199.                     strValue.Format(_T("%d"),pDoc->m_pColumnset->m_nLength);
  200.                     control.SetItem(item,column++,LVIF_TEXT,strValue,0,0,0,0);
  201.                 }
  202.  
  203.                 // only show precision,scale,radix if requested
  204.                 if (pDoc->m_bPrecision)
  205.                 {
  206.                     // precision
  207.                     strValue.Format(_T("%d"),pDoc->m_pColumnset->m_nPrecision);
  208.                     control.SetItem(item,column++,LVIF_TEXT,strValue,0,0,0,0);
  209.  
  210.                     // scale
  211.                     if (!pDoc->m_pColumnset->IsFieldNull(
  212.                         &(pDoc->m_pColumnset->m_nScale)))
  213.                     {
  214.                         strValue.Format(_T("%d"),pDoc->m_pColumnset->m_nScale);
  215.                         control.SetItem(item,column++,LVIF_TEXT,strValue,0,0,0,0);
  216.                     }
  217.                     else
  218.                         control.SetItem(item,column++,LVIF_TEXT,_T("<na>"),0,0,0,0);
  219.  
  220.                     // radix
  221.                     if (!pDoc->m_pColumnset->IsFieldNull(
  222.                         &(pDoc->m_pColumnset->m_nRadix)))
  223.                     {
  224.                         strValue.Format(_T("%d"),pDoc->m_pColumnset->m_nRadix);
  225.                         control.SetItem(item,column++,LVIF_TEXT,strValue,0,0,0,0);
  226.                     }
  227.                     else
  228.                         control.SetItem(item,column++,LVIF_TEXT,_T("<na>"),0,0,0,0);
  229.                 }
  230.  
  231.                 // only show nullability if requested
  232.                 if (pDoc->m_bNullability)
  233.                 {
  234.                     if (pDoc->m_pColumnset->m_fNullable == SQL_NO_NULLS)
  235.                         control.SetItem(item,column++,LVIF_TEXT,_T("No"),0,0,0,0);
  236.                     else if (pDoc->m_pColumnset->m_fNullable == SQL_NULLABLE)
  237.                         control.SetItem(item,column++,LVIF_TEXT,_T("Yes"),0,0,0,0);
  238.                     else
  239.                         control.SetItem(item,column++,LVIF_TEXT,_T("Unknown"),0,0,0,0);
  240.                 }
  241.  
  242.                 item++;
  243.                 pDoc->m_pColumnset->MoveNext();
  244.             }
  245.             break;
  246.         }
  247.     }
  248. }
  249.  
  250. void CCatalog2View::OnViewTablelevel()
  251. {
  252.     CCatalog2Doc*   pDoc = GetDocument();
  253.     ASSERT_VALID(pDoc);
  254.  
  255.     pDoc->SetLevel(CCatalog2Doc::levelTable);
  256. }
  257.  
  258. void CCatalog2View::OnViewColumnlevel()
  259. {
  260.     CCatalog2Doc*   pDoc = GetDocument();
  261.     ASSERT_VALID(pDoc);
  262.  
  263.     // determine list control selection
  264.     CListCtrl&  control = GetListCtrl();
  265.     int nCount = control.GetItemCount();
  266.     for (int i = 0; i < nCount; i++)
  267.     {
  268.         if (control.GetItemState(i,LVIS_SELECTED))
  269.             break;
  270.     }
  271.     if (i < nCount)
  272.     {
  273.         // pull table name to send to document
  274.         m_strTableName = control.GetItemText(i,0);
  275.  
  276. #ifndef _UNICODE
  277.         LPCSTR lpszName;
  278.         lpszName = m_strTableName;
  279. #else
  280.         LPSTR lpszName;
  281.         char rgchTableName[257];
  282.         lpszName = rgchTableName;
  283.         int nSize;
  284.         nSize = ::WideCharToMultiByte(CP_ACP,0,m_strTableName,
  285.             -1, lpszName, 257, NULL, NULL);
  286.         // Notify on failure
  287.         ASSERT(nSize);
  288. #endif  // _UNICODE
  289.  
  290.         pDoc->FetchColumnInfo(lpszName);
  291.         pDoc->SetLevel(CCatalog2Doc::levelColumn);
  292.     }
  293. }
  294.  
  295. void CCatalog2View::OnFileOpen()
  296. {
  297.     CCatalog2Doc*   pDoc = GetDocument();
  298.     ASSERT_VALID(pDoc);
  299.  
  300.     if (pDoc->OnOpenDocument())
  301.         pDoc->SetLevel(CCatalog2Doc::levelTable);
  302.     else
  303.         pDoc->SetLevel(CCatalog2Doc::levelNone);
  304. }
  305.  
  306. void CCatalog2View::OnUpdateViewColumnlevel(CCmdUI* pCmdUI)
  307. {
  308.     CCatalog2Doc*   pDoc = GetDocument();
  309.     ASSERT_VALID(pDoc);
  310.  
  311.     if (pDoc->m_nLevel == CCatalog2Doc::levelTable &&
  312.         GetListCtrl().GetSelectedCount())
  313.     {
  314.         pCmdUI->Enable();
  315.     }
  316.     else
  317.         pCmdUI->Enable(FALSE);
  318. }
  319.  
  320. void CCatalog2View::OnUpdateViewTablelevel(CCmdUI* pCmdUI)
  321. {
  322.     CCatalog2Doc*   pDoc = GetDocument();
  323.     ASSERT_VALID(pDoc);
  324.  
  325.     if (pDoc->m_nLevel == CCatalog2Doc::levelColumn)
  326.         pCmdUI->Enable();
  327.     else
  328.         pCmdUI->Enable(FALSE);
  329. }
  330.  
  331. BOOL CCatalog2View::OnChildNotify(UINT message, WPARAM wParam,
  332.  LPARAM lParam, LRESULT* pLResult)
  333. {
  334.     CCatalog2Doc*   pDoc = GetDocument();
  335.     ASSERT_VALID(pDoc);
  336.  
  337.     // handle double click if at table view level
  338.     if (pDoc->m_nLevel == CCatalog2Doc::levelTable)
  339.     {
  340.         if (message == WM_NOTIFY &&
  341.             ((NMHDR*)lParam)->code == NM_DBLCLK)
  342.         {
  343.             OnViewColumnlevel();
  344.             return 0;
  345.         }
  346.     }
  347.     return CListView::OnChildNotify(message,wParam,lParam,pLResult);
  348. }
  349.