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

@@ -0,0 +1,41 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/gtk/private/eventsdisabler.h
// Purpose: Helper for temporarily disabling events.
// Author: Vadim Zeitlin
// Created: 2016-02-06
// Copyright: (c) 2016 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _GTK_PRIVATE_EVENTSDISABLER_H_
#define _GTK_PRIVATE_EVENTSDISABLER_H_
// ----------------------------------------------------------------------------
// wxGtkEventsDisabler: calls GTKDisableEvents() and GTKEnableEvents() in dtor.
// ----------------------------------------------------------------------------
// Template parameter T must be a wxGTK class providing the required methods,
// e.g. wxCheckBox, wxChoice, ...
template <typename T>
class wxGtkEventsDisabler
{
public:
// Disable the events for the specified (non-NULL, having lifetime greater
// than ours) window for the lifetime of this object.
explicit wxGtkEventsDisabler(T* win) : m_win(win)
{
m_win->GTKDisableEvents();
}
~wxGtkEventsDisabler()
{
m_win->GTKEnableEvents();
}
private:
T* const m_win;
wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxGtkEventsDisabler, T);
};
#endif // _GTK_PRIVATE_EVENTSDISABLER_H_

View File

@@ -54,10 +54,10 @@ public:
static wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
protected:
void GTKDisableEvents();
void GTKEnableEvents();
protected:
virtual wxSize DoGetBestSize() const wxOVERRIDE;
virtual void DoApplyWidgetStyle(GtkRcStyle *style) wxOVERRIDE;