2D Graphics > Managers > Advanced Manager Features > Observers
 
Observers
Applications can be notified when the state of a manager changes. This notification mechanism is based on IlvManagerObserver, a subclass of IlvObserver. Observers are created by the application and set on the manager. The manager is in charge of sending messages to the observer under certain circumstances called reasons.
Notification messages are classified by their reason into different categories. An observer can choose to receive messages of one or several categories by setting its interest mask. The manager will only send a message to the observer if the notification reason belongs to a category of the observer interest mask. These categories are shown in Notification Categories:
Notification Categories
Category Description
Mask
General
IlvMgrMsgGeneralMask
Manager view
IlvMgrMsgViewMask
Manager layer
IlvMgrMsgLayerMask
Manager contents
IlvMgrMsgContentsMask
Object geometry
IlvMgrMsgObjectGeometryMask
An application wishing to get notification messages must define a subclass of IlvManagerObserver and overload the virtual member function update. In this member function, the observer receives an instance of IlvManagerMessage, or a subclass, containing the reason and additional relevant information about the notification.
General Notifications
This category concerns general notifications on the managers.
Interest mask: IlvMgrMsgGeneralMask
*Delete the manager
Reason: IlvMgrMsgDelete
Message type: IlvManagerMessage
Manager View Notifications
This category concerns notifications on operations performed on manager views.
Interest mask: IlvMgrMsgViewMask
*Add a view to the manager
Reason: IlvMgrMsgAddView
Message type: IlvManagerAddViewMessage
*Remove a view from the manager
Reason: IlvMgrMsgRemoveView
Message type: IlvManagerRemoveViewMessage
*Set an interactor on a view
Reason: IlvMgrMsgSetInteractor
Message type: IlvManagerSetInteractorMessage
*Set a transformer on a view
Reason: IlvMgrMsgSetTransformer
Message type: IlvManagerSetTransformerMessage
Manager Layer Notifications
This category concerns notifications on operations performed on manager layers.
Interest mask: IlvMgrMsgLayerMask
*Add a layer to the manager
Reason: IlvMgrMsgAddLayer
Message type: IlvManagerLayerMessage
*Remove a layer from the manager
Reason: IlvMgrMsgRemoveLayer
Message type: IlvManagerLayerMessage
*Change the index of a layer
Reason: IlvMgrMsgMoveLayer
Message type: IlvManagerMoveLayerMessage
*Swap indexes between two layers
Reason: IlvMgrMsgSwapLayer
Message type: IlvManagerSwapLayerMessage
*Set the name of a layer
Reason: IlvMgrMsgLayerName
Message type: IlvManagerLayerNameMessage
*Set the visibility of a layer
Reason: IlvMgrMsgLayerVisibility
Message type: IlvManagerLayerVisibilityMessage
*Set the selectabililty of a layer
Reason: IlvMgrMsgLayerSelectability
Message type: IlvManagerLayerMessage
Manager Contents Notifications
This category concerns notifications on the changes in the contents of managers.
Interest mask: IlvMgrMsgContentsMask
*Add a graphic object to the manager
Reason: IlvMgrMsgAddObject
Message type: IlvManagerContentsMessage
*Remove a graphic object from the manager
Reason: IlvMgrMsgRemoveObject
Message type: IlvManagerContentsMessage
*Set the layer of a graphic object
Reason: IlvMgrMsgObjectLayer
Message type: IlvManagerObjectLayerMessage
Graphic Object Geometry Notifications
This category concerns notifications on a change of geometry of the objects (for example, move, resize, and rotate).
Interest mask: IlvMgrMsgObjectGeometryMask
*Change the geometry of a graphic object
Reason: IlvMgrMsgObjectGeometry
Message type: IlvManagerObjectGeometryMessage
Example
Here is the implementation of an observer that receives notifications on adding or removing layers and views.
class MyManagerObserver
: public IlvManagerObserver
{
public:
MyManagerObserver(IlvManager* manager)
: IlvManagerObserver(manager,
IlvMgrMsgLayerMask | IlvMgrMsgViewMask)
{}
virtual void update(IlvObservable* o, IlAny arg);
};
The update member function:
void MyManagerObserver::update(IlvObservable* obs, IlAny arg)
{
IlvManager* manager = ((IlvManagerObservable*)obs)->getManager();
switch(((IlvManagerMessage*) arg)->_reason) {
// __ Notification on manager view
case IlvMgrMsgAddView:
IlvPrint("Add view notification");
break;
case IlvMgrMsgRemoveView:
IlvPrint("Remove view notification");
break;
// __ Notification on manager layer
case IlvMgrMsgAddLayer:
IlvPrint("Add layer notification: %d",
((IlvManagerLayerMessage*)arg)->getLayer());
break;
case IlvMgrMsgRemoveLayer:
IlvPrint("Remove layer notification: %d",
((IlvManagerLayerMessage*)arg)->getLayer());
break;
default:
IlvPrint("Unhandled notification");
break;
}
}
To attach the observer to the manager:
MyManagerObserver* observer = new MyManagerObserver(manager);

Version 6.3
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.