Code::Blocks SDK events
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.
Note: The SDK version number is defined in 'cbplugin.h'
There are many event constants defined for the interesting events that happens inside Code::Blocks. These constants are defined in
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 :).
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:
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
As mentioned above, there is no need to unregister anything in
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
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
List of SDK versions
Events that must be registered with RegisterEventSink (from 'sdk_events.cpp')
==> updated (July 02, 2017) since sdk 1.30.0 (Code::Blocks 16.01)
- 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_WORKSPACE_LOADING_COMPLETE (sdk 1.19.0)
- cbEVT_WORKSPACE_CLOSING_BEGIN (sdk 1.19.0)
- cbEVT_WORKSPACE_CLOSING_COMPLETE (sdk 1.19.0)
build targets events (CodeBlocksEvent) (sdk 1.19.0)
pipedprocess events (CodeBlocksEvent) (sdk 1.19.0)
thread-pool events (CodeBlocksEvent) (sdk 1.19.0)
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
layout related events (CodeBlocksDockEvent)
- cbEVT_UPDATE_VIEW_LAYOUT (sdk 1.19.0) : force update current view layout
- cbEVT_QUERY_VIEW_LAYOUT : ask which is the current view layout
- cbEVT_SWITCH_VIEW_LAYOUT : request app to switch view layout
- cbEVT_SWITCHED_VIEW_LAYOUT : app notifies that a new layout has been applied
- cbEVT_DOCK_WINDOW_VISIBILITY : app notifies that a docked window has been hidden/show
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)
- cbEVT_COMPILER_SET_BUILD_OPTIONS (sdk 1.19.0)
- cbEVT_COMPILER_CLEAN_PROJECT_STARTED (sdk 1.19.0)
- cbEVT_CLEAN_WORKSPACE_STARTED (sdk 1.19.0)
- cbEVT_COMPILER_SETTING_CHANGED (sdk 1.19.0)
- cbEVT_COMPILE_FILE_REQUEST (sdk 1.19.0) : request app to compile a single file
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_HIDE_LOG_WINDOW (sdk 1.19.0) : hide 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)
cbAUiNotebook related events (CodeBlocksLogEvent) (sdk 1.19.0)
code-completion related events (CodeBlocksLogEvent) (sdk 1.19.0)
Mandrav 10:31, 23 November 2007 (UTC)