home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / vcoledb / consumer / catdb / catdbvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  8.8 KB  |  345 lines

  1. // Cat3View.cpp : implementation of the CCatDBView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes and
  4. // Templates (MFC&T).
  5. // Copyright (C) 1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // MFC&T Reference and related electronic documentation provided
  10. // with the library.  See these sources for detailed information
  11. // regarding the MFC&T product.
  12. //
  13.  
  14. #include "stdafx.h"
  15. #include "CatDB.h"
  16.  
  17. #include "CatDBDoc.h"
  18. #include "CatDBVw.h"
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CCatDBView
  28.  
  29. IMPLEMENT_DYNCREATE(CCatDBView, CListView)
  30.  
  31. BEGIN_MESSAGE_MAP(CCatDBView, CListView)
  32.     //{{AFX_MSG_MAP(CCatDBView)
  33.     ON_UPDATE_COMMAND_UI(ID_VIEW_COLUMNINFO, OnUpdateViewColumnlevel)
  34.     ON_UPDATE_COMMAND_UI(ID_VIEW_TABLES, OnUpdateViewTablelevel)
  35.     ON_COMMAND(ID_VIEW_COLUMNINFO, OnViewColumnlevel)
  36.     ON_COMMAND(ID_VIEW_TABLES, OnViewTablelevel)
  37.     ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  38.     //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CCatDBView construction/destruction
  43.  
  44. CCatDBView::CCatDBView()
  45. {
  46. }
  47.  
  48. CCatDBView::~CCatDBView()
  49. {
  50. }
  51.  
  52. BOOL CCatDBView::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. // CCatDBView drawing
  64.  
  65. void CCatDBView::OnDraw(CDC* pDC)
  66. {
  67.     CCatDBDoc* pDoc = GetDocument();
  68.     ASSERT_VALID(pDoc);
  69.  
  70.     // TODO: add draw code for native data here
  71. }
  72.  
  73. void CCatDBView::OnInitialUpdate()
  74. {
  75.     CListView::OnInitialUpdate();
  76.  
  77.     // TODO: You may populate your ListView with items by directly accessing
  78.     //  its list control through a call to GetListCtrl().
  79. }
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CCatDBView diagnostics
  83.  
  84. #ifdef _DEBUG
  85. void CCatDBView::AssertValid() const
  86. {
  87.     CListView::AssertValid();
  88. }
  89.  
  90. void CCatDBView::Dump(CDumpContext& dc) const
  91. {
  92.     CListView::Dump(dc);
  93. }
  94.  
  95. CCatDBDoc* CCatDBView::GetDocument() // non-debug version is inline
  96. {
  97.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCatDBDoc)));
  98.     return (CCatDBDoc*)m_pDocument;
  99. }
  100. #endif //_DEBUG
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CCatDBView message handlers
  104.  
  105. void CCatDBView::OnUpdateViewColumnlevel(CCmdUI* pCmdUI)
  106. {
  107.     CCatDBDoc*  pDoc = GetDocument();
  108.     ASSERT_VALID(pDoc);
  109.  
  110.     if (pDoc->m_nLevel == CCatDBDoc::levelTable &&
  111.         GetListCtrl().GetSelectedCount())
  112.     {
  113.         pCmdUI->Enable();
  114.     }
  115.     else
  116.         pCmdUI->Enable(FALSE);
  117. }
  118.  
  119. void CCatDBView::OnUpdateViewTablelevel(CCmdUI* pCmdUI)
  120. {
  121.     CCatDBDoc*  pDoc = GetDocument();
  122.     ASSERT_VALID(pDoc);
  123.  
  124.     if (pDoc->m_nLevel == CCatDBDoc::levelColumn)
  125.         pCmdUI->Enable();
  126.     else
  127.         pCmdUI->Enable(FALSE);
  128. }
  129.  
  130. BOOL CCatDBView::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
  131. {
  132.     CCatDBDoc*  pDoc = GetDocument();
  133.     ASSERT_VALID(pDoc);
  134.  
  135.     // handle double click if at table view level
  136.     if (pDoc->m_nLevel == CCatDBDoc::levelTable)
  137.     {
  138.         if (message == WM_NOTIFY &&
  139.             ((NMHDR*)lParam)->code == NM_DBLCLK)
  140.         {
  141.             OnViewColumnlevel();
  142.             return 0;
  143.         }
  144.     }
  145.  
  146.     return CListView::OnChildNotify(message, wParam, lParam, pLResult);
  147. }
  148.  
  149. void CCatDBView::OnViewColumnlevel()
  150. {
  151.     CCatDBDoc*  pDoc = GetDocument();
  152.     ASSERT_VALID(pDoc);
  153.  
  154.     // determine list control selection
  155.     CListCtrl&  control = GetListCtrl();
  156.     int nCount = control.GetItemCount();
  157.     for (int i = 0; i < nCount; i++)
  158.     {
  159.         if (control.GetItemState(i,LVIS_SELECTED))
  160.             break;
  161.     }
  162.     if (i < nCount)
  163.     {
  164.         // pull table name to send to document
  165.         pDoc->m_strTableName = control.GetItemText(i,0);
  166.  
  167. #ifndef _UNICODE
  168.         LPCSTR lpszName;
  169.         lpszName = pDoc->m_strTableName;
  170. #else
  171.         LPSTR lpszName;
  172.         char rgchTableName[257];
  173.         lpszName = rgchTableName;
  174.         int nSize;
  175.         nSize = ::WideCharToMultiByte(CP_ACP,0,pDoc->m_strTableName,
  176.             -1, lpszName, 257, NULL, NULL);
  177.         // Notify on failure
  178.         ASSERT(nSize);
  179. #endif  // _UNICODE
  180.  
  181.         pDoc->FetchColumnInfo(lpszName);
  182.         pDoc->SetLevel(CCatDBDoc::levelColumn);
  183.     }
  184. }
  185.  
  186. void CCatDBView::OnViewTablelevel()
  187. {
  188.     CCatDBDoc*  pDoc = GetDocument();
  189.     ASSERT_VALID(pDoc);
  190.  
  191.     pDoc->m_strTableName.Empty();
  192.     pDoc->FetchTableInfo();
  193.     pDoc->SetLevel(CCatDBDoc::levelTable);
  194. }
  195.  
  196. void CCatDBView::OnFileOpen()
  197. {
  198.     CCatDBDoc*  pDoc = GetDocument();
  199.     ASSERT_VALID(pDoc);
  200.  
  201.     if (pDoc->OnOpenDocument())
  202.         pDoc->SetLevel(CCatDBDoc::levelTable);
  203.     else
  204.         pDoc->SetLevel(CCatDBDoc::levelNone);
  205. }
  206.  
  207. void CCatDBView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
  208. {
  209.     USES_CONVERSION;
  210.     CCatDBDoc*  pDoc = GetDocument();
  211.     ASSERT_VALID(pDoc);
  212.  
  213.     // delete all items and columns
  214.     CListCtrl& control = GetListCtrl();
  215.     control.DeleteAllItems();
  216.     while (control.DeleteColumn(0));
  217.  
  218.     // set up view based on the document's level
  219.     switch (pDoc->m_nLevel)
  220.     {
  221.         case CCatDBDoc::levelNone:
  222.  
  223.             // set the document title
  224.             pDoc->SetTitle(pDoc->GetDSN());
  225.             break;
  226.  
  227.         case CCatDBDoc::levelTable:
  228.         {
  229.             // set the document title
  230.             CString strDataSource = pDoc->GetDSN();
  231.             strDataSource += _T(" [Tables]");
  232.             pDoc->SetTitle(strDataSource);
  233.  
  234.             // add columns to display
  235.             control.InsertColumn(0,_T("Name"),LVCFMT_LEFT,100,-1);
  236.             control.InsertColumn(1,_T("Type"),LVCFMT_LEFT,100,1);
  237.             control.InsertColumn(2,_T("Catalog"),LVCFMT_LEFT,100,2);
  238.             control.InsertColumn(3,_T("Schema"),LVCFMT_LEFT,100,3);
  239.             control.InsertColumn(4,_T("Description"),LVCFMT_LEFT,100,4);
  240.  
  241.             // traverse the table recordset
  242.             // displaying the table information
  243.             int item = 0;
  244.             while (pDoc->m_pTableset->MoveNext() == S_OK)
  245.             {
  246.                 control.InsertItem(item,
  247.                     pDoc->m_pTableset->m_szName);
  248.                 control.SetItem(item,1,LVIF_TEXT,
  249.                     pDoc->m_pTableset->m_szType,0,0,0,0);
  250.                 control.SetItem(item,2,LVIF_TEXT,
  251.                     pDoc->m_pTableset->m_szCatalog,0,0,0,0);
  252.                 control.SetItem(item,3,LVIF_TEXT,
  253.                     pDoc->m_pTableset->m_szSchema,0,0,0,0);
  254.                 control.SetItem(item,4,LVIF_TEXT,
  255.                     pDoc->m_pTableset->m_szDescription,0,0,0,0);
  256.                 item++;
  257.             }
  258.  
  259.             break;
  260.         }
  261.  
  262.         case CCatDBDoc::levelColumn:
  263.         {
  264.             int column;
  265.  
  266.             // set the document title
  267.             CString strDataSource = pDoc->GetDSN();
  268.             strDataSource += _T(" - ");
  269.             strDataSource += pDoc->m_strTableName;
  270.             strDataSource += _T(" [Column Info]");
  271.             pDoc->SetTitle(strDataSource);
  272.  
  273.             // add columns to display
  274.             // respect the column info settings values
  275.             column = 0;
  276.             control.InsertColumn(column++,_T("Name"),LVCFMT_LEFT,100,-1);
  277.             control.InsertColumn(column,_T("Type"),LVCFMT_LEFT,100,column++);
  278.             if (pDoc->m_bLength)
  279.                 control.InsertColumn(column,_T("Length"),LVCFMT_LEFT,80,column++);
  280.             if (pDoc->m_bPrecision)
  281.             {
  282.                 control.InsertColumn(column,_T("Precision"),LVCFMT_LEFT,80,column++);
  283.                 control.InsertColumn(column,_T("Scale"),LVCFMT_LEFT,50,column++);
  284.             }
  285.             if (pDoc->m_bNullability)
  286.                 control.InsertColumn(column,_T("Nullable"),LVCFMT_LEFT,50,column++);
  287.  
  288.             // traverse the column info recordset
  289.             // respect the column info settings values
  290.             int item = 0;
  291.  
  292.             // If the column rowset couldn't be opened, don't attempt to fetch
  293.             // any data.
  294.             if (pDoc->m_pColumnset == NULL)
  295.                 break;
  296.  
  297.             while (pDoc->m_pColumnset->MoveNext() == S_OK)
  298.             {
  299.                 CString strValue;
  300.  
  301.                 // always insert the column name
  302.                 control.InsertItem(item, pDoc->m_pColumnset->m_szColumnName);
  303.  
  304.                 // always insert the column type
  305.                 column = 1;
  306.                 CString strType;
  307.                 strType.Format("%d", pDoc->m_pColumnset->m_nDataType);
  308.                 control.SetItem(item,column++,LVIF_TEXT, strType,0,0,0,0);
  309.  
  310.                 // only show type if requested
  311.                 if (pDoc->m_bLength)
  312.                 {
  313.                     strValue.Format(_T("%ld"),pDoc->m_pColumnset->m_nMaxLength);
  314.                     control.SetItem(item,column++,LVIF_TEXT,strValue,0,0,0,0);
  315.                 }
  316.  
  317.                 // only show precision,scale,radix if requested
  318.                 if (pDoc->m_bPrecision)
  319.                 {
  320.                     // precision
  321.                     strValue.Format(_T("%d"),pDoc->m_pColumnset->m_nNumericPrecision);
  322.                     control.SetItem(item,column++,LVIF_TEXT,strValue,0,0,0,0);
  323.  
  324.                     // scale
  325.                     int nOrdinal = pDoc->m_pColumnset->m_nOrdinalPosition;
  326.                     strValue.Format(_T("%d"),pDoc->m_pColumnset->m_nNumericScale);
  327.                     control.SetItem(item,column++,LVIF_TEXT,strValue,0,0,0,0);
  328.                 }
  329.  
  330.                 // only show nullability if requested
  331.                 if (pDoc->m_bNullability)
  332.                 {
  333.                     if (pDoc->m_pColumnset->m_bIsNullable == FALSE)
  334.                         control.SetItem(item,column++,LVIF_TEXT,_T("No"),0,0,0,0);
  335.                     else
  336.                         control.SetItem(item,column++,LVIF_TEXT,_T("Yes"),0,0,0,0);
  337.                 }
  338.  
  339.                 item++;
  340.             }
  341.             break;
  342.         }
  343.     }
  344. }
  345.