From 0cea0a67f1c8d66a4abd916b9ffb9a11bd0feb30 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 24 Jan 2018 18:18:22 +0100 Subject: [PATCH] Use RAII SelectionEventsSuppressor in wxGTK wxDataViewCtrl code This is simpler and safer than always remembering to call GtkDisableSelectionEvents() and GtkEnableSelectionEvents() in pairs. No real changes. --- include/wx/gtk/dataview.h | 19 +++++++++++++++++++ src/gtk/dataview.cpp | 20 +++++--------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/include/wx/gtk/dataview.h b/include/wx/gtk/dataview.h index a6d1d45c3b..059f1dc367 100644 --- a/include/wx/gtk/dataview.h +++ b/include/wx/gtk/dataview.h @@ -188,6 +188,25 @@ public: int GTKGetUniformRowHeight() const { return m_uniformRowHeight; } + // Simple RAII helper for disabling selection events during its lifetime. + class SelectionEventsSuppressor + { + public: + explicit SelectionEventsSuppressor(wxDataViewCtrl* ctrl) + : m_ctrl(ctrl) + { + m_ctrl->GtkDisableSelectionEvents(); + } + + ~SelectionEventsSuppressor() + { + m_ctrl->GtkEnableSelectionEvents(); + } + + private: + wxDataViewCtrl* const m_ctrl; + }; + protected: virtual void DoSetExpanderColumn() wxOVERRIDE; virtual void DoSetIndent() wxOVERRIDE; diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp index d78d075559..3ca8b55e60 100644 --- a/src/gtk/dataview.cpp +++ b/src/gtk/dataview.cpp @@ -5068,7 +5068,7 @@ void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel ) { wxCHECK_RET( m_internal, "model must be associated before calling SetSelections" ); - GtkDisableSelectionEvents(); + SelectionEventsSuppressor noSelection(this); GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); @@ -5093,8 +5093,6 @@ void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel ) iter.user_data = (gpointer) item.GetID(); gtk_tree_selection_select_iter( selection, &iter ); } - - GtkEnableSelectionEvents(); } void wxDataViewCtrl::Select( const wxDataViewItem & item ) @@ -5103,7 +5101,7 @@ void wxDataViewCtrl::Select( const wxDataViewItem & item ) ExpandAncestors(item); - GtkDisableSelectionEvents(); + SelectionEventsSuppressor noSelection(this); GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); @@ -5111,15 +5109,13 @@ void wxDataViewCtrl::Select( const wxDataViewItem & item ) iter.stamp = m_internal->GetGtkModel()->stamp; iter.user_data = (gpointer) item.GetID(); gtk_tree_selection_select_iter( selection, &iter ); - - GtkEnableSelectionEvents(); } void wxDataViewCtrl::Unselect( const wxDataViewItem & item ) { wxCHECK_RET( m_internal, "model must be associated before calling Unselect" ); - GtkDisableSelectionEvents(); + SelectionEventsSuppressor noSelection(this); GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); @@ -5127,8 +5123,6 @@ void wxDataViewCtrl::Unselect( const wxDataViewItem & item ) iter.stamp = m_internal->GetGtkModel()->stamp; iter.user_data = (gpointer) item.GetID(); gtk_tree_selection_unselect_iter( selection, &iter ); - - GtkEnableSelectionEvents(); } bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const @@ -5146,24 +5140,20 @@ bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const void wxDataViewCtrl::SelectAll() { - GtkDisableSelectionEvents(); + SelectionEventsSuppressor noSelection(this); GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); gtk_tree_selection_select_all( selection ); - - GtkEnableSelectionEvents(); } void wxDataViewCtrl::UnselectAll() { - GtkDisableSelectionEvents(); + SelectionEventsSuppressor noSelection(this); GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); gtk_tree_selection_unselect_all( selection ); - - GtkEnableSelectionEvents(); } void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,