KiCad PCB EDA Suite
ACTION_MENU Class Reference

Class ACTION_MENU. More...

#include <action_menu.h>

Inheritance diagram for ACTION_MENU:
BUS_UNFOLD_MENU CONDITIONAL_MENU DIFF_PAIR_MENU GRID_MENU HIGHLIGHT_MENU LOCK_CONTEXT_MENU PAD_CONTEXT_MENU ROUTER_TOOL_MENU SELECT_MENU SYMBOL_UNIT_MENU TRACK_WIDTH_MENU TUNER_TOOL_MENU ZONE_CONTEXT_MENU ZOOM_MENU

Public Member Functions

 ACTION_MENU ()
 

Default constructor

More...
 
virtual ~ACTION_MENU ()
 
 ACTION_MENU (const ACTION_MENU &aMenu)=delete
 
ACTION_MENUoperator= (const ACTION_MENU &aMenu)=delete
 
void SetTitle (const wxString &aTitle) override
 Function SetTitle() Sets title for the 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, bool aIsCheckmarkEntry=false)
 Function Add() Adds an entry to the menu, basing on the TOOL_ACTION object. More...
 
wxMenuItem * Add (ACTION_MENU *aMenu)
 Function Add() Adds an action 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...
 
ACTION_MENUClone () const
 Creates a deep, recursive copy of this ACTION_MENU. More...
 

Public Attributes

bool m_Dirty
 

Menu requires updating before display.

More...
 

Protected Member Functions

