From 8bb5eaa434875c0faf0abd58d5b1c666098618cb Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Tue, 13 May 2014 11:22:50 +0200 Subject: [PATCH] Better way of adding CONTEXT_MENU entries. --- common/tool/context_menu.cpp | 47 ++++++++++++++---------------------- include/tool/context_menu.h | 8 ------ 2 files changed, 18 insertions(+), 37 deletions(-) diff --git a/common/tool/context_menu.cpp b/common/tool/context_menu.cpp index 6dfab27e58..d45bbb3602 100644 --- a/common/tool/context_menu.cpp +++ b/common/tool/context_menu.cpp @@ -101,17 +101,28 @@ void CONTEXT_MENU::Add( const TOOL_ACTION& aAction ) { /// ID numbers for tool actions need to have a value higher than m_actionId int id = m_actionId + aAction.GetId(); - wxString menuEntry; + + wxMenuItem* item = new wxMenuItem( &m_menu, id, + wxString( aAction.GetMenuItem().c_str(), wxConvUTF8 ), + wxString( aAction.GetDescription().c_str(), wxConvUTF8 ), wxITEM_NORMAL ); if( aAction.HasHotKey() ) - menuEntry = wxString( ( aAction.GetMenuItem() + '\t' + - getHotKeyDescription( aAction ) ).c_str(), wxConvUTF8 ); - else - menuEntry = wxString( aAction.GetMenuItem().c_str(), wxConvUTF8 ); + { + int key = aAction.GetHotKey() & ~MD_MODIFIER_MASK; + int mod = aAction.GetHotKey() & MD_MODIFIER_MASK; + wxAcceleratorEntryFlags flags = wxACCEL_NORMAL; - m_menu.Append( new wxMenuItem( &m_menu, id, menuEntry, - wxString( aAction.GetDescription().c_str(), wxConvUTF8 ), wxITEM_NORMAL ) ); + switch( mod ) + { + case MD_ALT: flags = wxACCEL_ALT; break; + case MD_CTRL: flags = wxACCEL_CTRL; break; + case MD_SHIFT: flags = wxACCEL_SHIFT; break; + } + item->SetAccel( new wxAcceleratorEntry( flags, key, id, item ) ); + } + + m_menu.Append( item ); m_toolActions[id] = &aAction; } @@ -128,28 +139,6 @@ void CONTEXT_MENU::Clear() } -std::string CONTEXT_MENU::getHotKeyDescription( const TOOL_ACTION& aAction ) const -{ - int hotkey = aAction.GetHotKey(); - - std::string description = ""; - - if( hotkey & MD_ALT ) - description += "ALT+"; - - if( hotkey & MD_CTRL ) - description += "CTRL+"; - - if( hotkey & MD_SHIFT ) - description += "SHIFT+"; - - // TODO dispatch keys such as Fx, TAB, PG_UP/DN, HOME, END, etc. - description += char( hotkey & ~MD_MODIFIER_MASK ); - - return description; -} - - void CONTEXT_MENU::CMEventHandler::onEvent( wxEvent& aEvent ) { TOOL_EVENT evt; diff --git a/include/tool/context_menu.h b/include/tool/context_menu.h index df6f7cb0b7..e181b576bb 100644 --- a/include/tool/context_menu.h +++ b/include/tool/context_menu.h @@ -128,14 +128,6 @@ private: m_tool = aTool; } - /** - * Function getHotKeyDescription() - * Returns a hot key in the string format accepted by wxMenu. - * @param aAction is the action with hot key to be translated.. - * @return Hot key in the string format compatible with wxMenu. - */ - std::string getHotKeyDescription( const TOOL_ACTION& aAction ) const; - ///> Flag indicating that the menu title was set up. bool m_titleSet;