Difference between revisions of "Code::Blocks SDK events"
(→List of Available Code::Blocks Events)
|Line 66:||Line 66:|
=== List of Available Code::Blocks Events ===
=== List of Available Code::Blocks Events ===
==== Events that must be registered with RegisterEventSink
==== Events that must be registered with RegisterEventSink (from 'sdk_events.cpp')
Revision as of 13:20, 23 January 2015
During a Code::Blocks session, many things are happening: projects/files loading, closing, activating, saving, etc. These are interesting things that happen and various parts of Code::Blocks and its plugins are interested in them. The Code::Blocks SDK provides functions that allow a part of the code to be "subscribed" to these events (i.e. be notified when they happen). This is what we'll look at in this article.
Note: since mid-November 2007, loggers registration is handled with events too.
- 1 Available events
- 2 Subscribing to events
- 3 Unsubscribing from events
- 4 When to register/unregister for event notifications?
- 5 Any examples?
- 6 List of Available Code::Blocks Events
There are many event constants defined for the interesting events that happens inside Code::Blocks. These constants are defined in include/sdk_events.h and listed at the bottom of this page.
Also note that there 4 different event categories:
- Logging system related (CodeBlocksLogEvent)
- View layout related (CodeBlocksLayoutEvent)
- Docking system related (CodeBlocksDockEvent)
- and everything else (CodeBlocksEvent)
Subscribing to events
Up until July 6th (2007) rev4233, Code::Blocks events were handled like other wxWidgets events: by declaring an event table in your class that should handle them and adding relevant event table entries.
But the event system changed radically on July 6th (2007) rev4234, in order to battle some shortcomings of the wxWidgets event system (at least for our usage). While designing the new system, great attention has been paid so that plugin writers have to do minimum changes to convert their code.
The class that handles all event registrations is the central class in Code::Blocks which you use if you want to do anything with the Code::Blocks SDK: Manager. Let's see by example how would a class (e.g. SomePlugin) subscribe for the cbEVT_EDITOR_SAVE event (fired when an editor's contents are saved):
// assuming an existing member function: // void SomePlugin::OnEditorSaved(CodeBlocksEvent& event); Manager::Get()->RegisterEventSink(cbEVT_EDITOR_SAVE, new cbEventFunctor<SomePlugin, CodeBlocksEvent>(this, &SomePlugin::OnEditorSaved));
Seems difficult? It isn't, if you realize what the second argument to Manager::RegisterEventSink does.
cbEventFunctor is a class that encapsulates a this-pointer call. It is like a C function pointer except that it works with class instances (i.e. a member-function pointer). For obvious reasons it is a template class, taking two template arguments: the first is the class' name and the second is the type of the event the member function handles.
The constructor of cbEventFunctor takes two arguments: first is the class instance (we use the this pointer) and the second is the address of the member function.
And that's it. After using the above code, the defined function will be called-back on each editor saving :).
Important note: For various reasons, cbEVT_PIPEDPROCESS* and cbEVT_THREADEDTASK* events should still be bound using the old way (i.e. by adding event table entries). If this is changed in the future, this article will be updated accordingly.
Unsubscribing from events
Usually there is no need to unsubscribe from event notifications. A plugin, for example, is automatically removed from all event notifications when it is detached (deactivated, unloaded, uninstalled).
But if you really think you need to unsubscribe from events, there is a function to do that: Manager::Get()->RemoveAllEventSinksFor(void* owner). As the sole argument, pass the instance that you registered the events in the first place.
Notice that this function will remove all event notifications for the specified instance. There is no way to remove event notifications selectively. It's an all-or-nothing deal...
When to register/unregister for event notifications?
In plugins, event notifications should be registered in OnAttach(). As mentioned above, there is no need to unregister anything in OnDetach() because it is automatically done by Code::Blocks.
For code other than plugins, register whenever you think is better (depends on the actual code).
Also notice that Code::Blocks removes all registrations on shut down, so don't even think that we have memory leaks in that part ;).
If the example provided above is not enough, browse through the sources of the core/contrib Code::Blocks plugins. A few of them register for events: just search the code for RegisterEventSink. But it really is nothing difficult: just remove the event handlers from the class's event table and register with this new way.
Try using the DisplayEvents plugin if there is any difficulty determining which events to register.
List of Available Code::Blocks Events
Events that must be registered with RegisterEventSink (from 'sdk_events.cpp')
- cbEVT_APP_START_ACTIVATED (sdk 1.19.0)
- cbEVT_APP_START_DEACTIVATED (sdk 1.19.0)
plugin events (CodeBlocksEvent)
- cbEVT_PLUGIN_LOADING_COMPLETE (sdk 1.19.0)
editor events (CodeBlocksEvent)
- cbEVT_EDITOR_BEFORE_SAVE (sdk 1.19.0)
project events (CodeBlocksEvent)
- cbEVT_PROJECT_NEW (sdk 1.19.0)
- cbEVT_PROJECT_OPTIONS_CHANGED (sdk 1.19.0)
- cbEVT_CLEAN_WORKSPACE_LOADING_COMPLETE (sdk 1.19.0)
build targets events (CodeBlocksEvent) (sdk 1.19.0)
main menubar creation (CodeBlocksEvent)
- cbEVT_MENUBAR_CREATE_BEGIN: app notifies that the menubar is started being (re)created
- cbEVT_MENUBAR_CREATE_END: app notifies that the menubar (re)creation ended
compiler-related events (CodeBlocksEvent)
debugger-related events (CodeBlocksEvent)
logging-system related events (CodeBlocksLogEvent)
- cbEVT_ADD_LOG_WINDOW: add a log window
- cbEVT_REMOVE_LOG_WINDOW: remove a log window
- cbEVT_SWITCH_TO_LOG_WINDOW: switch to a log window (make it visible)
- cbEVT_SHOW_LOG_MANAGER: show log manager
- cbEVT_HIDE_LOG_MANAGER: hide log manager
- cbEVT_LOCK_LOG_MANAGER: "lock" it (used with auto-hiding functionality)
- cbEVT_UNLOCK_LOG_MANAGER: "unlock" it (used with auto-hiding functionality)
dockable windows (CodeBlocksDockEvent)
- cbEVT_ADD_DOCK_WINDOW: request app to add and manage a docked window
- cbEVT_REMOVE_DOCK_WINDOW: request app to stop managing a docked window
- cbEVT_SHOW_DOCK_WINDOW: request app to show a docked window
- cbEVT_HIDE_DOCK_WINDOW: request app to hide a docked window
- cbEVT_DOCK_WINDOW_VISIBILITY: app notifies that a docked window has been hidden/shown to actually find out its state use IsWindowReallyShown(event.pWindow);
layout related events (CodeBlocksLayoutEvent)
- cbEVT_SWITCH_VIEW_LAYOUT: request app to switch view layout
- cbEVT_SWITCHED_VIEW_LAYOUT: app notifies that a new layout has been applied
Events processed on wxWidgets event tables and not registered using RegisterEventSink
pipedprocess events (CodeBlocksEvent)
thread-pool events (CodeBlocksEvent)
Mandrav 10:31, 23 November 2007 (UTC)