KiCad PCB EDA Suite
hotkeys_basic.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2010-2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <fctsys.h>
27 #include <kiface_i.h>
28 #include <hotkeys_basic.h>
29 #include <id.h>
30 #include <confirm.h>
31 #include <kicad_string.h>
32 #include <gestfich.h>
33 #include <eda_base_frame.h>
34 #include <macros.h>
35 #include <menus_helpers.h>
36 #include <eda_draw_frame.h>
37 
38 #include <tool/tool_manager.h>
40 #include <wx/apptrait.h>
41 #include <wx/stdpaths.h>
42 #include <wx/tokenzr.h>
43 #include <tool/tool_action.h>
44 
45 
46 /*
47  * class to handle the printable name and the keycode
48  */
50 {
51  const wxChar* m_Name;
52  int m_KeyCode;
53 };
54 
55 /* table giving the hotkey name from the hotkey code, for special keys
56  * Note : when modifiers (ATL, SHIFT, CTRL) do not modify
57  * the code of the key, do need to enter the modified key code
58  * For instance wxT( "F1" ), WXK_F1 handle F1, AltF1, CtrlF1 ...
59  * Key names are:
60  * "Space","Ctrl+Space","Alt+Space" or
61  * "Alt+A","Ctrl+F1", ...
62  */
63 #define KEY_NON_FOUND -1
65 {
66  { wxT( "F1" ), WXK_F1 },
67  { wxT( "F2" ), WXK_F2 },
68  { wxT( "F3" ), WXK_F3 },
69  { wxT( "F4" ), WXK_F4 },
70  { wxT( "F5" ), WXK_F5 },
71  { wxT( "F6" ), WXK_F6 },
72  { wxT( "F7" ), WXK_F7 },
73  { wxT( "F8" ), WXK_F8 },
74  { wxT( "F9" ), WXK_F9 },
75  { wxT( "F10" ), WXK_F10 },
76  { wxT( "F11" ), WXK_F11 },
77  { wxT( "F12" ), WXK_F12 },
78 
79  { wxT( "Esc" ), WXK_ESCAPE },
80  { wxT( "Del" ), WXK_DELETE },
81  { wxT( "Tab" ), WXK_TAB },
82  { wxT( "Back" ), WXK_BACK },
83  { wxT( "Ins" ), WXK_INSERT },
84 
85  { wxT( "Home" ), WXK_HOME },
86  { wxT( "End" ), WXK_END },
87  { wxT( "PgUp" ), WXK_PAGEUP },
88  { wxT( "PgDn" ), WXK_PAGEDOWN },
89 
90  { wxT( "Up" ), WXK_UP },
91  { wxT( "Down" ), WXK_DOWN },
92  { wxT( "Left" ), WXK_LEFT },
93  { wxT( "Right" ), WXK_RIGHT },
94 
95  { wxT( "Return" ), WXK_RETURN },
96 
97  { wxT( "Space" ), WXK_SPACE },
98 
99  { wxT( "" ), 0 },
100 
101  { wxT( "Click" ), PSEUDO_WXK_CLICK },
102  { wxT( "DblClick" ), PSEUDO_WXK_DBLCLICK },
103  { wxT( "Wheel" ), PSEUDO_WXK_WHEEL },
104 
105  // Do not change this line: end of list
106  { wxT( "" ), KEY_NON_FOUND }
107 };
108 
109 // name of modifier keys.
110 // Note: the Ctrl key is Cmd key on Mac OS X.
111 // However, in wxWidgets defs, the key WXK_CONTROL is the Cmd key,
112 // so the code using WXK_CONTROL should be ok on any system.
113 // (on Mac OS X the actual Ctrl key code is WXK_RAW_CONTROL)
114 #ifdef __WXMAC__
115 #define USING_MAC_CMD
116 #endif
117 
118 #ifdef USING_MAC_CMD
119 #define MODIFIER_CTRL wxT( "Cmd+" )
120 #else
121 #define MODIFIER_CTRL wxT( "Ctrl+" )
122 #endif
123 #define MODIFIER_CMD_MAC wxT( "Cmd+" )
124 #define MODIFIER_CTRL_BASE wxT( "Ctrl+" )
125 #define MODIFIER_ALT wxT( "Alt+" )
126 #define MODIFIER_SHIFT wxT( "Shift+" )
127 
128 
138 wxString KeyNameFromKeyCode( int aKeycode, bool* aIsFound )
139 {
140  wxString keyname, modifier, fullkeyname;
141  int ii;
142  bool found = false;
143 
144  // Assume keycode of 0 is "unassigned"
145  if( (aKeycode & MD_CTRL) != 0 )
146  modifier << MODIFIER_CTRL;
147 
148  if( (aKeycode & MD_ALT) != 0 )
149  modifier << MODIFIER_ALT;
150 
151  if( (aKeycode & MD_SHIFT) != 0 )
152  modifier << MODIFIER_SHIFT;
153 
154  aKeycode &= ~( MD_CTRL | MD_ALT | MD_SHIFT );
155 
156  if( (aKeycode > ' ') && (aKeycode < 0x7F ) )
157  {
158  found = true;
159  keyname.Append( (wxChar)aKeycode );
160  }
161  else
162  {
163  for( ii = 0; ; ii++ )
164  {
165  if( hotkeyNameList[ii].m_KeyCode == KEY_NON_FOUND ) // End of list
166  {
167  keyname = wxT( "<unknown>" );
168  break;
169  }
170 
171  if( hotkeyNameList[ii].m_KeyCode == aKeycode )
172  {
173  keyname = hotkeyNameList[ii].m_Name;
174  found = true;
175  break;
176  }
177  }
178  }
179 
180  if( aIsFound )
181  *aIsFound = found;
182 
183  fullkeyname = modifier + keyname;
184  return fullkeyname;
185 }
186 
187 
195 wxString AddHotkeyName( const wxString& aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle )
196 {
197  wxString msg = aText;
198  wxString keyname = KeyNameFromKeyCode( aHotKey );
199 
200  if( !keyname.IsEmpty() )
201  {
202  switch( aStyle )
203  {
204  case IS_HOTKEY:
205  {
206  // Don't add a suffix for unassigned hotkeys:
207  // WX spews debug from wxAcceleratorEntry::ParseAccel if it doesn't
208  // recognise the keyname, which is the case for <unassigned>.
209  if( aHotKey != 0 )
210  {
211  msg << wxT( "\t" ) << keyname;
212  }
213  break;
214  }
215  case IS_COMMENT:
216  {
217  msg << wxT( " (" ) << keyname << wxT( ")" );
218  break;
219  }
220  }
221  }
222 
223 #ifdef USING_MAC_CMD
224  // On OSX, the modifier equivalent to the Ctrl key of PCs
225  // is the Cmd key, but in code we should use Ctrl as prefix in menus
226  msg.Replace( MODIFIER_CMD_MAC, MODIFIER_CTRL_BASE );
227 #endif
228 
229  return msg;
230 }
231 
232 
237 int KeyCodeFromKeyName( const wxString& keyname )
238 {
239  int ii, keycode = KEY_NON_FOUND;
240 
241  // Search for modifiers: Ctrl+ Alt+ and Shift+
242  // Note: on Mac OSX, the Cmd key is equiv here to Ctrl
243  wxString key = keyname;
244  wxString prefix;
245  int modifier = 0;
246 
247  while( true )
248  {
249  prefix.Empty();
250 
251  if( key.StartsWith( MODIFIER_CTRL_BASE ) )
252  {
253  modifier |= MD_CTRL;
254  prefix = MODIFIER_CTRL_BASE;
255  }
256  else if( key.StartsWith( MODIFIER_CMD_MAC ) )
257  {
258  modifier |= MD_CTRL;
259  prefix = MODIFIER_CMD_MAC;
260  }
261  else if( key.StartsWith( MODIFIER_ALT ) )
262  {
263  modifier |= MD_ALT;
264  prefix = MODIFIER_ALT;
265  }
266  else if( key.StartsWith( MODIFIER_SHIFT ) )
267  {
268  modifier |= MD_SHIFT;
269  prefix = MODIFIER_SHIFT;
270  }
271  else
272  {
273  break;
274  }
275 
276  if( !prefix.IsEmpty() )
277  key.Remove( 0, prefix.Len() );
278  }
279 
280  if( (key.length() == 1) && (key[0] > ' ') && (key[0] < 0x7F) )
281  {
282  keycode = key[0];
283  keycode += modifier;
284  return keycode;
285  }
286 
287  for( ii = 0; hotkeyNameList[ii].m_KeyCode != KEY_NON_FOUND; ii++ )
288  {
289  if( key.CmpNoCase( hotkeyNameList[ii].m_Name ) == 0 )
290  {
291  keycode = hotkeyNameList[ii].m_KeyCode + modifier;
292  break;
293  }
294  }
295 
296  return keycode;
297 }
298 
299 
300 /*
301  * DisplayHotkeyList
302  * Displays the hotkeys registered with the given tool manager.
303  */
304 void DisplayHotkeyList( EDA_BASE_FRAME* aParent, TOOL_MANAGER* aToolManager )
305 {
306  DIALOG_LIST_HOTKEYS dlg( aParent, aToolManager );
307  dlg.ShowModal();
308 }
309 
310 
311 void ReadHotKeyConfig( wxString fileName, std::map<std::string, int>& aHotKeys )
312 {
313  if( fileName.IsEmpty() )
314  {
315  wxFileName fn( "user" );
316  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
317  fn.SetPath( GetKicadConfigPath() );
318  fileName = fn.GetFullPath();
319  }
320 
321  if( !wxFile::Exists( fileName ) )
322  return;
323 
324  wxFile file( fileName, wxFile::OpenMode::read );
325 
326  if( !file.IsOpened() ) // There is a problem to open file
327  return;
328 
329  wxString input;
330  file.ReadAll( &input );
331  input.Replace( "\r\n", "\n" ); // Convert Windows files to Unix line-ends
332  wxStringTokenizer fileTokenizer( input, "\n", wxTOKEN_STRTOK );
333 
334  while( fileTokenizer.HasMoreTokens() )
335  {
336  wxStringTokenizer lineTokenizer( fileTokenizer.GetNextToken(), "\t" );
337 
338  wxString cmdName = lineTokenizer.GetNextToken();
339  wxString keyName = lineTokenizer.GetNextToken();
340 
341  if( !cmdName.IsEmpty() )
342  aHotKeys[ cmdName.ToStdString() ] = KeyCodeFromKeyName( keyName );
343  }
344 }
345 
346 
347 int WriteHotKeyConfig( const std::map<std::string, TOOL_ACTION*>& aActionMap )
348 {
349  std::map<std::string, int> hotkeys;
350  wxFileName fn( "user" );
351 
352  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
353  fn.SetPath( GetKicadConfigPath() );
354 
355  // Read the existing config (all hotkeys)
356  //
357  ReadHotKeyConfig( fn.GetFullPath(), hotkeys );
358 
359  // Overlay the current app's hotkey definitions onto the map
360  //
361  for( const auto& ii : aActionMap )
362  hotkeys[ ii.first ] = ii.second->GetHotKey();
363 
364  // Write entire hotkey set
365  //
366  wxFile file( fn.GetFullPath(), wxFile::OpenMode::write );
367 
368  for( const auto& ii : hotkeys )
369  file.Write( wxString::Format( "%s\t%s\n", ii.first, KeyNameFromKeyCode( ii.second ) ) );
370 
371  return 1;
372 }
373 
374 
375 int ReadLegacyHotkeyConfig( const wxString& aAppname, std::map<std::string, int>& aMap )
376 {
377  // For Eeschema and Pcbnew frames, we read the new combined file.
378  // For other kifaces, we read the frame-based file
379  if( aAppname == LIB_EDIT_FRAME_NAME || aAppname == SCH_EDIT_FRAME_NAME )
380  {
382  }
383  else if( aAppname == PCB_EDIT_FRAME_NAME || aAppname == FOOTPRINT_EDIT_FRAME_NAME )
384  {
386  }
387 
388  return ReadLegacyHotkeyConfigFile( aAppname, aMap );
389 }
390 
391 
392 int ReadLegacyHotkeyConfigFile( const wxString& aFilename, std::map<std::string, int>& aMap )
393 {
394  wxFileName fn( aFilename );
395 
396  fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
397  fn.SetPath( GetKicadConfigPath() );
398 
399  if( !wxFile::Exists( fn.GetFullPath() ) )
400  return 0;
401 
402  wxFile cfgfile( fn.GetFullPath() );
403 
404  if( !cfgfile.IsOpened() ) // There is a problem to open file
405  return 0;
406 
407  // get length
408  cfgfile.SeekEnd();
409  wxFileOffset size = cfgfile.Tell();
410  cfgfile.Seek( 0 );
411 
412  // read data
413  std::vector<char> buffer( size );
414  cfgfile.Read( buffer.data(), size );
415  wxString data( buffer.data(), wxConvUTF8, size );
416 
417  // Is this the wxConfig format? If so, remove "Keys=" and parse the newlines.
418  if( data.StartsWith( wxT("Keys="), &data ) )
419  data.Replace( "\\n", "\n", true );
420 
421  // parse
422  wxStringTokenizer tokenizer( data, L"\r\n", wxTOKEN_STRTOK );
423 
424  while( tokenizer.HasMoreTokens() )
425  {
426  wxString line = tokenizer.GetNextToken();
427  wxStringTokenizer lineTokenizer( line );
428 
429  wxString line_type = lineTokenizer.GetNextToken();
430 
431  if( line_type[0] == '#' ) // comment
432  continue;
433 
434  if( line_type[0] == '[' ) // tags ignored reading legacy hotkeys
435  continue;
436 
437  if( line_type == wxT( "$Endlist" ) )
438  break;
439 
440  if( line_type != wxT( "shortcut" ) )
441  continue;
442 
443  // Get the key name
444  lineTokenizer.SetString( lineTokenizer.GetString(), L"\"\r\n\t ", wxTOKEN_STRTOK );
445  wxString keyname = lineTokenizer.GetNextToken();
446 
447  wxString remainder = lineTokenizer.GetString();
448 
449  // Get the command name
450  wxString fctname = remainder.AfterFirst( '\"' ).BeforeFirst( '\"' );
451 
452  // Add the pair to the map
453  aMap[ fctname.ToStdString() ] = KeyCodeFromKeyName( keyname );
454  }
455 
456  // cleanup
457  cfgfile.Close();
458  return 1;
459 }
460 
461 
const wxChar * m_Name
#define MODIFIER_CTRL_BASE
This file is part of the common library TODO brief description.
#define KEY_NON_FOUND
This file is part of the common library.
Hotkey list dialog (as opposed to editor)
A dialog that presents the user with a read-only list of hotkeys and their current bindings.
wxString AddHotkeyName(const wxString &aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle)
AddHotkeyName.
#define PSEUDO_WXK_WHEEL
Definition: hotkeys_basic.h:52
This file contains miscellaneous commonly used macros and functions.
void ReadHotKeyConfig(wxString fileName, std::map< std::string, int > &aHotKeys)
Function ReadotKeyConfig Reads a hotkey config file into a map.
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
TOOL_MANAGER.
Definition: tool_manager.h:50
#define MODIFIER_CTRL
#define SCH_EDIT_FRAME_NAME
int ReadLegacyHotkeyConfig(const wxString &aAppname, std::map< std::string, int > &aMap)
Function ReadLegacyHotkeyConfig Read configuration data and fill the current hotkey list with hotkeys...
void DisplayHotkeyList(EDA_BASE_FRAME *aParent, TOOL_MANAGER *aToolManager)
Function DisplayHotkeyList Displays the current hotkey list.
#define PSEUDO_WXK_CLICK
Definition: hotkeys_basic.h:50
int KeyCodeFromKeyName(const wxString &keyname)
Function KeyCodeFromKeyName return the key code from its user-friendly key name (ie: "Ctrl+M")
#define MODIFIER_SHIFT
Base window classes and related definitions.
#define FOOTPRINT_EDIT_FRAME_NAME
wxString GetKicadConfigPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: common.cpp:242
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
The base frame for deriving all KiCad main window classes.
#define MODIFIER_ALT
#define MODIFIER_CMD_MAC
#define EESCHEMA_HOTKEY_NAME
Definition: hotkeys_basic.h:31
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are ...
#define PCB_EDIT_FRAME_NAME
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30
#define PSEUDO_WXK_DBLCLICK
Definition: hotkeys_basic.h:51
HOTKEY_ACTION_TYPE
An helper enum for AddHotkeyName function In menus we can add a hot key, or an accelerator ,...
Definition: hotkeys_basic.h:76
#define PCBNEW_HOTKEY_NAME
Definition: hotkeys_basic.h:32
int WriteHotKeyConfig(const std::map< std::string, TOOL_ACTION * > &aActionMap)
Function WriteHotKeyConfig Updates the hotkeys config file with the hotkeys from the given actions ma...
static struct hotkey_name_descr hotkeyNameList[]