Sharing wxDEFINE_EVENT in two cpp fiels

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
philm
Experienced Solver
Experienced Solver
Posts: 88
Joined: Fri Dec 18, 2015 4:46 am

Sharing wxDEFINE_EVENT in two cpp fiels

Post by philm »

Hello all,

I am currently trying to create my own event so that I can propagate coordinate positions from the canvas to the parent class so that I can display the position of the mouse pointer on the frame of the application.

I am using this site as a reference: https://wiki.wxwidgets.org/Custom_Events

At the site, I am using the following code:

Code: Select all

wxDEFINE_EVENT(MY_NEW_TYPE, wxCommandEvent);

wxCommandEvent event(MY_NEW_TYPE); // No specific id
 
// Add any data; sometimes the only information needed at the destination is the arrival of the event itself
event.SetString("This is the data");
 
// Then post the event
wxPostEvent(this, event); // to ourselves
wxPostEvent(pBar, event); // or to a different instance or class
The code above would be in my canvas class.

The code below is what I have defined in my main frame class (The parent):

Code: Select all

wxBEGIN_EVENT_TABLE(MyFoo, wxFoo))
  EVT_COMMAND(wxID_ANY, MY_NEW_TYPE, MyFoo::OnMyEvent)
  ...
wxEND_EVENT_TABLE()
I am using this as a foundation for writing my the coordinate of the mouse cursor to my main frame so that it can be displayed on the screen. I would also like to note that I converted my coordinate position into a string and I already have the string formatted to what I want. This is currently replacing the value "This is the data"

My small issue that I am running into is that my main frame class will not be able to know what is MY_NEW_TYPE since the canvas class and the main frame class use different .cpp file. With wxDefine_EVENT only being used in one class, what is hte best solution to define the Event in my main class so that the main frame class will be able to understand the MY_NEW_TYPE?
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: Sharing wxDEFINE_EVENT in two cpp fiels

Post by DavidHart »

Hi,

See the mentions of wxDECLARE_EVENT on that wiki page.

Regards,

David
philm
Experienced Solver
Experienced Solver
Posts: 88
Joined: Fri Dec 18, 2015 4:46 am

Re: Sharing wxDEFINE_EVENT in two cpp fiels

Post by philm »

If you are refereing to this
You must define the wxEventType exactly once. If you add wxDEFINE_EVENT(MY_NEW_TYPE, wxCommandEvent); to two files, it will be defined twice with two different values. The same thing will happen if you write it only once, but you put it in a header file that is #included more than once. Why does that matter? Because you'll be posting an event with the type (e.g.) 12000, and trying to catch events with type 12001; which will silently fail.
I am aware of this. My issue is that I would need to define MY_NEW_TYPE in the cpp file that actually posts the event and in the cpp file that will catch the event since the class that posts the event will not be the class that catches. I apologize if my question is not very clear. With this rule in mind, how would I define MY_NEW_TYPE in the class that with catch the event?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Sharing wxDEFINE_EVENT in two cpp fiels

Post by doublemax »

My issue is that I would need to define MY_NEW_TYPE in the cpp file that actually posts the event and in the cpp file that will catch the event since the class that posts the event will not be the class that catches.
There is a big difference between defining and declaring. Like already said, wxDEFINE_EVENT must only exist once, because that's the line that gives MY_NEW_TYPE its value. wxDECLARE_EVENT however, can be seen by the compiler multiple times, it just means "there's a value for MY_NEW_TYPE defined somewhere. Go find it and use it".

So, you put wxDEFINE_EVENT in the .cpp file and the matching wxDECLARE_EVENT in the .h file. Where ever you want to use that event, you just include the header file.
Use the source, Luke!
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Re: Sharing wxDEFINE_EVENT in two cpp fiels

Post by MagickPanda »

doublemax wrote:
My issue is that I would need to define MY_NEW_TYPE in the cpp file that actually posts the event and in the cpp file that will catch the event since the class that posts the event will not be the class that catches.
There is a big difference between defining and declaring. Like already said, wxDEFINE_EVENT must only exist once, because that's the line that gives MY_NEW_TYPE its value. wxDECLARE_EVENT however, can be seen by the compiler multiple times, it just means "there's a value for MY_NEW_TYPE defined somewhere. Go find it and use it".

So, you put wxDEFINE_EVENT in the .cpp file and the matching wxDECLARE_EVENT in the .h file. Where ever you want to use that event, you just include the header file.
Thanks for the explanation, I think I have a few instances of wxDEFINE_EVENT and wxDECLARE_EVENT in the wrong source file or getting included more than once, I will fix that kind of problem asap.
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

Re: Sharing wxDEFINE_EVENT in two cpp fiels

Post by ollydbg23 »

Today, I found a similar issue, I see that I should use this:

Code: Select all

wxDECLARE_EXPORTED_EVENT(__declspec(dllexport), MyEventType, MyEvent);
With this code, I can export the event type from the dll, and the other client link to the dll can use it, otherwise, there will be a linker error.

Hope that this can help others.
wxProject
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Jul 20, 2022 11:31 pm

Re: Sharing wxDEFINE_EVENT in two cpp fiels

Post by wxProject »

You could add the custom event type as a public constant static member of a class, so it will be initialized (defined) only one time, you are able to use it in any cpp with the format: classname::neweventtype.
Post Reply