2014-07-09 11:50:27 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Maciej Suminski <maciej.suminski@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
|
|
|
|
*/
|
|
|
|
|
2022-09-27 21:03:58 +00:00
|
|
|
#include <optional>
|
2014-07-09 11:50:27 +00:00
|
|
|
#include <tool/tool_action.h>
|
2021-06-06 17:26:26 +00:00
|
|
|
#include <tool/tool_event.h>
|
2015-05-05 18:39:41 +00:00
|
|
|
#include <tool/action_manager.h>
|
|
|
|
|
2017-11-01 11:14:16 +00:00
|
|
|
#include <algorithm>
|
2021-03-08 02:59:07 +00:00
|
|
|
#include <bitmaps.h>
|
2019-06-10 14:23:37 +00:00
|
|
|
#include <hotkeys_basic.h>
|
2022-09-26 22:44:34 +00:00
|
|
|
#include <wx/stringimpl.h>
|
2021-06-02 23:18:48 +00:00
|
|
|
#include <wx/translation.h>
|
2017-11-01 11:14:16 +00:00
|
|
|
|
2015-05-05 18:39:41 +00:00
|
|
|
TOOL_ACTION::TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope,
|
2019-06-09 21:57:23 +00:00
|
|
|
int aDefaultHotKey, const std::string& aLegacyHotKeyName,
|
2019-06-10 14:23:37 +00:00
|
|
|
const wxString& aLabel, const wxString& aTooltip,
|
2022-09-28 01:15:19 +00:00
|
|
|
BITMAPS aIcon, TOOL_ACTION_FLAGS aFlags ) :
|
2019-06-09 21:57:23 +00:00
|
|
|
m_name( aName ),
|
|
|
|
m_scope( aScope ),
|
|
|
|
m_defaultHotKey( aDefaultHotKey ),
|
|
|
|
m_legacyName( aLegacyHotKeyName ),
|
2019-06-10 14:23:37 +00:00
|
|
|
m_label( aLabel ),
|
2019-06-09 21:57:23 +00:00
|
|
|
m_tooltip( aTooltip ),
|
|
|
|
m_icon( aIcon ),
|
|
|
|
m_id( -1 ),
|
2022-09-28 01:15:19 +00:00
|
|
|
m_flags( aFlags )
|
2015-05-05 18:39:41 +00:00
|
|
|
{
|
2019-06-10 14:23:37 +00:00
|
|
|
SetHotKey( aDefaultHotKey );
|
2015-05-05 18:39:41 +00:00
|
|
|
ACTION_MANAGER::GetActionList().push_back( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-12 14:39:47 +00:00
|
|
|
TOOL_ACTION::TOOL_ACTION() :
|
|
|
|
m_scope( AS_GLOBAL ),
|
|
|
|
m_defaultHotKey( 0 ),
|
2021-03-08 02:59:07 +00:00
|
|
|
m_icon( BITMAPS::INVALID_BITMAP ),
|
2019-06-12 14:39:47 +00:00
|
|
|
m_id( -1 ),
|
2022-09-28 01:15:19 +00:00
|
|
|
m_flags( AF_NONE )
|
2019-06-12 14:39:47 +00:00
|
|
|
{
|
|
|
|
SetHotKey( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-26 22:44:34 +00:00
|
|
|
TOOL_ACTION::TOOL_ACTION( const TOOL_ACTION_ARGS& aArgs ) :
|
|
|
|
m_name( aArgs.m_name.value_or( "" ) ),
|
|
|
|
m_scope( aArgs.m_scope.value_or( AS_CONTEXT ) ),
|
|
|
|
m_defaultHotKey( aArgs.m_defaultHotKey.value_or( 0 ) ),
|
|
|
|
m_hotKey( aArgs.m_defaultHotKey.value_or( 0 ) ),
|
|
|
|
m_legacyName( aArgs.m_legacyName.value_or( "" ) ),
|
|
|
|
m_label( aArgs.m_menuText.value_or( wxEmptyString ) ),
|
|
|
|
m_tooltip( aArgs.m_tooltip.value_or( wxEmptyString ) ),
|
|
|
|
m_icon( aArgs.m_icon.value_or( BITMAPS::INVALID_BITMAP) ),
|
|
|
|
m_id( -1 ),
|
2022-09-27 21:03:58 +00:00
|
|
|
m_uiid( std::nullopt ),
|
2022-09-28 01:15:19 +00:00
|
|
|
m_flags( aArgs.m_flags.value_or( AF_NONE ) )
|
2022-09-26 22:44:34 +00:00
|
|
|
{
|
|
|
|
// Action name is the only mandatory part
|
|
|
|
assert( !m_name.empty() );
|
|
|
|
|
2022-09-27 21:03:58 +00:00
|
|
|
if( aArgs.m_uiid.has_value() )
|
|
|
|
m_uiid = aArgs.m_uiid.value();
|
|
|
|
|
2022-09-28 01:15:19 +00:00
|
|
|
if( aArgs.m_param.has_value() )
|
|
|
|
m_param = aArgs.m_param;
|
|
|
|
|
2023-06-29 23:06:03 +00:00
|
|
|
if( aArgs.m_description.has_value() )
|
|
|
|
m_description = aArgs.m_description;
|
|
|
|
|
2022-09-26 22:44:34 +00:00
|
|
|
ACTION_MANAGER::GetActionList().push_back( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-05 18:39:41 +00:00
|
|
|
TOOL_ACTION::~TOOL_ACTION()
|
|
|
|
{
|
|
|
|
ACTION_MANAGER::GetActionList().remove( this );
|
|
|
|
}
|
|
|
|
|
2014-07-09 11:50:27 +00:00
|
|
|
|
2021-06-06 17:26:26 +00:00
|
|
|
TOOL_EVENT TOOL_ACTION::MakeEvent() const
|
|
|
|
{
|
2022-09-28 01:15:19 +00:00
|
|
|
TOOL_EVENT evt;
|
|
|
|
|
2021-06-06 17:26:26 +00:00
|
|
|
if( IsActivation() )
|
2022-09-28 01:15:19 +00:00
|
|
|
evt = TOOL_EVENT( TC_COMMAND, TA_ACTIVATE, m_name, m_scope );
|
2021-06-06 17:26:26 +00:00
|
|
|
else if( IsNotification() )
|
2022-09-28 01:15:19 +00:00
|
|
|
evt = TOOL_EVENT( TC_MESSAGE, TA_NONE, m_name, m_scope );
|
2021-06-06 17:26:26 +00:00
|
|
|
else
|
2022-09-28 01:15:19 +00:00
|
|
|
evt = TOOL_EVENT( TC_COMMAND, TA_ACTION, m_name, m_scope );
|
|
|
|
|
|
|
|
if( m_param.has_value() )
|
|
|
|
evt.SetParameter( m_param );
|
|
|
|
|
|
|
|
return evt;
|
2021-06-06 17:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-17 00:34:21 +00:00
|
|
|
wxString TOOL_ACTION::GetLabel() const
|
|
|
|
{
|
|
|
|
return wxGetTranslation( m_label );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString TOOL_ACTION::GetMenuItem() const
|
|
|
|
{
|
2019-06-17 09:41:38 +00:00
|
|
|
wxString label = wxGetTranslation( m_label );
|
2023-01-17 12:42:30 +00:00
|
|
|
label.Replace( wxS( "&" ), wxS( "&&" ) );
|
2019-06-17 09:41:38 +00:00
|
|
|
return AddHotkeyName( label, m_hotKey, IS_HOTKEY );
|
2019-06-17 00:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-29 23:06:03 +00:00
|
|
|
wxString TOOL_ACTION::GetDescription() const
|
|
|
|
{
|
|
|
|
// If no description provided, use the tooltip without a hotkey
|
|
|
|
if( !m_description.has_value() )
|
|
|
|
return GetTooltip( false );
|
|
|
|
|
|
|
|
return wxGetTranslation( m_description.value() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString TOOL_ACTION::GetTooltip( bool aIncludeHotkey ) const
|
2019-06-17 00:34:21 +00:00
|
|
|
{
|
2019-07-07 19:53:55 +00:00
|
|
|
wxString tooltip = wxGetTranslation( m_tooltip );
|
|
|
|
|
2020-09-04 17:42:04 +00:00
|
|
|
if( aIncludeHotkey && GetHotKey() )
|
2019-07-07 19:53:55 +00:00
|
|
|
tooltip += wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( GetHotKey() ) );
|
|
|
|
|
|
|
|
return tooltip;
|
2019-06-17 00:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-10 14:23:37 +00:00
|
|
|
void TOOL_ACTION::SetHotKey( int aKeycode )
|
|
|
|
{
|
|
|
|
m_hotKey = aKeycode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-09 11:50:27 +00:00
|
|
|
std::string TOOL_ACTION::GetToolName() const
|
|
|
|
{
|
|
|
|
int dotCount = std::count( m_name.begin(), m_name.end(), '.' );
|
|
|
|
|
|
|
|
switch( dotCount )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
assert( false ); // Invalid action name format
|
|
|
|
return "";
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return m_name;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return m_name.substr( 0, m_name.rfind( '.' ) );
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( false ); // TODO not implemented
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|