wxEvtHandler::Connect problems and question

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
4ggre5510n
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sat Jan 14, 2012 10:34 pm

wxEvtHandler::Connect problems and question

Post by 4ggre5510n » Sun Mar 04, 2012 8:28 pm

Greetings.

Using wxEvtHandler::Conect() im binding certain event to a function processing that event.

Now, as I understand, this function can be a method from any class, since I can create functor using wxObjectEventFunction().
Yet, when I call function from class derived from wxEvtHandler - I can keep my access to member's of this class [ eventSink* aint this ].

My question is - is there any way to call method from a class not derived from wxEvtHandler ( 60 additional bytes are a lot in my case ) [and not being a fuctor, since I lost access to class members] ?

Also - what's the actual difference between wxCommandEventHandler() and wxObjectEventFunction() used in function argument of Connect() ?

I can use first only with connecting to functions from class I currently call Connect().

Simple code presenting my problem:

Code: Select all

/**
	W jaki sposób NIE używając STATIC i NIE dziedzicząc z exEvtHandler wywołać funkcję onEvent z odpowiedniej klasy ?
**/

#include <wx/wx.h>

class some_class {
private:
	int x;
public:
	void onClickRight(wxCommandEvent &evt) {
		wxString str;
		str << x;
		wxMessageBox(str);
	}

	some_class() :
		x(15)
		{}
} o_some_class ;

class some_class_derived : public wxEvtHandler {
private:
	int x;
public:
	void onClickLeft(wxCommandEvent &evt) {
		wxString str;
		str << x;
		wxMessageBox(str);
	}

	some_class_derived() :
		x(15)
		{}
} o_some_class_derived ;

class my_frame : public wxFrame {
public:

	void onClickMiddle(wxCommandEvent &evt) {
		wxMessageBox(L"wxCommandEvent vs wxObjectEventFunction");
	}

	my_frame() : wxFrame(NULL, wxID_ANY, L"Title")
		{
			Centre();
			Connect(wxEVT_RIGHT_DOWN, wxObjectEventFunction(&some_class::onClickRight) /*, NULL, &o_some_class*/); // Can't do that, some_class aint derived from EvtHandler
			Connect(wxEVT_LEFT_DOWN, wxObjectEventFunction(&some_class_derived::onClickLeft), NULL, &o_some_class_derived);
			Connect(wxEVT_MIDDLE_DOWN, wxCommandEventHandler(my_frame::onClickMiddle));
		}

};

class app : public wxApp {
	virtual bool OnInit() {
		my_frame* f = new my_frame();
		f->Show();
		return true;
	}
};

IMPLEMENT_APP(app);
Thanks in advance for your answers :)
Best regards.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2922
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxEvtHandler::Connect problems and question

Post by PB » Mon Mar 05, 2012 6:46 am

4ggre5510n wrote:My question is - is there any way to call method from a class not derived from wxEvtHandler ( 60 additional bytes are a lot in my case ) [and not being a fuctor, since I lost access to class members] ?
I don't quite understand what you mean by "I lost access to class members", but I believe that for Connect() the event handling function must be wxEventFunction (see below) and the event sink must be derived from wxEvtHandler. Have you looked into Bind() (available only for wxWidgets 2.9)? Bind() allows to use pretty much any function as an event handler and using e.g. boost::function<> you can use methods of any class, see examples here. I think using Bind() is the only way to do what you want, if you're on wxWidgets 2.8 you may be out of luck.
4ggre5510n wrote:Also - what's the actual difference between wxCommandEventHandler() and wxObjectEventFunction() used in function argument of Connect() ?

Code: Select all

typedef void (wxEvtHandler::*wxEventFunction)(wxEvent&);
typedef wxEventFunction wxObjectEventFunction;
#define wxStaticCastEvent(type, val) static_cast<type>(val)
#define wxEVENT_HANDLER_CAST( functype, func ) ( wxObjectEventFunction )( wxEventFunction )wxStaticCastEvent( functype, &func )
#define wxCommandEventHandler(func) wxEVENT_HANDLER_CAST(wxCommandEventFunction, func)

Post Reply