KiCad PCB EDA Suite
hotkeys_basic.cpp File Reference
#include <fctsys.h>
#include <kiface_i.h>
#include <hotkeys_basic.h>
#include <id.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <eda_base_frame.h>
#include <macros.h>
#include <menus_helpers.h>
#include <eda_draw_frame.h>
#include <settings/settings_manager.h>
#include <tool/tool_manager.h>
#include "dialogs/dialog_hotkey_list.h"
#include <wx/apptrait.h>
#include <wx/stdpaths.h>
#include <wx/tokenzr.h>
#include <tool/tool_action.h>

Go to the source code of this file.

Classes

struct  hotkey_name_descr
 

Macros

#define KEY_NON_FOUND   -1
 
#define MODIFIER_CTRL   wxT( "Ctrl+" )
 
#define MODIFIER_CMD_MAC   wxT( "Cmd+" )
 
#define MODIFIER_CTRL_BASE   wxT( "Ctrl+" )
 
#define MODIFIER_ALT   wxT( "Alt+" )
 
#define MODIFIER_SHIFT   wxT( "Shift+" )
 

Functions

wxString KeyNameFromKeyCode (int aKeycode, bool *aIsFound)
 Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are handled for function key ( see hotkeyNameList[] ) More...
 
wxString AddHotkeyName (const wxString &aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle)
 AddHotkeyName. More...
 
int KeyCodeFromKeyName (const wxString &keyname)
 Function KeyCodeFromKeyName return the key code from its user-friendly key name (ie: "Ctrl+M") More...
 
void DisplayHotkeyList (EDA_BASE_FRAME *aParent, TOOL_MANAGER *aToolManager)
 Function DisplayHotkeyList Displays the current hotkey list. More...
 
void ReadHotKeyConfig (wxString fileName, std::map< std::string, int > &aHotKeys)
 Function ReadotKeyConfig Reads a hotkey config file into a map. More...
 
int WriteHotKeyConfig (const std::map< std::string, TOOL_ACTION * > &aActionMap)
 Function WriteHotKeyConfig Updates the hotkeys config file with the hotkeys from the given actions map. More...
 
int ReadLegacyHotkeyConfig (const wxString &aAppname, std::map< std::string, int > &aMap)
 Function ReadLegacyHotkeyConfig Read configuration data and fill the current hotkey list with hotkeys. More...
 
int ReadLegacyHotkeyConfigFile (const wxString &aFilename, std::map< std::string, int > &aMap)
 Function ReadLegacyHotkeyConfigFile Read hotkey configuration for a given app, possibly before the frame for that app has been created. More...
 

Variables

static struct hotkey_name_descr hotkeyNameList []
 

Macro Definition Documentation

◆ KEY_NON_FOUND

#define KEY_NON_FOUND   -1

Definition at line 64 of file hotkeys_basic.cpp.

◆ MODIFIER_ALT

#define MODIFIER_ALT   wxT( "Alt+" )

Definition at line 126 of file hotkeys_basic.cpp.

◆ MODIFIER_CMD_MAC

#define MODIFIER_CMD_MAC   wxT( "Cmd+" )

Definition at line 124 of file hotkeys_basic.cpp.

◆ MODIFIER_CTRL

#define MODIFIER_CTRL   wxT( "Ctrl+" )

Definition at line 122 of file hotkeys_basic.cpp.

◆ MODIFIER_CTRL_BASE

#define MODIFIER_CTRL_BASE   wxT( "Ctrl+" )

Definition at line 125 of file hotkeys_basic.cpp.

◆ MODIFIER_SHIFT

#define MODIFIER_SHIFT   wxT( "Shift+" )

Definition at line 127 of file hotkeys_basic.cpp.

Function Documentation

◆ AddHotkeyName()

wxString AddHotkeyName ( const wxString &  aText,
int  aHotKey,
HOTKEY_ACTION_TYPE  aStyle 
)

AddHotkeyName.

Parameters
aText- the base text on which to append the hotkey
aHotKey- the hotkey keycode
aStyle- IS_HOTKEY to add <tab><keyname> (shortcuts in menus, same as hotkeys) IS_COMMENT to add <spaces><(keyname)> mainly in tool tips

Definition at line 196 of file hotkeys_basic.cpp.

