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.

229 lines
5.2 KiB

5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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 <wx/log.h>
  28. #include "action_plugin.h"
  29. #include "bitmaps.h"
  30. #include "bitmap_store.h"
  31. #include <pgm_base.h>
  32. ACTION_PLUGIN::~ACTION_PLUGIN()
  33. {
  34. }
  35. void ACTION_PLUGIN::register_action()
  36. {
  37. ACTION_PLUGINS::register_action( this );
  38. }
  39. std::vector<ACTION_PLUGIN*> ACTION_PLUGINS::m_actionsList;
  40. bool ACTION_PLUGINS::m_actionRunning = false;
  41. ACTION_PLUGIN* ACTION_PLUGINS::GetAction( int aIndex )
  42. {
  43. return m_actionsList[aIndex];
  44. }
  45. ACTION_PLUGIN* ACTION_PLUGINS::GetActionByMenu( int aMenu )
  46. {
  47. int max = GetActionsCount();
  48. for( int i = 0; i < max; i++ )
  49. {
  50. if( m_actionsList[i]->m_actionMenuId == aMenu )
  51. return m_actionsList[i];
  52. }
  53. return nullptr;
  54. }
  55. void ACTION_PLUGINS::SetActionMenu( int aIndex, int idMenu )
  56. {
  57. m_actionsList[aIndex]->m_actionMenuId = idMenu;
  58. }
  59. ACTION_PLUGIN* ACTION_PLUGINS::GetActionByButton( int aButton )
  60. {
  61. int max = GetActionsCount();
  62. for( int i = 0; i < max; i++ )
  63. {
  64. if( m_actionsList[i]->m_actionButtonId == aButton )
  65. return m_actionsList[i];
  66. }
  67. return nullptr;
  68. }
  69. void ACTION_PLUGINS::SetActionButton( ACTION_PLUGIN* aAction, int idButton )
  70. {
  71. aAction->m_actionButtonId = idButton;
  72. }
  73. ACTION_PLUGIN* ACTION_PLUGINS::GetActionByPath(const wxString& aPath)
  74. {
  75. for( int i = 0; i < GetActionsCount() ; i++ )
  76. {
  77. if( m_actionsList[i]->GetPluginPath() == aPath)
  78. {
  79. return m_actionsList[i];
  80. }
  81. }
  82. return nullptr;
  83. }
  84. ACTION_PLUGIN* ACTION_PLUGINS::GetAction( const wxString& aName )
  85. {
  86. int max = GetActionsCount();
  87. for( int i = 0; i<max; i++ )
  88. {
  89. ACTION_PLUGIN* action = GetAction( i );
  90. wxString name = action->GetName();
  91. if( name.Cmp( aName )==0 )
  92. return action;
  93. }
  94. return nullptr;
  95. }
  96. int ACTION_PLUGINS::GetActionsCount()
  97. {
  98. return m_actionsList.size();
  99. }
  100. void ACTION_PLUGINS::register_action( ACTION_PLUGIN* aAction )
  101. {
  102. // Search for this entry do not register twice this action:
  103. for( int ii = 0; ii < GetActionsCount(); ii++ )
  104. {
  105. if( aAction == GetAction( ii ) ) // Already registered
  106. return;
  107. }
  108. // Search for a action with the same name, and remove it if found
  109. for( int ii = 0; ii < GetActionsCount(); ii++ )
  110. {
  111. ACTION_PLUGIN* action = GetAction( ii );
  112. if( action->GetName() == aAction->GetName() )
  113. {
  114. m_actionsList.erase( m_actionsList.begin() + ii );
  115. delete action;
  116. break;
  117. }
  118. }
  119. wxASSERT( PgmOrNull() ); // PgmOrNull() returning nullptr should never happen,
  120. // but it sometimes happens on msys2 build
  121. if( PgmOrNull() ) // Hack for msys2. Must be removed when the root cause is fixed
  122. {
  123. // Load icon if supplied
  124. wxString icon_file_name = aAction->GetIconFileName( GetBitmapStore()->IsDarkTheme() );
  125. if( !icon_file_name.IsEmpty() )
  126. {
  127. {
  128. wxLogNull eat_errors;
  129. aAction->iconBitmap.LoadFile( icon_file_name, wxBITMAP_TYPE_PNG );
  130. }
  131. if ( !aAction->iconBitmap.IsOk() )
  132. {
  133. wxLogVerbose( wxT( "Failed to load icon " ) + icon_file_name + wxT( " for action plugin " ) );
  134. }
  135. }
  136. }
  137. m_actionsList.push_back( aAction );
  138. }
  139. bool ACTION_PLUGINS::deregister_object( void* aObject )
  140. {
  141. int max = GetActionsCount();
  142. for( int i = 0; i<max; i++ )
  143. {
  144. ACTION_PLUGIN* action = GetAction( i );
  145. if( action->GetObject() == aObject )
  146. {
  147. m_actionsList.erase( m_actionsList.begin() + i );
  148. //m_actionsListMenu.erase( m_actionsListMenu.begin() + i );
  149. delete action;
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. bool ACTION_PLUGINS::IsActionRunning()
  156. {
  157. return ACTION_PLUGINS::m_actionRunning;
  158. }
  159. void ACTION_PLUGINS::SetActionRunning( bool aRunning )
  160. {
  161. ACTION_PLUGINS::m_actionRunning = aRunning;
  162. }
  163. void ACTION_PLUGINS::UnloadAll()
  164. {
  165. for( ACTION_PLUGIN* plugin : m_actionsList )
  166. delete plugin;
  167. m_actionsList.clear();
  168. }