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>
|
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() :
|
|
|
|
m_titleSet( false ), m_handler( this ), m_tool( NULL )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
m_menu.Connect( wxEVT_MENU_HIGHLIGHT, wxEventHandler( CMEventHandler::onEvent ),
|
|
|
|
NULL, &m_handler );
|
|
|
|
m_menu.Connect( wxEVT_COMMAND_MENU_SELECTED, wxEventHandler( CMEventHandler::onEvent ),
|
|
|
|
NULL, &m_handler );
|
|
|
|
|
|
|
|
// Workaround for the case when mouse cursor never reaches menu (it hangs up tools using menu)
|
|
|
|
wxMenuEvent menuEvent( wxEVT_MENU_HIGHLIGHT, -1, &m_menu );
|
|
|
|
m_menu.AddPendingEvent( menuEvent );
|
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 ) :
|
|
|
|
m_titleSet( aMenu.m_titleSet ), m_handler( this ), m_tool( aMenu.m_tool )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2013-09-26 12:09:18 +00:00
|
|
|
m_menu.Connect( wxEVT_MENU_HIGHLIGHT, wxEventHandler( CMEventHandler::onEvent ),
|
|
|
|
NULL, &m_handler );
|
|
|
|
m_menu.Connect( wxEVT_COMMAND_MENU_SELECTED, wxEventHandler( CMEventHandler::onEvent ),
|
|
|
|
NULL, &m_handler );
|
|
|
|
|
|
|
|
// Workaround for the case when mouse cursor never reaches menu (it hangs up tools using menu)
|
2013-09-26 12:29:47 +00:00
|
|
|
wxMenuEvent menuEvent( wxEVT_MENU_HIGHLIGHT, -1, &m_menu );
|
2013-09-26 12:09:18 +00:00
|
|
|
m_menu.AddPendingEvent( menuEvent );
|
|
|
|
|
|
|
|
// Copy all the menu entries
|
|
|
|
for( unsigned i = 0; i < aMenu.m_menu.GetMenuItemCount(); ++i )
|
|
|
|
{
|
|
|
|
wxMenuItem* item = aMenu.m_menu.FindItemByPosition( i );
|
|
|
|
m_menu.Append( new wxMenuItem( &m_menu, item->GetId(), item->GetItemLabel(),
|
|
|
|
wxEmptyString, wxITEM_NORMAL ) );
|
|
|
|
}
|
2013-09-26 16:38:58 +00:00
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
// Copy tool actions that are available to choose from context menu
|
2013-09-26 16:38:58 +00:00
|
|
|
m_toolActions = aMenu.m_toolActions;
|
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)
|
|
|
|
|
2013-09-09 15:12:03 +00:00
|
|
|
// Unfortunately wxMenu::SetTitle() does nothing..
|
2013-10-14 11:43:57 +00:00
|
|
|
if( m_titleSet )
|
|
|
|
{
|
2013-09-26 12:09:18 +00:00
|
|
|
m_menu.FindItemByPosition( 0 )->SetItemLabel( aTitle );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-26 12:09:18 +00:00
|
|
|
m_menu.InsertSeparator( 0 );
|
|
|
|
m_menu.Insert( 0, new wxMenuItem( &m_menu, -1, aTitle, wxEmptyString, wxITEM_NORMAL ) );
|
|
|
|
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
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
if( m_menu.FindItem( aId ) != NULL )
|
|
|
|
wxLogWarning( wxT( "Adding more than one menu entry with the same ID may result in"
|
|
|
|
"undefined behaviour" ) );
|
|
|
|
#endif
|
2013-10-14 11:43:57 +00:00
|
|
|
m_menu.Append( new wxMenuItem( &m_menu, 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();
|
|
|
|
wxString menuEntry;
|
|
|
|
|
|
|
|
if( aAction.HasHotKey() )
|
2013-10-14 18:40:36 +00:00
|
|
|
menuEntry = wxString( ( aAction.GetMenuItem() + '\t' +
|
|
|
|
getHotKeyDescription( aAction ) ).c_str(), wxConvUTF8 );
|
2013-09-26 16:38:58 +00:00
|
|
|
else
|
2013-09-29 10:57:20 +00:00
|
|
|
menuEntry = wxString( aAction.GetMenuItem().c_str(), wxConvUTF8 );
|
2013-09-26 16:38:58 +00:00
|
|
|
|
|
|
|
m_menu.Append( new wxMenuItem( &m_menu, id, menuEntry,
|
2013-09-29 10:57:20 +00:00
|
|
|
wxString( aAction.GetDescription().c_str(), wxConvUTF8 ), wxITEM_NORMAL ) );
|
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
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
// Remove all the entries from context menu
|
|
|
|
for( unsigned i = 0; i < m_menu.GetMenuItemCount(); ++i )
|
|
|
|
m_menu.Destroy( m_menu.FindItemByPosition( 0 ) );
|
2013-09-26 16:38:58 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
m_toolActions.clear();
|
2013-09-26 12:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string CONTEXT_MENU::getHotKeyDescription( const TOOL_ACTION& aAction ) const
|
|
|
|
{
|
|
|
|
int hotkey = aAction.GetHotKey();
|
|
|
|
|
|
|
|
std::string description = "";
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
if( hotkey & MD_ALT )
|
2013-09-26 12:09:18 +00:00
|
|
|
description += "ALT+";
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
if( hotkey & MD_CTRL )
|
2013-09-26 12:09:18 +00:00
|
|
|
description += "CTRL+";
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
if( hotkey & MD_SHIFT )
|
2013-09-26 12:09:18 +00:00
|
|
|
description += "SHIFT+";
|
|
|
|
|
|
|
|
// TODO dispatch keys such as Fx, TAB, PG_UP/DN, HOME, END, etc.
|
2013-10-14 18:40:36 +00:00
|
|
|
description += char( hotkey & ~MD_MODIFIER_MASK );
|
2013-09-26 12:09:18 +00:00
|
|
|
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CONTEXT_MENU::CMEventHandler::onEvent( wxEvent& aEvent )
|
|
|
|
{
|
|
|
|
TOOL_EVENT evt;
|
|
|
|
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-09-27 16:51:21 +00:00
|
|
|
// Check if there is a TOOL_ACTION for the given ID
|
|
|
|
if( m_menu->m_toolActions.count( aEvent.GetId() ) == 1 )
|
2013-09-26 16:38:58 +00:00
|
|
|
{
|
2013-09-27 16:51:21 +00:00
|
|
|
evt = m_menu->m_toolActions[aEvent.GetId()]->MakeEvent();
|
2013-09-26 16:38:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-27 16:51:21 +00:00
|
|
|
// Handling non-action menu entries (e.g. items in clarification list)
|
2013-10-14 18:40:36 +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
|
|
|
|
2013-09-27 16:51:21 +00:00
|
|
|
// forward the action/update event to the TOOL_MANAGER
|
2013-09-26 12:09:18 +00:00
|
|
|
if( m_menu->m_tool )
|
|
|
|
m_menu->m_tool->GetManager()->ProcessEvent( evt );
|
2013-08-07 09:20:12 +00:00
|
|
|
}
|