KiCad PCB EDA Suite
Loading...
Searching...
No Matches
action_menu.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2013-2019 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include "tool/action_menu.h"
24
25#include <wx/grid.h>
26#include <wx/listctrl.h>
27#include <wx/log.h>
28#include <wx/textentry.h>
29#include <wx/stc/stc.h>
30
31#include <bitmaps.h>
32#include <eda_base_frame.h>
33#include <functional>
34#include <id.h>
35#include <kiface_base.h>
36#include <tool/actions.h>
37#include <tool/tool_event.h>
39#include <tool/tool_manager.h>
40#include <trace_helpers.h>
41#include <textentry_tricks.h>
42#include <widgets/ui_common.h>
43
44using namespace std::placeholders;
45
46
47ACTION_MENU::ACTION_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool ) :
48 m_isForcedPosition( false ),
49 m_dirty( true ),
50 m_titleDisplayed( false ),
51 m_isContextMenu( isContextMenu ),
53 m_selected( -1 ),
54 m_tool( aTool )
55{
57}
58
59
61{
62 Disconnect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), nullptr, this );
63 Disconnect( wxEVT_IDLE, wxIdleEventHandler( ACTION_MENU::OnIdle ), nullptr, this );
64
65 // Explicitly release the GDI resources
66 for( auto subitem : GetMenuItems() )
67 subitem->SetBitmap( wxNullBitmap );
68
69 // Free submenus while their native handles are alive. The base class frees our handle first, which on
70 // Windows frees theirs too, and each orphaned submenu would then free its dead handle again
71 for( int i = GetMenuItemCount() - 1; i >= 0; --i )
72 {
73 wxMenuItem* item = FindItemByPosition( i );
74
75 if( !item->IsSubMenu() )
76 continue;
77
78 wxMenu* submenu = item->GetSubMenu();
79
80 m_submenus.remove_if(
81 [submenu]( ACTION_MENU* aMenu )
82 {
83 return aMenu == submenu;
84 } );
85
86 Destroy( item );
87 }
88
89 // Set parent to NULL to prevent submenus from unregistering from a nonexistent object
90 for( ACTION_MENU* menu : m_submenus )
91 menu->SetParent( nullptr );
92
93 ACTION_MENU* parent = dynamic_cast<ACTION_MENU*>( GetParent() );
94
95 if( parent )
96 parent->m_submenus.remove( this );
97}
98
99
101{
102 m_icon = aIcon;
103}
104
105
107{
108 Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( ACTION_MENU::OnMenuEvent ), nullptr, this );
109 Connect( wxEVT_IDLE, wxIdleEventHandler( ACTION_MENU::OnIdle ), nullptr, this );
110}
111
112
113void ACTION_MENU::SetTitle( const wxString& aTitle )
114{
115 // Unfortunately wxMenu::SetTitle() does not work very well, so this is an alternative version
116 m_title = aTitle;
117
118 // Update the menu title
119 if( m_titleDisplayed )
120 DisplayTitle( true );
121}
122
123
124void ACTION_MENU::DisplayTitle( bool aDisplay )
125{
126 if( ( !aDisplay || m_title.IsEmpty() ) && m_titleDisplayed )
127 {
128 // Destroy the menu entry keeping the title..
129 wxMenuItem* item = FindItemByPosition( 0 );
130 wxASSERT( item->GetItemLabelText() == GetTitle() );
131 item->SetBitmap( wxNullBitmap );
132 Destroy( item );
133
134 // ..and separator
135 item = FindItemByPosition( 0 );
136 wxASSERT( item->IsSeparator() );
137 Destroy( item );
138 m_titleDisplayed = false;
139 }
140
141 else if( aDisplay && !m_title.IsEmpty() )
142 {
143 if( m_titleDisplayed )
144 {
145 // Simply update the title
146 FindItemByPosition( 0 )->SetItemLabel( m_title );
147 }
148 else
149 {
150 // Add a separator and a menu entry to display the title
151 InsertSeparator( 0 );
152 Insert( 0, new wxMenuItem( this, wxID_NONE, m_title, wxEmptyString, wxITEM_NORMAL ) );
153
154 if( !!m_icon )
155 KIUI::AddBitmapToMenuItem( FindItemByPosition( 0 ), KiBitmapBundle( m_icon ) );
156
157 m_titleDisplayed = true;
158 }
159 }
160}
161
162
163wxMenuItem* ACTION_MENU::Add( const wxString& aLabel, int aId, BITMAPS aIcon )
164{
165 wxASSERT_MSG( FindItem( aId ) == nullptr, wxS( "Duplicate menu IDs!" ) );
166
167 wxMenuItem* item = new wxMenuItem( this, aId, aLabel, wxEmptyString, wxITEM_NORMAL );
168
169 if( !!aIcon )
171
172 return Append( item );
173}
174
175
176wxMenuItem* ACTION_MENU::Add( const wxString& aLabel, const wxString& aTooltip, int aId,
177 BITMAPS aIcon, bool aIsCheckmarkEntry )
178{
179 wxASSERT_MSG( FindItem( aId ) == nullptr, wxS( "Duplicate menu IDs!" ) );
180
181 wxMenuItem* item = new wxMenuItem( this, aId, aLabel, aTooltip, aIsCheckmarkEntry ? wxITEM_CHECK
182 : wxITEM_NORMAL );
183
184 if( !!aIcon )
186
187 return Append( item );
188}
189
190
191wxMenuItem* ACTION_MENU::Add( const TOOL_ACTION& aAction, bool aIsCheckmarkEntry,
192 const wxString& aOverrideLabel )
193{
194 // ID numbers for tool actions are assigned above ACTION_BASE_UI_ID inside TOOL_EVENT
195 BITMAPS icon = aAction.GetIcon();
196
197 // Allow the label to be overridden at point of use
198 wxString menuLabel = aOverrideLabel.IsEmpty() ? aAction.GetMenuItem() : aOverrideLabel;
199
200 wxMenuItem* item = new wxMenuItem( this, aAction.GetUIId(), menuLabel, aAction.GetTooltip(),
201 aIsCheckmarkEntry ? wxITEM_CHECK : wxITEM_NORMAL );
202 if( !!icon )
204
205 m_toolActions[aAction.GetUIId()] = &aAction;
206
207 return Append( item );
208}
209
210
211wxMenuItem* ACTION_MENU::Add( ACTION_MENU* aMenu )
212{
213 m_submenus.push_back( aMenu );
214
215 wxASSERT_MSG( !aMenu->m_title.IsEmpty(), wxS( "Set a title for ACTION_MENU using SetTitle()" ) );
216
217 if( !!aMenu->m_icon )
218 {
219 wxMenuItem* newItem = new wxMenuItem( this, -1, aMenu->m_title );
220 KIUI::AddBitmapToMenuItem( newItem, KiBitmapBundle( aMenu->m_icon ) );
221 newItem->SetSubMenu( aMenu );
222 return Append( newItem );
223 }
224 else
225 {
226 return AppendSubMenu( aMenu, aMenu->m_title );
227 }
228}
229
230
231void ACTION_MENU::AddClose( const wxString& aAppname )
232{
233#ifdef __WINDOWS__
234 Add( _( "Close" ),
235 wxString::Format( _( "Close %s" ), aAppname ),
236 wxID_CLOSE,
238#else
239 Add( _( "Close" ) + wxS( "\tCtrl+W" ),
240 wxString::Format( _( "Close %s" ), aAppname ),
241 wxID_CLOSE,
243#endif
244}
245
246
247void ACTION_MENU::AddQuitOrClose( KIFACE_BASE* aKiface, wxString aAppname )
248{
249 if( !aKiface || aKiface->IsSingle() ) // not when under a project mgr
250 {
251 // Don't use ACTIONS::quit; wxWidgets moves this on OSX and expects to find it via
252 // wxID_EXIT
253 Add( _( "Quit" ) + wxS( "\tCtrl+Q" ),
254 wxString::Format( _( "Quit %s" ), aAppname ),
255 wxID_EXIT,
257 }
258 else
259 {
260 AddClose( aAppname );
261 }
262}
263
264
265void ACTION_MENU::AddQuit( const wxString& aAppname )
266{
267 // Don't use ACTIONS::quit; wxWidgets moves this on OSX and expects to find it via
268 // wxID_EXIT
269 Add( _( "Quit" ) + wxS( "\tCtrl+Q" ),
270 wxString::Format( _( "Quit %s" ), aAppname ),
271 wxID_EXIT,
273}
274
275
277{
278 m_titleDisplayed = false;
279
280 for( int i = GetMenuItemCount() - 1; i >= 0; --i )
281 Destroy( FindItemByPosition( i ) );
282
283 m_toolActions.clear();
284 m_submenus.clear();
285
286 wxASSERT( GetMenuItemCount() == 0 );
287}
288
289
291{
292 for( wxMenuItem* item : GetMenuItems() )
293 {
294 if( item->IsEnabled() && !item->IsSeparator() )
295 return true;
296 }
297
298 return false;
299}
300
301
303{
304 try
305 {
306 update();
307 }
308 catch( std::exception& )
309 {
310 }
311
312 if( m_tool )
314
315 runOnSubmenus( std::bind( &ACTION_MENU::UpdateAll, _1 ) );
316}
317
318
320{
321 m_dirty = false;
322 runOnSubmenus( std::bind( &ACTION_MENU::ClearDirty, _1 ) );
323}
324
325
327{
328 m_dirty = true;
329 runOnSubmenus( std::bind( &ACTION_MENU::SetDirty, _1 ) );
330}
331
332
334{
335 m_tool = aTool;
336 runOnSubmenus( std::bind( &ACTION_MENU::SetTool, _1, aTool ) );
337}
338
339
341{
342 ACTION_MENU* clone = create();
343 clone->Clear();
344 clone->copyFrom( *this );
345 return clone;
346}
347
348
350{
351 ACTION_MENU* menu = new ACTION_MENU( false );
352
353 wxASSERT_MSG( typeid( *this ) == typeid( *menu ),
354 wxString::Format( "You need to override create() method for class %s", typeid( *this ).name() ) );
355
356 return menu;
357}
358
359
361{
362 return m_tool ? m_tool->GetManager() : nullptr;
363}
364
365
367{
368 TOOL_MANAGER* toolMgr = getToolManager();
369
370 wxASSERT( toolMgr );
371
372 for( std::pair<const int, const TOOL_ACTION*>& ii : m_toolActions )
373 {
374 int id = ii.first;
375 const TOOL_ACTION& action = *ii.second;
376 int key = toolMgr->GetHotKey( action ) & ~MD_MODIFIER_MASK;
377
378 if( key > 0 )
379 {
380 int mod = toolMgr->GetHotKey( action ) & MD_MODIFIER_MASK;
381 int flags = 0;
382 wxMenuItem* item = FindChildItem( id );
383
384 if( item )
385 {
386 flags |= ( mod & MD_ALT ) ? wxACCEL_ALT : 0;
387 flags |= ( mod & MD_CTRL ) ? wxACCEL_CTRL : 0;
388 flags |= ( mod & MD_SHIFT ) ? wxACCEL_SHIFT : 0;
389
390 if( !flags )
391 flags = wxACCEL_NORMAL;
392
393 wxAcceleratorEntry accel( flags, key, id, item );
394 item->SetAccel( &accel );
395 }
396 }
397 }
398}
399
400
401// wxWidgets doesn't tell us when a menu command was generated from a hotkey or from
402// a menu selection. It's important to us because a hotkey can be an immediate action
403// while the menu selection can not (as it has no associated position).
404//
405// We get around this by storing the last highlighted menuId. If it matches the command
406// id then we know this is a menu selection. (You might think we could use the menuOpen
407// menuClose events, but these are actually generated for hotkeys as well.)
408
410
411
412// We need to store the position of the mouse when the menu was opened so it can be passed
413// to the command event generated when the menu item is selected.
415
416
417void ACTION_MENU::OnIdle( wxIdleEvent& event )
418{
420 g_menu_open_position.x = 0.0;
421 g_menu_open_position.y = 0.0;
422}
423
424
425void ACTION_MENU::OnMenuEvent( wxMenuEvent& aEvent )
426{
427 OPT_TOOL_EVENT evt;
428 wxString menuText;
429 wxEventType type = aEvent.GetEventType();
430 wxWindow* focus = wxWindow::FindFocus();
431 TOOL_MANAGER* toolMgr = getToolManager();
432
433 if( type == wxEVT_MENU_OPEN )
434 {
435 if( m_dirty && toolMgr )
436 toolMgr->RunAction<ACTION_MENU*>( ACTIONS::updateMenu, this );
437
438 wxMenu* parent = dynamic_cast<wxMenu*>( GetParent() );
439
440 // Don't update the position if this menu has a parent or is a menubar menu
441 if( !parent && !IsAttached() && toolMgr )
443
445 }
446 else if( type == wxEVT_MENU_HIGHLIGHT )
447 {
448 if( aEvent.GetId() > 0 )
449 g_last_menu_highlighted_id = aEvent.GetId();
450
451 evt = TOOL_EVENT( TC_COMMAND, TA_CHOICE_MENU_UPDATE, aEvent.GetId() );
452 }
453 else if( type == wxEVT_COMMAND_MENU_SELECTED )
454 {
455 // Despite our attempts to catch the theft of text editor CHAR_HOOK and CHAR events
456 // in TOOL_DISPATCHER::DispatchWxEvent, wxWidgets sometimes converts those it knows
457 // about into menu commands without ever generating the appropriate CHAR_HOOK and CHAR
458 // events first.
459 if( dynamic_cast<wxTextEntry*>( focus )
460 || dynamic_cast<wxStyledTextCtrl*>( focus )
461 || dynamic_cast<wxListView*>( focus )
462 || dynamic_cast<wxGrid*>( focus ) )
463 {
464 // Original key event has been lost, so we have to re-create it from the menu's
465 // wxAcceleratorEntry.
466 wxMenuItem* menuItem = FindItem( aEvent.GetId() );
467 wxAcceleratorEntry* acceleratorKey = menuItem ? menuItem->GetAccel() : nullptr;
468
469 if( acceleratorKey )
470 {
471 wxKeyEvent keyEvent( wxEVT_CHAR_HOOK );
472 keyEvent.m_keyCode = acceleratorKey->GetKeyCode();
473 keyEvent.m_controlDown = ( acceleratorKey->GetFlags() & wxMOD_CONTROL ) > 0;
474 keyEvent.m_shiftDown = ( acceleratorKey->GetFlags() & wxMOD_SHIFT ) > 0;
475 keyEvent.m_altDown = ( acceleratorKey->GetFlags() & wxMOD_ALT ) > 0;
476
477 if( wxTextEntry* ctrl = dynamic_cast<wxTextEntry*>( focus ) )
478 TEXTENTRY_TRICKS::OnCharHook( ctrl, keyEvent );
479 else
480 focus->HandleWindowEvent( keyEvent );
481
482 if( keyEvent.GetSkipped() )
483 {
484 keyEvent.SetEventType( wxEVT_CHAR );
485
486 // CHAR_HOOK always uses uppercase key codes for letters, but
487 // wxEVT_CHAR should use the translated (case-correct) character.
488 // Since the original key event was lost to the menu system, check
489 // the actual keyboard state to determine proper case.
490 int keyCode = keyEvent.GetKeyCode();
491
492 if( keyCode >= 'A' && keyCode <= 'Z' )
493 {
494 bool shiftActive = wxGetKeyState( WXK_SHIFT );
495 bool capsActive = wxGetKeyState( WXK_CAPITAL );
496
497 if( !( shiftActive ^ capsActive ) )
498 keyEvent.m_keyCode = keyCode + 32; // Convert to lowercase
499 }
500
501 #if wxUSE_UNICODE
502 keyEvent.m_uniChar = keyEvent.m_keyCode;
503 #endif
504
505 focus->HandleWindowEvent( keyEvent );
506
507 // A native text box types from the platform key event, which the menu
508 // took, so the character has to be put in by hand. A shifted key other
509 // than a letter is left alone, since its character depends on the layout.
510 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( focus );
511 bool isLetter = keyCode >= 'A' && keyCode <= 'Z';
512
513 if( textEntry && textEntry->IsEditable() && keyEvent.GetSkipped() && !keyEvent.ControlDown()
514 && !keyEvent.AltDown() && keyCode >= WXK_SPACE && keyCode < WXK_START
515 && ( isLetter || !keyEvent.ShiftDown() ) )
516 {
517 textEntry->WriteText( wxString( wxUniChar( keyEvent.m_keyCode ) ) );
518 return;
519 }
520 }
521
522 // Don't bubble-up dangerous actions; the target may be behind a modeless dialog.
523 // Cf. https://gitlab.com/kicad/code/kicad/-/issues/17229
524 if( keyEvent.GetKeyCode() == WXK_BACK || keyEvent.GetKeyCode() == WXK_DELETE )
525 return;
526
527 // If the event was used as a KEY event (not skipped) by the focused window,
528 // just finish. Otherwise this is actually a wxEVT_COMMAND_MENU_SELECTED (or the
529 // focused window is read only).
530 if( !keyEvent.GetSkipped() )
531 return;
532 }
533 }
534
535 // Store the selected position, so it can be checked by the tools
536 m_selected = aEvent.GetId();
537
538 ACTION_MENU* parent = dynamic_cast<ACTION_MENU*>( GetParent() );
539
540 while( parent )
541 {
542 parent->m_selected = m_selected;
543 parent = dynamic_cast<ACTION_MENU*>( parent->GetParent() );
544 }
545
546 // Check if there is a TOOL_ACTION for the given UI ID
547 if( toolMgr && toolMgr->GetActionManager()->IsActionUIId( m_selected ) )
548 evt = findToolAction( m_selected );
549
550 if( !evt )
551 {
552#ifdef __WINDOWS__
553 if( !evt )
554 {
555 // Try to find the submenu which holds the selected item
556 wxMenu* menu = nullptr;
557 FindItem( m_selected, &menu );
558
559 // This conditional compilation is probably not needed.
560 if( menu )
561 {
562 ACTION_MENU* cxmenu = static_cast<ACTION_MENU*>( menu );
563 evt = cxmenu->eventHandler( aEvent );
564 }
565 }
566#else
567 if( !evt )
568 runEventHandlers( aEvent, evt );
569#endif
570
571 // Handling non-ACTION menu entries. Two ranges of ids are supported:
572 // between 0 and ID_CONTEXT_MENU_ID_MAX
573 // between ID_POPUP_MENU_START and ID_POPUP_MENU_END
574
575 #define ID_CONTEXT_MENU_ID_MAX wxID_LOWEST /* = 100 should be plenty */
576
577 if( !evt && ( ( m_selected >= 0 && m_selected < ID_CONTEXT_MENU_ID_MAX )
579 {
580 ACTION_MENU* actionMenu = dynamic_cast<ACTION_MENU*>( GetParent() );
581
582 if( PassHelpTextToHandler() || ( actionMenu && actionMenu->PassHelpTextToHandler() ) )
583 menuText = GetHelpString( aEvent.GetId() );
584 else
585 menuText = GetLabelText( aEvent.GetId() );
586
588 evt->SetParameter( &menuText );
589 }
590 }
591 }
592
593 // forward the action/update event to the TOOL_MANAGER
594 // clients that don't supply a tool will have to check GetSelected() themselves
595 if( evt && toolMgr )
596 {
597 wxLogTrace( kicadTraceToolStack, wxS( "ACTION_MENU::OnMenuEvent %s" ), evt->Format() );
598
599 // WARNING: if you're squeamish, look away.
600 // What follows is a series of egregious hacks necessitated by a lack of information from
601 // wxWidgets on where context-menu-commands and command-key-events originated.
602
603 // If it's a context menu then fetch the mouse position from our context-menu-position
604 // hack.
605 if( m_isContextMenu )
606 {
607 evt->SetMousePosition( g_menu_open_position );
608 }
609 // Check if it is a menubar event, and don't get any position if it is. Note that we
610 // can't use IsAttached() here, as it only differentiates a context menu, and we also
611 // want a hotkey to generate a position.
612 else if( g_last_menu_highlighted_id == aEvent.GetId() )
613 {
614 evt->SetHasPosition( false );
615 }
616 // Otherwise it's a command-key-event and we need to get the mouse position from the tool
617 // manager so that immediate actions work.
618 else
619 {
620 evt->SetMousePosition( toolMgr->GetMousePosition() );
621 }
622
623 toolMgr->ProcessEvent( *evt );
624 }
625 else
626 {
627 aEvent.Skip();
628 }
629}
630
631
632void ACTION_MENU::runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent )
633{
634 aToolEvent = eventHandler( aMenuEvent );
635
636 if( !aToolEvent )
637 runOnSubmenus( std::bind( &ACTION_MENU::runEventHandlers, _1, aMenuEvent, aToolEvent ) );
638}
639
640
641void ACTION_MENU::runOnSubmenus( std::function<void(ACTION_MENU*)> aFunction )
642{
643 try
644 {
645 std::for_each( m_submenus.begin(), m_submenus.end(),
646 [&]( ACTION_MENU* m )
647 {
648 aFunction( m );
649 m->runOnSubmenus( aFunction );
650 } );
651 }
652 catch( std::exception& )
653 {
654 }
655}
656
657
659{
660 OPT_TOOL_EVENT evt;
661
662 auto findFunc =
663 [&]( ACTION_MENU* m )
664 {
665 if( evt )
666 return;
667
668 const auto it = m->m_toolActions.find( aId );
669
670 if( it != m->m_toolActions.end() )
671 evt = it->second->MakeEvent();
672 };
673
674 findFunc( this );
675
676 if( !evt )
677 runOnSubmenus( findFunc );
678
679 return evt;
680}
681
682
684{
685 m_icon = aMenu.m_icon;
686 m_title = aMenu.m_title;
688 m_selected = -1; // aMenu.m_selected;
689 m_tool = aMenu.m_tool;
691
692 // Copy all menu entries
693 for( int i = 0; i < (int) aMenu.GetMenuItemCount(); ++i )
694 {
695 wxMenuItem* item = aMenu.FindItemByPosition( i );
696 appendCopy( item );
697 }
698}
699
700
701wxMenuItem* ACTION_MENU::appendCopy( const wxMenuItem* aSource )
702{
703 wxMenuItem* newItem = new wxMenuItem( this, aSource->GetId(), aSource->GetItemLabel(),
704 aSource->GetHelp(), aSource->GetKind() );
705
706 // Add the source bitmap if it is not the wxNullBitmap
707 // On Windows, for Checkable Menu items, adding a bitmap adds also
708 // our predefined checked alternate bitmap
709 // On other OS, wxITEM_CHECK and wxITEM_RADIO Menu items do not use custom bitmaps.
710
711#if defined( __WXMSW__ )
712 // On Windows, AddBitmapToMenuItem() uses the unchecked bitmap for wxITEM_CHECK and
713 // wxITEM_RADIO menuitems and automatically adds a checked bitmap.
714 // For other menuitrms, use the "checked" bitmap.
715 bool use_checked_bm = ( aSource->GetKind() == wxITEM_CHECK ||
716 aSource->GetKind() == wxITEM_RADIO ) ? false : true;
717 const wxBitmap& src_bitmap = aSource->GetBitmap( use_checked_bm );
718#else
719 const wxBitmap& src_bitmap = aSource->GetBitmap();
720#endif
721
722 if( src_bitmap.IsOk() && src_bitmap.GetHeight() > 1 ) // a null bitmap has a 0 size
723 KIUI::AddBitmapToMenuItem( newItem, src_bitmap );
724
725 if( aSource->IsSubMenu() )
726 {
727 ACTION_MENU* menu = dynamic_cast<ACTION_MENU*>( aSource->GetSubMenu() );
728 wxASSERT_MSG( menu, wxS( "Submenus are expected to be a ACTION_MENU" ) );
729
730 if( menu )
731 {
732 ACTION_MENU* menuCopy = menu->Clone();
733 newItem->SetSubMenu( menuCopy );
734 m_submenus.push_back( menuCopy );
735 }
736 }
737
738 // wxMenuItem has to be added before enabling/disabling or checking
739 Append( newItem );
740
741 if( aSource->IsCheckable() )
742 newItem->Check( aSource->IsChecked() );
743
744 newItem->Enable( aSource->IsEnabled() );
745
746 return newItem;
747}
static int g_last_menu_highlighted_id
static VECTOR2D g_menu_open_position
#define ID_CONTEXT_MENU_ID_MAX
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
BITMAPS
A list of all bitmap identifiers.
@ INVALID_BITMAP
static TOOL_ACTION updateMenu
Definition actions.h:268
bool IsActionUIId(int aId) const
Test if a UI ID corresponds to an action ID in our system.
void updateHotKeys()
Update hot key settings for TOOL_ACTIONs in this menu.
ACTION_MENU(bool isContextMenu, TOOL_INTERACTIVE *aTool=nullptr)
Default constructor.
TOOL_MANAGER * getToolManager() const
Return an instance of TOOL_MANAGER class.
void setupEvents()
Initialize handlers for events.
wxString m_title
Menu title.
virtual ACTION_MENU * create() const
Return an instance of this class. It has to be overridden in inheriting classes.
virtual void update()
Update menu state stub.
void AddQuitOrClose(KIFACE_BASE *aKiface, wxString aAppname="")
Add either a standard Quit or Close item to the menu.
void ClearDirty()
Clear the dirty flag on the menu and all descendants.
bool m_isForcedPosition
wxMenuItem * appendCopy(const wxMenuItem *aSource)
Append a copy of wxMenuItem.
OPT_TOOL_EVENT findToolAction(int aId)
Check if any of submenus contains a TOOL_ACTION with a specific ID.
void runEventHandlers(const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
Traverse the submenus tree looking for a submenu capable of handling a particular menu event.
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
bool m_isContextMenu
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
void OnMenuEvent(wxMenuEvent &aEvent)
void AddClose(const wxString &aAppname="")
Add a standard close item to the menu with the accelerator key CTRL-W.
void DisplayTitle(bool aDisplay=true)
Decide whether a title for a pop up menu should be displayed.
void AddQuit(const wxString &aAppname="")
Add a standard Quit item to the menu.
ACTION_MENU * Clone() const
Create a deep, recursive copy of this ACTION_MENU.
void Clear()
Remove all the entries from the menu (as well as its title).
void UpdateAll()
Run update handlers for the menu and its submenus.
void SetTitle(const wxString &aTitle) override
Set title for the menu.
void SetTool(TOOL_INTERACTIVE *aTool)
Set a tool that is the creator of the menu.
void OnIdle(wxIdleEvent &event)
std::list< ACTION_MENU * > m_submenus
List of submenus.
void SetIcon(BITMAPS aIcon)
Assign an icon for the entry.
~ACTION_MENU() override
wxString GetTitle() const
Definition action_menu.h:63
wxMenuItem * Add(const wxString &aLabel, int aId, BITMAPS aIcon)
Add a wxWidgets-style entry to the menu.
bool m_titleDisplayed
friend class TOOL_INTERACTIVE
int m_selected
Store the id number of selected item.
BITMAPS m_icon
Optional icon.
bool HasEnabledItems() const
Return true if the menu has any enabled items.
TOOL_INTERACTIVE * m_tool
Creator of the menu.
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Run a function on the menu and all its submenus.
virtual bool PassHelpTextToHandler()
void copyFrom(const ACTION_MENU &aMenu)
Copy another menus data to this instance.
A KIFACE implementation.
Definition kiface_base.h:35
bool IsSingle() const
Is this KIFACE running under single_top?
Represent a single user action.
wxString GetTooltip(bool aIncludeHotkey=true) const
BITMAPS GetIcon() const
Return an icon associated with the action.
wxString GetMenuItem() const
int GetUIId() const
Get the unique ID for this action in the user interface system.
Generic, UI-independent tool event.
Definition tool_event.h:167
Master controller class:
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagate an event to tools that requested events of matching type(s).
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
ACTION_MANAGER * GetActionManager() const
VECTOR2D GetMousePosition() const
int GetHotKey(const TOOL_ACTION &aAction) const
Return the hot key associated with a given action or 0 if there is none.
#define _(s)
Base window classes and related definitions.
const wxChar *const kicadTraceToolStack
Flag to enable tracing of the tool handling stack.
@ ID_POPUP_MENU_START
Definition id.h:159
@ ID_POPUP_MENU_END
Definition id.h:164
KICOMMON_API void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmapBundle &aImage)
Add a bitmap to a menuitem.
static void OnCharHook(wxTextEntry *aTextEntry, wxKeyEvent &aEvent)
@ AS_GLOBAL
Global action (toolbar/main menu event, global shortcut)
Definition tool_action.h:45
@ TA_CHOICE_MENU_CHOICE
Context menu choice.
Definition tool_event.h:94
@ TA_CHOICE_MENU_UPDATE
Context menu update.
Definition tool_event.h:90
std::optional< TOOL_EVENT > OPT_TOOL_EVENT
Definition tool_event.h:652
@ MD_MODIFIER_MASK
Definition tool_event.h:145
@ MD_ALT
Definition tool_event.h:141
@ MD_CTRL
Definition tool_event.h:140
@ MD_SHIFT
Definition tool_event.h:139
@ TC_COMMAND
Definition tool_event.h:53
wxLogTrace helper definitions.
Functions to provide common constants and other functions to assist in making a consistent UI.
VECTOR2< double > VECTOR2D
Definition vector2d.h:707