View Hooks
Manager view hooks are part of a mechanism allowing the application to be notified when certain actions are performed on or by the manager. This can be used for various reasons such as monitoring the contents of a manager, performing additional drawings when the manager redraws its graphic objects, or taking an action when the transformer of a manager view changes.
Note: Another notification mechanism is described in section Observers. |
This section is divided as follows:
Manager View Hooks
A manager view hook is an instance of the
IlvManagerViewHook class. To be active, it must be associated with a manager view. Each manager view handles a list of view hooks. To connect and disconnect view hooks from a manager view, use the following
IlvManager member functions:
IlvManager::installViewHook IlvManager::removeViewHook The
IlvManagerViewHook class has a number of virtual member functions that are automatically called by the manager when certain predefined operations occur. Here is the list of these member functions and the circumstances under which they are called:
Called before the manager draws in the manager view. Applications often overload this member function to perform additional drawings before the manager displays its graphic objects.
Called after the manager has drawn in the manager view. Applications often overload this member function to perform additional drawings on top of the graphic objects displayed by the manager.
Called after the manager has received an Expose event in the view.
Called when the interactor of the manager view changes.
Called when the transformer of the manager view changes.
Called when the manager view is resized.
Called when the manager view is detached from the manager.
Called when the contents of the manager change, that is, graphic objects have been added, removed, or their geometry has changed.
When an event occurs in view, the manager calls the corresponding member functions of all the hooks attached to this view.
Example: Monitoring the Number of Objects in a Manager
The following code is a subclass of
IlvManagerViewHook that displays in an
IlvTextField the number of objects contained in the manager:
class DisplayObjectsHook : public IlvManagerViewHook { public: DisplayObjectsHook(IlvManager* manager, IlvView* view, IlvTextField* textfield) : IlvManagerViewHook(manager, view), _textfield(textfield) {} virtual void contentsChanged(); protected: IlvTextField* _textfield; }; void DisplayObjectsHook::contentsChanged() { IlvUInt count = getManager()->getCardinal(); _textfield->setValue((IlvInt)count, IlTrue); } |
Example: Maintaining a Scale Displayed With No Transformation
This part presents an example of subtyping an
IlvManagerViewHook. At first there is a map and a circular scale used as a compass card. Then, because of hooks, the manager translates and zooms the view without affecting the compass card. The
afterDraw and
transformerChanged member functions are redefined to redraw the scale to its original dimensions and location.
static void ILVCALLBACK
Quit(IlvView* view, IlAny)
{
delete view->getDisplay();
IlvExit(0);
}
char* labels[] = {“N”, “O”, “S”, “E”, ““};
class ExHook
: public IlvManagerViewHook
{
public :
ExHook(IlvManager* m, IlvView* v, const IlvRect* psize=0)
: IlvManagerViewHook(m, v)
{
_cirscale = new IlvCircularScale(m->getDisplay(),
IlvRect(30, 30, 100, 100),
“%.4f”,
0, 100, 90., 360.);
_cirscale->setLabels(5, (const char* const*)labels);
}
virtual void afterDraw(IlvPort*,
const IlvTransformer* = 0,
const IlvRegion* = 0,
const IlvRegion* = 0);
virtual void transformerChanged(const IlvTransformer*,
const IlvTransformer*);
protected :
IlvRect _size;
IlvCircularScale* _cirscale;
};
void ExHook::afterDraw(IlvPort* dst,
const IlvTransformer*,
const IlvRegion*,
const IlvRegion* clip)
{
if (getManager()->isInvalidating())
getManager()->reDrawViews();
_cirscale->draw(dst, 0, 0 /*clip*/);
if (dst->isABitmap())
_cirscale->draw(getView(), 0, 0);
}
void ExHook::transformerChanged(const IlvTransformer* current,
const IlvTransformer* old)
{
IlvRect bbox;
_cirscale->boundingBox(bbox);
if (old) old->inverse(bbox);
if (current) current->apply(bbox);
if (!getManager()->isInvalidating())
{
getManager()->initReDraws();
getManager()->invalidateRegion(getView(), bbox);
}
}
static void
SetDoubleBuffering(IlvManager* m,
IlvView* v,
IlvEvent&,
IlAny)
{
m->setDoubleBuffering(v, !m->isDoubleBuffering(v));
}
int
main(int argc, char* argv[])
{
IlvDisplay* display = new IlvDisplay(“Example”, ““, argc, argv);
if (!display || display->isBad())
{
IlvFatalError(“Can’t open display”);
IlvExit(-1);
}
IlvView* view = new IlvView(display, “ExMan”, “Manager”,
IlvRect(0, 0, 400, 400));
view->setDestroyCallback(Quit);
IlvManager* manager = new IlvManager(display);
manager->addView(view);
manager->addAccelerator(SetDoubleBuffering, IlvKeyUp, ‘b’);
// Description of a map
manager->read(“../hook.ilv”);
// Instantiation of the hook class
ExHook* pHook = new ExHook(manager, view);
// Connect the hook to the manager view
manager->installViewHook(pHook);
manager->setInteractor(new IlvSelectInteractor(manager, view));
IlvMainLoop();
}
Version 6.1
Copyright © 2016, Rogue Wave Software, Inc. All Rights Reserved.