Add context menu

This commit is contained in:
Chris Pavlina 2016-01-15 20:58:53 -05:00 committed by Chris Pavlina
parent 4dad12794a
commit 06add0d130
2 changed files with 113 additions and 7 deletions

View File

@ -31,6 +31,17 @@
#include <dialog_shim.h> #include <dialog_shim.h>
/**
* Menu IDs for the hotkey context menu
*/
enum ID_WHKL_MENU_IDS
{
ID_EDIT = 2001,
ID_RESET,
ID_RESET_ALL,
};
/** /**
* Class WIDGET_HOTKEY_CLIENT_DATA * Class WIDGET_HOTKEY_CLIENT_DATA
* Stores the hotkey and section tag associated with each row. To change a * Stores the hotkey and section tag associated with each row. To change a
@ -210,21 +221,18 @@ void WIDGET_HOTKEY_LIST::LoadSection( EDA_HOTKEY_CONFIG* aSection )
m_hotkeys.push_back( list ); m_hotkeys.push_back( list );
} }
void WIDGET_HOTKEY_LIST::EditItem( wxTreeListItem aItem )
void WIDGET_HOTKEY_LIST::OnActivated( wxTreeListEvent& aEvent )
{ {
wxTreeListItem item = aEvent.GetItem(); WIDGET_HOTKEY_CLIENT_DATA* hkdata = GetHKClientData( aItem );
WIDGET_HOTKEY_CLIENT_DATA* hkdata = GetHKClientData( item );
if( !hkdata ) if( !hkdata )
{ {
// Activated item was not a hotkey row // Activated item was not a hotkey row
aEvent.Skip();
return; return;
} }
wxString name = GetItemText( item, 0 ); wxString name = GetItemText( aItem, 0 );
wxString current_key = GetItemText( item, 1 ); wxString current_key = GetItemText( aItem, 1 );
wxKeyEvent key_event = HK_PROMPT_DIALOG::PromptForKey( GetParent(), name, current_key ); wxKeyEvent key_event = HK_PROMPT_DIALOG::PromptForKey( GetParent(), name, current_key );
@ -262,6 +270,77 @@ void WIDGET_HOTKEY_LIST::OnActivated( wxTreeListEvent& aEvent )
} }
void WIDGET_HOTKEY_LIST::ResetItem( wxTreeListItem aItem )
{
WIDGET_HOTKEY_CLIENT_DATA* hkdata = GetHKClientData( aItem );
EDA_HOTKEY* hk = &hkdata->GetHotkey();
for( size_t sec_index = 0; sec_index < m_sections.size(); ++sec_index )
{
wxString& section_tag = *( m_sections[sec_index].m_section->m_SectionTag );
if( section_tag != hkdata->GetSectionTag() )
continue;
HOTKEY_LIST& each_list = m_hotkeys[sec_index];
HOTKEY_LIST::iterator hk_it;
for( hk_it = each_list.begin(); hk_it != each_list.end(); ++hk_it )
{
if( hk_it->m_Idcommand == hk->m_Idcommand )
{
hk->m_KeyCode = hk_it->m_KeyCode;
break;
}
}
}
UpdateFromClientData();
}
void WIDGET_HOTKEY_LIST::OnActivated( wxTreeListEvent& aEvent )
{
EditItem( aEvent.GetItem() );
}
void WIDGET_HOTKEY_LIST::OnContextMenu( wxTreeListEvent& aEvent )
{
// Save the active event for use in OnMenu
m_context_menu_item = aEvent.GetItem();
wxMenu menu;
menu.Append( ID_EDIT, _( "Edit..." ) );
menu.Append( ID_RESET, _( "Reset" ) );
menu.Append( wxID_SEPARATOR );
menu.Append( ID_RESET_ALL, _( "Reset all" ) );
PopupMenu( &menu );
}
void WIDGET_HOTKEY_LIST::OnMenu( wxCommandEvent& aEvent )
{
switch( aEvent.GetId() )
{
case ID_EDIT:
EditItem( m_context_menu_item );
break;
case ID_RESET:
ResetItem( m_context_menu_item );
break;
case ID_RESET_ALL:
TransferDataToControl();
break;
default:
wxFAIL_MSG( wxT( "Unknown ID in context menu event" ) );
}
}
void WIDGET_HOTKEY_LIST::OnSize( wxSizeEvent& aEvent ) void WIDGET_HOTKEY_LIST::OnSize( wxSizeEvent& aEvent )
{ {
// Handle this manually - wxTreeListCtrl screws up the width of the first column // Handle this manually - wxTreeListCtrl screws up the width of the first column
@ -382,6 +461,8 @@ WIDGET_HOTKEY_LIST::WIDGET_HOTKEY_LIST( wxWindow* aParent, const HOTKEY_SECTIONS
AppendColumn( _( "Hotkey" ) ); AppendColumn( _( "Hotkey" ) );
Bind( wxEVT_TREELIST_ITEM_ACTIVATED, &WIDGET_HOTKEY_LIST::OnActivated, this ); Bind( wxEVT_TREELIST_ITEM_ACTIVATED, &WIDGET_HOTKEY_LIST::OnActivated, this );
Bind( wxEVT_TREELIST_ITEM_CONTEXT_MENU, &WIDGET_HOTKEY_LIST::OnContextMenu, this );
Bind( wxEVT_MENU, &WIDGET_HOTKEY_LIST::OnMenu, this );
Bind( wxEVT_SIZE, &WIDGET_HOTKEY_LIST::OnSize, this ); Bind( wxEVT_SIZE, &WIDGET_HOTKEY_LIST::OnSize, this );
} }

View File

@ -61,6 +61,7 @@ class WIDGET_HOTKEY_LIST : public wxTreeListCtrl
{ {
HOTKEY_SECTIONS m_sections; HOTKEY_SECTIONS m_sections;
std::vector<HOTKEY_LIST> m_hotkeys; std::vector<HOTKEY_LIST> m_hotkeys;
wxTreeListItem m_context_menu_item;
/** /**
* Method GetHKClientData * Method GetHKClientData
@ -90,12 +91,36 @@ protected:
*/ */
void LoadSection( EDA_HOTKEY_CONFIG* aSection ); void LoadSection( EDA_HOTKEY_CONFIG* aSection );
/**
* Method EditItem
* Prompt the user for a new hotkey given a list item.
*/
void EditItem( wxTreeListItem aItem );
/**
* Method ResetItem
* Reset the item to the original from the dialog was created.
*/
void ResetItem( wxTreeListItem aItem );
/** /**
* Method OnActivated * Method OnActivated
* Handle activation of a row. * Handle activation of a row.
*/ */
void OnActivated( wxTreeListEvent& aEvent ); void OnActivated( wxTreeListEvent& aEvent );
/**
* Method OnContextMenu
* Handle right-click on a row.
*/
void OnContextMenu( wxTreeListEvent& aEvent );
/**
* Method OnMenu
* Handle activation of a context menu item.
*/
void OnMenu( wxCommandEvent& aEvent );
/** /**
* Function OnSize * Function OnSize
* Handle resizing of the control. Overrides the buggy wxTreeListCtrl::OnSize. * Handle resizing of the control. Overrides the buggy wxTreeListCtrl::OnSize.