wxEvent derived event Topic is solved

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
msdobrescu
Knows some wx things
Knows some wx things
Posts: 35
Joined: Wed Sep 08, 2004 6:22 am

wxEvent derived event

Post 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
msdobrescu
Knows some wx things
Knows some wx things
Posts: 35
Joined: Wed Sep 08, 2004 6:22 am

Post by msdobrescu »

Additionally,
I develop a small application from scratch, so I'm no interested in keeping any compatibility with previous wx version.
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post 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
msdobrescu
Knows some wx things
Knows some wx things
Posts: 35
Joined: Wed Sep 08, 2004 6:22 am

Post 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'
msdobrescu
Knows some wx things
Knows some wx things
Posts: 35
Joined: Wed Sep 08, 2004 6:22 am

Post by msdobrescu »

I build an .exe.
I don't import/export anything.
Why should I declare the class as __declspec(dllimport/dllexport)?
msdobrescu
Knows some wx things
Knows some wx things
Posts: 35
Joined: Wed Sep 08, 2004 6:22 am

Post 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?
msdobrescu
Knows some wx things
Knows some wx things
Posts: 35
Joined: Wed Sep 08, 2004 6:22 am

Post 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?
framepointer
Super wx Problem Solver
Super wx Problem Solver
Posts: 264
Joined: Mon Aug 07, 2006 3:25 pm
Location: Baia Mare, Romania
Contact:

Post 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.
Software is like sex,
It's better when it's free.
~Linus Torvalds
msdobrescu
Knows some wx things
Knows some wx things
Posts: 35
Joined: Wed Sep 08, 2004 6:22 am

Post 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.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post 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 ;)
framepointer
Super wx Problem Solver
Super wx Problem Solver
Posts: 264
Joined: Mon Aug 07, 2006 3:25 pm
Location: Baia Mare, Romania
Contact:

Post 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
Software is like sex,
It's better when it's free.
~Linus Torvalds
msdobrescu
Knows some wx things
Knows some wx things
Posts: 35
Joined: Wed Sep 08, 2004 6:22 am

Post 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.
Post Reply