Updated wxWidgets Coding Strategies (markdown)

Kingcom 2014-08-05 04:06:02 -07:00
parent 954ca419c9
commit 6c88411e9b
1 changed files with 7 additions and 7 deletions

@ -61,27 +61,27 @@ There are several ways to send events between windows in wxWidgets, and only one
```C++
// This method ensures cross-platform consistency and thread safety, but cannot be
// used to get a return code.
myWidget->GetEventHandler()->AddPendingEvent( evt );
myWidget->GetEventHandler()->AddPendingEvent( evt );
// This one performs an immediate handling of the event, and should only be used
// if you need a return code, or know for sure the caller is on the Main/GUI thread
myWidget->GetEventHandler()->ProcessEvent( evt );
myWidget->GetEventHandler()->ProcessEvent( evt );
// The wxApp class does not have a GetEventHandler() so for it you use this:
wxGetApp()->AddPendingEvent( evt ); // safe from any thread
wxGetApp()->PocessEvent( evt ); // safe form GUI thread only
wxGetApp()->AddPendingEvent( evt ); // safe from any thread
wxGetApp()->PocessEvent( evt ); // safe form GUI thread only
```
This can be a bit confusing because wx has several other options for sending events, and most of them have caveats as noted below:
```C++
// This one works but is depreciated, and can lead to cross-platform inconsistencies:
myWidget->AddPendingEvent( evt );
myWidget->AddPendingEvent( evt );
// This one has the same problem as above.
wxPostEvent( myWidget, evt );
// This one actually works correctly, but might as well just use the more direct
// example of myWidget->GetEventHandler()->AddPendingEvent( evt );
wxPostEvent( myWidget->GetEventHandler(), evt );
// example of myWidget->GetEventHandler()->AddPendingEvent( evt );
wxPostEvent( myWidget->GetEventHandler(), evt );
```
... So the moral of the story is: Use `GetEventHandler()` when possible (basically anything except the `wxApp` object), and use `AddPendingEvent()` unless you need a return code _and_ know you're on the main GUI thread.