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

  1. // ListCtrlPage.cpp : implementation file
  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 "ctrldemo.h"
  16. #include "listcpg.h"
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. #define CELEMS(rgFoo) (sizeof(rgFoo) / sizeof(rgFoo[0]))
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CListCtrlPage property page
  28.  
  29. IMPLEMENT_DYNCREATE(CListCtrlPage, CPropertyPage)
  30.  
  31. CListCtrlPage::CListCtrlPage() : CPropertyPage(CListCtrlPage::IDD)
  32. {
  33.     //{{AFX_DATA_INIT(CListCtrlPage)
  34.     m_bAutoArrange = FALSE;
  35.     m_bCallBacks = FALSE;
  36.     m_bEditLabels = FALSE;
  37.     m_bNoColumnHeader = FALSE;
  38.     m_bNoLabelWrap = FALSE;
  39.     m_bNoSortHeader = FALSE;
  40.     m_bSingleSel = FALSE;
  41.     m_cstrViewMode = _T("REPORT");  // report is the default mode
  42.     m_cstrAlignMode = _T("ALIGNTOP");        // top alignment is the default mode.
  43.     m_cstrSort = _T("None");
  44.     m_cstrStatus = _T("");
  45.     //}}AFX_DATA_INIT
  46.  
  47.     m_pimagelist = NULL;
  48.     m_pimagelistSmall = NULL;
  49. }
  50.  
  51. CListCtrlPage::~CListCtrlPage()
  52. {
  53.     delete m_pimagelist;
  54.     delete m_pimagelistSmall;
  55. }
  56.  
  57.  
  58. void CListCtrlPage::DoDataExchange(CDataExchange* pDX)
  59. {
  60.     CPropertyPage::DoDataExchange(pDX);
  61.     //{{AFX_DATA_MAP(CListCtrlPage)
  62.     DDX_Control(pDX, IDC_LISTVIEW1, m_listctrl);
  63.     DDX_Check(pDX, IDC_AUTOARRANGE, m_bAutoArrange);
  64.     DDX_Check(pDX, IDC_EDITLABELS, m_bEditLabels);
  65.     DDX_Check(pDX, IDC_NOCOLUMNHEADER, m_bNoColumnHeader);
  66.     DDX_Check(pDX, IDC_NOLABELWRAP, m_bNoLabelWrap);
  67.     DDX_Check(pDX, IDC_NOSORTHEADER, m_bNoSortHeader);
  68.     DDX_Check(pDX, IDC_SINGLESEL, m_bSingleSel);
  69.     DDX_CBString(pDX, IDC_VIEWMODE, m_cstrViewMode);
  70.     DDX_CBString(pDX, IDC_ALIGN, m_cstrAlignMode);
  71.     DDX_CBString(pDX, IDC_SORT, m_cstrSort);
  72.     DDX_Text(pDX, IDC_STATUS, m_cstrStatus);
  73.     //}}AFX_DATA_MAP
  74. }
  75.  
  76. //    ON_NOTIFY(LVN_BEGINDRAG, IDC_LISTVIEW1, OnBeginDrag)
  77. //    ON_WM_MOUSEMOVE()
  78. //    ON_WM_LBUTTONUP()
  79. //    ON_WM_RBUTTONUP()
  80.  
  81. BEGIN_MESSAGE_MAP(CListCtrlPage, CPropertyPage)
  82.     //{{AFX_MSG_MAP(CListCtrlPage)
  83.     ON_NOTIFY(LVN_ITEMCHANGING, IDC_LISTVIEW1, ShowNotification)
  84.     ON_NOTIFY(LVN_ITEMCHANGED, IDC_LISTVIEW1, ShowNotification)
  85.     ON_NOTIFY(LVN_INSERTITEM, IDC_LISTVIEW1, ShowNotification)
  86.     ON_NOTIFY(LVN_DELETEITEM, IDC_LISTVIEW1, ShowNotification)
  87.     ON_NOTIFY(LVN_DELETEALLITEMS, IDC_LISTVIEW1, ShowNotification)
  88.     ON_NOTIFY(LVN_BEGINLABELEDIT, IDC_LISTVIEW1, ShowNotification)
  89.     ON_NOTIFY(LVN_ENDLABELEDIT, IDC_LISTVIEW1, ShowNotification)
  90.     ON_NOTIFY(LVN_COLUMNCLICK, IDC_LISTVIEW1, ShowNotification)
  91.     ON_NOTIFY(LVN_GETDISPINFO, IDC_LISTVIEW1, ShowNotification)
  92.     ON_NOTIFY(LVN_SETDISPINFO, IDC_LISTVIEW1, ShowNotification)
  93.     ON_BN_CLICKED(IDC_AUTOARRANGE, OnAutoArrange)
  94.     ON_BN_CLICKED(IDC_SINGLESEL, OnSingleSel)
  95.     ON_BN_CLICKED(IDC_NOLABELWRAP, OnNoLabelWrap)
  96.     ON_BN_CLICKED(IDC_EDITLABELS, OnEditLabels)
  97.     ON_BN_CLICKED(IDC_NOCOLUMNHEADER, OnNoColumnHeader)
  98.     ON_BN_CLICKED(IDC_NOSORTHEADER, OnNoSortHeader)
  99.     ON_CBN_SELCHANGE(IDC_VIEWMODE, OnChangeViewMode)
  100.     ON_CBN_SELCHANGE(IDC_ALIGN, OnChangeAlignMode)
  101.     ON_CBN_SELCHANGE(IDC_SORT, OnChangeSortMode)
  102.     //}}AFX_MSG_MAP
  103. END_MESSAGE_MAP()
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CListCtrlPage message handlers
  107. void CListCtrlPage::ChangeListCtrlStyle(long lStyle, BOOL bSetBit)
  108. {
  109.     long    lStyleOld;
  110.     CRect    rect;
  111.  
  112.     m_listctrl.GetWindowRect(&rect);
  113.     ScreenToClient(&rect);
  114.     ASSERT(lStyle != 0);  // watch out for LVS_foo DEFINITIONS WHICH ARE 0.
  115.     lStyleOld = GetWindowLong(m_listctrl.m_hWnd, GWL_STYLE);
  116.     lStyleOld &= ~lStyle;  // turn off bits specified by caller.
  117.     if (bSetBit)
  118.         lStyleOld |= lStyle;
  119.  
  120.     SetWindowLong(m_listctrl.m_hWnd, GWL_STYLE, lStyleOld);
  121.     m_listctrl.SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(),    SWP_NOZORDER | SWP_SHOWWINDOW);
  122.  
  123.     UpdateData(FALSE/*bSaveAndValidate*/);  // send information back to the dialog
  124. }
  125.  
  126. void CListCtrlPage::OnAutoArrange()
  127. {
  128.     UpdateData(TRUE);  // get the information from the dialog
  129.     ChangeListCtrlStyle(LVS_AUTOARRANGE, m_bAutoArrange);
  130. }
  131.  
  132. void CListCtrlPage::OnSingleSel()
  133. {
  134.     UpdateData(TRUE/*bSaveAndValidate*/);
  135.     ChangeListCtrlStyle(LVS_SINGLESEL, m_bSingleSel);
  136. }
  137.  
  138. void CListCtrlPage::OnNoLabelWrap()
  139. {
  140.     UpdateData(TRUE);
  141.     ChangeListCtrlStyle(LVS_NOLABELWRAP, m_bNoLabelWrap);
  142. }
  143.  
  144. void CListCtrlPage::OnEditLabels()
  145. {
  146.     UpdateData(TRUE);
  147.     ChangeListCtrlStyle(LVS_EDITLABELS, m_bEditLabels);
  148. }
  149.  
  150. void CListCtrlPage::OnNoColumnHeader()
  151. {
  152.     UpdateData(TRUE);
  153.     RenewListCtrl(LVS_NOCOLUMNHEADER, m_bNoColumnHeader);
  154. }
  155.  
  156. void CListCtrlPage::OnNoSortHeader()
  157. {
  158.     UpdateData(TRUE);
  159.     RenewListCtrl(LVS_NOSORTHEADER, m_bNoSortHeader);
  160. }
  161.  
  162. void CListCtrlPage::OnChangeViewMode()
  163. {
  164.     long        lStyle, lStyleOld;
  165.     BOOL        bReport, bIconic;
  166.  
  167.     UpdateData(TRUE);
  168.     if (m_cstrViewMode == _T("ICON"))
  169.         lStyle = LVS_ICON;
  170.     else if (m_cstrViewMode == _T("SMALL ICON"))
  171.         lStyle = LVS_SMALLICON;
  172.     else if (m_cstrViewMode == _T("REPORT"))
  173.         lStyle = LVS_REPORT;
  174.     else
  175.     {
  176.         ASSERT(m_cstrViewMode == _T("LIST"));
  177.         lStyle = LVS_LIST;
  178.     }
  179.  
  180.     bReport = lStyle == LVS_REPORT;
  181.     bIconic = lStyle == LVS_ICON || lStyle == LVS_SMALLICON;
  182.     GetDlgItem(IDC_NOSORTHEADER)->EnableWindow(bReport);
  183.     GetDlgItem(IDC_NOCOLUMNHEADER)->EnableWindow(bReport);
  184.     GetDlgItem(IDC_ALIGN)->EnableWindow(bIconic);
  185.     GetDlgItem(IDC_AUTOARRANGE)->EnableWindow(bIconic);
  186.  
  187.     lStyleOld = GetWindowLong(m_listctrl.m_hWnd, GWL_STYLE);
  188.     lStyleOld &= ~(LVS_TYPEMASK);  // turn off all the style (view mode) bits
  189.     lStyleOld |= lStyle;        // Set the new Style for the control
  190.     SetWindowLong(m_listctrl.m_hWnd, GWL_STYLE, lStyleOld);  // done!
  191. }
  192.  
  193. void CListCtrlPage::OnChangeSortMode()
  194. {
  195.     long    lStyle;
  196.  
  197.     UpdateData(TRUE/*bSaveAndValidate*/);
  198.     lStyle = GetWindowLong(m_listctrl.m_hWnd, GWL_STYLE);
  199.     lStyle &= ~(LVS_SORTASCENDING | LVS_SORTDESCENDING);
  200.     SetWindowLong(m_listctrl.m_hWnd, GWL_STYLE, lStyle);  // set style without sorting 
  201.     if (m_cstrSort == _T("None"))
  202.         RenewListCtrl(LVS_SORTASCENDING | LVS_SORTDESCENDING, FALSE/*bSetBits*/);
  203.     else if (m_cstrSort == _T("ASCENDING"))
  204.         RenewListCtrl(LVS_SORTASCENDING, TRUE);
  205.     else
  206.     {
  207.         ASSERT(m_cstrSort == _T("DESCENDING"));
  208.         RenewListCtrl(LVS_SORTDESCENDING, TRUE);
  209.     }
  210. }
  211.  
  212. void CListCtrlPage::OnChangeAlignMode()
  213. {
  214.     // this function takes into consideration that LVS_ALIGNTOP is defined as zero.
  215.     UpdateData(TRUE);
  216.     ASSERT(m_cstrAlignMode == _T("ALIGNTOP") || m_cstrAlignMode == _T("ALIGNLEFT"));
  217.     ChangeListCtrlStyle(LVS_ALIGNLEFT, (m_cstrAlignMode == _T("ALIGNLEFT")));
  218. }
  219.  
  220. void CListCtrlPage::FillListCtrl()
  221. {
  222.     CRect            rect;
  223.     int                i, iIcon, iItem, iSubItem, iActualItem;
  224.     LV_ITEM            lvitem;
  225.     LV_COLUMN        lvcolumn;
  226.     TCHAR            rgtsz[2][10] = {_T("MAIN ITEM"), _T("SUB_ITEM")};
  227.     CCtrldemoApp    *pApp;
  228.     TCHAR            rgtszIconDescrip[LISTICONCOUNT][50], rgtszIconShortDesc[LISTICONCOUNT][12];
  229.  
  230.     ASSERT(LISTICONCOUNT == 4);
  231.     _tcscpy(rgtszIconDescrip[0], _T("Blue Ellipse, Yellow Triangle, Red Rectangle"));
  232.     _tcscpy(rgtszIconDescrip[1], _T("Yellow Ellipse, Red Triangle, Blue Rectangle"));
  233.     _tcscpy(rgtszIconDescrip[2], _T("Red Ellipse, Blue Triangle, Yellow Rectangle"));
  234.     _tcscpy(rgtszIconDescrip[3], _T("Red Ellipse, Yellow Triangle, Blue Rectangle"));
  235.  
  236.     _tcscpy(rgtszIconShortDesc[0], _T("BE, YT, RR"));
  237.     _tcscpy(rgtszIconShortDesc[1], _T("YE, RT, BR"));
  238.     _tcscpy(rgtszIconShortDesc[2], _T("RE, BT, YR"));
  239.     _tcscpy(rgtszIconShortDesc[3], _T("RE, YT, BR"));
  240.  
  241.  
  242.     pApp = (CCtrldemoApp *)AfxGetApp();
  243.     m_listctrl.SetImageList(m_pimagelist, LVSIL_NORMAL);
  244.     m_listctrl.SetImageList(m_pimagelistSmall, LVSIL_SMALL);
  245.     m_listctrl.GetWindowRect(&rect);
  246.  
  247.     for (i = 0; i < 2; i++)  // add the columns to the list control
  248.     {
  249.         lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  250.         lvcolumn.fmt = LVCFMT_LEFT;
  251.         lvcolumn.pszText = rgtsz[i];
  252.         lvcolumn.iSubItem = i;
  253.         lvcolumn.cx = rect.Width() * (i + 1) / 3;  // SubItem is twice as large
  254.         m_listctrl.InsertColumn(i, &lvcolumn);  // assumes return value is OK.
  255.     }
  256.  
  257.     for (iItem = 0; iItem < 50; iItem++)  // will now insert the items and subitems into the list view.
  258.         for (iSubItem = 0; iSubItem < 2; iSubItem++)
  259.         {
  260.             if (iSubItem == 0)
  261.                 iIcon = rand() % 4;  // choose the icon and legend for the entry
  262.  
  263.             lvitem.mask = LVIF_TEXT | (iSubItem == 0? LVIF_IMAGE : 0);
  264.             lvitem.iItem = (iSubItem == 0)? iItem : iActualItem;
  265.             lvitem.iSubItem = iSubItem;
  266.             lvitem.pszText = iSubItem == 0? rgtszIconShortDesc[iIcon] : rgtszIconDescrip[iIcon];
  267.  
  268.             lvitem.iImage = iIcon;
  269.             if (iSubItem == 0)
  270.                 iActualItem = m_listctrl.InsertItem(&lvitem);
  271.             else
  272.                 m_listctrl.SetItem(&lvitem);
  273.         }
  274. }
  275.  
  276. void CListCtrlPage::RenewListCtrl(long lStyle, BOOL bSetBits)
  277. {
  278.     long    lStyleOld;
  279.     CRect    rect;
  280.  
  281.     lStyleOld = GetWindowLong(m_listctrl.m_hWnd, GWL_STYLE);
  282.     lStyleOld &= ~lStyle;
  283.     if (bSetBits)
  284.         lStyleOld |= lStyle;
  285.  
  286.     m_listctrl.GetWindowRect(&rect);
  287.     ScreenToClient(&rect);
  288.     m_listctrl.DestroyWindow();
  289.     m_listctrl.Create(lStyleOld, rect, this, IDC_LISTVIEW1);
  290.     m_listctrl.SetImageList(m_pimagelist, LVSIL_NORMAL);
  291.     m_listctrl.SetImageList(m_pimagelistSmall, LVSIL_SMALL);
  292.     m_listctrl.GetWindowRect(&rect);
  293.     FillListCtrl();
  294. }
  295.  
  296. BOOL CListCtrlPage::OnInitDialog()
  297. {
  298.     CCtrldemoApp    *pApp;
  299.     CRect            rect;
  300.     UINT            rgIndicators[] = {ID_SEPARATOR, ID_INDICATOR_CAPS, ID_INDICATOR_NUM};
  301.  
  302.     CPropertyPage::OnInitDialog();  // let the base class do the default work
  303.     UpdateData(TRUE/*bSaveAndValidate*/);  // bring the information from the dialog.
  304.     pApp = (CCtrldemoApp *)AfxGetApp();
  305.     srand((unsigned) time(NULL));  // start the random number generator
  306.     m_pimagelist = new CImageList();        
  307.     m_pimagelistSmall = new CImageList();
  308.     ASSERT(m_pimagelist != NULL && m_pimagelistSmall != NULL);    // no serious allocation failure checking
  309.     m_pimagelist->Create(32, 32, TRUE/*bMask*/,    LISTICONCOUNT/*nInitial*/, 4/*nGrow*/);
  310.     m_pimagelistSmall->Create(16, 16, TRUE/*bMask*/, LISTICONCOUNT, 4);
  311.     m_pimagelist->Add(pApp->LoadIcon(IDI_ICONLIST1));
  312.     m_pimagelist->Add(pApp->LoadIcon(IDI_ICONLIST2));
  313.     m_pimagelist->Add(pApp->LoadIcon(IDI_ICONLIST3));
  314.     m_pimagelist->Add(pApp->LoadIcon(IDI_ICONLIST4));
  315.     m_pimagelistSmall->Add(pApp->LoadIcon(IDI_ICONLIST1));
  316.     m_pimagelistSmall->Add(pApp->LoadIcon(IDI_ICONLIST2));
  317.     m_pimagelistSmall->Add(pApp->LoadIcon(IDI_ICONLIST3));
  318.     m_pimagelistSmall->Add(pApp->LoadIcon(IDI_ICONLIST4));
  319.  
  320.     FillListCtrl();
  321.     GetDlgItem(IDC_ALIGN)->EnableWindow(FALSE);  // default is  report mode Does not support this
  322.     GetDlgItem(IDC_AUTOARRANGE)->EnableWindow(FALSE);
  323.     return FALSE;  // there is no change in any control focus stuff here.
  324. }
  325.  
  326. void CListCtrlPage::ShowNotification(LPNMHDR pnmhdr, LRESULT *pResult)
  327. {
  328.     ((CNotifyShowCase *)GetDlgItem(IDC_LISTSTATUS))->ShowNotification(pnmhdr->code);
  329. }
  330.