You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

516 lines
18 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
  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 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright (C) 1992-2021 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. #include <kiface_base.h>
  26. #include <hotkeys_basic.h>
  27. #include <id.h>
  28. #include <string_utils.h>
  29. #include <eda_base_frame.h>
  30. #include <eda_draw_frame.h>
  31. #include <wildcards_and_files_ext.h>
  32. #include <settings/settings_manager.h>
  33. #include <tool/tool_manager.h>
  34. #include "dialogs/dialog_hotkey_list.h"
  35. #include <wx/apptrait.h>
  36. #include <wx/stdpaths.h>
  37. #include <wx/tokenzr.h>
  38. #include <wx/txtstrm.h>
  39. #include <wx/wfstream.h>
  40. #include <tool/tool_action.h>
  41. /*
  42. * class to handle the printable name and the keycode
  43. */
  44. struct hotkey_name_descr
  45. {
  46. const wxChar* m_Name;
  47. int m_KeyCode;
  48. };
  49. /* table giving the hotkey name from the hotkey code, for special keys
  50. * Note : when modifiers (ATL, SHIFT, CTRL) do not modify
  51. * the code of the key, do need to enter the modified key code
  52. * For instance wxT( "F1" ), WXK_F1 handle F1, AltF1, CtrlF1 ...
  53. * Key names are:
  54. * "Space","Ctrl+Space","Alt+Space" or
  55. * "Alt+A","Ctrl+F1", ...
  56. */
  57. #define KEY_NON_FOUND -1
  58. static struct hotkey_name_descr hotkeyNameList[] =
  59. {
  60. { wxT( "F1" ), WXK_F1 },
  61. { wxT( "F2" ), WXK_F2 },
  62. { wxT( "F3" ), WXK_F3 },
  63. { wxT( "F4" ), WXK_F4 },
  64. { wxT( "F5" ), WXK_F5 },
  65. { wxT( "F6" ), WXK_F6 },
  66. { wxT( "F7" ), WXK_F7 },
  67. { wxT( "F8" ), WXK_F8 },
  68. { wxT( "F9" ), WXK_F9 },
  69. { wxT( "F10" ), WXK_F10 },
  70. { wxT( "F11" ), WXK_F11 },
  71. { wxT( "F12" ), WXK_F12 },
  72. { wxT( "F13" ), WXK_F13 },
  73. { wxT( "F14" ), WXK_F14 },
  74. { wxT( "F15" ), WXK_F15 },
  75. { wxT( "F16" ), WXK_F16 },
  76. { wxT( "F17" ), WXK_F17 },
  77. { wxT( "F18" ), WXK_F18 },
  78. { wxT( "F19" ), WXK_F19 },
  79. { wxT( "F20" ), WXK_F20 },
  80. { wxT( "F21" ), WXK_F21 },
  81. { wxT( "F22" ), WXK_F22 },
  82. { wxT( "F23" ), WXK_F23 },
  83. { wxT( "F24" ), WXK_F24 },
  84. { wxT( "Esc" ), WXK_ESCAPE },
  85. { wxT( "Del" ), WXK_DELETE },
  86. { wxT( "Tab" ), WXK_TAB },
  87. { wxT( "Back" ), WXK_BACK },
  88. { wxT( "Ins" ), WXK_INSERT },
  89. { wxT( "Home" ), WXK_HOME },
  90. { wxT( "End" ), WXK_END },
  91. { wxT( "PgUp" ), WXK_PAGEUP },
  92. { wxT( "PgDn" ), WXK_PAGEDOWN },
  93. { wxT( "Up" ), WXK_UP },
  94. { wxT( "Down" ), WXK_DOWN },
  95. { wxT( "Left" ), WXK_LEFT },
  96. { wxT( "Right" ), WXK_RIGHT },
  97. { wxT( "Return" ), WXK_RETURN },
  98. { wxT( "Space" ), WXK_SPACE },
  99. { wxT( "Num Pad 0" ), WXK_NUMPAD0 },
  100. { wxT( "Num Pad 1" ), WXK_NUMPAD1 },
  101. { wxT( "Num Pad 2" ), WXK_NUMPAD2 },
  102. { wxT( "Num Pad 3" ), WXK_NUMPAD3 },
  103. { wxT( "Num Pad 4" ), WXK_NUMPAD4 },
  104. { wxT( "Num Pad 5" ), WXK_NUMPAD5 },
  105. { wxT( "Num Pad 6" ), WXK_NUMPAD6 },
  106. { wxT( "Num Pad 7" ), WXK_NUMPAD7 },
  107. { wxT( "Num Pad 8" ), WXK_NUMPAD8 },
  108. { wxT( "Num Pad 9" ), WXK_NUMPAD9 },
  109. { wxT( "Num Pad +" ), WXK_NUMPAD_ADD },
  110. { wxT( "Num Pad -" ), WXK_NUMPAD_SUBTRACT },
  111. { wxT( "Num Pad *" ), WXK_NUMPAD_MULTIPLY },
  112. { wxT( "Num Pad /" ), WXK_NUMPAD_DIVIDE },
  113. { wxT( "Num Pad ." ), WXK_NUMPAD_SEPARATOR },
  114. { wxT( "Num Pad F1"), WXK_NUMPAD_F1 },
  115. { wxT( "Num Pad F2"), WXK_NUMPAD_F2 },
  116. { wxT( "Num Pad F3"), WXK_NUMPAD_F3 },
  117. { wxT( "Num Pad F4"), WXK_NUMPAD_F4 },
  118. { wxT( "" ), 0 },
  119. { wxT( "Click" ), PSEUDO_WXK_CLICK },
  120. { wxT( "DblClick" ), PSEUDO_WXK_DBLCLICK },
  121. { wxT( "Wheel" ), PSEUDO_WXK_WHEEL },
  122. // Do not change this line: end of list
  123. { wxT( "" ), KEY_NON_FOUND }
  124. };
  125. // name of modifier keys.
  126. // Note: the Ctrl key is Cmd key on Mac OS X.
  127. // However, in wxWidgets defs, the key WXK_CONTROL is the Cmd key,
  128. // so the code using WXK_CONTROL should be ok on any system.
  129. // (on Mac OS X the actual Ctrl key code is WXK_RAW_CONTROL)
  130. #ifdef __WXMAC__
  131. #define USING_MAC_CMD
  132. #endif
  133. #ifdef USING_MAC_CMD
  134. #define MODIFIER_CTRL wxT( "Cmd+" )
  135. #define MODIFIER_ALT wxT( "Option+" )
  136. #else
  137. #define MODIFIER_CTRL wxT( "Ctrl+" )
  138. #define MODIFIER_ALT wxT( "Alt+" )
  139. #endif
  140. #define MODIFIER_CMD_MAC wxT( "Cmd+" )
  141. #define MODIFIER_CTRL_BASE wxT( "Ctrl+" )
  142. #define MODIFIER_SHIFT wxT( "Shift+" )
  143. /**
  144. * Return the key name from the key code.
  145. *
  146. * Only some wxWidgets key values are handled for function key ( see hotkeyNameList[] )
  147. *
  148. * @param aKeycode key code (ASCII value, or wxWidgets value for function keys).
  149. * @param aIsFound a pointer to a bool to return true if found, or false. an be nullptr default).
  150. * @return the key name in a wxString.
  151. */
  152. wxString KeyNameFromKeyCode( int aKeycode, bool* aIsFound )
  153. {
  154. wxString keyname, modifier, fullkeyname;
  155. int ii;
  156. bool found = false;
  157. if( aKeycode == WXK_CONTROL )
  158. return wxString( MODIFIER_CTRL ).BeforeFirst( '+' );
  159. else if( aKeycode == WXK_RAW_CONTROL )
  160. return wxString( MODIFIER_CTRL_BASE ).BeforeFirst( '+' );
  161. else if( aKeycode == WXK_SHIFT )
  162. return wxString( MODIFIER_SHIFT ).BeforeFirst( '+' );
  163. else if( aKeycode == WXK_ALT )
  164. return wxString( MODIFIER_ALT ).BeforeFirst( '+' );
  165. // Assume keycode of 0 is "unassigned"
  166. if( (aKeycode & MD_CTRL) != 0 )
  167. modifier << MODIFIER_CTRL;
  168. if( (aKeycode & MD_ALT) != 0 )
  169. modifier << MODIFIER_ALT;
  170. if( (aKeycode & MD_SHIFT) != 0 )
  171. modifier << MODIFIER_SHIFT;
  172. aKeycode &= ~( MD_CTRL | MD_ALT | MD_SHIFT );
  173. if( (aKeycode > ' ') && (aKeycode < 0x7F ) )
  174. {
  175. found = true;
  176. keyname.Append( (wxChar)aKeycode );
  177. }
  178. else
  179. {
  180. for( ii = 0; ; ii++ )
  181. {
  182. if( hotkeyNameList[ii].m_KeyCode == KEY_NON_FOUND ) // End of list
  183. {
  184. keyname = wxT( "<unknown>" );
  185. break;
  186. }
  187. if( hotkeyNameList[ii].m_KeyCode == aKeycode )
  188. {
  189. keyname = hotkeyNameList[ii].m_Name;
  190. found = true;
  191. break;
  192. }
  193. }
  194. }
  195. if( aIsFound )
  196. *aIsFound = found;
  197. fullkeyname = modifier + keyname;
  198. return fullkeyname;
  199. }
  200. /**
  201. * @param aText the base text on which to append the hotkey.
  202. * @param aHotKey the hotkey keycode.
  203. * @param aStyle IS_HOTKEY to add <tab><keyname> (shortcuts in menus, same as hotkeys).
  204. * IS_COMMENT to add <spaces><(keyname)> mainly in tool tips.
  205. */
  206. wxString AddHotkeyName( const wxString& aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle )
  207. {
  208. wxString msg = aText;
  209. wxString keyname = KeyNameFromKeyCode( aHotKey );
  210. if( !keyname.IsEmpty() )
  211. {
  212. switch( aStyle )
  213. {
  214. case IS_HOTKEY:
  215. {
  216. // Don't add a suffix for unassigned hotkeys:
  217. // WX spews debug from wxAcceleratorEntry::ParseAccel if it doesn't
  218. // recognize the keyname, which is the case for <unassigned>.
  219. if( aHotKey != 0 )
  220. {
  221. msg << wxT( "\t" ) << keyname;
  222. }
  223. break;
  224. }
  225. case IS_COMMENT:
  226. {
  227. msg << wxT( " (" ) << keyname << wxT( ")" );
  228. break;
  229. }
  230. }
  231. }
  232. #ifdef USING_MAC_CMD
  233. // On OSX, the modifier equivalent to the Ctrl key of PCs
  234. // is the Cmd key, but in code we should use Ctrl as prefix in menus
  235. msg.Replace( MODIFIER_CMD_MAC, MODIFIER_CTRL_BASE );
  236. #endif
  237. return msg;
  238. }
  239. /**
  240. * Return the key code from its user-friendly key name (ie: "Ctrl+M").
  241. */
  242. int KeyCodeFromKeyName( const wxString& keyname )
  243. {
  244. int ii, keycode = KEY_NON_FOUND;
  245. // Search for modifiers: Ctrl+ Alt+ and Shift+
  246. // Note: on Mac OSX, the Cmd key is equiv here to Ctrl
  247. wxString key = keyname;
  248. wxString prefix;
  249. int modifier = 0;
  250. while( true )
  251. {
  252. prefix.Empty();
  253. if( key.StartsWith( MODIFIER_CTRL_BASE ) )
  254. {
  255. modifier |= MD_CTRL;
  256. prefix = MODIFIER_CTRL_BASE;
  257. }
  258. else if( key.StartsWith( MODIFIER_CMD_MAC ) )
  259. {
  260. modifier |= MD_CTRL;
  261. prefix = MODIFIER_CMD_MAC;
  262. }
  263. else if( key.StartsWith( MODIFIER_ALT ) )
  264. {
  265. modifier |= MD_ALT;
  266. prefix = MODIFIER_ALT;
  267. }
  268. else if( key.StartsWith( MODIFIER_SHIFT ) )
  269. {
  270. modifier |= MD_SHIFT;
  271. prefix = MODIFIER_SHIFT;
  272. }
  273. else
  274. {
  275. break;
  276. }
  277. if( !prefix.IsEmpty() )
  278. key.Remove( 0, prefix.Len() );
  279. }
  280. if( (key.length() == 1) && (key[0] > ' ') && (key[0] < 0x7F) )
  281. {
  282. keycode = key[0];
  283. keycode += modifier;
  284. return keycode;
  285. }
  286. for( ii = 0; hotkeyNameList[ii].m_KeyCode != KEY_NON_FOUND; ii++ )
  287. {
  288. if( key.CmpNoCase( hotkeyNameList[ii].m_Name ) == 0 )
  289. {
  290. keycode = hotkeyNameList[ii].m_KeyCode + modifier;
  291. break;
  292. }
  293. }
  294. return keycode;
  295. }
  296. /*
  297. * Displays the hotkeys registered with the given tool manager.
  298. */
  299. void DisplayHotkeyList( EDA_BASE_FRAME* aParent )
  300. {
  301. DIALOG_LIST_HOTKEYS dlg( aParent );
  302. dlg.ShowModal();
  303. }
  304. void ReadHotKeyConfig( const wxString& aFileName, std::map<std::string, int>& aHotKeys )
  305. {
  306. wxString fileName = aFileName;
  307. if( fileName.IsEmpty() )
  308. {
  309. wxFileName fn( "user" );
  310. fn.SetExt( HotkeyFileExtension );
  311. fn.SetPath( SETTINGS_MANAGER::GetUserSettingsPath() );
  312. fileName = fn.GetFullPath();
  313. }
  314. if( !wxFile::Exists( fileName ) )
  315. return;
  316. wxFFile file( fileName, "rb" );
  317. if( !file.IsOpened() ) // There is a problem to open file
  318. return;
  319. wxString input;
  320. file.ReadAll( &input );
  321. input.Replace( "\r\n", "\n" ); // Convert Windows files to Unix line-ends
  322. wxStringTokenizer fileTokenizer( input, "\n", wxTOKEN_STRTOK );
  323. while( fileTokenizer.HasMoreTokens() )
  324. {
  325. wxStringTokenizer lineTokenizer( fileTokenizer.GetNextToken(), "\t" );
  326. wxString cmdName = lineTokenizer.GetNextToken();
  327. wxString keyName = lineTokenizer.GetNextToken();
  328. if( !cmdName.IsEmpty() )
  329. aHotKeys[ cmdName.ToStdString() ] = KeyCodeFromKeyName( keyName );
  330. }
  331. }
  332. void ReadHotKeyConfigIntoActions( const wxString& aFileName, std::vector<TOOL_ACTION*>& aActions )
  333. {
  334. std::map<std::string, int> hotkeys;
  335. // Read the existing config (all hotkeys)
  336. ReadHotKeyConfig( aFileName, hotkeys );
  337. // Set each tool action hotkey to the config file hotkey if present
  338. for( TOOL_ACTION* action : aActions )
  339. if( hotkeys.find( action->GetName() ) != hotkeys.end() )
  340. action->SetHotKey( hotkeys[action->GetName()] );
  341. }
  342. int WriteHotKeyConfig( const std::vector<TOOL_ACTION*>& aActions )
  343. {
  344. std::map<std::string, int> hotkeys;
  345. wxFileName fn( "user" );
  346. fn.SetExt( HotkeyFileExtension );
  347. fn.SetPath( SETTINGS_MANAGER::GetUserSettingsPath() );
  348. // Read the existing config (all hotkeys)
  349. ReadHotKeyConfig( fn.GetFullPath(), hotkeys );
  350. // Overlay the current app's hotkey definitions onto the map
  351. for( const TOOL_ACTION* action : aActions )
  352. hotkeys[ action->GetName() ] = action->GetHotKey();
  353. // Write entire hotkey set
  354. wxFFileOutputStream outStream( fn.GetFullPath() );
  355. wxTextOutputStream txtStream( outStream, wxEOL_UNIX );
  356. for( const std::pair<const std::string, int>& entry : hotkeys )
  357. txtStream << entry.first << "\t" << KeyNameFromKeyCode( entry.second ) << endl;
  358. txtStream.Flush();
  359. outStream.Close();
  360. return 1;
  361. }
  362. int ReadLegacyHotkeyConfig( const wxString& aAppname, std::map<std::string, int>& aMap )
  363. {
  364. // For Eeschema and Pcbnew frames, we read the new combined file.
  365. // For other kifaces, we read the frame-based file
  366. if( aAppname == LIB_EDIT_FRAME_NAME || aAppname == SCH_EDIT_FRAME_NAME )
  367. {
  368. return ReadLegacyHotkeyConfigFile( EESCHEMA_HOTKEY_NAME, aMap );
  369. }
  370. else if( aAppname == PCB_EDIT_FRAME_NAME || aAppname == FOOTPRINT_EDIT_FRAME_NAME )
  371. {
  372. return ReadLegacyHotkeyConfigFile( PCBNEW_HOTKEY_NAME, aMap );
  373. }
  374. return ReadLegacyHotkeyConfigFile( aAppname, aMap );
  375. }
  376. int ReadLegacyHotkeyConfigFile( const wxString& aFilename, std::map<std::string, int>& aMap )
  377. {
  378. wxFileName fn( aFilename );
  379. fn.SetExt( HotkeyFileExtension );
  380. fn.SetPath( SETTINGS_MANAGER::GetUserSettingsPath() );
  381. if( !wxFile::Exists( fn.GetFullPath() ) )
  382. return 0;
  383. wxFFile cfgfile( fn.GetFullPath(), "rb" );
  384. if( !cfgfile.IsOpened() ) // There is a problem to open file
  385. return 0;
  386. // get length
  387. wxFileOffset size = cfgfile.Length();
  388. // read data
  389. std::vector<char> buffer( size );
  390. cfgfile.Read( buffer.data(), size );
  391. wxString data( buffer.data(), wxConvUTF8, size );
  392. // Is this the wxConfig format? If so, remove "Keys=" and parse the newlines.
  393. if( data.StartsWith( wxT("Keys="), &data ) )
  394. data.Replace( "\\n", "\n", true );
  395. // parse
  396. wxStringTokenizer tokenizer( data, L"\r\n", wxTOKEN_STRTOK );
  397. while( tokenizer.HasMoreTokens() )
  398. {
  399. wxString line = tokenizer.GetNextToken();
  400. wxStringTokenizer lineTokenizer( line );
  401. wxString line_type = lineTokenizer.GetNextToken();
  402. if( line_type[0] == '#' ) // comment
  403. continue;
  404. if( line_type[0] == '[' ) // tags ignored reading legacy hotkeys
  405. continue;
  406. if( line_type == wxT( "$Endlist" ) )
  407. break;
  408. if( line_type != wxT( "shortcut" ) )
  409. continue;
  410. // Get the key name
  411. lineTokenizer.SetString( lineTokenizer.GetString(), L"\"\r\n\t ", wxTOKEN_STRTOK );
  412. wxString keyname = lineTokenizer.GetNextToken();
  413. wxString remainder = lineTokenizer.GetString();
  414. // Get the command name
  415. wxString fctname = remainder.AfterFirst( '\"' ).BeforeFirst( '\"' );
  416. // Add the pair to the map
  417. aMap[ fctname.ToStdString() ] = KeyCodeFromKeyName( keyname );
  418. }
  419. // cleanup
  420. cfgfile.Close();
  421. return 1;
  422. }