]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/treectlg.cpp
We need to update the scrollbar even if it's not shown, otherwise it can get stuck...
[wxWidgets.git] / src / generic / treectlg.cpp
index 04c73b4e20829e76a332ad233105b734182bea5b..89df6f6b1954038a6c6f6243cbcb9bb978c1ef38 100644 (file)
@@ -88,25 +88,18 @@ class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
 public:
     wxTreeTextCtrl(wxGenericTreeCtrl *owner, wxGenericTreeItem *item);
 
-    // wxGenericTreeCtrl can use this one to abandon editing the given item,
-    // it's not an error to call it if this item is not being edited
-    void StopEditing(wxGenericTreeItem *item)
+    void StopEditing()
     {
-        if ( item == m_itemEdited )
-            DoStopEditing();
+        Finish();
+        m_owner->OnRenameCancelled(m_itemEdited);
     }
+    const wxGenericTreeItem* item() const { return m_itemEdited; }
 
 protected:
     void OnChar( wxKeyEvent &event );
     void OnKeyUp( wxKeyEvent &event );
     void OnKillFocus( wxFocusEvent &event );
 
-    void DoStopEditing()
-    {
-        Finish();
-        m_owner->OnRenameCancelled(m_itemEdited);
-    }
-
     bool AcceptChanges();
     void Finish();
 
@@ -302,7 +295,7 @@ static void EventFlagsToSelType(long style,
 }
 
 // check if the given item is under another one
-static bool IsDescendantOf(wxGenericTreeItem *parent, wxGenericTreeItem *item)
+static bool IsDescendantOf(const wxGenericTreeItem *parent, const wxGenericTreeItem *item)
 {
     while ( item )
     {
@@ -422,7 +415,7 @@ void wxTreeTextCtrl::Finish()
 
         m_finished = true;
 
-        m_owner->SetFocus(); // This doesn't work. TODO.
+        m_owner->SetFocusIgnoringChildren();
     }
 }
 
@@ -440,7 +433,7 @@ void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
             break;
 
         case WXK_ESCAPE:
-            DoStopEditing();
+            StopEditing();
             break;
 
         default:
@@ -536,6 +529,8 @@ void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
             tree->SendDeleteEvent(child);
 
         child->DeleteChildren(tree);
+        if (child == tree->m_select_me)
+            tree->m_select_me = NULL;
         delete child;
     }
 
@@ -763,9 +758,11 @@ void wxGenericTreeCtrl::Init()
 
     m_findTimer = NULL;
 
+    m_dropEffectAboveItem = false;
+
     m_lastOnSame = false;
 
-#ifdef __WXMAC_CARBON__ 
+#ifdef __WXMAC_CARBON__
     m_normalFont.MacCreateThemeFont( kThemeViewsFont ) ;
 #else
     m_normalFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
@@ -1503,11 +1500,31 @@ void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
     ProcessEvent( event );
 }
 
+// Don't leave edit or selection on a child which is about to disappear
+void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem* item)
+{
+    if (m_textCtrl != NULL && item != m_textCtrl->item() && IsDescendantOf(item, m_textCtrl->item())) {
+        m_textCtrl->StopEditing();
+    }
+    if (item != m_key_current && IsDescendantOf(item, m_key_current)) {
+        m_key_current = NULL;
+    }
+    if (IsDescendantOf(item, m_select_me)) {
+        m_select_me = item;
+    }
+    if (item != m_current && IsDescendantOf(item, m_current)) {
+        m_current->SetHilight( false );
+        m_current = NULL;
+        m_select_me = item;
+    }
+}
+
 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
 {
     m_dirty = true;     // do this first so stuff below doesn't cause flicker
 
     wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
+    ChildrenClosing(item);
     item->DeleteChildren(this);
 }
 
@@ -1517,10 +1534,10 @@ void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
 
     wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
 
-    if ( m_textCtrl )
+    if (m_textCtrl != NULL && IsDescendantOf(item, m_textCtrl->item()))
     {
         // can't delete the item being edited, cancel editing it first
-        m_textCtrl->StopEditing(item);
+        m_textCtrl->StopEditing();
     }
 
     wxGenericTreeItem *parent = item->GetParent();
@@ -1568,6 +1585,10 @@ void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
     // and delete all of its children and the item itself now
     item->DeleteChildren(this);
     SendDeleteEvent(item);
+
+    if (item == m_select_me)
+        m_select_me = NULL;
+
     delete item;
 }
 
@@ -1650,6 +1671,7 @@ void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
         return;
     }
 
+    ChildrenClosing(item);
     item->Collapse();
 
 #if 0  // TODO why should items be collapsed recursively?
@@ -2479,7 +2501,7 @@ void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
         {
             // draw a line under the drop target because the item will be
             // dropped there
-            DrawLine(item, true /* below */);
+            DrawLine(item, !m_dropEffectAboveItem );
         }
 
         SetCursor(wxCURSOR_BULLSEYE);
@@ -2633,6 +2655,14 @@ void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
             }
             break;
 
+        case WXK_MENU:
+            {
+                wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_MENU, GetId() );
+                event.m_item = m_current;
+                event.SetEventObject( this );
+                GetEventHandler()->ProcessEvent( event );
+                break;
+            }
         case ' ':
         case WXK_RETURN:
             if ( !event.HasModifiers() )
@@ -3074,7 +3104,7 @@ void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
             CaptureMouse();
         }
     }
-    else if ( event.Moving() )
+    else if ( event.Dragging() )
     {
         if ( item != m_dropTarget )
         {
@@ -3086,7 +3116,7 @@ void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
             // highlight the current drop target if any
             DrawDropEffect(m_dropTarget);
 
-#if defined( __WXMSW__ ) || defined(__WXMAC__)
+#if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
             Update();
 #else
             wxYieldIfNeeded();
@@ -3147,11 +3177,12 @@ void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
 
         if ( event.RightDown() )
         {
+            DoSelectItem(item, true, false);
             wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
             nevent.m_item = item;
             nevent.m_pointDrag = CalcScrolledPosition(pt);
             nevent.SetEventObject(this);
-            GetEventHandler()->ProcessEvent(nevent);
+            event.Skip(!GetEventHandler()->ProcessEvent(nevent));
         }
         else if ( event.LeftUp() )
         {
@@ -3533,4 +3564,27 @@ wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
 #endif
 }
 
+#if WXWIN_COMPATIBILITY_2_4
+
+int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
+{
+    return GetItemImage(item, wxTreeItemIcon_Selected);
+}
+
+void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
+{
+    SetItemImage(item, image, wxTreeItemIcon_Selected);
+}
+
+#endif // WXWIN_COMPATIBILITY_2_4
+
+#if WXWIN_COMPATIBILITY_2_2
+
+wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const
+{
+    return GetItemParent( item );
+}
+
+#endif  // WXWIN_COMPATIBILITY_2_2
+
 #endif // wxUSE_TREECTRL