Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions

MenuHandler Class Reference

List of all members.

Public Member Functions

 MenuHandler (int id)
virtual bool executeMenu ()=0
int menuID () const
void setMenuID (int newid)
bool isHandled () const
void setIsHandled (bool)

Public Attributes

Notifier< MenuHandlercreatenotifier
CNotifier< MenuHandler, int > handlenotifier

Protected Member Functions

void assignItemID (MenuItem &)

Protected Attributes

int freeid_
int id_
bool ishandled_

Private Member Functions

 mRefCountImpl (MenuHandler)

Detailed Description

A generic representation of a menu. It allows anyone to add their own custom menuitems to it. The principle is that the menu triggers it's createnotifier just before the menu should be displayed, and the application adds the items it wants into the menu. When the user has clicked on something in the menu, the handlenotifier is triggered and the application checks what should be done.

Usage:

    menu->createnotifier.notify( mCB(this,myclass,createMenuCB) );
    menu->handlenotifier.notify( mCB(this,myclass,handleMenuCB) );

Upon a create notification, your class might do something like this:

    void myclass::createMenuCB( CallBacker* callback )
    {
        mDynamicCastGet( MenuHandler*, menu, callback );

        mAddMenuItem( menu, &mymenuitem, true, false );
        mAddMenuItem( menu, &mysubmenu, true, false );
        mAddMenuItem( &mysubmenu, &mysubmenuitem1, true, false );
        mAddMenuItem( &mysubmenu, &mysubmenuitem2, true, false );
    }

The code will make a menu with two items, and the second item will have a submenu with two items. The first boolean says whether the item should be enabled, the second one says where there should be a check before it.

The menuitems are instantiations of MenuItem and should be stored in your class. They hold information about the item itself (like text, enabled or not enabled, checked or not checked, information on where in the menu it should be placed. In addition, it has an unique id that is set when the item is inserted into the menu.

Upon a handle notification, your class might do something like this:

    void myclass::handleMenuCB(CallBacker* callback )
    {
        mCBCapsuleUnpackWithCaller( int, mnuid, caller, callback );
        mDynamicCastGet( MenuHandler*, menu, caller );
        if ( mnuid==-1 || menu->isHandled() )
            return;

        bool ishandled = true;
        if ( mnuid==mymenuitem.id )
            do_something();
        else if ( mnuid==mysubmenuitem1.id )
            do_something_else();
        else if ( mnuid==mymenusubitem2.id )
            do_something_else();
        else
            ishandled = false;

        menu->setIsHandled(ishandled);
    }

Constructor & Destructor Documentation

MenuHandler::MenuHandler ( int  id  ) 

Member Function Documentation

void MenuHandler::assignItemID ( MenuItem  )  [protected]
virtual bool MenuHandler::executeMenu (  )  [pure virtual]
bool MenuHandler::isHandled (  )  const

Should be called as the first thing from callbacks that is triggered from uiMenuHandler::handlenotifier. If isHandled() returns true, the callback should return immediately.

int MenuHandler::menuID (  )  const [inline]
MenuHandler::mRefCountImpl ( MenuHandler   )  [private]
void MenuHandler::setIsHandled ( bool   ) 

Should be called from callbacks that are triggered from uiMenuHandler::handlenotifier if they have found the menu id they are looking for.

void MenuHandler::setMenuID ( int  newid  )  [inline]

Member Data Documentation

int MenuHandler::freeid_ [protected]
int MenuHandler::id_ [protected]
bool MenuHandler::ishandled_ [protected]