KiCad PCB EDA Suite
CONTEXT_MENU Class Reference

Class CONTEXT_MENU. More...

#include <context_menu.h>

Inheritance diagram for CONTEXT_MENU:
DIFF_PAIR_MENU GRID_MENU HIGHLIGHT_MENU LOCK_CONTEXT_MENU PAD_CONTEXT_MENU ROUTER_TOOL_MENU SELECT_MENU TRACK_WIDTH_MENU TUNER_TOOL_MENU ZONE_CONTEXT_MENU ZOOM_MENU

Public Member Functions

 CONTEXT_MENU ()
 

Default constructor

More...
 
virtual ~CONTEXT_MENU ()
 
 CONTEXT_MENU (const CONTEXT_MENU &aMenu)=delete
 
CONTEXT_MENUoperator= (const CONTEXT_MENU &aMenu)=delete
 
void SetTitle (const wxString &aTitle) override
 Function SetTitle() Sets title for the context menu. More...
 
void DisplayTitle (bool aDisplay=true)
 Function DisplayTitle() Decides whether a title for a pop up menu should be displayed. More...
 
void SetIcon (const BITMAP_OPAQUE *aIcon)
 Function SetIcon() Assigns an icon for the entry. More...
 
wxMenuItem * Add (const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
 Function Add() Adds an entry to the menu. More...
 
wxMenuItem * Add (const TOOL_ACTION &aAction)
 Function Add() Adds an entry to the menu, basing on the TOOL_ACTION object. More...
 
std::list< wxMenuItem * > Add (CONTEXT_MENU *aMenu, bool aExpand=false)
 Function Add() Adds a context menu as a submenu. More...
 
void Clear ()
 Function Clear() Removes all the entries from the menu (as well as its title). More...
 
bool HasEnabledItems () const
 Function HasEnabledItems();. More...
 
int GetSelected () const
 Function GetSelected() Returns the position of selected item. More...
 
void UpdateAll ()
 Function UpdateAll() Runs update handlers for the menu and its submenus. More...
 
void SetTool (TOOL_INTERACTIVE *aTool)
 Function SetTool() Sets a tool that is the creator of the menu. More...
 
CONTEXT_MENUClone () const
 Creates a deep, recursive copy of this CONTEXT_MENU. More...
 

Protected Member Functions

virtual CONTEXT_MENUcreate () const
 

Returns an instance of this class. It has to be overridden in inheriting classes.

More...
 
TOOL_MANAGERgetToolManager () const
 

Returns an instance of TOOL_MANAGER class.

More...
 
virtual void update ()
 Update menu state stub. More...
 
virtual OPT_TOOL_EVENT eventHandler (const wxMenuEvent &)
 Event handler stub. More...
 
void copyFrom (const CONTEXT_MENU &aMenu)
 Copies another menus data to this instance. More...
 

Static Protected Member Functions

static int getMenuId (const TOOL_ACTION &aAction)
 

Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.

More...
 

Private Member Functions

wxMenuItem * appendCopy (const wxMenuItem *aSource)
 Function appendCopy Appends a copy of wxMenuItem. More...
 
void setupEvents ()
 

Initializes handlers for events.

More...
 
void onMenuEvent (wxMenuEvent &aEvent)
 

The default menu event handler.

More...
 
void updateHotKeys ()
 

Updates hot key settings for TOOL_ACTIONs in this menu.

More...
 
void runEventHandlers (const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
 

Traverses the submenus tree looking for a submenu capable of handling a particular menu event.

More...
 
void runOnSubmenus (std::function< void(CONTEXT_MENU *)> aFunction)
 

Runs a function on the menu and all its submenus.

More...
 
OPT_TOOL_EVENT findToolAction (int aId)
 

Checks if any of submenus contains a TOOL_ACTION with a specific ID.

More...
 

Private Attributes

bool m_titleDisplayed
 

Flag indicating that the menu title was set up.

More...
 
wxString m_title
 

Menu title

More...
 
int m_selected
 

Stores the id number of selected item.

More...
 
TOOL_INTERACTIVEm_tool
 

Creator of the menu

More...
 
std::map< int, const TOOL_ACTION * > m_toolActions
 

Associates tool actions with menu item IDs. Non-owning.

More...
 
std::list< CONTEXT_MENU * > m_submenus
 

List of submenus.

More...
 
const BITMAP_OPAQUEm_icon
 

Optional icon

More...
 

Static Private Attributes

static const int ACTION_ID = 2000
 

Menu items with ID higher than that are considered TOOL_ACTIONs

More...
 

Friends

class TOOL_INTERACTIVE
 

Detailed Description

Class CONTEXT_MENU.

Defines the structure of a context (usually right-click) popup menu for a given tool.

Definition at line 44 of file context_menu.h.

Constructor & Destructor Documentation

CONTEXT_MENU::CONTEXT_MENU ( )

Default constructor

Definition at line 36 of file context_menu.cpp.

References setupEvents().

Referenced by create().

36  :
37  m_titleDisplayed( false ), m_selected( -1 ), m_tool( nullptr ), m_icon( nullptr )
38 {
39  setupEvents();
40 }
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: context_menu.h:219
int m_selected
Stores the id number of selected item.
Definition: context_menu.h:216
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: context_menu.h:231
void setupEvents()
Initializes handlers for events.
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: context_menu.h:210
CONTEXT_MENU::~CONTEXT_MENU ( )
virtual

Definition at line 43 of file context_menu.cpp.

References m_submenus.

44 {
45  // Set parent to NULL to prevent submenus from unregistering from a notexisting object
46  for( auto menu : m_submenus )
47  menu->SetParent( nullptr );
48 
49  CONTEXT_MENU* parent = dynamic_cast<CONTEXT_MENU*>( GetParent() );
50  wxASSERT( parent || !GetParent() );
51 
52  if( parent )
53  parent->m_submenus.remove( this );
54 }
Class CONTEXT_MENU.
Definition: context_menu.h:44
std::list< CONTEXT_MENU * > m_submenus
List of submenus.
Definition: context_menu.h:228
CONTEXT_MENU::CONTEXT_MENU ( const CONTEXT_MENU aMenu)
delete

Member Function Documentation

wxMenuItem * CONTEXT_MENU::Add ( const wxString &  aLabel,
int  aId,
const BITMAP_OPAQUE aIcon = NULL 
)

Function Add() Adds an entry to the menu.

After highlighting/selecting the entry, a TOOL_EVENT command is sent that contains ID of the entry.

Parameters
aLabelis the text label show in the menu.
aIdis the ID that is sent in the TOOL_EVENT. It should be unique for every entry.
aIconis an optional icon.

Definition at line 136 of file context_menu.cpp.

References set_wxMenuIcon().

Referenced by GERBVIEW_SELECTION_TOOL::disambiguationMenu(), SELECTION_TOOL::doSelectionMenu(), CONDITIONAL_MENU::Generate(), and PAD_CONTEXT_MENU::PAD_CONTEXT_MENU().

137 {
138 #ifdef DEBUG
139  if( FindItem( aId ) != NULL )
140  wxLogWarning( wxT( "Adding more than one menu entry with the same ID may result in"
141  "undefined behaviour" ) );
142 #endif
143 
144  wxMenuItem* item = new wxMenuItem( this, aId, aLabel, wxEmptyString, wxITEM_NORMAL );
145  set_wxMenuIcon( item, aIcon );
146 
147  return Append( item );
148 }
static void set_wxMenuIcon(wxMenuItem *aMenu, const BITMAP_OPAQUE *aIcon)
wxMenuItem * CONTEXT_MENU::Add ( const TOOL_ACTION aAction)

Function Add() Adds an entry to the menu, basing on the TOOL_ACTION object.

After selecting the entry, a TOOL_EVENT command containing name of the action is sent.

Parameters
aActionis the action to be added to menu entry.

ID numbers for tool actions need to have a value higher than ACTION_ID

Definition at line 151 of file context_menu.cpp.

References TOOL_ACTION::GetDescription(), TOOL_ACTION::GetIcon(), getMenuId(), TOOL_ACTION::GetMenuItem(), i, m_toolActions, and set_wxMenuIcon().

152 {
154  const BITMAP_OPAQUE* icon = aAction.GetIcon();
155 
156  wxMenuItem* item = new wxMenuItem( this, getMenuId( aAction ), aAction.GetMenuItem(),
157  aAction.GetDescription(), wxITEM_NORMAL );
158 
159  set_wxMenuIcon( item, icon );
160 
161  m_toolActions[getMenuId( aAction )] = &aAction;
162 
163  wxMenuItem* i = Append( item );
164  return i;
165 }
PNG memory record (file in memory).
Definition: bitmap_types.h:43
const wxString & GetDescription() const
Definition: tool_action.h:124
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: context_menu.h:225
const wxString & GetMenuItem() const
Definition: tool_action.h:114
static int getMenuId(const TOOL_ACTION &aAction)
Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.
Definition: context_menu.h:155
size_t i
Definition: json11.cpp:597
const BITMAP_OPAQUE * GetIcon() const
Returns an icon associated with the action.
Definition: tool_action.h:165
static void set_wxMenuIcon(wxMenuItem *aMenu, const BITMAP_OPAQUE *aIcon)
std::list< wxMenuItem * > CONTEXT_MENU::Add ( CONTEXT_MENU aMenu,
bool  aExpand = false 
)

Function Add() Adds a context menu as a submenu.

The difference between this function and wxMenu::AppendSubMenu() is the capability to handle icons.

Parameters
aMenuis the submenu to be added.
aExpandallows one to add all entries from the menu as individual entries rather than add everything as a submenu.

Definition at line 168 of file context_menu.cpp.

References appendCopy(), Clone(), i, m_icon, m_submenus, m_title, and set_wxMenuIcon().

169 {
170  std::list<wxMenuItem*> items;
171  CONTEXT_MENU* menuCopy = aMenu->Clone();
172  m_submenus.push_back( menuCopy );
173 
174  if( aExpand )
175  {
176  for( int i = 0; i < (int) aMenu->GetMenuItemCount(); ++i )
177  {
178  wxMenuItem* item = aMenu->FindItemByPosition( i );
179  items.push_back( appendCopy( item ) );
180  }
181  }
182  else
183  {
184  wxASSERT_MSG( !menuCopy->m_title.IsEmpty(), "Set a title for CONTEXT_MENU using SetTitle()" );
185 
186  if( aMenu->m_icon )
187  {
188  wxMenuItem* newItem = new wxMenuItem( this, -1, menuCopy->m_title );
189  set_wxMenuIcon( newItem, aMenu->m_icon );
190  newItem->SetSubMenu( menuCopy );
191  items.push_back( Append( newItem ) );
192  }
193  else
194  {
195  items.push_back( AppendSubMenu( menuCopy, menuCopy->m_title ) );
196  }
197  }
198 
199  return items;
200 }
Class CONTEXT_MENU.
Definition: context_menu.h:44
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: context_menu.h:231
CONTEXT_MENU * Clone() const
Creates a deep, recursive copy of this CONTEXT_MENU.
size_t i
Definition: json11.cpp:597
std::list< CONTEXT_MENU * > m_submenus
List of submenus.
Definition: context_menu.h:228
wxMenuItem * appendCopy(const wxMenuItem *aSource)
Function appendCopy Appends a copy of wxMenuItem.
wxString m_title
Menu title
Definition: context_menu.h:213
static void set_wxMenuIcon(wxMenuItem *aMenu, const BITMAP_OPAQUE *aIcon)
wxMenuItem * CONTEXT_MENU::appendCopy ( const wxMenuItem *  aSource)
private

Function appendCopy Appends a copy of wxMenuItem.

Definition at line 454 of file context_menu.cpp.

References Clone(), PGM_BASE::CommonSettings(), m_submenus, Pgm(), and USE_ICONS_IN_MENUS_KEY.

Referenced by Add(), copyFrom(), and eventHandler().

455 {
456  wxMenuItem* newItem = new wxMenuItem( this, aSource->GetId(), aSource->GetItemLabel(),
457  aSource->GetHelp(), aSource->GetKind() );
458 
459  bool useImagesInMenus;
460  Pgm().CommonSettings()->Read( USE_ICONS_IN_MENUS_KEY, &useImagesInMenus );
461 
462  if( aSource->GetKind() == wxITEM_NORMAL && useImagesInMenus )
463  newItem->SetBitmap( aSource->GetBitmap() );
464 
465  if( aSource->IsSubMenu() )
466  {
467  CONTEXT_MENU* menu = dynamic_cast<CONTEXT_MENU*>( aSource->GetSubMenu() );
468  wxASSERT_MSG( menu, "Submenus are expected to be a CONTEXT_MENU" );
469 
470  if( menu )
471  {
472  CONTEXT_MENU* menuCopy = menu->Clone();
473  newItem->SetSubMenu( menuCopy );
474  m_submenus.push_back( menuCopy );
475  }
476  }
477 
478  // wxMenuItem has to be added before enabling/disabling or checking
479  Append( newItem );
480 
481  if( aSource->IsCheckable() )
482  newItem->Check( aSource->IsChecked() );
483 
484  newItem->Enable( aSource->IsEnabled() );
485 
486  return newItem;
487 }
Class CONTEXT_MENU.
Definition: context_menu.h:44
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
CONTEXT_MENU * Clone() const
Creates a deep, recursive copy of this CONTEXT_MENU.
VTBL_ENTRY wxConfigBase * CommonSettings() const
Definition: pgm_base.h:187
#define USE_ICONS_IN_MENUS_KEY
Definition: pgm_base.h:44
std::list< CONTEXT_MENU * > m_submenus
List of submenus.
Definition: context_menu.h:228
void CONTEXT_MENU::Clear ( )

Function Clear() Removes all the entries from the menu (as well as its title).

It leaves the menu in the initial state.

Definition at line 203 of file context_menu.cpp.

References i, m_submenus, m_titleDisplayed, and m_toolActions.

Referenced by Clone().

204 {
205  m_titleDisplayed = false;
206 
207  for( int i = GetMenuItemCount() - 1; i >= 0; --i )
208  Destroy( FindItemByPosition( i ) );
209 
210  m_toolActions.clear();
211  m_submenus.clear();
212 
213  wxASSERT( GetMenuItemCount() == 0 );
214 }
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: context_menu.h:225
size_t i
Definition: json11.cpp:597
std::list< CONTEXT_MENU * > m_submenus
List of submenus.
Definition: context_menu.h:228
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: context_menu.h:210
CONTEXT_MENU * CONTEXT_MENU::Clone ( ) const

Creates a deep, recursive copy of this CONTEXT_MENU.

Definition at line 261 of file context_menu.cpp.

References Clear(), copyFrom(), and create().

Referenced by Add(), appendCopy(), TOOL_MANAGER::dispatchContextMenu(), and GetSelected().

262 {
263  CONTEXT_MENU* clone = create();
264  clone->Clear();
265  clone->copyFrom( *this );
266  return clone;
267 }
Class CONTEXT_MENU.
Definition: context_menu.h:44
void copyFrom(const CONTEXT_MENU &aMenu)
Copies another menus data to this instance.
void Clear()
Function Clear() Removes all the entries from the menu (as well as its title).
virtual CONTEXT_MENU * create() const
Returns an instance of this class. It has to be overridden in inheriting classes. ...
void CONTEXT_MENU::copyFrom ( const CONTEXT_MENU aMenu)
protected

Copies another menus data to this instance.

Old entries are preserved, and ones form aMenu are copied.

Definition at line 436 of file context_menu.cpp.

References appendCopy(), i, m_icon, m_selected, m_title, m_titleDisplayed, m_tool, and m_toolActions.

Referenced by Clone(), and eventHandler().

437 {
438  m_icon = aMenu.m_icon;
439  m_title = aMenu.m_title;
441  m_selected = -1; // aMenu.m_selected;
442  m_tool = aMenu.m_tool;
444 
445  // Copy all menu entries
446  for( int i = 0; i < (int) aMenu.GetMenuItemCount(); ++i )
447  {
448  wxMenuItem* item = aMenu.FindItemByPosition( i );
449  appendCopy( item );
450  }
451 }
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: context_menu.h:219
int m_selected
Stores the id number of selected item.
Definition: context_menu.h:216
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: context_menu.h:231
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: context_menu.h:225
size_t i
Definition: json11.cpp:597
wxMenuItem * appendCopy(const wxMenuItem *aSource)
Function appendCopy Appends a copy of wxMenuItem.
wxString m_title
Menu title
Definition: context_menu.h:213
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: context_menu.h:210
CONTEXT_MENU * CONTEXT_MENU::create ( ) const
protectedvirtual

Returns an instance of this class. It has to be overridden in inheriting classes.

Reimplemented in ROUTER_TOOL_MENU, DIFF_PAIR_MENU, LOCK_CONTEXT_MENU, TRACK_WIDTH_MENU, SELECT_MENU, ZONE_CONTEXT_MENU, HIGHLIGHT_MENU, TUNER_TOOL_MENU, PAD_CONTEXT_MENU, GRID_MENU, and ZOOM_MENU.

Definition at line 270 of file context_menu.cpp.

References CONTEXT_MENU(), and Format().

Referenced by Clone(), and GetSelected().

271 {
272  CONTEXT_MENU* menu = new CONTEXT_MENU();
273 
274  wxASSERT_MSG( typeid( *this ) == typeid( *menu ),
275  wxString::Format( "You need to override create() method for class %s", typeid(*this).name() ) );
276 
277  return menu;
278 }
Class CONTEXT_MENU.
Definition: context_menu.h:44
CONTEXT_MENU()
Default constructor
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void CONTEXT_MENU::DisplayTitle ( bool  aDisplay = true)

Function DisplayTitle() Decides whether a title for a pop up menu should be displayed.

Definition at line 99 of file context_menu.cpp.

References m_icon, m_title, m_titleDisplayed, and set_wxMenuIcon().

Referenced by GERBVIEW_SELECTION_TOOL::disambiguationMenu(), SELECTION_TOOL::doSelectionMenu(), and SetTitle().

100 {
101  if( ( !aDisplay || m_title.IsEmpty() ) && m_titleDisplayed )
102  {
103  // Destroy the menu entry keeping the title..
104  wxMenuItem* item = FindItemByPosition( 0 );
105  wxASSERT( item->GetItemLabelText() == GetTitle() );
106  Destroy( item );
107  // ..and separator
108  item = FindItemByPosition( 0 );
109  wxASSERT( item->IsSeparator() );
110  Destroy( item );
111  m_titleDisplayed = false;
112  }
113 
114  else if( aDisplay && !m_title.IsEmpty() )
115  {
116  if( m_titleDisplayed )
117  {
118  // Simply update the title
119  FindItemByPosition( 0 )->SetItemLabel( m_title );
120  }
121  else
122  {
123  // Add a separator and a menu entry to display the title
124  InsertSeparator( 0 );
125  Insert( 0, new wxMenuItem( this, wxID_NONE, m_title, wxEmptyString, wxITEM_NORMAL ) );
126 
127  if( m_icon )
128  set_wxMenuIcon( FindItemByPosition( 0 ), m_icon );
129 
130  m_titleDisplayed = true;
131  }
132  }
133 }
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: context_menu.h:231
wxString m_title
Menu title
Definition: context_menu.h:213
static void set_wxMenuIcon(wxMenuItem *aMenu, const BITMAP_OPAQUE *aIcon)
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: context_menu.h:210
virtual OPT_TOOL_EVENT CONTEXT_MENU::eventHandler ( const wxMenuEvent &  )
inlineprotectedvirtual

Event handler stub.

It should be used if you want to generate a TOOL_EVENT from a wxMenuEvent. It will be called when a menu entry is clicked.

Reimplemented in DIFF_PAIR_MENU, TRACK_WIDTH_MENU, GRID_MENU, and ZOOM_MENU.

Definition at line 172 of file context_menu.h.

References appendCopy(), copyFrom(), findToolAction(), onMenuEvent(), runEventHandlers(), runOnSubmenus(), setupEvents(), and updateHotKeys().

Referenced by onMenuEvent(), and runEventHandlers().

173  {
174  return OPT_TOOL_EVENT();
175  }
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:495
OPT_TOOL_EVENT CONTEXT_MENU::findToolAction ( int  aId)
private

Checks if any of submenus contains a TOOL_ACTION with a specific ID.

Definition at line 413 of file context_menu.cpp.

References runOnSubmenus().

Referenced by eventHandler(), and onMenuEvent().

414 {
415  OPT_TOOL_EVENT evt;
416 
417  auto findFunc = [&]( CONTEXT_MENU* m ) {
418  if( evt )
419  return;
420 
421  const auto it = m->m_toolActions.find( aId );
422 
423  if( it != m->m_toolActions.end() )
424  evt = it->second->MakeEvent();
425  };
426 
427  findFunc( this );
428 
429  if( !evt )
430  runOnSubmenus( findFunc );
431 
432  return evt;
433 }
Class CONTEXT_MENU.
Definition: context_menu.h:44
void runOnSubmenus(std::function< void(CONTEXT_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:495
static int CONTEXT_MENU::getMenuId ( const TOOL_ACTION aAction)
inlinestaticprotected

Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.

Definition at line 155 of file context_menu.h.

References ACTION_ID, and TOOL_ACTION::GetId().

Referenced by Add(), and PAD_CONTEXT_MENU::update().

156  {
157  return aAction.GetId() + ACTION_ID;
158  }
static const int ACTION_ID
Menu items with ID higher than that are considered TOOL_ACTIONs
Definition: context_menu.h:222
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:85
int CONTEXT_MENU::GetSelected ( ) const
inline

Function GetSelected() Returns the position of selected item.

If the returned value is negative, that means that menu was dismissed.

Returns
The position of selected item in the context menu.

Definition at line 124 of file context_menu.h.

References Clone(), create(), getToolManager(), m_selected, SetTool(), and UpdateAll().

Referenced by LIB_EDIT_FRAME::OnViewEntryDoc().

125  {
126  return m_selected;
127  }
int m_selected
Stores the id number of selected item.
Definition: context_menu.h:216
TOOL_MANAGER * CONTEXT_MENU::getToolManager ( ) const
protected

Returns an instance of TOOL_MANAGER class.

Definition at line 281 of file context_menu.cpp.

References TOOL_BASE::GetManager(), and m_tool.

Referenced by GetSelected(), PAD_CONTEXT_MENU::update(), and updateHotKeys().

282 {
283  wxASSERT( m_tool );
284  return m_tool ? m_tool->GetManager() : nullptr;
285 }
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: context_menu.h:219
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool...
Definition: tool_base.h:144
bool CONTEXT_MENU::HasEnabledItems ( ) const

Function HasEnabledItems();.

Returns true if the menu has any enabled items

Definition at line 217 of file context_menu.cpp.

218 {
219  bool hasEnabled = false;
220 
221  auto& items = GetMenuItems();
222 
223  for( auto item : items )
224  {
225  if( item->IsEnabled() && !item->IsSeparator() )
226  {
227  hasEnabled = true;
228  break;
229  }
230  }
231 
232  return hasEnabled;
233 }
void CONTEXT_MENU::onMenuEvent ( wxMenuEvent &  aEvent)
private

The default menu event handler.

Definition at line 322 of file context_menu.cpp.

References ACTION_ID, eventHandler(), findToolAction(), TOOL_BASE::GetManager(), m_selected, m_tool, TOOL_MANAGER::ProcessEvent(), runEventHandlers(), TA_CONTEXT_MENU_CHOICE, TA_CONTEXT_MENU_UPDATE, and TC_COMMAND.

Referenced by eventHandler(), and setupEvents().

323 {
324  OPT_TOOL_EVENT evt;
325 
326  wxEventType type = aEvent.GetEventType();
327 
328  // When the currently chosen item in the menu is changed, an update event is issued.
329  // For example, the selection tool can use this to dynamically highlight the current item
330  // from selection clarification popup.
331  if( type == wxEVT_MENU_HIGHLIGHT )
332  evt = TOOL_EVENT( TC_COMMAND, TA_CONTEXT_MENU_UPDATE, aEvent.GetId() );
333 
334  // One of menu entries was selected..
335  else if( type == wxEVT_COMMAND_MENU_SELECTED )
336  {
337  // Store the selected position, so it can be checked by the tools
338  m_selected = aEvent.GetId();
339 
340  CONTEXT_MENU* parent = dynamic_cast<CONTEXT_MENU*>( GetParent() );
341 
342  while( parent )
343  {
344  parent->m_selected = m_selected;
345  parent = dynamic_cast<CONTEXT_MENU*>( parent->GetParent() );
346  }
347 
348  // Check if there is a TOOL_ACTION for the given ID
349  if( m_selected >= ACTION_ID )
350  evt = findToolAction( m_selected );
351 
352  if( !evt )
353  {
354 #ifdef __WINDOWS__
355  if( !evt )
356  {
357  // Try to find the submenu which holds the selected item
358  wxMenu* menu = nullptr;
359  FindItem( m_selected, &menu );
360 
361  if( menu && menu != this )
362  {
363  CONTEXT_MENU* cxmenu = static_cast<CONTEXT_MENU*>( menu );
364  evt = cxmenu->eventHandler( aEvent );
365  }
366  }
367 #else
368  if( !evt )
369  runEventHandlers( aEvent, evt );
370 #endif
371 
372  // Handling non-action menu entries (e.g. items in clarification list)
373  if( !evt )
374  evt = TOOL_EVENT( TC_COMMAND, TA_CONTEXT_MENU_CHOICE, aEvent.GetId() );
375  }
376  }
377 
378  // forward the action/update event to the TOOL_MANAGER
379  // clients that don't supply a tool will have to check GetSelected() themselves
380  if( evt && m_tool )
381  {
382  //aEvent.StopPropagation();
383  m_tool->GetManager()->ProcessEvent( *evt );
384  }
385 }
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: context_menu.h:219
void runEventHandlers(const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
Traverses the submenus tree looking for a submenu capable of handling a particular menu event...
int m_selected
Stores the id number of selected item.
Definition: context_menu.h:216
OPT_TOOL_EVENT findToolAction(int aId)
Checks if any of submenus contains a TOOL_ACTION with a specific ID.
Class CONTEXT_MENU.
Definition: context_menu.h:44
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
Definition: context_menu.h:172
static const int ACTION_ID
Menu items with ID higher than that are considered TOOL_ACTIONs
Definition: context_menu.h:222
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
Class TOOL_EVENT.
Definition: tool_event.h:168
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool...
Definition: tool_base.h:144
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:495
CONTEXT_MENU& CONTEXT_MENU::operator= ( const CONTEXT_MENU aMenu)
delete
void CONTEXT_MENU::runEventHandlers ( const wxMenuEvent &  aMenuEvent,
OPT_TOOL_EVENT aToolEvent 
)
private

Traverses the submenus tree looking for a submenu capable of handling a particular menu event.

In case it is handled, it is returned the aToolEvent parameter.

Definition at line 388 of file context_menu.cpp.

References eventHandler(), and runOnSubmenus().

Referenced by eventHandler(), and onMenuEvent().

389 {
390  aToolEvent = eventHandler( aMenuEvent );
391 
392  if( !aToolEvent )
393  runOnSubmenus( std::bind( &CONTEXT_MENU::runEventHandlers, _1, aMenuEvent, aToolEvent ) );
394 }
void runEventHandlers(const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
Traverses the submenus tree looking for a submenu capable of handling a particular menu event...
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
Definition: context_menu.h:172
void runOnSubmenus(std::function< void(CONTEXT_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
void CONTEXT_MENU::runOnSubmenus ( std::function< void(CONTEXT_MENU *)>  aFunction)
private

Runs a function on the menu and all its submenus.

Definition at line 397 of file context_menu.cpp.

References Format(), and m_submenus.

Referenced by eventHandler(), findToolAction(), runEventHandlers(), SetTool(), and UpdateAll().

398 {
399  try
400  {
401  std::for_each( m_submenus.begin(), m_submenus.end(), [&]( CONTEXT_MENU* m ) {
402  aFunction( m );
403  m->runOnSubmenus( aFunction );
404  } );
405  }
406  catch( std::exception& e )
407  {
408  wxLogDebug( wxString::Format( "CONTEXT_MENU runOnSubmenus exception: %s", e.what() ) );
409  }
410 }
Class CONTEXT_MENU.
Definition: context_menu.h:44
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
std::list< CONTEXT_MENU * > m_submenus
List of submenus.
Definition: context_menu.h:228
void CONTEXT_MENU::SetIcon ( const BITMAP_OPAQUE aIcon)

Function SetIcon() Assigns an icon for the entry.

Parameters
aIconis the icon to be assigned. NULL is used to remove icon.

Definition at line 75 of file context_menu.cpp.

References m_icon.

Referenced by GERBVIEW_SELECTION_TOOL::disambiguationMenu(), SELECTION_TOOL::doSelectionMenu(), GRID_MENU::GRID_MENU(), ALIGN_DISTRIBUTE_TOOL::Init(), PAD_CONTEXT_MENU::PAD_CONTEXT_MENU(), and ZOOM_MENU::ZOOM_MENU().

76 {
77  m_icon = aIcon;
78 }
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: context_menu.h:231
void CONTEXT_MENU::SetTitle ( const wxString &  aTitle)
override

Function SetTitle() Sets title for the context menu.

The title is shown as a text label shown on the top of the menu.

Parameters
aTitleis the new title.

Definition at line 88 of file context_menu.cpp.

References DisplayTitle(), m_title, and m_titleDisplayed.

Referenced by GERBVIEW_SELECTION_TOOL::disambiguationMenu(), SELECTION_TOOL::doSelectionMenu(), GRID_MENU::GRID_MENU(), PAD_CONTEXT_MENU::PAD_CONTEXT_MENU(), and ZOOM_MENU::ZOOM_MENU().

89 {
90  // Unfortunately wxMenu::SetTitle() does not work very well, so this is an alternative version
91  m_title = aTitle;
92 
93  // Update the menu title
94  if( m_titleDisplayed )
95  DisplayTitle( true );
96 }
void DisplayTitle(bool aDisplay=true)
Function DisplayTitle() Decides whether a title for a pop up menu should be displayed.
wxString m_title
Menu title
Definition: context_menu.h:213
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: context_menu.h:210
void CONTEXT_MENU::SetTool ( TOOL_INTERACTIVE aTool)

Function SetTool() Sets a tool that is the creator of the menu.

Parameters
aToolis the tool that created the menu.

Definition at line 254 of file context_menu.cpp.

References m_tool, and runOnSubmenus().

Referenced by CONDITIONAL_MENU::Generate(), GetSelected(), and TOOL_INTERACTIVE::SetContextMenu().

255 {
256  m_tool = aTool;
257  runOnSubmenus( std::bind( &CONTEXT_MENU::SetTool, _1, aTool ) );
258 }
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: context_menu.h:219
void runOnSubmenus(std::function< void(CONTEXT_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
void SetTool(TOOL_INTERACTIVE *aTool)
Function SetTool() Sets a tool that is the creator of the menu.
void CONTEXT_MENU::setupEvents ( )
private

Initializes handlers for events.

Definition at line 81 of file context_menu.cpp.

References onMenuEvent().

Referenced by CONTEXT_MENU(), and eventHandler().

82 {
83  Connect( wxEVT_MENU_HIGHLIGHT, wxMenuEventHandler( CONTEXT_MENU::onMenuEvent ), NULL, this );
84  Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( CONTEXT_MENU::onMenuEvent ), NULL, this );
85 }
void onMenuEvent(wxMenuEvent &aEvent)
The default menu event handler.
virtual void CONTEXT_MENU::update ( )
inlineprotectedvirtual

Update menu state stub.

It is called before a menu is shown, in order to update its state. Here you can tick current settings, enable/disable entries, etc.

Reimplemented in DIFF_PAIR_MENU, TRACK_WIDTH_MENU, ZONE_CONTEXT_MENU, SELECT_MENU, PAD_CONTEXT_MENU, HIGHLIGHT_MENU, GRID_MENU, and ZOOM_MENU.

Definition at line 164 of file context_menu.h.

Referenced by UpdateAll().

165  {
166  }
void CONTEXT_MENU::UpdateAll ( )

Function UpdateAll() Runs update handlers for the menu and its submenus.

Definition at line 236 of file context_menu.cpp.

References Format(), m_tool, runOnSubmenus(), update(), and updateHotKeys().

Referenced by GetSelected().

237 {
238  try
239  {
240  update();
241  }
242  catch( std::exception& e )
243  {
244  wxLogDebug( wxString::Format( "CONTEXT_MENU update handler exception: %s", e.what() ) );
245  }
246 
247  if( m_tool )
248  updateHotKeys();
249 
250  runOnSubmenus( std::bind( &CONTEXT_MENU::UpdateAll, _1 ) );
251 }
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: context_menu.h:219
void updateHotKeys()
Updates hot key settings for TOOL_ACTIONs in this menu.
virtual void update()
Update menu state stub.
Definition: context_menu.h:164
void runOnSubmenus(std::function< void(CONTEXT_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
void UpdateAll()
Function UpdateAll() Runs update handlers for the menu and its submenus.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void CONTEXT_MENU::updateHotKeys ( )
private

Updates hot key settings for TOOL_ACTIONs in this menu.

Definition at line 288 of file context_menu.cpp.

References TOOL_MANAGER::GetHotKey(), getToolManager(), m_toolActions, MD_ALT, MD_CTRL, MD_MODIFIER_MASK, MD_SHIFT, and mod.

Referenced by eventHandler(), and UpdateAll().

289 {
290  TOOL_MANAGER* toolMgr = getToolManager();
291 
292  for( std::map<int, const TOOL_ACTION*>::const_iterator it = m_toolActions.begin();
293  it != m_toolActions.end(); ++it )
294  {
295  int id = it->first;
296  const TOOL_ACTION& action = *it->second;
297  int key = toolMgr->GetHotKey( action ) & ~MD_MODIFIER_MASK;
298 
299  if( key )
300  {
301  int mod = toolMgr->GetHotKey( action ) & MD_MODIFIER_MASK;
302  int flags = 0;
303  wxMenuItem* item = FindChildItem( id );
304 
305  if( item )
306  {
307  flags |= ( mod & MD_ALT ) ? wxACCEL_ALT : 0;
308  flags |= ( mod & MD_CTRL ) ? wxACCEL_CTRL : 0;
309  flags |= ( mod & MD_SHIFT ) ? wxACCEL_SHIFT : 0;
310 
311  if( !flags )
312  flags = wxACCEL_NORMAL;
313 
314  wxAcceleratorEntry accel( flags, key, id, item );
315  item->SetAccel( &accel );
316  }
317  }
318  }
319 }
int GetHotKey(const TOOL_ACTION &aAction)
>
Class TOOL_MANAGER.
Definition: tool_manager.h:49
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: context_menu.h:225
TOOL_MANAGER * getToolManager() const
Returns an instance of TOOL_MANAGER class.
Class TOOL_ACTION.
Definition: tool_action.h:46
#define mod(a, n)
Definition: greymap.cpp:24

Friends And Related Function Documentation

friend class TOOL_INTERACTIVE
friend

Definition at line 233 of file context_menu.h.

Member Data Documentation

const int CONTEXT_MENU::ACTION_ID = 2000
staticprivate

Menu items with ID higher than that are considered TOOL_ACTIONs

Definition at line 222 of file context_menu.h.

Referenced by getMenuId(), and onMenuEvent().

const BITMAP_OPAQUE* CONTEXT_MENU::m_icon
private

Optional icon

Definition at line 231 of file context_menu.h.

Referenced by Add(), copyFrom(), DisplayTitle(), and SetIcon().

int CONTEXT_MENU::m_selected
private

Stores the id number of selected item.

Definition at line 216 of file context_menu.h.

Referenced by copyFrom(), GetSelected(), and onMenuEvent().

std::list<CONTEXT_MENU*> CONTEXT_MENU::m_submenus
private

List of submenus.

Definition at line 228 of file context_menu.h.

Referenced by Add(), appendCopy(), Clear(), runOnSubmenus(), and ~CONTEXT_MENU().

wxString CONTEXT_MENU::m_title
private

Menu title

Definition at line 213 of file context_menu.h.

Referenced by Add(), copyFrom(), DisplayTitle(), and SetTitle().

bool CONTEXT_MENU::m_titleDisplayed
private

Flag indicating that the menu title was set up.

Definition at line 210 of file context_menu.h.

Referenced by Clear(), copyFrom(), DisplayTitle(), and SetTitle().

TOOL_INTERACTIVE* CONTEXT_MENU::m_tool
private

Creator of the menu

Definition at line 219 of file context_menu.h.

Referenced by copyFrom(), getToolManager(), onMenuEvent(), SetTool(), and UpdateAll().

std::map<int, const TOOL_ACTION*> CONTEXT_MENU::m_toolActions
private

Associates tool actions with menu item IDs. Non-owning.

Definition at line 225 of file context_menu.h.

Referenced by Add(), Clear(), copyFrom(), and updateHotKeys().


The documentation for this class was generated from the following files: