]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/treectrl.cpp
Some change logs
[wxWidgets.git] / src / msw / treectrl.cpp
index 8cc634935eedc1d129c7556adf02e9fa53dffff1..2df7fb4f75b6677a6d13abea9bcf6b2a99d25137 100644 (file)
 
 #include "wx/msw/private.h"
 
 
 #include "wx/msw/private.h"
 
+// include <commctrl.h> "properly"
+#include "wx/msw/wrapcctl.h"
+
+#include "wx/msw/missing.h"
+
 // Set this to 1 to be _absolutely_ sure that repainting will work for all
 // comctl32.dll versions
 #define wxUSE_COMCTL32_SAFELY 0
 // Set this to 1 to be _absolutely_ sure that repainting will work for all
 // comctl32.dll versions
 #define wxUSE_COMCTL32_SAFELY 0
@@ -44,9 +49,6 @@
 #include "wx/msw/treectrl.h"
 #include "wx/msw/dragimag.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
 // --------------------------------
 
 // macros to hide the cast ugliness
 // --------------------------------
 
@@ -346,7 +348,8 @@ public:
         {
             m_selections.Empty();
 
         {
             m_selections.Empty();
 
-            DoTraverse(tree->GetRootItem());
+            if (tree->GetCount() > 0)
+                DoTraverse(tree->GetRootItem());
         }
 
     virtual bool OnVisit(const wxTreeItemId& item)
         }
 
     virtual bool OnVisit(const wxTreeItemId& item)
@@ -677,7 +680,7 @@ bool wxTreeCtrl::Create(wxWindow *parent,
         wstyle |= TVS_CHECKBOXES;
 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
 
         wstyle |= TVS_CHECKBOXES;
 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
 
-#ifndef __WXWINCE__
+#if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
     // Need so that TVN_GETINFOTIP messages will be sent
     wstyle |= TVS_INFOTIP;
 #endif
     // Need so that TVN_GETINFOTIP messages will be sent
     wstyle |= TVS_INFOTIP;
 #endif
@@ -1951,6 +1954,8 @@ void wxTreeCtrl::UnselectAll()
             ::UnselectItem(GetHwnd(), HITEM_PTR(selections[n]));
 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
         }
             ::UnselectItem(GetHwnd(), HITEM_PTR(selections[n]));
 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
         }
+
+        m_htSelStart.Unset();
     }
     else
     {
     }
     else
     {
@@ -2241,17 +2246,42 @@ WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
     WXLRESULT rc = 0;
     bool isMultiple = HasFlag(wxTR_MULTIPLE);
 
     WXLRESULT rc = 0;
     bool isMultiple = HasFlag(wxTR_MULTIPLE);
 
-#ifdef WM_CONTEXTMENU
+    // This message is sent after a right-click, or when the "menu" key is pressed
     if ( nMsg == WM_CONTEXTMENU )
     {
     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() );
         wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_MENU, GetId() );
-        event.m_item = GetSelection();
+
+        // can't use GetSelection() here as it would assert in multiselect mode
+        event.m_item = wxTreeItemId(TreeView_GetSelection(GetHwnd()));
         event.SetEventObject( this );
         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
     }
         if ( GetEventHandler()->ProcessEvent(event) )
             processed = true;
         //else: continue with generating wxEVT_CONTEXT_MENU in base class code
     }
-#endif // WM_CONTEXTMENU
     else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
     {
         // we only process mouse messages here and these parameters have the
     else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
     {
         // we only process mouse messages here and these parameters have the
@@ -2275,13 +2305,6 @@ WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
                 // newly selected item
                 ::SelectItem(GetHwnd(), htItem);
                 ::SetFocus(GetHwnd(), htItem);
                 // newly selected item
                 ::SelectItem(GetHwnd(), htItem);
                 ::SetFocus(GetHwnd(), htItem);
-
-                // default WM_RBUTTONUP handler enters modal loop inside
-                // DefWindowProc() waiting for WM_RBUTTONDOWN 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
-                processed = true;
                 break;
 
 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
                 break;
 
 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
@@ -2443,79 +2466,82 @@ WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
         bool bCtrl = wxIsCtrlDown(),
              bShift = wxIsShiftDown();
 
         bool bCtrl = wxIsCtrlDown(),
              bShift = wxIsShiftDown();
 
-        // we handle.arrows and space, but not page up/down and home/end: the
-        // latter should be easy, but not the former
-
         HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
         HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
-        if ( !m_htSelStart )
+        switch ( wParam )
         {
         {
-            m_htSelStart = htSel;
-        }
-
-        if ( wParam == VK_SPACE )
-        {
-            if ( bCtrl )
-            {
-                ::ToggleItemSelection(GetHwnd(), htSel);
-            }
-            else
-            {
-                UnselectAll();
-
-                ::SelectItem(GetHwnd(), htSel);
-            }
+            case VK_SPACE:
+                if ( bCtrl )
+                {
+                    ::ToggleItemSelection(GetHwnd(), htSel);
+                }
+                else
+                {
+                    UnselectAll();
 
 
-            processed = true;
-        }
-        else if ( wParam == VK_UP || wParam == 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);
+                    ::SelectItem(GetHwnd(), htSel);
+                }
 
 
-                HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP
-                                    ? TreeView_GetPrevVisible(GetHwnd(), htSel)
-                                    : TreeView_GetNextVisible(GetHwnd(), htSel));
+                processed = true;
+                break;
 
 
-                if ( !htNext )
+            case VK_UP:
+            case VK_DOWN:
+                if ( !bCtrl && !bShift )
                 {
                 {
-                    // at the top/bottom
-                    htNext = htSel;
+                    // no modifiers, just clear selection and then let the default
+                    // processing to take place
+                    UnselectAll();
                 }
                 }
-
-                if ( bShift )
+                else if ( htSel )
                 {
                 {
-                    SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
+                    (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;
                 }
                 }
-                else // bCtrl
+                break;
+
+            case VK_HOME:
+            case VK_END:
+            case VK_PRIOR:
+            case VK_NEXT:
+                // TODO: handle Shift/Ctrl with these keys
+                if ( !bCtrl && !bShift )
                 {
                 {
-                    // without changing selection
-                    ::SetFocus(GetHwnd(), htNext);
-                }
+                    UnselectAll();
 
 
-                processed = true;
-            }
+                    m_htSelStart.Unset();
+                }
         }
     }
 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
         }
     }
 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
-    else if ( nMsg == WM_CHAR )
-    {
-        // 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 )
-        {
-            processed = true;
-        }
-    }
     else if ( nMsg == WM_COMMAND )
     {
         // if we receive a EN_KILLFOCUS command from the in-place edit control
     else if ( nMsg == WM_COMMAND )
     {
         // if we receive a EN_KILLFOCUS command from the in-place edit control
@@ -2541,6 +2567,46 @@ WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
     return rc;
 }
 
     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);
+}
+
 // process WM_NOTIFY Windows message
 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
 {
 // process WM_NOTIFY Windows message
 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
 {
@@ -2629,6 +2695,7 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 break;
             }
 
                 break;
             }
 
+#ifdef TVN_GETINFOTIP
         case TVN_GETINFOTIP:
             {
                 eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP;
         case TVN_GETINFOTIP:
             {
                 eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP;
@@ -2639,6 +2706,7 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
 
                 break;
             }
 
                 break;
             }
+#endif
 #endif
 
         case TVN_GETDISPINFO:
 #endif
 
         case TVN_GETDISPINFO:
@@ -2993,6 +3061,7 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
             break;
 
 #ifndef __WXWINCE__
             break;
 
 #ifndef __WXWINCE__
+#ifdef TVN_GETINFOTIP
          case TVN_GETINFOTIP:
             {
                 // If the user permitted a tooltip change, change it
          case TVN_GETINFOTIP:
             {
                 // If the user permitted a tooltip change, change it
@@ -3002,6 +3071,7 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 }
             }
             break;
                 }
             }
             break;
+#endif
 #endif
 
         case TVN_SELCHANGING:
 #endif
 
         case TVN_SELCHANGING: