KiCad PCB EDA Suite
conditional_menu.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <tool/conditional_menu.h>
26 #include <tool/context_menu.h>
27 
28 void CONDITIONAL_MENU::AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
29  int aOrder )
30 {
31  assert( aAction.GetId() > 0 ); // Check if action was previously registered in ACTION_MANAGER
32  addEntry( ENTRY( &aAction, aCondition, aOrder ) );
33 }
34 
35 
36 void CONDITIONAL_MENU::AddMenu( CONTEXT_MENU* aMenu, bool aExpand,
37  const SELECTION_CONDITION& aCondition, int aOrder )
38 {
39  addEntry( ENTRY( aMenu, aExpand, aCondition, aOrder ) );
40 }
41 
42 
43 void CONDITIONAL_MENU::AddSeparator( const SELECTION_CONDITION& aCondition, int aOrder )
44 {
45  addEntry( ENTRY( aCondition, aOrder ) );
46 }
47 
48 
50 {
51  CONTEXT_MENU* m_menu = new CONTEXT_MENU;
52  m_menu->SetTool( m_tool );
53 
54  for( std::list<ENTRY>::iterator it = m_entries.begin(); it != m_entries.end(); ++it )
55  {
56  const SELECTION_CONDITION& cond = it->Condition();
57 
58  try
59  {
60  if( !cond( aSelection ) )
61  continue;
62  }
63  catch( std::exception& )
64  {
65  continue;
66  }
67 
68  switch( it->Type() )
69  {
70  case ENTRY::ACTION:
71  m_menu->Add( *it->Action() );
72  break;
73 
74  case ENTRY::MENU:
75  m_menu->Add( it->Menu(), it->Expand() );
76  break;
77 
78  case ENTRY::WXITEM:
79  m_menu->Append( it->wxItem() );
80  break;
81 
82  case ENTRY::SEPARATOR:
83  m_menu->AppendSeparator();
84  break;
85 
86  default:
87  assert( false );
88  break;
89  }
90  }
91 
92  return m_menu;
93 }
94 
95 
97 {
98  if( aEntry.Order() < 0 ) // Any order, so give it any order number
99  aEntry.SetOrder( m_entries.size() );
100 
101  std::list<ENTRY>::iterator it = m_entries.begin();
102 
103  // Find the right spot for the entry
104  while( it != m_entries.end() && it->Order() <= aEntry.Order() )
105  ++it;
106 
107  m_entries.insert( it, aEntry );
108 }
TOOL_INTERACTIVE * m_tool
tool owning the menu
Helper class to organize menu entries.
Class CONTEXT_MENU.
Definition: context_menu.h:44
void AddSeparator(const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddSeparator()
std::list< ENTRY > m_entries
List of all menu entries.
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
Function Add() Adds an entry to the menu.
void SetOrder(int aOrder)
void SetTool(TOOL_INTERACTIVE *aTool)
Function SetTool() Sets a tool that is the creator of the menu.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.
void AddMenu(CONTEXT_MENU *aMenu, bool aExpand=false, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddMenu()
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddItem()
CONTEXT_MENU * Generate(SELECTION &aSelection)
Function Generate()
Class TOOL_ACTION.
Definition: tool_action.h:46
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:85