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.

206 lines
4.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file action_plugin.cpp
  25. * @brief Class ACTION_PLUGIN and ACTION_PLUGINS
  26. */
  27. #include "action_plugin.h"
  28. ACTION_PLUGIN::~ACTION_PLUGIN()
  29. {
  30. }
  31. void ACTION_PLUGIN::register_action()
  32. {
  33. ACTION_PLUGINS::register_action( this );
  34. }
  35. std::vector<ACTION_PLUGIN*> ACTION_PLUGINS::m_actionsList;
  36. bool ACTION_PLUGINS::m_actionRunning = false;
  37. ACTION_PLUGIN* ACTION_PLUGINS::GetAction( int aIndex )
  38. {
  39. return m_actionsList[aIndex];
  40. }
  41. ACTION_PLUGIN* ACTION_PLUGINS::GetActionByMenu( int aMenu )
  42. {
  43. int max = GetActionsCount();
  44. for( int i = 0; i < max; i++ )
  45. {
  46. if( m_actionsList[i]->m_actionMenuId == aMenu )
  47. return m_actionsList[i];
  48. }
  49. return NULL;
  50. }
  51. void ACTION_PLUGINS::SetActionMenu( int aIndex, int idMenu )
  52. {
  53. m_actionsList[aIndex]->m_actionMenuId = idMenu;
  54. }
  55. ACTION_PLUGIN* ACTION_PLUGINS::GetActionByButton( int aButton )
  56. {
  57. int max = GetActionsCount();
  58. for( int i = 0; i < max; i++ )
  59. {
  60. if( m_actionsList[i]->m_actionButtonId == aButton )
  61. return m_actionsList[i];
  62. }
  63. return NULL;
  64. }
  65. void ACTION_PLUGINS::SetActionButton( ACTION_PLUGIN* aAction, int idButton )
  66. {
  67. aAction->m_actionButtonId = idButton;
  68. }
  69. ACTION_PLUGIN* ACTION_PLUGINS::GetActionByPath(const wxString& aPath)
  70. {
  71. for( int i = 0; i < GetActionsCount() ; i++ )
  72. {
  73. if( m_actionsList[i]->GetPluginPath() == aPath)
  74. {
  75. return m_actionsList[i];
  76. }
  77. }
  78. return NULL;
  79. }
  80. ACTION_PLUGIN* ACTION_PLUGINS::GetAction( const wxString& aName )
  81. {
  82. int max = GetActionsCount();
  83. for( int i = 0; i<max; i++ )
  84. {
  85. ACTION_PLUGIN* action = GetAction( i );
  86. wxString name = action->GetName();
  87. if( name.Cmp( aName )==0 )
  88. return action;
  89. }
  90. return NULL;
  91. }
  92. int ACTION_PLUGINS::GetActionsCount()
  93. {
  94. return m_actionsList.size();
  95. }
  96. void ACTION_PLUGINS::register_action( ACTION_PLUGIN* aAction )
  97. {
  98. // Search for this entry do not register twice this action:
  99. for( int ii = 0; ii < GetActionsCount(); ii++ )
  100. {
  101. if( aAction == GetAction( ii ) ) // Already registered
  102. return;
  103. }
  104. // Search for a action with the same name, and remove it if found
  105. for( int ii = 0; ii < GetActionsCount(); ii++ )
  106. {
  107. ACTION_PLUGIN* action = GetAction( ii );
  108. if( action->GetName() == aAction->GetName() )
  109. {
  110. m_actionsList.erase( m_actionsList.begin() + ii );
  111. delete action;
  112. break;
  113. }
  114. }
  115. // Load icon if supplied
  116. if (!aAction->GetIconFileName().IsEmpty())
  117. {
  118. {
  119. wxLogNull eat_errors;
  120. aAction->iconBitmap.LoadFile( aAction->GetIconFileName() , wxBITMAP_TYPE_PNG );
  121. }
  122. if ( !aAction->iconBitmap.IsOk() )
  123. {
  124. wxLogVerbose( "Failed to load icon " + aAction->GetIconFileName() + " for action plugin " );
  125. }
  126. }
  127. m_actionsList.push_back( aAction );
  128. }
  129. bool ACTION_PLUGINS::deregister_object( void* aObject )
  130. {
  131. int max = GetActionsCount();
  132. for( int i = 0; i<max; i++ )
  133. {
  134. ACTION_PLUGIN* action = GetAction( i );
  135. if( action->GetObject() == aObject )
  136. {
  137. m_actionsList.erase( m_actionsList.begin() + i );
  138. //m_actionsListMenu.erase( m_actionsListMenu.begin() + i );
  139. delete action;
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. bool ACTION_PLUGINS::IsActionRunning()
  146. {
  147. return ACTION_PLUGINS::m_actionRunning;
  148. }
  149. void ACTION_PLUGINS::SetActionRunning( bool aRunning )
  150. {
  151. ACTION_PLUGINS::m_actionRunning = aRunning;
  152. }