2013-08-07 09:20:12 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2019-08-14 08:28:07 +00:00
|
|
|
* Copyright (C) 2013-2019 CERN
|
2020-08-18 14:17:16 +00:00
|
|
|
* Copyright (C) 2013-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-08-07 09:20:12 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2015-04-30 08:46:05 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
2013-08-07 09:20:12 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-07-27 14:11:41 +00:00
|
|
|
#include <eda_base_frame.h>
|
2019-05-13 20:42:40 +00:00
|
|
|
#include <functional>
|
2019-07-27 14:11:41 +00:00
|
|
|
#include <id.h>
|
2020-07-27 22:42:23 +00:00
|
|
|
#include <kiface_i.h>
|
2019-07-27 14:11:41 +00:00
|
|
|
#include <menus_helpers.h>
|
|
|
|
#include <tool/action_menu.h>
|
2019-05-13 20:42:40 +00:00
|
|
|
#include <tool/actions.h>
|
2013-08-02 14:46:53 +00:00
|
|
|
#include <tool/tool_event.h>
|
|
|
|
#include <tool/tool_interactive.h>
|
2019-07-27 14:11:41 +00:00
|
|
|
#include <tool/tool_manager.h>
|
|
|
|
#include <trace_helpers.h>
|
2017-02-22 16:54:01 +00:00
|
|
|
#include <wx/log.h>
|
2020-05-30 11:31:19 +00:00
|
|
|
#include <wx/stc/stc.h>
|
|
|
|
#include <textentry_tricks.h>
|
2020-05-30 19:00:09 +00:00
|
|
|
#include <wx/listctrl.h>
|
2019-05-13 20:42:40 +00:00
|
|
|
|
2016-06-29 10:23:11 +00:00
|
|
|
using namespace std::placeholders;
|
2013-08-02 14:46:53 +00:00
|
|
|
|
2019-05-13 20:42:40 +00:00
|
|
|
|
2020-07-27 22:42:23 +00:00
|
|
|
ACTION_MENU::ACTION_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool ) :
|
2019-05-22 21:11:05 +00:00
|
|
|
m_dirty( true ),
|
2019-05-13 20:42:40 +00:00
|
|
|
m_titleDisplayed( false ),
|
2019-06-15 00:29:42 +00:00
|
|
|
m_isContextMenu( isContextMenu ),
|
|
|
|
m_icon( nullptr ),
|
2019-05-13 20:42:40 +00:00
|
|
|
m_selected( -1 ),
|
2020-07-27 22:42:23 +00:00
|
|
|
m_tool( aTool )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
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
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU::~ACTION_MENU()
|
2015-04-30 08:46:05 +00:00
|
|
|
{
|
|
|
|
// Set parent to NULL to prevent submenus from unregistering from a notexisting object
|
2017-01-20 17:33:11 +00:00
|
|
|
for( auto menu : m_submenus )
|
2017-01-23 08:59:36 +00:00
|
|
|
menu->SetParent( nullptr );
|
2015-04-30 08:46:05 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* parent = dynamic_cast<ACTION_MENU*>( GetParent() );
|
2017-01-23 08:59:36 +00:00
|
|
|
|
|
|
|
if( parent )
|
|
|
|
parent->m_submenus.remove( this );
|
2015-04-30 08:46:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 16:48:03 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::SetIcon( const BITMAP_OPAQUE* aIcon )
|
2017-10-23 16:48:03 +00:00
|
|
|
{
|
2017-10-23 17:11:32 +00:00
|
|
|
m_icon = aIcon;
|
2017-10-23 16:48:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 08:46:05 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::setupEvents()
|
2014-05-13 09:22:51 +00:00
|
|
|
{
|
2020-03-24 01:01:23 +00:00
|
|
|
// See wxWidgets hack in TOOL_DISPATCHER::DispatchWxEvent().
|
2019-05-19 20:59:28 +00:00
|
|
|
// Connect( wxEVT_MENU_OPEN, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), NULL, this );
|
|
|
|
// Connect( wxEVT_MENU_HIGHLIGHT, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), NULL, this );
|
2019-07-08 09:40:44 +00:00
|
|
|
// Connect( wxEVT_MENU_CLOSE, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), NULL, this );
|
2019-05-19 20:59:28 +00:00
|
|
|
|
|
|
|
Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), NULL, this );
|
2019-07-08 09:40:44 +00:00
|
|
|
Connect( wxEVT_IDLE, wxIdleEventHandler( ACTION_MENU::OnIdle ), NULL, this );
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 09:20:12 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::SetTitle( const wxString& aTitle )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2017-02-09 22:25:58 +00:00
|
|
|
// Unfortunately wxMenu::SetTitle() does not work very well, so this is an alternative version
|
|
|
|
m_title = aTitle;
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2017-01-23 13:47:49 +00:00
|
|
|
// Update the menu title
|
|
|
|
if( m_titleDisplayed )
|
|
|
|
DisplayTitle( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::DisplayTitle( bool aDisplay )
|
2017-01-23 13:47:49 +00:00
|
|
|
{
|
2017-02-09 22:25:58 +00:00
|
|
|
if( ( !aDisplay || m_title.IsEmpty() ) && m_titleDisplayed )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2017-01-23 13:47:49 +00:00
|
|
|
// Destroy the menu entry keeping the title..
|
2017-02-09 22:25:58 +00:00
|
|
|
wxMenuItem* item = FindItemByPosition( 0 );
|
2017-02-10 07:33:48 +00:00
|
|
|
wxASSERT( item->GetItemLabelText() == GetTitle() );
|
2017-02-09 22:25:58 +00:00
|
|
|
Destroy( item );
|
2017-01-23 13:47:49 +00:00
|
|
|
// ..and separator
|
2017-02-09 22:25:58 +00:00
|
|
|
item = FindItemByPosition( 0 );
|
|
|
|
wxASSERT( item->IsSeparator() );
|
|
|
|
Destroy( item );
|
2017-01-23 13:47:49 +00:00
|
|
|
m_titleDisplayed = false;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2017-01-23 13:47:49 +00:00
|
|
|
|
2017-02-09 22:25:58 +00:00
|
|
|
else if( aDisplay && !m_title.IsEmpty() )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2017-01-23 13:47:49 +00:00
|
|
|
if( m_titleDisplayed )
|
|
|
|
{
|
|
|
|
// Simply update the title
|
2017-02-09 22:25:58 +00:00
|
|
|
FindItemByPosition( 0 )->SetItemLabel( m_title );
|
2017-01-23 13:47:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Add a separator and a menu entry to display the title
|
|
|
|
InsertSeparator( 0 );
|
2017-02-09 22:25:58 +00:00
|
|
|
Insert( 0, new wxMenuItem( this, wxID_NONE, m_title, wxEmptyString, wxITEM_NORMAL ) );
|
2017-12-13 18:42:33 +00:00
|
|
|
|
|
|
|
if( m_icon )
|
2019-05-21 15:50:05 +00:00
|
|
|
AddBitmapToMenuItem( FindItemByPosition( 0 ), KiBitmap( m_icon ) );
|
2017-12-13 18:42:33 +00:00
|
|
|
|
2017-01-23 13:47:49 +00:00
|
|
|
m_titleDisplayed = 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
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
wxMenuItem* ACTION_MENU::Add( const wxString& aLabel, int aId, const BITMAP_OPAQUE* aIcon )
|
2013-09-26 12:09:18 +00:00
|
|
|
{
|
2019-05-18 10:27:36 +00:00
|
|
|
wxASSERT_MSG( FindItem( aId ) == nullptr, "Duplicate menu IDs!" );
|
2015-04-30 08:46:05 +00:00
|
|
|
|
2015-04-02 14:09:48 +00:00
|
|
|
wxMenuItem* item = new wxMenuItem( this, aId, aLabel, wxEmptyString, wxITEM_NORMAL );
|
2019-05-21 15:50:05 +00:00
|
|
|
|
|
|
|
if( aIcon )
|
|
|
|
AddBitmapToMenuItem( item, KiBitmap( aIcon ) );
|
2015-04-02 14:09:48 +00:00
|
|
|
|
2015-04-30 08:46:04 +00:00
|
|
|
return Append( item );
|
2013-09-26 12:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-18 10:27:36 +00:00
|
|
|
wxMenuItem* ACTION_MENU::Add( const wxString& aLabel, const wxString& aTooltip, int aId,
|
2020-07-27 22:42:23 +00:00
|
|
|
const BITMAP_OPAQUE* aIcon, bool aIsCheckmarkEntry )
|
2019-05-18 10:27:36 +00:00
|
|
|
{
|
|
|
|
wxASSERT_MSG( FindItem( aId ) == nullptr, "Duplicate menu IDs!" );
|
|
|
|
|
2020-07-27 22:42:23 +00:00
|
|
|
wxMenuItem* item = new wxMenuItem( this, aId, aLabel, aTooltip,
|
|
|
|
aIsCheckmarkEntry ? wxITEM_CHECK : wxITEM_NORMAL );
|
2019-05-21 15:50:05 +00:00
|
|
|
|
|
|
|
if( aIcon )
|
|
|
|
AddBitmapToMenuItem( item, KiBitmap( aIcon ) );
|
2019-05-18 10:27:36 +00:00
|
|
|
|
|
|
|
return Append( item );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
wxMenuItem* ACTION_MENU::Add( const TOOL_ACTION& aAction, bool aIsCheckmarkEntry )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2020-07-23 23:47:19 +00:00
|
|
|
/// ID numbers for tool actions are assigned above ACTION_BASE_UI_ID inside TOOL_EVENT
|
2015-04-02 14:09:48 +00:00
|
|
|
const BITMAP_OPAQUE* icon = aAction.GetIcon();
|
2013-09-26 16:38:58 +00:00
|
|
|
|
2020-07-23 23:47:19 +00:00
|
|
|
wxMenuItem* item = new wxMenuItem( this, aAction.GetUIId(), aAction.GetMenuItem(),
|
2019-05-13 20:42:40 +00:00
|
|
|
aAction.GetDescription(),
|
|
|
|
aIsCheckmarkEntry ? wxITEM_CHECK : wxITEM_NORMAL );
|
2019-05-21 15:50:05 +00:00
|
|
|
if( icon )
|
|
|
|
AddBitmapToMenuItem( item, KiBitmap( icon ) );
|
2015-04-02 14:09:48 +00:00
|
|
|
|
2020-07-23 23:47:19 +00:00
|
|
|
m_toolActions[aAction.GetUIId()] = &aAction;
|
2015-04-30 08:46:04 +00:00
|
|
|
|
2019-08-16 10:08:43 +00:00
|
|
|
return Append( item );
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 09:20:12 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
wxMenuItem* ACTION_MENU::Add( ACTION_MENU* aMenu )
|
2015-04-02 14:09:48 +00:00
|
|
|
{
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* menuCopy = aMenu->Clone();
|
2015-07-24 07:42:46 +00:00
|
|
|
m_submenus.push_back( menuCopy );
|
2015-04-30 08:46:04 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
wxASSERT_MSG( !menuCopy->m_title.IsEmpty(), "Set a title for ACTION_MENU using SetTitle()" );
|
2019-05-13 20:42:40 +00:00
|
|
|
|
|
|
|
if( aMenu->m_icon )
|
2015-04-02 14:09:48 +00:00
|
|
|
{
|
2019-05-13 20:42:40 +00:00
|
|
|
wxMenuItem* newItem = new wxMenuItem( this, -1, menuCopy->m_title );
|
2019-05-21 15:50:05 +00:00
|
|
|
AddBitmapToMenuItem( newItem, KiBitmap( aMenu->m_icon ) );
|
2019-05-13 20:42:40 +00:00
|
|
|
newItem->SetSubMenu( menuCopy );
|
|
|
|
return Append( newItem );
|
2015-04-02 14:09:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-13 20:42:40 +00:00
|
|
|
return AppendSubMenu( menuCopy, menuCopy->m_title );
|
2015-04-02 14:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-27 22:42:23 +00:00
|
|
|
void ACTION_MENU::AddClose( wxString aAppname )
|
|
|
|
{
|
2020-11-04 01:28:55 +00:00
|
|
|
Add( _( "Close" ) + "\tCtrl+W",
|
|
|
|
wxString::Format( _( "Close %s" ), aAppname ),
|
2020-07-27 22:42:23 +00:00
|
|
|
wxID_CLOSE,
|
|
|
|
exit_xpm );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ACTION_MENU::AddQuitOrClose( KIFACE_I* aKiface, wxString aAppname )
|
|
|
|
{
|
|
|
|
if( !aKiface || aKiface->IsSingle() ) // not when under a project mgr
|
|
|
|
{
|
|
|
|
// Don't use ACTIONS::quit; wxWidgets moves this on OSX and expects to find it via
|
|
|
|
// wxID_EXIT
|
|
|
|
Add( _( "Quit" ),
|
2020-11-04 01:28:55 +00:00
|
|
|
wxString::Format( _( "Quit %s" ), aAppname ),
|
2020-07-27 22:42:23 +00:00
|
|
|
wxID_EXIT,
|
|
|
|
exit_xpm );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddClose( aAppname );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::Clear()
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2017-01-23 13:47:49 +00:00
|
|
|
m_titleDisplayed = false;
|
2013-09-26 12:09:18 +00:00
|
|
|
|
2015-04-30 08:46:07 +00:00
|
|
|
for( int i = GetMenuItemCount() - 1; i >= 0; --i )
|
|
|
|
Destroy( FindItemByPosition( i ) );
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
m_toolActions.clear();
|
2015-04-30 08:46:05 +00:00
|
|
|
m_submenus.clear();
|
2014-07-09 13:10:32 +00:00
|
|
|
|
2017-01-20 17:33:11 +00:00
|
|
|
wxASSERT( GetMenuItemCount() == 0 );
|
2013-09-26 12:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
bool ACTION_MENU::HasEnabledItems() const
|
2017-02-08 15:45:56 +00:00
|
|
|
{
|
|
|
|
bool hasEnabled = false;
|
|
|
|
|
|
|
|
auto& items = GetMenuItems();
|
|
|
|
|
|
|
|
for( auto item : items )
|
|
|
|
{
|
|
|
|
if( item->IsEnabled() && !item->IsSeparator() )
|
|
|
|
{
|
|
|
|
hasEnabled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::UpdateAll()
|
2015-04-30 08:46:05 +00:00
|
|
|
{
|
2015-07-30 11:49:35 +00:00
|
|
|
try
|
|
|
|
{
|
2017-01-20 17:33:11 +00:00
|
|
|
update();
|
2015-07-30 11:49:35 +00:00
|
|
|
}
|
2020-08-18 14:17:16 +00:00
|
|
|
catch( std::exception& )
|
2015-07-30 11:49:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:49:35 +00:00
|
|
|
if( m_tool )
|
|
|
|
updateHotKeys();
|
2015-04-30 08:46:05 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
runOnSubmenus( std::bind( &ACTION_MENU::UpdateAll, _1 ) );
|
2015-04-30 08:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-22 21:11:05 +00:00
|
|
|
void ACTION_MENU::ClearDirty()
|
|
|
|
{
|
|
|
|
m_dirty = false;
|
|
|
|
runOnSubmenus( std::bind( &ACTION_MENU::ClearDirty, _1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ACTION_MENU::SetDirty()
|
|
|
|
{
|
|
|
|
m_dirty = true;
|
|
|
|
runOnSubmenus( std::bind( &ACTION_MENU::SetDirty, _1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::SetTool( TOOL_INTERACTIVE* aTool )
|
2015-08-03 09:53:58 +00:00
|
|
|
{
|
|
|
|
m_tool = aTool;
|
2019-05-14 11:14:00 +00:00
|
|
|
runOnSubmenus( std::bind( &ACTION_MENU::SetTool, _1, aTool ) );
|
2015-08-03 09:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* ACTION_MENU::Clone() const
|
2017-01-20 17:33:11 +00:00
|
|
|
{
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* clone = create();
|
2017-01-20 17:33:11 +00:00
|
|
|
clone->Clear();
|
|
|
|
clone->copyFrom( *this );
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* ACTION_MENU::create() const
|
2017-01-20 17:33:11 +00:00
|
|
|
{
|
2019-06-15 00:29:42 +00:00
|
|
|
ACTION_MENU* menu = new ACTION_MENU( false );
|
2017-01-20 17:33:11 +00:00
|
|
|
|
|
|
|
wxASSERT_MSG( typeid( *this ) == typeid( *menu ),
|
|
|
|
wxString::Format( "You need to override create() method for class %s", typeid(*this).name() ) );
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
TOOL_MANAGER* ACTION_MENU::getToolManager() const
|
2015-06-19 15:32:33 +00:00
|
|
|
{
|
2017-01-20 17:33:11 +00:00
|
|
|
wxASSERT( m_tool );
|
|
|
|
return m_tool ? m_tool->GetManager() : nullptr;
|
2015-06-19 15:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::updateHotKeys()
|
2015-05-05 18:39:42 +00:00
|
|
|
{
|
2015-07-28 08:29:00 +00:00
|
|
|
TOOL_MANAGER* toolMgr = getToolManager();
|
|
|
|
|
2019-06-15 00:29:42 +00:00
|
|
|
for( auto& ii : m_toolActions )
|
2015-05-05 18:39:42 +00:00
|
|
|
{
|
2019-06-15 00:29:42 +00:00
|
|
|
int id = ii.first;
|
|
|
|
const TOOL_ACTION& action = *ii.second;
|
|
|
|
int key = toolMgr->GetHotKey( action ) & ~MD_MODIFIER_MASK;
|
2015-05-05 18:39:42 +00:00
|
|
|
|
2015-07-28 08:29:00 +00:00
|
|
|
if( key )
|
2015-05-05 18:39:42 +00:00
|
|
|
{
|
2015-07-28 08:29:00 +00:00
|
|
|
int mod = toolMgr->GetHotKey( action ) & MD_MODIFIER_MASK;
|
2015-07-07 16:37:00 +00:00
|
|
|
int flags = 0;
|
2015-05-05 18:39:42 +00:00
|
|
|
wxMenuItem* item = FindChildItem( id );
|
|
|
|
|
|
|
|
if( item )
|
|
|
|
{
|
2015-07-07 16:37:00 +00:00
|
|
|
flags |= ( mod & MD_ALT ) ? wxACCEL_ALT : 0;
|
|
|
|
flags |= ( mod & MD_CTRL ) ? wxACCEL_CTRL : 0;
|
|
|
|
flags |= ( mod & MD_SHIFT ) ? wxACCEL_SHIFT : 0;
|
|
|
|
|
|
|
|
if( !flags )
|
|
|
|
flags = wxACCEL_NORMAL;
|
2015-05-05 18:39:42 +00:00
|
|
|
|
|
|
|
wxAcceleratorEntry accel( flags, key, id, item );
|
|
|
|
item->SetAccel( &accel );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-08 09:40:44 +00:00
|
|
|
// wxWidgets doesn't tell us when a menu command was generated from a hotkey or from
|
|
|
|
// a menu selection. It's important to us because a hotkey can be an immediate action
|
|
|
|
// while the menu selection can not (as it has no associated position).
|
|
|
|
//
|
|
|
|
// We get around this by storing the last highlighted menuId. If it matches the command
|
|
|
|
// id then we know this is a menu selection. (You might think we could use the menuOpen
|
|
|
|
// menuClose events, but these are actually generated for hotkeys as well.)
|
|
|
|
|
|
|
|
static int g_last_menu_highlighted_id = 0;
|
|
|
|
|
2019-10-03 15:55:05 +00:00
|
|
|
// We need to store the position of the mouse when the menu was opened so it can be passed
|
|
|
|
// to the command event generated when the menu item is selected.
|
|
|
|
static VECTOR2D g_menu_open_position;
|
|
|
|
|
2019-07-08 09:40:44 +00:00
|
|
|
|
|
|
|
void ACTION_MENU::OnIdle( wxIdleEvent& event )
|
2013-09-26 12:09:18 +00:00
|
|
|
{
|
2019-07-08 09:40:44 +00:00
|
|
|
g_last_menu_highlighted_id = 0;
|
2019-10-03 15:55:05 +00:00
|
|
|
g_menu_open_position.x = 0.0;
|
|
|
|
g_menu_open_position.y = 0.0;
|
2019-07-08 09:40:44 +00:00
|
|
|
}
|
2019-06-15 00:29:42 +00:00
|
|
|
|
2014-05-13 09:22:51 +00:00
|
|
|
|
2019-07-08 09:40:44 +00:00
|
|
|
void ACTION_MENU::OnMenuEvent( wxMenuEvent& aEvent )
|
|
|
|
{
|
|
|
|
OPT_TOOL_EVENT evt;
|
|
|
|
wxString menuText;
|
|
|
|
wxEventType type = aEvent.GetEventType();
|
2020-05-30 11:31:19 +00:00
|
|
|
wxWindow* focus = wxWindow::FindFocus();
|
2013-09-26 12:09:18 +00:00
|
|
|
|
2019-06-15 00:29:42 +00:00
|
|
|
if( type == wxEVT_MENU_OPEN )
|
2019-05-13 20:42:40 +00:00
|
|
|
{
|
2019-06-15 00:29:42 +00:00
|
|
|
if( m_dirty && m_tool )
|
2019-05-22 02:58:49 +00:00
|
|
|
getToolManager()->RunAction( ACTIONS::updateMenu, true, this );
|
|
|
|
|
2019-10-03 15:55:05 +00:00
|
|
|
wxMenu* parent = dynamic_cast<wxMenu*>( GetParent() );
|
|
|
|
|
|
|
|
// Don't update the position if this menu has a parent
|
|
|
|
if( !parent && m_tool )
|
2019-10-04 14:58:32 +00:00
|
|
|
g_menu_open_position = getToolManager()->GetMousePosition();
|
2019-10-03 15:55:05 +00:00
|
|
|
|
2019-07-08 09:40:44 +00:00
|
|
|
g_last_menu_highlighted_id = 0;
|
2019-05-13 20:42:40 +00:00
|
|
|
}
|
|
|
|
else if( type == wxEVT_MENU_HIGHLIGHT )
|
2019-06-15 00:29:42 +00:00
|
|
|
{
|
2019-07-08 09:40:44 +00:00
|
|
|
if( aEvent.GetId() > 0 )
|
|
|
|
g_last_menu_highlighted_id = aEvent.GetId();
|
|
|
|
|
2019-06-15 00:29:42 +00:00
|
|
|
evt = TOOL_EVENT( TC_COMMAND, TA_CHOICE_MENU_UPDATE, aEvent.GetId() );
|
|
|
|
}
|
2013-09-26 12:09:18 +00:00
|
|
|
else if( type == wxEVT_COMMAND_MENU_SELECTED )
|
2013-09-26 16:38:58 +00:00
|
|
|
{
|
2020-05-30 11:31:19 +00:00
|
|
|
// Despite our attempts to catch the theft of text editor CHAR_HOOK and CHAR events
|
|
|
|
// in TOOL_DISPATCHER::DispatchWxEvent, wxWidgets sometimes converts those it knows
|
|
|
|
// about into menu commands without ever generating the appropriate CHAR_HOOK and CHAR
|
|
|
|
// events first.
|
2020-05-30 19:00:09 +00:00
|
|
|
if( dynamic_cast<wxTextEntry*>( focus )
|
|
|
|
|| dynamic_cast<wxStyledTextCtrl*>( focus )
|
|
|
|
|| dynamic_cast<wxListView*>( focus ) )
|
2020-05-30 11:31:19 +00:00
|
|
|
{
|
|
|
|
// Original key event has been lost, so we have to re-create it from the menu's
|
|
|
|
// wxAcceleratorEntry.
|
|
|
|
wxMenuItem* menuItem = FindItem( aEvent.GetId() );
|
|
|
|
wxAcceleratorEntry* acceleratorKey = menuItem ? menuItem->GetAccel() : nullptr;
|
|
|
|
|
|
|
|
if( acceleratorKey )
|
|
|
|
{
|
|
|
|
wxKeyEvent keyEvent( wxEVT_CHAR_HOOK );
|
|
|
|
keyEvent.m_keyCode = acceleratorKey->GetKeyCode();
|
|
|
|
keyEvent.m_controlDown = ( acceleratorKey->GetFlags() & wxMOD_CONTROL ) > 0;
|
|
|
|
keyEvent.m_shiftDown = ( acceleratorKey->GetFlags() & wxMOD_SHIFT ) > 0;
|
|
|
|
keyEvent.m_altDown = ( acceleratorKey->GetFlags() & wxMOD_ALT ) > 0;
|
|
|
|
|
2020-06-05 11:49:32 +00:00
|
|
|
if( auto ctrl = dynamic_cast<wxTextEntry*>( focus ) )
|
|
|
|
TEXTENTRY_TRICKS::OnCharHook( ctrl, keyEvent );
|
2020-06-04 21:08:09 +00:00
|
|
|
else
|
|
|
|
focus->HandleWindowEvent( keyEvent );
|
2020-05-30 11:31:19 +00:00
|
|
|
|
|
|
|
if( keyEvent.GetSkipped() )
|
|
|
|
{
|
|
|
|
keyEvent.SetEventType( wxEVT_CHAR );
|
|
|
|
focus->HandleWindowEvent( keyEvent );
|
|
|
|
}
|
|
|
|
|
2020-06-17 10:00:34 +00:00
|
|
|
// If the event was used as KEY event (not skipped) by the focused window,
|
|
|
|
// just finish.
|
|
|
|
// Otherwise this is actually a wxEVT_COMMAND_MENU_SELECTED, or the
|
|
|
|
// focused window is read only
|
|
|
|
if( !keyEvent.GetSkipped() )
|
|
|
|
return;
|
2020-06-04 21:08:09 +00:00
|
|
|
}
|
2020-05-30 11:31:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:37:23 +00:00
|
|
|
// Store the selected position, so it can be checked by the tools
|
2014-05-13 09:22:51 +00:00
|
|
|
m_selected = aEvent.GetId();
|
2013-11-29 15:45:39 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* parent = dynamic_cast<ACTION_MENU*>( GetParent() );
|
2018-03-26 19:34:41 +00:00
|
|
|
|
|
|
|
while( parent )
|
|
|
|
{
|
|
|
|
parent->m_selected = m_selected;
|
2019-05-14 11:14:00 +00:00
|
|
|
parent = dynamic_cast<ACTION_MENU*>( parent->GetParent() );
|
2018-03-26 19:34:41 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:51:21 +00:00
|
|
|
// Check if there is a TOOL_ACTION for the given ID
|
2020-07-23 23:47:19 +00:00
|
|
|
if( m_selected >= TOOL_ACTION::GetBaseUIId() )
|
2017-02-09 20:37:23 +00:00
|
|
|
evt = findToolAction( m_selected );
|
|
|
|
|
|
|
|
if( !evt )
|
2013-09-26 16:38:58 +00:00
|
|
|
{
|
2015-01-25 18:11:02 +00:00
|
|
|
#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
|
2017-01-20 17:33:11 +00:00
|
|
|
wxMenu* menu = nullptr;
|
2015-01-25 18:11:02 +00:00
|
|
|
FindItem( m_selected, &menu );
|
|
|
|
|
2018-12-28 14:30:22 +00:00
|
|
|
// This conditional compilation is probably not needed.
|
|
|
|
// It will be removed later, for the Kicad V 6.x version.
|
|
|
|
// But in "old" 3.0 version, the "&& menu != this" contition was added to avoid hang
|
|
|
|
// This hang is no longer encountered in wxWidgets 3.0.4 version, and this condition is no longer needed.
|
|
|
|
// And in 3.1.2, we have to remove it, as "menu != this" never happens
|
|
|
|
// ("menu != this" always happens in 3.1.1 and older!).
|
|
|
|
#if wxCHECK_VERSION(3, 1, 2)
|
|
|
|
if( menu )
|
|
|
|
#else
|
2015-02-10 23:23:45 +00:00
|
|
|
if( menu && menu != this )
|
2018-12-28 14:30:22 +00:00
|
|
|
#endif
|
2015-01-25 18:11:02 +00:00
|
|
|
{
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* cxmenu = static_cast<ACTION_MENU*>( menu );
|
2017-01-20 17:33:11 +00:00
|
|
|
evt = cxmenu->eventHandler( aEvent );
|
2015-01-25 18:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-20 17:33:11 +00:00
|
|
|
#else
|
|
|
|
if( !evt )
|
|
|
|
runEventHandlers( aEvent, evt );
|
2015-02-10 23:23:45 +00:00
|
|
|
#endif
|
2014-05-13 09:22:51 +00:00
|
|
|
|
2021-02-10 15:02:28 +00:00
|
|
|
// Handling non-ACTION menu entries. Two ranges of ids are supported:
|
|
|
|
// between 0 and ID_CONTEXT_MENU_ID_MAX
|
|
|
|
// between ID_POPUP_MENU_START and ID_POPUP_MENU_END
|
|
|
|
|
|
|
|
#define ID_CONTEXT_MENU_ID_MAX wxID_LOWEST /* = 100 should be plenty */
|
2019-06-17 22:02:30 +00:00
|
|
|
|
|
|
|
if( !evt &&
|
|
|
|
( ( m_selected >= 0 && m_selected < ID_CONTEXT_MENU_ID_MAX ) ||
|
|
|
|
( m_selected >= ID_POPUP_MENU_START && m_selected <= ID_POPUP_MENU_END ) ) )
|
2019-05-02 20:03:03 +00:00
|
|
|
{
|
2021-02-10 15:02:28 +00:00
|
|
|
ACTION_MENU* actionMenu = dynamic_cast<ACTION_MENU*>( GetParent() );
|
|
|
|
|
|
|
|
if( actionMenu && actionMenu->PassHelpTextToHandler() )
|
|
|
|
menuText = GetHelpString( aEvent.GetId() );
|
|
|
|
else
|
|
|
|
menuText = GetLabelText( aEvent.GetId() );
|
|
|
|
|
2019-06-15 00:29:42 +00:00
|
|
|
evt = TOOL_EVENT( TC_COMMAND, TA_CHOICE_MENU_CHOICE, m_selected, AS_GLOBAL,
|
2019-05-14 19:21:10 +00:00
|
|
|
&menuText );
|
2019-05-02 20:03:03 +00:00
|
|
|
}
|
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
|
2018-08-13 22:27:54 +00:00
|
|
|
// clients that don't supply a tool will have to check GetSelected() themselves
|
2014-05-14 14:29:53 +00:00
|
|
|
if( evt && m_tool )
|
2017-01-20 17:33:11 +00:00
|
|
|
{
|
2019-07-27 14:11:41 +00:00
|
|
|
wxLogTrace( kicadTraceToolStack, "ACTION_MENU::OnMenuEvent %s", evt->Format() );
|
|
|
|
|
2021-02-05 16:17:32 +00:00
|
|
|
// WARNING: if you're squeamish, look away.
|
|
|
|
// What follows is a series of egregious hacks necessitated by a lack of information from
|
|
|
|
// wxWidgets on where context-menu-commands and command-key-events originated.
|
|
|
|
|
|
|
|
// If it's a context menu then fetch the mouse position from our context-menu-position
|
|
|
|
// hack.
|
|
|
|
if( m_isContextMenu )
|
|
|
|
{
|
2019-10-03 15:55:05 +00:00
|
|
|
evt->SetMousePosition( g_menu_open_position );
|
2021-02-05 16:17:32 +00:00
|
|
|
}
|
|
|
|
// Otherwise, if g_last_menu_highlighted_id matches then it's a menubar menu event and has
|
|
|
|
// no position.
|
|
|
|
else if( g_last_menu_highlighted_id == aEvent.GetId() )
|
|
|
|
{
|
|
|
|
evt->SetHasPosition( false );
|
|
|
|
}
|
|
|
|
// Otherwise it's a command-key-event and we need to get the mouse position from the tool
|
|
|
|
// manager so that immediate actions work.
|
2019-10-03 15:55:05 +00:00
|
|
|
else
|
2021-02-05 16:17:32 +00:00
|
|
|
{
|
2019-10-03 20:36:11 +00:00
|
|
|
evt->SetMousePosition( getToolManager()->GetMousePosition() );
|
2021-02-05 16:17:32 +00:00
|
|
|
}
|
2019-06-15 00:29:42 +00:00
|
|
|
|
2019-05-23 11:28:56 +00:00
|
|
|
if( m_tool->GetManager() )
|
|
|
|
m_tool->GetManager()->ProcessEvent( *evt );
|
2017-01-20 17:33:11 +00:00
|
|
|
}
|
2019-05-14 19:21:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
2014-05-13 09:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent )
|
2015-04-30 08:46:05 +00:00
|
|
|
{
|
2017-01-20 17:33:11 +00:00
|
|
|
aToolEvent = eventHandler( aMenuEvent );
|
2015-04-30 08:46:05 +00:00
|
|
|
|
|
|
|
if( !aToolEvent )
|
2019-05-14 11:14:00 +00:00
|
|
|
runOnSubmenus( std::bind( &ACTION_MENU::runEventHandlers, _1, aMenuEvent, aToolEvent ) );
|
2015-04-30 08:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::runOnSubmenus( std::function<void(ACTION_MENU*)> aFunction )
|
2015-04-30 08:46:05 +00:00
|
|
|
{
|
2015-08-15 14:00:33 +00:00
|
|
|
try
|
|
|
|
{
|
2019-05-14 11:14:00 +00:00
|
|
|
std::for_each( m_submenus.begin(), m_submenus.end(), [&]( ACTION_MENU* m ) {
|
2017-02-09 20:37:23 +00:00
|
|
|
aFunction( m );
|
|
|
|
m->runOnSubmenus( aFunction );
|
|
|
|
} );
|
2015-08-15 14:00:33 +00:00
|
|
|
}
|
2020-08-18 14:17:16 +00:00
|
|
|
catch( std::exception& )
|
2015-08-15 14:00:33 +00:00
|
|
|
{
|
|
|
|
}
|
2014-05-31 14:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
OPT_TOOL_EVENT ACTION_MENU::findToolAction( int aId )
|
2017-02-09 20:37:23 +00:00
|
|
|
{
|
|
|
|
OPT_TOOL_EVENT evt;
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
auto findFunc = [&]( ACTION_MENU* m ) {
|
2017-02-09 20:37:23 +00:00
|
|
|
if( evt )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto it = m->m_toolActions.find( aId );
|
|
|
|
|
|
|
|
if( it != m->m_toolActions.end() )
|
|
|
|
evt = it->second->MakeEvent();
|
|
|
|
};
|
|
|
|
|
|
|
|
findFunc( this );
|
|
|
|
|
|
|
|
if( !evt )
|
|
|
|
runOnSubmenus( findFunc );
|
|
|
|
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
void ACTION_MENU::copyFrom( const ACTION_MENU& aMenu )
|
2015-04-02 14:09:48 +00:00
|
|
|
{
|
|
|
|
m_icon = aMenu.m_icon;
|
2017-02-09 22:25:58 +00:00
|
|
|
m_title = aMenu.m_title;
|
2017-01-23 13:47:49 +00:00
|
|
|
m_titleDisplayed = aMenu.m_titleDisplayed;
|
2015-04-30 08:46:04 +00:00
|
|
|
m_selected = -1; // aMenu.m_selected;
|
|
|
|
m_tool = aMenu.m_tool;
|
|
|
|
m_toolActions = aMenu.m_toolActions;
|
2015-04-02 14:09:48 +00:00
|
|
|
|
2017-02-09 22:25:58 +00:00
|
|
|
// Copy all menu entries
|
2015-04-30 08:46:07 +00:00
|
|
|
for( int i = 0; i < (int) aMenu.GetMenuItemCount(); ++i )
|
2015-04-02 14:09:48 +00:00
|
|
|
{
|
|
|
|
wxMenuItem* item = aMenu.FindItemByPosition( i );
|
2015-04-30 08:46:04 +00:00
|
|
|
appendCopy( item );
|
2015-04-02 14:09:48 +00:00
|
|
|
}
|
2013-08-07 09:20:12 +00:00
|
|
|
}
|
2015-04-30 08:46:05 +00:00
|
|
|
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
wxMenuItem* ACTION_MENU::appendCopy( const wxMenuItem* aSource )
|
2015-04-30 08:46:05 +00:00
|
|
|
{
|
2017-01-20 17:33:11 +00:00
|
|
|
wxMenuItem* newItem = new wxMenuItem( this, aSource->GetId(), aSource->GetItemLabel(),
|
|
|
|
aSource->GetHelp(), aSource->GetKind() );
|
2015-04-30 08:46:05 +00:00
|
|
|
|
2019-05-24 19:01:15 +00:00
|
|
|
// Add the source bitmap if it is not the wxNullBitmap
|
2020-09-20 14:28:56 +00:00
|
|
|
// On Windows, for Checkable Menu items, adding a bitmap adds also
|
2019-05-24 19:01:15 +00:00
|
|
|
// our predefined checked alternate bitmap
|
2020-09-20 14:28:56 +00:00
|
|
|
// On other OS, wxITEM_CHECK and wxITEM_RADIO Menu items do not use custom bitmaps.
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// On Windows, AddBitmapToMenuItem() uses the unchecked bitmap for wxITEM_CHECK and wxITEM_RADIO menuitems
|
|
|
|
// and autoamtically adds a checked bitmap.
|
|
|
|
// For other menuitrms, use the "checked" bitmap.
|
|
|
|
bool use_checked_bm = ( aSource->GetKind() == wxITEM_CHECK || aSource->GetKind() == wxITEM_RADIO ) ? false : true;
|
|
|
|
const wxBitmap& src_bitmap = aSource->GetBitmap( use_checked_bm );
|
|
|
|
#else
|
2019-05-24 19:01:15 +00:00
|
|
|
const wxBitmap& src_bitmap = aSource->GetBitmap();
|
2020-09-20 14:28:56 +00:00
|
|
|
#endif
|
2019-05-24 19:01:15 +00:00
|
|
|
|
2019-05-26 15:36:40 +00:00
|
|
|
if( src_bitmap.IsOk() && src_bitmap.GetHeight() > 1 ) // a null bitmap has a 0 size
|
2019-05-24 19:01:15 +00:00
|
|
|
AddBitmapToMenuItem( newItem, src_bitmap );
|
2015-04-30 08:46:05 +00:00
|
|
|
|
2017-01-20 17:33:11 +00:00
|
|
|
if( aSource->IsSubMenu() )
|
|
|
|
{
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* menu = dynamic_cast<ACTION_MENU*>( aSource->GetSubMenu() );
|
|
|
|
wxASSERT_MSG( menu, "Submenus are expected to be a ACTION_MENU" );
|
2017-01-20 17:33:11 +00:00
|
|
|
|
|
|
|
if( menu )
|
|
|
|
{
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* menuCopy = menu->Clone();
|
2017-01-20 17:33:11 +00:00
|
|
|
newItem->SetSubMenu( menuCopy );
|
|
|
|
m_submenus.push_back( menuCopy );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wxMenuItem has to be added before enabling/disabling or checking
|
|
|
|
Append( newItem );
|
|
|
|
|
|
|
|
if( aSource->IsCheckable() )
|
|
|
|
newItem->Check( aSource->IsChecked() );
|
|
|
|
|
|
|
|
newItem->Enable( aSource->IsEnabled() );
|
|
|
|
|
|
|
|
return newItem;
|
2015-04-30 08:46:05 +00:00
|
|
|
}
|