2018-05-14 17:34:18 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
2021-07-28 17:25:40 +00:00
|
|
|
* Copyright (C) 1992-2021 Kicad Developers, see AUTHORS.txt for contributors.
|
2018-05-14 17:34:18 +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 2
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-02-20 18:41:42 +00:00
|
|
|
#include <advanced_config.h>
|
|
|
|
#include <gestfich.h>
|
2019-06-10 15:58:32 +00:00
|
|
|
#include <hotkeys_basic.h>
|
2021-02-20 18:41:42 +00:00
|
|
|
#include <kiway_player.h>
|
|
|
|
#include <locale_io.h>
|
|
|
|
#include <panel_hotkeys_editor.h>
|
2021-07-28 17:25:40 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2021-02-20 18:41:42 +00:00
|
|
|
#include <tool/tool_manager.h>
|
2018-10-01 16:18:29 +00:00
|
|
|
#include <widgets/button_row_panel.h>
|
2018-10-03 10:02:43 +00:00
|
|
|
#include <widgets/ui_common.h>
|
2021-06-03 01:19:20 +00:00
|
|
|
#include <wx/filedlg.h>
|
2021-02-20 18:41:42 +00:00
|
|
|
#include <wx/panel.h>
|
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/srchctrl.h>
|
2019-06-10 15:58:32 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2021-02-20 18:41:42 +00:00
|
|
|
#include <wx/txtstrm.h>
|
|
|
|
#include <wx/wfstream.h>
|
2018-10-01 16:18:29 +00:00
|
|
|
|
|
|
|
static const wxSize default_dialog_size { 500, 350 };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to add a filter box to a panel, with some
|
|
|
|
* sensible defaults for that purpose.
|
|
|
|
*
|
2021-04-05 13:24:57 +00:00
|
|
|
* @param aParent The parent widget/panel
|
2018-10-01 16:18:29 +00:00
|
|
|
* @param aDescriptiveText The text to show when the box is empty.
|
|
|
|
* @return A newly constructed filter box - the caller owns it
|
|
|
|
*/
|
|
|
|
static wxSearchCtrl* CreateTextFilterBox( wxWindow* aParent, const wxString& aDescriptiveText )
|
|
|
|
{
|
2020-04-20 20:49:00 +00:00
|
|
|
wxSearchCtrl* search_widget = new wxSearchCtrl( aParent, wxID_ANY );
|
2018-10-01 16:18:29 +00:00
|
|
|
|
|
|
|
search_widget->ShowSearchButton( false );
|
|
|
|
search_widget->ShowCancelButton( true );
|
|
|
|
|
2020-04-27 16:56:52 +00:00
|
|
|
search_widget->SetDescriptiveText( aDescriptiveText );
|
2018-10-01 16:18:29 +00:00
|
|
|
|
|
|
|
return search_widget;
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2019-06-10 15:58:32 +00:00
|
|
|
PANEL_HOTKEYS_EDITOR::PANEL_HOTKEYS_EDITOR( EDA_BASE_FRAME* aFrame, wxWindow* aWindow,
|
|
|
|
bool aReadOnly ) :
|
2020-07-15 23:08:16 +00:00
|
|
|
RESETTABLE_PANEL( aWindow, wxID_ANY, wxDefaultPosition, default_dialog_size ),
|
2019-06-10 15:58:32 +00:00
|
|
|
m_frame( aFrame ),
|
2018-10-02 21:37:15 +00:00
|
|
|
m_readOnly( aReadOnly ),
|
2019-06-09 21:57:23 +00:00
|
|
|
m_hotkeyStore()
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2018-10-03 10:02:43 +00:00
|
|
|
const auto margin = KIUI::GetStdMargin();
|
2020-04-20 20:49:00 +00:00
|
|
|
wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
|
2018-08-01 09:15:05 +00:00
|
|
|
|
2020-04-20 20:09:14 +00:00
|
|
|
const int side_margins = margin * 2;
|
2020-04-20 20:49:00 +00:00
|
|
|
wxBoxSizer* bMargins = new wxBoxSizer( wxVERTICAL );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2020-04-20 20:49:00 +00:00
|
|
|
wxSearchCtrl* filterSearch = CreateTextFilterBox( this, _( "Type filter text" ) );
|
2020-04-27 16:56:52 +00:00
|
|
|
bMargins->Add( filterSearch, 0, wxALL | wxEXPAND, margin );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2018-10-02 21:37:15 +00:00
|
|
|
m_hotkeyListCtrl = new WIDGET_HOTKEY_LIST( this, m_hotkeyStore, m_readOnly );
|
2018-10-03 10:02:43 +00:00
|
|
|
bMargins->Add( m_hotkeyListCtrl, 1, wxALL | wxEXPAND, margin );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2018-10-02 21:37:15 +00:00
|
|
|
if( !m_readOnly )
|
|
|
|
installButtons( bMargins );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2018-10-01 16:18:29 +00:00
|
|
|
mainSizer->Add( bMargins, 1, wxEXPAND | wxRIGHT | wxLEFT, side_margins );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2020-04-20 20:49:00 +00:00
|
|
|
#ifdef __WXGTK__
|
|
|
|
// Work around a bug that clips the text vertically in the wxSearchCtrl on GTK
|
|
|
|
filterSearch->SetMinSize( wxSize( filterSearch->GetSize().x,
|
2020-04-24 17:57:35 +00:00
|
|
|
int( filterSearch->GetSize().y * 1.6 ) ) );
|
2020-11-16 22:46:39 +00:00
|
|
|
#endif
|
2020-04-20 20:49:00 +00:00
|
|
|
|
2020-11-16 22:46:39 +00:00
|
|
|
SetSizer( mainSizer );
|
2020-04-27 16:56:52 +00:00
|
|
|
Layout();
|
2020-04-20 20:49:00 +00:00
|
|
|
|
2018-10-01 16:18:29 +00:00
|
|
|
// Connect Events
|
2019-06-09 21:57:23 +00:00
|
|
|
filterSearch->Bind( wxEVT_COMMAND_TEXT_UPDATED, &PANEL_HOTKEYS_EDITOR::OnFilterSearch, this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PANEL_HOTKEYS_EDITOR::AddHotKeys( TOOL_MANAGER* aToolMgr )
|
|
|
|
{
|
|
|
|
m_toolManagers.push_back( aToolMgr );
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 23:08:16 +00:00
|
|
|
void PANEL_HOTKEYS_EDITOR::ResetPanel()
|
|
|
|
{
|
|
|
|
m_hotkeyListCtrl->ResetAllHotkeys( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-01 16:18:29 +00:00
|
|
|
void PANEL_HOTKEYS_EDITOR::installButtons( wxSizer* aSizer )
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2021-02-20 18:41:42 +00:00
|
|
|
BUTTON_ROW_PANEL::BTN_DEF_LIST l_btn_defs = {
|
2018-10-01 16:18:29 +00:00
|
|
|
{
|
|
|
|
wxID_RESET,
|
2019-06-14 10:55:54 +00:00
|
|
|
_( "Undo All Changes" ),
|
2018-10-02 12:41:16 +00:00
|
|
|
_( "Undo all changes made so far in this dialog" ),
|
2020-12-10 22:26:35 +00:00
|
|
|
[this]( wxCommandEvent& )
|
|
|
|
{
|
2018-10-01 16:18:29 +00:00
|
|
|
m_hotkeyListCtrl->ResetAllHotkeys( false );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
wxID_ANY,
|
2019-06-09 21:57:23 +00:00
|
|
|
_( "Import Hotkeys..." ),
|
|
|
|
_( "Import hotkey definitions from an external file, replacing the current values" ),
|
2020-12-10 22:26:35 +00:00
|
|
|
[this]( wxCommandEvent& )
|
|
|
|
{
|
2019-06-10 15:58:32 +00:00
|
|
|
ImportHotKeys();
|
2018-10-01 16:18:29 +00:00
|
|
|
}
|
2019-06-09 21:57:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-20 18:41:42 +00:00
|
|
|
|
|
|
|
if( ADVANCED_CFG::GetCfg().m_HotkeysDumper )
|
|
|
|
{
|
|
|
|
// Add hotkeys dumper (does not need translation, it's a dev tool only)
|
|
|
|
l_btn_defs.push_back( {
|
|
|
|
wxID_ANY, wxT( "Dump Hotkeys" ), wxEmptyString,
|
|
|
|
[this]( wxCommandEvent& )
|
|
|
|
{
|
|
|
|
dumpHotkeys();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-06-09 21:57:23 +00:00
|
|
|
const BUTTON_ROW_PANEL::BTN_DEF_LIST r_btn_defs = {
|
2018-10-01 16:18:29 +00:00
|
|
|
};
|
|
|
|
|
2019-02-25 13:31:58 +00:00
|
|
|
auto btnPanel = std::make_unique<BUTTON_ROW_PANEL>( this, l_btn_defs, r_btn_defs );
|
2018-10-01 16:18:29 +00:00
|
|
|
|
2019-02-25 13:31:58 +00:00
|
|
|
aSizer->Add( btnPanel.release(), 0, wxEXPAND | wxTOP, KIUI::GetStdMargin() );
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 16:18:29 +00:00
|
|
|
|
|
|
|
bool PANEL_HOTKEYS_EDITOR::TransferDataToWindow()
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2019-06-14 14:20:25 +00:00
|
|
|
m_hotkeyStore.Init( m_toolManagers, m_readOnly );
|
2018-10-01 16:18:29 +00:00
|
|
|
return m_hotkeyListCtrl->TransferDataToControl();
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-01 16:18:29 +00:00
|
|
|
bool PANEL_HOTKEYS_EDITOR::TransferDataFromWindow()
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2018-10-01 16:18:29 +00:00
|
|
|
if( !m_hotkeyListCtrl->TransferDataFromControl() )
|
|
|
|
return false;
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2019-06-14 14:20:25 +00:00
|
|
|
if( m_readOnly )
|
|
|
|
return true;
|
|
|
|
|
2018-10-01 16:18:29 +00:00
|
|
|
// save the hotkeys
|
2019-06-09 21:57:23 +00:00
|
|
|
for( TOOL_MANAGER* toolMgr : m_toolManagers )
|
|
|
|
WriteHotKeyConfig( toolMgr->GetActions() );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2018-10-01 16:18:29 +00:00
|
|
|
return true;
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
2018-08-01 09:15:05 +00:00
|
|
|
|
2018-10-01 16:18:29 +00:00
|
|
|
|
2018-08-01 09:15:05 +00:00
|
|
|
void PANEL_HOTKEYS_EDITOR::OnFilterSearch( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
const auto searchStr = aEvent.GetString();
|
|
|
|
m_hotkeyListCtrl->ApplyFilterString( searchStr );
|
|
|
|
}
|
2019-06-10 15:58:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
void PANEL_HOTKEYS_EDITOR::ImportHotKeys()
|
|
|
|
{
|
2021-07-28 17:25:40 +00:00
|
|
|
wxString filename = wxFileSelector( _( "Import Hotkeys File:" ), m_frame->GetMruPath(),
|
|
|
|
wxEmptyString, HotkeyFileExtension,
|
|
|
|
HotkeyFileWildcard(), wxFD_OPEN, this );
|
2019-06-10 15:58:32 +00:00
|
|
|
|
|
|
|
if( filename.IsEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::map<std::string, int> importedHotKeys;
|
|
|
|
ReadHotKeyConfig( filename, importedHotKeys );
|
|
|
|
m_frame->SetMruPath( wxFileName( filename ).GetPath() );
|
|
|
|
|
|
|
|
// Overlay the imported hotkeys onto the hotkey store
|
|
|
|
for( HOTKEY_SECTION& section: m_hotkeyStore.GetSections() )
|
|
|
|
{
|
|
|
|
for( HOTKEY& hotkey: section.m_HotKeys )
|
|
|
|
{
|
2019-06-14 10:55:54 +00:00
|
|
|
if( importedHotKeys.count( hotkey.m_Actions[ 0 ]->GetName() ) )
|
|
|
|
hotkey.m_EditKeycode = importedHotKeys[ hotkey.m_Actions[ 0 ]->GetName() ];
|
2019-06-10 15:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-14 18:50:46 +00:00
|
|
|
|
2019-06-10 15:58:32 +00:00
|
|
|
m_hotkeyListCtrl->TransferDataToControl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-20 18:41:42 +00:00
|
|
|
void PANEL_HOTKEYS_EDITOR::dumpHotkeys()
|
|
|
|
{
|
2021-07-28 17:25:40 +00:00
|
|
|
wxString filename = wxFileSelector( wxT( "Hotkeys File" ), m_frame->GetMruPath(),
|
|
|
|
wxEmptyString, TextFileExtension, TextFileWildcard(),
|
|
|
|
wxFD_SAVE, this );
|
2019-06-10 15:58:32 +00:00
|
|
|
|
2021-02-20 18:41:42 +00:00
|
|
|
if( filename.IsEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
wxFileName fn( filename );
|
|
|
|
|
|
|
|
wxFFileOutputStream fileStream( fn.GetFullPath(), "w" );
|
|
|
|
wxTextOutputStream stream( fileStream );
|
|
|
|
|
|
|
|
if( !fn.IsDirWritable() || ( fn.Exists() && !fn.IsFileWritable() ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for( HOTKEY_SECTION& section : m_hotkeyStore.GetSections() )
|
|
|
|
{
|
|
|
|
stream << wxT( "=== " ) << section.m_SectionName << endl << endl;
|
|
|
|
|
|
|
|
stream << wxT( "[width=\"100%\",options=\"header\",cols=\"20%,15%,65%\"]" ) << endl;
|
|
|
|
stream << wxT( "|===" ) << endl;
|
2021-02-20 18:55:10 +00:00
|
|
|
stream << _( "| Action | Default Hotkey | Description" ) << endl;
|
2021-02-20 18:41:42 +00:00
|
|
|
|
|
|
|
for( HOTKEY& hk : section.m_HotKeys )
|
|
|
|
{
|
|
|
|
stream << wxT( "| " ) << hk.m_Actions[0]->GetLabel() << endl;
|
|
|
|
|
|
|
|
if( hk.m_EditKeycode > 0 )
|
2021-06-02 02:46:02 +00:00
|
|
|
{
|
|
|
|
stream << wxT( " | kbd:[" ) << KeyNameFromKeyCode( hk.m_EditKeycode ) << ']'
|
|
|
|
<< endl;
|
|
|
|
}
|
2021-02-20 18:41:42 +00:00
|
|
|
else
|
2021-06-02 02:46:02 +00:00
|
|
|
{
|
2021-02-20 18:41:42 +00:00
|
|
|
stream << wxT( " |" ) << endl;
|
2021-06-02 02:46:02 +00:00
|
|
|
}
|
2021-02-20 18:41:42 +00:00
|
|
|
|
|
|
|
stream << wxT( " | " ) << hk.m_Actions[0]->GetDescription( false ) << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream << wxT( "|===" ) << endl << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.Flush();
|
|
|
|
fileStream.Close();
|
|
|
|
}
|