2013-08-07 09:20:12 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
#include <tool/tool_event.h>
|
|
|
|
#include <tool/tool_manager.h>
|
|
|
|
#include <tool/tool_interactive.h>
|
|
|
|
#include <tool/context_menu.h>
|
2014-05-13 09:22:51 +00:00
|
|
|
#include <boost/bind.hpp>
|
2013-09-26 12:09:18 +00:00
|
|
|
#include <cassert>
|
2013-08-02 14:46:53 +00:00
|
|
|
|
2013-09-26 12:09:18 +00:00
|
|
|
CONTEXT_MENU::CONTEXT_MENU() :
|
2014-05-13 09:22:51 +00:00
|
|
|
m_titleSet( false ), m_selected( -1 ), m_tool( NULL )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2014-05-13 09:22:51 +00:00
|
|
|
setCustomEventHandler( boost::bind( &CONTEXT_MENU::handleCustomEvent, this, _1 ) );
|
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
setupEvents();
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 09:20:12 +00:00
|
|
|
|
2013-09-26 12:09:18 +00:00
|
|
|
CONTEXT_MENU::CONTEXT_MENU( const CONTEXT_MENU& aMenu ) :
|
2014-05-13 09:22:51 +00:00
|
|
|
m_titleSet( aMenu.m_titleSet ), m_selected( -1 ), m_tool( aMenu.m_tool ),
|
|
|
|
m_toolActions( aMenu.m_toolActions ), m_customHandler( aMenu.m_customHandler )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2013-09-26 12:09:18 +00:00
|
|
|
// Copy all the menu entries
|
2014-05-13 09:22:51 +00:00
|
|
|
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();
|
2014-05-13 09:22:51 +00:00
|
|
|
}
|
2013-09-26 16:38:58 +00:00
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
|
2014-07-09 13:10:32 +00:00
|
|
|
CONTEXT_MENU& CONTEXT_MENU::operator=( const CONTEXT_MENU& aMenu )
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
m_titleSet = aMenu.m_titleSet;
|
|
|
|
m_selected = aMenu.m_selected;
|
|
|
|
m_tool = aMenu.m_tool;
|
|
|
|
m_toolActions = aMenu.m_toolActions;
|
|
|
|
m_customHandler = aMenu.m_customHandler;
|
|
|
|
|
|
|
|
// Copy all the menu entries
|
|
|
|
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();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
void CONTEXT_MENU::setupEvents()
|
|
|
|
{
|
2015-01-25 18:11:02 +00:00
|
|
|
Connect( wxEVT_MENU_HIGHLIGHT, wxMenuEventHandler( CONTEXT_MENU::onMenuEvent ), NULL, this );
|
|
|
|
Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( CONTEXT_MENU::onMenuEvent ), NULL, this );
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 09:20:12 +00:00
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
void CONTEXT_MENU::SetTitle( const wxString& aTitle )
|
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
// TODO handle an empty string (remove title and separator)
|
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
// Unfortunately wxMenu::SetTitle() does nothing.. (at least wxGTK)
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( m_titleSet )
|
|
|
|
{
|
2014-05-13 09:22:51 +00:00
|
|
|
FindItemByPosition( 0 )->SetItemLabel( aTitle );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-13 09:22:51 +00:00
|
|
|
InsertSeparator( 0 );
|
2015-02-06 21:01:57 +00:00
|
|
|
Insert( 0, new wxMenuItem( this, wxID_NONE, aTitle, wxEmptyString, wxITEM_NORMAL ) );
|
2013-09-26 12:09:18 +00:00
|
|
|
m_titleSet = true;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 09:20:12 +00:00
|
|
|
|
2013-09-26 12:09:18 +00:00
|
|
|
void CONTEXT_MENU::Add( const wxString& aLabel, int aId )
|
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
#ifdef DEBUG
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
if( FindItem( aId ) != NULL )
|
2013-09-27 14:23:43 +00:00
|
|
|
wxLogWarning( wxT( "Adding more than one menu entry with the same ID may result in"
|
|
|
|
"undefined behaviour" ) );
|
|
|
|
#endif
|
2014-05-13 09:22:51 +00:00
|
|
|
Append( new wxMenuItem( this, aId, aLabel, wxEmptyString, wxITEM_NORMAL ) );
|
2013-09-26 12:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-26 16:38:58 +00:00
|
|
|
void CONTEXT_MENU::Add( const TOOL_ACTION& aAction )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
/// ID numbers for tool actions need to have a value higher than m_actionId
|
2013-09-26 16:38:58 +00:00
|
|
|
int id = m_actionId + aAction.GetId();
|
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
wxMenuItem* item = new wxMenuItem( this, id,
|
2014-05-13 09:22:50 +00:00
|
|
|
wxString( aAction.GetMenuItem().c_str(), wxConvUTF8 ),
|
|
|
|
wxString( aAction.GetDescription().c_str(), wxConvUTF8 ), wxITEM_NORMAL );
|
2013-09-26 16:38:58 +00:00
|
|
|
|
2014-05-13 09:22:50 +00:00
|
|
|
if( aAction.HasHotKey() )
|
|
|
|
{
|
|
|
|
int key = aAction.GetHotKey() & ~MD_MODIFIER_MASK;
|
|
|
|
int mod = aAction.GetHotKey() & MD_MODIFIER_MASK;
|
2014-05-13 09:22:51 +00:00
|
|
|
int flags = wxACCEL_NORMAL;
|
2014-05-13 09:22:50 +00:00
|
|
|
|
|
|
|
switch( mod )
|
|
|
|
{
|
|
|
|
case MD_ALT: flags = wxACCEL_ALT; break;
|
|
|
|
case MD_CTRL: flags = wxACCEL_CTRL; break;
|
|
|
|
case MD_SHIFT: flags = wxACCEL_SHIFT; break;
|
|
|
|
}
|
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
wxAcceleratorEntry accel( flags, key, id, item );
|
|
|
|
item->SetAccel( &accel );
|
2014-05-13 09:22:50 +00:00
|
|
|
}
|
2013-09-26 16:38:58 +00:00
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
Append( item );
|
2013-09-26 16:38:58 +00:00
|
|
|
m_toolActions[id] = &aAction;
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 09:20:12 +00:00
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
void CONTEXT_MENU::Clear()
|
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
m_titleSet = false;
|
2013-09-26 12:09:18 +00:00
|
|
|
|
2014-07-09 13:10:32 +00:00
|
|
|
GetMenuItems().DeleteContents( true );
|
|
|
|
GetMenuItems().Clear();
|
2013-10-14 11:43:57 +00:00
|
|
|
m_toolActions.clear();
|
2014-07-09 13:10:32 +00:00
|
|
|
GetMenuItems().DeleteContents( false ); // restore the default so destructor does not go wild
|
|
|
|
|
|
|
|
assert( GetMenuItemCount() == 0 );
|
2013-09-26 12:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-25 18:11:02 +00:00
|
|
|
void CONTEXT_MENU::onMenuEvent( wxMenuEvent& aEvent )
|
2013-09-26 12:09:18 +00:00
|
|
|
{
|
2014-05-13 09:22:51 +00:00
|
|
|
OPT_TOOL_EVENT evt;
|
|
|
|
|
2013-09-26 12:09:18 +00:00
|
|
|
wxEventType type = aEvent.GetEventType();
|
|
|
|
|
2013-09-27 16:51:21 +00:00
|
|
|
// When the currently chosen item in the menu is changed, an update event is issued.
|
|
|
|
// For example, the selection tool can use this to dynamically highlight the current item
|
|
|
|
// from selection clarification popup.
|
2013-09-26 12:09:18 +00:00
|
|
|
if( type == wxEVT_MENU_HIGHLIGHT )
|
2013-10-14 18:40:36 +00:00
|
|
|
evt = TOOL_EVENT( TC_COMMAND, TA_CONTEXT_MENU_UPDATE, aEvent.GetId() );
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-09-27 16:51:21 +00:00
|
|
|
// One of menu entries was selected..
|
2013-09-26 12:09:18 +00:00
|
|
|
else if( type == wxEVT_COMMAND_MENU_SELECTED )
|
2013-09-26 16:38:58 +00:00
|
|
|
{
|
2013-11-29 15:45:39 +00:00
|
|
|
// Store the selected position
|
2014-05-13 09:22:51 +00:00
|
|
|
m_selected = aEvent.GetId();
|
2013-11-29 15:45:39 +00:00
|
|
|
|
2013-09-27 16:51:21 +00:00
|
|
|
// Check if there is a TOOL_ACTION for the given ID
|
2014-05-13 09:22:51 +00:00
|
|
|
if( m_toolActions.count( aEvent.GetId() ) == 1 )
|
2013-09-26 16:38:58 +00:00
|
|
|
{
|
2014-05-13 09:22:51 +00:00
|
|
|
evt = m_toolActions[aEvent.GetId()]->MakeEvent();
|
2013-09-26 16:38:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-25 18:11:02 +00:00
|
|
|
// Under Linux, every submenu can have a separate event handler, under
|
|
|
|
// Windows all submenus are handled by the main menu.
|
|
|
|
#ifdef __WINDOWS__
|
2015-02-11 10:10:53 +00:00
|
|
|
if( !evt )
|
|
|
|
{
|
2015-01-25 18:11:02 +00:00
|
|
|
// Try to find the submenu which holds the selected item
|
2015-02-10 23:23:45 +00:00
|
|
|
wxMenu* menu = NULL;
|
2015-01-25 18:11:02 +00:00
|
|
|
FindItem( m_selected, &menu );
|
|
|
|
|
2015-02-10 23:23:45 +00:00
|
|
|
if( menu && menu != this )
|
2015-01-25 18:11:02 +00:00
|
|
|
{
|
|
|
|
menu->ProcessEvent( aEvent );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 23:23:45 +00:00
|
|
|
#endif
|
2014-05-13 09:22:51 +00:00
|
|
|
evt = m_customHandler( aEvent );
|
2014-05-13 09:22:51 +00:00
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
// Handling non-action menu entries (e.g. items in clarification list)
|
|
|
|
if( !evt )
|
2014-05-13 09:22:51 +00:00
|
|
|
evt = TOOL_EVENT( TC_COMMAND, TA_CONTEXT_MENU_CHOICE, aEvent.GetId() );
|
2013-09-26 16:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-26 12:09:18 +00:00
|
|
|
|
2014-05-14 14:29:53 +00:00
|
|
|
assert( m_tool ); // without tool & tool manager we cannot handle events
|
|
|
|
|
2013-09-27 16:51:21 +00:00
|
|
|
// forward the action/update event to the TOOL_MANAGER
|
2014-05-14 14:29:53 +00:00
|
|
|
if( evt && m_tool )
|
|
|
|
m_tool->GetManager()->ProcessEvent( *evt );
|
2014-05-13 09:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-31 14:04:10 +00:00
|
|
|
void CONTEXT_MENU::setTool( TOOL_INTERACTIVE* aTool )
|
|
|
|
{
|
|
|
|
m_tool = aTool;
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < GetMenuItemCount(); ++i )
|
|
|
|
{
|
|
|
|
wxMenuItem* item = FindItemByPosition( i );
|
|
|
|
|
|
|
|
if( item->IsSubMenu() )
|
|
|
|
{
|
|
|
|
CONTEXT_MENU* menu = static_cast<CONTEXT_MENU*>( item->GetSubMenu() );
|
|
|
|
menu->setTool( aTool );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
void CONTEXT_MENU::copyItem( const wxMenuItem* aSource, wxMenuItem* aDest ) const
|
|
|
|
{
|
2014-05-13 09:22:51 +00:00
|
|
|
assert( !aSource->IsSubMenu() ); // it does not transfer submenus
|
2014-05-13 09:22:51 +00:00
|
|
|
|
|
|
|
aDest->SetKind( aSource->GetKind() );
|
|
|
|
aDest->SetHelp( aSource->GetHelp() );
|
|
|
|
aDest->Enable( aSource->IsEnabled() );
|
|
|
|
|
|
|
|
if( aSource->IsCheckable() )
|
|
|
|
aDest->Check( aSource->IsChecked() );
|
|
|
|
|
|
|
|
if( aSource->GetKind() == wxITEM_NORMAL )
|
|
|
|
aDest->SetBitmap( aSource->GetBitmap() );
|
2013-08-07 09:20:12 +00:00
|
|
|
}
|