//***************************************************************************** //* //* //* TreeListCtrl.cpp //* //* //***************************************************************************** // // (C) Copyright Anton Zechner 2007 // // This code creates a Tree-List-Control for MFC // #include "StdAfx.h" #include "TreeListCtrl.h" #include "rxmfcapi.h" IMPLEMENT_DYNAMIC(CTreeListCtrl, CWnd) BEGIN_MESSAGE_MAP(CTreeListCtrl, CTreeCtrl) //{{AFX_MSG_MAP(CMyTreeListCtrl) ON_WM_KEYDOWN() //}}AFX_MSG_MAP END_MESSAGE_MAP() //***************************************************************************** //* //* Create //* //***************************************************************************** // Creates a new Tree-List-Control // dwStyle : Style of the Window // TVS_CHECKBOXES // TVS_DISABLEDRAGDROP // TVS_EDITLABELS // TVS_FULLROWSELECT // TVS_HASBUTTONS // TVS_HASLINES // TVS_INFOTIP // TVS_LINESATROOT // TVS_NONEVENHEIGHT // TVS_NOSCROLL // TVS_NOTOOLTIPS // TVS_RTLREADING // TVS_SHOWSELALWAYS // TVS_SINGLEEXPAND // TVS_TRACKSELECT // sRect : Rectangle wher the window should be shown // pParentWnd : Pointer to parent window // nId : Is the ID of the window // Returns TRUE if the window was created or FALSE if an error occurs BOOL CTreeListCtrl::Create(DWORD dwStyle, const RECT &sRect, CWnd* pParentWnd,UINT nId) { TreeListRegister(); return CWnd::Create(_T("TreeList"),NULL,dwStyle,sRect,pParentWnd,nId); } //***************************************************************************** //* //* InsertColumn //* //***************************************************************************** // Inserts a column in the header of the Tree-List-Control // nCol : Position where the column should be inserted (use -1 for end) // pColumnText : Text for the column // nFormat : Ist the format for the text // TVCFMT_CENTER // TVCFMT_IMAGE // TVCFMT_RIGHT // TVCFMT_LEFT // nWidth : Is the width for the column in pixels (use -1 for an auto expanding column) // Returns the position of the column or -1 if an error occurs int CTreeListCtrl::InsertColumn(int nCol,LPCTSTR pColumnText,int nFormat,int nWidth) { TV_COLUMN sItem; ASSERT(::IsWindow(m_hWnd)); sItem.mask = 0; if(pColumnText) { sItem.mask |= LVCF_TEXT; sItem.pszText = (LPTSTR)pColumnText; sItem.cchTextMax = 256; } if(nFormat>=0) { sItem.mask |= LVCF_FMT; sItem.fmt = nFormat; } if(nWidth>=0) { sItem.mask |= LVCF_WIDTH; sItem.cx = nWidth; } return InsertColumn(nCol,&sItem); } //***************************************************************************** //* //* SetItem //* //***************************************************************************** // Changes some properties of an item // hItem : is the handle of the item // nCol : is the column for the text (0=tree column) // nMask : defies which propaties should be set (TVIF_????) // pText : is the new text of the item (add TVIF_TEXT to nMask) // nImage : is the new image for the item (add TVIF_IMAGE to nMask) // nSelectedImage : is the new image for the item (add TVIF_SELECTEDIMAGE to nMask) // nState : is the new state for the item (add TVIF_STATE to nMask) // nStateMask : is a mask for the states // lParam : is the new LPARAM parameter for the item (add TVIF_PARAM to nMask) // Returns TRUE if the text was changed or FALSE if an error occurs BOOL CTreeListCtrl::SetItem(HTREEITEM hItem,int nCol,UINT nMask,LPCTSTR pText,int nImage,int nSelectedImage,UINT nState, UINT nStateMask,LPARAM lParam) { TV_ITEM sItem; ASSERT(::IsWindow(m_hWnd)); sItem.mask = nMask|TVIF_SUBITEM; sItem.hItem = hItem; sItem.state = nState; sItem.stateMask = nStateMask; sItem.pszText = (LPTSTR)pText; sItem.cchTextMax =(pText)? 256:0; sItem.iImage = nImage; sItem.iSelectedImage = nSelectedImage; sItem.cChildren = nCol; sItem.lParam = lParam; return SetItem(&sItem); } //***************************************************************************** //* //* SetItemText //* //***************************************************************************** // Set the text of an item // hItem : Is the handle of the item // pText : Is the new text of the item // nCol : Is the column for the text (0=tree column) // Returns TRUE if the text was changed or FALSE if an error occurs BOOL CTreeListCtrl::SetItemText(HTREEITEM hItem,LPCTSTR pText,int nCol) { TV_ITEM sItem; ASSERT(::IsWindow(m_hWnd)); sItem.mask = TVIF_SUBITEM|TVIF_TEXT; sItem.hItem = hItem; sItem.stateMask = 0; sItem.pszText = (LPTSTR)pText; sItem.cchTextMax = (pText)? 256:0; sItem.cChildren = nCol; return SetItem(&sItem); } //***************************************************************************** //* //* ListDeleteItem //* //***************************************************************************** // Deletes an item in a Tree-List-Control which is used as List-Control // iRow : Is the row of the item // Returns TRUE if the item was deleted or FALSE if an error occurs BOOL CTreeListCtrl::ListDeleteItem(int iRow) { ASSERT(::IsWindow(m_hWnd)); return DeleteItem(GetItemOfRow(iRow)); } //***************************************************************************** //* //* ListInsertItem //* //***************************************************************************** // Inserts an item in a Tree-List-Control which is used as List-Control // iRow : Is the row of the item // pText : Is the text for the item // nImage : Is the number for the icon (use TV_NOIMAGE for no icon) // nState : Is the state of the image // TVIS_BOLD = text is bolded // TVIS_UNTERLINE = text is underlined // TVIS_SELECTED = sItem is selected // TVIS_OVERLAYMASK = overlay bits for image // TVIS_STATEIMAGEMASK = image for state icons // iMask : Is the mask of bits which are used in the nState parameter // TVIS_BOLD = text is bolded // TVIS_UNTERLINE = text is underlined // TVIS_SELECTED = sItem is selected // TVIS_OVERLAYMASK = overlay bits for image // TVIS_STATEIMAGEMASK = image for state icons // Returns the insert position of the item or -1 if an error occurs int CTreeListCtrl::ListInsertItem(int iRow,LPCTSTR pText,int nImage,int nState,int iMask) { HTREEITEM hItem; TVINSERTSTRUCT sData; ASSERT(::IsWindow(m_hWnd)); sData.item.mask = 0; if(pText) { sData.item.mask |= TVIF_TEXT; sData.item.pszText = (LPTSTR)pText; sData.item.cchTextMax = 256; } if(nImage>=0) { sData.item.mask |= TVIF_SELECTEDIMAGE|TVIF_IMAGE ; sData.item.iImage = nImage; sData.item.iSelectedImage = nImage; } if(nState>=0) { sData.item.mask |= TVIF_STATE; sData.item.state = nState; sData.item.stateMask = iMask&(TVIS_BOLD|TVIS_UNTERLINE|TVIS_SELECTED|TVIS_OVERLAYMASK|TVIS_STATEIMAGEMASK); } hItem = GetItemOfRow(iRow); if(!hItem) { sData.hInsertAfter = TVI_LAST; sData.hParent = TVI_ROOT; } else{ sData.hParent = hItem; sData.hInsertAfter = TVI_BEFORE; } hItem = InsertItem(&sData); return GetRowOfItem(hItem); } //***************************************************************************** //* //* ListSetItem //* //***************************************************************************** // Changes an item in a Tree-List-Control which is used as List-Control // iRow : Is the row of the item // nCol : Is the column of the item // pText : Is the text for the item // nImage : Is the number for the icon (use TV_NOIMAGE for no icon) // nState : Is the state of the image // TVIS_BOLD = text is bolded // TVIS_UNTERLINE = text is underlined // TVIS_SELECTED = sItem is selected // TVIS_OVERLAYMASK = overlay bits for image // TVIS_STATEIMAGEMASK = image for state icons (only for column 0) // iMask : Is the mask of bits which are used in the nState parameter // TVIS_BOLD = text is bolded // TVIS_UNTERLINE = text is underlined // TVIS_SELECTED = sItem is selected // TVIS_OVERLAYMASK = overlay bits for image // TVIS_STATEIMAGEMASK = image for state icons (only for column 0) // Returns TRUE if ok or FALSE if an error occurs BOOL CTreeListCtrl::ListSetItem(int iRow,int nCol,LPCTSTR pText,int nImage,int nState,int iMask) { TV_ITEM sData; ASSERT(::IsWindow(m_hWnd)); sData.mask = TVIF_HANDLE|TVIF_SUBITEM; sData.hItem = GetItemOfRow(iRow); sData.cChildren = nCol; if(!sData.hItem)return FALSE; if(pText) { sData.mask |= TVIF_TEXT; sData.pszText = (LPTSTR)pText; sData.cchTextMax = 256; } if(nImage>=0) { sData.mask |= TVIF_SELECTEDIMAGE|TVIF_IMAGE ; sData.iImage = nImage; sData.iSelectedImage = nImage; } if(nState>=0) { sData.mask |= TVIF_STATE; sData.state = nState; sData.stateMask = iMask&(TVIS_BOLD|TVIS_UNTERLINE|TVIS_SELECTED); } return SetItem(&sData); } //***************************************************************************** //* //* ListSetColor //* //***************************************************************************** // Changes the colors of an item in a Tree-List-Control which is used as List-Control // iRow : Is the row of the item // nCol : Is the column of the item // uBkColor : Is the new background color (use TV_NOCOLOR for the default color) // uTextColor : Is the new text color (use TV_NOCOLOR for the default color) // Returns TRUE if ok or FALSE if an error occurs BOOL CTreeListCtrl::ListSetColor(int iRow,int nCol,COLORREF uBkColor,COLORREF uTextColor) { HTREEITEM hItem; ASSERT(::IsWindow(m_hWnd)); hItem = GetItemOfRow(iRow); if(!hItem)return FALSE; SetItemBkColor (hItem,nCol,uBkColor); SetItemTextColor(hItem,nCol,uTextColor); return TRUE; } //***************************************************************************** //* //* ListGetColor //* //***************************************************************************** // Gets the colors of an item in a Tree-List-Control which is used as List-Control // iRow : Is the row of the item // nCol : Is the column of the item // uBkColor : Is the new background color (TV_NOCOLOR for the default color) // uTextColor : Is the new background color (TV_NOCOLOR for the default color) // Returns TRUE if ok or FALSE if an error occurs BOOL CTreeListCtrl::ListGetColor(int iRow,int nCol,COLORREF &uBkColor,COLORREF &uTextColor) { HTREEITEM hItem; hItem = GetItemOfRow(iRow); if(!hItem) { uBkColor = TV_NOCOLOR; uTextColor = TV_NOCOLOR; return FALSE; } uBkColor = GetItemBkColor (hItem,nCol); uTextColor = GetItemTextColor(hItem,nCol); return TRUE; } //***************************************************************************** //* //* GetItemState //* //***************************************************************************** // Gets the state of an Item // hItem : Is the handle of the item // nStateMask : Is the state mask of the requested states // nCol : Is the column of the item // Returns the state bits of the item UINT CTreeListCtrl::GetItemState(HTREEITEM hItem,UINT uMask,int nCol) { TV_ITEM sItem; ASSERT(::IsWindow(m_hWnd)); sItem.mask = TVIF_STATE|TVIF_HANDLE|TVIF_SUBITEM; sItem.stateMask = uMask; sItem.hItem = hItem; sItem.cChildren = nCol; if(!GetItem(&sItem))return 0; return sItem.state; } //***************************************************************************** //* //* SetItemState //* //***************************************************************************** // Sets the state of an Item // hItem : Is the handle of the item // nCol : Is the column of the item // nState : New state bits // nStateMask : Mask of the bits which should be changed // Returns the state bits of the item BOOL CTreeListCtrl::SetItemState(HTREEITEM hItem,int nCol,UINT nState,UINT nStateMask) { TV_ITEM sItem; ASSERT(::IsWindow(m_hWnd)); sItem.mask = TVIF_STATE|TVIF_HANDLE|TVIF_SUBITEM; sItem.hItem = hItem; sItem.cChildren = nCol; sItem.stateMask = nStateMask; sItem.state = nState; return SetItem(&sItem); } //***************************************************************************** //* //* GetItemText //* //***************************************************************************** // Gets the text of an item // hItem : Is the handle of the item // nCol : Is the column of the item // Returns a pointer to the text or NULL if an error occurs LPCTSTR CTreeListCtrl::GetItemText(HTREEITEM hItem,int nCol) { TV_ITEM sItem; ASSERT(::IsWindow(m_hWnd)); sItem.mask = TVIF_TEXT|TVIF_SUBITEM|TVIF_TEXTPTR; sItem.hItem = hItem; sItem.cChildren = nCol; if(!GetItem(&sItem))return 0; return sItem.pszText; } //***************************************************************************** //* //* GetItemText //* //***************************************************************************** // Gets the text of an item and stores it in an buffer // hItem : Is the handle of the item // pBuffer : Is the text buffer where the text will be saved // iMax : Is the size of the text buffer in chars // nCol : Is the column of the item // Returns a pointer to the text or NULL if an error occurs BOOL CTreeListCtrl::GetItemText(HTREEITEM hItem,LPTSTR pBuffer,int iMax,int nCol) { TV_ITEM sItem; ASSERT(::IsWindow(m_hWnd)); if(iMax<=0)return FALSE; pBuffer[0] = 0; sItem.mask = TVIF_TEXT|TVIF_SUBITEM; sItem.hItem = hItem; sItem.pszText = pBuffer; sItem.cchTextMax = iMax; sItem.cChildren = nCol; return GetItem(&sItem); } //***************************************************************************** //* //* GetItemImageEx //* //***************************************************************************** // Reads the image of an entry // hItem : Is the handle of the item // nCol : Is the column of the item // Returns the image number or -1 if an error occurs int CTreeListCtrl::GetItemImageEx(HTREEITEM hItem,int nCol) { TVITEM sItem; ASSERT(::IsWindow(m_hWnd)); sItem.mask = TVIF_IMAGE|TVIF_SUBITEM; sItem.hItem = hItem; sItem.cChildren = nCol; if(!GetItem(&sItem))return -1; return sItem.iImage; } //***************************************************************************** //* //* GetItemImageEx //* //***************************************************************************** // Reads the image of an entry // hItem : Is the handle of the item // nImage : Here the image number will be stored // nCol : Is the column of the item // Returns TRUE if the image number was detected or FALSE if an error occurs BOOL CTreeListCtrl::GetItemImageEx(HTREEITEM hItem,int *pImage,int nCol) { TVITEM sItem; ASSERT(::IsWindow(m_hWnd)); sItem.mask = TVIF_IMAGE|TVIF_SUBITEM; sItem.hItem = hItem; sItem.cChildren = nCol; if(!GetItem(&sItem))return FALSE; pImage[0] = sItem.iImage; return TRUE; } //***************************************************************************** //* //* SetItemImageEx //* //***************************************************************************** // Changes the image of an entry // hItem : Is the handle of the item // nImage : Is the new the image number // nCol : Is the column of the item // Returns TRUE if the image number was detected or FALSE if an error occurs BOOL CTreeListCtrl::SetItemImageEx(HTREEITEM hItem,int nImage, int nCol) { TVITEM sItem; ASSERT(::IsWindow(m_hWnd)); sItem.mask = TVIF_IMAGE|TVIF_SUBITEM; sItem.hItem = hItem; sItem.iImage = nImage; sItem.cChildren = nCol; return SetItem(&sItem); } #define IsCTRLpressed() ( (GetKeyState(VK_CONTROL) & (1 << (sizeof(SHORT)*8-1))) != 0 ) IMPLEMENT_DYNAMIC(CMyTreeListCtrl, CTreeListCtrl) BEGIN_MESSAGE_MAP(CMyTreeListCtrl, CTreeListCtrl) //{{AFX_MSG_MAP(CMyTreeListCtrl) ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave) ON_WM_ERASEBKGND() ON_WM_MOUSEMOVE() //}}AFX_MSG_MAP END_MESSAGE_MAP() CMyTreeListCtrl::CMyTreeListCtrl() { m_bTracking=FALSE; } CMyTreeListCtrl::~CMyTreeListCtrl() { } LRESULT CMyTreeListCtrl::OnMouseLeave(WPARAM wParam, LPARAM lParam) { CRect rect; CPoint point; GetClientRect(&rect); ClientToScreen(&rect); GetCursorPos(&point); if(!PtInRect(rect,point)) { CWnd *pWnd = acedGetAcadDwgView(); if(pWnd) pWnd->SetFocus(); } return 0; } void CMyTreeListCtrl::OnMouseMove(UINT nFlags, CPoint point) { // if (!m_bTracking) { TRACKMOUSEEVENT tme; tme.cbSize = sizeof(tme); tme.hwndTrack = m_hWnd; tme.dwFlags = TME_LEAVE | TME_HOVER; tme.dwHoverTime = 1; m_bTracking = _TrackMouseEvent(&tme); } CTreeListCtrl::OnMouseMove(nFlags, point); } BOOL CMyTreeListCtrl::OnEraseBkgnd(CDC *pDC) { return TRUE; } void CMyTreeListCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { CTreeListCtrl::OnKeyDown(nChar, nRepCnt, nFlags); }