home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CATALG2.PAK / CAT2VIEW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  9.3 KB  |  350 lines

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