add a RAII wx connect wrapper

master
jp9000 2013-12-13 08:15:50 -07:00
parent 40f922ca37
commit fa00c6fa47
2 changed files with 71 additions and 12 deletions

View File

@ -20,10 +20,12 @@
#include "obs-app.hpp"
#include "settings-basic.hpp"
#include "window-settings-basic.hpp"
#include "wx-wrappers.hpp"
#include "platform.hpp"
class GeneralSettings : public BasicSettingsData {
ConfigFile localeIni;
WXConnector languageBoxConnector;
void LanguageChanged(wxCommandEvent &event);
@ -32,7 +34,6 @@ class GeneralSettings : public BasicSettingsData {
public:
GeneralSettings(OBSBasicSettings *window);
virtual ~GeneralSettings();
virtual void Apply();
};
@ -86,16 +87,8 @@ GeneralSettings::GeneralSettings(OBSBasicSettings *window)
"General", "Language");
FillLanguageList(currentLang);
window->languageList->Connect(
wxEVT_COMBOBOX,
wxCommandEventHandler(GeneralSettings::LanguageChanged),
NULL,
this);
}
GeneralSettings::~GeneralSettings()
{
window->languageList->Disconnect(
languageBoxConnector.Connect(
window->languageList,
wxEVT_COMBOBOX,
wxCommandEventHandler(GeneralSettings::LanguageChanged),
NULL,

View File

@ -17,8 +17,74 @@
#pragma once
#include <wx/window.h>
#include <wx/event.h>
struct gs_window;
class wxWindow;
gs_window WxToGSWindow(const wxWindow *window);
void OBSErrorBox(wxWindow *parent, const char *message, ...);
/*
* RAII wx connection wrapper
*
* Automatically disconnects events on destruction rather than having to
* manually call Disconnect for every Connect.
*/
class WXConnector {
wxEvtHandler *obj;
wxEventType eventType;
wxObjectEventFunction func;
wxObject *userData;
wxEvtHandler *eventSink;
public:
inline WXConnector()
: obj (NULL),
eventType (0),
func (NULL),
userData (NULL),
eventSink (NULL)
{
}
inline WXConnector(wxEvtHandler *obj, wxEventType eventType,
wxObjectEventFunction func, wxObject *userData,
wxEvtHandler *eventSink)
: obj (obj),
eventType (eventType),
func (func),
userData (userData),
eventSink (eventSink)
{
obj->Connect(eventType, func, userData, eventSink);
}
inline ~WXConnector()
{
Disconnect();
}
inline void Connect(wxEvtHandler *obj, wxEventType eventType,
wxObjectEventFunction func, wxObject *userData,
wxEvtHandler *eventSink)
{
Disconnect();
this->obj = obj;
this->eventType = eventType;
this->func = func;
this->userData = userData;
this->eventSink = eventSink;
obj->Connect(eventType, func, userData, eventSink);
}
inline void Disconnect()
{
if (obj)
obj->Disconnect(eventType, func, userData, eventSink);
obj = NULL;
}
};