+// 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
+long wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
+{
+ if ( m_dragImage )
+ {
+ switch ( nMsg )
+ {
+ case WM_MOUSEMOVE:
+ {
+ int x = GET_X_LPARAM(lParam),
+ y = GET_Y_LPARAM(lParam);
+
+ m_dragImage->Move(wxPoint(x, y), this);
+
+ HTREEITEM htiTarget = GetItemFromPoint(GetHwnd(), x, y);
+ if ( htiTarget )
+ {
+ // highlight the item as target (hiding drag image is
+ // necessary - otherwise the display will be corrupted)
+ m_dragImage->Hide(this);
+ TreeView_SelectDropTarget(GetHwnd(), htiTarget);
+ m_dragImage->Show(this);
+ }
+ }
+ break;
+
+ case WM_LBUTTONUP:
+ case WM_RBUTTONUP:
+ {
+ m_dragImage->EndDrag(this);
+ delete m_dragImage;
+ m_dragImage = NULL;
+
+ // generate the drag end event
+ wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
+
+ int x = GET_X_LPARAM(lParam),
+ y = GET_Y_LPARAM(lParam);
+
+ event.m_item
+ = (WXHTREEITEM)GetItemFromPoint(GetHwnd(), x, y);
+ event.m_pointDrag = wxPoint(x, y);
+ event.SetEventObject(this);
+
+ (void)GetEventHandler()->ProcessEvent(event);
+ }
+ break;
+ }
+ }
+
+ return wxControl::MSWWindowProc(nMsg, wParam, lParam);
+}
+
+// process WM_NOTIFY Windows message
+bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
+{
+ wxTreeEvent event(wxEVT_NULL, m_windowId);
+ wxEventType eventType = wxEVT_NULL;
+ NMHDR *hdr = (NMHDR *)lParam;
+
+ switch ( hdr->code )
+ {
+ case NM_RCLICK:
+ {
+ if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
+ return TRUE;
+
+ TV_HITTESTINFO tvhti;
+ ::GetCursorPos(&(tvhti.pt));
+ ::ScreenToClient(GetHwnd(),&(tvhti.pt));
+ if ( TreeView_HitTest(GetHwnd(),&tvhti) )
+ {
+ if( tvhti.flags & TVHT_ONITEM )
+ {
+ event.m_item = (WXHTREEITEM) tvhti.hItem;
+ eventType = wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
+ }
+ }
+ }
+ break;
+
+ case TVN_BEGINDRAG:
+ eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
+ // fall through
+
+ case TVN_BEGINRDRAG:
+ {
+ if ( eventType == wxEVT_NULL )
+ eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
+ //else: left drag, already set above
+
+ NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
+
+ event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
+ event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
+
+ // don't allow dragging by default: the user code must
+ // explicitly say that it wants to allow it to avoid breaking
+ // the old apps
+ event.Veto();
+ }
+ break;
+
+ case TVN_BEGINLABELEDIT:
+ {
+ eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
+ TV_DISPINFO *info = (TV_DISPINFO *)lParam;
+
+ event.m_item = (WXHTREEITEM) info->item.hItem;
+ event.m_label = info->item.pszText;
+ }
+ break;
+
+ case TVN_DELETEITEM:
+ {
+ eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
+ NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
+
+ event.m_item = (WXHTREEITEM)tv->itemOld.hItem;
+
+ if ( m_hasAnyAttr )
+ {
+ delete (wxTreeItemAttr *)m_attrs.
+ Delete((long)tv->itemOld.hItem);
+ }
+ }
+ break;
+
+ case TVN_ENDLABELEDIT:
+ {
+ eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
+ TV_DISPINFO *info = (TV_DISPINFO *)lParam;
+
+ event.m_item = (WXHTREEITEM)info->item.hItem;
+ event.m_label = info->item.pszText;
+ if (info->item.pszText == NULL)
+ return FALSE;
+ break;
+ }
+
+ case TVN_GETDISPINFO:
+ eventType = wxEVT_COMMAND_TREE_GET_INFO;
+ // fall through
+
+ case TVN_SETDISPINFO:
+ {
+ if ( eventType == wxEVT_NULL )
+ eventType = wxEVT_COMMAND_TREE_SET_INFO;
+ //else: get, already set above
+
+ TV_DISPINFO *info = (TV_DISPINFO *)lParam;
+
+ event.m_item = (WXHTREEITEM) info->item.hItem;
+ break;
+ }
+
+ case TVN_ITEMEXPANDING:
+ event.m_code = FALSE;
+ // fall through
+
+ case TVN_ITEMEXPANDED:
+ {
+ NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
+
+ bool expand = FALSE;
+ switch ( tv->action )
+ {
+ case TVE_EXPAND:
+ expand = TRUE;
+ break;
+
+ case TVE_COLLAPSE:
+ expand = FALSE;
+ break;
+
+ default:
+ wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
+ "message"), tv->action);
+ }
+
+ bool ing = ((int)hdr->code == TVN_ITEMEXPANDING);
+ eventType = g_events[expand][ing];
+
+ event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
+ }
+ break;
+
+ case TVN_KEYDOWN:
+ {
+ eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
+ TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
+
+ event.m_code = wxCharCodeMSWToWX(info->wVKey);
+
+ // a separate event for this case
+ if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN )
+ {
+ wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
+ m_windowId);
+ event2.SetEventObject(this);
+
+ GetEventHandler()->ProcessEvent(event2);
+ }
+ }
+ break;
+
+ case TVN_SELCHANGED:
+ eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
+ // fall through
+
+ case TVN_SELCHANGING:
+ {
+ if ( eventType == wxEVT_NULL )
+ eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
+ //else: already set above
+
+ NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
+
+ event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
+ event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
+ }
+ break;
+
+#if defined(_WIN32_IE) && _WIN32_IE >= 0x300
+ case NM_CUSTOMDRAW:
+ {
+ LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
+ NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
+ switch( nmcd.dwDrawStage )
+ {
+ case CDDS_PREPAINT:
+ // if we've got any items with non standard attributes,
+ // notify us before painting each item
+ *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
+ : CDRF_DODEFAULT;
+ return TRUE;
+
+ case CDDS_ITEMPREPAINT:
+ {
+ wxTreeItemAttr *attr =
+ (wxTreeItemAttr *)m_attrs.Get(nmcd.dwItemSpec);
+
+ if ( !attr )
+ {
+ // nothing to do for this item
+ return CDRF_DODEFAULT;
+ }
+
+ HFONT hFont;
+ wxColour colText, colBack;
+ if ( attr->HasFont() )
+ {
+ wxFont font = attr->GetFont();
+ hFont = (HFONT)font.GetResourceHandle();
+ }
+ else
+ {
+ hFont = 0;
+ }
+
+ if ( attr->HasTextColour() )
+ {
+ colText = attr->GetTextColour();
+ }
+ else
+ {
+ colText = GetForegroundColour();
+ }
+
+ // selection colours should override ours
+ if ( nmcd.uItemState & CDIS_SELECTED )
+ {
+ DWORD clrBk = ::GetSysColor(COLOR_HIGHLIGHT);
+ lptvcd->clrTextBk = clrBk;
+
+ // try to make the text visible
+ lptvcd->clrText = wxColourToRGB(colText);
+ lptvcd->clrText |= ~clrBk;
+ lptvcd->clrText &= 0x00ffffff;
+ }
+ else
+ {
+ if ( attr->HasBackgroundColour() )
+ {
+ colBack = attr->GetBackgroundColour();
+ }
+ else
+ {
+ colBack = GetBackgroundColour();
+ }
+
+ lptvcd->clrText = wxColourToRGB(colText);
+ lptvcd->clrTextBk = wxColourToRGB(colBack);
+ }
+
+ // note that if we wanted to set colours for
+ // individual columns (subitems), we would have
+ // returned CDRF_NOTIFYSUBITEMREDRAW from here
+ if ( hFont )
+ {
+ ::SelectObject(nmcd.hdc, hFont);
+
+ *result = CDRF_NEWFONT;
+ }
+ else
+ {
+ *result = CDRF_DODEFAULT;
+ }
+
+ return TRUE;
+ }
+
+ default:
+ *result = CDRF_DODEFAULT;
+ return TRUE;
+ }
+ }
+ break;
+#endif // _WIN32_IE >= 0x300
+
+ default:
+ return wxControl::MSWOnNotify(idCtrl, lParam, result);
+ }
+
+ event.SetEventObject(this);
+ event.SetEventType(eventType);
+
+ bool processed = GetEventHandler()->ProcessEvent(event);
+
+ // post processing
+ switch ( hdr->code )
+ {
+ case TVN_BEGINDRAG:
+ case TVN_BEGINRDRAG:
+ if ( event.IsAllowed() )
+ {
+ // normally this is impossible because the m_dragImage is
+ // deleted once the drag operation is over
+ wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
+
+ m_dragImage = new wxDragImage(*this, event.m_item);
+ m_dragImage->BeginDrag(wxPoint(0, 0), this);
+ m_dragImage->Show(this);
+ }
+ break;
+
+ case TVN_DELETEITEM:
+ {
+ // NB: we might process this message using wxWindows event
+ // tables, but due to overhead of wxWin event system we
+ // prefer to do it here ourself (otherwise deleting a tree
+ // with many items is just too slow)
+ NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
+
+ wxTreeItemId item = event.m_item;
+ if ( HasIndirectData(item) )
+ {
+ wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
+ tv->itemOld.lParam;
+ delete data; // can't be NULL here
+
+ m_itemsWithIndirectData.Remove(item);
+ }
+ else
+ {
+ wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
+ delete data; // may be NULL, ok
+ }
+
+ processed = TRUE; // Make sure we don't get called twice
+ }
+ break;
+
+ case TVN_BEGINLABELEDIT:
+ // return TRUE to cancel label editing
+ *result = !event.IsAllowed();
+ break;
+
+ case TVN_ENDLABELEDIT:
+ // return TRUE to set the label to the new string
+ *result = event.IsAllowed();
+
+ // ensure that we don't have the text ctrl which is going to be
+ // deleted any more
+ DeleteTextCtrl();
+ break;
+
+ case TVN_SELCHANGING:
+ case TVN_ITEMEXPANDING:
+ // return TRUE to prevent the action from happening
+ *result = !event.IsAllowed();
+ break;
+
+ case TVN_GETDISPINFO:
+ // NB: so far the user can't set the image himself anyhow, so do it
+ // anyway - but this may change later
+ if ( /* !processed && */ 1 )
+ {
+ wxTreeItemId item = event.m_item;
+ TV_DISPINFO *info = (TV_DISPINFO *)lParam;
+ if ( info->item.mask & TVIF_IMAGE )
+ {
+ info->item.iImage =
+ DoGetItemImageFromData
+ (
+ item,
+ IsExpanded(item) ? wxTreeItemIcon_Expanded
+ : wxTreeItemIcon_Normal
+ );
+ }
+ if ( info->item.mask & TVIF_SELECTEDIMAGE )
+ {
+ info->item.iSelectedImage =
+ DoGetItemImageFromData
+ (
+ item,
+ IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
+ : wxTreeItemIcon_Selected
+ );
+ }
+ }
+ break;
+
+ //default:
+ // for the other messages the return value is ignored and there is
+ // nothing special to do
+ }
+
+ return processed;
+}
+
+// ----------------------------------------------------------------------------