]> git.saurik.com Git - wxWidgets.git/commitdiff
No changes, just add wxGtkList to ensure g_list_free() is always called.
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 22 Aug 2011 12:41:00 +0000 (12:41 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 22 Aug 2011 12:41:00 +0000 (12:41 +0000)
Add an extremely simple RAII wrapper around GList and use it.

Also add wxGtkTreePathList which also automatically frees its contents to
simplify working with the lists of GtkTreePaths.

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

include/wx/gtk/private/list.h [new file with mode: 0644]
src/gtk/button.cpp
src/gtk/dataview.cpp

diff --git a/include/wx/gtk/private/list.h b/include/wx/gtk/private/list.h
new file mode 100644 (file)
index 0000000..b4e50ab
--- /dev/null
@@ -0,0 +1,33 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        wx/gtk/private/list.h
+// Purpose:     wxGtkList class.
+// Author:      Vadim Zeitlin
+// Created:     2011-08-21
+// RCS-ID:      $Id$
+// Copyright:   (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
+// Licence:     wxWindows licence
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef _WX_GTK_PRIVATE_LIST_H_
+#define _WX_GTK_PRIVATE_LIST_H_
+
+// ----------------------------------------------------------------------------
+// Convenience class for calling g_list_free() automatically
+// ----------------------------------------------------------------------------
+
+class wxGtkList
+{
+public:
+    explicit wxGtkList(GList* list) : m_list(list) { }
+    ~wxGtkList() { g_list_free(m_list); }
+
+    operator GList *() const { return m_list; }
+    GList * operator->() const { return m_list; }
+
+protected:
+    GList* const m_list;
+
+    wxDECLARE_NO_COPY_CLASS(wxGtkList);
+};
+
+#endif // _WX_GTK_PRIVATE_LIST_H_
index 28379ea99881fc20be1a0ab18a646c4dd092c82e..cf3fb8b26aea5aaff6be2c5278e78b97a5e25134 100644 (file)
@@ -19,6 +19,7 @@
 #include "wx/stockitem.h"
 
 #include "wx/gtk/private.h"
+#include "wx/gtk/private/list.h"
 
 // ----------------------------------------------------------------------------
 // GTK callbacks
@@ -252,13 +253,12 @@ GtkLabel *wxButton::GTKGetLabel() const
     {
         GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
         GtkLabel* label = NULL;
-        GList* list = gtk_container_get_children(GTK_CONTAINER(box));
+        wxGtkList list(gtk_container_get_children(GTK_CONTAINER(box)));
         for (GList* item = list; item; item = item->next)
         {
             if (GTK_IS_LABEL(item->data))
                 label = GTK_LABEL(item->data);
         }
-        g_list_free(list);
 
         return label;
     }
@@ -280,12 +280,11 @@ void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
         GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
         if ( GTK_IS_BOX(box) )
         {
-            GList* list = gtk_container_get_children(GTK_CONTAINER(box));
+            wxGtkList list(gtk_container_get_children(GTK_CONTAINER(box)));
             for (GList* item = list; item; item = item->next)
             {
                 gtk_widget_modify_style(GTK_WIDGET(item->data), style);
             }
-            g_list_free(list);
         }
     }
 }
index ee2ea2b68683c56f7f3e65a1e88221a08fe3ced6..b664d402d58257bab1283a061a0e987901c94980 100644 (file)
@@ -34,6 +34,7 @@
 #include "wx/gtk/dcclient.h"
 
 #include "wx/gtk/private/gdkconv.h"
+#include "wx/gtk/private/list.h"
 using namespace wxGTKImpl;
 
 class wxGtkDataViewModelNotifier;
@@ -98,6 +99,26 @@ private:
     wxDECLARE_NO_COPY_CLASS(wxGtkTreePath);
 };
 
+// ----------------------------------------------------------------------------
+// wxGtkTreePathList: self-destroying list of GtkTreePath objects.
+// ----------------------------------------------------------------------------
+
+class wxGtkTreePathList : public wxGtkList
+{
+public:
+    // Ctor takes ownership of the list.
+    explicit wxGtkTreePathList(GList* list)
+        : wxGtkList(list)
+    {
+    }
+
+    ~wxGtkTreePathList()
+    {
+        // Delete the list contents, wxGtkList will delete the list itself.
+        g_list_foreach(m_list, (GFunc)gtk_tree_path_free, NULL);
+    }
+};
+
 // ----------------------------------------------------------------------------
 // wxGtkTreeSelectionLock: prevent selection from changing during the
 //                                 lifetime of this object
@@ -4811,13 +4832,9 @@ int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
 {
     GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
 
-    GList *list = gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview) );
-
-    gint pos = g_list_index( list, (gconstpointer)  gtk_column );
+    wxGtkList list(gtk_tree_view_get_columns(GTK_TREE_VIEW(m_treeview)));
 
-    g_list_free( list );
-
-    return pos;
+    return g_list_index( list, (gconstpointer)  gtk_column );
 }
 
 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
@@ -4956,23 +4973,17 @@ int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
     if (HasFlag(wxDV_MULTIPLE))
     {
         GtkTreeModel *model;
-        GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
+        wxGtkTreePathList list(gtk_tree_selection_get_selected_rows(selection, &model));
 
         int count = 0;
-        while (list)
+        for ( GList* current = list; current; current = g_list_next(current) )
         {
             GtkTreePath *path = (GtkTreePath*) list->data;
 
             sel.Add(GTKPathToItem(path));
-
-            list = g_list_next( list );
             count++;
         }
 
-        // delete list
-        g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
-        g_list_free( list );
-
         return count;
     }
     else