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>
38 
39 #include <tool/tool_manager.h>
41 #include <wx/apptrait.h>
42 #include <wx/stdpaths.h>
43 #include <wx/tokenzr.h>
44 #include <tool/tool_action.h>
45 
46 
47 /*
48  * class to handle the printable name and the keycode
49  */
51 {
52  const wxChar* m_Name;
53  int m_KeyCode;
54 };
55 
56 /* table giving the hotkey name from the hotkey code, for special keys
57  * Note : when modifiers (ATL, SHIFT, CTRL) do not modify
58  * the code of the key, do need to enter the modified key code
59  * For instance wxT( "F1" ), WXK_F1 handle F1, AltF1, CtrlF1 ...
60  * Key names are:
61  * "Space","Ctrl+Space","Alt+Space" or
62  * "Alt+A","Ctrl+F1", ...
63  */
64 #define KEY_NON_FOUND -1
66 {
67  { wxT( "F1" ), WXK_F1 },
68  { wxT( "F2" ), WXK_F2 },
69  { wxT( "F3" ), WXK_F3 },
70  { wxT( "F4" ), WXK_F4 },
71  { wxT( "F5" ), WXK_F5 },
72  { wxT( "F6" ), WXK_F6 },
73  { wxT( "F7" ), WXK_F7 },
74  { wxT( "F8" ), WXK_F8 },
75  { wxT( "F9" ), WXK_F9 },
76  { wxT( "F10" ), WXK_F10 },
77  { wxT( "F11" ), WXK_F11 },
78  { wxT( "F12" ), WXK_F12 },
79 
80  { wxT( "Esc" ), WXK_ESCAPE },
81  { wxT( "Del" ), WXK_DELETE },
82  { wxT( "Tab" ), WXK_TAB },
83  { wxT( "Back" ), WXK_BACK },
84  { wxT( "Ins" ), WXK_INSERT },
85 
86  { wxT( "Home" ), WXK_HOME },
87  { wxT( "End" ), WXK_END },
88  { wxT( "PgUp" ), WXK_PAGEUP },
89  { wxT( "PgDn" ), WXK_PAGEDOWN },
90 
91  { wxT( "Up" ), WXK_UP },
92  { wxT( "Down" ), WXK_DOWN },
93  { wxT( "Left" ), WXK_LEFT },
94  { wxT( "Right" ), WXK_RIGHT },
95 
96  { wxT( "Return" ), WXK_RETURN },
97 
98  { wxT( "Space" ), WXK_SPACE },
99 
100  { wxT( "" ), 0 },
101 
102  { wxT( "Click" ), PSEUDO_WXK_CLICK },
103  { wxT( "DblClick" ), PSEUDO_WXK_DBLCLICK },
104  { wxT( "Wheel" ), PSEUDO_WXK_WHEEL },
105 
106  // Do not change this line: end of list
107  { wxT( "" ), KEY_NON_FOUND }
108 };
109 
110 // name of modifier keys.
111 // Note: the Ctrl key is Cmd key on Mac OS X.
112 // However, in wxWidgets defs, the key WXK_CONTROL is the Cmd key,
113 // so the code using WXK_CONTROL should be ok on any system.
114 // (on Mac OS X the actual Ctrl key code is WXK_RAW_CONTROL)
115 #ifdef __WXMAC__
116 #define USING_MAC_CMD
117 #endif
118 
119 #ifdef USING_MAC_CMD
120 #define MODIFIER_CTRL wxT( "Cmd+" )
121 #else
122 #define MODIFIER_CTRL wxT( "Ctrl+" )
123 #endif
124 #define MODIFIER_CMD_MAC wxT( "Cmd+" )
125 #define MODIFIER_CTRL_BASE wxT( "Ctrl+" )
126 #define MODIFIER_ALT wxT( "Alt+" )
127 #define MODIFIER_SHIFT wxT( "Shift+" )
128 
129 
139 wxString KeyNameFromKeyCode( int aKeycode, bool* aIsFound )
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 }
187 
188 
196 wxString AddHotkeyName( const wxString& aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle )
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 }
232 
233 
238 int KeyCodeFromKeyName( const wxString& keyname )
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 }
299 
300 
301 /*
302  * DisplayHotkeyList
303  * Displays the hotkeys registered with the given tool manager.
304  */
305 void DisplayHotkeyList( EDA_BASE_FRAME* aParent, TOOL_MANAGER* aToolManager )
306 {
307  DIALOG_LIST_HOTKEYS dlg( aParent, aToolManager );
308  dlg.ShowModal();
309 }
310 
311 
312 void ReadHotKeyConfig( wxString fileName, std::map<std::string, int>& aHotKeys )
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 }
346 
347 
348 int WriteHotKeyConfig( const std::map<std::string, TOOL_ACTION*>& aActionMap )
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 }
374 
375 
376 int ReadLegacyHotkeyConfig( const wxString& aAppname, std::map<std::string, int>& aMap )
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 }
391 
392 
393 int ReadLegacyHotkeyConfigFile( const wxString& aFilename, std::map<std::string, int>& aMap )
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 }
461 
462 
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
static std::string GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
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
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[]