Commit e2feefc0 authored by Maciej Suminski's avatar Maciej Suminski

Fixed custom event handlers for CONTEXT_MENU.

Moved menuCopy to CONTEXT_MENU copy constructor.
parent 05ee03d6
...@@ -26,22 +26,48 @@ ...@@ -26,22 +26,48 @@
#include <tool/tool_manager.h> #include <tool/tool_manager.h>
#include <tool/tool_interactive.h> #include <tool/tool_interactive.h>
#include <tool/context_menu.h> #include <tool/context_menu.h>
#include <boost/bind.hpp>
#include <cassert> #include <cassert>
CONTEXT_MENU::CONTEXT_MENU() : CONTEXT_MENU::CONTEXT_MENU() :
m_titleSet( false ), m_selected( -1 ), m_tool( NULL ) m_titleSet( false ), m_selected( -1 ), m_tool( NULL )
{ {
setCustomEventHandler( boost::bind( &CONTEXT_MENU::handleCustomEvent, this, _1 ) );
setupEvents(); setupEvents();
} }
CONTEXT_MENU::CONTEXT_MENU( const CONTEXT_MENU& aMenu ) : 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 // 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 ) ...@@ -156,10 +182,12 @@ void CONTEXT_MENU::onMenuEvent( wxEvent& aEvent )
} }
else else
{ {
OPT_TOOL_EVENT custom = handleCustomEvent( aEvent ); OPT_TOOL_EVENT custom = m_customHandler( aEvent );
if(custom)
if( custom )
evt = *custom; evt = *custom;
else { else
{
// Handling non-action menu entries (e.g. items in clarification list) // Handling non-action menu entries (e.g. items in clarification list)
evt = TOOL_EVENT( TC_COMMAND, TA_CONTEXT_MENU_CHOICE, aEvent.GetId() ); evt = TOOL_EVENT( TC_COMMAND, TA_CONTEXT_MENU_CHOICE, aEvent.GetId() );
} }
...@@ -167,47 +195,13 @@ void CONTEXT_MENU::onMenuEvent( wxEvent& aEvent ) ...@@ -167,47 +195,13 @@ void CONTEXT_MENU::onMenuEvent( wxEvent& aEvent )
} }
// forward the action/update event to the TOOL_MANAGER // forward the action/update event to the TOOL_MANAGER
if( m_tool ) TOOL_MANAGER::Instance().ProcessEvent( evt );
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;
} }
void CONTEXT_MENU::copyItem( const wxMenuItem* aSource, wxMenuItem* aDest ) const 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->SetKind( aSource->GetKind() );
aDest->SetHelp( aSource->GetHelp() ); aDest->SetHelp( aSource->GetHelp() );
...@@ -218,12 +212,4 @@ void CONTEXT_MENU::copyItem( const wxMenuItem* aSource, wxMenuItem* aDest ) cons ...@@ -218,12 +212,4 @@ void CONTEXT_MENU::copyItem( const wxMenuItem* aSource, wxMenuItem* aDest ) cons
if( aSource->GetKind() == wxITEM_NORMAL ) if( aSource->GetKind() == wxITEM_NORMAL )
aDest->SetBitmap( aSource->GetBitmap() ); 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 );
}
} }
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <wx/menu.h> #include <wx/menu.h>
#include <tool/tool_action.h> #include <tool/tool_action.h>
#include <map> #include <map>
#include <boost/function.hpp>
class TOOL_INTERACTIVE; class TOOL_INTERACTIVE;
...@@ -90,28 +91,25 @@ public: ...@@ -90,28 +91,25 @@ public:
return m_selected; return m_selected;
} }
protected: 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(); return OPT_TOOL_EVENT();
}; }
private: 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 * 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; void copyItem( const wxMenuItem* aSource, wxMenuItem* aDest ) const;
///> Initializes handlers for events.
void setupEvents(); void setupEvents();
///> Event handler. ///> Event handler.
...@@ -146,6 +144,9 @@ private: ...@@ -146,6 +144,9 @@ private:
/// Associates tool actions with menu item IDs. Non-owning. /// Associates tool actions with menu item IDs. Non-owning.
std::map<int, const TOOL_ACTION*> m_toolActions; 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 #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment