From 72af0d4ca1425ae0aa0db0b8e3cb911461dc5ff8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 6 Feb 2016 18:52:56 +0100 Subject: [PATCH] Use wxGtkTreePath for wxListBox and wxCheckListBox in wxGTK too Avoid manual calls to gtk_tree_path_free() and use the RAII wrapper instead. This makes the code shorter and safer. No real changes. --- src/gtk/checklst.cpp | 4 ++-- src/gtk/listbox.cpp | 41 ++++++++++++++--------------------------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/gtk/checklst.cpp b/src/gtk/checklst.cpp index ae4b3f67fa..4105ce2716 100644 --- a/src/gtk/checklst.cpp +++ b/src/gtk/checklst.cpp @@ -16,6 +16,7 @@ #include #include "wx/gtk/private.h" +#include "wx/gtk/private/treeview.h" //----------------------------------------------------------------------------- // "toggled" @@ -27,13 +28,12 @@ static void gtk_checklist_toggled(GtkCellRendererToggle * WXUNUSED(renderer), { wxCHECK_RET( listbox->m_treeview != NULL, wxT("invalid listbox") ); - GtkTreePath* path = gtk_tree_path_new_from_string(stringpath); + wxGtkTreePath path(stringpath); wxCommandEvent new_event( wxEVT_CHECKLISTBOX, listbox->GetId() ); new_event.SetEventObject( listbox ); new_event.SetInt( gtk_tree_path_get_indices(path)[0] ); new_event.SetString( listbox->GetString( new_event.GetInt() )); - gtk_tree_path_free(path); listbox->Check( new_event.GetInt(), !listbox->IsChecked(new_event.GetInt())); listbox->HandleWindowEvent( new_event ); } diff --git a/src/gtk/listbox.cpp b/src/gtk/listbox.cpp index 4d53d7779a..24b3495a97 100644 --- a/src/gtk/listbox.cpp +++ b/src/gtk/listbox.cpp @@ -33,6 +33,7 @@ #include "wx/gtk/private/gtk2-compat.h" #include "wx/gtk/private/object.h" #include "wx/gtk/private/treeentry_gtk.h" +#include "wx/gtk/private/treeview.h" #include #ifdef __WXGTK3__ @@ -525,18 +526,14 @@ bool wxListBox::GTKGetIteratorFor(unsigned pos, GtkTreeIter *iter) const int wxListBox::GTKGetIndexFor(GtkTreeIter& iter) const { - GtkTreePath *path = - gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter); + wxGtkTreePath path( + gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter)); gint* pIntPath = gtk_tree_path_get_indices(path); wxCHECK_MSG( pIntPath, wxNOT_FOUND, wxT("failed to get iterator path") ); - int idx = pIntPath[0]; - - gtk_tree_path_free( path ); - - return idx; + return pIntPath[0]; } // get GtkTreeEntry from position (note: you need to g_unref it if valid) @@ -586,9 +583,8 @@ void wxListBox::SetString(unsigned int n, const wxString& label) // signal row changed GtkTreeModel* tree_model = GTK_TREE_MODEL(m_liststore); - GtkTreePath* path = gtk_tree_model_get_path(tree_model, &iter); + wxGtkTreePath path(gtk_tree_model_get_path(tree_model, &iter)); gtk_tree_model_row_changed(tree_model, path, &iter); - gtk_tree_path_free(path); } wxString wxListBox::GetString(unsigned int n) const @@ -728,13 +724,11 @@ void wxListBox::DoSetSelection( int n, bool select ) else gtk_tree_selection_unselect_iter(selection, &iter); - GtkTreePath* path = gtk_tree_model_get_path( - GTK_TREE_MODEL(m_liststore), &iter); + wxGtkTreePath path( + gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter)); gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f); - gtk_tree_path_free(path); - GTKEnableEvents(); } @@ -751,14 +745,12 @@ void wxListBox::DoScrollToCell(int n, float alignY, float alignX) if ( !GTKGetIteratorFor(n, &iter) ) return; - GtkTreePath* path = gtk_tree_model_get_path( - GTK_TREE_MODEL(m_liststore), &iter); + wxGtkTreePath path( + gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter)); // Scroll to the desired cell (0.0 == topleft alignment) gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, TRUE, alignY, alignX); - - gtk_tree_path_free(path); } void wxListBox::DoSetFirstItem(int n) @@ -775,15 +767,13 @@ int wxListBox::GetTopItem() const { int idx = wxNOT_FOUND; - GtkTreePath *start; - if ( gtk_tree_view_get_visible_range(m_treeview, &start, NULL) ) + wxGtkTreePath start; + if ( gtk_tree_view_get_visible_range(m_treeview, start.ByRef(), NULL) ) { gint *ptr = gtk_tree_path_get_indices(start); if ( ptr ) idx = *ptr; - - gtk_tree_path_free(start); } return idx; @@ -805,13 +795,13 @@ int wxListBox::DoListHitTest(const wxPoint& point) const gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview), &binx, &biny, NULL, NULL); - GtkTreePath* path; + wxGtkTreePath path; if ( !gtk_tree_view_get_path_at_pos ( m_treeview, point.x - binx, point.y - biny, - &path, + path.ByRef(), NULL, // [out] column (always 0 here) NULL, // [out] x-coord relative to the cell (not interested) NULL // [out] y-coord relative to the cell @@ -820,10 +810,7 @@ int wxListBox::DoListHitTest(const wxPoint& point) const return wxNOT_FOUND; } - int index = gtk_tree_path_get_indices(path)[0]; - gtk_tree_path_free(path); - - return index; + return gtk_tree_path_get_indices(path)[0]; } // ----------------------------------------------------------------------------