Add RAII wrapper for GTKDisableEvents/GTKEnableEvents() calls

Ensure GTKEnableEvents() is called automatically on scope exit whenever
GTKDisableEvents() is called.

This fixes a couple of potential bugs where GTKEnableEvents() could be not
called if wxCHECK() condition failed and makes the code shorter and safer.
This commit is contained in:
Vadim Zeitlin
2016-02-06 19:10:11 +01:00
parent 72af0d4ca1
commit 7a8684a8bd
7 changed files with 65 additions and 39 deletions

View File

@@ -20,6 +20,7 @@
#include <gtk/gtk.h>
#include "wx/gtk/private/gtk2-compat.h"
#include "wx/gtk/private/eventsdisabler.h"
//-----------------------------------------------------------------------------
// data
@@ -231,9 +232,8 @@ gtk_event_after(GtkRange* range, GdkEvent* event, wxSlider* win)
ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE);
}
// Keep slider at an integral position
win->GTKDisableEvents();
wxGtkEventsDisabler<wxSlider> noEvents(win);
gtk_range_set_value(GTK_RANGE (win->m_scale), win->GetValue());
win->GTKEnableEvents();
}
}
}
@@ -422,21 +422,20 @@ void wxSlider::SetValue( int value )
void wxSlider::GTKSetValue(int value)
{
GTKDisableEvents();
wxGtkEventsDisabler<wxSlider> noEvents(this);
gtk_range_set_value(GTK_RANGE (m_scale), value);
// GTK only updates value label if handle moves at least 1 pixel
gtk_widget_queue_draw(m_scale);
GTKEnableEvents();
}
void wxSlider::SetRange( int minValue, int maxValue )
{
GTKDisableEvents();
wxGtkEventsDisabler<wxSlider> noEvents(this);
if (minValue == maxValue)
maxValue++;
gtk_range_set_range(GTK_RANGE (m_scale), minValue, maxValue);
gtk_range_set_increments(GTK_RANGE (m_scale), 1, (maxValue - minValue + 9) / 10);
GTKEnableEvents();
if (HasFlag(wxSL_MIN_MAX_LABELS))
{
@@ -471,9 +470,8 @@ int wxSlider::GetMax() const
void wxSlider::SetPageSize( int pageSize )
{
GTKDisableEvents();
wxGtkEventsDisabler<wxSlider> noEvents(this);
gtk_range_set_increments(GTK_RANGE (m_scale), GetLineSize(), pageSize);
GTKEnableEvents();
}
int wxSlider::GetPageSize() const
@@ -494,9 +492,8 @@ int wxSlider::GetThumbLength() const
void wxSlider::SetLineSize( int lineSize )
{
GTKDisableEvents();
wxGtkEventsDisabler<wxSlider> noEvents(this);
gtk_range_set_increments(GTK_RANGE (m_scale), lineSize, GetPageSize());
GTKEnableEvents();
}
int wxSlider::GetLineSize() const