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.

227 lines
7.0 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 CERN
  5. * Copyright (C) 2015-2019 KiCad Developers, see CHANGELOG.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <tool/conditional_menu.h>
  26. #include <tool/action_menu.h>
  27. #include <menus_helpers.h>
  28. CONDITIONAL_MENU::CONDITIONAL_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool ) :
  29. ACTION_MENU( isContextMenu )
  30. {
  31. m_tool = aTool;
  32. }
  33. ACTION_MENU* CONDITIONAL_MENU::create() const
  34. {
  35. CONDITIONAL_MENU* clone = new CONDITIONAL_MENU( m_isContextMenu, m_tool );
  36. clone->m_entries = m_entries;
  37. return clone;
  38. }
  39. void CONDITIONAL_MENU::AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
  40. int aOrder )
  41. {
  42. wxASSERT( aAction.GetId() > 0 ); // Check if action was previously registered in ACTION_MANAGER
  43. addEntry( ENTRY( &aAction, aCondition, aOrder, false ) );
  44. }
  45. void CONDITIONAL_MENU::AddCheckItem( const TOOL_ACTION& aAction,
  46. const SELECTION_CONDITION& aCondition, int aOrder )
  47. {
  48. wxASSERT( aAction.GetId() > 0 ); // Check if action was previously registered in ACTION_MANAGER
  49. addEntry( ENTRY( &aAction, aCondition, aOrder, true ) );
  50. }
  51. void CONDITIONAL_MENU::AddItem( int aId, const wxString& aText, const wxString& aTooltip,
  52. BITMAP_DEF aIcon, const SELECTION_CONDITION& aCondition,
  53. int aOrder )
  54. {
  55. wxMenuItem* item = new wxMenuItem( nullptr, aId, aText, aTooltip, wxITEM_NORMAL );
  56. if( aIcon )
  57. AddBitmapToMenuItem( item, KiBitmap( aIcon ) );
  58. addEntry( ENTRY( item, aIcon, aCondition, aOrder, false ) );
  59. }
  60. void CONDITIONAL_MENU::AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip,
  61. BITMAP_DEF aIcon, const SELECTION_CONDITION& aCondition,
  62. int aOrder )
  63. {
  64. wxMenuItem* item = new wxMenuItem( nullptr, aId, aText, aTooltip, wxITEM_CHECK );
  65. if( aIcon )
  66. AddBitmapToMenuItem( item, KiBitmap( aIcon ) );
  67. addEntry( ENTRY( item, aIcon, aCondition, aOrder, true ) );
  68. }
  69. void CONDITIONAL_MENU::AddMenu( ACTION_MENU* aMenu, const SELECTION_CONDITION& aCondition,
  70. int aOrder )
  71. {
  72. addEntry( ENTRY( aMenu, aCondition, aOrder ) );
  73. }
  74. void CONDITIONAL_MENU::AddSeparator( int aOrder )
  75. {
  76. addEntry( ENTRY( SELECTION_CONDITIONS::ShowAlways, aOrder ) );
  77. }
  78. SELECTION g_resolveDummySelection;
  79. void CONDITIONAL_MENU::Resolve()
  80. {
  81. Evaluate( g_resolveDummySelection );
  82. UpdateAll();
  83. runOnSubmenus( [] ( ACTION_MENU* aMenu ) {
  84. CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( aMenu );
  85. if( conditionalMenu )
  86. conditionalMenu->Resolve();
  87. } );
  88. }
  89. void CONDITIONAL_MENU::Evaluate( SELECTION& aSelection )
  90. {
  91. Clear();
  92. // We try to avoid adding useless separators (when no menuitems between separators)
  93. int menu_count = 0; // number of menus since the latest separator
  94. for( const ENTRY& entry : m_entries )
  95. {
  96. const SELECTION_CONDITION& cond = entry.Condition();
  97. bool result;
  98. wxMenuItem* menuItem = nullptr;
  99. try
  100. {
  101. result = cond( aSelection );
  102. }
  103. catch( std::exception& )
  104. {
  105. continue;
  106. }
  107. if( m_isContextMenu && !result )
  108. continue;
  109. switch( entry.Type() )
  110. {
  111. case ENTRY::ACTION:
  112. menuItem = Add( *entry.Action(), entry.IsCheckmarkEntry() );
  113. menu_count++;
  114. break;
  115. case ENTRY::MENU:
  116. menuItem = Add( entry.Menu() );
  117. menu_count++;
  118. break;
  119. case ENTRY::WXITEM:
  120. #ifdef __WXMAC__
  121. // Instantiate the Preferences item only on the first Resolve(); after that
  122. // wxWidgets will have moved it to the Application menu
  123. if( entry.wxItem()->GetId() == wxID_PREFERENCES )
  124. {
  125. if( &aSelection != &g_resolveDummySelection )
  126. continue;
  127. }
  128. #endif
  129. menuItem = new wxMenuItem( this,
  130. entry.wxItem()->GetId(),
  131. entry.wxItem()->GetItemLabel(),
  132. entry.wxItem()->GetHelp(),
  133. entry.wxItem()->GetKind() );
  134. if( entry.GetIcon() )
  135. AddBitmapToMenuItem( menuItem, KiBitmap( entry.GetIcon() ) );
  136. // the wxMenuItem must be append only after the bitmap is set:
  137. Append( menuItem );
  138. menu_count++;
  139. break;
  140. case ENTRY::SEPARATOR:
  141. if( menu_count )
  142. menuItem = AppendSeparator();
  143. menu_count = 0;
  144. break;
  145. default:
  146. wxASSERT( false );
  147. break;
  148. }
  149. if( menuItem )
  150. {
  151. if( entry.IsCheckmarkEntry() )
  152. menuItem->Check( result );
  153. else
  154. menuItem->Enable( result );
  155. }
  156. }
  157. // Recursively call Evaluate on all the submenus that are CONDITIONAL_MENUs to ensure
  158. // they are updated. This is also required on GTK to make sure the menus have the proper
  159. // size when created.
  160. runOnSubmenus(
  161. [&aSelection]( ACTION_MENU* aMenu )
  162. {
  163. CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( aMenu );
  164. if( conditionalMenu )
  165. conditionalMenu->Evaluate( aSelection );
  166. } );
  167. }
  168. void CONDITIONAL_MENU::addEntry( ENTRY aEntry )
  169. {
  170. if( aEntry.Order() < 0 ) // Any order, so give it any order number
  171. aEntry.SetOrder( m_entries.size() );
  172. std::list<ENTRY>::iterator it = m_entries.begin();
  173. // Find the right spot for the entry
  174. while( it != m_entries.end() && it->Order() <= aEntry.Order() )
  175. ++it;
  176. m_entries.insert( it, aEntry );
  177. }