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.

257 lines
7.7 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
4 years ago
4 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
4 years ago
4 years ago
11 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2019 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 <bitmaps.h>
  26. #include <tool/conditional_menu.h>
  27. #include <tool/action_menu.h>
  28. #include <kiface_base.h>
  29. #include <widgets/ui_common.h>
  30. CONDITIONAL_MENU::CONDITIONAL_MENU( TOOL_INTERACTIVE* aTool ) :
  31. ACTION_MENU( true, aTool )
  32. {
  33. }
  34. ACTION_MENU* CONDITIONAL_MENU::create() const
  35. {
  36. CONDITIONAL_MENU* clone = new CONDITIONAL_MENU( m_tool );
  37. clone->m_entries = m_entries;
  38. return clone;
  39. }
  40. void CONDITIONAL_MENU::AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
  41. int aOrder )
  42. {
  43. wxASSERT( aAction.GetId() > 0 ); // Check if action was previously registered in ACTION_MANAGER
  44. addEntry( ENTRY( &aAction, aCondition, aOrder, false ) );
  45. }
  46. void CONDITIONAL_MENU::AddCheckItem( const TOOL_ACTION& aAction,
  47. const SELECTION_CONDITION& aCondition, int aOrder )
  48. {
  49. wxASSERT( aAction.GetId() > 0 ); // Check if action was previously registered in ACTION_MANAGER
  50. addEntry( ENTRY( &aAction, aCondition, aOrder, true ) );
  51. }
  52. void CONDITIONAL_MENU::AddItem( int aId, const wxString& aText, const wxString& aTooltip,
  53. BITMAPS aIcon, const SELECTION_CONDITION& aCondition,
  54. int aOrder )
  55. {
  56. wxMenuItem item( nullptr, aId, aText, aTooltip, wxITEM_NORMAL );
  57. if( !!aIcon )
  58. KIUI::AddBitmapToMenuItem( &item, KiBitmap( aIcon ) );
  59. addEntry( ENTRY( item, aIcon, aCondition, aOrder, false ) );
  60. }
  61. void CONDITIONAL_MENU::AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip,
  62. BITMAPS aIcon, const SELECTION_CONDITION& aCondition,
  63. int aOrder )
  64. {
  65. wxMenuItem item( nullptr, aId, aText, aTooltip, wxITEM_CHECK );
  66. if( !!aIcon )
  67. KIUI::AddBitmapToMenuItem( &item, KiBitmap( aIcon ) );
  68. addEntry( ENTRY( item, aIcon, aCondition, aOrder, true ) );
  69. }
  70. void CONDITIONAL_MENU::AddMenu( ACTION_MENU* aMenu, const SELECTION_CONDITION& aCondition,
  71. int aOrder )
  72. {
  73. addEntry( ENTRY( aMenu, aCondition, aOrder ) );
  74. }
  75. void CONDITIONAL_MENU::AddSeparator( int aOrder )
  76. {
  77. addEntry( ENTRY( SELECTION_CONDITIONS::ShowAlways, aOrder ) );
  78. }
  79. void CONDITIONAL_MENU::AddSeparator( const SELECTION_CONDITION& aCondition, int aOrder )
  80. {
  81. addEntry( ENTRY( aCondition, aOrder ) );
  82. }
  83. SELECTION g_resolveDummySelection;
  84. void CONDITIONAL_MENU::Resolve()
  85. {
  86. Evaluate( g_resolveDummySelection );
  87. UpdateAll();
  88. runOnSubmenus(
  89. [] ( ACTION_MENU* aMenu )
  90. {
  91. CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( aMenu );
  92. if( conditionalMenu )
  93. conditionalMenu->Resolve();
  94. } );
  95. }
  96. void CONDITIONAL_MENU::Evaluate( const SELECTION& aSelection )
  97. {
  98. Clear();
  99. // We try to avoid adding useless separators (when no menuitems between separators)
  100. int menu_count = 0; // number of menus since the latest separator
  101. for( const ENTRY& entry : m_entries )
  102. {
  103. const SELECTION_CONDITION& cond = entry.Condition();
  104. bool result;
  105. wxMenuItem* menuItem = nullptr;
  106. try
  107. {
  108. result = cond( aSelection );
  109. }
  110. catch( std::exception& )
  111. {
  112. continue;
  113. }
  114. if( !result )
  115. continue;
  116. switch( entry.Type() )
  117. {
  118. case ENTRY::ACTION:
  119. Add( *entry.Action(), entry.IsCheckmarkEntry() );
  120. menu_count++;
  121. break;
  122. case ENTRY::MENU:
  123. entry.Menu()->UpdateTitle();
  124. Add( entry.Menu()->Clone() );
  125. menu_count++;
  126. break;
  127. case ENTRY::WXITEM:
  128. menuItem = new wxMenuItem( this,
  129. entry.wxItem()->GetId(),
  130. wxGetTranslation( entry.wxItem()->GetItemLabel() ),
  131. wxGetTranslation( entry.wxItem()->GetHelp() ),
  132. entry.wxItem()->GetKind() );
  133. if( !!entry.GetIcon() )
  134. KIUI::AddBitmapToMenuItem( menuItem, KiBitmap( entry.GetIcon() ) );
  135. // the wxMenuItem must be append only after the bitmap is set:
  136. Append( menuItem );
  137. menu_count++;
  138. break;
  139. case ENTRY::SEPARATOR:
  140. if( menu_count )
  141. AppendSeparator();
  142. menu_count = 0;
  143. break;
  144. default:
  145. wxASSERT( false );
  146. break;
  147. }
  148. }
  149. // Recursively call Evaluate on all the submenus that are CONDITIONAL_MENUs to ensure
  150. // they are updated. This is also required on GTK to make sure the menus have the proper
  151. // size when created.
  152. runOnSubmenus(
  153. [&aSelection]( ACTION_MENU* aMenu )
  154. {
  155. CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( aMenu );
  156. if( conditionalMenu )
  157. conditionalMenu->Evaluate( aSelection );
  158. } );
  159. }
  160. void CONDITIONAL_MENU::addEntry( ENTRY aEntry )
  161. {
  162. if( aEntry.Order() < 0 ) // Any order, so give it any order number
  163. aEntry.SetOrder( m_entries.size() );
  164. std::list<ENTRY>::iterator it = m_entries.begin();
  165. // Find the right spot for the entry
  166. while( it != m_entries.end() && it->Order() <= aEntry.Order() )
  167. ++it;
  168. m_entries.insert( it, aEntry );
  169. }
  170. CONDITIONAL_MENU::ENTRY::ENTRY( const ENTRY& aEntry )
  171. {
  172. m_type = aEntry.m_type;
  173. m_icon = aEntry.m_icon;
  174. switch( aEntry.m_type )
  175. {
  176. case ACTION:
  177. m_data.action = aEntry.m_data.action;
  178. break;
  179. case MENU:
  180. m_data.menu = aEntry.m_data.menu;
  181. break;
  182. case WXITEM:
  183. // We own the wxItem, so we need to make a new one for the new object
  184. m_data.wxItem = new wxMenuItem( nullptr,
  185. aEntry.m_data.wxItem->GetId(),
  186. aEntry.m_data.wxItem->GetItemLabel(),
  187. aEntry.m_data.wxItem->GetHelp(),
  188. aEntry.m_data.wxItem->GetKind() );
  189. break;
  190. case SEPARATOR:
  191. break; //No data to copy
  192. }
  193. m_condition = aEntry.m_condition;
  194. m_order = aEntry.m_order;
  195. m_isCheckmarkEntry = aEntry.m_isCheckmarkEntry;
  196. }
  197. CONDITIONAL_MENU::ENTRY::~ENTRY()
  198. {
  199. if( WXITEM == m_type )
  200. delete m_data.wxItem;
  201. }