+ #undef TRANSLATE_FLAG
+
+ return wxTreeItemId(hitTestInfo.hItem);
+}
+
+bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
+ wxRect& rect,
+ bool textOnly) const
+{
+ // Virtual root items have no bounding rectangle
+ if ( IS_VIRTUAL_ROOT(item) )
+ {
+ return false;
+ }
+
+ TVGetItemRectParam param;
+
+ if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(item), param, textOnly) )
+ {
+ rect = wxRect(wxPoint(param.rect.left, param.rect.top),
+ wxPoint(param.rect.right, param.rect.bottom));
+
+ return true;
+ }
+ else
+ {
+ // couldn't retrieve rect: for example, item isn't visible
+ return false;
+ }
+}
+
+void wxTreeCtrl::ClearFocusedItem()
+{
+ TempSetter set(m_changingSelection);
+
+ if ( !TreeView_SelectItem(GetHwnd(), 0) )
+ {
+ wxLogLastError(wxT("TreeView_SelectItem"));
+ }
+}
+
+void wxTreeCtrl::SetFocusedItem(const wxTreeItemId& item)
+{
+ wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
+
+ TempSetter set(m_changingSelection);
+
+ ::SetFocus(GetHwnd(), HITEM(item));
+}
+
+void wxTreeCtrl::DoUnselectItem(const wxTreeItemId& item)
+{
+ TempSetter set(m_changingSelection);
+
+ ::UnselectItem(GetHwnd(), HITEM(item));
+}
+
+void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId& item)
+{
+ TempSetter set(m_changingSelection);
+
+ ::ToggleItemSelection(GetHwnd(), HITEM(item));
+}
+
+// ----------------------------------------------------------------------------
+// sorting stuff
+// ----------------------------------------------------------------------------
+
+// this is just a tiny namespace which is friend to wxTreeCtrl and so can use
+// functions such as IsDataIndirect()
+class wxTreeSortHelper
+{
+public:
+ static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
+
+private:
+ static wxTreeItemId GetIdFromData(LPARAM lParam)
+ {
+ return ((wxTreeItemParam*)lParam)->GetItem();
+ }
+};
+
+int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
+ LPARAM pItem2,
+ LPARAM htree)
+{
+ wxCHECK_MSG( pItem1 && pItem2, 0,
+ wxT("sorting tree without data doesn't make sense") );
+
+ wxTreeCtrl *tree = (wxTreeCtrl *)htree;
+
+ return tree->OnCompareItems(GetIdFromData(pItem1),
+ GetIdFromData(pItem2));
+}
+
+void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
+{
+ wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
+
+ // rely on the fact that TreeView_SortChildren does the same thing as our
+ // default behaviour, i.e. sorts items alphabetically and so call it
+ // directly if we're not in derived class (much more efficient!)
+ // RN: Note that if you find you're code doesn't sort as expected this
+ // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
+ // combo for your derived wxTreeCtrl if will sort without
+ // OnCompareItems
+ if ( GetClassInfo() == wxCLASSINFO(wxTreeCtrl) )
+ {
+ TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
+ }
+ else
+ {
+ TV_SORTCB tvSort;
+ tvSort.hParent = HITEM(item);
+ tvSort.lpfnCompare = wxTreeSortHelper::Compare;
+ tvSort.lParam = (LPARAM)this;
+ TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
+ }
+}
+
+// ----------------------------------------------------------------------------
+// implementation
+// ----------------------------------------------------------------------------
+
+bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
+{
+ if ( msg->message == WM_KEYDOWN )
+ {
+ // Only eat VK_RETURN if not being used by the application in
+ // conjunction with modifiers
+ if ( (msg->wParam == VK_RETURN) && !wxIsAnyModifierDown() )
+ {
+ // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
+ return false;
+ }
+ }
+
+ return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg);
+}
+
+bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id_)
+{
+ const int id = (signed short)id_;
+
+ if ( cmd == EN_UPDATE )
+ {
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
+ event.SetEventObject( this );
+ ProcessCommand(event);
+ }
+ else if ( cmd == EN_KILLFOCUS )
+ {
+ wxCommandEvent event(wxEVT_KILL_FOCUS, id);
+ event.SetEventObject( this );
+ ProcessCommand(event);
+ }
+ else
+ {
+ // nothing done
+ return false;
+ }
+
+ // command processed
+ return true;
+}
+
+bool wxTreeCtrl::MSWIsOnItem(unsigned flags) const
+{
+ unsigned mask = TVHT_ONITEM;
+ if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
+ mask |= TVHT_ONITEMINDENT | TVHT_ONITEMRIGHT;
+
+ return (flags & mask) != 0;
+}
+
+bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey)
+{
+ const bool bCtrl = wxIsCtrlDown();
+ const bool bShift = wxIsShiftDown();
+ const HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
+
+ switch ( vkey )
+ {
+ case VK_RETURN:
+ case VK_SPACE:
+ if ( !htSel )
+ break;
+
+ if ( vkey != VK_RETURN && bCtrl )
+ {
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, htSel);
+ changingEvent.m_itemOld = htSel;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoToggleItemSelection(wxTreeItemId(htSel));
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, htSel);
+ changedEvent.m_itemOld = htSel;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ else
+ {
+ wxArrayTreeItemIds selections;
+ size_t count = GetSelections(selections);
+
+ if ( count != 1 || HITEM(selections[0]) != htSel )
+ {
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, htSel);
+ changingEvent.m_itemOld = htSel;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoUnselectAll();
+ DoSelectItem(wxTreeItemId(htSel));
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, htSel);
+ changedEvent.m_itemOld = htSel;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ }
+ break;
+
+ case VK_UP:
+ case VK_DOWN:
+ if ( !bCtrl && !bShift )
+ {
+ wxArrayTreeItemIds selections;
+ wxTreeItemId next;
+
+ if ( htSel )
+ {
+ next = vkey == VK_UP
+ ? TreeView_GetPrevVisible(GetHwnd(), htSel)
+ : TreeView_GetNextVisible(GetHwnd(), htSel);
+ }
+ else
+ {
+ next = GetRootItem();
+
+ if ( IsHiddenRoot(next) )
+ next = TreeView_GetChild(GetHwnd(), HITEM(next));
+ }
+
+ if ( !next.IsOk() )
+ {
+ break;
+ }
+
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, next);
+ changingEvent.m_itemOld = htSel;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoUnselectAll();
+ DoSelectItem(next);
+ SetFocusedItem(next);
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, next);
+ changedEvent.m_itemOld = htSel;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ else if ( htSel )
+ {
+ wxTreeItemId next = vkey == VK_UP
+ ? TreeView_GetPrevVisible(GetHwnd(), htSel)
+ : TreeView_GetNextVisible(GetHwnd(), htSel);
+
+ if ( !next.IsOk() )
+ {
+ break;
+ }
+
+ if ( !m_htSelStart )
+ {
+ m_htSelStart = htSel;
+ }
+
+ if ( bShift && SelectRange(GetHwnd(), HITEM(m_htSelStart), HITEM(next),
+ SR_UNSELECT_OTHERS | SR_SIMULATE) )
+ {
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next);
+ changingEvent.m_itemOld = htSel;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ SelectRange(GetHwnd(), HITEM(m_htSelStart), HITEM(next),
+ SR_UNSELECT_OTHERS);
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next);
+ changedEvent.m_itemOld = htSel;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+
+ SetFocusedItem(next);
+ }
+ break;
+
+ case VK_LEFT:
+ if ( HasChildren(htSel) && IsExpanded(htSel) )
+ {
+ Collapse(htSel);
+ }
+ else
+ {
+ wxTreeItemId next = GetItemParent(htSel);
+
+ if ( next.IsOk() && !IsHiddenRoot(next) )
+ {
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, next);
+ changingEvent.m_itemOld = htSel;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoUnselectAll();
+ DoSelectItem(next);
+ SetFocusedItem(next);
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, next);
+ changedEvent.m_itemOld = htSel;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ }
+ break;
+
+ case VK_RIGHT:
+ if ( !IsVisible(htSel) )
+ {
+ EnsureVisible(htSel);
+ }
+
+ if ( !HasChildren(htSel) )
+ break;
+
+ if ( !IsExpanded(htSel) )
+ {
+ Expand(htSel);
+ }
+ else
+ {
+ wxTreeItemId next = TreeView_GetChild(GetHwnd(), htSel);
+
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next);
+ changingEvent.m_itemOld = htSel;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoUnselectAll();
+ DoSelectItem(next);
+ SetFocusedItem(next);
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next);
+ changedEvent.m_itemOld = htSel;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ break;
+
+ case VK_HOME:
+ case VK_END:
+ {
+ wxTreeItemId next = GetRootItem();
+
+ if ( IsHiddenRoot(next) )
+ {
+ next = TreeView_GetChild(GetHwnd(), HITEM(next));
+ }
+
+ if ( !next.IsOk() )
+ break;
+
+ if ( vkey == VK_END )
+ {
+ for ( ;; )
+ {
+ wxTreeItemId nextTemp = TreeView_GetNextVisible(
+ GetHwnd(), HITEM(next));
+
+ if ( !nextTemp.IsOk() )
+ break;
+
+ next = nextTemp;
+ }
+ }
+
+ if ( htSel == HITEM(next) )
+ break;
+
+ if ( bShift )
+ {
+ if ( !m_htSelStart )
+ {
+ m_htSelStart = htSel;
+ }
+
+ if ( SelectRange(GetHwnd(),
+ HITEM(m_htSelStart), HITEM(next),
+ SR_UNSELECT_OTHERS | SR_SIMULATE) )
+ {
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, next);
+ changingEvent.m_itemOld = htSel;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ SelectRange(GetHwnd(),
+ HITEM(m_htSelStart), HITEM(next),
+ SR_UNSELECT_OTHERS);
+ SetFocusedItem(next);
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, next);
+ changedEvent.m_itemOld = htSel;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ }
+ else // no Shift
+ {
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, next);
+ changingEvent.m_itemOld = htSel;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoUnselectAll();
+ DoSelectItem(next);
+ SetFocusedItem(next);
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, next);
+ changedEvent.m_itemOld = htSel;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ }
+ break;
+
+ case VK_PRIOR:
+ case VK_NEXT:
+ if ( bCtrl )
+ {
+ wxTreeItemId firstVisible = GetFirstVisibleItem();
+ size_t visibleCount = TreeView_GetVisibleCount(GetHwnd());
+ wxTreeItemId nextAdjacent = (vkey == VK_PRIOR) ?
+ TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible)) :
+ TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible));
+
+ if ( !nextAdjacent )
+ {
+ break;
+ }
+
+ wxTreeItemId nextStart = firstVisible;
+
+ for ( size_t n = 1; n < visibleCount; n++ )
+ {
+ wxTreeItemId nextTemp = (vkey == VK_PRIOR) ?
+ TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart)) :
+ TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart));
+
+ if ( nextTemp.IsOk() )
+ {
+ nextStart = nextTemp;
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ EnsureVisible(nextStart);
+
+ if ( vkey == VK_NEXT )
+ {
+ wxTreeItemId nextEnd = nextStart;
+
+ for ( size_t n = 1; n < visibleCount; n++ )
+ {
+ wxTreeItemId nextTemp =
+ TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd));
+
+ if ( nextTemp.IsOk() )
+ {
+ nextEnd = nextTemp;
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ EnsureVisible(nextEnd);
+ }
+ }
+ else // no Ctrl
+ {
+ size_t visibleCount = TreeView_GetVisibleCount(GetHwnd());
+ wxTreeItemId nextAdjacent = (vkey == VK_PRIOR) ?
+ TreeView_GetPrevVisible(GetHwnd(), htSel) :
+ TreeView_GetNextVisible(GetHwnd(), htSel);
+
+ if ( !nextAdjacent )
+ {
+ break;
+ }
+
+ wxTreeItemId next(htSel);
+
+ for ( size_t n = 1; n < visibleCount; n++ )
+ {
+ wxTreeItemId nextTemp = vkey == VK_PRIOR ?
+ TreeView_GetPrevVisible(GetHwnd(), HITEM(next)) :
+ TreeView_GetNextVisible(GetHwnd(), HITEM(next));
+
+ if ( !nextTemp.IsOk() )
+ break;
+
+ next = nextTemp;
+ }
+
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, next);
+ changingEvent.m_itemOld = htSel;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoUnselectAll();
+ m_htSelStart.Unset();
+ DoSelectItem(next);
+ SetFocusedItem(next);
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, next);
+ changedEvent.m_itemOld = htSel;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ break;
+
+ default:
+ return false;
+ }
+
+ return true;
+}
+
+bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam, WXLPARAM lParam)
+{
+ wxTreeEvent keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN, this);
+ keyEvent.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN, wParam, lParam);
+
+ bool processed = HandleTreeEvent(keyEvent);
+
+ // generate a separate event for Space/Return
+ if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
+ ((wParam == VK_SPACE) || (wParam == VK_RETURN)) )
+ {
+ const HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
+ if ( htSel )
+ {
+ wxTreeEvent activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
+ this, htSel);
+ (void)HandleTreeEvent(activatedEvent);
+ }
+ }
+
+ return processed;
+}
+
+// we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
+// only do it during dragging, minimize wxWin overhead (this is important for
+// WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
+// instead of passing by wxWin events
+WXLRESULT
+wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
+{
+ bool processed = false;
+ WXLRESULT rc = 0;
+ bool isMultiple = HasFlag(wxTR_MULTIPLE);
+
+ if ( nMsg == WM_CONTEXTMENU )
+ {
+ int x = GET_X_LPARAM(lParam),
+ y = GET_Y_LPARAM(lParam);
+
+ // the item for which the menu should be shown
+ wxTreeItemId item;
+
+ // the position where the menu should be shown in client coordinates
+ // (so that it can be passed directly to PopupMenu())
+ wxPoint pt;
+
+ if ( x == -1 || y == -1 )
+ {
+ // this means that the event was generated from keyboard (e.g. with
+ // Shift-F10 or special Windows menu key)
+ //
+ // use the Explorer standard of putting the menu at the left edge
+ // of the text, in the vertical middle of the text
+ item = wxTreeItemId(TreeView_GetSelection(GetHwnd()));
+ if ( item.IsOk() )
+ {
+ // Use the bounding rectangle of only the text part
+ wxRect rect;
+ GetBoundingRect(item, rect, true);
+ pt = wxPoint(rect.GetX(), rect.GetY() + rect.GetHeight() / 2);
+ }
+ }
+ else // event from mouse, use mouse position
+ {
+ pt = ScreenToClient(wxPoint(x, y));
+
+ TV_HITTESTINFO tvhti;
+ tvhti.pt.x = pt.x;
+ tvhti.pt.y = pt.y;
+
+ if ( TreeView_HitTest(GetHwnd(), &tvhti) )
+ item = wxTreeItemId(tvhti.hItem);
+ }
+
+ // create the event
+ if ( item.IsOk() )
+ {
+ wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this, item);
+
+ event.m_pointDrag = pt;
+
+ if ( HandleTreeEvent(event) )
+ processed = true;
+ //else: continue with generating wxEVT_CONTEXT_MENU in base class code
+ }
+ }
+ else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
+ {
+ // we only process mouse messages here and these parameters have the
+ // same meaning for all of them
+ int x = GET_X_LPARAM(lParam),
+ y = GET_Y_LPARAM(lParam);
+
+ TV_HITTESTINFO tvht;
+ tvht.pt.x = x;
+ tvht.pt.y = y;
+
+ HTREEITEM htOldItem = TreeView_GetSelection(GetHwnd());
+ HTREEITEM htItem = TreeView_HitTest(GetHwnd(), &tvht);
+
+ switch ( nMsg )
+ {
+ case WM_LBUTTONDOWN:
+ if ( !isMultiple )
+ break;
+
+ m_htClickedItem.Unset();
+
+ if ( !MSWIsOnItem(tvht.flags) )
+ {
+ if ( tvht.flags & TVHT_ONITEMBUTTON )
+ {
+ // either it's going to be handled by user code or
+ // we're going to use it ourselves to toggle the
+ // branch, in either case don't pass it to the base
+ // class which would generate another mouse click event
+ // for it even though it's already handled here
+ processed = true;
+ SetFocus();
+
+ if ( !HandleMouseEvent(nMsg, x, y, wParam) )
+ {
+ if ( !IsExpanded(htItem) )
+ {
+ Expand(htItem);
+ }
+ else
+ {
+ Collapse(htItem);
+ }
+ }
+ }
+
+ m_focusLost = false;
+ break;
+ }
+
+ processed = true;
+ SetFocus();
+ m_htClickedItem = (WXHTREEITEM) htItem;
+ m_ptClick = wxPoint(x, y);
+
+ if ( wParam & MK_CONTROL )
+ {
+ if ( HandleMouseEvent(nMsg, x, y, wParam) )
+ {
+ m_htClickedItem.Unset();
+ break;
+ }
+
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, htItem);
+ changingEvent.m_itemOld = htOldItem;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ // toggle selected state
+ DoToggleItemSelection(wxTreeItemId(htItem));
+
+ SetFocusedItem(wxTreeItemId(htItem));
+
+ // reset on any click without Shift
+ m_htSelStart.Unset();
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, htItem);
+ changedEvent.m_itemOld = htOldItem;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ else if ( wParam & MK_SHIFT )
+ {
+ if ( HandleMouseEvent(nMsg, x, y, wParam) )
+ {
+ m_htClickedItem.Unset();
+ break;
+ }
+
+ int srFlags = 0;
+ bool willChange = true;
+
+ if ( !(wParam & MK_CONTROL) )
+ {
+ srFlags |= SR_UNSELECT_OTHERS;
+ }
+
+ if ( !m_htSelStart )
+ {
+ // take the focused item
+ m_htSelStart = htOldItem;
+ }
+ else
+ {
+ willChange = SelectRange(GetHwnd(), HITEM(m_htSelStart),
+ htItem, srFlags | SR_SIMULATE);
+ }
+
+ if ( willChange )
+ {
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, htItem);
+ changingEvent.m_itemOld = htOldItem;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ // this selects all items between the starting one
+ // and the current
+ if ( m_htSelStart )
+ {
+ SelectRange(GetHwnd(), HITEM(m_htSelStart),
+ htItem, srFlags);
+ }
+ else
+ {
+ DoSelectItem(wxTreeItemId(htItem));
+ }
+
+ SetFocusedItem(wxTreeItemId(htItem));
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, htItem);
+ changedEvent.m_itemOld = htOldItem;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ }
+ else // normal click
+ {
+ // avoid doing anything if we click on the only
+ // currently selected item
+
+ wxArrayTreeItemIds selections;
+ size_t count = GetSelections(selections);
+
+ if ( count == 0 ||
+ count > 1 ||
+ HITEM(selections[0]) != htItem )
+ {
+ if ( HandleMouseEvent(nMsg, x, y, wParam) )
+ {
+ m_htClickedItem.Unset();
+ break;
+ }
+
+ // clear the previously selected items, if the user
+ // clicked outside of the present selection, otherwise,
+ // perform the deselection on mouse-up, this allows
+ // multiple drag and drop to work.
+ if ( !IsItemSelected(GetHwnd(), htItem))
+ {
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, htItem);
+ changingEvent.m_itemOld = htOldItem;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoUnselectAll();
+ DoSelectItem(wxTreeItemId(htItem));
+ SetFocusedItem(wxTreeItemId(htItem));
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, htItem);
+ changedEvent.m_itemOld = htOldItem;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ else
+ {
+ SetFocusedItem(wxTreeItemId(htItem));
+ m_mouseUpDeselect = true;
+ }
+ }
+ else // click on a single selected item
+ {
+ // don't interfere with the default processing in
+ // WM_MOUSEMOVE handler below as the default window
+ // proc will start the drag itself if we let have
+ // WM_LBUTTONDOWN
+ m_htClickedItem.Unset();
+
+ // prevent in-place editing from starting if focus lost
+ // since previous click
+ if ( m_focusLost )
+ {
+ ClearFocusedItem();
+ DoSelectItem(wxTreeItemId(htItem));
+ SetFocusedItem(wxTreeItemId(htItem));
+ }
+ else
+ {
+ processed = false;
+ }
+ }
+
+ // reset on any click without Shift
+ m_htSelStart.Unset();
+ }
+
+ m_focusLost = false;
+
+ // we consumed the event so we need to trigger state image
+ // click if needed
+ if ( processed )
+ {
+ if ( tvht.flags & TVHT_ONITEMSTATEICON )
+ {
+ m_triggerStateImageClick = true;
+ }
+ }
+ break;
+
+ case WM_RBUTTONDOWN:
+ if ( !isMultiple )
+ break;
+
+ processed = true;
+ SetFocus();
+
+ if ( HandleMouseEvent(nMsg, x, y, wParam) || !htItem )
+ {
+ break;
+ }
+
+ // default handler removes the highlight from the currently
+ // focused item when right mouse button is pressed on another
+ // one but keeps the remaining items highlighted, which is
+ // confusing, so override this default behaviour
+ if ( !IsItemSelected(GetHwnd(), htItem) )
+ {
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, htItem);
+ changingEvent.m_itemOld = htOldItem;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoUnselectAll();
+ DoSelectItem(wxTreeItemId(htItem));
+ SetFocusedItem(wxTreeItemId(htItem));
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, htItem);
+ changedEvent.m_itemOld = htOldItem;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+
+ break;
+
+ case WM_MOUSEMOVE:
+#ifndef __WXWINCE__
+ if ( m_htClickedItem )
+ {
+ int cx = abs(m_ptClick.x - x);
+ int cy = abs(m_ptClick.y - y);
+
+ if ( cx > ::GetSystemMetrics(SM_CXDRAG) ||
+ cy > ::GetSystemMetrics(SM_CYDRAG) )
+ {
+ NM_TREEVIEW tv;
+ wxZeroMemory(tv);
+
+ tv.hdr.hwndFrom = GetHwnd();
+ tv.hdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
+ tv.hdr.code = TVN_BEGINDRAG;
+
+ tv.itemNew.hItem = HITEM(m_htClickedItem);
+
+
+ TVITEM tviAux;
+ wxZeroMemory(tviAux);
+
+ tviAux.hItem = HITEM(m_htClickedItem);
+ tviAux.mask = TVIF_STATE | TVIF_PARAM;
+ tviAux.stateMask = 0xffffffff;
+ TreeView_GetItem(GetHwnd(), &tviAux);
+
+ tv.itemNew.state = tviAux.state;
+ tv.itemNew.lParam = tviAux.lParam;
+
+ tv.ptDrag.x = x;
+ tv.ptDrag.y = y;
+
+ // do it before SendMessage() call below to avoid
+ // reentrancies here if there is another WM_MOUSEMOVE
+ // in the queue already
+ m_htClickedItem.Unset();
+
+ ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY,
+ tv.hdr.idFrom, (LPARAM)&tv );
+
+ // don't pass it to the default window proc, it would
+ // start dragging again
+ processed = true;
+ }
+ }
+#endif // __WXWINCE__
+
+#if wxUSE_DRAGIMAGE
+ if ( m_dragImage )
+ {
+ m_dragImage->Move(wxPoint(x, y));
+ if ( htItem )
+ {
+ // highlight the item as target (hiding drag image is
+ // necessary - otherwise the display will be corrupted)
+ m_dragImage->Hide();
+ TreeView_SelectDropTarget(GetHwnd(), htItem);
+ m_dragImage->Show();
+ }
+ }
+#endif // wxUSE_DRAGIMAGE
+ break;
+
+ case WM_LBUTTONUP:
+ if ( isMultiple )
+ {
+ // deselect other items if needed
+ if ( htItem )
+ {
+ if ( m_mouseUpDeselect )
+ {
+ m_mouseUpDeselect = false;
+
+ wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
+ this, htItem);
+ changingEvent.m_itemOld = htOldItem;
+
+ if ( IsTreeEventAllowed(changingEvent) )
+ {
+ DoUnselectAll();
+ DoSelectItem(wxTreeItemId(htItem));
+ SetFocusedItem(wxTreeItemId(htItem));
+
+ wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
+ this, htItem);
+ changedEvent.m_itemOld = htOldItem;
+ (void)HandleTreeEvent(changedEvent);
+ }
+ }
+ }
+
+ m_htClickedItem.Unset();
+
+ if ( m_triggerStateImageClick )
+ {
+ if ( tvht.flags & TVHT_ONITEMSTATEICON )
+ {
+ wxTreeEvent event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK,
+ this, htItem);
+ (void)HandleTreeEvent(event);
+
+ m_triggerStateImageClick = false;
+ processed = true;
+ }
+ }
+
+ if ( !m_dragStarted && MSWIsOnItem(tvht.flags) )
+ {
+ processed = true;
+ }
+ }
+
+ // fall through
+
+ case WM_RBUTTONUP:
+#if wxUSE_DRAGIMAGE
+ if ( m_dragImage )
+ {
+ m_dragImage->EndDrag();
+ wxDELETE(m_dragImage);
+
+ // generate the drag end event
+ wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG,
+ this, htItem);
+ event.m_pointDrag = wxPoint(x, y);
+ (void)HandleTreeEvent(event);
+
+ // if we don't do it, the tree seems to think that 2 items
+ // are selected simultaneously which is quite weird
+ TreeView_SelectDropTarget(GetHwnd(), 0);
+ }
+#endif // wxUSE_DRAGIMAGE
+
+ if ( isMultiple && nMsg == WM_RBUTTONUP )
+ {
+ // send NM_RCLICK
+ NMHDR nmhdr;
+ nmhdr.hwndFrom = GetHwnd();
+ nmhdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
+ nmhdr.code = NM_RCLICK;
+ ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY,
+ nmhdr.idFrom, (LPARAM)&nmhdr);
+ processed = true;
+ }
+
+ m_dragStarted = false;
+
+ break;
+ }
+ }
+ else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) )
+ {
+ if ( isMultiple )
+ {
+ // the tree control greys out the selected item when it loses focus
+ // and paints it as selected again when it regains it, but it won't
+ // do it for the other items itself - help it
+ wxArrayTreeItemIds selections;
+ size_t count = GetSelections(selections);
+ TVGetItemRectParam param;
+
+ for ( size_t n = 0; n < count; n++ )
+ {
+ // TreeView_GetItemRect() will return false if item is not
+ // visible, which may happen perfectly well
+ if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
+ param, TRUE) )
+ {
+ ::InvalidateRect(GetHwnd(), ¶m.rect, FALSE);
+ }
+ }
+ }
+
+ if ( nMsg == WM_KILLFOCUS )
+ {
+ m_focusLost = true;
+ }
+ }
+ else if ( (nMsg == WM_KEYDOWN || nMsg == WM_SYSKEYDOWN) && isMultiple )
+ {
+ // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
+ // notification but for the keys which can be used to change selection
+ // we need to do it from here so as to not apply the default behaviour
+ // if the events are handled by the user code
+ switch ( wParam )
+ {
+ case VK_RETURN:
+ case VK_SPACE:
+ case VK_UP:
+ case VK_DOWN:
+ case VK_LEFT:
+ case VK_RIGHT:
+ case VK_HOME:
+ case VK_END:
+ case VK_PRIOR:
+ case VK_NEXT:
+ if ( !HandleKeyDown(wParam, lParam) &&
+ !MSWHandleTreeKeyDownEvent(wParam, lParam) )
+ {
+ // use the key to update the selection if it was left
+ // unprocessed
+ MSWHandleSelectionKey(wParam);
+ }