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.

287 lines
8.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
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-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 <tool/conditional_menu.h>
  26. #include <tool/action_menu.h>
  27. #include <menus_helpers.h>
  28. #include <kiface_i.h>
  29. CONDITIONAL_MENU::CONDITIONAL_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool ) :
  30. ACTION_MENU( isContextMenu )
  31. {
  32. m_tool = aTool;
  33. }
  34. ACTION_MENU* CONDITIONAL_MENU::create() const
  35. {
  36. CONDITIONAL_MENU* clone = new CONDITIONAL_MENU( m_isContextMenu, 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. BITMAP_DEF aIcon, const SELECTION_CONDITION& aCondition,
  54. int aOrder )
  55. {
  56. wxMenuItem item( nullptr, aId, aText, aTooltip, wxITEM_NORMAL );
  57. if( aIcon )
  58. 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. BITMAP_DEF aIcon, const SELECTION_CONDITION& aCondition,
  63. int aOrder )
  64. {
  65. wxMenuItem item( nullptr, aId, aText, aTooltip, wxITEM_CHECK );
  66. if( aIcon )
  67. 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::AddClose( wxString aAppname )
  80. {
  81. AddItem( wxID_CLOSE, _( "Close\tCTRL+W" ), wxString::Format( "Close %s", aAppname ), exit_xpm,
  82. SELECTION_CONDITIONS::ShowAlways );
  83. }
  84. void CONDITIONAL_MENU::AddQuitOrClose( KIFACE_I* aKiface, wxString aAppname )
  85. {
  86. if( !aKiface || aKiface->IsSingle() ) // not when under a project mgr
  87. {
  88. // Don't use ACTIONS::quit; wxWidgets moves this on OSX and expects to find it via
  89. // wxID_EXIT
  90. AddItem( wxID_EXIT, _( "Quit" ), wxString::Format( "Quit %s", aAppname ), exit_xpm,
  91. SELECTION_CONDITIONS::ShowAlways );
  92. }
  93. else
  94. {
  95. AddClose( aAppname );
  96. }
  97. }
  98. SELECTION g_resolveDummySelection;
  99. void CONDITIONAL_MENU::Resolve()
  100. {
  101. Evaluate( g_resolveDummySelection );
  102. UpdateAll();
  103. runOnSubmenus( [] ( ACTION_MENU* aMenu ) {
  104. CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( aMenu );
  105. if( conditionalMenu )
  106. conditionalMenu->Resolve();
  107. } );
  108. }
  109. void CONDITIONAL_MENU::Evaluate( SELECTION& aSelection )
  110. {
  111. Clear();
  112. // We try to avoid adding useless separators (when no menuitems between separators)
  113. int menu_count = 0; // number of menus since the latest separator
  114. for( const ENTRY& entry : m_entries )
  115. {
  116. const SELECTION_CONDITION& cond = entry.Condition();
  117. bool result;
  118. wxMenuItem* menuItem = nullptr;
  119. try
  120. {
  121. result = cond( aSelection );
  122. }
  123. catch( std::exception& )
  124. {
  125. continue;
  126. }
  127. if( m_isContextMenu && !result )
  128. continue;
  129. switch( entry.Type() )
  130. {
  131. case ENTRY::ACTION:
  132. menuItem = Add( *entry.Action(), entry.IsCheckmarkEntry() );
  133. menu_count++;
  134. break;
  135. case ENTRY::MENU:
  136. menuItem = Add( entry.Menu() );
  137. menu_count++;
  138. break;
  139. case ENTRY::WXITEM:
  140. #ifdef __WXMAC__
  141. // Instantiate the Preferences item only on the first Resolve(); after that
  142. // wxWidgets will have moved it to the Application menu
  143. if( entry.wxItem()->GetId() == wxID_PREFERENCES )
  144. {
  145. if( &aSelection != &g_resolveDummySelection )
  146. continue;
  147. }
  148. #endif
  149. menuItem = new wxMenuItem( this,
  150. entry.wxItem()->GetId(),
  151. entry.wxItem()->GetItemLabel(),
  152. entry.wxItem()->GetHelp(),
  153. entry.wxItem()->GetKind() );
  154. if( entry.GetIcon() )
  155. AddBitmapToMenuItem( menuItem, KiBitmap( entry.GetIcon() ) );
  156. // the wxMenuItem must be append only after the bitmap is set:
  157. Append( menuItem );
  158. menu_count++;
  159. break;
  160. case ENTRY::SEPARATOR:
  161. if( menu_count )
  162. menuItem = AppendSeparator();
  163. menu_count = 0;
  164. break;
  165. default:
  166. wxASSERT( false );
  167. break;
  168. }
  169. if( menuItem )
  170. {
  171. if( entry.IsCheckmarkEntry() )
  172. menuItem->Check( result );
  173. else
  174. menuItem->Enable( result );
  175. }
  176. }
  177. // Recursively call Evaluate on all the submenus that are CONDITIONAL_MENUs to ensure
  178. // they are updated. This is also required on GTK to make sure the menus have the proper
  179. // size when created.
  180. runOnSubmenus(
  181. [&aSelection]( ACTION_MENU* aMenu )
  182. {
  183. CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( aMenu );
  184. if( conditionalMenu )
  185. conditionalMenu->Evaluate( aSelection );
  186. } );
  187. }
  188. void CONDITIONAL_MENU::addEntry( ENTRY aEntry )
  189. {
  190. if( aEntry.Order() < 0 ) // Any order, so give it any order number
  191. aEntry.SetOrder( m_entries.size() );
  192. std::list<ENTRY>::iterator it = m_entries.begin();
  193. // Find the right spot for the entry
  194. while( it != m_entries.end() && it->Order() <= aEntry.Order() )
  195. ++it;
  196. m_entries.insert( it, aEntry );
  197. }
  198. CONDITIONAL_MENU::ENTRY::ENTRY( const ENTRY& aEntry )
  199. {
  200. m_type = aEntry.m_type;
  201. m_icon = aEntry.m_icon;
  202. switch( aEntry.m_type )
  203. {
  204. case ACTION:
  205. m_data.action = aEntry.m_data.action;
  206. break;
  207. case MENU:
  208. m_data.menu = aEntry.m_data.menu;
  209. break;
  210. case WXITEM:
  211. // We own the wxItem, so we need to make a new one for the new object
  212. m_data.wxItem = new wxMenuItem( nullptr,
  213. aEntry.m_data.wxItem->GetId(),
  214. aEntry.m_data.wxItem->GetItemLabel(),
  215. aEntry.m_data.wxItem->GetHelp(),
  216. aEntry.m_data.wxItem->GetKind() );
  217. break;
  218. case SEPARATOR:
  219. break; //No data to copy
  220. }
  221. m_condition = aEntry.m_condition;
  222. m_order = aEntry.m_order;
  223. m_isCheckmarkEntry = aEntry.m_isCheckmarkEntry;
  224. }
  225. CONDITIONAL_MENU::ENTRY::~ENTRY()
  226. {
  227. if( WXITEM == m_type )
  228. delete m_data.wxItem;
  229. }