+// this helper class is used on vista systems for preventing unwanted
+// item state changes in the vista tree control. It is only effective in
+// multi-select mode on vista systems.
+
+// The vista tree control includes some new code that originally broke the
+// multi-selection tree, causing seemingly spurious item selection state changes
+// during Shift or Ctrl-click item selection. (To witness the original broken
+// behaviour, simply make IsLocked() below always return false). This problem was
+// solved by using the following class to 'unlock' an item's selection state.
+
+class TreeItemUnlocker
+{
+public:
+ // unlock a single item
+ TreeItemUnlocker(HTREEITEM item)
+ {
+ m_oldUnlockedItem = ms_unlockedItem;
+ ms_unlockedItem = item;
+ }
+
+ // unlock all items, don't use unless absolutely necessary
+ TreeItemUnlocker()
+ {
+ m_oldUnlockedItem = ms_unlockedItem;
+ ms_unlockedItem = (HTREEITEM)-1;
+ }
+
+ // lock everything back
+ ~TreeItemUnlocker() { ms_unlockedItem = m_oldUnlockedItem; }
+
+
+ // check if the item state is currently locked
+ static bool IsLocked(HTREEITEM item)
+ { return ms_unlockedItem != (HTREEITEM)-1 && item != ms_unlockedItem; }
+
+private:
+ static HTREEITEM ms_unlockedItem;
+ HTREEITEM m_oldUnlockedItem;
+
+ wxDECLARE_NO_COPY_CLASS(TreeItemUnlocker);
+};
+
+HTREEITEM TreeItemUnlocker::ms_unlockedItem = NULL;
+
+// another helper class: set the variable to true during its lifetime and reset
+// it to false when it is destroyed
+//
+// it is currently always used with wxTreeCtrl::m_changingSelection
+class TempSetter
+{
+public:
+ TempSetter(bool& var) : m_var(var)
+ {
+ wxASSERT_MSG( !m_var, "variable shouldn't be already set" );
+ m_var = true;
+ }
+
+ ~TempSetter()
+ {
+ m_var = false;
+ }
+
+private:
+ bool& m_var;
+
+ wxDECLARE_NO_COPY_CLASS(TempSetter);
+};
+
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------
+
+namespace
+{
+
+// Work around a problem with TreeView_GetItemRect() when using MinGW/Cygwin:
+// it results in warnings about breaking strict aliasing rules because HITEM is
+// passed via a RECT pointer, so use a union to avoid them and define our own
+// version of the standard macro using it.
+union TVGetItemRectParam
+{
+ RECT rect;
+ HTREEITEM hItem;
+};
+
+inline bool
+wxTreeView_GetItemRect(HWND hwnd,
+ HTREEITEM hItem,
+ TVGetItemRectParam& param,
+ BOOL fItemRect)
+{
+ param.hItem = hItem;
+ return ::SendMessage(hwnd, TVM_GETITEMRECT, fItemRect,
+ (LPARAM)¶m) == TRUE;
+}
+
+} // anonymous namespace
+
+// 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;
+
+ TreeItemUnlocker unlocker(hItem);
+
+ if ( !TreeView_GetItem(hwndTV, &tvi) )
+ {
+ wxLogLastError(wxT("TreeView_GetItem"));
+ }
+
+ return (tvi.state & TVIS_SELECTED) != 0;
+}
+
+static bool 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;
+
+ TreeItemUnlocker unlocker(hItem);
+
+ if ( TreeView_SetItem(hwndTV, &tvi) == -1 )
+ {
+ wxLogLastError(wxT("TreeView_SetItem"));
+ return false;
+ }
+
+ return true;
+}
+
+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,
+// deselects all the other ones
+//
+// returns true if the selection changed at all or false if nothing changed
+
+// flags for SelectRange()
+enum
+{
+ SR_SIMULATE = 1, // don't do anything, just return true or false
+ SR_UNSELECT_OTHERS = 2 // deselect the items not in range
+};
+
+static bool SelectRange(HWND hwndTV,
+ HTREEITEM htFirst,
+ HTREEITEM htLast,
+ int flags)
+{
+ // find the first (or last) item and select it
+ bool changed = false;
+ bool cont = true;
+ HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV);
+
+ while ( htItem && cont )
+ {
+ if ( (htItem == htFirst) || (htItem == htLast) )
+ {
+ if ( !IsItemSelected(hwndTV, htItem) )
+ {
+ if ( !(flags & SR_SIMULATE) )
+ {
+ SelectItem(hwndTV, htItem);
+ }
+
+ changed = true;
+ }
+
+ cont = false;
+ }
+ else // not first or last
+ {
+ if ( flags & SR_UNSELECT_OTHERS )
+ {
+ if ( IsItemSelected(hwndTV, htItem) )
+ {
+ if ( !(flags & SR_SIMULATE) )
+ UnselectItem(hwndTV, htItem);
+
+ changed = true;
+ }
+ }
+ }
+
+ htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
+ }
+
+ // select the items in range
+ cont = htFirst != htLast;
+ while ( htItem && cont )
+ {
+ if ( !IsItemSelected(hwndTV, htItem) )
+ {
+ if ( !(flags & SR_SIMULATE) )
+ {
+ SelectItem(hwndTV, htItem);
+ }
+
+ changed = true;
+ }
+
+ cont = (htItem != htFirst) && (htItem != htLast);
+
+ htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
+ }
+
+ // optionally deselect the rest
+ if ( flags & SR_UNSELECT_OTHERS )
+ {
+ while ( htItem )
+ {
+ if ( IsItemSelected(hwndTV, htItem) )
+ {
+ if ( !(flags & SR_SIMULATE) )
+ {
+ UnselectItem(hwndTV, htItem);
+ }
+
+ changed = true;
+ }
+
+ htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
+ }
+ }
+
+ // seems to be necessary - otherwise the just selected items don't always
+ // appear as selected
+ if ( !(flags & SR_SIMULATE) )
+ {
+ UpdateWindow(hwndTV);
+ }
+
+ return changed;
+}
+
+// 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!)
+//
+// returns true if the focus was changed, false if the given item was already
+// the focused one
+static bool SetFocus(HWND hwndTV, HTREEITEM htItem)
+{
+ // the current focus
+ HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV);
+
+ if ( htItem == htFocus )
+ return false;
+
+ if ( htItem )
+ {
+ // 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 // reset focus
+ {
+ bool wasFocusSelected = IsItemSelected(hwndTV, htFocus);
+
+ // just clear the focus
+ TreeView_SelectItem(hwndTV, 0);
+
+ if ( wasFocusSelected )
+ {
+ // restore the selection state
+ SelectItem(hwndTV, htFocus);
+ }
+ }
+
+ return true;
+}
+
+// ----------------------------------------------------------------------------
+// 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);
+ }
+};
+
+// ----------------------------------------------------------------------------
+// This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
+//
+// We need this for a couple of reasons:
+//
+// 1) 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.
+//
+// 2) This class is also needed to hold the HITEM so that we can sort
+// it correctly in the MSW sort callback.
+//
+// In addition it makes other workarounds such as this easier and helps
+// simplify the code.
+// ----------------------------------------------------------------------------
+
+class wxTreeItemParam
+{
+public:
+ wxTreeItemParam()
+ {
+ m_data = NULL;
+
+ for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
+ {
+ m_images[n] = -1;
+ }
+ }
+
+ // dtor deletes the associated data as well
+ virtual ~wxTreeItemParam() { 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, falling back to the other images if this one is not
+ // specified
+ int GetImage(wxTreeItemIcon which) const
+ {
+ int image = m_images[which];
+ if ( image == -1 )
+ {
+ switch ( which )
+ {
+ case wxTreeItemIcon_SelectedExpanded:
+ // We consider that expanded icon is more important than
+ // selected so test for it first.
+ image = m_images[wxTreeItemIcon_Expanded];
+ if ( image == -1 )
+ image = m_images[wxTreeItemIcon_Selected];
+ if ( image != -1 )
+ break;
+ //else: fall through
+
+ case wxTreeItemIcon_Selected:
+ case wxTreeItemIcon_Expanded:
+ image = m_images[wxTreeItemIcon_Normal];
+ break;
+
+ case wxTreeItemIcon_Normal:
+ // no fallback
+ break;
+
+ default:
+ wxFAIL_MSG( wxT("unsupported wxTreeItemIcon value") );
+ }
+ }
+
+ return image;
+ }
+ // change the given image
+ void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
+
+ // get item
+ const wxTreeItemId& GetItem() const { return m_item; }
+ // set item
+ void SetItem(const wxTreeItemId& item) { m_item = item; }
+
+protected:
+ // all the images associated with the item
+ int m_images[wxTreeItemIcon_Max];
+
+ // item for sort callbacks
+ wxTreeItemId m_item;
+
+ // the real client data
+ wxTreeItemData *m_data;
+
+ wxDECLARE_NO_COPY_CLASS(wxTreeItemParam);
+};
+
+// wxVirutalNode is used in place of a single root when 'hidden' root is
+// specified.
+class wxVirtualNode : public wxTreeViewItem
+{
+public:
+ wxVirtualNode(wxTreeItemParam *param)
+ : wxTreeViewItem(TVI_ROOT, 0)
+ {
+ m_param = param;
+ }
+
+ ~wxVirtualNode()
+ {
+ delete m_param;
+ }
+
+ wxTreeItemParam *GetParam() const { return m_param; }
+ void SetParam(wxTreeItemParam *param) { delete m_param; m_param = param; }
+
+private:
+ wxTreeItemParam *m_param;
+
+ wxDECLARE_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;
+ }
+
+ // give it a virtual dtor: not really needed as the class is never used
+ // polymorphically and not even allocated on heap at all, but this is safer
+ // (in case it ever is) and silences the compiler warnings for now
+ virtual ~wxTreeTraversal() { }
+
+ // 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;
+
+ wxDECLARE_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();
+
+ if (tree->GetCount() > 0)
+ DoTraverse(tree->GetRootItem());
+ }
+
+ virtual bool OnVisit(const wxTreeItemId& item)
+ {
+ const wxTreeCtrl * const tree = GetTree();
+
+ // can't visit a virtual node.
+ if ( (tree->GetRootItem() == item) && tree->HasFlag(wxTR_HIDE_ROOT) )
+ {
+ return true;
+ }
+
+ if ( ::IsItemSelected(GetHwndOf(tree), HITEM(item)) )
+ {
+ m_selections.Add(item);
+ }
+
+ return true;
+ }
+
+ size_t GetCount() const { return m_selections.GetCount(); }
+
+private:
+ wxArrayTreeItemIds& m_selections;
+
+ wxDECLARE_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;
+
+ wxDECLARE_NO_COPY_CLASS(TraverseCounter);
+};
+
+// ----------------------------------------------------------------------------
+// wxWin macros
+// ----------------------------------------------------------------------------
+
+// ----------------------------------------------------------------------------
+// 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_textCtrl = NULL;
+ m_hasAnyAttr = false;
+#if wxUSE_DRAGIMAGE
+ m_dragImage = NULL;
+#endif
+ m_pVirtualRoot = NULL;
+ m_dragStarted = false;
+ m_focusLost = true;
+ m_changingSelection = false;
+ m_triggerStateImageClick = false;
+ m_mouseUpDeselect = false;
+
+ // 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;
+
+ WXDWORD exStyle = 0;
+ DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle);
+ wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS;
+
+ if ( !(m_windowStyle & wxTR_NO_LINES) )
+ 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 ( wxApp::GetComCtl32Version() >= 471 )
+ wstyle |= TVS_FULLROWSELECT;
+ }
+
+#if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
+ // Need so that TVN_GETINFOTIP messages will be sent
+ wstyle |= TVS_INFOTIP;
+#endif
+
+ // Create the tree control.
+ if ( !MSWCreateControl(WC_TREEVIEW, wstyle, pos, size) )
+ return false;
+
+ SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
+ SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
+
+ wxSetCCUnicodeFormat(GetHwnd());
+
+ if ( m_windowStyle & wxTR_TWIST_BUTTONS )
+ {
+ // Under Vista and later Explorer uses rotating ("twist") buttons
+ // instead of the default "+/-" ones so apply its theme to the tree
+ // control to implement this style.
+ if ( wxGetWinVersion() >= wxWinVersion_Vista )
+ {
+ if ( wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive() )
+ {
+ theme->SetWindowTheme(GetHwnd(), L"EXPLORER", NULL);
+ }
+ }
+ }
+
+ return true;
+}
+
+wxTreeCtrl::~wxTreeCtrl()
+{
+ m_isBeingDeleted = true;
+
+ // 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();
+}
+
+// ----------------------------------------------------------------------------
+// accessors
+// ----------------------------------------------------------------------------
+
+/* static */ wxVisualAttributes
+wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
+{
+ wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant);
+
+ // common controls have their own default font
+ attrs.font = wxGetCCDefaultFont();
+
+ return attrs;
+}
+
+
+// simple wrappers which add error checking in debug mode
+
+bool wxTreeCtrl::DoGetItem(wxTreeViewItem *tvItem) const
+{
+ wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false,
+ wxT("can't retrieve virtual root item") );
+
+ if ( !TreeView_GetItem(GetHwnd(), tvItem) )
+ {
+ wxLogLastError(wxT("TreeView_GetItem"));
+
+ return false;
+ }
+
+ return true;
+}
+
+void wxTreeCtrl::DoSetItem(wxTreeViewItem *tvItem)
+{
+ TreeItemUnlocker unlocker(tvItem->hItem);
+
+ if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
+ {
+ wxLogLastError(wxT("TreeView_SetItem"));
+ }
+}
+
+unsigned int wxTreeCtrl::GetCount() const
+{
+ return (unsigned int)TreeView_GetCount(GetHwnd());
+}
+
+unsigned int wxTreeCtrl::GetIndent() const
+{
+ return TreeView_GetIndent(GetHwnd());
+}
+
+void wxTreeCtrl::SetIndent(unsigned int indent)
+{
+ TreeView_SetIndent(GetHwnd(), indent);
+}
+
+void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
+{
+ // no error return
+ (void) 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;
+}
+
+size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
+ bool recursively) const
+{
+ wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
+
+ TraverseCounter counter(this, item, recursively);
+ return counter.GetCount() - 1;
+}
+
+// ----------------------------------------------------------------------------
+// control colours
+// ----------------------------------------------------------------------------
+
+bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
+{
+ if ( !wxWindowBase::SetBackgroundColour(colour) )
+ return false;
+
+ ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
+
+ return true;
+}
+
+bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
+{
+ if ( !wxWindowBase::SetForegroundColour(colour) )
+ return false;
+
+ ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
+
+ return true;
+}
+
+// ----------------------------------------------------------------------------
+// Item access
+// ----------------------------------------------------------------------------
+
+bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId& item) const
+{
+ return HITEM(item) == TVI_ROOT && HasFlag(wxTR_HIDE_ROOT);
+}
+
+wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
+{
+ wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") );
+
+ 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)
+{
+ wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
+
+ if ( IS_VIRTUAL_ROOT(item) )
+ return;
+
+ wxTreeViewItem tvItem(item, TVIF_TEXT);
+ tvItem.pszText = wxMSW_CONV_LPTSTR(text);
+ 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 == m_idEdited )
+ {
+ ::SetWindowText(hwndEdit, text.t_str());
+ }
+ }
+}
+
+int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
+ wxTreeItemIcon which) const
+{
+ wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
+
+ if ( IsHiddenRoot(item) )
+ {
+ // no images for hidden root item
+ return -1;
+ }
+
+ wxTreeItemParam *param = GetItemParam(item);
+
+ return param && param->HasImage(which) ? param->GetImage(which) : -1;
+}
+
+void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
+ wxTreeItemIcon which)
+{
+ wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
+ wxCHECK_RET( which >= 0 &&
+ which < wxTreeItemIcon_Max,
+ wxT("invalid image index"));
+
+
+ if ( IsHiddenRoot(item) )
+ {
+ // no images for hidden root item
+ return;
+ }
+
+ wxTreeItemParam *data = GetItemParam(item);
+ if ( !data )
+ return;
+
+ data->SetImage(image, which);
+
+ RefreshItem(item);
+}
+
+wxTreeItemParam *wxTreeCtrl::GetItemParam(const wxTreeItemId& item) const
+{
+ wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
+
+ wxTreeViewItem tvItem(item, TVIF_PARAM);
+
+ // hidden root may still have data.
+ if ( IS_VIRTUAL_ROOT(item) )
+ {
+ return GET_VIRTUAL_ROOT()->GetParam();
+ }
+
+ // visible node.
+ if ( !DoGetItem(&tvItem) )
+ {
+ return NULL;
+ }
+
+ return (wxTreeItemParam *)tvItem.lParam;
+}
+
+bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent& event) const
+{
+ if ( event.m_item.IsOk() )
+ {
+ event.SetClientObject(GetItemData(event.m_item));
+ }
+
+ return HandleWindowEvent(event);
+}
+
+wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
+{
+ wxTreeItemParam *data = GetItemParam(item);
+
+ return data ? data->GetData() : NULL;
+}
+
+void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
+{
+ // first, associate this piece of data with this item
+ if ( data )
+ {
+ data->SetId(item);
+ }
+
+ wxTreeItemParam *param = GetItemParam(item);
+
+ wxCHECK_RET( param, wxT("failed to change tree items data") );
+
+ param->SetData(data);
+}
+
+void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
+{
+ wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
+
+ if ( IS_VIRTUAL_ROOT(item) )
+ return;
+
+ wxTreeViewItem tvItem(item, TVIF_CHILDREN);
+ tvItem.cChildren = (int)has;
+ DoSetItem(&tvItem);
+}
+
+void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
+{
+ wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
+
+ if ( IS_VIRTUAL_ROOT(item) )
+ return;
+
+ wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
+ tvItem.state = bold ? TVIS_BOLD : 0;
+ DoSetItem(&tvItem);
+}
+
+void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
+{
+ if ( IS_VIRTUAL_ROOT(item) )
+ return;
+
+ wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
+ tvItem.state = highlight ? TVIS_DROPHILITED : 0;
+ DoSetItem(&tvItem);
+}
+
+void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
+{
+ if ( IS_VIRTUAL_ROOT(item) )
+ return;
+
+ wxRect rect;
+ if ( GetBoundingRect(item, rect) )
+ {
+ RefreshRect(rect);
+ }
+}
+
+wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
+{
+ wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
+
+ wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
+ return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour();
+}
+
+wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
+{
+ wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
+
+ wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
+ return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour();
+}
+
+wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const
+{
+ wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
+
+ wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
+ return it == m_attrs.end() ? wxNullFont : it->second->GetFont();
+}
+
+void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
+ const wxColour& col)
+{
+ wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
+
+ wxTreeItemAttr *attr;
+ wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
+ if ( it == m_attrs.end() )
+ {
+ m_hasAnyAttr = true;
+
+ m_attrs[item.m_pItem] =
+ attr = new wxTreeItemAttr;
+ }
+ else
+ {
+ attr = it->second;
+ }
+
+ attr->SetTextColour(col);
+
+ RefreshItem(item);
+}
+
+void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
+ const wxColour& col)
+{
+ wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
+
+ wxTreeItemAttr *attr;
+ wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
+ if ( it == m_attrs.end() )
+ {
+ m_hasAnyAttr = true;
+
+ m_attrs[item.m_pItem] =
+ attr = new wxTreeItemAttr;
+ }
+ else // already in the hash
+ {
+ attr = it->second;
+ }
+
+ attr->SetBackgroundColour(col);
+
+ RefreshItem(item);
+}
+
+void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
+{
+ wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
+
+ wxTreeItemAttr *attr;
+ wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
+ if ( it == m_attrs.end() )
+ {
+ m_hasAnyAttr = true;
+
+ m_attrs[item.m_pItem] =
+ attr = new wxTreeItemAttr;
+ }
+ else // already in the hash
+ {
+ attr = it->second;
+ }
+
+ attr->SetFont(font);
+
+ // Reset the item's text to ensure that the bounding rect will be adjusted
+ // for the new font.
+ SetItemText(item, GetItemText(item));
+
+ RefreshItem(item);
+}
+
+// ----------------------------------------------------------------------------
+// Item status
+// ----------------------------------------------------------------------------
+
+bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
+{
+ wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
+
+ if ( item == wxTreeItemId(TVI_ROOT) )
+ {
+ // virtual (hidden) root is never visible
+ return false;
+ }
+
+ // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
+ TVGetItemRectParam param;
+
+ // true means to get rect for just the text, not the whole line
+ if ( !wxTreeView_GetItemRect(GetHwnd(), HITEM(item), param, TRUE) )
+ {
+ // if TVM_GETITEMRECT returned false, then the item is definitely not
+ // visible (because its parent is not expanded)
+ return false;
+ }
+
+ // however if it returned true, the item might still be outside the
+ // currently visible part of the tree, test for it (notice that partly
+ // visible means visible here)
+ return param.rect.bottom > 0 && param.rect.top < GetClientSize().y;
+}
+
+bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
+{
+ wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
+
+ if ( IS_VIRTUAL_ROOT(item) )
+ {
+ wxTreeItemIdValue cookie;
+ return GetFirstChild(item, cookie).IsOk();
+ }
+
+ wxTreeViewItem tvItem(item, TVIF_CHILDREN);
+ DoGetItem(&tvItem);
+
+ return tvItem.cChildren != 0;
+}
+
+bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
+{
+ wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );