2016-01-16 01:56:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Chris Pavlina <pavlina.chris@gmail.com>
|
2017-02-19 02:39:55 +00:00
|
|
|
* Copyright (C) 2016-2017 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-01-16 01:56:57 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2016-01-16 02:07:12 +00:00
|
|
|
#include <cctype>
|
|
|
|
|
2016-01-16 01:56:57 +00:00
|
|
|
#include <widgets/widget_hotkey_list.h>
|
|
|
|
|
2016-01-16 01:58:23 +00:00
|
|
|
#include <wx/statline.h>
|
2016-01-16 01:56:57 +00:00
|
|
|
|
2019-06-11 16:38:00 +00:00
|
|
|
#include <bitmaps.h>
|
|
|
|
#include <confirm.h>
|
2016-01-16 01:57:10 +00:00
|
|
|
#include <dialog_shim.h>
|
2019-06-11 16:38:00 +00:00
|
|
|
#include <draw_frame.h>
|
|
|
|
|
|
|
|
#include <panel_hotkeys_editor.h>
|
2016-01-16 01:56:57 +00:00
|
|
|
|
|
|
|
|
2016-01-16 02:06:51 +00:00
|
|
|
/**
|
|
|
|
* Minimum width of the hotkey column
|
|
|
|
*/
|
|
|
|
static const int HOTKEY_MIN_WIDTH = 100;
|
|
|
|
|
2016-01-16 02:07:12 +00:00
|
|
|
|
2016-01-16 01:58:53 +00:00
|
|
|
/**
|
|
|
|
* Menu IDs for the hotkey context menu
|
|
|
|
*/
|
|
|
|
enum ID_WHKL_MENU_IDS
|
|
|
|
{
|
|
|
|
ID_EDIT = 2001,
|
|
|
|
ID_RESET,
|
2016-03-30 18:51:47 +00:00
|
|
|
ID_DEFAULT,
|
2016-01-16 01:58:53 +00:00
|
|
|
ID_RESET_ALL,
|
2016-03-30 18:51:47 +00:00
|
|
|
ID_DEFAULT_ALL,
|
2016-01-16 01:58:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-16 01:56:57 +00:00
|
|
|
/**
|
|
|
|
* Class WIDGET_HOTKEY_CLIENT_DATA
|
2018-09-26 14:26:26 +00:00
|
|
|
* Stores the hotkey change data associated with each row. To change a
|
|
|
|
* hotkey, edit it via GetCurrentValue() in the row's client data, then call
|
|
|
|
* WIDGET_HOTKEY_LIST::UpdateFromClientData().
|
2016-01-16 01:56:57 +00:00
|
|
|
*/
|
2016-01-16 02:06:25 +00:00
|
|
|
class WIDGET_HOTKEY_CLIENT_DATA : public wxClientData
|
2016-01-16 01:56:57 +00:00
|
|
|
{
|
2018-09-26 14:26:26 +00:00
|
|
|
CHANGED_HOTKEY& m_changed_hotkey;
|
2016-01-16 01:56:57 +00:00
|
|
|
|
|
|
|
public:
|
2018-09-26 14:26:26 +00:00
|
|
|
WIDGET_HOTKEY_CLIENT_DATA( CHANGED_HOTKEY& aChangedHotkey )
|
|
|
|
: m_changed_hotkey( aChangedHotkey )
|
2016-01-16 02:06:25 +00:00
|
|
|
{}
|
2016-01-16 01:56:57 +00:00
|
|
|
|
2018-09-26 14:26:26 +00:00
|
|
|
CHANGED_HOTKEY& GetChangedHotkey() { return m_changed_hotkey; }
|
2016-01-16 01:56:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-16 01:57:10 +00:00
|
|
|
/**
|
|
|
|
* Class HK_PROMPT_DIALOG
|
|
|
|
* Dialog to prompt the user to enter a key.
|
|
|
|
*/
|
2016-01-16 02:06:25 +00:00
|
|
|
class HK_PROMPT_DIALOG : public DIALOG_SHIM
|
2016-01-16 01:57:10 +00:00
|
|
|
{
|
|
|
|
wxKeyEvent m_event;
|
|
|
|
|
|
|
|
public:
|
|
|
|
HK_PROMPT_DIALOG( wxWindow* aParent, wxWindowID aId, const wxString& aTitle,
|
2019-06-11 16:38:00 +00:00
|
|
|
const wxString& aName, const wxString& aCurrentKey, const bool aValidKey,
|
|
|
|
const wxString& aValidMessage )
|
|
|
|
: DIALOG_SHIM( aParent, aId, aTitle, wxDefaultPosition, wxDefaultSize )
|
2016-01-16 01:57:10 +00:00
|
|
|
{
|
|
|
|
wxPanel* panel = new wxPanel( this, wxID_ANY, wxDefaultPosition, wxDefaultSize );
|
|
|
|
wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
|
|
|
|
|
2016-01-16 02:06:25 +00:00
|
|
|
/* Dialog layout:
|
2019-06-11 16:38:00 +00:00
|
|
|
*
|
|
|
|
* valid_img valid_label...........
|
|
|
|
* ----------------------------------
|
2016-01-16 02:06:25 +00:00
|
|
|
*
|
|
|
|
* inst_label........................
|
|
|
|
* ----------------------------------
|
|
|
|
*
|
|
|
|
* cmd_label_0 cmd_label_1 \
|
|
|
|
* | fgsizer
|
|
|
|
* key_label_0 key_label_1 /
|
|
|
|
*/
|
2016-01-16 01:58:23 +00:00
|
|
|
|
2019-06-11 16:38:00 +00:00
|
|
|
// If there is a validity error, display the error message to the user
|
|
|
|
wxBoxSizer* valid_sizer = new wxBoxSizer( wxHORIZONTAL );
|
|
|
|
if( !aValidKey )
|
|
|
|
{
|
|
|
|
wxStaticBitmap* valid_img = new wxStaticBitmap(
|
|
|
|
panel, wxID_ANY, KiBitmap( cancel_xpm ), wxDefaultPosition, wxDefaultSize, 0 );
|
|
|
|
valid_sizer->Add( valid_img, 0, wxALL, 5 );
|
|
|
|
|
|
|
|
wxStaticText* valid_label = new wxStaticText( panel, wxID_ANY, wxEmptyString,
|
|
|
|
wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER_HORIZONTAL );
|
|
|
|
|
|
|
|
valid_label->SetLabelText( aValidMessage );
|
|
|
|
valid_sizer->Add( valid_label, 0, wxALL, 5 );
|
|
|
|
|
|
|
|
// Add the validity text to the main sizer
|
|
|
|
sizer->Add( valid_sizer, 0, wxALL, 5 );
|
|
|
|
sizer->Add( new wxStaticLine( panel ), 0, wxALL | wxEXPAND, 2 );
|
|
|
|
}
|
|
|
|
|
2016-01-16 01:57:10 +00:00
|
|
|
wxStaticText* inst_label = new wxStaticText( panel, wxID_ANY, wxEmptyString,
|
|
|
|
wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL );
|
2016-01-16 02:06:25 +00:00
|
|
|
|
2016-01-16 01:58:23 +00:00
|
|
|
inst_label->SetLabelText( _( "Press a new hotkey, or press Esc to cancel..." ) );
|
|
|
|
sizer->Add( inst_label, 0, wxALL, 5 );
|
2016-01-16 01:57:10 +00:00
|
|
|
|
2016-01-16 01:58:23 +00:00
|
|
|
sizer->Add( new wxStaticLine( panel ), 0, wxALL | wxEXPAND, 2 );
|
2016-01-16 01:57:10 +00:00
|
|
|
|
2016-01-16 01:58:23 +00:00
|
|
|
wxFlexGridSizer* fgsizer = new wxFlexGridSizer( 2 );
|
|
|
|
|
|
|
|
wxStaticText* cmd_label_0 = new wxStaticText( panel, wxID_ANY, _( "Command:" ) );
|
|
|
|
fgsizer->Add( cmd_label_0, 0, wxALL | wxALIGN_CENTRE_VERTICAL, 5 );
|
|
|
|
|
2016-01-16 02:07:35 +00:00
|
|
|
wxStaticText* cmd_label_1 = new wxStaticText( panel, wxID_ANY, wxEmptyString );
|
2016-01-16 02:07:42 +00:00
|
|
|
cmd_label_1->SetFont( cmd_label_1->GetFont().Bold() );
|
2016-01-16 02:07:35 +00:00
|
|
|
cmd_label_1->SetLabel( aName );
|
2016-01-16 01:58:23 +00:00
|
|
|
fgsizer->Add( cmd_label_1, 0, wxALL | wxALIGN_CENTRE_VERTICAL, 5 );
|
|
|
|
|
|
|
|
wxStaticText* key_label_0 = new wxStaticText( panel, wxID_ANY, _( "Current key:" ) );
|
|
|
|
fgsizer->Add( key_label_0, 0, wxALL | wxALIGN_CENTRE_VERTICAL, 5 );
|
|
|
|
|
2016-01-16 02:07:35 +00:00
|
|
|
wxStaticText* key_label_1 = new wxStaticText( panel, wxID_ANY, wxEmptyString );
|
2016-01-16 02:07:42 +00:00
|
|
|
key_label_1->SetFont( key_label_1->GetFont().Bold() );
|
2016-01-16 02:07:35 +00:00
|
|
|
key_label_1->SetLabel( aCurrentKey );
|
2016-01-16 01:58:23 +00:00
|
|
|
fgsizer->Add( key_label_1, 0, wxALL | wxALIGN_CENTRE_VERTICAL, 5 );
|
2016-01-16 01:57:10 +00:00
|
|
|
|
2016-01-16 01:58:23 +00:00
|
|
|
sizer->Add( fgsizer, 1, wxEXPAND );
|
2016-01-16 01:57:10 +00:00
|
|
|
|
|
|
|
// Wrap the sizer in a second to give a larger border around the whole dialog
|
|
|
|
wxBoxSizer* outer_sizer = new wxBoxSizer( wxVERTICAL );
|
2016-01-16 01:58:23 +00:00
|
|
|
outer_sizer->Add( sizer, 0, wxALL | wxEXPAND, 10 );
|
2016-01-16 01:57:10 +00:00
|
|
|
panel->SetSizer( outer_sizer );
|
|
|
|
|
|
|
|
Layout();
|
|
|
|
outer_sizer->Fit( this );
|
|
|
|
Center();
|
|
|
|
|
|
|
|
SetMinClientSize( GetClientSize() );
|
|
|
|
|
2016-01-16 02:07:12 +00:00
|
|
|
// Binding both EVT_CHAR and EVT_CHAR_HOOK ensures that all key events,
|
|
|
|
// including specials like Tab and Return, are received, particularly
|
|
|
|
// on MSW.
|
2016-01-16 01:57:10 +00:00
|
|
|
panel->Bind( wxEVT_CHAR, &HK_PROMPT_DIALOG::OnChar, this );
|
2016-01-16 02:07:12 +00:00
|
|
|
panel->Bind( wxEVT_CHAR_HOOK, &HK_PROMPT_DIALOG::OnCharHook, this );
|
2016-01-16 01:57:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 02:07:12 +00:00
|
|
|
|
|
|
|
void OnCharHook( wxKeyEvent& aEvent )
|
2016-01-16 01:57:10 +00:00
|
|
|
{
|
2016-01-16 02:07:12 +00:00
|
|
|
// On certain platforms, EVT_CHAR_HOOK is the only handler that receives
|
|
|
|
// certain "special" keys. However, it doesn't always receive "normal"
|
|
|
|
// keys correctly. For example, with a US keyboard, it sees ? as shift+/.
|
|
|
|
//
|
|
|
|
// Untangling these incorrect keys would be too much trouble, so we bind
|
|
|
|
// both events, and simply skip the EVT_CHAR_HOOK if it receives a
|
|
|
|
// "normal" key.
|
|
|
|
|
2016-01-16 02:07:28 +00:00
|
|
|
const enum wxKeyCode skipped_keys[] =
|
|
|
|
{
|
|
|
|
WXK_NONE, WXK_SHIFT, WXK_ALT, WXK_CONTROL, WXK_CAPITAL,
|
|
|
|
WXK_NUMLOCK, WXK_SCROLL, WXK_RAW_CONTROL
|
|
|
|
};
|
2016-01-16 02:07:12 +00:00
|
|
|
|
|
|
|
int key = aEvent.GetKeyCode();
|
|
|
|
|
2016-01-16 02:07:28 +00:00
|
|
|
for( size_t i = 0; i < sizeof( skipped_keys ) / sizeof( skipped_keys[0] ); ++i )
|
2016-01-16 02:07:12 +00:00
|
|
|
{
|
|
|
|
if( key == skipped_keys[i] )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-16 02:07:19 +00:00
|
|
|
if( key <= 255 && isprint( key ) && !isspace( key ) )
|
2016-01-16 02:06:59 +00:00
|
|
|
{
|
2016-01-16 02:07:12 +00:00
|
|
|
// Let EVT_CHAR handle this one
|
|
|
|
aEvent.DoAllowNextEvent();
|
2016-01-16 02:07:28 +00:00
|
|
|
|
|
|
|
// On Windows, wxEvent::Skip must NOT be called.
|
|
|
|
// On Linux and OSX, wxEvent::Skip MUST be called.
|
|
|
|
// No, I don't know why.
|
2016-01-16 02:07:19 +00:00
|
|
|
#ifndef __WXMSW__
|
2016-01-16 02:07:12 +00:00
|
|
|
aEvent.Skip();
|
2016-01-16 02:07:19 +00:00
|
|
|
#endif
|
2016-01-16 02:06:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-16 02:07:12 +00:00
|
|
|
OnChar( aEvent );
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 02:07:06 +00:00
|
|
|
|
|
|
|
|
2016-01-16 02:07:12 +00:00
|
|
|
void OnChar( wxKeyEvent& aEvent )
|
|
|
|
{
|
2019-06-11 16:38:00 +00:00
|
|
|
int keyCode = WIDGET_HOTKEY_LIST::MapKeypressToKeycode( aEvent );
|
|
|
|
|
|
|
|
// Test for if the key is valid
|
|
|
|
wxString validMsg;
|
|
|
|
if( HOTKEY_STORE::CheckKeyValidity( keyCode, validMsg ) )
|
|
|
|
{
|
|
|
|
// Valid key, close the window and return
|
|
|
|
m_event = aEvent;
|
|
|
|
EndFlexible( wxID_OK );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Invalid key, tell the user
|
|
|
|
DisplayErrorMessage( this, validMsg, wxEmptyString );
|
|
|
|
}
|
2016-01-16 01:57:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 02:07:12 +00:00
|
|
|
|
2016-01-16 01:57:10 +00:00
|
|
|
/**
|
|
|
|
* End the dialog whether modal or quasimodal
|
|
|
|
*/
|
|
|
|
void EndFlexible( int aRtnCode )
|
|
|
|
{
|
|
|
|
if( IsQuasiModal() )
|
|
|
|
EndQuasiModal( aRtnCode );
|
|
|
|
else
|
|
|
|
EndModal( aRtnCode );
|
|
|
|
}
|
|
|
|
|
2016-01-16 02:07:12 +00:00
|
|
|
|
2016-01-16 01:57:10 +00:00
|
|
|
static wxKeyEvent PromptForKey( wxWindow* aParent, const wxString& aName,
|
2019-06-11 16:38:00 +00:00
|
|
|
const wxString& aCurrentKey, const wxString& aValidMessage, const bool aValidKey )
|
2016-01-16 01:57:10 +00:00
|
|
|
{
|
2019-06-11 16:38:00 +00:00
|
|
|
HK_PROMPT_DIALOG dialog( aParent, wxID_ANY, _( "Set Hotkey" ), aName, aCurrentKey,
|
|
|
|
aValidKey, aValidMessage );
|
2016-01-16 02:06:25 +00:00
|
|
|
|
2016-01-16 01:57:10 +00:00
|
|
|
if( dialog.ShowModal() == wxID_OK )
|
|
|
|
{
|
|
|
|
return dialog.m_event;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxKeyEvent dummy;
|
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-08-01 09:15:05 +00:00
|
|
|
/**
|
|
|
|
* Class HOTKEY_FILTER
|
|
|
|
*
|
|
|
|
* Class to manage logic for filtering hotkeys based on user input
|
|
|
|
*/
|
|
|
|
class HOTKEY_FILTER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HOTKEY_FILTER( const wxString& aFilterStr )
|
|
|
|
{
|
|
|
|
m_normalised_filter_str = aFilterStr.Upper();
|
|
|
|
m_valid = m_normalised_filter_str.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method FilterMatches
|
|
|
|
*
|
|
|
|
* Checks if the filter matches the given hotkey
|
|
|
|
*
|
|
|
|
* @return true on match (or if filter is disabled)
|
|
|
|
*/
|
|
|
|
bool FilterMatches( const EDA_HOTKEY& aHotkey ) const
|
|
|
|
{
|
|
|
|
if( !m_valid )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Match in the (translated) filter string
|
|
|
|
const auto normedInfo = wxGetTranslation( aHotkey.m_InfoMsg ).Upper();
|
|
|
|
if( normedInfo.Contains( m_normalised_filter_str ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const wxString keyName = KeyNameFromKeyCode( aHotkey.m_KeyCode );
|
|
|
|
if( keyName.Upper().Contains( m_normalised_filter_str ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool m_valid;
|
|
|
|
wxString m_normalised_filter_str;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-16 01:56:57 +00:00
|
|
|
WIDGET_HOTKEY_CLIENT_DATA* WIDGET_HOTKEY_LIST::GetHKClientData( wxTreeListItem aItem )
|
|
|
|
{
|
|
|
|
if( aItem.IsOk() )
|
|
|
|
{
|
|
|
|
wxClientData* data = GetItemData( aItem );
|
2016-01-16 02:06:25 +00:00
|
|
|
|
2016-01-16 01:56:57 +00:00
|
|
|
if( !data )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return static_cast<WIDGET_HOTKEY_CLIENT_DATA*>( data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WIDGET_HOTKEY_CLIENT_DATA* WIDGET_HOTKEY_LIST::GetSelHKClientData()
|
|
|
|
{
|
|
|
|
return GetHKClientData( GetSelection() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-27 13:44:03 +00:00
|
|
|
WIDGET_HOTKEY_CLIENT_DATA* WIDGET_HOTKEY_LIST::getExpectedHkClientData( wxTreeListItem aItem )
|
|
|
|
{
|
|
|
|
const auto hkdata = GetHKClientData( aItem );
|
|
|
|
|
|
|
|
// This probably means a hotkey-only action is being attempted on
|
|
|
|
// a row that is not a hotkey (like a section heading)
|
|
|
|
wxASSERT_MSG( hkdata != nullptr, "No hotkey data found for list item" );
|
|
|
|
|
|
|
|
return hkdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-16 01:56:57 +00:00
|
|
|
void WIDGET_HOTKEY_LIST::UpdateFromClientData()
|
|
|
|
{
|
2019-06-11 16:38:00 +00:00
|
|
|
// Run a validity check on the hotkey store before updating
|
|
|
|
m_hk_store.TestStoreValidity();
|
|
|
|
|
2016-01-16 01:56:57 +00:00
|
|
|
for( wxTreeListItem i = GetFirstItem(); i.IsOk(); i = GetNextItem( i ) )
|
|
|
|
{
|
|
|
|
WIDGET_HOTKEY_CLIENT_DATA* hkdata = GetHKClientData( i );
|
|
|
|
|
|
|
|
if( hkdata )
|
|
|
|
{
|
2018-09-27 11:30:43 +00:00
|
|
|
const auto& changed_hk = hkdata->GetChangedHotkey();
|
|
|
|
const EDA_HOTKEY& hk = changed_hk.GetCurrentValue();
|
|
|
|
|
|
|
|
wxString key_text = KeyNameFromKeyCode( hk.m_KeyCode );
|
|
|
|
|
|
|
|
// mark unsaved changes
|
|
|
|
if( changed_hk.HasUnsavedChange() )
|
|
|
|
key_text += " *";
|
2016-01-16 01:56:57 +00:00
|
|
|
|
|
|
|
SetItemText( i, 0, wxGetTranslation( hk.m_InfoMsg ) );
|
2018-09-27 11:30:43 +00:00
|
|
|
SetItemText( i, 1, key_text);
|
2019-06-11 16:38:00 +00:00
|
|
|
|
|
|
|
// Add the image to the column if the item is invalid
|
|
|
|
if( changed_hk.IsValid() )
|
|
|
|
SetItemImage( i, NO_IMAGE, NO_IMAGE );
|
|
|
|
else
|
|
|
|
SetItemImage( i, 0, NO_IMAGE );
|
2016-01-16 01:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-27 12:31:24 +00:00
|
|
|
|
|
|
|
// Trigger a resize in case column widths have changed
|
|
|
|
wxSizeEvent dummy_evt;
|
|
|
|
TWO_COLUMN_TREE_LIST::OnSize( dummy_evt );
|
2019-06-11 16:38:00 +00:00
|
|
|
|
|
|
|
// Update the panel's error message if it exists
|
|
|
|
if( m_parentPanel )
|
|
|
|
m_parentPanel->UpdateErrorMessage();
|
2018-09-27 12:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WIDGET_HOTKEY_LIST::changeHotkey( CHANGED_HOTKEY& aHotkey, long aKey )
|
|
|
|
{
|
|
|
|
// See if this key code is handled in hotkeys names list
|
|
|
|
bool exists;
|
|
|
|
KeyNameFromKeyCode( aKey, &exists );
|
|
|
|
|
|
|
|
auto& curr_hk = aHotkey.GetCurrentValue();
|
|
|
|
|
|
|
|
if( exists && curr_hk.m_KeyCode != aKey )
|
|
|
|
{
|
|
|
|
const auto& tag = aHotkey.GetSectionTag();
|
|
|
|
bool can_update = ResolveKeyConflicts( aKey, tag );
|
|
|
|
|
|
|
|
if( can_update )
|
|
|
|
{
|
|
|
|
curr_hk.m_KeyCode = aKey;
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 01:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-16 01:58:53 +00:00
|
|
|
void WIDGET_HOTKEY_LIST::EditItem( wxTreeListItem aItem )
|
2016-01-16 01:56:57 +00:00
|
|
|
{
|
2018-09-27 13:44:03 +00:00
|
|
|
WIDGET_HOTKEY_CLIENT_DATA* hkdata = getExpectedHkClientData( aItem );
|
2016-01-16 01:57:10 +00:00
|
|
|
|
|
|
|
if( !hkdata )
|
|
|
|
return;
|
|
|
|
|
2016-01-16 02:06:25 +00:00
|
|
|
wxString name = GetItemText( aItem, 0 );
|
|
|
|
wxString current_key = GetItemText( aItem, 1 );
|
2019-06-11 16:38:00 +00:00
|
|
|
wxString valid_msg;
|
2016-01-16 01:57:10 +00:00
|
|
|
|
2019-06-11 16:38:00 +00:00
|
|
|
bool valid_key = hkdata->GetChangedHotkey().IsValid( valid_msg );
|
|
|
|
wxKeyEvent key_event =
|
|
|
|
HK_PROMPT_DIALOG::PromptForKey( GetParent(), name, current_key, valid_msg, valid_key );
|
2016-01-16 02:06:07 +00:00
|
|
|
long key = MapKeypressToKeycode( key_event );
|
2016-01-16 01:56:57 +00:00
|
|
|
|
2018-09-27 13:44:03 +00:00
|
|
|
if( key )
|
2016-01-16 01:56:57 +00:00
|
|
|
{
|
2018-09-27 12:31:24 +00:00
|
|
|
changeHotkey( hkdata->GetChangedHotkey(), key );
|
2016-01-16 02:06:07 +00:00
|
|
|
UpdateFromClientData();
|
2016-01-16 01:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-16 01:58:53 +00:00
|
|
|
void WIDGET_HOTKEY_LIST::ResetItem( wxTreeListItem aItem )
|
|
|
|
{
|
2018-09-27 13:44:03 +00:00
|
|
|
WIDGET_HOTKEY_CLIENT_DATA* hkdata = getExpectedHkClientData( aItem );
|
|
|
|
|
|
|
|
if( !hkdata )
|
|
|
|
return;
|
2016-01-16 01:58:53 +00:00
|
|
|
|
2018-09-27 12:31:24 +00:00
|
|
|
auto& changed_hk = hkdata->GetChangedHotkey();
|
|
|
|
const auto& orig_hk = changed_hk.GetOriginalValue();
|
|
|
|
|
|
|
|
changeHotkey( changed_hk, orig_hk.m_KeyCode );
|
2016-01-16 01:58:53 +00:00
|
|
|
UpdateFromClientData();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-30 18:51:47 +00:00
|
|
|
void WIDGET_HOTKEY_LIST::ResetItemToDefault( wxTreeListItem aItem )
|
|
|
|
{
|
2018-09-27 13:44:03 +00:00
|
|
|
WIDGET_HOTKEY_CLIENT_DATA* hkdata = getExpectedHkClientData( aItem );
|
|
|
|
|
|
|
|
if( !hkdata )
|
|
|
|
return;
|
2018-09-26 14:26:26 +00:00
|
|
|
|
2018-09-27 12:31:24 +00:00
|
|
|
auto& changed_hk = hkdata->GetChangedHotkey();
|
|
|
|
|
|
|
|
changeHotkey( changed_hk, changed_hk.GetCurrentValue().GetDefaultKeyCode() );
|
2016-03-30 18:51:47 +00:00
|
|
|
UpdateFromClientData();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-16 01:58:53 +00:00
|
|
|
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;
|
|
|
|
|
2018-09-27 13:44:03 +00:00
|
|
|
WIDGET_HOTKEY_CLIENT_DATA* hkdata = GetHKClientData( m_context_menu_item );
|
|
|
|
|
|
|
|
// Some actions only apply if the row is hotkey data
|
|
|
|
if( hkdata )
|
|
|
|
{
|
|
|
|
menu.Append( ID_EDIT, _( "Edit..." ) );
|
|
|
|
menu.Append( ID_RESET, _( "Undo Changes" ) );
|
|
|
|
menu.Append( ID_DEFAULT, _( "Restore Default" ) );
|
|
|
|
menu.Append( wxID_SEPARATOR );
|
|
|
|
}
|
|
|
|
|
2017-09-16 05:05:22 +00:00
|
|
|
menu.Append( ID_RESET_ALL, _( "Undo All Changes" ) );
|
|
|
|
menu.Append( ID_DEFAULT_ALL, _( "Restore All to Default" ) );
|
2016-01-16 01:58:53 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-03-30 18:51:47 +00:00
|
|
|
case ID_DEFAULT:
|
|
|
|
ResetItemToDefault( m_context_menu_item );
|
|
|
|
break;
|
|
|
|
|
2016-01-16 01:58:53 +00:00
|
|
|
case ID_RESET_ALL:
|
2018-09-26 14:26:26 +00:00
|
|
|
ResetAllHotkeys( false );
|
2016-01-16 01:58:53 +00:00
|
|
|
break;
|
|
|
|
|
2016-03-30 18:51:47 +00:00
|
|
|
case ID_DEFAULT_ALL:
|
2018-09-26 14:26:26 +00:00
|
|
|
ResetAllHotkeys( true );
|
2016-03-30 18:51:47 +00:00
|
|
|
break;
|
|
|
|
|
2016-01-16 01:58:53 +00:00
|
|
|
default:
|
|
|
|
wxFAIL_MSG( wxT( "Unknown ID in context menu event" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-16 01:56:57 +00:00
|
|
|
bool WIDGET_HOTKEY_LIST::ResolveKeyConflicts( long aKey, const wxString& aSectionTag )
|
|
|
|
{
|
2018-09-26 14:26:26 +00:00
|
|
|
EDA_HOTKEY* conflicting_key = nullptr;
|
|
|
|
EDA_HOTKEY_CONFIG* conflicting_section = nullptr;
|
2016-01-16 01:56:57 +00:00
|
|
|
|
2018-09-26 14:26:26 +00:00
|
|
|
m_hk_store.CheckKeyConflicts( aKey, aSectionTag, &conflicting_key, &conflicting_section );
|
2016-01-16 01:56:57 +00:00
|
|
|
|
2018-09-26 14:26:26 +00:00
|
|
|
if( conflicting_key != nullptr )
|
2016-01-16 01:56:57 +00:00
|
|
|
{
|
2016-01-16 02:06:25 +00:00
|
|
|
wxString info = wxGetTranslation( conflicting_key->m_InfoMsg );
|
|
|
|
wxString msg = wxString::Format(
|
2017-12-15 11:37:46 +00:00
|
|
|
_( "\"%s\" is already assigned to \"%s\" in section \"%s\". Are you sure you want "
|
2016-01-16 02:06:25 +00:00
|
|
|
"to change its assignment?" ),
|
|
|
|
KeyNameFromKeyCode( aKey ), GetChars( info ),
|
|
|
|
*(conflicting_section->m_Title) );
|
2016-01-16 01:56:57 +00:00
|
|
|
|
|
|
|
wxMessageDialog dlg( GetParent(), msg, _( "Confirm change" ), wxYES_NO | wxNO_DEFAULT );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_YES )
|
|
|
|
{
|
2018-09-26 14:26:26 +00:00
|
|
|
// Reset the other hotkey
|
2016-01-16 01:56:57 +00:00
|
|
|
conflicting_key->m_KeyCode = 0;
|
|
|
|
UpdateFromClientData();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-11 16:38:00 +00:00
|
|
|
WIDGET_HOTKEY_LIST::WIDGET_HOTKEY_LIST(
|
|
|
|
PANEL_HOTKEYS_EDITOR* aParent, HOTKEY_STORE& aHotkeyStore, bool aReadOnly )
|
|
|
|
: TWO_COLUMN_TREE_LIST( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_SINGLE ),
|
|
|
|
m_hk_store( aHotkeyStore ),
|
|
|
|
m_readOnly( aReadOnly ),
|
|
|
|
m_parentPanel( aParent )
|
|
|
|
{
|
|
|
|
initializeElements();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WIDGET_HOTKEY_LIST::WIDGET_HOTKEY_LIST(
|
|
|
|
wxWindow* aParent, HOTKEY_STORE& aHotkeyStore, bool aReadOnly )
|
|
|
|
: TWO_COLUMN_TREE_LIST( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_SINGLE ),
|
|
|
|
m_hk_store( aHotkeyStore ),
|
|
|
|
m_readOnly( aReadOnly ),
|
|
|
|
m_parentPanel( nullptr )
|
|
|
|
{
|
|
|
|
initializeElements();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WIDGET_HOTKEY_LIST::initializeElements()
|
2016-01-16 01:56:57 +00:00
|
|
|
{
|
2018-10-02 21:37:15 +00:00
|
|
|
wxString command_header = _( "Command" );
|
|
|
|
|
|
|
|
if( !m_readOnly )
|
|
|
|
command_header << " " << _( "(double-click to edit)" );
|
|
|
|
|
|
|
|
AppendColumn( command_header );
|
2016-01-16 01:56:57 +00:00
|
|
|
AppendColumn( _( "Hotkey" ) );
|
2017-02-19 02:39:55 +00:00
|
|
|
SetRubberBandColumn( 0 );
|
|
|
|
SetClampedMinWidth( HOTKEY_MIN_WIDTH );
|
2016-01-16 01:56:57 +00:00
|
|
|
|
2019-06-11 16:38:00 +00:00
|
|
|
// Add the image for invalid hotkey
|
|
|
|
m_imgList = new wxImageList();
|
|
|
|
m_imgList->Add( KiBitmap( cancel_xpm ) );
|
|
|
|
AssignImageList( m_imgList );
|
|
|
|
|
2018-10-02 21:37:15 +00:00
|
|
|
if( !m_readOnly )
|
|
|
|
{
|
|
|
|
// The event only apply if the widget is in editable mode
|
|
|
|
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 );
|
|
|
|
}
|
2016-01-16 01:56:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 09:15:05 +00:00
|
|
|
void WIDGET_HOTKEY_LIST::ApplyFilterString( const wxString& aFilterStr )
|
|
|
|
{
|
|
|
|
updateShownItems( aFilterStr );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-26 14:26:26 +00:00
|
|
|
void WIDGET_HOTKEY_LIST::ResetAllHotkeys( bool aResetToDefault )
|
2016-03-29 16:35:24 +00:00
|
|
|
{
|
|
|
|
Freeze();
|
|
|
|
|
2018-09-26 14:26:26 +00:00
|
|
|
// Reset all the hotkeys, not just the ones shown
|
|
|
|
// Should not need to check conflicts, as the state we're about
|
|
|
|
// to set to a should be consistent
|
|
|
|
if( aResetToDefault )
|
2016-03-29 16:35:24 +00:00
|
|
|
{
|
2018-09-26 14:26:26 +00:00
|
|
|
m_hk_store.ResetAllHotkeysToDefault();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_hk_store.ResetAllHotkeysToOriginal();
|
2016-03-29 16:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UpdateFromClientData();
|
|
|
|
Thaw();
|
2019-06-11 16:38:00 +00:00
|
|
|
|
|
|
|
// Update the panel's error message if it exists
|
|
|
|
// Call here again since the freeze/thaw seems to disrupt the update
|
|
|
|
// inside UpdateFromClientData
|
|
|
|
if( m_parentPanel )
|
|
|
|
m_parentPanel->UpdateErrorMessage();
|
2016-03-29 16:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-01 09:15:05 +00:00
|
|
|
bool WIDGET_HOTKEY_LIST::TransferDataToControl()
|
|
|
|
{
|
|
|
|
updateShownItems( "" );
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-26 14:26:26 +00:00
|
|
|
|
|
|
|
|
2018-08-01 09:15:05 +00:00
|
|
|
void WIDGET_HOTKEY_LIST::updateShownItems( const wxString& aFilterStr )
|
2016-01-16 01:56:57 +00:00
|
|
|
{
|
|
|
|
Freeze();
|
|
|
|
DeleteAllItems();
|
|
|
|
|
2018-08-01 09:15:05 +00:00
|
|
|
HOTKEY_FILTER filter( aFilterStr );
|
|
|
|
|
2018-09-26 14:26:26 +00:00
|
|
|
for( auto& section: m_hk_store.GetSections() )
|
2016-01-16 01:56:57 +00:00
|
|
|
{
|
|
|
|
// Create parent tree item
|
2018-09-26 14:26:26 +00:00
|
|
|
wxTreeListItem parent = AppendItem( GetRootItem(), section.m_name );
|
2016-01-16 02:06:25 +00:00
|
|
|
|
2018-09-26 14:26:26 +00:00
|
|
|
for( auto& hotkey: section.m_hotkeys )
|
2016-01-16 01:56:57 +00:00
|
|
|
{
|
2018-08-01 09:15:05 +00:00
|
|
|
if( filter.FilterMatches( hotkey.GetCurrentValue() ) )
|
|
|
|
{
|
|
|
|
wxTreeListItem item = AppendItem( parent, wxEmptyString );
|
|
|
|
SetItemData( item, new WIDGET_HOTKEY_CLIENT_DATA( hotkey ) );
|
|
|
|
}
|
2016-01-16 01:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Expand( parent );
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateFromClientData();
|
|
|
|
Thaw();
|
2019-06-11 16:38:00 +00:00
|
|
|
|
|
|
|
// Update the panel's error message if it exists
|
|
|
|
// Call here again since the freeze/thaw seems to disrupt the update
|
|
|
|
// inside UpdateFromClientData
|
|
|
|
if( m_parentPanel )
|
|
|
|
m_parentPanel->UpdateErrorMessage();
|
2016-01-16 01:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool WIDGET_HOTKEY_LIST::TransferDataFromControl()
|
|
|
|
{
|
2018-09-26 14:26:26 +00:00
|
|
|
m_hk_store.SaveAllHotkeys();
|
2016-01-16 01:56:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-01-16 01:58:31 +00:00
|
|
|
|
2016-01-16 02:06:25 +00:00
|
|
|
|
2016-01-16 01:58:31 +00:00
|
|
|
long WIDGET_HOTKEY_LIST::MapKeypressToKeycode( const wxKeyEvent& aEvent )
|
|
|
|
{
|
|
|
|
long key = aEvent.GetKeyCode();
|
|
|
|
|
|
|
|
if( key == WXK_ESCAPE )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-16 02:06:25 +00:00
|
|
|
if( key >= 'a' && key <= 'z' ) // convert to uppercase
|
2016-01-16 01:58:31 +00:00
|
|
|
key = key + ('A' - 'a');
|
|
|
|
|
|
|
|
// Remap Ctrl A (=1+GR_KB_CTRL) to Ctrl Z(=26+GR_KB_CTRL)
|
|
|
|
// to GR_KB_CTRL+'A' .. GR_KB_CTRL+'Z'
|
|
|
|
if( aEvent.ControlDown() && key >= WXK_CONTROL_A && key <= WXK_CONTROL_Z )
|
|
|
|
key += 'A' - 1;
|
|
|
|
|
|
|
|
/* Disallow shift for keys that have two keycodes on them (e.g. number and
|
2016-01-16 02:06:25 +00:00
|
|
|
* punctuation keys) leaving only the "letter keys" of A-Z.
|
|
|
|
* Then, you can have, e.g. Ctrl-5 and Ctrl-% (GB layout)
|
|
|
|
* and Ctrl-( and Ctrl-5 (FR layout).
|
|
|
|
* Otherwise, you'd have to have to say Ctrl-Shift-5 on a FR layout
|
|
|
|
*/
|
2016-01-16 01:58:31 +00:00
|
|
|
bool keyIsLetter = key >= 'A' && key <= 'Z';
|
|
|
|
|
|
|
|
if( aEvent.ShiftDown() && ( keyIsLetter || key > 256 ) )
|
|
|
|
key |= GR_KB_SHIFT;
|
|
|
|
|
|
|
|
if( aEvent.ControlDown() )
|
|
|
|
key |= GR_KB_CTRL;
|
|
|
|
|
|
|
|
if( aEvent.AltDown() )
|
|
|
|
key |= GR_KB_ALT;
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|