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.

298 lines
10 KiB

11 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 CERN
  5. * Copyright (C) 2016-2018 KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
  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. * @file pagelayout_editor/hotkeys.cpp
  27. */
  28. #include <fctsys.h>
  29. #include <common.h>
  30. #include <kicad_device_context.h>
  31. #include <id.h>
  32. #include <class_drawpanel.h>
  33. #include <pl_editor_frame.h>
  34. #include <design_tree_frame.h>
  35. #include <worksheet_dataitem.h>
  36. #include <hotkeys.h>
  37. #include <pl_editor_id.h>
  38. // Remark: the hotkey message info is used as keyword in hotkey config files and
  39. // as comments in help windows, therefore translated only when displayed
  40. // they are marked _HKI to be extracted by translation tools
  41. // See hotkeys_basic.h for more info
  42. /* How to add a new hotkey:
  43. * add a new id in the enum hotkey_id_commnand like MY_NEW_ID_FUNCTION.
  44. * add a new EDA_HOTKEY entry like:
  45. * static EDA_HOTKEY HkMyNewEntry(_HKI("Command Label"), MY_NEW_ID_FUNCTION, default key value);
  46. * 'Command Label' is the name used in hotkey list display, and the identifier in the
  47. * hotkey list file
  48. * 'MY_NEW_ID_FUNCTION' is the id event function used in the switch in OnHotKey() function.
  49. * 'Default key value' is the default hotkey for this command.
  50. * Can be overrided by the user hotkey list
  51. * Add the 'HkMyNewEntry' pointer in the s_PlEditor_Hotkey_List list
  52. * Add the new code in the switch in OnHotKey() function.
  53. *
  54. * Note: If an hotkey is a special key, be sure the corresponding wxWidget keycode (WXK_XXXX)
  55. * is handled in the hotkey_name_descr s_Hotkey_Name_List list (see hotkeys_basic.cpp)
  56. * and see this list for some ascii keys (space ...)
  57. */
  58. // Hotkey list:
  59. // mouse click command:
  60. static EDA_HOTKEY HkMouseLeftClick( _HKI( "Mouse Left Click" ), HK_LEFT_CLICK, WXK_RETURN, 0 );
  61. static EDA_HOTKEY HkMouseLeftDClick( _HKI( "Mouse Left Double Click" ), HK_LEFT_DCLICK,
  62. WXK_END, 0 );
  63. static EDA_HOTKEY HkResetLocalCoord( _HKI( "Reset Local Coordinates" ),
  64. HK_RESET_LOCAL_COORD, ' ' );
  65. static EDA_HOTKEY HkZoomAuto( _HKI( "Zoom Auto" ), HK_ZOOM_AUTO, WXK_HOME, ID_ZOOM_PAGE );
  66. static EDA_HOTKEY HkZoomCenter( _HKI( "Zoom Center" ), HK_ZOOM_CENTER, WXK_F4,
  67. ID_POPUP_ZOOM_CENTER );
  68. static EDA_HOTKEY HkZoomRedraw( _HKI( "Zoom Redraw" ), HK_ZOOM_REDRAW, WXK_F3, ID_ZOOM_REDRAW );
  69. static EDA_HOTKEY HkZoomOut( _HKI( "Zoom Out" ), HK_ZOOM_OUT, WXK_F2, ID_KEY_ZOOM_OUT );
  70. static EDA_HOTKEY HkZoomIn( _HKI( "Zoom In" ), HK_ZOOM_IN, WXK_F1, ID_KEY_ZOOM_IN );
  71. static EDA_HOTKEY HkZoomSelection( _HKI( "Zoom to Selection" ), HK_ZOOM_SELECTION,
  72. GR_KB_CTRL + WXK_F5, ID_ZOOM_SELECTION );
  73. static EDA_HOTKEY HkHelp( _HKI( "Help (this window)" ), HK_HELP, GR_KB_CTRL + WXK_F1 );
  74. static EDA_HOTKEY HkMoveItem( _HKI( "Move Item" ), HK_MOVE_ITEM, 'M', ID_POPUP_ITEM_MOVE );
  75. static EDA_HOTKEY HkPlaceItem( _HKI( "Place Item" ), HK_PLACE_ITEM, 'P', ID_POPUP_ITEM_PLACE );
  76. static EDA_HOTKEY HkMoveStartPoint( _HKI( "Move Start Point" ), HK_MOVE_START_POINT, 'S',
  77. ID_POPUP_ITEM_MOVE_START_POINT );
  78. static EDA_HOTKEY HkMoveEndPoint( _HKI( "Move End Point" ), HK_MOVE_END_POINT, 'E',
  79. ID_POPUP_ITEM_MOVE_END_POINT );
  80. static EDA_HOTKEY HkDeleteItem( _HKI( "Delete Item" ), HK_DELETE_ITEM, WXK_DELETE,
  81. ID_POPUP_ITEM_DELETE );
  82. // Common: hotkeys_basic.h
  83. static EDA_HOTKEY HkUndo( _HKI( "Undo" ), HK_UNDO, GR_KB_CTRL + 'Z', (int) wxID_UNDO );
  84. static EDA_HOTKEY HkRedo( _HKI( "Redo" ), HK_REDO, GR_KB_CTRL + 'Y', (int) wxID_REDO );
  85. static EDA_HOTKEY HkNew( _HKI( "New" ), HK_NEW, GR_KB_CTRL + 'N', (int) wxID_NEW );
  86. static EDA_HOTKEY HkOpen( _HKI( "Open" ), HK_OPEN, GR_KB_CTRL + 'O', (int) wxID_OPEN );
  87. static EDA_HOTKEY HkSave( _HKI( "Save" ), HK_SAVE, GR_KB_CTRL + 'S', (int) wxID_SAVE );
  88. static EDA_HOTKEY HkSaveAs( _HKI( "Save As" ), HK_SAVEAS, GR_KB_CTRL + GR_KB_SHIFT + 'S',
  89. (int) wxID_SAVEAS );
  90. static EDA_HOTKEY HkPrint( _HKI( "Print" ), HK_PRINT, GR_KB_CTRL + 'P', (int) wxID_PRINT );
  91. // List of common hotkey descriptors
  92. EDA_HOTKEY* s_Common_Hotkey_List[] =
  93. {
  94. &HkNew, &HkOpen, &HkSave, &HkSaveAs, &HkPrint,
  95. &HkUndo, &HkRedo,
  96. &HkZoomIn, &HkZoomOut, &HkZoomRedraw, &HkZoomCenter,
  97. &HkZoomAuto, &HkZoomSelection, &HkResetLocalCoord,
  98. &HkHelp,
  99. &HkMouseLeftClick,
  100. &HkMouseLeftDClick,
  101. NULL
  102. };
  103. EDA_HOTKEY* s_PlEditor_Hotkey_List[] =
  104. {
  105. &HkMoveItem, &HkMoveStartPoint,
  106. &HkMoveEndPoint,
  107. &HkPlaceItem,
  108. &HkDeleteItem,
  109. NULL
  110. };
  111. // Titles for hotkey editor and hotkey display
  112. static wxString commonSectionTitle( _HKI( "Common" ) );
  113. // list of sections and corresponding hotkey list for Pl_Editor
  114. // (used to create an hotkey config file)
  115. static wxString s_PlEditorSectionTag( wxT( "[pl_editor]" ) );
  116. static wxString s_PlEditorSectionTitle( _HKI( "Page Layout Editor" ) );
  117. struct EDA_HOTKEY_CONFIG PlEditorHokeysDescr[] =
  118. {
  119. { &g_CommonSectionTag, s_Common_Hotkey_List, &commonSectionTitle },
  120. { &s_PlEditorSectionTag, s_PlEditor_Hotkey_List, &s_PlEditorSectionTitle },
  121. { NULL, NULL, NULL }
  122. };
  123. EDA_HOTKEY* PL_EDITOR_FRAME::GetHotKeyDescription( int aCommand ) const
  124. {
  125. EDA_HOTKEY* HK_Descr = GetDescriptorFromCommand( aCommand, s_Common_Hotkey_List );
  126. if( HK_Descr == NULL )
  127. HK_Descr = GetDescriptorFromCommand( aCommand, s_PlEditor_Hotkey_List );
  128. return HK_Descr;
  129. }
  130. bool PL_EDITOR_FRAME::OnHotKey( wxDC* aDC, int aHotkeyCode,
  131. const wxPoint& aPosition, EDA_ITEM* aItem )
  132. {
  133. bool busy = GetScreen()->GetCurItem() != NULL;
  134. wxCommandEvent cmd( wxEVT_COMMAND_MENU_SELECTED );
  135. cmd.SetEventObject( this );
  136. /* Convert lower to upper case (the usual toupper function has problem with non ascii
  137. * codes like function keys */
  138. if( (aHotkeyCode >= 'a') && (aHotkeyCode <= 'z') )
  139. aHotkeyCode += 'A' - 'a';
  140. EDA_HOTKEY * HK_Descr = GetDescriptorFromHotkey( aHotkeyCode, s_PlEditor_Hotkey_List );
  141. if( HK_Descr == NULL )
  142. HK_Descr = GetDescriptorFromHotkey( aHotkeyCode, s_Common_Hotkey_List );
  143. if( HK_Descr == NULL )
  144. return false;
  145. WORKSHEET_DATAITEM* item;
  146. switch( HK_Descr->m_Idcommand )
  147. {
  148. case HK_NOT_FOUND:
  149. return false;
  150. case HK_LEFT_CLICK:
  151. OnLeftClick( aDC, aPosition );
  152. break;
  153. case HK_LEFT_DCLICK: // Simulate a double left click: generate 2 events
  154. OnLeftClick( aDC, aPosition );
  155. OnLeftDClick( aDC, aPosition );
  156. break;
  157. case HK_NEW:
  158. cmd.SetId( wxID_NEW );
  159. GetEventHandler()->ProcessEvent( cmd );
  160. break;
  161. case HK_OPEN:
  162. cmd.SetId( wxID_OPEN );
  163. GetEventHandler()->ProcessEvent( cmd );
  164. break;
  165. case HK_SAVE:
  166. cmd.SetId( wxID_SAVE );
  167. GetEventHandler()->ProcessEvent( cmd );
  168. break;
  169. case HK_SAVEAS:
  170. cmd.SetId( wxID_SAVEAS );
  171. GetEventHandler()->ProcessEvent( cmd );
  172. break;
  173. case HK_PRINT:
  174. cmd.SetId( wxID_PRINT );
  175. GetEventHandler()->ProcessEvent( cmd );
  176. break;
  177. case HK_UNDO:
  178. case HK_REDO:
  179. if( busy )
  180. break;
  181. cmd.SetId( HK_Descr->m_IdMenuEvent );
  182. GetEventHandler()->ProcessEvent( cmd );
  183. break;
  184. case HK_ZOOM_IN:
  185. cmd.SetId( ID_POPUP_ZOOM_IN );
  186. GetEventHandler()->ProcessEvent( cmd );
  187. break;
  188. case HK_ZOOM_OUT:
  189. cmd.SetId( ID_POPUP_ZOOM_OUT );
  190. GetEventHandler()->ProcessEvent( cmd );
  191. break;
  192. case HK_ZOOM_REDRAW:
  193. cmd.SetId( ID_ZOOM_REDRAW );
  194. GetEventHandler()->ProcessEvent( cmd );
  195. break;
  196. case HK_ZOOM_CENTER:
  197. cmd.SetId( ID_POPUP_ZOOM_CENTER );
  198. GetEventHandler()->ProcessEvent( cmd );
  199. break;
  200. case HK_ZOOM_AUTO:
  201. cmd.SetId( ID_ZOOM_PAGE );
  202. GetEventHandler()->ProcessEvent( cmd );
  203. break;
  204. case HK_ZOOM_SELECTION:
  205. cmd.SetId( ID_ZOOM_SELECTION );
  206. GetEventHandler()->ProcessEvent( cmd );
  207. break;
  208. case HK_RESET_LOCAL_COORD: // Reset the relative coord
  209. GetScreen()->m_O_Curseur = GetCrossHairPosition();
  210. break;
  211. case HK_HELP: // Display Current hotkey list
  212. DisplayHotkeyList( this, PlEditorHokeysDescr );
  213. break;
  214. case HK_SET_GRID_ORIGIN:
  215. SetGridOrigin( GetCrossHairPosition() );
  216. break;
  217. case HK_MOVE_ITEM:
  218. case HK_MOVE_START_POINT:
  219. case HK_MOVE_END_POINT:
  220. case HK_DELETE_ITEM:
  221. if( busy )
  222. break;
  223. if( (item = Locate( aDC, aPosition ) ) == NULL )
  224. break;
  225. // Only rect and lines have a end point.
  226. if( HK_Descr->m_Idcommand == HK_MOVE_END_POINT && !item->HasEndPoint() )
  227. break;
  228. if( m_treePagelayout->GetPageLayoutSelectedItem() != item )
  229. m_treePagelayout->SelectCell( item );
  230. cmd.SetId( HK_Descr->m_IdMenuEvent );
  231. GetEventHandler()->ProcessEvent( cmd );
  232. break;
  233. case HK_PLACE_ITEM:
  234. if( busy )
  235. {
  236. cmd.SetId( HK_Descr->m_IdMenuEvent );
  237. GetEventHandler()->ProcessEvent( cmd );
  238. }
  239. break;
  240. default:
  241. wxMessageBox( wxT( "Unknown hotkey" ) );
  242. return false;
  243. }
  244. return true;
  245. }