virtual ACTION_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 ACTION_MENU &aMenu)
 Copies another menus data to this instance. More...
 
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(ACTION_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...
 

Static Protected Member Functions

static int getMenuId (const TOOL_ACTION &aAction)
 

Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.

More...
 

Protected 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< ACTION_MENU * > m_submenus
 

List of submenus.

More...
 
const BITMAP_OPAQUE * m_icon
 

Optional icon

More...
 

Static Protected Attributes

static const int ACTION_ID = 20000
 

Menu items with ID higher than that are considered TOOL_ACTIONs

More...
 

Friends

class TOOL_INTERACTIVE
 

Detailed Description

Class ACTION_MENU.

Defines the structure of a menu based on ACTIONs.

Definition at line 43 of file action_menu.h.

Constructor & Destructor Documentation

◆ ACTION_MENU() [1/2]

ACTION_MENU::ACTION_MENU ( )

Default constructor

Definition at line 40 of file action_menu.cpp.

40  :
41  m_Dirty( true ),
42  m_titleDisplayed( false ),
43  m_selected( -1 ),
44  m_tool( nullptr ),
45  m_icon( nullptr )
46 {
47  setupEvents();
48 }
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: action_menu.h:211
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:232
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:217
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:220
bool m_Dirty
Menu requires updating before display.
Definition: action_menu.h:146
void setupEvents()
Initializes handlers for events.
Definition: action_menu.cpp:88

References setupEvents().

Referenced by create(), FOOTPRINT_EDIT_FRAME::ReCreateMenuBar(), PCB_EDIT_FRAME::ReCreateMenuBar(), and BUS_UNFOLD_MENU::update().

◆ ~ACTION_MENU()

ACTION_MENU::~ACTION_MENU ( )
virtual

Definition at line 51 of file action_menu.cpp.

52 {
53  // Set parent to NULL to prevent submenus from unregistering from a notexisting object
54  for( auto menu : m_submenus )
55  menu->SetParent( nullptr );
56 
57  ACTION_MENU* parent = dynamic_cast<ACTION_MENU*>( GetParent() );
58 
59  if( parent )
60  parent->m_submenus.remove( this );
61 }
Class ACTION_MENU.
Definition: action_menu.h:43
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:229

References m_submenus.

◆ ACTION_MENU() [2/2]

ACTION_MENU::ACTION_MENU ( const ACTION_MENU aMenu)
delete

Member Function Documentation

◆ Add() [1/3]

wxMenuItem * ACTION_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 144 of file action_menu.cpp.

145 {
146 #ifdef DEBUG
147  if( FindItem( aId ) != NULL )
148  wxLogWarning( wxT( "Adding more than one menu entry with the same ID may result in"
149  "undefined behaviour" ) );
150 #endif
151 
152  wxMenuItem* item = new wxMenuItem( this, aId, aLabel, wxEmptyString, wxITEM_NORMAL );
153  set_wxMenuIcon( item, aIcon );
154 
155  return Append( item );
156 }
static void set_wxMenuIcon(wxMenuItem *aMenu, const BITMAP_OPAQUE *aIcon)
Definition: action_menu.cpp:68

References set_wxMenuIcon().

Referenced by GERBVIEW_SELECTION_TOOL::disambiguationMenu(), EE_SELECTION_TOOL::doSelectionMenu(), SELECTION_TOOL::doSelectionMenu(), CONDITIONAL_MENU::Evaluate(), ALIGN_DISTRIBUTE_TOOL::Init(), PAD_CONTEXT_MENU::PAD_CONTEXT_MENU(), ROUTER_TOOL_MENU::ROUTER_TOOL_MENU(), and TUNER_TOOL_MENU::TUNER_TOOL_MENU().

◆ Add() [2/3]

wxMenuItem * ACTION_MENU::Add ( const TOOL_ACTION aAction,
bool  aIsCheckmarkEntry = false 
)

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 159 of file action_menu.cpp.

160 {
162  const BITMAP_OPAQUE* icon = aAction.GetIcon();
163 
164  wxMenuItem* item = new wxMenuItem( this, getMenuId( aAction ), aAction.GetMenuItem(),
165  aAction.GetDescription(),
166  aIsCheckmarkEntry ? wxITEM_CHECK : wxITEM_NORMAL );
167 
168  set_wxMenuIcon( item, icon );
169 
170  m_toolActions[getMenuId( aAction )] = &aAction;
171 
172  wxMenuItem* i = Append( item );
173  return i;
174 }
static int getMenuId(const TOOL_ACTION &aAction)
Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.
Definition: action_menu.h:156
const wxString & GetDescription() const
Definition: tool_action.h:115
static void set_wxMenuIcon(wxMenuItem *aMenu, const BITMAP_OPAQUE *aIcon)
Definition: action_menu.cpp:68
const BITMAP_OPAQUE * GetIcon() const
Returns an icon associated with the action.
Definition: tool_action.h:156
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: action_menu.h:226
const wxString & GetMenuItem() const
Definition: tool_action.h:105
size_t i
Definition: json11.cpp:597

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

◆ Add() [3/3]

wxMenuItem * ACTION_MENU::Add ( ACTION_MENU aMenu)

Function Add() Adds an action 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.

Definition at line 177 of file action_menu.cpp.

178 {
179  ACTION_MENU* menuCopy = aMenu->Clone();
180  m_submenus.push_back( menuCopy );
181 
182  wxASSERT_MSG( !menuCopy->m_title.IsEmpty(), "Set a title for ACTION_MENU using SetTitle()" );
183 
184  if( aMenu->m_icon )
185  {
186  wxMenuItem* newItem = new wxMenuItem( this, -1, menuCopy->m_title );
187  set_wxMenuIcon( newItem, aMenu->m_icon );
188  newItem->SetSubMenu( menuCopy );
189  return Append( newItem );
190  }
191  else
192  {
193  return AppendSubMenu( menuCopy, menuCopy->m_title );
194  }
195 }
Class ACTION_MENU.
Definition: action_menu.h:43
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:232
static void set_wxMenuIcon(wxMenuItem *aMenu, const BITMAP_OPAQUE *aIcon)
Definition: action_menu.cpp:68
wxString m_title
Menu title
Definition: action_menu.h:214
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:229
ACTION_MENU * Clone() const
Creates a deep, recursive copy of this ACTION_MENU.

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

◆ appendCopy()

wxMenuItem * ACTION_MENU::appendCopy ( const wxMenuItem *  aSource)
protected

Function appendCopy Appends a copy of wxMenuItem.

Definition at line 475 of file action_menu.cpp.

476 {
477  wxMenuItem* newItem = new wxMenuItem( this, aSource->GetId(), aSource->GetItemLabel(),
478  aSource->GetHelp(), aSource->GetKind() );
479 
480  bool useImagesInMenus;
481  Pgm().CommonSettings()->Read( USE_ICONS_IN_MENUS_KEY, &useImagesInMenus );
482 
483  if( aSource->GetKind() == wxITEM_NORMAL && useImagesInMenus )
484  newItem->SetBitmap( aSource->GetBitmap() );
485 
486  if( aSource->IsSubMenu() )
487  {
488  ACTION_MENU* menu = dynamic_cast<ACTION_MENU*>( aSource->GetSubMenu() );
489  wxASSERT_MSG( menu, "Submenus are expected to be a ACTION_MENU" );
490 
491  if( menu )
492  {
493  ACTION_MENU* menuCopy = menu->Clone();
494  newItem->SetSubMenu( menuCopy );
495  m_submenus.push_back( menuCopy );
496  }
497  }
498 
499  // wxMenuItem has to be added before enabling/disabling or checking
500  Append( newItem );
501 
502  if( aSource->IsCheckable() )
503  newItem->Check( aSource->IsChecked() );
504 
505  newItem->Enable( aSource->IsEnabled() );
506 
507  return newItem;
508 }
Class ACTION_MENU.
Definition: action_menu.h:43
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:229
ACTION_MENU * Clone() const
Creates a deep, recursive copy of this ACTION_MENU.
VTBL_ENTRY wxConfigBase * CommonSettings() const
Definition: pgm_base.h:190
#define USE_ICONS_IN_MENUS_KEY
Definition: pgm_base.h:45

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

Referenced by copyFrom().

◆ Clear()

void ACTION_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 198 of file action_menu.cpp.

199 {
200  m_titleDisplayed = false;
201 
202  for( int i = GetMenuItemCount() - 1; i >= 0; --i )
203  Destroy( FindItemByPosition( i ) );
204 
205  m_toolActions.clear();
206  m_submenus.clear();
207 
208  wxASSERT( GetMenuItemCount() == 0 );
209 }
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: action_menu.h:211
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:229
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: action_menu.h:226
size_t i
Definition: json11.cpp:597

References i, m_submenus, m_titleDisplayed, and m_toolActions.

Referenced by Clone(), CONDITIONAL_MENU::Evaluate(), TRACK_WIDTH_MENU::update(), and DIFF_PAIR_MENU::update().

◆ Clone()

ACTION_MENU * ACTION_MENU::Clone ( ) const

Creates a deep, recursive copy of this ACTION_MENU.

Definition at line 256 of file action_menu.cpp.

257 {
258  ACTION_MENU* clone = create();
259  clone->Clear();
260  clone->copyFrom( *this );
261  return clone;
262 }
Class ACTION_MENU.
Definition: action_menu.h:43
virtual ACTION_MENU * create() const
Returns an instance of this class. It has to be overridden in inheriting classes.
void copyFrom(const ACTION_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).

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

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

◆ copyFrom()

void ACTION_MENU::copyFrom ( const ACTION_MENU aMenu)
protected

Copies another menus data to this instance.

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

Definition at line 457 of file action_menu.cpp.

458 {
459  m_icon = aMenu.m_icon;
460  m_title = aMenu.m_title;
462  m_selected = -1; // aMenu.m_selected;
463  m_tool = aMenu.m_tool;
465 
466  // Copy all menu entries
467  for( int i = 0; i < (int) aMenu.GetMenuItemCount(); ++i )
468  {
469  wxMenuItem* item = aMenu.FindItemByPosition( i );
470  appendCopy( item );
471  }
472 }
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: action_menu.h:211
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:232
wxString m_title
Menu title
Definition: action_menu.h:214
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:217
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:220
wxMenuItem * appendCopy(const wxMenuItem *aSource)
Function appendCopy Appends a copy of wxMenuItem.
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: action_menu.h:226
size_t i
Definition: json11.cpp:597

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

Referenced by Clone().

◆ create()

ACTION_MENU * ACTION_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, SYMBOL_UNIT_MENU, ZONE_CONTEXT_MENU, SELECT_MENU, HIGHLIGHT_MENU, BUS_UNFOLD_MENU, TUNER_TOOL_MENU, PAD_CONTEXT_MENU, CONDITIONAL_MENU, GRID_MENU, and ZOOM_MENU.

Definition at line 265 of file action_menu.cpp.

266 {
267  ACTION_MENU* menu = new ACTION_MENU();
268 
269  wxASSERT_MSG( typeid( *this ) == typeid( *menu ),
270  wxString::Format( "You need to override create() method for class %s", typeid(*this).name() ) );
271 
272  return menu;
273 }
Class ACTION_MENU.
Definition: action_menu.h:43
ACTION_MENU()
Default constructor
Definition: action_menu.cpp:40
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

References ACTION_MENU(), and Format().

Referenced by Clone().

◆ DisplayTitle()

void ACTION_MENU::DisplayTitle ( bool  aDisplay = true)

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

Definition at line 107 of file action_menu.cpp.

108 {
109  if( ( !aDisplay || m_title.IsEmpty() ) && m_titleDisplayed )
110  {
111  // Destroy the menu entry keeping the title..
112  wxMenuItem* item = FindItemByPosition( 0 );
113  wxASSERT( item->GetItemLabelText() == GetTitle() );
114  Destroy( item );
115  // ..and separator
116  item = FindItemByPosition( 0 );
117  wxASSERT( item->IsSeparator() );
118  Destroy( item );
119  m_titleDisplayed = false;
120  }
121 
122  else if( aDisplay && !m_title.IsEmpty() )
123  {
124  if( m_titleDisplayed )
125  {
126  // Simply update the title
127  FindItemByPosition( 0 )->SetItemLabel( m_title );
128  }
129  else
130  {
131  // Add a separator and a menu entry to display the title
132  InsertSeparator( 0 );
133  Insert( 0, new wxMenuItem( this, wxID_NONE, m_title, wxEmptyString, wxITEM_NORMAL ) );
134 
135  if( m_icon )
136  set_wxMenuIcon( FindItemByPosition( 0 ), m_icon );
137 
138  m_titleDisplayed = true;
139  }
140  }
141 }
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: action_menu.h:211
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:232
static void set_wxMenuIcon(wxMenuItem *aMenu, const BITMAP_OPAQUE *aIcon)
Definition: action_menu.cpp:68
wxString m_title
Menu title
Definition: action_menu.h:214

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

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

◆ eventHandler()

virtual OPT_TOOL_EVENT ACTION_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 173 of file action_menu.h.

174  {
175  return OPT_TOOL_EVENT();
176  }
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:486

Referenced by onMenuEvent(), and runEventHandlers().

◆ findToolAction()

OPT_TOOL_EVENT ACTION_MENU::findToolAction ( int  aId)
protected

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

Definition at line 434 of file action_menu.cpp.

435 {
436  OPT_TOOL_EVENT evt;
437 
438  auto findFunc = [&]( ACTION_MENU* m ) {
439  if( evt )
440  return;
441 
442  const auto it = m->m_toolActions.find( aId );
443 
444  if( it != m->m_toolActions.end() )
445  evt = it->second->MakeEvent();
446  };
447 
448  findFunc( this );
449 
450  if( !evt )
451  runOnSubmenus( findFunc );
452 
453  return evt;
454 }
Class ACTION_MENU.
Definition: action_menu.h:43
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:486

References runOnSubmenus().

Referenced by onMenuEvent().

◆ getMenuId()

static int ACTION_MENU::getMenuId ( const TOOL_ACTION aAction)
inlinestaticprotected

Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.

Definition at line 156 of file action_menu.h.

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

References ACTION_ID, and TOOL_ACTION::GetId().

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

◆ GetSelected()

int ACTION_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 action menu.

Definition at line 121 of file action_menu.h.

122  {
123  return m_selected;
124  }
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:217

References m_selected.

Referenced by LIB_EDIT_FRAME::OnViewEntryDoc().

◆ getToolManager()

TOOL_MANAGER * ACTION_MENU::getToolManager ( ) const
protected

Returns an instance of TOOL_MANAGER class.

Definition at line 276 of file action_menu.cpp.

277 {
278  wxASSERT( m_tool );
279  return m_tool ? m_tool->GetManager() : nullptr;
280 }
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:144
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:220

References TOOL_BASE::GetManager(), and m_tool.

Referenced by onMenuEvent(), BUS_UNFOLD_MENU::update(), PAD_CONTEXT_MENU::update(), SYMBOL_UNIT_MENU::update(), and updateHotKeys().

◆ HasEnabledItems()

bool ACTION_MENU::HasEnabledItems ( ) const

Function HasEnabledItems();.

Returns true if the menu has any enabled items

Definition at line 212 of file action_menu.cpp.

213 {
214  bool hasEnabled = false;
215 
216  auto& items = GetMenuItems();
217 
218  for( auto item : items )
219  {
220  if( item->IsEnabled() && !item->IsSeparator() )
221  {
222  hasEnabled = true;
223  break;
224  }
225  }
226 
227  return hasEnabled;
228 }

◆ onMenuEvent()

void ACTION_MENU::onMenuEvent ( wxMenuEvent &  aEvent)
protected

The default menu event handler.

Definition at line 317 of file action_menu.cpp.

318 {
319  OPT_TOOL_EVENT evt;
320  wxString menuText;
321 
322  wxEventType type = aEvent.GetEventType();
323 
324  if( type == wxEVT_MENU_OPEN && m_Dirty )
325  {
326  getToolManager()->RunAction( ACTIONS::updateMenu, true, this );
327  aEvent.Skip();
328  return;
329  }
330 
331  // When the currently chosen item in the menu is changed, an update event is issued.
332  // For example, the selection tool can use this to dynamically highlight the current item
333  // from selection clarification popup.
334  else if( type == wxEVT_MENU_HIGHLIGHT )
335  evt = TOOL_EVENT( TC_COMMAND, TA_CONTEXT_MENU_UPDATE, aEvent.GetId() );
336 
337  // One of menu entries was selected..
338  else if( type == wxEVT_COMMAND_MENU_SELECTED )
339  {
340  // Store the selected position, so it can be checked by the tools
341  m_selected = aEvent.GetId();
342 
343  ACTION_MENU* parent = dynamic_cast<ACTION_MENU*>( GetParent() );
344 
345  while( parent )
346  {
347  parent->m_selected = m_selected;
348  parent = dynamic_cast<ACTION_MENU*>( parent->GetParent() );
349  }
350 
351  // Check if there is a TOOL_ACTION for the given ID
352  if( m_selected >= ACTION_ID )
353  evt = findToolAction( m_selected );
354 
355  if( !evt )
356  {
357 #ifdef __WINDOWS__
358  if( !evt )
359  {
360  // Try to find the submenu which holds the selected item
361  wxMenu* menu = nullptr;
362  FindItem( m_selected, &menu );
363 
364  // This conditional compilation is probably not needed.
365  // It will be removed later, for the Kicad V 6.x version.
366  // But in "old" 3.0 version, the "&& menu != this" contition was added to avoid hang
367  // This hang is no longer encountered in wxWidgets 3.0.4 version, and this condition is no longer needed.
368  // And in 3.1.2, we have to remove it, as "menu != this" never happens
369  // ("menu != this" always happens in 3.1.1 and older!).
370  #if wxCHECK_VERSION(3, 1, 2)
371  if( menu )
372  #else
373  if( menu && menu != this )
374  #endif
375  {
376  ACTION_MENU* cxmenu = static_cast<ACTION_MENU*>( menu );
377  evt = cxmenu->eventHandler( aEvent );
378  }
379  }
380 #else
381  if( !evt )
382  runEventHandlers( aEvent, evt );
383 #endif
384 
385  // Handling non-action menu entries (e.g. items in clarification list)
386  if( !evt && m_selected < wxID_LOWEST )
387  {
388  menuText = GetLabelText( aEvent.GetId() );
390  &menuText );
391  }
392  }
393  }
394 
395  // forward the action/update event to the TOOL_MANAGER
396  // clients that don't supply a tool will have to check GetSelected() themselves
397  if( evt && m_tool )
398  {
399  //aEvent.StopPropagation();
400  m_tool->GetManager()->ProcessEvent( *evt );
401  }
402  else
403  {
404  aEvent.Skip();
405  }
406 }
OPT_TOOL_EVENT findToolAction(int aId)
Checks if any of submenus contains a TOOL_ACTION with a specific ID.
Class ACTION_MENU.
Definition: action_menu.h:43
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
void runEventHandlers(const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
Traverses the submenus tree looking for a submenu capable of handling a particular menu event.
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:144
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
Definition: action_menu.h:173
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:167
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:217
static const int ACTION_ID
Menu items with ID higher than that are considered TOOL_ACTIONs
Definition: action_menu.h:223
static TOOL_ACTION updateMenu
Definition: actions.h:49
All active tools
Definition: tool_event.h:143
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:220
bool m_Dirty
Menu requires updating before display.
Definition: action_menu.h:146
TOOL_MANAGER * getToolManager() const
Returns an instance of TOOL_MANAGER class.
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:486

References ACTION_ID, AS_GLOBAL, eventHandler(), findToolAction(), TOOL_BASE::GetManager(), getToolManager(), m_Dirty, m_selected, m_tool, TOOL_MANAGER::ProcessEvent(), TOOL_MANAGER::RunAction(), runEventHandlers(), TA_CONTEXT_MENU_CHOICE, TA_CONTEXT_MENU_UPDATE, TC_COMMAND, and ACTIONS::updateMenu.

Referenced by setupEvents().

◆ operator=()

ACTION_MENU& ACTION_MENU::operator= ( const ACTION_MENU aMenu)
delete

◆ runEventHandlers()

void ACTION_MENU::runEventHandlers ( const wxMenuEvent &  aMenuEvent,
OPT_TOOL_EVENT aToolEvent 
)
protected

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 409 of file action_menu.cpp.

410 {
411  aToolEvent = eventHandler( aMenuEvent );
412 
413  if( !aToolEvent )
414  runOnSubmenus( std::bind( &ACTION_MENU::runEventHandlers, _1, aMenuEvent, aToolEvent ) );
415 }
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: action_menu.h:173
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.

References eventHandler(), and runOnSubmenus().

Referenced by onMenuEvent().

◆ runOnSubmenus()

void ACTION_MENU::runOnSubmenus ( std::function< void(ACTION_MENU *)>  aFunction)
protected

Runs a function on the menu and all its submenus.

Definition at line 418 of file action_menu.cpp.

419 {
420  try
421  {
422  std::for_each( m_submenus.begin(), m_submenus.end(), [&]( ACTION_MENU* m ) {
423  aFunction( m );
424  m->runOnSubmenus( aFunction );
425  } );
426  }
427  catch( std::exception& e )
428  {
429  wxLogDebug( wxString::Format( "ACTION_MENU runOnSubmenus exception: %s", e.what() ) );
430  }
431 }
Class ACTION_MENU.
Definition: action_menu.h:43
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:229
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

References Format(), and m_submenus.

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

◆ SetIcon()

◆ SetTitle()

void ACTION_MENU::SetTitle ( const wxString &  aTitle)
override

Function SetTitle() Sets title for the menu.

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

Parameters
aTitleis the new title.

Definition at line 96 of file action_menu.cpp.

97 {
98  // Unfortunately wxMenu::SetTitle() does not work very well, so this is an alternative version
99  m_title = aTitle;
100 
101  // Update the menu title
102  if( m_titleDisplayed )
103  DisplayTitle( true );
104 }
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: action_menu.h:211
wxString m_title
Menu title
Definition: action_menu.h:214
void DisplayTitle(bool aDisplay=true)
Function DisplayTitle() Decides whether a title for a pop up menu should be displayed.

References DisplayTitle(), m_title, and m_titleDisplayed.

Referenced by BUS_UNFOLD_MENU::BUS_UNFOLD_MENU(), DIFF_PAIR_MENU::DIFF_PAIR_MENU(), GERBVIEW_SELECTION_TOOL::disambiguationMenu(), EE_SELECTION_TOOL::doSelectionMenu(), SELECTION_TOOL::doSelectionMenu(), GRID_MENU::GRID_MENU(), ALIGN_DISTRIBUTE_TOOL::Init(), PAD_CONTEXT_MENU::PAD_CONTEXT_MENU(), FOOTPRINT_EDIT_FRAME::ReCreateMenuBar(), LIB_EDIT_FRAME::ReCreateMenuBar(), SCH_EDIT_FRAME::ReCreateMenuBar(), PCB_EDIT_FRAME::ReCreateMenuBar(), ROUTER_TOOL_MENU::ROUTER_TOOL_MENU(), SYMBOL_UNIT_MENU::SYMBOL_UNIT_MENU(), TRACK_WIDTH_MENU::TRACK_WIDTH_MENU(), TUNER_TOOL_MENU::TUNER_TOOL_MENU(), and ZOOM_MENU::ZOOM_MENU().

◆ SetTool()

void ACTION_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 249 of file action_menu.cpp.

250 {
251  m_tool = aTool;
252  runOnSubmenus( std::bind( &ACTION_MENU::SetTool, _1, aTool ) );
253 }
void SetTool(TOOL_INTERACTIVE *aTool)
Function SetTool() Sets a tool that is the creator of the menu.
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:220
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.

References m_tool, and runOnSubmenus().

Referenced by FOOTPRINT_EDIT_FRAME::ReCreateMenuBar(), PCB_EDIT_FRAME::ReCreateMenuBar(), TOOL_INTERACTIVE::SetContextMenu(), and SCH_WIRE_BUS_TOOL::UnfoldBus().

◆ setupEvents()

void ACTION_MENU::setupEvents ( )
protected

Initializes handlers for events.

Definition at line 88 of file action_menu.cpp.

89 {
90  Connect( wxEVT_MENU_OPEN, wxMenuEventHandler( ACTION_MENU::onMenuEvent ), NULL, this );
91  Connect( wxEVT_MENU_HIGHLIGHT, wxMenuEventHandler( ACTION_MENU::onMenuEvent ), NULL, this );
92  Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( ACTION_MENU::onMenuEvent ), NULL, this );
93 }
void onMenuEvent(wxMenuEvent &aEvent)
The default menu event handler.

