Fixed custom event handlers for CONTEXT_MENU.

Moved menuCopy to CONTEXT_MENU copy constructor.
This commit is contained in:
Maciej Suminski 2014-05-13 11:22:51 +02:00
parent 05ee03d6b0
commit e2feefc08c
2 changed files with 50 additions and 63 deletions

View File

@ -26,22 +26,48 @@
#include <tool/tool_manager.h>
#include <tool/tool_interactive.h>
#include <tool/context_menu.h>
#include <boost/bind.hpp>
#include <cassert>
CONTEXT_MENU::CONTEXT_MENU() :
m_titleSet( false ), m_selected( -1 ), m_tool( NULL )
{
setCustomEventHandler( boost::bind( &CONTEXT_MENU::handleCustomEvent, this, _1 ) );
setupEvents();
}
CONTEXT_MENU::CONTEXT_MENU( const CONTEXT_MENU& aMenu ) :
m_titleSet( aMenu.m_titleSet ), m_selected( -1 ), m_tool( aMenu.m_tool )
m_titleSet( aMenu.m_titleSet ), m_selected( -1 ), m_tool( aMenu.m_tool ),
m_toolActions( aMenu.m_toolActions ), m_customHandler( aMenu.m_customHandler )
{
setupEvents();
// Copy all the menu entries
copyMenu( &aMenu, this );
for( unsigned i = 0; i < aMenu.GetMenuItemCount(); ++i )
{
wxMenuItem* item = aMenu.FindItemByPosition( i );
if( item->IsSubMenu() )
{
#ifdef DEBUG
// Submenus of a CONTEXT_MENU are supposed to be CONTEXT_MENUs as well
assert( dynamic_cast<CONTEXT_MENU*>( item->GetSubMenu() ) );
#endif
CONTEXT_MENU* menu = new CONTEXT_MENU( static_cast<const CONTEXT_MENU&>( *item->GetSubMenu() ) );
AppendSubMenu( menu, item->GetItemLabel(), wxEmptyString );
}
else
{
wxMenuItem* newItem = new wxMenuItem( this, item->GetId(), item->GetItemLabel(),
wxEmptyString, item->GetKind() );
Append( newItem );
copyItem( item, newItem );
}
}
setupEvents();
}
@ -156,10 +182,12 @@ void CONTEXT_MENU::onMenuEvent( wxEvent& aEvent )
}
else
{
OPT_TOOL_EVENT custom = handleCustomEvent( aEvent );
if(custom)
OPT_TOOL_EVENT custom = m_customHandler( aEvent );
if( custom )
evt = *custom;
else {
else
{
// Handling non-action menu entries (e.g. items in clarification list)
evt = TOOL_EVENT( TC_COMMAND, TA_CONTEXT_MENU_CHOICE, aEvent.GetId() );
}
@ -167,47 +195,13 @@ void CONTEXT_MENU::onMenuEvent( wxEvent& aEvent )
}
// forward the action/update event to the TOOL_MANAGER
if( m_tool )
m_tool->GetManager()->ProcessEvent( evt );
}
void CONTEXT_MENU::copyMenu( const CONTEXT_MENU* aParent, CONTEXT_MENU* aTarget ) const
{
// Copy all the menu entries
for( unsigned i = 0; i < aParent->GetMenuItemCount(); ++i )
{
wxMenuItem* item = aParent->FindItemByPosition( i );
if( item->IsSubMenu() )
{
#ifdef DEBUG
// Submenus of a CONTEXT_MENU are supposed to be CONTEXT_MENUs as well
assert( dynamic_cast<CONTEXT_MENU*>( item->GetSubMenu() ) );
#endif
CONTEXT_MENU* menu = new CONTEXT_MENU;
copyMenu( static_cast<const CONTEXT_MENU*>( item->GetSubMenu() ), menu );
aTarget->AppendSubMenu( menu, item->GetItemLabel(), wxT( "" ) );
}
else
{
wxMenuItem* newItem = new wxMenuItem( aTarget, item->GetId(), item->GetItemLabel(),
wxEmptyString, item->GetKind() );
aTarget->Append( newItem );
copyItem( item, newItem );
}
}
// Copy tool actions that are available to choose from context menu
aTarget->m_toolActions = aParent->m_toolActions;
TOOL_MANAGER::Instance().ProcessEvent( evt );
}
void CONTEXT_MENU::copyItem( const wxMenuItem* aSource, wxMenuItem* aDest ) const
{
assert( !aSource->IsSubMenu() );
assert( !aSource->IsSubMenu() ); // it does not transfer submenus
aDest->SetKind( aSource->GetKind() );
aDest->SetHelp( aSource->GetHelp() );
@ -218,12 +212,4 @@ void CONTEXT_MENU::copyItem( const wxMenuItem* aSource, wxMenuItem* aDest ) cons
if( aSource->GetKind() == wxITEM_NORMAL )
aDest->SetBitmap( aSource->GetBitmap() );
if( aSource->IsSubMenu() )
{
CONTEXT_MENU* newMenu = new CONTEXT_MENU;
copyMenu( static_cast<const CONTEXT_MENU*>( aSource->GetSubMenu() ), newMenu );
aDest->SetSubMenu( newMenu );
}
}

View File

@ -28,6 +28,7 @@
#include <wx/menu.h>
#include <tool/tool_action.h>
#include <map>
#include <boost/function.hpp>
class TOOL_INTERACTIVE;
@ -90,28 +91,25 @@ public:
return m_selected;
}
protected:
virtual OPT_TOOL_EVENT handleCustomEvent ( wxEvent& aEvent )
void setCustomEventHandler( boost::function<OPT_TOOL_EVENT(const wxEvent&)> aHandler )
{
m_customHandler = aHandler;
}
virtual OPT_TOOL_EVENT handleCustomEvent(const wxEvent& aEvent )
{
return OPT_TOOL_EVENT();
};
}
private:
/**
* Function copyMenu
* Copies recursively all entries and submenus.
* @param aParent is the source.
* @param aTarget is the destination.
*/
void copyMenu( const CONTEXT_MENU* aParent, CONTEXT_MENU* aTarget ) const;
/**
* Function copyItem
* Copies all properties of a menu entry.
* Copies all properties of a menu entry to another.
*/
void copyItem( const wxMenuItem* aSource, wxMenuItem* aDest ) const;
///> Initializes handlers for events.
void setupEvents();
///> Event handler.
@ -146,6 +144,9 @@ private:
/// Associates tool actions with menu item IDs. Non-owning.
std::map<int, const TOOL_ACTION*> m_toolActions;
/// Custom events handler, allows to translate wxEvents to TOOL_EVENTs.
boost::function<OPT_TOOL_EVENT(const wxEvent& aEvent)> m_customHandler;
};
#endif