From dd23c25cde86903f2a4115e4a24e891e80d477e4 Mon Sep 17 00:00:00 2001
From: Julian Smart <julian@anthemion.co.uk>
Date: Thu, 5 Sep 2002 19:50:41 +0000
Subject: [PATCH] Applied patch [ 605189 ] add edit cancel notify to
 wxTreeEvent

Benjamin I. Williams

Currently there is no way of receiving notification
that a user cancelled an edit operation on a wxTreeCtrl
tree node label.

This patch adds a method "IsEditCancelled" to the
wxTreeEvent class. During an EVT_TREE_END_LABEL_EDIT
event, the programmer can now determine whether or not
the edit operation was cancelled by the user (by
pressing <ESC>).

This patch provides this implementation for both
wxMSW's wxTreeCtrl and the wxGenericTreeCtrl. Both
situations have been tested and work well.

The patch is not very invasive, and is much smaller
than I had expected.

Please see the relevent discussion on the wx-dev list
for more details.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16995 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/changes.txt              |  2 ++
 docs/latex/wx/treeevt.tex     |  7 +++++++
 include/wx/generic/treectlg.h |  1 +
 include/wx/treebase.h         |  4 ++++
 src/common/treebase.cpp       |  1 +
 src/generic/treectlg.cpp      | 17 +++++++++++++++++
 src/msw/treectrl.cpp          |  9 ++++++++-
 7 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/docs/changes.txt b/docs/changes.txt
index 30398ec024..04d174c991 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -240,6 +240,8 @@ All (GUI):
 - generic wxListCtrl renamed to wxGenericListCtrl, wxImageList
   renamed to wxGenericImageList, so they can be used on wxMSW
   (Rene Rivera).
+- Added wxTreeEvent::IsEditCancelled so the application can tell
+  whether a label edit was cancelled.
 
 wxMSW:
 
diff --git a/docs/latex/wx/treeevt.tex b/docs/latex/wx/treeevt.tex
index 45ddeccb8b..3889143b0f 100644
--- a/docs/latex/wx/treeevt.tex
+++ b/docs/latex/wx/treeevt.tex
@@ -91,3 +91,10 @@ Returns the old item index (valid for EVT\_TREE\_ITEM\_CHANGING and CHANGED even
 
 Returns the position of the mouse pointer if the event is a drag event.
 
+\membersection{wxTreeEvent::IsEditCancelled()}
+
+\constfunc{bool}{IsEditCancelled}{}
+
+Returns TRUE if the label edit was cancelled. This should be
+called from within an EVT\_TREE\_END\_LABEL\_EDIT handler.
+
diff --git a/include/wx/generic/treectlg.h b/include/wx/generic/treectlg.h
index b5e4c2f549..8d88362e3a 100644
--- a/include/wx/generic/treectlg.h
+++ b/include/wx/generic/treectlg.h
@@ -429,6 +429,7 @@ protected:
 
     void OnRenameTimer();
     bool OnRenameAccept(wxGenericTreeItem *item, const wxString& value);
+    void OnRenameCancelled(wxGenericTreeItem *item);
 
     void FillArray(wxGenericTreeItem*, wxArrayTreeItemIds&) const;
     void SelectItemRange( wxGenericTreeItem *item1, wxGenericTreeItem *item2 );
diff --git a/include/wx/treebase.h b/include/wx/treebase.h
index 1cce85d260..4f1af7fb65 100644
--- a/include/wx/treebase.h
+++ b/include/wx/treebase.h
@@ -239,6 +239,9 @@ public:
         // label (for EVT_TREE_{BEGIN|END}_LABEL_EDIT only)
     const wxString& GetLabel() const { return m_label; }
 
+        // edit cancel flag (for EVT_TREE_{BEGIN|END}_LABEL_EDIT only)
+    bool IsEditCancelled() const { return m_editCancelled; }
+
 #if WXWIN_COMPATIBILITY_2_2
     // for compatibility only, don't use
     int GetCode() const { return m_evtKey.GetKeyCode(); }
@@ -251,6 +254,7 @@ private:
                   m_itemOld;
     wxPoint       m_pointDrag;
     wxString      m_label;
+    bool          m_editCancelled;
 
     friend class WXDLLEXPORT wxTreeCtrl;
     friend class WXDLLEXPORT wxGenericTreeCtrl;
diff --git a/src/common/treebase.cpp b/src/common/treebase.cpp
index 7c83963ef9..6bf725823f 100644
--- a/src/common/treebase.cpp
+++ b/src/common/treebase.cpp
@@ -74,6 +74,7 @@ wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
            : wxNotifyEvent(commandType, id)
 {
     m_itemOld = 0l;
+    m_editCancelled = FALSE;
 }
 
 #endif // wxUSE_TREECTRL
diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp
index 8c71e9962d..3fb31eb750 100644
--- a/src/generic/treectlg.cpp
+++ b/src/generic/treectlg.cpp
@@ -449,6 +449,7 @@ void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
 
         case WXK_ESCAPE:
             Finish();
+            m_owner->OnRenameCancelled(m_itemEdited);
             break;
 
         default:
@@ -2792,10 +2793,26 @@ bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item,
     le.m_item = (long) item;
     le.SetEventObject( this );
     le.m_label = value;
+    le.m_editCancelled = FALSE;
 
     return !GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
 }
 
+void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem *item)
+{
+    // let owner know that the edit was cancelled
+    wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
+    le.m_item = (long) item;
+    le.SetEventObject( this );
+    le.m_label = wxEmptyString;
+    le.m_editCancelled = FALSE;
+
+    GetEventHandler()->ProcessEvent( le );
+}
+
+
+
+
 void wxGenericTreeCtrl::OnRenameTimer()
 {
     Edit( m_current );
diff --git a/src/msw/treectrl.cpp b/src/msw/treectrl.cpp
index 187d568c38..97348c58ff 100644
--- a/src/msw/treectrl.cpp
+++ b/src/msw/treectrl.cpp
@@ -2228,6 +2228,7 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
 
                 event.m_item = (WXHTREEITEM) info->item.hItem;
                 event.m_label = info->item.pszText;
+                event.m_editCancelled = FALSE;
             }
             break;
 
@@ -2254,7 +2255,13 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 event.m_item = (WXHTREEITEM)info->item.hItem;
                 event.m_label = info->item.pszText;
                 if (info->item.pszText == NULL)
-                    return FALSE;
+                {
+                    event.m_editCancelled = TRUE;
+                }
+                 else
+                {
+                    event.m_editCancelled = FALSE;
+                }
                 break;
             }
 
-- 
2.50.0

