KiCad PCB EDA Suite
CONDITIONAL_MENU Class Reference

#include <conditional_menu.h>

Inheritance diagram for CONDITIONAL_MENU:
ACTION_MENU SPECIAL_TOOLS_CONTEXT_MENU

Classes

class  ENTRY
 

Helper class to organize menu entries.

More...
 

Public Member Functions

 CONDITIONAL_MENU (TOOL_INTERACTIVE *aTool)
 
ACTION_MENUcreate () const override
 

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

More...
 
void AddItem (const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
 Adds a menu entry to run a TOOL_ACTION on selected items. More...
 
void AddItem (int aId, const wxString &aText, const wxString &aTooltip, BITMAP_DEF aIcon, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
 
void AddCheckItem (const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
 Adds a checked menu entry to run a TOOL_ACTION on selected items. More...
 
void AddCheckItem (int aId, const wxString &aText, const wxString &aTooltip, BITMAP_DEF aIcon, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
 
void AddMenu (ACTION_MENU *aMenu, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
 Adds a submenu to the menu. More...
 
void AddSeparator (int aOrder=ANY_ORDER)
 Adds a separator to the menu. More...
 
void Evaluate (SELECTION &aSelection)
 Updates the contents of the menu based on the supplied conditions. More...
 
void Resolve ()
 Updates the initial contents so that wxWidgets doesn't get its knickers tied in a knot over the menu being empty (mainly an issue on GTK, but also on OSX with the preferences and quit menu items). More...
 
void SetTitle (const wxString &aTitle) override
 Sets title for the menu. More...
 
void DisplayTitle (bool aDisplay=true)
 Decides whether a title for a pop up menu should be displayed. More...
 
void SetIcon (const BITMAP_OPAQUE *aIcon)
 Assigns an icon for the entry. More...
 
wxMenuItem * Add (const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon)
 Adds a wxWidgets-style entry to the menu. More...
 
wxMenuItem * Add (const wxString &aLabel, const wxString &aToolTip, int aId, const BITMAP_OPAQUE *aIcon, bool aIsCheckmarkEntry=false)
 
wxMenuItem * Add (const TOOL_ACTION &aAction, bool aIsCheckmarkEntry=false)
 Adds an entry to the menu, basing on the TOOL_ACTION object. More...
 
wxMenuItem * Add (ACTION_MENU *aMenu)
 Adds an action menu as a submenu. More...
 
void AddClose (wxString aAppname="")
 Add a standard close item to the menu with the accelerator key CTRL-W. More...
 
void AddQuitOrClose (KIFACE_I *aKiface, wxString aAppname="")
 Adds either a standard Quit or Close item to the menu. More...
 
void Clear ()
 Removes all the entries from the menu (as well as its title). More...
 
bool HasEnabledItems () const
 Returns true if the menu has any enabled items. More...
 
int GetSelected () const
 Returns the position of selected item. More...
 
void UpdateAll ()
 Runs update handlers for the menu and its submenus. More...
 
void ClearDirty ()
 Clears the dirty flag on the menu and all descendants. More...
 
void SetDirty ()
 
void SetTool (TOOL_INTERACTIVE *aTool)
 Sets a tool that is the creator of the menu. More...
 
ACTION_MENUClone () const
 Creates a deep, recursive copy of this ACTION_MENU. More...
 
void OnMenuEvent (wxMenuEvent &aEvent)
 
void OnIdle (wxIdleEvent &event)
 

Static Public Attributes

static const int ANY_ORDER = -1
 

Constant to indicate that we do not care about an ENTRY location in the menu.

More...
 
static constexpr bool CHECK = true
 

Protected Member Functions

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 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...
 

Protected Attributes

bool m_isForcedPosition
 
wxPoint m_forcedPosition
 
bool m_dirty
 
bool m_titleDisplayed
 
bool m_isContextMenu
 
wxString m_title
 

Menu title

More...
 
const BITMAP_OPAQUEm_icon
 

Optional icon

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...
 

Private Member Functions

void addEntry (ENTRY aEntry)
 

Inserts the entry, preserving the requested order.

More...
 

Private Attributes

std::list< ENTRYm_entries
 

List of all menu entries.

More...
 

Detailed Description

Definition at line 38 of file conditional_menu.h.

Constructor & Destructor Documentation

◆ CONDITIONAL_MENU()

CONDITIONAL_MENU::CONDITIONAL_MENU ( TOOL_INTERACTIVE aTool)

Definition at line 32 of file conditional_menu.cpp.

32  :
33  ACTION_MENU( true, aTool )
34 {
35 }
ACTION_MENU(bool isContextMenu, TOOL_INTERACTIVE *aTool=nullptr)
Default constructor
Definition: action_menu.cpp:46

Referenced by create().

Member Function Documentation

◆ Add() [1/4]

wxMenuItem * ACTION_MENU::Add ( const wxString &  aLabel,
int  aId,
const BITMAP_OPAQUE aIcon 
)
inherited

Adds a wxWidgets-style entry to the menu.

After highlighting/selecting the entry, a wxWidgets event is generated.

Definition at line 137 of file action_menu.cpp.

138 {
139  wxASSERT_MSG( FindItem( aId ) == nullptr, "Duplicate menu IDs!" );
140 
141  wxMenuItem* item = new wxMenuItem( this, aId, aLabel, wxEmptyString, wxITEM_NORMAL );
142 
143  if( aIcon )
144  AddBitmapToMenuItem( item, KiBitmap( aIcon ) );
145 
146  return Append( item );
147 }
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:205

References AddBitmapToMenuItem(), and KiBitmap().

Referenced by ACTION_MENU::AddClose(), AddMenuLanguageList(), ACTION_MENU::AddQuitOrClose(), EDA_3D_VIEWER::CreateMenuBar(), GERBVIEW_SELECTION_TOOL::disambiguationMenu(), PL_SELECTION_TOOL::doSelectionMenu(), Evaluate(), ALIGN_DISTRIBUTE_TOOL::Init(), NET_CONTEXT_MENU::NET_CONTEXT_MENU(), LIB_VIEW_FRAME::ReCreateMenuBar(), KICAD_MANAGER_FRAME::ReCreateMenuBar(), FOOTPRINT_EDIT_FRAME::ReCreateMenuBar(), FOOTPRINT_VIEWER_FRAME::ReCreateMenuBar(), LIB_EDIT_FRAME::ReCreateMenuBar(), PL_EDITOR_FRAME::ReCreateMenuBar(), CVPCB_MAINFRAME::ReCreateMenuBar(), SCH_EDIT_FRAME::ReCreateMenuBar(), GERBVIEW_FRAME::ReCreateMenuBar(), PCB_EDIT_FRAME::ReCreateMenuBar(), PCB_EDIT_FRAME::ReCreateVToolbar(), and CVPCB_MAINFRAME::setupTools().

◆ Add() [2/4]

wxMenuItem * ACTION_MENU::Add ( const wxString &  aLabel,
const wxString &  aToolTip,
int  aId,
const BITMAP_OPAQUE aIcon,
bool  aIsCheckmarkEntry = false 
)
inherited

Definition at line 150 of file action_menu.cpp.

152 {
153  wxASSERT_MSG( FindItem( aId ) == nullptr, "Duplicate menu IDs!" );
154 
155  wxMenuItem* item = new wxMenuItem( this, aId, aLabel, aTooltip,
156  aIsCheckmarkEntry ? wxITEM_CHECK : wxITEM_NORMAL );
157 
158  if( aIcon )
159  AddBitmapToMenuItem( item, KiBitmap( aIcon ) );
160 
161  return Append( item );
162 }
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:205

References AddBitmapToMenuItem(), and KiBitmap().

◆ Add() [3/4]

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

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 are assigned above ACTION_BASE_UI_ID inside TOOL_EVENT

Definition at line 165 of file action_menu.cpp.

166 {
168  const BITMAP_OPAQUE* icon = aAction.GetIcon();
169 
170  wxMenuItem* item = new wxMenuItem( this, aAction.GetUIId(), aAction.GetMenuItem(),
171  aAction.GetDescription(),
172  aIsCheckmarkEntry ? wxITEM_CHECK : wxITEM_NORMAL );
173  if( icon )
174  AddBitmapToMenuItem( item, KiBitmap( icon ) );
175 
176  m_toolActions[aAction.GetUIId()] = &aAction;
177 
178  return Append( item );
179 }
PNG memory record (file in memory).
Definition: bitmap_def.h:29
int GetUIId() const
Definition: tool_action.h:102
const BITMAP_OPAQUE * GetIcon() const
Returns an icon associated with the action.
Definition: tool_action.h:155
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:205
wxString GetDescription(bool aIncludeHotkey=true) const
Definition: tool_action.cpp:83
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: action_menu.h:238
wxString GetMenuItem() const
Definition: tool_action.cpp:75

References AddBitmapToMenuItem(), TOOL_ACTION::GetDescription(), TOOL_ACTION::GetIcon(), TOOL_ACTION::GetMenuItem(), TOOL_ACTION::GetUIId(), KiBitmap(), and ACTION_MENU::m_toolActions.

◆ Add() [4/4]

wxMenuItem * ACTION_MENU::Add ( ACTION_MENU aMenu)
inherited

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

183 {
184  ACTION_MENU* menuCopy = aMenu->Clone();
185  m_submenus.push_back( menuCopy );
186 
187  wxASSERT_MSG( !menuCopy->m_title.IsEmpty(), "Set a title for ACTION_MENU using SetTitle()" );
188 
189  if( aMenu->m_icon )
190  {
191  wxMenuItem* newItem = new wxMenuItem( this, -1, menuCopy->m_title );
192  AddBitmapToMenuItem( newItem, KiBitmap( aMenu->m_icon ) );
193  newItem->SetSubMenu( menuCopy );
194  return Append( newItem );
195  }
196  else
197  {
198  return AppendSubMenu( menuCopy, menuCopy->m_title );
199  }
200 }
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:43
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:229
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:205
wxString m_title
Menu title
Definition: action_menu.h:226
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:241
ACTION_MENU * Clone() const
Creates a deep, recursive copy of this ACTION_MENU.

References AddBitmapToMenuItem(), ACTION_MENU::Clone(), KiBitmap(), ACTION_MENU::m_icon, ACTION_MENU::m_submenus, and ACTION_MENU::m_title.

◆ AddCheckItem() [1/2]

void CONDITIONAL_MENU::AddCheckItem ( const TOOL_ACTION aAction,
const SELECTION_CONDITION aCondition,
int  aOrder = ANY_ORDER 
)

Adds a checked menu entry to run a TOOL_ACTION on selected items.

The condition for checking the menu entry should be supplied through a ACTION_CONDITION registered with the ACTION_MANAGER.

Parameters
aActionis a menu entry to be added.
aConditionis a condition that has to be fulfilled to show the menu entry in the menu.
aOrderdetermines location of the added item, higher numbers are put on the bottom. You may use ANY_ORDER here if you think it does not matter.

Definition at line 54 of file conditional_menu.cpp.

56 {
57  wxASSERT( aAction.GetId() > 0 ); // Check if action was previously registered in ACTION_MANAGER
58  addEntry( ENTRY( &aAction, aCondition, aOrder, true ) );
59 }
int GetId() const
Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:94
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.

References addEntry(), and TOOL_ACTION::GetId().

◆ AddCheckItem() [2/2]

void CONDITIONAL_MENU::AddCheckItem ( int  aId,
const wxString &  aText,
const wxString &  aTooltip,
BITMAP_DEF  aIcon,
const SELECTION_CONDITION aCondition,
int  aOrder = ANY_ORDER 
)

Definition at line 75 of file conditional_menu.cpp.

78 {
79  wxMenuItem item( nullptr, aId, aText, aTooltip, wxITEM_CHECK );
80 
81  if( aIcon )
82  AddBitmapToMenuItem( &item, KiBitmap( aIcon ) );
83 
84  addEntry( ENTRY( item, aIcon, aCondition, aOrder, true ) );
85 }
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:205
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.

References AddBitmapToMenuItem(), addEntry(), and KiBitmap().

◆ AddClose()

void ACTION_MENU::AddClose ( wxString  aAppname = "")
inherited

Add a standard close item to the menu with the accelerator key CTRL-W.

Emits the wxID_CLOSE event.

Parameters
aAppnameis the application name to append to the tooltip

Definition at line 203 of file action_menu.cpp.

204 {
205  Add( _( "Close\tCTRL+W" ),
206  wxString::Format( "Close %s", aAppname ),
207  wxID_CLOSE,
208  exit_xpm );
209 }
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon)
Adds a wxWidgets-style entry to the menu.
const BITMAP_OPAQUE exit_xpm[1]
Definition: exit.cpp:63
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:201
#define _(s)
Definition: 3d_actions.cpp:33

References _, ACTION_MENU::Add(), exit_xpm, and Format().

Referenced by ACTION_MENU::AddQuitOrClose(), EDA_3D_VIEWER::CreateMenuBar(), LIB_VIEW_FRAME::ReCreateMenuBar(), FOOTPRINT_EDIT_FRAME::ReCreateMenuBar(), FOOTPRINT_VIEWER_FRAME::ReCreateMenuBar(), LIB_EDIT_FRAME::ReCreateMenuBar(), and CVPCB_MAINFRAME::ReCreateMenuBar().

◆ addEntry()

void CONDITIONAL_MENU::addEntry ( ENTRY  aEntry)
private

Inserts the entry, preserving the requested order.

Definition at line 198 of file conditional_menu.cpp.

199 {
200  if( aEntry.Order() < 0 ) // Any order, so give it any order number
201  aEntry.SetOrder( m_entries.size() );
202 
203  std::list<ENTRY>::iterator it = m_entries.begin();
204 
205  // Find the right spot for the entry
206  while( it != m_entries.end() && it->Order() <= aEntry.Order() )
207  ++it;
208 
209  m_entries.insert( it, aEntry );
210 }
std::list< ENTRY > m_entries
List of all menu entries.

References m_entries, CONDITIONAL_MENU::ENTRY::Order(), and CONDITIONAL_MENU::ENTRY::SetOrder().

Referenced by AddCheckItem(), AddItem(), AddMenu(), and AddSeparator().

◆ AddItem() [1/2]

void CONDITIONAL_MENU::AddItem ( const TOOL_ACTION aAction,
const SELECTION_CONDITION aCondition,
int  aOrder = ANY_ORDER 
)

Adds a menu entry to run a TOOL_ACTION on selected items.

Parameters
aActionis a menu entry to be added.
aConditionis a condition that has to be fulfilled to show the menu entry in the menu.
aOrderdetermines location of the added item, higher numbers are put on the bottom. You may use ANY_ORDER here if you think it does not matter.

Definition at line 46 of file conditional_menu.cpp.

48 {
49  wxASSERT( aAction.GetId() > 0 ); // Check if action was previously registered in ACTION_MANAGER
50  addEntry( ENTRY( &aAction, aCondition, aOrder, false ) );
51 }
int GetId() const
Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:94
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.

References addEntry(), and TOOL_ACTION::GetId().

Referenced by LIB_EDIT_TOOL::Init(), PICKER_TOOL::Init(), LIB_MOVE_TOOL::Init(), LIB_PIN_TOOL::Init(), PL_EDIT_TOOL::Init(), SCH_EDIT_TOOL::Init(), EE_INSPECTION_TOOL::Init(), CONVERT_TOOL::Init(), PAD_TOOL::Init(), LIB_CONTROL::Init(), LIB_DRAWING_TOOLS::Init(), PCB_INSPECTION_TOOL::Init(), FOOTPRINT_EDITOR_TOOLS::Init(), PCB_VIEWER_TOOLS::Init(), EE_POINT_EDITOR::Init(), SCH_MOVE_TOOL::Init(), PCB_EDITOR_CONTROL::Init(), POINT_EDITOR::Init(), EDA_3D_CONTROLLER::Init(), SCH_DRAWING_TOOLS::Init(), DRAWING_TOOL::Init(), EE_SELECTION_TOOL::Init(), EE_TOOL_BASE< SCH_BASE_FRAME >::Init(), SCH_LINE_WIRE_BUS_TOOL::Init(), EDIT_TOOL::Init(), PCB_TOOL_BASE::Init(), and SPECIAL_TOOLS_CONTEXT_MENU::SPECIAL_TOOLS_CONTEXT_MENU().

◆ AddItem() [2/2]

void CONDITIONAL_MENU::AddItem ( int  aId,
const wxString &  aText,
const wxString &  aTooltip,
BITMAP_DEF  aIcon,
const SELECTION_CONDITION aCondition,
int  aOrder = ANY_ORDER 
)

Definition at line 62 of file conditional_menu.cpp.

65 {
66  wxMenuItem item( nullptr, aId, aText, aTooltip, wxITEM_NORMAL );
67 
68  if( aIcon )
69  AddBitmapToMenuItem( &item, KiBitmap( aIcon ) );
70 
71  addEntry( ENTRY( item, aIcon, aCondition, aOrder, false ) );
72 }
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:205
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.

References AddBitmapToMenuItem(), addEntry(), and KiBitmap().

◆ AddMenu()

void CONDITIONAL_MENU::AddMenu ( ACTION_MENU aMenu,
const SELECTION_CONDITION aCondition = SELECTION_CONDITIONS::ShowAlways,
int  aOrder = ANY_ORDER 
)

Adds a submenu to the menu.

CONDITIONAL_MENU takes ownership of the added menu, so it will be freed when the CONDITIONAL_MENU object is destroyed.

Parameters
aMenuis the submenu to be added.
aExpanddetermines if the added submenu items should be added as individual items or as a submenu.
aConditionis a condition that has to be fulfilled to show the submenu entry in the menu.
aOrderdetermines location of the added menu, higher numbers are put on the bottom. You may use ANY_ORDER here if you think it does not matter.

Definition at line 88 of file conditional_menu.cpp.

90 {
91  addEntry( ENTRY( aMenu, aCondition, aOrder ) );
92 }
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.

References addEntry().

Referenced by SCH_EDIT_TOOL::Init(), PCB_INSPECTION_TOOL::Init(), ALIGN_DISTRIBUTE_TOOL::Init(), GERBVIEW_SELECTION_TOOL::Init(), SCH_LINE_WIRE_BUS_TOOL::Init(), and EDIT_TOOL::Init().

◆ AddQuitOrClose()

void ACTION_MENU::AddQuitOrClose ( KIFACE_I aKiface,
wxString  aAppname = "" 
)
inherited

Adds either a standard Quit or Close item to the menu.

If aKiface is NULL or in single-instance then Quite (wxID_QUIT) is used, otherwise Close (wxID_CLOSE) is used.

Parameters
aAppnameis the application name to append to the tooltip

Definition at line 212 of file action_menu.cpp.

213 {
214  if( !aKiface || aKiface->IsSingle() ) // not when under a project mgr
215  {
216  // Don't use ACTIONS::quit; wxWidgets moves this on OSX and expects to find it via
217  // wxID_EXIT
218  Add( _( "Quit" ),
219  wxString::Format( "Quit %s", aAppname ),
220  wxID_EXIT,
221  exit_xpm );
222  }
223  else
224  {
225  AddClose( aAppname );
226  }
227 }
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon)
Adds a wxWidgets-style entry to the menu.
bool IsSingle() const
Function IsSingle is this KIFACE_I running under single_top?
Definition: kiface_i.h:117
const BITMAP_OPAQUE exit_xpm[1]
Definition: exit.cpp:63
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:201
#define _(s)
Definition: 3d_actions.cpp:33
void AddClose(wxString aAppname="")
Add a standard close item to the menu with the accelerator key CTRL-W.

References _, ACTION_MENU::Add(), ACTION_MENU::AddClose(), exit_xpm, Format(), and KIFACE_I::IsSingle().

Referenced by KICAD_MANAGER_FRAME::ReCreateMenuBar(), PL_EDITOR_FRAME::ReCreateMenuBar(), SCH_EDIT_FRAME::ReCreateMenuBar(), GERBVIEW_FRAME::ReCreateMenuBar(), and PCB_EDIT_FRAME::ReCreateMenuBar().

◆ AddSeparator()

void CONDITIONAL_MENU::AddSeparator ( int  aOrder = ANY_ORDER)

Adds a separator to the menu.

Parameters
aOrderdetermines location of the separator, higher numbers are put on the bottom.

Definition at line 95 of file conditional_menu.cpp.

96 {
97  addEntry( ENTRY( SELECTION_CONDITIONS::ShowAlways, aOrder ) );
98 }
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.

References addEntry(), and SELECTION_CONDITIONS::ShowAlways().

Referenced by EDA_DRAW_FRAME::AddStandardSubMenus(), LIB_EDIT_TOOL::Init(), LIB_PIN_TOOL::Init(), PL_EDIT_TOOL::Init(), SCH_EDIT_TOOL::Init(), LIB_CONTROL::Init(), PAD_TOOL::Init(), FOOTPRINT_EDITOR_TOOLS::Init(), EDA_3D_CONTROLLER::Init(), PL_SELECTION_TOOL::Init(), and EDIT_TOOL::Init().

◆ appendCopy()

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

Function appendCopy Appends a copy of wxMenuItem.

Definition at line 608 of file action_menu.cpp.

609 {
610  wxMenuItem* newItem = new wxMenuItem( this, aSource->GetId(), aSource->GetItemLabel(),
611  aSource->GetHelp(), aSource->GetKind() );
612 
613  // Add the source bitmap if it is not the wxNullBitmap
614  // On Windows, for Checkable Menu items, adding a bitmap adds also
615  // our predefined checked alternate bitmap
616  // On other OS, wxITEM_CHECK and wxITEM_RADIO Menu items do not use custom bitmaps.
617 #if defined(_WIN32)
618  // On Windows, AddBitmapToMenuItem() uses the unchecked bitmap for wxITEM_CHECK and wxITEM_RADIO menuitems
619  // and autoamtically adds a checked bitmap.
620  // For other menuitrms, use the "checked" bitmap.
621  bool use_checked_bm = ( aSource->GetKind() == wxITEM_CHECK || aSource->GetKind() == wxITEM_RADIO ) ? false : true;
622  const wxBitmap& src_bitmap = aSource->GetBitmap( use_checked_bm );
623 #else
624  const wxBitmap& src_bitmap = aSource->GetBitmap();
625 #endif
626 
627  if( src_bitmap.IsOk() && src_bitmap.GetHeight() > 1 ) // a null bitmap has a 0 size
628  AddBitmapToMenuItem( newItem, src_bitmap );
629 
630  if( aSource->IsSubMenu() )
631  {
632  ACTION_MENU* menu = dynamic_cast<ACTION_MENU*>( aSource->GetSubMenu() );
633  wxASSERT_MSG( menu, "Submenus are expected to be a ACTION_MENU" );
634 
635  if( menu )
636  {
637  ACTION_MENU* menuCopy = menu->Clone();
638  newItem->SetSubMenu( menuCopy );
639  m_submenus.push_back( menuCopy );
640  }
641  }
642 
643  // wxMenuItem has to be added before enabling/disabling or checking
644  Append( newItem );
645 
646  if( aSource->IsCheckable() )
647  newItem->Check( aSource->IsChecked() );
648 
649  newItem->Enable( aSource->IsEnabled() );
650 
651  return newItem;
652 }
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:43
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:205
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:241
ACTION_MENU * Clone() const
Creates a deep, recursive copy of this ACTION_MENU.

References AddBitmapToMenuItem(), ACTION_MENU::Clone(), and ACTION_MENU::m_submenus.

Referenced by ACTION_MENU::copyFrom().

◆ Clear()

void ACTION_MENU::Clear ( )
inherited

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

It leaves the menu in the initial state.

Definition at line 230 of file action_menu.cpp.

231 {
232  m_titleDisplayed = false;
233 
234  for( int i = GetMenuItemCount() - 1; i >= 0; --i )
235  Destroy( FindItemByPosition( i ) );
236 
237  m_toolActions.clear();
238  m_submenus.clear();
239 
240  wxASSERT( GetMenuItemCount() == 0 );
241 }
bool m_titleDisplayed
Definition: action_menu.h:222
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:241
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: action_menu.h:238

References ACTION_MENU::m_submenus, ACTION_MENU::m_titleDisplayed, and ACTION_MENU::m_toolActions.

Referenced by ACTION_MENU::Clone(), Evaluate(), VIA_SIZE_MENU::update(), SYMBOL_UNIT_MENU::update(), BUS_UNFOLD_MENU::update(), TRACK_WIDTH_MENU::update(), and DIFF_PAIR_MENU::update().

◆ ClearDirty()

void ACTION_MENU::ClearDirty ( )
inherited

Clears the dirty flag on the menu and all descendants.

Definition at line 280 of file action_menu.cpp.

281 {
282  m_dirty = false;
283  runOnSubmenus( std::bind( &ACTION_MENU::ClearDirty, _1 ) );
284 }
void ClearDirty()
Clears the dirty flag on the menu and all descendants.
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.

References ACTION_MENU::m_dirty, and ACTION_MENU::runOnSubmenus().

Referenced by TOOL_MENU::ShowContextMenu().

◆ Clone()

ACTION_MENU * ACTION_MENU::Clone ( ) const
inherited

Creates a deep, recursive copy of this ACTION_MENU.

Definition at line 301 of file action_menu.cpp.

302 {
303  ACTION_MENU* clone = create();
304  clone->Clear();
305  clone->copyFrom( *this );
306  return clone;
307 }
Defines the structure of a menu based on ACTIONs.
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()
Removes all the entries from the menu (as well as its title).

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

Referenced by ACTION_MENU::Add(), ACTION_MENU::appendCopy(), and TOOL_MANAGER::DispatchContextMenu().

◆ copyFrom()

void ACTION_MENU::copyFrom ( const ACTION_MENU aMenu)
protectedinherited

Copies another menus data to this instance.

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

Definition at line 590 of file action_menu.cpp.

591 {
592  m_icon = aMenu.m_icon;
593  m_title = aMenu.m_title;
595  m_selected = -1; // aMenu.m_selected;
596  m_tool = aMenu.m_tool;
598 
599  // Copy all menu entries
600  for( int i = 0; i < (int) aMenu.GetMenuItemCount(); ++i )
601  {
602  wxMenuItem* item = aMenu.FindItemByPosition( i );
603  appendCopy( item );
604  }
605 }
bool m_titleDisplayed
Definition: action_menu.h:222
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:229
wxString m_title
Menu title
Definition: action_menu.h:226
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:232
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:235
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:238

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

Referenced by ACTION_MENU::Clone().

◆ create()

ACTION_MENU * CONDITIONAL_MENU::create ( ) const
overridevirtual

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

Reimplemented from ACTION_MENU.

Definition at line 38 of file conditional_menu.cpp.

39 {
41  clone->m_entries = m_entries;
42  return clone;
43 }
std::list< ENTRY > m_entries
List of all menu entries.
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:235
CONDITIONAL_MENU(TOOL_INTERACTIVE *aTool)

References CONDITIONAL_MENU(), m_entries, and ACTION_MENU::m_tool.

◆ DisplayTitle()

void ACTION_MENU::DisplayTitle ( bool  aDisplay = true)
inherited

Decides whether a title for a pop up menu should be displayed.

Definition at line 100 of file action_menu.cpp.

101 {
102  if( ( !aDisplay || m_title.IsEmpty() ) && m_titleDisplayed )
103  {
104  // Destroy the menu entry keeping the title..
105  wxMenuItem* item = FindItemByPosition( 0 );
106  wxASSERT( item->GetItemLabelText() == GetTitle() );
107  Destroy( item );
108  // ..and separator
109  item = FindItemByPosition( 0 );
110  wxASSERT( item->IsSeparator() );
111  Destroy( item );
112  m_titleDisplayed = false;
113  }
114 
115  else if( aDisplay && !m_title.IsEmpty() )
116  {
117  if( m_titleDisplayed )
118  {
119  // Simply update the title
120  FindItemByPosition( 0 )->SetItemLabel( m_title );
121  }
122  else
123  {
124  // Add a separator and a menu entry to display the title
125  InsertSeparator( 0 );
126  Insert( 0, new wxMenuItem( this, wxID_NONE, m_title, wxEmptyString, wxITEM_NORMAL ) );
127 
128  if( m_icon )
129  AddBitmapToMenuItem( FindItemByPosition( 0 ), KiBitmap( m_icon ) );
130 
131  m_titleDisplayed = true;
132  }
133  }
134 }
bool m_titleDisplayed
Definition: action_menu.h:222
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:229
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:205
wxString m_title
Menu title
Definition: action_menu.h:226

References AddBitmapToMenuItem(), KiBitmap(), ACTION_MENU::m_icon, ACTION_MENU::m_title, and ACTION_MENU::m_titleDisplayed.

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

◆ Evaluate()

void CONDITIONAL_MENU::Evaluate ( SELECTION aSelection)

Updates the contents of the menu based on the supplied conditions.

Definition at line 118 of file conditional_menu.cpp.

119 {
120  Clear();
121 
122  // We try to avoid adding useless separators (when no menuitems between separators)
123  int menu_count = 0; // number of menus since the latest separator
124 
125  for( const ENTRY& entry : m_entries )
126  {
127  const SELECTION_CONDITION& cond = entry.Condition();
128  bool result;
129  wxMenuItem* menuItem = nullptr;
130 
131  try
132  {
133  result = cond( aSelection );
134  }
135  catch( std::exception& )
136  {
137  continue;
138  }
139 
140  if( !result )
141  continue;
142 
143  switch( entry.Type() )
144  {
145  case ENTRY::ACTION:
146  menuItem = Add( *entry.Action(), entry.IsCheckmarkEntry() );
147  menu_count++;
148  break;
149 
150  case ENTRY::MENU:
151  menuItem = Add( entry.Menu() );
152  menu_count++;
153  break;
154 
155  case ENTRY::WXITEM:
156  menuItem = new wxMenuItem( this,
157  entry.wxItem()->GetId(),
158  entry.wxItem()->GetItemLabel(),
159  entry.wxItem()->GetHelp(),
160  entry.wxItem()->GetKind() );
161 
162  if( entry.GetIcon() )
163  AddBitmapToMenuItem( menuItem, KiBitmap( entry.GetIcon() ) );
164 
165  // the wxMenuItem must be append only after the bitmap is set:
166  Append( menuItem );
167 
168  menu_count++;
169  break;
170 
171  case ENTRY::SEPARATOR:
172  if( menu_count )
173  menuItem = AppendSeparator();
174 
175  menu_count = 0;
176  break;
177 
178  default:
179  wxASSERT( false );
180  break;
181  }
182  }
183 
184  // Recursively call Evaluate on all the submenus that are CONDITIONAL_MENUs to ensure
185  // they are updated. This is also required on GTK to make sure the menus have the proper
186  // size when created.
188  [&aSelection]( ACTION_MENU* aMenu )
189  {
190  CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( aMenu );
191 
192  if( conditionalMenu )
193  conditionalMenu->Evaluate( aSelection );
194  } );
195 }
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon)
Adds a wxWidgets-style entry to the menu.
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:43
std::list< ENTRY > m_entries
List of all menu entries.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:205
void Evaluate(SELECTION &aSelection)
Updates the contents of the menu based on the supplied conditions.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
void Clear()
Removes all the entries from the menu (as well as its title).

References CONDITIONAL_MENU::ENTRY::ACTION, ACTION_MENU::Add(), AddBitmapToMenuItem(), ACTION_MENU::Clear(), Evaluate(), KiBitmap(), m_entries, CONDITIONAL_MENU::ENTRY::MENU, ACTION_MENU::runOnSubmenus(), CONDITIONAL_MENU::ENTRY::SEPARATOR, and CONDITIONAL_MENU::ENTRY::WXITEM.

Referenced by Evaluate(), Resolve(), and TOOL_MENU::ShowContextMenu().

◆ eventHandler()

virtual OPT_TOOL_EVENT ACTION_MENU::eventHandler ( const wxMenuEvent &  )
inlineprotectedvirtualinherited

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, VIA_SIZE_MENU, GRID_MENU, and ZOOM_MENU.

Definition at line 183 of file action_menu.h.

184  {
185  return OPT_TOOL_EVENT();
186  }
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:556

Referenced by ACTION_MENU::OnMenuEvent(), and ACTION_MENU::runEventHandlers().

◆ findToolAction()

OPT_TOOL_EVENT ACTION_MENU::findToolAction ( int  aId)
protectedinherited

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

Definition at line 567 of file action_menu.cpp.

568 {
569  OPT_TOOL_EVENT evt;
570 
571  auto findFunc = [&]( ACTION_MENU* m ) {
572  if( evt )
573  return;
574 
575  const auto it = m->m_toolActions.find( aId );
576 
577  if( it != m->m_toolActions.end() )
578  evt = it->second->MakeEvent();
579  };
580 
581  findFunc( this );
582 
583  if( !evt )
584  runOnSubmenus( findFunc );
585 
586  return evt;
587 }
Defines the structure of a menu based on ACTIONs.
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:556

References ACTION_MENU::runOnSubmenus().

Referenced by ACTION_MENU::OnMenuEvent().

◆ GetSelected()

int ACTION_MENU::GetSelected ( ) const
inlineinherited

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 131 of file action_menu.h.

132  {
133  return m_selected;
134  }
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:232

References ACTION_MENU::m_selected.

◆ getToolManager()

TOOL_MANAGER * ACTION_MENU::getToolManager ( ) const
protectedinherited

Returns an instance of TOOL_MANAGER class.

Definition at line 321 of file action_menu.cpp.

322 {
323  wxASSERT( m_tool );
324  return m_tool ? m_tool->GetManager() : nullptr;
325 }
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:143
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:235

References TOOL_BASE::GetManager(), and ACTION_MENU::m_tool.

Referenced by VIA_SIZE_MENU::eventHandler(), ACTION_MENU::OnMenuEvent(), VIA_SIZE_MENU::update(), SYMBOL_UNIT_MENU::update(), BUS_UNFOLD_MENU::update(), and ACTION_MENU::updateHotKeys().

◆ HasEnabledItems()

bool ACTION_MENU::HasEnabledItems ( ) const
inherited

Returns true if the menu has any enabled items.

Definition at line 244 of file action_menu.cpp.

245 {
246  bool hasEnabled = false;
247 
248  auto& items = GetMenuItems();
249 
250  for( auto item : items )
251  {
252  if( item->IsEnabled() && !item->IsSeparator() )
253  {
254  hasEnabled = true;
255  break;
256  }
257  }
258 
259  return hasEnabled;
260 }

◆ OnIdle()

void ACTION_MENU::OnIdle ( wxIdleEvent &  event)
inherited

Definition at line 376 of file action_menu.cpp.

377 {
379  g_menu_open_position.x = 0.0;
380  g_menu_open_position.y = 0.0;
381 }
static int g_last_menu_highlighted_id
static VECTOR2D g_menu_open_position

References g_last_menu_highlighted_id, g_menu_open_position, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by ACTION_MENU::setupEvents().

◆ OnMenuEvent()

void ACTION_MENU::OnMenuEvent ( wxMenuEvent &  aEvent)
inherited

Definition at line 384 of file action_menu.cpp.

385 {
386  OPT_TOOL_EVENT evt;
387  wxString menuText;
388  wxEventType type = aEvent.GetEventType();
389  wxWindow* focus = wxWindow::FindFocus();
390 
391  if( type == wxEVT_MENU_OPEN )
392  {
393  if( m_dirty && m_tool )
394  getToolManager()->RunAction( ACTIONS::updateMenu, true, this );
395 
396  wxMenu* parent = dynamic_cast<wxMenu*>( GetParent() );
397 
398  // Don't update the position if this menu has a parent
399  if( !parent && m_tool )
401 
403  }
404  else if( type == wxEVT_MENU_HIGHLIGHT )
405  {
406  if( aEvent.GetId() > 0 )
407  g_last_menu_highlighted_id = aEvent.GetId();
408 
409  evt = TOOL_EVENT( TC_COMMAND, TA_CHOICE_MENU_UPDATE, aEvent.GetId() );
410  }
411  else if( type == wxEVT_COMMAND_MENU_SELECTED )
412  {
413  // Despite our attempts to catch the theft of text editor CHAR_HOOK and CHAR events
414  // in TOOL_DISPATCHER::DispatchWxEvent, wxWidgets sometimes converts those it knows
415  // about into menu commands without ever generating the appropriate CHAR_HOOK and CHAR
416  // events first.
417  if( dynamic_cast<wxTextEntry*>( focus )
418  || dynamic_cast<wxStyledTextCtrl*>( focus )
419  || dynamic_cast<wxListView*>( focus ) )
420  {
421  // Original key event has been lost, so we have to re-create it from the menu's
422  // wxAcceleratorEntry.
423  wxMenuItem* menuItem = FindItem( aEvent.GetId() );
424  wxAcceleratorEntry* acceleratorKey = menuItem ? menuItem->GetAccel() : nullptr;
425 
426  if( acceleratorKey )
427  {
428  wxKeyEvent keyEvent( wxEVT_CHAR_HOOK );
429  keyEvent.m_keyCode = acceleratorKey->GetKeyCode();
430  keyEvent.m_controlDown = ( acceleratorKey->GetFlags() & wxMOD_CONTROL ) > 0;
431  keyEvent.m_shiftDown = ( acceleratorKey->GetFlags() & wxMOD_SHIFT ) > 0;
432  keyEvent.m_altDown = ( acceleratorKey->GetFlags() & wxMOD_ALT ) > 0;
433 
434  if( auto ctrl = dynamic_cast<wxTextEntry*>( focus ) )
435  TEXTENTRY_TRICKS::OnCharHook( ctrl, keyEvent );
436  else
437  focus->HandleWindowEvent( keyEvent );
438 
439  if( keyEvent.GetSkipped() )
440  {
441  keyEvent.SetEventType( wxEVT_CHAR );
442  focus->HandleWindowEvent( keyEvent );
443  }
444 
445  // If the event was used as KEY event (not skipped) by the focused window,
446  // just finish.
447  // Otherwise this is actually a wxEVT_COMMAND_MENU_SELECTED, or the
448  // focused window is read only
449  if( !keyEvent.GetSkipped() )
450  return;
451  }
452  }
453 
454  // Store the selected position, so it can be checked by the tools
455  m_selected = aEvent.GetId();
456 
457  ACTION_MENU* parent = dynamic_cast<ACTION_MENU*>( GetParent() );
458 
459  while( parent )
460  {
461  parent->m_selected = m_selected;
462  parent = dynamic_cast<ACTION_MENU*>( parent->GetParent() );
463  }
464 
465  // Check if there is a TOOL_ACTION for the given ID
467  evt = findToolAction( m_selected );
468 
469  if( !evt )
470  {
471 #ifdef __WINDOWS__
472  if( !evt )
473  {
474  // Try to find the submenu which holds the selected item
475  wxMenu* menu = nullptr;
476  FindItem( m_selected, &menu );
477 
478  // This conditional compilation is probably not needed.
479  // It will be removed later, for the Kicad V 6.x version.
480  // But in "old" 3.0 version, the "&& menu != this" contition was added to avoid hang
481  // This hang is no longer encountered in wxWidgets 3.0.4 version, and this condition is no longer needed.
482  // And in 3.1.2, we have to remove it, as "menu != this" never happens
483  // ("menu != this" always happens in 3.1.1 and older!).
484  #if wxCHECK_VERSION(3, 1, 2)
485  if( menu )
486  #else
487  if( menu && menu != this )
488  #endif
489  {
490  ACTION_MENU* cxmenu = static_cast<ACTION_MENU*>( menu );
491  evt = cxmenu->eventHandler( aEvent );
492  }
493  }
494 #else
495  if( !evt )
496  runEventHandlers( aEvent, evt );
497 #endif
498 
499  // Handling non-action menu entries (e.g. items in clarification list)
500  // in some context menus, that have IDs explicitly chosen between
501  // ID_POPUP_MENU_START and ID_POPUP_MENU_END
502  // Note also items in clarification list have an id >= 0 and < wxID_LOWEST
503  // in fact 0 to n-1 for n items in clarification list)
504  // id < 0 are automatically created for menuitems created with wxID_ANY
505  #define ID_CONTEXT_MENU_ID_MAX wxID_LOWEST /* = 100 should be enough and better */
506 
507  if( !evt &&
508  ( ( m_selected >= 0 && m_selected < ID_CONTEXT_MENU_ID_MAX ) ||
510  {
511  menuText = GetLabelText( aEvent.GetId() );
513  &menuText );
514  }
515  }
516  }
517 
518  // forward the action/update event to the TOOL_MANAGER
519  // clients that don't supply a tool will have to check GetSelected() themselves
520  if( evt && m_tool )
521  {
522  wxLogTrace( kicadTraceToolStack, "ACTION_MENU::OnMenuEvent %s", evt->Format() );
523 
524  // Pass the position the menu was opened from into the generated event if it is a select event
525  if( type == wxEVT_COMMAND_MENU_SELECTED )
526  evt->SetMousePosition( g_menu_open_position );
527  else
528  evt->SetMousePosition( getToolManager()->GetMousePosition() );
529 
530  if( g_last_menu_highlighted_id == aEvent.GetId() && !m_isContextMenu )
531  evt->SetHasPosition( false );
532 
533  if( m_tool->GetManager() )
534  m_tool->GetManager()->ProcessEvent( *evt );
535  }
536  else
537  {
538  aEvent.Skip();
539  }
540 }
OPT_TOOL_EVENT findToolAction(int aId)
Checks if any of submenus contains a TOOL_ACTION with a specific ID.
static int g_last_menu_highlighted_id
Defines the structure of a menu based on ACTIONs.
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:140
void runEventHandlers(const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
Traverses the submenus tree looking for a submenu capable of handling a particular menu event.
static int GetBaseUIId()
Definition: tool_action.h:107
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:143
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
Definition: action_menu.h:183
#define ID_CONTEXT_MENU_ID_MAX
bool m_isContextMenu
Definition: action_menu.h:223
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
TOOL_EVENT.
Definition: tool_event.h:171
static VECTOR2D g_menu_open_position
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:232
static TOOL_ACTION updateMenu
Definition: actions.h:165
All active tools
Definition: tool_event.h:147
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:235
static void OnCharHook(wxTextEntry *aTextEntry, wxKeyEvent &aEvent)
TOOL_MANAGER * getToolManager() const
Returns an instance of TOOL_MANAGER class.
const wxChar *const kicadTraceToolStack
Flag to enable tracing of the tool handling stack.
VECTOR2D GetMousePosition()
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:556

References AS_GLOBAL, ACTION_MENU::eventHandler(), ACTION_MENU::findToolAction(), g_last_menu_highlighted_id, g_menu_open_position, TOOL_ACTION::GetBaseUIId(), TOOL_BASE::GetManager(), TOOL_MANAGER::GetMousePosition(), ACTION_MENU::getToolManager(), ID_CONTEXT_MENU_ID_MAX, ID_POPUP_MENU_END, ID_POPUP_MENU_START, kicadTraceToolStack, ACTION_MENU::m_dirty, ACTION_MENU::m_isContextMenu, ACTION_MENU::m_selected, ACTION_MENU::m_tool, TEXTENTRY_TRICKS::OnCharHook(), TOOL_MANAGER::ProcessEvent(), TOOL_MANAGER::RunAction(), ACTION_MENU::runEventHandlers(), TA_CHOICE_MENU_CHOICE, TA_CHOICE_MENU_UPDATE, TC_COMMAND, and ACTIONS::updateMenu.

Referenced by TOOL_DISPATCHER::DispatchWxEvent(), and ACTION_MENU::setupEvents().

◆ Resolve()

void CONDITIONAL_MENU::Resolve ( )

Updates the initial contents so that wxWidgets doesn't get its knickers tied in a knot over the menu being empty (mainly an issue on GTK, but also on OSX with the preferences and quit menu items).

Definition at line 104 of file conditional_menu.cpp.

105 {
107  UpdateAll();
108 
109  runOnSubmenus( [] ( ACTION_MENU* aMenu ) {
110  CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( aMenu );
111 
112  if( conditionalMenu )
113  conditionalMenu->Resolve();
114  } );
115 }
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:43
SELECTION g_resolveDummySelection
void UpdateAll()
Runs update handlers for the menu and its submenus.
void Evaluate(SELECTION &aSelection)
Updates the contents of the menu based on the supplied conditions.
void Resolve()
Updates the initial contents so that wxWidgets doesn't get its knickers tied in a knot over the menu ...
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.

References Evaluate(), g_resolveDummySelection, Resolve(), ACTION_MENU::runOnSubmenus(), and ACTION_MENU::UpdateAll().

Referenced by Resolve().

◆ runEventHandlers()

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

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

544 {
545  aToolEvent = eventHandler( aMenuEvent );
546 
547  if( !aToolEvent )
548  runOnSubmenus( std::bind( &ACTION_MENU::runEventHandlers, _1, aMenuEvent, aToolEvent ) );
549 }
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:183
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.

References ACTION_MENU::eventHandler(), and ACTION_MENU::runOnSubmenus().

Referenced by ACTION_MENU::OnMenuEvent().

◆ runOnSubmenus()

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

Runs a function on the menu and all its submenus.

Definition at line 552 of file action_menu.cpp.

553 {
554  try
555  {
556  std::for_each( m_submenus.begin(), m_submenus.end(), [&]( ACTION_MENU* m ) {
557  aFunction( m );
558  m->runOnSubmenus( aFunction );
559  } );
560  }
561  catch( std::exception& )
562  {
563  }
564 }
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:43
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:241

References ACTION_MENU::m_submenus.

Referenced by ACTION_MENU::ClearDirty(), Evaluate(), ACTION_MENU::findToolAction(), Resolve(), ACTION_MENU::runEventHandlers(), ACTION_MENU::SetDirty(), ACTION_MENU::SetTool(), and ACTION_MENU::UpdateAll().

◆ SetDirty()

void ACTION_MENU::SetDirty ( )
inherited

Definition at line 287 of file action_menu.cpp.

288 {
289  m_dirty = true;
290  runOnSubmenus( std::bind( &ACTION_MENU::SetDirty, _1 ) );
291 }
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
void SetDirty()

References ACTION_MENU::m_dirty, and ACTION_MENU::runOnSubmenus().

Referenced by TOOL_MENU::ShowContextMenu().

◆ SetIcon()

◆ SetTitle()

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

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

90 {
91  // Unfortunately wxMenu::SetTitle() does not work very well, so this is an alternative version
92  m_title = aTitle;
93 
94  // Update the menu title
95  if( m_titleDisplayed )
96  DisplayTitle( true );
97 }
bool m_titleDisplayed
Definition: action_menu.h:222
wxString m_title
Menu title
Definition: action_menu.h:226
void DisplayTitle(bool aDisplay=true)
Decides whether a title for a pop up menu should be displayed.

References ACTION_MENU::DisplayTitle(), ACTION_MENU::m_title, and ACTION_MENU::m_titleDisplayed.

Referenced by AddMenuLanguageList(), BUS_UNFOLD_MENU::BUS_UNFOLD_MENU(), EDA_3D_VIEWER::CreateMenuBar(), DIFF_PAIR_MENU::DIFF_PAIR_MENU(), GERBVIEW_SELECTION_TOOL::disambiguationMenu(), PL_SELECTION_TOOL::doSelectionMenu(), GRID_MENU::GRID_MENU(), ROUTER_TOOL::Init(), LENGTH_TUNER_TOOL::Init(), CONVERT_TOOL::Init(), ALIGN_DISTRIBUTE_TOOL::Init(), NET_CONTEXT_MENU::NET_CONTEXT_MENU(), KICAD_MANAGER_FRAME::ReCreateMenuBar(), FOOTPRINT_EDIT_FRAME::ReCreateMenuBar(), LIB_EDIT_FRAME::ReCreateMenuBar(), PL_EDITOR_FRAME::ReCreateMenuBar(), SCH_EDIT_FRAME::ReCreateMenuBar(), GERBVIEW_FRAME::ReCreateMenuBar(), PCB_EDIT_FRAME::ReCreateMenuBar(), SPECIAL_TOOLS_CONTEXT_MENU::SPECIAL_TOOLS_CONTEXT_MENU(), SYMBOL_UNIT_MENU::SYMBOL_UNIT_MENU(), TRACK_WIDTH_MENU::TRACK_WIDTH_MENU(), VIA_SIZE_MENU::VIA_SIZE_MENU(), and ZOOM_MENU::ZOOM_MENU().

◆ SetTool()

void ACTION_MENU::SetTool ( TOOL_INTERACTIVE aTool)
inherited

Sets a tool that is the creator of the menu.

Parameters
aToolis the tool that created the menu.

Definition at line 294 of file action_menu.cpp.

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

References ACTION_MENU::m_tool, and ACTION_MENU::runOnSubmenus().

Referenced by ALIGN_DISTRIBUTE_TOOL::Init(), FOOTPRINT_EDIT_FRAME::ReCreateMenuBar(), LIB_EDIT_FRAME::ReCreateMenuBar(), PL_EDITOR_FRAME::ReCreateMenuBar(), SCH_EDIT_FRAME::ReCreateMenuBar(), GERBVIEW_FRAME::ReCreateMenuBar(), PCB_EDIT_FRAME::ReCreateMenuBar(), TOOL_INTERACTIVE::SetContextMenu(), CVPCB_MAINFRAME::setupTools(), SCH_LINE_WIRE_BUS_TOOL::UnfoldBus(), and BUS_UNFOLD_MENU::update().

◆ setupEvents()

void ACTION_MENU::setupEvents ( )
protectedinherited

Initializes handlers for events.

Definition at line 77 of file action_menu.cpp.

78 {
79 // See wxWidgets hack in TOOL_DISPATCHER::DispatchWxEvent().
80 // Connect( wxEVT_MENU_OPEN, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), NULL, this );
81 // Connect( wxEVT_MENU_HIGHLIGHT, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), NULL, this );
82 // Connect( wxEVT_MENU_CLOSE, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), NULL, this );
83 
84  Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), NULL, this );
85  Connect( wxEVT_IDLE, wxIdleEventHandler( ACTION_MENU::OnIdle ), NULL, this );
86 }
#define NULL
void OnMenuEvent(wxMenuEvent &aEvent)
void OnIdle(wxIdleEvent &event)

References NULL, ACTION_MENU::OnIdle(), and ACTION_MENU::OnMenuEvent().

Referenced by ACTION_MENU::ACTION_MENU().

◆ update()

virtual void ACTION_MENU::update ( )
inlineprotectedvirtualinherited

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, GROUP_CONTEXT_MENU, BUS_UNFOLD_MENU, SYMBOL_UNIT_MENU, VIA_SIZE_MENU, HIGHLIGHT_MENU, GRID_MENU, and ZOOM_MENU.

Definition at line 175 of file action_menu.h.

176  {
177  }

Referenced by ACTION_MENU::UpdateAll().

◆ UpdateAll()

void ACTION_MENU::UpdateAll ( )
inherited

Runs update handlers for the menu and its submenus.

Definition at line 263 of file action_menu.cpp.

264 {
265  try
266  {
267  update();
268  }
269  catch( std::exception& )
270  {
271  }
272 
273  if( m_tool )
274  updateHotKeys();
275 
276  runOnSubmenus( std::bind( &ACTION_MENU::UpdateAll, _1 ) );
277 }
virtual void update()
Update menu state stub.
Definition: action_menu.h:175
void UpdateAll()
Runs update handlers for the menu and its submenus.
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:235
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
void updateHotKeys()
Updates hot key settings for TOOL_ACTIONs in this menu.

References ACTION_MENU::m_tool, ACTION_MENU::runOnSubmenus(), ACTION_MENU::update(), and ACTION_MENU::updateHotKeys().

Referenced by ACTION_TOOLBAR::onToolRightClick(), Resolve(), TOOL_MENU::ShowContextMenu(), KICAD_MANAGER_CONTROL::UpdateMenu(), EDA_3D_CONTROLLER::UpdateMenu(), GERBVIEW_SELECTION_TOOL::UpdateMenu(), PL_SELECTION_TOOL::UpdateMenu(), EE_SELECTION_TOOL::UpdateMenu(), CVPCB_FOOTPRINT_VIEWER_SELECTION_TOOL::UpdateMenu(), CVPCB_CONTROL::UpdateMenu(), and SELECTION_TOOL::UpdateMenu().

◆ updateHotKeys()

void ACTION_MENU::updateHotKeys ( )
protectedinherited

Updates hot key settings for TOOL_ACTIONs in this menu.

Definition at line 328 of file action_menu.cpp.

329 {
330  TOOL_MANAGER* toolMgr = getToolManager();
331 
332  for( auto& ii : m_toolActions )
333  {
334  int id = ii.first;
335  const TOOL_ACTION& action = *ii.second;
336  int key = toolMgr->GetHotKey( action ) & ~MD_MODIFIER_MASK;
337 
338  if( key )
339  {
340  int mod = toolMgr->GetHotKey( action ) & MD_MODIFIER_MASK;
341  int flags = 0;
342  wxMenuItem* item = FindChildItem( id );
343 
344  if( item )
345  {
346  flags |= ( mod & MD_ALT ) ? wxACCEL_ALT : 0;
347  flags |= ( mod & MD_CTRL ) ? wxACCEL_CTRL : 0;
348  flags |= ( mod & MD_SHIFT ) ? wxACCEL_SHIFT : 0;
349 
350  if( !flags )
351  flags = wxACCEL_NORMAL;
352 
353  wxAcceleratorEntry accel( flags, key, id, item );
354  item->SetAccel( &accel );
355  }
356  }
357  }
358 }
int GetHotKey(const TOOL_ACTION &aAction)
TOOL_MANAGER.
Definition: tool_manager.h:51
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: action_menu.h:238
TOOL_MANAGER * getToolManager() const
Returns an instance of TOOL_MANAGER class.
Represents a single user action.
Definition: tool_action.h:44

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

Referenced by ACTION_MENU::UpdateAll().

Member Data Documentation

◆ ANY_ORDER

const int CONDITIONAL_MENU::ANY_ORDER = -1
static

Constant to indicate that we do not care about an ENTRY location in the menu.

Definition at line 42 of file conditional_menu.h.

◆ CHECK

◆ m_dirty

bool ACTION_MENU::m_dirty
protectedinherited

◆ m_entries

std::list<ENTRY> CONDITIONAL_MENU::m_entries
private

List of all menu entries.

Definition at line 242 of file conditional_menu.h.

Referenced by addEntry(), create(), and Evaluate().

◆ m_forcedPosition

wxPoint ACTION_MENU::m_forcedPosition
protectedinherited

Definition at line 218 of file action_menu.h.

◆ m_icon

const BITMAP_OPAQUE* ACTION_MENU::m_icon
protectedinherited

Optional icon

Definition at line 229 of file action_menu.h.

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

◆ m_isContextMenu

bool ACTION_MENU::m_isContextMenu
protectedinherited

Definition at line 223 of file action_menu.h.

Referenced by ACTION_MENU::OnMenuEvent().

◆ m_isForcedPosition

bool ACTION_MENU::m_isForcedPosition
protectedinherited

Definition at line 217 of file action_menu.h.

◆ m_selected

int ACTION_MENU::m_selected
protectedinherited

Stores the id number of selected item.

Definition at line 232 of file action_menu.h.

Referenced by ACTION_MENU::copyFrom(), ACTION_MENU::GetSelected(), and ACTION_MENU::OnMenuEvent().

◆ m_submenus

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

◆ m_title

wxString ACTION_MENU::m_title
protectedinherited

◆ m_titleDisplayed

bool ACTION_MENU::m_titleDisplayed
protectedinherited

◆ m_tool

◆ m_toolActions

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

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

Definition at line 238 of file action_menu.h.

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


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