+#ifdef __BORLANDC__
+ #pragma hdrstop
+#endif
+
+#if wxUSE_TREECTRL
+
+#include "wx/msw/private.h"
+
+// Set this to 1 to be _absolutely_ sure that repainting will work for all
+// comctl32.dll versions
+#define wxUSE_COMCTL32_SAFELY 0
+
+#include "wx/app.h"
+#include "wx/log.h"
+#include "wx/dynarray.h"
+#include "wx/imaglist.h"
+#include "wx/settings.h"
+#include "wx/msw/treectrl.h"
+#include "wx/msw/dragimag.h"
+
+// include <commctrl.h> "properly"
+#include "wx/msw/wrapcctl.h"
+
+// macros to hide the cast ugliness
+// --------------------------------
+
+// ptr is the real item id, i.e. wxTreeItemId::m_pItem
+#define HITEM_PTR(ptr) (HTREEITEM)(ptr)
+
+// item here is a wxTreeItemId
+#define HITEM(item) HITEM_PTR((item).m_pItem)
+
+// the native control doesn't support multiple selections under MSW and we
+// have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
+// checkboxes be the selection status (checked == selected) or by really
+// emulating everything, i.e. intercepting mouse and key events &c. The first
+// approach is much easier but doesn't work with comctl32.dll < 4.71 and also
+// looks quite ugly.
+#define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
+
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------
+
+// wrapper for TreeView_HitTest
+static HTREEITEM GetItemFromPoint(HWND hwndTV, int x, int y)
+{
+ TV_HITTESTINFO tvht;
+ tvht.pt.x = x;
+ tvht.pt.y = y;
+
+ return (HTREEITEM)TreeView_HitTest(hwndTV, &tvht);
+}
+
+#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
+
+// wrappers for TreeView_GetItem/TreeView_SetItem
+static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem)
+{
+
+ TV_ITEM tvi;
+ tvi.mask = TVIF_STATE | TVIF_HANDLE;
+ tvi.stateMask = TVIS_SELECTED;
+ tvi.hItem = hItem;
+
+ if ( !TreeView_GetItem(hwndTV, &tvi) )
+ {
+ wxLogLastError(wxT("TreeView_GetItem"));
+ }
+
+ return (tvi.state & TVIS_SELECTED) != 0;
+}
+
+static void SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = true)
+{
+ TV_ITEM tvi;
+ tvi.mask = TVIF_STATE | TVIF_HANDLE;
+ tvi.stateMask = TVIS_SELECTED;
+ tvi.state = select ? TVIS_SELECTED : 0;
+ tvi.hItem = hItem;
+
+ if ( TreeView_SetItem(hwndTV, &tvi) == -1 )
+ {
+ wxLogLastError(wxT("TreeView_SetItem"));
+ }
+}
+
+static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem)
+{
+ SelectItem(hwndTV, htItem, false);
+}
+
+static inline void ToggleItemSelection(HWND hwndTV, HTREEITEM htItem)
+{
+ SelectItem(hwndTV, htItem, !IsItemSelected(hwndTV, htItem));
+}
+
+// helper function which selects all items in a range and, optionally,
+// unselects all others
+static void SelectRange(HWND hwndTV,
+ HTREEITEM htFirst,
+ HTREEITEM htLast,
+ bool unselectOthers = true)
+{
+ // find the first (or last) item and select it
+ bool cont = true;
+ HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV);
+ while ( htItem && cont )
+ {
+ if ( (htItem == htFirst) || (htItem == htLast) )
+ {
+ if ( !IsItemSelected(hwndTV, htItem) )
+ {
+ SelectItem(hwndTV, htItem);
+ }
+
+ cont = false;
+ }
+ else
+ {
+ if ( unselectOthers && IsItemSelected(hwndTV, htItem) )
+ {
+ UnselectItem(hwndTV, htItem);
+ }
+ }
+
+ htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
+ }
+
+ // select the items in range
+ cont = htFirst != htLast;
+ while ( htItem && cont )
+ {
+ if ( !IsItemSelected(hwndTV, htItem) )
+ {
+ SelectItem(hwndTV, htItem);
+ }
+
+ cont = (htItem != htFirst) && (htItem != htLast);
+
+ htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
+ }
+
+ // unselect the rest
+ if ( unselectOthers )
+ {
+ while ( htItem )
+ {
+ if ( IsItemSelected(hwndTV, htItem) )
+ {
+ UnselectItem(hwndTV, htItem);
+ }
+
+ htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
+ }
+ }
+
+ // seems to be necessary - otherwise the just selected items don't always
+ // appear as selected
+ UpdateWindow(hwndTV);
+}
+
+// helper function which tricks the standard control into changing the focused
+// item without changing anything else (if someone knows why Microsoft doesn't
+// allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
+static void SetFocus(HWND hwndTV, HTREEITEM htItem)
+{
+ // the current focus
+ HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV);
+
+ if ( htItem )
+ {
+ // set the focus
+ if ( htItem != htFocus )
+ {
+ // remember the selection state of the item
+ bool wasSelected = IsItemSelected(hwndTV, htItem);
+
+ if ( htFocus && IsItemSelected(hwndTV, htFocus) )
+ {
+ // prevent the tree from unselecting the old focus which it
+ // would do by default (TreeView_SelectItem unselects the
+ // focused item)
+ TreeView_SelectItem(hwndTV, 0);
+ SelectItem(hwndTV, htFocus);
+ }
+
+ TreeView_SelectItem(hwndTV, htItem);
+
+ if ( !wasSelected )
+ {
+ // need to clear the selection which TreeView_SelectItem() gave
+ // us
+ UnselectItem(hwndTV, htItem);
+ }
+ //else: was selected, still selected - ok
+ }
+ //else: nothing to do, focus already there
+ }
+ else
+ {
+ if ( htFocus )
+ {
+ bool wasFocusSelected = IsItemSelected(hwndTV, htFocus);
+
+ // just clear the focus
+ TreeView_SelectItem(hwndTV, 0);
+
+ if ( wasFocusSelected )
+ {
+ // restore the selection state
+ SelectItem(hwndTV, htFocus);
+ }
+ }
+ //else: nothing to do, no focus already
+ }
+}
+
+#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
+
+// ----------------------------------------------------------------------------
+// private classes
+// ----------------------------------------------------------------------------
+
+// a convenient wrapper around TV_ITEM struct which adds a ctor
+#ifdef __VISUALC__
+#pragma warning( disable : 4097 ) // inheriting from typedef
+#endif
+
+struct wxTreeViewItem : public TV_ITEM
+{
+ wxTreeViewItem(const wxTreeItemId& item, // the item handle
+ UINT mask_, // fields which are valid
+ UINT stateMask_ = 0) // for TVIF_STATE only
+ {
+ wxZeroMemory(*this);
+
+ // hItem member is always valid
+ mask = mask_ | TVIF_HANDLE;
+ stateMask = stateMask_;
+ hItem = HITEM(item);
+ }
+};
+
+// wxVirutalNode is used in place of a single root when 'hidden' root is
+// specified.
+class wxVirtualNode : public wxTreeViewItem
+{
+public:
+ wxVirtualNode(wxTreeItemData *data)
+ : wxTreeViewItem(TVI_ROOT, 0)
+ {
+ m_data = data;
+ }
+
+ ~wxVirtualNode()
+ {
+ delete m_data;
+ }
+
+ wxTreeItemData *GetData() const { return m_data; }
+ void SetData(wxTreeItemData *data) { delete m_data; m_data = data; }
+
+private:
+ wxTreeItemData *m_data;
+
+ DECLARE_NO_COPY_CLASS(wxVirtualNode)
+};
+
+#ifdef __VISUALC__
+#pragma warning( default : 4097 )
+#endif
+
+// a macro to get the virtual root, returns NULL if none
+#define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
+
+// returns true if the item is the virtual root
+#define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
+
+// 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;
+
+ DECLARE_NO_COPY_CLASS(wxTreeTraversal)
+};
+
+// internal class for getting the selected items
+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)
+ {
+ // can't visit a virtual node.
+ if ( (GetTree()->GetRootItem() == item) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT))
+ {
+ return true;
+ }
+
+#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
+ if ( GetTree()->IsItemChecked(item) )
+#else
+ if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item)) )
+#endif
+ {
+ m_selections.Add(item);
+ }
+
+ return true;
+ }
+
+ size_t GetCount() const { return m_selections.GetCount(); }
+
+private:
+ wxArrayTreeItemIds& m_selections;
+
+ DECLARE_NO_COPY_CLASS(TraverseSelections)
+};
+
+// internal class for counting tree items
+class TraverseCounter : public wxTreeTraversal
+{
+public:
+ TraverseCounter(const wxTreeCtrl *tree,
+ const wxTreeItemId& root,
+ bool recursively)
+ : wxTreeTraversal(tree)
+ {
+ m_count = 0;
+
+ DoTraverse(root, recursively);
+ }
+
+ virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item))
+ {
+ m_count++;
+
+ return true;
+ }
+
+ size_t GetCount() const { return m_count; }
+
+private:
+ size_t m_count;
+
+ DECLARE_NO_COPY_CLASS(TraverseCounter)
+};
+
+// ----------------------------------------------------------------------------
+// This class is needed for support of different images: the Win32 common
+// control natively supports only 2 images (the normal one and another for the
+// selected state). We wish to provide support for 2 more of them for folder
+// items (i.e. those which have children): for expanded state and for expanded
+// selected state. For this we use this structure to store the additional items
+// images.
+//
+// There is only one problem with this: when we retrieve the item's data, we
+// don't know whether we get a pointer to wxTreeItemData or
+// wxTreeItemIndirectData. So we always set the item id to an invalid value
+// in this class and the code using the client data checks for it and retrieves
+// the real client data in this case.
+// ----------------------------------------------------------------------------
+
+class wxTreeItemIndirectData : public wxTreeItemData
+{
+public:
+ // ctor associates this data with the item and the real item data becomes
+ // available through our GetData() method
+ wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item)
+ {
+ for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
+ {
+ m_images[n] = -1;
+ }
+
+ // save the old data
+ m_data = tree->GetItemData(item);
+
+ // and set ourselves as the new one
+ tree->SetIndirectItemData(item, this);
+
+ // we must have the invalid value for the item
+ m_pItem = 0l;
+ }
+
+ // dtor deletes the associated data as well
+ virtual ~wxTreeItemIndirectData() { delete m_data; }
+
+ // accessors
+ // get the real data associated with the item
+ wxTreeItemData *GetData() const { return m_data; }
+ // change it
+ void SetData(wxTreeItemData *data) { m_data = data; }
+
+ // do we have such image?
+ bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
+ // get image
+ int GetImage(wxTreeItemIcon which) const { return m_images[which]; }
+ // change it
+ void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
+
+private:
+ // all the images associated with the item
+ int m_images[wxTreeItemIcon_Max];
+
+ // the real client data
+ wxTreeItemData *m_data;
+
+ DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData)
+};
+
+// ----------------------------------------------------------------------------
+// wxWin macros
+// ----------------------------------------------------------------------------
+
+#if wxUSE_EXTENDED_RTTI
+IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl, wxControl,"wx/treectrl.h")
+
+WX_BEGIN_PROPERTIES_TABLE(wxTreeCtrl)
+WX_END_PROPERTIES_TABLE()
+
+WX_BEGIN_HANDLERS_TABLE(wxTreeCtrl)
+WX_END_HANDLERS_TABLE()
+
+WX_CONSTRUCTOR_5( wxTreeCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
+#else
+IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
+#endif
+
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
+
+// indices in gs_expandEvents table below
+enum
+{
+ IDX_COLLAPSE,
+ IDX_EXPAND,
+ IDX_WHAT_MAX
+};
+
+enum
+{
+ IDX_DONE,
+ IDX_DOING,
+ IDX_HOW_MAX
+};
+
+// handy table for sending events - it has to be initialized during run-time
+// now so can't be const any more
+static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX];
+
+/*
+ but logically it's a const table with the following entries:
+=
+{
+ { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
+ { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
+};
+*/
+
+// ============================================================================
+// 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)
+{
+ wxTreeItemIdValue 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
+// ----------------------------------------------------------------------------
+
+void wxTreeCtrl::Init()
+{
+ m_imageListNormal = NULL;
+ m_imageListState = NULL;
+ m_ownsImageListNormal = m_ownsImageListState = false;
+ m_textCtrl = NULL;
+ m_hasAnyAttr = false;
+ m_dragImage = NULL;
+ m_pVirtualRoot = NULL;
+
+ // initialize the global array of events now as it can't be done statically
+ // with the wxEVT_XXX values being allocated during run-time only
+ gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED;
+ gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING;
+ gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED;
+ gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING;
+}
+
+bool wxTreeCtrl::Create(wxWindow *parent,
+ wxWindowID id,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxValidator& validator,
+ const wxString& name)
+{
+ Init();
+
+ if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
+ style |= wxBORDER_SUNKEN;
+
+ if ( !CreateControl(parent, id, pos, size, style, validator, name) )
+ return false;
+
+ DWORD exStyle = 0;
+ DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle);
+ wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS;
+
+ if ((m_windowStyle & wxTR_NO_LINES) == 0)
+ wstyle |= TVS_HASLINES;
+ if ( m_windowStyle & wxTR_HAS_BUTTONS )
+ wstyle |= TVS_HASBUTTONS;
+
+ if ( m_windowStyle & wxTR_EDIT_LABELS )
+ wstyle |= TVS_EDITLABELS;
+
+ if ( m_windowStyle & wxTR_LINES_AT_ROOT )
+ wstyle |= TVS_LINESATROOT;
+
+ if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT )
+ {
+ if ( wxTheApp->GetComCtl32Version() >= 471 )
+ wstyle |= TVS_FULLROWSELECT;
+ }
+
+ // using TVS_CHECKBOXES for emulation of a multiselection tree control
+ // doesn't work without the new enough headers
+#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
+ !defined( __GNUWIN32_OLD__ ) && \
+ !defined( __BORLANDC__ ) && \
+ !defined( __WATCOMC__ ) && \
+ (!defined(__VISUALC__) || (__VISUALC__ > 1010))
+
+ // 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 // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
+
+ // Create the tree control.
+ if ( !MSWCreateControl(WC_TREEVIEW, wstyle) )
+ return false;
+
+#if wxUSE_COMCTL32_SAFELY
+ wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
+ wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
+#elif 1
+ SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
+ SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
+#else
+ // This works around a bug in the Windows tree control whereby for some versions
+ // of comctrl32, setting any colour actually draws the background in black.
+ // This will initialise the background to the system colour.
+ // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
+ // Assume the user has an updated comctl32.dll.
+ ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1);
+ wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
+ SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
+#endif
+
+
+ // 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 )
+ {
+ 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(wxT("DrawFrameControl(check)"));
+ }
+
+ bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
+ imagelistCheckboxes.Add(bmp);
+
+ if ( !::DrawFrameControl(hdcMem, &rect,
+ DFC_BUTTON,
+ DFCS_BUTTONCHECK) )
+ {
+ wxLogLastError(wxT("DrawFrameControl(uncheck)"));
+ }
+
+ bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
+ imagelistCheckboxes.Add(bmp);
+
+ // clean up
+ ::DeleteDC(hdcMem);
+
+ // set the imagelist
+ SetStateImageList(&imagelistCheckboxes);
+ }
+#endif // 0
+
+ SetSize(pos.x, pos.y, size.x, size.y);
+
+ return true;
+}
+
+wxTreeCtrl::~wxTreeCtrl()
+{
+ // delete any attributes
+ if ( m_hasAnyAttr )
+ {
+ WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs);
+
+ // prevent TVN_DELETEITEM handler from deleting the attributes again!
+ m_hasAnyAttr = false;
+ }
+
+ DeleteTextCtrl();
+
+ // delete user data to prevent memory leaks
+ // also deletes hidden root node storage.
+ DeleteAllItems();
+
+ if (m_ownsImageListNormal) delete m_imageListNormal;
+ if (m_ownsImageListState) delete m_imageListState;
+}
+
+// ----------------------------------------------------------------------------
+// accessors
+// ----------------------------------------------------------------------------
+
+// simple wrappers which add error checking in debug mode
+
+bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
+{
+ wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false,
+ _T("can't retrieve virtual root item") );
+
+ if ( !TreeView_GetItem(GetHwnd(), tvItem) )
+ {
+ wxLogLastError(wxT("TreeView_GetItem"));
+
+ return false;
+ }
+
+ return true;
+}
+
+void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
+{
+ if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
+ {
+ wxLogLastError(wxT("TreeView_SetItem"));
+ }
+}
+
+size_t wxTreeCtrl::GetCount() const
+{
+ return (size_t)TreeView_GetCount(GetHwnd());
+}
+
+unsigned int wxTreeCtrl::GetIndent() const
+{
+ return TreeView_GetIndent(GetHwnd());
+}
+
+void wxTreeCtrl::SetIndent(unsigned int indent)
+{
+ TreeView_SetIndent(GetHwnd(), indent);
+}
+
+wxImageList *wxTreeCtrl::GetImageList() const
+{
+ return m_imageListNormal;
+}
+
+wxImageList *wxTreeCtrl::GetStateImageList() const
+{
+ return m_imageListState;
+}
+
+void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
+{
+ // no error return
+ TreeView_SetImageList(GetHwnd(),
+ imageList ? imageList->GetHIMAGELIST() : 0,
+ which);
+}
+
+void wxTreeCtrl::SetImageList(wxImageList *imageList)
+{
+ if (m_ownsImageListNormal)
+ delete m_imageListNormal;
+
+ SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
+ m_ownsImageListNormal = false;
+}
+
+void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
+{
+ if (m_ownsImageListState) delete m_imageListState;
+ SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
+ m_ownsImageListState = false;
+}
+
+void wxTreeCtrl::AssignImageList(wxImageList *imageList)
+{
+ SetImageList(imageList);
+ m_ownsImageListNormal = true;
+}
+
+void wxTreeCtrl::AssignStateImageList(wxImageList *imageList)
+{
+ SetStateImageList(imageList);
+ m_ownsImageListState = true;
+}
+
+size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
+ bool recursively) const
+{
+ TraverseCounter counter(this, item, recursively);
+
+ return counter.GetCount() - 1;
+}
+
+// ----------------------------------------------------------------------------
+// control colours
+// ----------------------------------------------------------------------------
+
+bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
+{
+#if !wxUSE_COMCTL32_SAFELY
+ if ( !wxWindowBase::SetBackgroundColour(colour) )
+ return false;
+
+ SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
+#endif
+
+ return true;
+}
+
+bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
+{
+#if !wxUSE_COMCTL32_SAFELY
+ if ( !wxWindowBase::SetForegroundColour(colour) )
+ return false;
+
+ SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
+#endif
+
+ return true;
+}
+
+// ----------------------------------------------------------------------------
+// Item access
+// ----------------------------------------------------------------------------
+
+wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
+{
+ wxChar buf[512]; // the size is arbitrary...
+
+ wxTreeViewItem tvItem(item, TVIF_TEXT);
+ tvItem.pszText = buf;
+ tvItem.cchTextMax = WXSIZEOF(buf);
+ if ( !DoGetItem(&tvItem) )
+ {
+ // don't return some garbage which was on stack, but an empty string
+ buf[0] = wxT('\0');
+ }
+
+ return wxString(buf);
+}
+
+void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
+{
+ if ( IS_VIRTUAL_ROOT(item) )
+ return;
+
+ wxTreeViewItem tvItem(item, TVIF_TEXT);
+ tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok
+ DoSetItem(&tvItem);
+
+ // when setting the text of the item being edited, the text control should
+ // be updated to reflect the new text as well, otherwise calling
+ // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
+ //
+ // don't use GetEditControl() here because m_textCtrl is not set yet
+ HWND hwndEdit = TreeView_GetEditControl(GetHwnd());
+ if ( hwndEdit )
+ {
+ if ( item == GetSelection() )
+ {
+ ::SetWindowText(hwndEdit, text);
+ }
+ }
+}
+
+int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item,
+ wxTreeItemIcon which) const
+{
+ wxTreeViewItem tvItem(item, TVIF_PARAM);
+ if ( !DoGetItem(&tvItem) )
+ {
+ return -1;
+ }
+
+ return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which);
+}
+
+void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item,
+ int image,
+ wxTreeItemIcon which) const
+{
+ wxTreeViewItem tvItem(item, TVIF_PARAM);
+ if ( !DoGetItem(&tvItem) )
+ {
+ return;
+ }
+
+ wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam);
+
+ data->SetImage(image, which);
+
+ // make sure that we have selected images as well
+ if ( which == wxTreeItemIcon_Normal &&
+ !data->HasImage(wxTreeItemIcon_Selected) )
+ {
+ data->SetImage(image, wxTreeItemIcon_Selected);
+ }
+
+ if ( which == wxTreeItemIcon_Expanded &&
+ !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
+ {
+ data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
+ }
+}
+
+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,
+ wxTreeItemIcon which) const
+{
+ if ( (HITEM(item) == TVI_ROOT) && (m_windowStyle & wxTR_HIDE_ROOT) )
+ {
+ // TODO: Maybe a hidden root can still provide images?
+ return -1;
+ }
+
+ if ( HasIndirectData(item) )
+ {
+ return DoGetItemImageFromData(item, which);
+ }
+
+ UINT mask;
+ switch ( which )
+ {
+ default:
+ wxFAIL_MSG( wxT("unknown tree item image type") );
+
+ case wxTreeItemIcon_Normal:
+ mask = TVIF_IMAGE;
+ break;
+
+ case wxTreeItemIcon_Selected:
+ mask = TVIF_SELECTEDIMAGE;
+ break;
+
+ case wxTreeItemIcon_Expanded:
+ case wxTreeItemIcon_SelectedExpanded:
+ return -1;
+ }
+
+ wxTreeViewItem tvItem(item, mask);
+ DoGetItem(&tvItem);
+
+ return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage;
+}
+
+void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
+ wxTreeItemIcon which)
+{
+ if ( IS_VIRTUAL_ROOT(item) )
+ {
+ // TODO: Maybe a hidden root can still store images?
+ return;
+ }
+
+ int imageNormal,
+ imageSel;
+
+ switch ( which )
+ {
+ default:
+ wxFAIL_MSG( wxT("unknown tree item image type") );
+ // fall through
+
+ case wxTreeItemIcon_Normal:
+ {
+ const int imageNormalOld = GetItemImage(item);
+ const int imageSelOld =
+ GetItemImage(item, wxTreeItemIcon_Selected);
+
+ // always set the normal image
+ imageNormal = image;
+
+ // if the selected and normal images were the same, they should
+ // be the same after the update, otherwise leave the selected
+ // image as it was
+ imageSel = imageNormalOld == imageSelOld ? image : imageSelOld;
+ }
+ break;
+
+ case wxTreeItemIcon_Selected:
+ imageNormal = GetItemImage(item);
+ imageSel = image;
+ break;
+
+ case wxTreeItemIcon_Expanded:
+ case wxTreeItemIcon_SelectedExpanded:
+ if ( !HasIndirectData(item) )
+ {
+ // we need to get the old images first, because after we create
+ // the wxTreeItemIndirectData GetItemXXXImage() will use it to
+ // get the images
+ imageNormal = GetItemImage(item);
+ imageSel = GetItemImage(item, wxTreeItemIcon_Selected);
+
+ // if it doesn't have it yet, add it
+ wxTreeItemIndirectData *data = new
+ wxTreeItemIndirectData(this, item);
+
+ // copy the data to the new location
+ data->SetImage(imageNormal, wxTreeItemIcon_Normal);
+ data->SetImage(imageSel, wxTreeItemIcon_Selected);
+ }
+
+ DoSetItemImageFromData(item, image, which);
+
+ // reset the normal/selected images because we won't use them any
+ // more - now they're stored inside the indirect data
+ imageNormal =
+ imageSel = I_IMAGECALLBACK;
+ break;
+ }
+
+ // 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, imageNormal, imageSel);
+}
+
+wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
+{
+ wxTreeViewItem tvItem(item, TVIF_PARAM);
+
+ // Hidden root may have data.
+ if ( IS_VIRTUAL_ROOT(item) )
+ {
+ return GET_VIRTUAL_ROOT()->GetData();
+ }
+
+ // Visible node.
+ if ( !DoGetItem(&tvItem) )
+ {
+ return NULL;
+ }
+
+ wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
+ if ( IsDataIndirect(data) )
+ {
+ data = ((wxTreeItemIndirectData *)data)->GetData();
+ }
+
+ return data;
+}
+
+void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
+{
+ if ( IS_VIRTUAL_ROOT(item) )
+ {
+ GET_VIRTUAL_ROOT()->SetData(data);
+ }
+
+ // first, associate this piece of data with this item
+ if ( data )
+ {
+ data->SetId(item);
+ }
+
+ wxTreeViewItem tvItem(item, TVIF_PARAM);
+
+ if ( HasIndirectData(item) )
+ {
+ if ( DoGetItem(&tvItem) )
+ {
+ ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
+ }
+ else
+ {
+ wxFAIL_MSG( wxT("failed to change tree items data") );
+ }
+ }
+ else
+ {
+ tvItem.lParam = (LPARAM)data;
+ DoSetItem(&tvItem);
+ }
+}
+
+void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
+ wxTreeItemIndirectData *data)
+{
+ // this should never happen because it's unnecessary and will probably lead
+ // to crash too because the code elsewhere supposes that the pointer the
+ // wxTreeItemIndirectData has is a real wxItemData and not
+ // wxTreeItemIndirectData as well
+ wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
+
+ SetItemData(item, data);
+}