]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxItemContainer::DetachClientObject() and use it in wxRearrangeList.
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 22 Jul 2010 12:09:15 +0000 (12:09 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 22 Jul 2010 12:09:15 +0000 (12:09 +0000)
Add a method to detach the item from an item control without deleting it and
use it in wxRearrangeList to correctly swap object client data without
deleting the pointers in the process.

Closes #12201.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65040 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/ctrlsub.h
interface/wx/ctrlsub.h
src/common/ctrlsub.cpp
src/common/rearrangectrl.cpp

index dba63cca7b7a3caea416f9efd93f257d786846b9..82a98dfdfb835f9b1d39cc0a768aa9b8fca3c512 100644 (file)
@@ -296,8 +296,13 @@ public:
     void SetClientData(unsigned int n, void* clientData);
     void* GetClientData(unsigned int n) const;
 
+    // SetClientObject() takes ownership of the pointer, GetClientObject()
+    // returns it but keeps the ownership while DetachClientObject() expects
+    // the caller to delete the pointer and also resets the internally stored
+    // one to NULL for this item
     void SetClientObject(unsigned int n, wxClientData* clientData);
     wxClientData* GetClientObject(unsigned int n) const;
+    wxClientData* DetachClientObject(unsigned int n);
 
     // return the type of client data stored in this control: usually it just
     // returns m_clientDataItemsType but must be overridden in the controls
index f23616e4e02481218e9435d7235855ff81a30574..33652cb4619f2d99695190a83c4e238d8661799d 100644 (file)
@@ -348,20 +348,45 @@ public:
     void Delete(unsigned int n);
 
 
+    /**
+        Returns the client object associated with the given item and transfers
+        its ownership to the caller.
+
+        This method, unlike GetClientObject(), expects the caller to delete the
+        returned pointer. It also replaces the internally stored pointer with
+        @NULL, i.e. completely detaches the client object pointer from the
+        control.
+
+        It's an error to call this method unless HasClientObjectData() returns
+        @true.
+
+        @param n
+            The zero-based item index.
+        @return The associated client object pointer to be deleted by caller or
+            @NULL.
+
+        @since 2.9.2
+     */
+    wxClientData *DetachClientObject(unsigned int n);
+
     /**
        Returns true, if either untyped data (@c void*) or object data (wxClientData*)
        is associated with the items of the control.
     */
     bool HasClientData() const;
-    
+
     /**
-       Returns true, if object data (wxClientData*)
-       is associated with the items of the control.
+       Returns true, if object data is associated with the items of the
+       control.
+
+       Object data pointers have the type @c wxClientData* instead of @c void*
+       and, importantly, are owned by the control, i.e. will be deleted by it,
+       unlike their untyped counterparts.
     */
     bool HasClientObjectData() const;
 
     /**
-       Returns true, if untyped data (@c void*) 
+       Returns true, if untyped data (@c void*)
        is associated with the items of the control.
     */
     bool HasClientUntypedData() const;
@@ -390,6 +415,10 @@ public:
         given item doesn't have any client data associated with it (but other
         items do).
 
+        Notice that the returned pointer is still owned by the control and will
+        be deleted by it, use DetachClientObject() if you want to remove the
+        pointer from the control.
+
         @param n
             The zero-based position of the item.
 
index d0a197da1fe2f92cc62e7a0937822e88f3f9b45d..68d4b1a5916150f05b753abca0a2c6d77307e0f2 100644 (file)
@@ -191,6 +191,18 @@ wxClientData *wxItemContainer::GetClientObject(unsigned int n) const
     return static_cast<wxClientData *>(DoGetItemClientData(n));
 }
 
+wxClientData *wxItemContainer::DetachClientObject(unsigned int n)
+{
+    wxClientData * const data = GetClientObject(n);
+    if ( data )
+    {
+        // reset the pointer as we don't own it any more
+        DoSetItemClientData(n, NULL);
+    }
+
+    return data;
+}
+
 void wxItemContainer::SetClientData(unsigned int n, void *data)
 {
     if ( !HasClientData() )
index 5f6bc2bf0680597fe9fada703f7732de135b881f..6027fd2fb386b96bb4bc1aebc3f50f0526b3b865 100644 (file)
@@ -150,8 +150,8 @@ void wxRearrangeList::Swap(int pos1, int pos2)
 
         case wxClientData_Object:
             {
-                wxClientData * const dataTmp = GetClientObject(pos1);
-                SetClientObject(pos1, GetClientObject(pos2));
+                wxClientData * const dataTmp = DetachClientObject(pos1);
+                SetClientObject(pos1, DetachClientObject(pos2));
                 SetClientObject(pos2, dataTmp);
             }
             break;