Generating IDs in wxWidgets 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
xargon
Knows some wx things
Knows some wx things
Posts: 25
Joined: Wed Feb 25, 2009 3:00 pm

Generating IDs in wxWidgets

Post by xargon » Thu Jan 14, 2016 12:50 pm

I just started using wxWidgets and looking at some examples when we want to give some identifiers which we can use in event connectors, I see the following usage:

Code: Select all

class MyClass
{
....
private:
    enum {MY_CTRL_ID=wxID_HIGHEST + 1};
}
And then I can use this in a Connect() call for example, which works fine.

My question is say I have another class now and I want to generate another identifier. Would this mean, I should do something like:

Code: Select all

class MyClass2
{
....
private:
    enum {MY_CTRL_ID_2=wxID_HIGHEST + 2};
}
Or would things not interfere. So basically do these things get registered globally or at the each class level internally.

User avatar
doublemax
Moderator
Moderator
Posts: 15263
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Generating IDs in wxWidgets

Post by doublemax » Thu Jan 14, 2016 1:59 pm

So basically do these things get registered globally or at the each class level internally.
IDs based on wxID_HIGHEST are just enums, they don't get managed in any way. It's your responsibility to make sure there are no collisions.

You could do something like this:
In a global header file you assign ID ranges for certain classes:

Code: Select all

enum
{ 
  CLASS1_ID_BASE = wxID_HIGHEST + 1,
  CLASS2_ID_BASE = wxID_HIGHEST + 100,
  CLASS3_ID_BASE = wxID_HIGHEST + 200
}
Then, for the actual classes:

Code: Select all

#include "id_bases.h"
enum
{
  MY_CTRL1 = CLASS1_ID_BASE,
  MY_CTRL2
}
Alternatively you can use wxNewId() to generate a unique ID at runtime.

Code: Select all

const int MY_CTRL_ID = wxNewId();
One of the disadvantages: You can't use these IDs with switch/case.
Use the source, Luke!

xargon
Knows some wx things
Knows some wx things
Posts: 25
Joined: Wed Feb 25, 2009 3:00 pm

Re: Generating IDs in wxWidgets

Post by xargon » Thu Jan 14, 2016 2:30 pm

That's great advise! Thanks.

Post Reply