Storing the selected item position from a context menu.

This commit is contained in:
Maciej Suminski 2013-11-29 16:45:39 +01:00
parent 3ce3d22b97
commit 4876dbea24
4 changed files with 26 additions and 6 deletions

View File

@ -29,7 +29,7 @@
#include <cassert>
CONTEXT_MENU::CONTEXT_MENU() :
m_titleSet( false ), m_handler( this ), m_tool( NULL )
m_titleSet( false ), m_selected( -1 ), m_handler( this ), m_tool( NULL )
{
m_menu.Connect( wxEVT_MENU_HIGHLIGHT, wxEventHandler( CMEventHandler::onEvent ),
NULL, &m_handler );
@ -43,7 +43,7 @@ CONTEXT_MENU::CONTEXT_MENU() :
CONTEXT_MENU::CONTEXT_MENU( const CONTEXT_MENU& aMenu ) :
m_titleSet( aMenu.m_titleSet ), m_handler( this ), m_tool( aMenu.m_tool )
m_titleSet( aMenu.m_titleSet ), m_selected( -1 ), m_handler( this ), m_tool( aMenu.m_tool )
{
m_menu.Connect( wxEVT_MENU_HIGHLIGHT, wxEventHandler( CMEventHandler::onEvent ),
NULL, &m_handler );
@ -164,6 +164,9 @@ void CONTEXT_MENU::CMEventHandler::onEvent( wxEvent& aEvent )
// One of menu entries was selected..
else if( type == wxEVT_COMMAND_MENU_SELECTED )
{
// Store the selected position
m_menu->m_selected = aEvent.GetId();
// Check if there is a TOOL_ACTION for the given ID
if( m_menu->m_toolActions.count( aEvent.GetId() ) == 1 )
{

View File

@ -147,7 +147,7 @@ bool TOOL_DISPATCHER::handleMouseButton( wxEvent& aEvent, int aIndex, bool aMoti
st->pressed = true;
evt = TOOL_EVENT( TC_MOUSE, TA_MOUSE_DOWN, args );
}
else if( up ) // Handle mouse button release
else if( up ) // Handle mouse button release
{
st->pressed = false;

View File

@ -446,9 +446,12 @@ bool TOOL_MANAGER::ProcessEvent( TOOL_EVENT& aEvent )
boost::scoped_ptr<CONTEXT_MENU> menu( new CONTEXT_MENU( *st->contextMenu ) );
GetEditFrame()->PopupMenu( menu->GetMenu() );
//
TOOL_EVENT evt( TC_COMMAND, TA_CONTEXT_MENU_CHOICE );
dispatchInternal( evt );
// If nothing was chosen from the context menu, we must notify the tool as well
if( menu->GetSelected() < 0 )
{
TOOL_EVENT evt( TC_COMMAND, TA_CONTEXT_MENU_CHOICE );
dispatchInternal( evt );
}
break;
}

View File

@ -78,6 +78,17 @@ public:
*/
void Clear();
/**
* Function GetSelected()
* Returns the position of selected item. If the returned value is negative, that means that
* menu was dismissed.
* @return The position of selected item in the context menu.
*/
int GetSelected() const
{
return m_selected;
}
/**
* Function GetMenu()
* Returns the instance of wxMenu object used to display the menu.
@ -131,6 +142,9 @@ private:
///> Instance of wxMenu used for display of the context menu.
wxMenu m_menu;
///> Stores the id number of selected item.
int m_selected;
///> Instance of menu event handler.
CMEventHandler m_handler;