You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 CERN
  5. * @author Maciej Suminski <maciej.suminski@cern.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <tool/tool_action.h>
  25. #include <tool/action_manager.h>
  26. #include <algorithm>
  27. #include <hotkeys_basic.h>
  28. TOOL_ACTION::TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope,
  29. int aDefaultHotKey, const std::string& aLegacyHotKeyName,
  30. const wxString& aLabel, const wxString& aTooltip,
  31. const BITMAP_OPAQUE* aIcon, TOOL_ACTION_FLAGS aFlags, void* aParam ) :
  32. m_name( aName ),
  33. m_scope( aScope ),
  34. m_defaultHotKey( aDefaultHotKey ),
  35. m_legacyName( aLegacyHotKeyName ),
  36. m_label( aLabel ),
  37. m_tooltip( aTooltip ),
  38. m_icon( aIcon ),
  39. m_id( -1 ),
  40. m_flags( aFlags ),
  41. m_param( aParam )
  42. {
  43. SetHotKey( aDefaultHotKey );
  44. ACTION_MANAGER::GetActionList().push_back( this );
  45. }
  46. TOOL_ACTION::TOOL_ACTION() :
  47. m_scope( AS_GLOBAL ),
  48. m_defaultHotKey( 0 ),
  49. m_icon( nullptr ),
  50. m_id( -1 ),
  51. m_flags( AF_NONE ),
  52. m_param( nullptr )
  53. {
  54. SetHotKey( 0 );
  55. }
  56. TOOL_ACTION::~TOOL_ACTION()
  57. {
  58. ACTION_MANAGER::GetActionList().remove( this );
  59. }
  60. wxString TOOL_ACTION::GetLabel() const
  61. {
  62. return wxGetTranslation( m_label );
  63. }
  64. wxString TOOL_ACTION::GetMenuItem() const
  65. {
  66. wxString label = wxGetTranslation( m_label );
  67. label.Replace( "&", "&&" );
  68. return AddHotkeyName( label, m_hotKey, IS_HOTKEY );
  69. }
  70. wxString TOOL_ACTION::GetDescription( bool aIncludeHotkey ) const
  71. {
  72. wxString tooltip = wxGetTranslation( m_tooltip );
  73. if( aIncludeHotkey && GetHotKey() )
  74. tooltip += wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( GetHotKey() ) );
  75. return tooltip;
  76. }
  77. void TOOL_ACTION::SetHotKey( int aKeycode )
  78. {
  79. m_hotKey = aKeycode;
  80. }
  81. std::string TOOL_ACTION::GetToolName() const
  82. {
  83. int dotCount = std::count( m_name.begin(), m_name.end(), '.' );
  84. switch( dotCount )
  85. {
  86. case 0:
  87. assert( false ); // Invalid action name format
  88. return "";
  89. case 1:
  90. return m_name;
  91. case 2:
  92. return m_name.substr( 0, m_name.rfind( '.' ) );
  93. default:
  94. assert( false ); // TODO not implemented
  95. return "";
  96. }
  97. }