PageTreeCtrl.cpp
资源名称:propgrid.zip [点击查看]
上传用户:yangzi5763
上传日期:2007-01-02
资源大小:239k
文件大小:10k
源码类别:
ActiveX/DCOM/ATL
开发平台:
Visual C++
- /************************************
- REVISION LOG ENTRY
- Revision By: Mihai Filimon
- Revised on 12/4/98 12:48:57 PM
- Comments: PageTreeCtrl.cpp: implementation of the CPageTreeCtrl class.
- ************************************/
- #include "stdafx.h"
- #include "xpropertieswnd.h"
- #include "PageTreeCtrl.h"
- #include "XPropertiesWndCtl.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- // Function name : CPageTreeCtrl::CPageTreeCtrl
- // Description : default constructor
- // Return type :
- CPageTreeCtrl::CPageTreeCtrl(CXPropertiesWndCtrl* pControl, int nHeight ):
- CPageListCtrl(pControl, nHeight)
- {
- }
- // Function name : CPageTreeCtrl::~CPageTreeCtrl
- // Description : virtual destructor
- // Return type :
- CPageTreeCtrl::~CPageTreeCtrl()
- {
- }
- BEGIN_MESSAGE_MAP(CPageTreeCtrl, CPageListCtrl)
- //{{AFX_MSG_MAP(CPageTreeCtrl)
- ON_WM_LBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // Function name : CPageTreeCtrl::OnInit
- // Description : Called by control, when this page has to be initialized
- // Return type : BOOL
- BOOL CPageTreeCtrl::OnInit()
- {
- ASSERT(m_allItemsList.Init(this));
- // Insert one column for tree representation
- InsertColumn(0, NULL, LVCFMT_LEFT, 16);
- return CPageListCtrl::OnInit();
- }
- static const int dRect = 9;
- static const int dDXPlus = 3;
- // Function name : CPageTreeCtrl::DrawMinus
- // Description :
- // Return type : void
- // Argument : CDC *pDC
- // Argument : CPoint p
- void CPageTreeCtrl::DrawMinus(CDC *pDC, CPoint p)
- {
- //Draw the minus
- pDC->MoveTo(p);
- pDC->LineTo(p + CSize(dDXPlus,0));
- pDC->MoveTo(p);
- pDC->LineTo(p - CSize(dDXPlus,0));
- }
- // Function name : CPageTreeCtrl::DrawPlus
- // Description :
- // Return type : void
- // Argument : CDC *pDC
- // Argument : CPoint p
- void CPageTreeCtrl::DrawPlus(CDC *pDC, CPoint p)
- {
- //Draw the plus
- pDC->MoveTo(p);
- pDC->LineTo(p + CSize(0, dDXPlus));
- pDC->MoveTo(p);
- pDC->LineTo(p - CSize(0, dDXPlus));
- }
- // Function name : CPageTreeCtrl::OnDrawItem
- // Description : On draw item
- // Return type : void
- void CPageTreeCtrl::OnBeginDrawItem( CDC* pDC, int nItem , LPARAM itemData)
- {
- if (IsGroupData(itemData))
- {
- CRect rLabel;
- GetItemRect(nItem, &rLabel, LVIR_LABEL);
- CRect rDraw(rLabel);
- rDraw.InflateRect(-CSize((rLabel.Width() - dRect) / 2, (rLabel.Height() - dRect) / 2));
- rDraw.right = rDraw.left + dRect;
- rDraw.bottom = rDraw.top + dRect;
- pDC->FrameRect(rDraw, &CBrush(RGB(128,128,128)));
- CPoint p((rDraw.left + rDraw.right)/2, (rDraw.top + rDraw.bottom)/2);
- DrawMinus(pDC, p);
- CControlsWnd_CGroup* pGroup = (CControlsWnd_CGroup*)GetControl(nItem);
- if (pGroup)
- if (!pGroup->IsExtended())
- DrawPlus(pDC, p);
- m_pDrawFont = GetControlWnd()->SelectStockFont(pDC);
- }
- else
- CPageListCtrl::OnBeginDrawItem( pDC, nItem, itemData );
- }
- // Function name : CPageTreeCtrl::OnFontChanged
- // Description : Called when font is changed
- // Return type : void
- void CPageTreeCtrl::OnFontChanged()
- {
- Invalidate();
- }
- // Function name : CPageTreeCtrl::InsertItemVrt
- // Description : Called when new property is added into control
- // Return type : int
- // Argument : int nItem
- // Argument : LPCTSTR lpszItemName
- // Argument : LPARAM lParam
- // Description lParam parameter: MAKELPARAM(asControl, asType)
- int CPageTreeCtrl::InsertItemVrt(int nItem, LPCTSTR lpszItemName, LPARAM lParam, LPCTSTR lpszGroup)
- {
- int nResult = m_allItemsList.OnInsertItemVrt(nItem, lpszItemName, lParam, lpszGroup);
- if (IsGroupData(lParam))
- {
- int nGroup = CPageListCtrl::InsertItemVrt( nItem, lpszItemName, lParam , lpszGroup);
- CControlsWnd_CGroup* pGroup = (CControlsWnd_CGroup*)GetControl(nGroup, FALSE);
- // Group must be not deleted item...
- ASSERT (pGroup);
- pGroup->Update(&m_allItemsList);
- return nResult;
- }
- UpdateAllGroup();
- return nResult;
- }
- // Function name : CPageTreeCtrl::UpdateAllGroup
- // Description : Update all group
- // Return type : void
- void CPageTreeCtrl::UpdateAllGroup()
- {
- // We have to search all group and update them
- int n = GetItemCount();
- for (int i = 0; i < n; i++)
- if (IsGroup(i))
- {
- CControlsWnd_CGroup* pGroup = (CControlsWnd_CGroup*)GetControl(i, FALSE);
- ASSERT (pGroup);
- pGroup->Update(&m_allItemsList);
- }
- }
- // Function name : CPageTreeCtrl::DeleteItemVrt
- // Description : Called when a propery need to be deleted
- // Return type : BOOL
- // Argument : int nItem
- BOOL CPageTreeCtrl::DeleteItemVrt(int nItem)
- {
- return CPageListCtrl::DeleteItemVrt(nItem);
- }
- // Function name : CPageTreeCtrl::Expand
- // Description :
- // Return type : void
- // Argument : int nItem
- // Argument : BOOL &bExppanded
- void CPageTreeCtrl::Expand(int nItem, BOOL &bExppanded)
- {
- ResetEditControl();
- BOOL bVisible = GetStyle() & WS_VISIBLE;
- BOOL bVScroll = GetStyle() & WS_VSCROLL;
- ModifyStyle(WS_VISIBLE, 0, SWP_NOREDRAW);
- // Item nItem have to be a group item
- ASSERT(IsGroup(nItem));
- CControlsWnd_CGroup* pGroup = (CControlsWnd_CGroup*)GetControl(nItem, FALSE);
- ASSERT (pGroup);
- TArrayIDItem* pChilds = pGroup->GetChilds();
- int n = pChilds->GetSize();
- bExppanded = !bExppanded;
- // Prepare controls
- if (int nStep = bExppanded ? n : -n)
- {
- int nCount = GetItemCount();
- if ( bExppanded )
- {
- for (int j = nCount - 1; j >= nItem + 1; j--)
- if (CControlsWnd* pControl = GetControl(j))
- pControl->RefreshNDelete(pControl, pControl->GetSelectedItem() + nStep );
- }
- else
- {
- for (int j = nItem + 1; j < nCount; j++ )
- if (CControlsWnd* pControl = GetControl(j))
- if (pControl->IsGroup())
- pControl->RefreshNDelete(pControl, pControl->GetSelectedItem() + nStep);
- else
- pControl->Kill();
- }
- }
- // expand the item
- if (bExppanded)
- {
- int nNextItem = 1 + nItem;
- for (int i = 0; i < n; i++)
- {
- int nItem = (*pChilds)[i], nInserted;
- nNextItem = 1 + (nInserted = CPageListCtrl::InsertItemVrt(nNextItem, m_allItemsList.GetName(nItem), m_allItemsList.GetUserData(nItem), m_allItemsList.GetGroup(nItem)));
- CString sValue = m_allItemsList.GetValue(nItem);
- SetDefaultValue(nInserted, sValue);
- }
- }
- else
- // collapse it
- for (int i = 0; i < n; i++)
- CPageListCtrl::DeleteItemVrt(nItem + 1);
- EnsureVisible(nItem, TRUE);
- RedrawItems(nItem, nItem);
- ModifyStyle(0, WS_VISIBLE, SWP_NOREDRAW);
- InvalidateNew();
- BOOL bNVscroll = GetStyle() & WS_VSCROLL;
- if (bVScroll != bNVscroll)
- if (bNVscroll)
- {
- //Redraw the nc area of vertical scrool bar
- SetWindowPos(0, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED | SWP_DRAWFRAME);
- }
- CreateEditControl(nItem);
- }
- // Function name : CPageTreeCtrl::OnLoad
- // Description :
- // Return type : int
- // Argument : LPCTSTR lpszName
- // Argument : LPCTSTR lpszAs
- // Argument : LPCTSTR lpszType
- // Argument : LPCTSTR lpszGroup
- int CPageTreeCtrl::OnLoad(LPCTSTR lpszName, LPCTSTR lpszAs, LPCTSTR lpszType, LPCTSTR lpszGroup)
- {
- static const CString enable(_T("Enable"));
- static const CString grayed(_T("Grayed"));
- int nItem = InsertItemVrt(m_allItemsList.GetItemCount(), lpszName, MAKELPARAM(m_asControls.GetID(lpszAs), enable.CompareNoCase(lpszType) == 0 ? 0 : (grayed.CompareNoCase(lpszType)) == 0 ? 1 : 2), lpszGroup);
- return nItem;
- }
- // Function name : CPageTreeCtrl::SetPropertyName
- // Description : PageTreeCtrl do not accepts the changing one property name
- // Return type : void
- // Argument : int nIndexProperty
- // Argument : LPCTSTR lpszPropertyName
- void CPageTreeCtrl::SetPropertyName(int nIndexProperty, LPCTSTR lpszPropertyName)
- {
- // do nothing
- }
- // Function name : CPageTreeCtrl::SetDefaultValue
- // Description : Don't forget to save the default value into m_allItemsList
- // Return type : BOOL
- // Argument : int nIndexProperty
- // Argument : LPCTSTR lpszdefaultValue
- BOOL CPageTreeCtrl::SetDefaultValue(int nIndexProperty, LPCTSTR lpszdefaultValue)
- {
- BOOL bResult = CPageListCtrl::SetDefaultValue(nIndexProperty, lpszdefaultValue );
- if (bResult)
- if (!IsGroup(nIndexProperty))
- {
- int nIndexGroup = nIndexProperty;
- while (--nIndexGroup >= 0)
- if (IsGroup(nIndexGroup))
- {
- CControlsWnd_CGroup* pGroup = (CControlsWnd_CGroup*)GetControl(nIndexGroup);
- int nIndex = nIndexProperty - nIndexGroup - 1;
- ASSERT (nIndex >= 0 );
- ASSERT (nIndex < pGroup->GetChilds()->GetSize());
- m_allItemsList.SetValue(pGroup->GetChilds()->GetAt(nIndex), lpszdefaultValue);
- break;
- }
- }
- return bResult;
- }
- // Function name : CPageTreeCtrl::IsGroup
- // Description : return TRUE if the item nItem is group or not
- // Return type : BOOL
- // Argument : int nItem
- BOOL CPageTreeCtrl::IsGroup(int nItem)
- {
- if (nItem >= 0)
- if (nItem < GetItemCount())
- {
- LPARAM lParam = GetItemData(nItem);
- return IsGroupData(lParam);
- }
- return FALSE;
- }
- // Function name : CPageTreeCtrl::IsGroupData
- // Description :
- // Return type : BOOL
- // Argument : DWORD lParam
- BOOL CPageTreeCtrl::IsGroupData(DWORD lParam)
- {
- return m_asControls.IsGroup(LOWORD(lParam));
- }
- // Function name : CPageTreeCtrl::DeleteAll
- // Description : Called to remove all items
- // Return type : void
- void CPageTreeCtrl::DeleteAll()
- {
- CPageListCtrl::DeleteAll();
- m_allItemsList.DeleteAllItems();
- }
- // Function name : CPageTreeCtrl::OnLButtonDown
- // Description : Check if user click in first column....
- // Return type : void
- // Argument : UINT nFlags
- // Argument : CPoint point
- void CPageTreeCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CPageListCtrl::OnLButtonDown(nFlags, point);
- int nItem = HitTest(point);
- if (nItem >= 0)
- {
- CRect rect; GetItemRect(nItem, &rect, LVIR_LABEL );
- if (rect.PtInRect(point))
- if (IsGroup(nItem))
- {
- CControlsWnd_CGroup* pGroup = (CControlsWnd_CGroup*)GetControl(nItem);
- ASSERT (pGroup);
- Expand(nItem, pGroup->m_bExpanded);
- }
- }
- }