197 {
198  wxString msg = aText;
199  wxString keyname = KeyNameFromKeyCode( aHotKey );
200 
201  if( !keyname.IsEmpty() )
202  {
203  switch( aStyle )
204  {
205  case IS_HOTKEY:
206  {
207  // Don't add a suffix for unassigned hotkeys:
208  // WX spews debug from wxAcceleratorEntry::ParseAccel if it doesn't
209  // recognise the keyname, which is the case for <unassigned>.
210  if( aHotKey != 0 )
211  {
212  msg << wxT( "\t" ) << keyname;
213  }
214  break;
215  }
216  case IS_COMMENT:
217  {
218  msg << wxT( " (" ) << keyname << wxT( ")" );
219  break;
220  }
221  }
222  }
223 
224 #ifdef USING_MAC_CMD
225  // On OSX, the modifier equivalent to the Ctrl key of PCs
226  // is the Cmd key, but in code we should use Ctrl as prefix in menus
227  msg.Replace( MODIFIER_CMD_MAC, MODIFIER_CTRL_BASE );
228 #endif
229 
230  return msg;
231 }
#define MODIFIER_CTRL_BASE
#define MODIFIER_CMD_MAC
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are ...

References IS_COMMENT, IS_HOTKEY, KeyNameFromKeyCode(), MODIFIER_CMD_MAC, and MODIFIER_CTRL_BASE.

Referenced by TOOL_ACTION::GetMenuItem(), and PCB_LAYER_BOX_SELECTOR::Resync().

◆ DisplayHotkeyList()

void DisplayHotkeyList ( EDA_BASE_FRAME aFrame,
TOOL_MANAGER aToolMgr 
)

Function DisplayHotkeyList Displays the current hotkey list.

Parameters
aFrame= current active frame
aToolMgr= the tool manager holding the registered actions from which the hotkeys will be harvested

Definition at line 305 of file hotkeys_basic.cpp.

306 {
307  DIALOG_LIST_HOTKEYS dlg( aParent, aToolManager );
308  dlg.ShowModal();
309 }
A dialog that presents the user with a read-only list of hotkeys and their current bindings.

Referenced by COMMON_CONTROL::ListHotKeys().

◆ KeyCodeFromKeyName()

int KeyCodeFromKeyName ( const wxString &  keyname)

Function KeyCodeFromKeyName return the key code from its user-friendly key name (ie: "Ctrl+M")

Definition at line 238 of file hotkeys_basic.cpp.

239 {
240  int ii, keycode = KEY_NON_FOUND;
241 
242  // Search for modifiers: Ctrl+ Alt+ and Shift+
243  // Note: on Mac OSX, the Cmd key is equiv here to Ctrl
244  wxString key = keyname;
245  wxString prefix;
246  int modifier = 0;
247 
248  while( true )
249  {
250  prefix.Empty();
251 
252  if( key.StartsWith( MODIFIER_CTRL_BASE ) )
253  {
254  modifier |= MD_CTRL;
255  prefix = MODIFIER_CTRL_BASE;
256  }
257  else if( key.StartsWith( MODIFIER_CMD_MAC ) )
258  {
259  modifier |= MD_CTRL;
260  prefix = MODIFIER_CMD_MAC;
261  }
262  else if( key.StartsWith( MODIFIER_ALT ) )
263  {
264  modifier |= MD_ALT;
265  prefix = MODIFIER_ALT;
266  }
267  else if( key.StartsWith( MODIFIER_SHIFT ) )
268  {
269  modifier |= MD_SHIFT;
270  prefix = MODIFIER_SHIFT;
271  }
272  else
273  {
274  break;
275  }
276 
277  if( !prefix.IsEmpty() )
278  key.Remove( 0, prefix.Len() );
279  }
280 
281  if( (key.length() == 1) && (key[0] > ' ') && (key[0] < 0x7F) )
282  {
283  keycode = key[0];
284  keycode += modifier;
285  return keycode;
286  }
287 
288  for( ii = 0; hotkeyNameList[ii].m_KeyCode != KEY_NON_FOUND; ii++ )
289  {
290  if( key.CmpNoCase( hotkeyNameList[ii].m_Name ) == 0 )
291  {
292  keycode = hotkeyNameList[ii].m_KeyCode + modifier;
293  break;
294  }
295  }
296 
297  return keycode;
298 }
const wxChar * m_Name
#define MODIFIER_CTRL_BASE
#define KEY_NON_FOUND
#define MODIFIER_SHIFT
#define MODIFIER_ALT
#define MODIFIER_CMD_MAC
static struct hotkey_name_descr hotkeyNameList[]