References onMenuEvent().

Referenced by ACTION_MENU().

◆ update()

virtual void ACTION_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, SYMBOL_UNIT_MENU, ZONE_CONTEXT_MENU, SELECT_MENU, PAD_CONTEXT_MENU, BUS_UNFOLD_MENU, HIGHLIGHT_MENU, GRID_MENU, and ZOOM_MENU.

Definition at line 165 of file action_menu.h.

166  {
167  }

Referenced by UpdateAll().

◆ UpdateAll()

void ACTION_MENU::UpdateAll ( )

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

Definition at line 231 of file action_menu.cpp.

232 {
233  try
234  {
235  update();
236  }
237  catch( std::exception& e )
238  {
239  wxLogDebug( wxString::Format( "ACTION_MENU update handler exception: %s", e.what() ) );
240  }
241 
242  if( m_tool )
243  updateHotKeys();
244 
245  runOnSubmenus( std::bind( &ACTION_MENU::UpdateAll, _1 ) );
246 }
virtual void update()
Update menu state stub.
Definition: action_menu.h:165
void UpdateAll()
Function UpdateAll() Runs update handlers for the menu and its submenus.
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:220
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all 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 updateHotKeys()
Updates hot key settings for TOOL_ACTIONs in this menu.

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

Referenced by PAD_TOOL::Init(), TOOL_MENU::ShowContextMenu(), EE_SELECTION_TOOL::UpdateMenu(), and SELECTION_TOOL::UpdateMenu().

