#include "wx/window.h"
#include "wx/msw/private.h"
-#ifndef WX_PRECOMP
- #include "wx/settings.h"
-#endif
-
// Mingw32 is a bit mental even though this is done in winundef
#ifdef GetFirstChild
#undef GetFirstChild
#include "wx/log.h"
#include "wx/dynarray.h"
#include "wx/imaglist.h"
-#include "wx/msw/treectrl.h"
+#include "wx/treectrl.h"
#ifdef __GNUWIN32__
+#ifndef wxUSE_NORLANDER_HEADERS
#include "wx/msw/gnuwin32/extra.h"
#endif
+#endif
-#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
+#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
#include <commctrl.h>
#endif
// a convenient wrapper around TV_ITEM struct which adds a ctor
struct wxTreeViewItem : public TV_ITEM
{
- wxTreeViewItem(const wxTreeItemId& item,
- UINT mask_, UINT stateMask_ = 0)
+ wxTreeViewItem(const wxTreeItemId& item, // the item handle
+ UINT mask_, // fields which are valid
+ UINT stateMask_ = 0) // for TVIF_STATE only
{
- mask = mask_;
+ // hItem member is always valid
+ mask = mask_ | TVIF_HANDLE;
stateMask = stateMask_;
hItem = (HTREEITEM) (WXHTREEITEM) item;
}
};
+// a class which encapsulates the tree traversal logic: it vists all (unless
+// OnVisit() returns FALSE) items under the given one
+class wxTreeTraversal
+{
+public:
+ wxTreeTraversal(const wxTreeCtrl *tree)
+ {
+ m_tree = tree;
+ }
+
+ // do traverse the tree: visit all items (recursively by default) under the
+ // given one; return TRUE if all items were traversed or FALSE if the
+ // traversal was aborted because OnVisit returned FALSE
+ bool DoTraverse(const wxTreeItemId& root, bool recursively = TRUE);
+
+ // override this function to do whatever is needed for each item, return
+ // FALSE to stop traversing
+ virtual bool OnVisit(const wxTreeItemId& item) = 0;
+
+protected:
+ const wxTreeCtrl *GetTree() const { return m_tree; }
+
+private:
+ bool Traverse(const wxTreeItemId& root, bool recursively);
+
+ const wxTreeCtrl *m_tree;
+};
+
// ----------------------------------------------------------------------------
// macros
// ----------------------------------------------------------------------------
// implementation
// ============================================================================
+// ----------------------------------------------------------------------------
+// tree traversal
+// ----------------------------------------------------------------------------
+
+bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
+{
+ if ( !OnVisit(root) )
+ return FALSE;
+
+ return Traverse(root, recursively);
+}
+
+bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
+{
+ long cookie;
+ wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
+ while ( child.IsOk() )
+ {
+ // depth first traversal
+ if ( recursively && !Traverse(child, TRUE) )
+ return FALSE;
+
+ if ( !OnVisit(child) )
+ return FALSE;
+
+ child = m_tree->GetNextChild(root, cookie);
+ }
+
+ return TRUE;
+}
+
// ----------------------------------------------------------------------------
// construction and destruction
// ----------------------------------------------------------------------------
m_textCtrl = NULL;
}
-bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
- const wxPoint& pos, const wxSize& size,
- long style, const wxValidator& validator,
+bool wxTreeCtrl::Create(wxWindow *parent,
+ wxWindowID id,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxValidator& validator,
const wxString& name)
{
Init();
- wxSystemSettings settings;
+ if ( !CreateControl(parent, id, pos, size, style, validator, name) )
+ return FALSE;
- SetName(name);
- SetValidator(validator);
+ DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
+ TVS_HASLINES | TVS_SHOWSELALWAYS;
- m_windowStyle = style;
+ if ( m_windowStyle & wxTR_HAS_BUTTONS )
+ wstyle |= TVS_HASBUTTONS;
- SetParent(parent);
+ if ( m_windowStyle & wxTR_EDIT_LABELS )
+ wstyle |= TVS_EDITLABELS;
- m_windowId = (id == -1) ? NewControlId() : id;
+ if ( m_windowStyle & wxTR_LINES_AT_ROOT )
+ wstyle |= TVS_LINESATROOT;
- DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_SHOWSELALWAYS ;
+#if !defined( __GNUWIN32__ ) && !defined(wxUSE_NORLANDER_HEADERS)
+ // we emulate the multiple selection tree controls by using checkboxes: set
+ // up the image list we need for this if we do have multiple selections
+ if ( m_windowStyle & wxTR_MULTIPLE )
+ wstyle |= TVS_CHECKBOXES;
+#endif
+ // Create the tree control.
+ if ( !MSWCreateControl(WC_TREEVIEW, wstyle) )
+ return FALSE;
- bool want3D;
- WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
+ // the treectrl with any other background looks ugly because the items
+ // background is white anyhow
+ SetBackgroundColour(*wxWHITE);
- // Even with extended styles, need to combine with WS_BORDER
- // for them to look right.
- if ( want3D || wxStyleHasBorder(m_windowStyle) )
+ // VZ: this is some experimental code which may be used to get the
+ // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
+ // AFAIK, the standard DLL does about the same thing anyhow.
+#if 0
+ if ( m_windowStyle & wxTR_MULTIPLE )
{
- wstyle |= WS_BORDER;
- }
+ wxBitmap bmp;
+
+ // create the DC compatible with the current screen
+ HDC hdcMem = CreateCompatibleDC(NULL);
+
+ // create a mono bitmap of the standard size
+ int x = GetSystemMetrics(SM_CXMENUCHECK);
+ int y = GetSystemMetrics(SM_CYMENUCHECK);
+ wxImageList imagelistCheckboxes(x, y, FALSE, 2);
+ HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size
+ 1, // # of color planes
+ 1, // # bits needed for one pixel
+ 0); // array containing colour data
+ SelectObject(hdcMem, hbmpCheck);
+
+ // then draw a check mark into it
+ RECT rect = { 0, 0, x, y };
+ if ( !::DrawFrameControl(hdcMem, &rect,
+ DFC_BUTTON,
+ DFCS_BUTTONCHECK | DFCS_CHECKED) )
+ {
+ wxLogLastError(_T("DrawFrameControl(check)"));
+ }
- if ( m_windowStyle & wxTR_HAS_BUTTONS )
- wstyle |= TVS_HASBUTTONS;
+ bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
+ imagelistCheckboxes.Add(bmp);
- if ( m_windowStyle & wxTR_EDIT_LABELS )
- wstyle |= TVS_EDITLABELS;
+ if ( !::DrawFrameControl(hdcMem, &rect,
+ DFC_BUTTON,
+ DFCS_BUTTONCHECK) )
+ {
+ wxLogLastError(_T("DrawFrameControl(uncheck)"));
+ }
- if ( m_windowStyle & wxTR_LINES_AT_ROOT )
- wstyle |= TVS_LINESATROOT;
+ bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
+ imagelistCheckboxes.Add(bmp);
- // Create the tree control.
- m_hWnd = (WXHWND)::CreateWindowEx
- (
- exStyle,
- WC_TREEVIEW,
- _T(""),
- wstyle,
- pos.x, pos.y, size.x, size.y,
- (HWND)parent->GetHWND(),
- (HMENU)m_windowId,
- wxGetInstance(),
- NULL
- );
-
- wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create tree ctrl") );
-
- if ( parent )
- parent->AddChild(this);
-
- SubclassWin(m_hWnd);
+ // clean up
+ ::DeleteDC(hdcMem);
+
+ // set the imagelist
+ SetStateImageList(&imagelistCheckboxes);
+ }
+#endif // 0
+
+ SetSize(pos.x, pos.y, size.x, size.y);
return TRUE;
}
SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
}
-size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
+size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
+ bool recursively) const
{
- long cookie;
+ class TraverseCounter : public wxTreeTraversal
+ {
+ public:
+ TraverseCounter(const wxTreeCtrl *tree,
+ const wxTreeItemId& root,
+ bool recursively)
+ : wxTreeTraversal(tree)
+ {
+ m_count = 0;
- size_t result = 0;
+ DoTraverse(root, recursively);
+ }
- wxArrayLong children;
- wxTreeItemId child = GetFirstChild(item, cookie);
- while ( child.IsOk() )
- {
- if ( recursively )
+ virtual bool OnVisit(const wxTreeItemId& item)
{
- // recursive call
- result += GetChildrenCount(child, TRUE);
+ m_count++;
+
+ return TRUE;
}
- // add the child to the result in any case
- result++;
+ size_t GetCount() const { return m_count; }
- child = GetNextChild(item, cookie);
- }
+ private:
+ size_t m_count;
+ } counter(this, item, recursively);
- return result;
+ return counter.GetCount();
}
// ----------------------------------------------------------------------------
DoSetItem(&tvItem);
}
+void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item,
+ int image,
+ int imageSel)
+{
+ wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE);
+ tvItem.iSelectedImage = imageSel;
+ tvItem.iImage = image;
+ DoSetItem(&tvItem);
+}
+
int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
{
wxTreeViewItem tvItem(item, TVIF_IMAGE);
void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
{
- wxTreeViewItem tvItem(item, TVIF_IMAGE);
- tvItem.iImage = image;
- DoSetItem(&tvItem);
+ // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
+ // change both normal and selected image - otherwise the change simply
+ // doesn't take place!
+ DoSetItemImages(item, image, GetItemSelectedImage(item));
}
int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
{
- wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE);
- tvItem.iSelectedImage = image;
- DoSetItem(&tvItem);
+ // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
+ // change both normal and selected image - otherwise the change simply
+ // doesn't take place!
+ DoSetItemImages(item, GetItemImage(item), image);
}
wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
wxTreeItemId wxTreeCtrl::GetSelection() const
{
+ wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (WXHTREEITEM)0,
+ _T("this only works with single selection controls") );
+
return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd()));
}
return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
}
+// ----------------------------------------------------------------------------
+// multiple selections emulation
+// ----------------------------------------------------------------------------
+
+bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
+{
+ // receive the desired information.
+ wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
+ DoGetItem(&tvItem);
+
+ // state image indices are 1 based
+ return ((tvItem.state >> 12) - 1) == 1;
+}
+
+void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
+{
+ // receive the desired information.
+ wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
+
+ // state images are one-based
+ tvItem.state = (check ? 2 : 1) << 12;
+
+ DoSetItem(&tvItem);
+}
+
+size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
+{
+ class TraverseSelections : public wxTreeTraversal
+ {
+ public:
+ TraverseSelections(const wxTreeCtrl *tree,
+ wxArrayTreeItemIds& selections)
+ : wxTreeTraversal(tree), m_selections(selections)
+ {
+ m_selections.Empty();
+
+ DoTraverse(tree->GetRootItem());
+ }
+
+ virtual bool OnVisit(const wxTreeItemId& item)
+ {
+ if ( GetTree()->IsItemChecked(item) )
+ {
+ m_selections.Add(item);
+ }
+
+ return TRUE;
+ }
+
+ private:
+ wxArrayTreeItemIds& m_selections;
+ } selector(this, selections);
+
+ return selections.GetCount();
+}
+
// ----------------------------------------------------------------------------
// Usual operations
// ----------------------------------------------------------------------------
void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
{
- DoExpand(item, action);
+ DoExpand(item, action);
}
void wxTreeCtrl::Unselect()
{
+ wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), _T("doesn't make sense") );
+
+ // just remove the selection
SelectItem(wxTreeItemId((WXHTREEITEM) 0));
}
-void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
+void wxTreeCtrl::UnselectAll()
{
- // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
- // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
- // send them ourselves
-
- wxTreeEvent event(wxEVT_NULL, m_windowId);
- event.m_item = item;
- event.SetEventObject(this);
-
- event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
- if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
+ if ( m_windowStyle & wxTR_MULTIPLE )
{
- if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
+ wxArrayTreeItemIds selections;
+ size_t count = GetSelections(selections);
+ for ( size_t n = 0; n < count; n++ )
{
- wxLogLastError("TreeView_SelectItem");
+ SetItemCheck(selections[n], FALSE);
}
- else
+ }
+ else
+ {
+ // just remove the selection
+ Unselect();
+ }
+}
+
+void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
+{
+ if ( m_windowStyle & wxTR_MULTIPLE )
+ {
+ // selecting the item means checking it
+ SetItemCheck(item);
+ }
+ else
+ {
+ // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
+ // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
+ // send them ourselves
+
+ wxTreeEvent event(wxEVT_NULL, m_windowId);
+ event.m_item = item;
+ event.SetEventObject(this);
+
+ event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
+ if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
{
- event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
- (void)GetEventHandler()->ProcessEvent(event);
+ if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
+ {
+ wxLogLastError("TreeView_SelectItem");
+ }
+ else
+ {
+ event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
+ (void)GetEventHandler()->ProcessEvent(event);
+ }
}
+ //else: program vetoed the change
}
- //else: program vetoed the change
}
void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item);
- wxCHECK_MSG( hWnd, NULL, _T("Can't edit tree ctrl label") );
+ // this is not an error - the TVN_BEGINLABELEDIT handler might have
+ // returned FALSE
+ if ( !hWnd )
+ {
+ return NULL;
+ }
DeleteTextCtrl();
TV_DISPINFO *info = (TV_DISPINFO *)lParam;
event.m_item = (WXHTREEITEM) info->item.hItem;
+ event.m_label = info->item.pszText;
break;
}
eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
TV_DISPINFO *info = (TV_DISPINFO *)lParam;
- event.m_item = (WXHTREEITEM) info->item.hItem;
+ event.m_item = (WXHTREEITEM)info->item.hItem;
+ event.m_label = info->item.pszText;
break;
}
bool processed = GetEventHandler()->ProcessEvent(event);
// post processing
- if ( hdr->code == TVN_DELETEITEM )
+ switch ( hdr->code )
{
- // NB: we might process this message using wxWindows event tables, but
- // due to overhead of wxWin event system we prefer to do it here
- // (otherwise deleting a tree with many items is just too slow)
- NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
- wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
- delete data; // may be NULL, ok
- processed = TRUE; // Make sure we don't get called twice
- }
+ case TVN_DELETEITEM:
+ {
+ // NB: we might process this message using wxWindows event
+ // tables, but due to overhead of wxWin event system we
+ // prefer to do it here ourself (otherwise deleting a tree
+ // with many items is just too slow)
+ NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
+ wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
+ delete data; // may be NULL, ok
+
+ processed = TRUE; // Make sure we don't get called twice
+ }
+ break;
- *result = !event.IsAllowed();
+ case TVN_BEGINLABELEDIT:
+ // return TRUE to cancel label editing
+ *result = !event.IsAllowed();
+ break;
+
+ case TVN_ENDLABELEDIT:
+ // return TRUE to set the label to the new string
+ *result = event.IsAllowed();
+
+ // ensure that we don't have the text ctrl which is going to be
+ // deleted any more
+ DeleteTextCtrl();
+ break;
+
+ case TVN_SELCHANGING:
+ case TVN_ITEMEXPANDING:
+ // return TRUE to prevent the action from happening
+ *result = !event.IsAllowed();
+ break;
+
+ //default:
+ // for the other messages the return value is ignored and there is
+ // nothing special to do
+ }
return processed;
}