]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/treectrl.cpp
WinCE fix
[wxWidgets.git] / src / msw / treectrl.cpp
index b7329c5289178ace09ea60d6e6d3b0dc8790acda..10d4f48857a416c337f742110e6cab6f24fae78e 100644 (file)
@@ -659,7 +659,7 @@ bool wxTreeCtrl::Create(wxWindow *parent,
 
     if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT )
     {
-        if ( wxTheApp->GetComCtl32Version() >= 471 )
+        if ( wxApp::GetComCtl32Version() >= 471 )
             wstyle |= TVS_FULLROWSELECT;
     }
 
@@ -716,8 +716,8 @@ bool wxTreeCtrl::Create(wxWindow *parent,
         HDC hdcMem = CreateCompatibleDC(NULL);
 
         // create a mono bitmap of the standard size
-        int x = GetSystemMetrics(SM_CXMENUCHECK);
-        int y = GetSystemMetrics(SM_CYMENUCHECK);
+        int x = ::GetSystemMetrics(SM_CXMENUCHECK);
+        int y = ::GetSystemMetrics(SM_CYMENUCHECK);
         wxImageList imagelistCheckboxes(x, y, false, 2);
         HBITMAP hbmpCheck = CreateBitmap(x, y,   // bitmap size
                                          1,      // # of color planes
@@ -926,7 +926,7 @@ bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
 
 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
 {
-    wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
+    wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") );
 
     wxChar buf[512];  // the size is arbitrary...
 
@@ -1719,6 +1719,26 @@ wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
                         image, selImage, NULL);
 }
 
+wxImageList *wxTreeCtrl::GetImageList(int) const
+{
+    return GetImageList();
+}
+
+void wxTreeCtrl::SetImageList(wxImageList *imageList, int)
+{
+    SetImageList(imageList);
+}
+
+int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
+{
+    return GetItemImage(item, wxTreeItemIcon_Selected);
+}
+
+void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
+{
+    SetItemImage(item, image, wxTreeItemIcon_Selected);
+}
+
 #endif // WXWIN_COMPATIBILITY_2_4
 
 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
@@ -1900,10 +1920,12 @@ void wxTreeCtrl::Toggle(const wxTreeItemId& item)
 }
 
 #if WXWIN_COMPATIBILITY_2_4
+
 void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
 {
     DoExpand(item, action);
 }
+
 #endif
 
 void wxTreeCtrl::Unselect()
@@ -1929,6 +1951,8 @@ void wxTreeCtrl::UnselectAll()
             ::UnselectItem(GetHwnd(), HITEM_PTR(selections[n]));
 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
         }
+
+        m_htSelStart.Unset();
     }
     else
     {
@@ -2053,7 +2077,7 @@ wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
 }
 
 // End label editing, optionally cancelling the edit
-void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool discardChanges)
+void wxTreeCtrl::DoEndEditLabel(bool discardChanges)
 {
     TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
 
@@ -2217,9 +2241,21 @@ WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
 {
     bool processed = false;
     WXLRESULT rc = 0;
-    bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0;
+    bool isMultiple = HasFlag(wxTR_MULTIPLE);
 
-    if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
+    if ( nMsg == WM_CONTEXTMENU )
+    {
+        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 );
+
+        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
@@ -2392,9 +2428,9 @@ WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
             // 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) )
+                                      &rect, TRUE) )
             {
-                ::InvalidateRect(GetHwnd(), &rect, false);
+                ::InvalidateRect(GetHwnd(), &rect, FALSE);
             }
         }
     }
@@ -2403,84 +2439,145 @@ WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
         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());
-        if ( !m_htSelStart )
-        {
-            m_htSelStart = htSel;
-        }
-
-        if ( wParam == VK_SPACE )
+        switch ( wParam )
         {
-            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();
+
+                    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;
             }
         }
     }
-#endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
-    else if ( nMsg == WM_CHAR )
+
+    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 )
     {
-        // don't let the control process Space and Return keys because it
+        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 )
-        {
-            processed = true;
-        }
+            return 0;
     }
 
-    if ( !processed )
-        rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
-
-    return rc;
+    return wxControl::MSWDefWindowProc(nMsg, wParam, lParam);
 }
 
 // process WM_NOTIFY Windows message
@@ -2519,6 +2616,10 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
                 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
 
+                // although the user event handler may still veto it, it is
+                // important to set it now so that calls to SetItemText() from
+                // the event handler would change the text controls contents
+                m_idEdited =
                 event.m_item = info->item.hItem;
                 event.m_label = info->item.pszText;
                 event.m_editCancelled = false;
@@ -2851,7 +2952,7 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 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->BeginDrag(wxPoint(0,0), this);
                 m_dragImage->Show();
             }
             break;
@@ -2884,8 +2985,9 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
         case TVN_BEGINLABELEDIT:
             // return true to cancel label editing
             *result = !event.IsAllowed();
+
             // set ES_WANTRETURN ( like we do in BeginLabelEdit )
-            if(event.IsAllowed())
+            if ( event.IsAllowed() )
             {
                 HWND hText = TreeView_GetEditControl(GetHwnd());
                 if(hText != NULL)
@@ -2910,6 +3012,10 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                                                | wxTE_PROCESS_ENTER);
                 }
             }
+            else // we had set m_idEdited before
+            {
+                m_idEdited.Unset();
+            }
             break;
 
         case TVN_ENDLABELEDIT:
@@ -3039,5 +3145,14 @@ int wxTreeCtrl::GetState(const wxTreeItemId& node)
     return STATEIMAGEMASKTOINDEX(tvi.state);
 }
 
+#if WXWIN_COMPATIBILITY_2_2
+
+wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
+{
+    return GetItemParent( item );
+}
+
+#endif  // WXWIN_COMPATIBILITY_2_2
+
 #endif // wxUSE_TREECTRL