◆ updateHotKeys()

void ACTION_MENU::updateHotKeys ( )
protected

Updates hot key settings for TOOL_ACTIONs in this menu.

Definition at line 283 of file action_menu.cpp.

284 {
285  TOOL_MANAGER* toolMgr = getToolManager();
286 
287  for( std::map<int, const TOOL_ACTION*>::const_iterator it = m_toolActions.begin();
288  it != m_toolActions.end(); ++it )
289  {
290  int id = it->first;
291  const TOOL_ACTION& action = *it->second;
292  int key = toolMgr->GetHotKey( action ) & ~MD_MODIFIER_MASK;
293 
294  if( key )
295  {
296  int mod = toolMgr->GetHotKey( action ) & MD_MODIFIER_MASK;
297  int flags = 0;
298  wxMenuItem* item = FindChildItem( id );
299 
300  if( item )
301  {
302  flags |= ( mod & MD_ALT ) ? wxACCEL_ALT : 0;
303  flags |= ( mod & MD_CTRL ) ? wxACCEL_CTRL : 0;
304  flags |= ( mod & MD_SHIFT ) ? wxACCEL_SHIFT : 0;
305 
306  if( !flags )
307  flags = wxACCEL_NORMAL;
308 
309  wxAcceleratorEntry accel( flags, key, id, item );
310  item->SetAccel( &accel );
311  }
312  }
313  }
314 }
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: action_menu.h:226
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

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

Referenced by UpdateAll().

Friends And Related Function Documentation

◆ TOOL_INTERACTIVE

friend class TOOL_INTERACTIVE
friend

Definition at line 234 of file action_menu.h.

Member Data Documentation

◆ ACTION_ID

const int ACTION_MENU::ACTION_ID = 20000
staticprotected

Menu items with ID higher than that are considered TOOL_ACTIONs

Definition at line 223 of file action_menu.h.

Referenced by getMenuId(), and onMenuEvent().

◆ m_Dirty

bool ACTION_MENU::m_Dirty

Menu requires updating before display.

Definition at line 146 of file action_menu.h.

Referenced by onMenuEvent(), and TOOL_MENU::ShowContextMenu().

◆ m_icon

const BITMAP_OPAQUE* ACTION_MENU::m_icon
protected

Optional icon

Definition at line 232 of file action_menu.h.

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

◆ m_selected

int ACTION_MENU::m_selected
protected

Stores the id number of selected item.

Definition at line 217 of file action_menu.h.

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

◆ m_submenus

std::list<ACTION_MENU*> ACTION_MENU::m_submenus
protected

List of submenus.

Definition at line 229 of file action_menu.h.

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

◆ m_title

wxString ACTION_MENU::m_title
protected

Menu title

Definition at line 214 of file action_menu.h.

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

◆ m_titleDisplayed

bool ACTION_MENU::m_titleDisplayed
protected

Flag indicating that the menu title was set up.

Definition at line 211 of file action_menu.h.

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

◆ m_tool

TOOL_INTERACTIVE* ACTION_MENU::m_tool
protected

◆ m_toolActions

std::map<int, const TOOL_ACTION*> ACTION_MENU::m_toolActions
protected

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

Definition at line 226 of file action_menu.h.

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


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