Page 1 of 1

wxEvent derived event

Posted: Mon Feb 11, 2008 6:04 am
by msdobrescu
Hello,

I need help writing a derived class from wxEvent.
I need to split it into .h & .cpp.
The sample from documentation can't be compiled saying 'cannot instantiate abstract class' (see Event handling overview).
In my case, I use the dll build of wx 2.8.7 and MSVC 2008 as compiler,a precompiled headers project template of code::blocks.

Thanks in advance,
Mike

Posted: Mon Feb 11, 2008 6:14 am
by msdobrescu
Additionally,
I develop a small application from scratch, so I'm no interested in keeping any compatibility with previous wx version.

Posted: Mon Feb 11, 2008 10:18 am
by DavidHart
Hi Mike,

Are you missing a #include "wx/event.h" ?

Otherwise try the example at http://www.wxwidgets.org/wiki/index.php ... ethod_4.29

Regards,

David

Posted: Mon Feb 11, 2008 10:25 am
by msdobrescu
Hi,

I've managed to implement some class, but now I have this:

MyEvent.cpp(20) : warning C4273: 'wxMY_EVENT' : inconsistent dll linkage
m:\development-projects\codeblocks\wxsmith\finup\MyEvent.h(12) : see previous definition of 'wxMY_EVENT'

Posted: Mon Feb 11, 2008 11:13 am
by msdobrescu
I build an .exe.
I don't import/export anything.
Why should I declare the class as __declspec(dllimport/dllexport)?

Posted: Mon Feb 11, 2008 1:02 pm
by msdobrescu
So far, I've found that it is not related to precompiled headers.
I have created a regular application project with no precompiled headers.

In any case I have this linkage warning which results in not triggering the event.

Anybody has a sample of application with one basic event that works and it is written in header and implementation files?

Posted: Mon Feb 11, 2008 1:18 pm
by msdobrescu
I have just pointed to the defines to see what's there.

For: DECLARE_EVENT_TYPE it goes to:

#define DECLARE_EVENT_TYPE(name, value) name = wxEVT_FIRST + value,


For DEFINE_EVENT_TYPE it goes to:

#define DEFINE_EVENT_TYPE(name)

I think this should be the reason, but I don't know why DEFINE_EVENT_TYPE it is not defined.

Any hint?

Posted: Mon Feb 11, 2008 3:05 pm
by framepointer
Hi,

Here is how I do it.

Header file:

Code: Select all

#ifndef __CUSTOM_EVENT_H__
#define __CUSTOM_EVENT_H__

#define WXDLLIMPEXP_CUSTOM_EVENT

class CustomEvent : public wxNotifyEvent
{
	private:
	protected:
	
	public:
		CustomEvent(wxEventType commandType = wxEVT_NULL, 
			       int winid = 0
			      );
		
		CustomEvent(CustomEvent & inEvent);
		
		virtual ~CustomEvent();
		
		virtual wxEvent* Clone() const;
		DECLARE_DYNAMIC_CLASS(CustomEvent)
};

BEGIN_DECLARE_EVENT_TYPES()
		DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_CUSTOM_EVENT, wxEVT_COMMAND_CUSTOM_EVENT, 52000)
END_DECLARE_EVENT_TYPES()

typedef void (wxEvtHandler::*CustomEventFunction) (CustomEvent&);


#define CustomEventHandler(func) \
	(wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(CustomEventFunction, &func)

#define EVT_COMMAND_CUSTOM_EVENT_UPDATE(fn) \
		wx__DECLARE_EVT1(wxEVT_COMMAND_CUSTOM_EVENT, 0, CustomEventHandler(fn))
#endif
cpp file:

Code: Select all

#include "CustomEvent.h"


//---------------------------------------------------------------------------------------------------------
// 
IMPLEMENT_DYNAMIC_CLASS(CustomEvent, wxNotifyEvent)
DEFINE_EVENT_TYPE(wxEVT_COMMAND_CUSTOM)

//---------------------------------------------------------------------------------------------------------
// 
CustomEvent::CustomEvent(wxEventType commandType, 
			       int winid
			      )
	:
	wxNotifyEvent(commandType, winid)
{
}

//---------------------------------------------------------------------------------------------------------
// 
CustomEvent::CustomEvent(CustomEvent & inEvent) 
	: 
		wxNotifyEvent(inEvent.GetEventType(), inEvent.GetId())
{
	
}

//---------------------------------------------------------------------------------------------------------
// 
wxEvent * CustomEvent::Clone() const
{
	return new CustomEvent( GetEventType(),
				   GetId()
				 );
}
//---------------------------------------------------------------------------------------------------------
// 
CustomEvent::~CustomEvent()
{
}
Note: if you want to be able to set members of the class in the constructor, you must be able to set an implicit value, or you will get errors.

Also remeber to set members in the Clone() method.

Posted: Mon Feb 11, 2008 4:22 pm
by msdobrescu
Thanks, it links with no error/warning.

But I still don't know why.

I wish to understand why it works this way when it doesn't in the 'officially recommended' way.

Posted: Mon Feb 11, 2008 7:16 pm
by Auria
msdobrescu wrote:I have just pointed to the defines to see what's there.

For: DECLARE_EVENT_TYPE it goes to:

#define DECLARE_EVENT_TYPE(name, value) name = wxEVT_FIRST + value,


For DEFINE_EVENT_TYPE it goes to:

#define DEFINE_EVENT_TYPE(name)

I think this should be the reason, but I don't know why DEFINE_EVENT_TYPE it is not defined.

Any hint?
as far as I know, these macros are not for wxEvent-derived stuff, but for e.g. declaring a custom wxCommandEvent (that's what I do... I use these macros, but do not derive from wxEvent) if you wish to derive, framepointer's example is probably what you're looking for

* Note that I say "probably", i'm not a guru when it comes to event handling ;)

Posted: Tue Feb 12, 2008 11:54 am
by framepointer
msdobrescu wrote:Thanks, it links with no error/warning.

But I still don't know why.

I wish to understand why it works this way when it doesn't in the 'officially recommended' way.
What do you mean by offiacly recommanded?
You have to know that the examples that are on wxWiki might be outdated or incomplete sometimes.

The example I gave you is the simplest way of dealing with custom errors. Of course, these events wont propagate, so usually you must send them to the object that is responsable for handling the event.

Regards

Posted: Tue Feb 12, 2008 1:42 pm
by msdobrescu
I mean: why DECLARE_EXPORTED_EVENT_TYPE and not DECLARE_EVENT_TYPE?

Btw, WXDLLIMPEXP_CUSTOM_EVENT is defined to nothing, so

DECLARE_EXPORTED_EVENT_TYPE(, wxEVT_COMMAND_CUSTOM_EVENT, 52000)

is the same thing.

I need to know if there's some build option I set wrong that makes it need DECLARE_EXPORTED_EVENT_TYPE in a plain exe.