Proper way to queue a wxMenuEvent for next idle cycle?

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
delt
Knows some wx things
Knows some wx things
Posts: 27
Joined: Mon Dec 26, 2016 11:17 pm

Proper way to queue a wxMenuEvent for next idle cycle?

Post by delt »

Hello everyone, i'm trying to figure this out. I have a main window class and a "new document" dialog class, i want the dialog to appear on app startup by default. I can't just call ShowModal () on my dialog from the main window constructor, because that will block the redraw events on the main window. I've tried the following and several variations of it, from my main window constructor:

Code: Select all

  wxMenuEvent *ev = new wxMenuEvent (wxID_ANY, wxID_NEW);
  QueueEvent (ev);
...however the event just doesn't get processed. I've also tried AddPendingEvents () with a wxEvent as my mainwindow class member, same. Any tips?

/* edit */ Forgot to add, wxID_NEW is the same ID as my "file->new" menu item. Selecting it from the menu, or pressing its keyboard shortcut, both work fine. Clicking the toolbar entry with the same ID also works.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Proper way to queue a wxMenuEvent for next idle cycle?

Post by doublemax »

Code: Select all

wxMenuEvent *ev = new wxMenuEvent (wxID_ANY, wxID_NEW);
QueueEvent (ev);
That go that route, that hackish and horrible.

Try using CallAfter():
(in the ctor of your mainframe, assuming you have a method to create a new document)

Code: Select all

CallAfter( [this] {
  // this will be executed at the end of the next event handling loop
  this->OpenNewDocument();
});
If that still executes too early, use a oneshot wxTimer.
Use the source, Luke!
delt
Knows some wx things
Knows some wx things
Posts: 27
Joined: Mon Dec 26, 2016 11:17 pm

Re: Proper way to queue a wxMenuEvent for next idle cycle?

Post by delt »

Thanks, works!

...except a small problem, the position of the dialog relative to the main window is off-center, even if CallAfter () gets called after setting its size. Moving it manually would be kind of hackish and ugly. Seems like a similar issue to this one: viewtopic.php?f=1&t=43183
Post Reply