+ return true;
+}
+
+// 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);
+
+ // This message is sent after a right-click, or when the "menu" key is pressed
+ if ( nMsg == WM_CONTEXTMENU )
+ {
+ int x = GET_X_LPARAM(lParam),
+ y = GET_Y_LPARAM(lParam);
+ // Convert the screen point to a client point
+ wxPoint MenuPoint = ScreenToClient(wxPoint(x, y));
+
+ wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_MENU, GetId() );
+
+ // can't use GetSelection() here as it would assert in multiselect mode
+ event.m_item = wxTreeItemId(TreeView_GetSelection(GetHwnd()));
+ event.SetEventObject( this );
+
+ // Get the bounding rectangle for the item, including the non-text areas
+ wxRect ItemRect;
+ GetBoundingRect(event.m_item, ItemRect, false);
+ // If the point is inside the bounding rectangle, use it as the click position.
+ // This should be the case for WM_CONTEXTMENU as the result of a right-click
+ if (ItemRect.Inside(MenuPoint))
+ {
+ event.m_pointDrag = MenuPoint;
+ }
+ // Use the Explorer standard of putting the menu at the left edge of the text,
+ // in the vertical middle of the text. Should be the case for the "menu" key
+ else
+ {
+ // Use the bounding rectangle of only the text part
+ GetBoundingRect(event.m_item, ItemRect, true);
+ event.m_pointDrag = wxPoint(ItemRect.GetX(), ItemRect.GetY() + ItemRect.GetHeight() / 2);
+ }
+
+ if ( GetEventHandler()->ProcessEvent(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);
+ HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y);
+
+ switch ( nMsg )
+ {
+ case WM_RBUTTONDOWN:
+ // if the item we are about to right click on is not already
+ // selected or if we click outside of any item, remove the
+ // entire previous selection
+ if ( !htItem || !::IsItemSelected(GetHwnd(), htItem) )
+ {
+ UnselectAll();
+ }
+
+ // select item and set the focus to the
+ // newly selected item
+ ::SelectItem(GetHwnd(), htItem);
+ ::SetFocus(GetHwnd(), htItem);
+ break;
+
+#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
+ case WM_LBUTTONDOWN:
+ if ( htItem && isMultiple )
+ {
+ if ( wParam & MK_CONTROL )
+ {
+ SetFocus();
+
+ // toggle selected state
+ ::ToggleItemSelection(GetHwnd(), htItem);
+
+ ::SetFocus(GetHwnd(), htItem);
+
+ // reset on any click without Shift
+ m_htSelStart.Unset();
+
+ processed = true;
+ }
+ else if ( wParam & MK_SHIFT )
+ {
+ // this selects all items between the starting one and
+ // the current
+
+ if ( !m_htSelStart )
+ {
+ // take the focused item
+ m_htSelStart = TreeView_GetSelection(GetHwnd());
+ }
+
+ SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
+ !(wParam & MK_CONTROL));
+
+ ::SetFocus(GetHwnd(), htItem);
+
+ processed = true;
+ }
+ 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_PTR(selections[0]) != htItem )
+ {
+ // 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))
+ {
+ ::SetFocus(GetHwnd(), htItem);
+ }
+ else
+ {
+ UnselectAll();
+
+ // prevent the click from starting in-place editing
+ // which should only happen if we click on the
+ // already selected item (and nothing else is
+ // selected)
+
+ TreeView_SelectItem(GetHwnd(), 0);
+ ::SelectItem(GetHwnd(), htItem);
+ }
+ }
+
+ // reset on any click without Shift
+ m_htSelStart.Unset();
+ }
+ }
+ break;
+#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
+
+ case WM_MOUSEMOVE:
+ 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();
+ }
+ }
+ break;
+
+ case WM_LBUTTONUP:
+
+ // facilitates multiple drag-and-drop
+ if (htItem && isMultiple)
+ {
+ wxArrayTreeItemIds selections;
+ size_t count = GetSelections(selections);
+
+ if (count > 1 &&
+ !(wParam & MK_CONTROL) &&
+ !(wParam & MK_SHIFT))
+ {
+ UnselectAll();
+ TreeView_SelectItem(GetHwnd(), htItem);
+ }
+ }
+
+ // fall through
+
+ case WM_RBUTTONUP:
+ if ( m_dragImage )
+ {
+ m_dragImage->EndDrag();
+ delete m_dragImage;
+ m_dragImage = NULL;
+
+ // generate the drag end event
+ wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
+
+ event.m_item = htItem;
+ event.m_pointDrag = wxPoint(x, y);
+ event.SetEventObject(this);
+
+ (void)GetEventHandler()->ProcessEvent(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);
+ }
+ break;
+ }
+ }
+#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
+ else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && 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);
+ RECT rect;
+ for ( size_t n = 0; n < count; n++ )
+ {
+ // TreeView_GetItemRect() will return false if item is not visible,
+ // which may happen perfectly well
+ if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections[n]),
+ &rect, TRUE) )
+ {
+ ::InvalidateRect(GetHwnd(), &rect, FALSE);
+ }
+ }
+ }
+ else if ( nMsg == WM_KEYDOWN && isMultiple )
+ {
+ bool bCtrl = wxIsCtrlDown(),
+ bShift = wxIsShiftDown();
+
+ HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
+ switch ( wParam )
+ {
+ case VK_SPACE:
+ if ( bCtrl )
+ {
+ ::ToggleItemSelection(GetHwnd(), htSel);
+ }
+ else
+ {
+ UnselectAll();
+
+ ::SelectItem(GetHwnd(), htSel);
+ }
+
+ processed = true;
+ break;
+
+ case VK_UP:
+ case VK_DOWN:
+ if ( !bCtrl && !bShift )
+ {
+ // no modifiers, just clear selection and then let the default
+ // processing to take place
+ UnselectAll();
+ }
+ else if ( htSel )
+ {
+ (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
+
+ HTREEITEM htNext = (HTREEITEM)
+ TreeView_GetNextItem
+ (
+ GetHwnd(),
+ htSel,
+ wParam == VK_UP ? TVGN_PREVIOUSVISIBLE
+ : TVGN_NEXTVISIBLE
+ );
+
+ if ( !htNext )
+ {
+ // at the top/bottom
+ htNext = htSel;
+ }
+
+ if ( bShift )
+ {
+ if ( !m_htSelStart )
+ m_htSelStart = htSel;
+
+ SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
+ }
+ else // bCtrl
+ {
+ // without changing selection
+ ::SetFocus(GetHwnd(), htNext);
+ }
+
+ processed = true;
+ }
+ break;
+
+ case VK_HOME:
+ case VK_END:
+ case VK_PRIOR:
+ case VK_NEXT:
+ // TODO: handle Shift/Ctrl with these keys
+ if ( !bCtrl && !bShift )
+ {
+ UnselectAll();
+
+ m_htSelStart.Unset();
+ }
+ }
+ }
+#endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
+ else if ( nMsg == WM_COMMAND )
+ {
+ // if we receive a EN_KILLFOCUS command from the in-place edit control
+ // used for label editing, make sure to end editing
+ WORD id, cmd;
+ WXHWND hwnd;
+ UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
+
+ if ( cmd == EN_KILLFOCUS )
+ {
+ if ( m_textCtrl && m_textCtrl->GetHandle() == hwnd )
+ {
+ DoEndEditLabel();
+
+ processed = true;
+ }
+ }
+ }
+
+ if ( !processed )
+ rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
+
+ return rc;
+}
+
+WXLRESULT
+wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
+{
+ // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
+ // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
+ // the parent window, not us, which completely breaks everything so simply
+ // don't let it see this message at all
+ if ( nMsg == WM_RBUTTONDOWN )
+ return 0;
+
+ // but because of the above we don't get NM_RCLICK which is normally
+ // generated by tree window proc when the modal loop mentioned above ends
+ // because the mouse is released -- synthesize it ourselves instead
+ if ( nMsg == WM_RBUTTONUP )
+ {
+ NMHDR hdr;
+ hdr.hwndFrom = GetHwnd();
+ hdr.idFrom = GetId();
+ hdr.code = NM_RCLICK;
+
+ WXLPARAM rc;
+ MSWOnNotify(GetId(), (LPARAM)&hdr, &rc);
+
+ // continue as usual
+ }
+
+ if ( nMsg == WM_CHAR )
+ {
+ // also don't let the control process Space and Return keys because it
+ // doesn't do anything useful with them anyhow but always beeps
+ // annoyingly when it receives them and there is no way to turn it off
+ // simply if you just process TREEITEM_ACTIVATED event to which Space
+ // and Enter presses are mapped in your code
+ if ( wParam == VK_SPACE || wParam == VK_RETURN )
+ return 0;
+ }
+
+ return wxControl::MSWDefWindowProc(nMsg, wParam, lParam);