References hotkeyNameList, KEY_NON_FOUND, hotkey_name_descr::m_KeyCode, hotkey_name_descr::m_Name, MD_ALT, MD_CTRL, MD_SHIFT, MODIFIER_ALT, MODIFIER_CMD_MAC, MODIFIER_CTRL_BASE, and MODIFIER_SHIFT.

Referenced by ReadHotKeyConfig(), and ReadLegacyHotkeyConfigFile().

◆ KeyNameFromKeyCode()

wxString KeyNameFromKeyCode ( int  aKeycode,
bool *  aIsFound 
)

Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are handled for function key ( see hotkeyNameList[] )

Function KeyNameFromKeyCode return the user-friendly key name (ie: "Ctrl+M") from the key code.

Parameters
aKeycode= key code (ascii value, or wxWidgets value for function keys)
aIsFound= a pointer to a bool to return true if found, or false. an be nullptr default)
Returns
the key name in a wxString

Definition at line 139 of file hotkeys_basic.cpp.

140 {
141  wxString keyname, modifier, fullkeyname;
142  int ii;
143  bool found = false;
144 
145  // Assume keycode of 0 is "unassigned"
146  if( (aKeycode & MD_CTRL) != 0 )
147  modifier << MODIFIER_CTRL;
148 
149  if( (aKeycode & MD_ALT) != 0 )
150  modifier << MODIFIER_ALT;
151 
152  if( (aKeycode & MD_SHIFT) != 0 )
153  modifier << MODIFIER_SHIFT;
154 
155  aKeycode &= ~( MD_CTRL | MD_ALT | MD_SHIFT );
156 
157  if( (aKeycode > ' ') && (aKeycode < 0x7F ) )
158  {
159  found = true;
160  keyname.Append( (wxChar)aKeycode );
161  }
162  else
163  {
164  for( ii = 0; ; ii++ )
165  {
166  if( hotkeyNameList[ii].m_KeyCode == KEY_NON_FOUND ) // End of list
167  {
168  keyname = wxT( "<unknown>" );
169  break;
170  }
171 
172  if( hotkeyNameList[ii].m_KeyCode == aKeycode )
173  {
174  keyname = hotkeyNameList[ii].m_Name;
175  found = true;
176  break;
177  }
178  }
179  }
180 
181  if( aIsFound )
182  *aIsFound = found;
183 
184  fullkeyname = modifier + keyname;
185  return fullkeyname;
186 }
const wxChar * m_Name
#define KEY_NON_FOUND
#define MODIFIER_CTRL
#define MODIFIER_SHIFT
#define MODIFIER_ALT
static struct hotkey_name_descr hotkeyNameList[]

References hotkeyNameList, KEY_NON_FOUND, hotkey_name_descr::m_KeyCode, hotkey_name_descr::m_Name, MD_ALT, MD_CTRL, MD_SHIFT, MODIFIER_ALT, MODIFIER_CTRL, and MODIFIER_SHIFT.

Referenced by AddHotkeyName(), WIDGET_HOTKEY_LIST::changeHotkey(), PAD_TOOL::EditPad(), HOTKEY_FILTER::FilterMatches(), TOOL_ACTION::GetDescription(), WIDGET_HOTKEY_LIST::ResolveKeyConflicts(), ACTION_MANAGER::RunHotKey(), DIALOG_GRID_SETTINGS::TransferDataToWindow(), WIDGET_HOTKEY_LIST::UpdateFromClientData(), and WriteHotKeyConfig().

◆ ReadHotKeyConfig()

void ReadHotKeyConfig ( wxString  aFileName,
std::map< std::string, int > &  aHotKeys 
)

Function ReadotKeyConfig Reads a hotkey config file into a map.

If aFileName is empty it will read in the defualt hotkeys file.

Parameters
aHotKeys

Definition at line 312 of file hotkeys_basic.cpp.

313 {
314  if( fileName.IsEmpty() )
315  {
316  wxFileName fn( "user" );
317  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
319  fileName = fn.GetFullPath();
320  }
321 
322  if( !wxFile::Exists( fileName ) )
323  return;
324 
325  wxFile file( fileName, wxFile::OpenMode::read );
326 
327  if( !file.IsOpened() ) // There is a problem to open file
328  return;
329 
330  wxString input;
331  file.ReadAll( &input );
332  input.Replace( "\r\n", "\n" ); // Convert Windows files to Unix line-ends
333  wxStringTokenizer fileTokenizer( input, "\n", wxTOKEN_STRTOK );
334 
335  while( fileTokenizer.HasMoreTokens() )
336  {
337  wxStringTokenizer lineTokenizer( fileTokenizer.GetNextToken(), "\t" );
338 
339  wxString cmdName = lineTokenizer.GetNextToken();
340  wxString keyName = lineTokenizer.GetNextToken();
341 
342  if( !cmdName.IsEmpty() )
343  aHotKeys[ cmdName.ToStdString() ] = KeyCodeFromKeyName( keyName );
344  }
345 }
int KeyCodeFromKeyName(const wxString &keyname)
Function KeyCodeFromKeyName return the key code from its user-friendly key name (ie: "Ctrl+M")
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30

References DEFAULT_HOTKEY_FILENAME_EXT, SETTINGS_MANAGER::GetUserSettingsPath(), and KeyCodeFromKeyName().

Referenced by PANEL_HOTKEYS_EDITOR::ImportHotKeys(), ACTION_MANAGER::UpdateHotKeys(), and WriteHotKeyConfig().

◆ ReadLegacyHotkeyConfig()

int ReadLegacyHotkeyConfig ( const wxString &  aAppname,
std::map< std::string, int > &  aMap 
)

Function ReadLegacyHotkeyConfig Read configuration data and fill the current hotkey list with hotkeys.

Parameters
aAppname= the value of the app's m_FrameName
aMapThe list of keycodes mapped by legacy property names

Definition at line 376 of file hotkeys_basic.cpp.

377 {
378  // For Eeschema and Pcbnew frames, we read the new combined file.
379  // For other kifaces, we read the frame-based file
380  if( aAppname == LIB_EDIT_FRAME_NAME || aAppname == SCH_EDIT_FRAME_NAME )
381  {
383  }
384  else if( aAppname == PCB_EDIT_FRAME_NAME || aAppname == FOOTPRINT_EDIT_FRAME_NAME )
385  {
387  }
388 
389  return ReadLegacyHotkeyConfigFile( aAppname, aMap );
390 }
int ReadLegacyHotkeyConfigFile(const wxString &aFilename, std::map< std::string, int > &aMap)
Function ReadLegacyHotkeyConfigFile Read hotkey configuration for a given app, possibly before the fr...
#define LIB_EDIT_FRAME_NAME
#define SCH_EDIT_FRAME_NAME
#define FOOTPRINT_EDIT_FRAME_NAME
#define EESCHEMA_HOTKEY_NAME
Definition: hotkeys_basic.h:31
#define PCB_EDIT_FRAME_NAME
#define PCBNEW_HOTKEY_NAME
Definition: hotkeys_basic.h:32

References EESCHEMA_HOTKEY_NAME, FOOTPRINT_EDIT_FRAME_NAME, LIB_EDIT_FRAME_NAME, PCB_EDIT_FRAME_NAME, PCBNEW_HOTKEY_NAME, ReadLegacyHotkeyConfigFile(), and SCH_EDIT_FRAME_NAME.

Referenced by ACTION_MANAGER::UpdateHotKeys().

◆ ReadLegacyHotkeyConfigFile()

int ReadLegacyHotkeyConfigFile ( const wxString &  aFilename,
std::map< std::string, int > &  aMap 
)

Function ReadLegacyHotkeyConfigFile Read hotkey configuration for a given app, possibly before the frame for that app has been created.

Parameters
aFilename= the filename to save the hotkeys as
aMapThe list of keycodes mapped by legacy property names
Returns
1 on success, 0 on failure

Definition at line 393 of file hotkeys_basic.cpp.

394 {
395  wxFileName fn( aFilename );
396 
397  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
399 
400  if( !wxFile::Exists( fn.GetFullPath() ) )
401  return 0;
402 
403  wxFile cfgfile( fn.GetFullPath() );
404 
405  if( !cfgfile.IsOpened() ) // There is a problem to open file
406  return 0;
407 
408  // get length
409  cfgfile.SeekEnd();
410  wxFileOffset size = cfgfile.Tell();
411  cfgfile.Seek( 0 );
412 
413  // read data
414  std::vector<char> buffer( size );
415  cfgfile.Read( buffer.data(), size );
416  wxString data( buffer.data(), wxConvUTF8, size );
417 
418  // Is this the wxConfig format? If so, remove "Keys=" and parse the newlines.
419  if( data.StartsWith( wxT("Keys="), &data ) )
420  data.Replace( "\\n", "\n", true );
421 
422  // parse
423  wxStringTokenizer tokenizer( data, L"\r\n", wxTOKEN_STRTOK );
424 
425  while( tokenizer.HasMoreTokens() )
426  {
427  wxString line = tokenizer.GetNextToken();
428  wxStringTokenizer lineTokenizer( line );
429 
430  wxString line_type = lineTokenizer.GetNextToken();
431 
432  if( line_type[0] == '#' ) // comment
433  continue;
434 
435  if( line_type[0] == '[' ) // tags ignored reading legacy hotkeys
436  continue;
437 
438  if( line_type == wxT( "$Endlist" ) )
439  break;
440 
441  if( line_type != wxT( "shortcut" ) )
442  continue;
443 
444  // Get the key name
445  lineTokenizer.SetString( lineTokenizer.GetString(), L"\"\r\n\t ", wxTOKEN_STRTOK );
446  wxString keyname = lineTokenizer.GetNextToken();
447 
448  wxString remainder = lineTokenizer.GetString();
449 
450  // Get the command name
451  wxString fctname = remainder.AfterFirst( '\"' ).BeforeFirst( '\"' );
452 
453  // Add the pair to the map
454  aMap[ fctname.ToStdString() ] = KeyCodeFromKeyName( keyname );
455  }
456 
457  // cleanup
458  cfgfile.Close();
459  return 1;
460 }
int KeyCodeFromKeyName(const wxString &keyname)
Function KeyCodeFromKeyName return the key code from its user-friendly key name (ie: "Ctrl+M")
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30

References DEFAULT_HOTKEY_FILENAME_EXT, SETTINGS_MANAGER::GetUserSettingsPath(), and KeyCodeFromKeyName().

Referenced by ReadLegacyHotkeyConfig().

◆ WriteHotKeyConfig()

int WriteHotKeyConfig ( const std::map< std::string, TOOL_ACTION * > &  aActionMap)

Function WriteHotKeyConfig Updates the hotkeys config file with the hotkeys from the given actions map.

Definition at line 348 of file hotkeys_basic.cpp.

349 {
350  std::map<std::string, int> hotkeys;
351  wxFileName fn( "user" );
352 
353  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
355 
356  // Read the existing config (all hotkeys)
357  //
358  ReadHotKeyConfig( fn.GetFullPath(), hotkeys );
359 
360  // Overlay the current app's hotkey definitions onto the map
361  //
362  for( const auto& ii : aActionMap )
363  hotkeys[ ii.first ] = ii.second->GetHotKey();
364 
365  // Write entire hotkey set
366  //
367  wxFile file( fn.GetFullPath(), wxFile::OpenMode::write );
368 
369  for( const auto& ii : hotkeys )
370  file.Write( wxString::Format( "%s\t%s\n", ii.first, KeyNameFromKeyCode( ii.second ) ) );
371 
372  return 1;
373 }
void ReadHotKeyConfig(wxString fileName, std::map< std::string, int > &aHotKeys)
Function ReadotKeyConfig Reads a hotkey config file into a map.
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are ...
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30

References DEFAULT_HOTKEY_FILENAME_EXT, Format(), SETTINGS_MANAGER::GetUserSettingsPath(), KeyNameFromKeyCode(), and ReadHotKeyConfig().

Referenced by PANEL_HOTKEYS_EDITOR::TransferDataFromWindow().

Variable Documentation

◆ hotkeyNameList

struct hotkey_name_descr hotkeyNameList[]
static

Definition at line 65 of file hotkeys_basic.cpp.

Referenced by KeyCodeFromKeyName(), and KeyNameFromKeyCode().