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.

257 lines
7.3 KiB

  1. /*
  2. dialog_hotkeys_editor.cpp
  3. */
  4. /*
  5. * This program source code file is part of KICAD, a free EDA CAD application.
  6. *
  7. * Copyright (C) 1992-2010 Kicad Developers, see change_log.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <algorithm>
  27. #include "fctsys.h"
  28. #include "appl_wxstruct.h"
  29. #include "common.h"
  30. #include "dialog_hotkeys_editor.h"
  31. void InstallHotkeyFrame( WinEDA_DrawFrame* parent,
  32. Ki_HotkeyInfoSectionDescriptor* hotkeys )
  33. {
  34. HOTKEYS_EDITOR_DIALOG dialog( parent, hotkeys );
  35. int diag = dialog.ShowModal();
  36. if( diag == wxID_OK )
  37. {
  38. parent->ReCreateMenuBar();
  39. parent->Refresh();
  40. }
  41. }
  42. HOTKEYS_EDITOR_DIALOG::HOTKEYS_EDITOR_DIALOG( WinEDA_DrawFrame* parent,
  43. Ki_HotkeyInfoSectionDescriptor* hotkeys ) :
  44. HOTKEYS_EDITOR_DIALOG_BASE( parent )
  45. {
  46. m_parent = parent;
  47. m_hotkeys = hotkeys;
  48. m_curEditingRow = -1;
  49. m_table = new HotkeyGridTable( hotkeys );
  50. m_hotkeyGrid->SetTable( m_table, true );
  51. m_hotkeyGrid->AutoSizeColumn( 0 );
  52. m_hotkeyGrid->EnableDragGridSize( false );
  53. for( int i = 0; i < m_hotkeyGrid->GetNumberRows(); ++i )
  54. {
  55. m_hotkeyGrid->SetReadOnly( i, 0, true );
  56. m_hotkeyGrid->SetReadOnly( i, 1, true );
  57. }
  58. SetFocus();
  59. GetSizer()->SetSizeHints( this );
  60. Center();
  61. }
  62. void HOTKEYS_EDITOR_DIALOG::OnOKClicked( wxCommandEvent& event )
  63. {
  64. /* edit the live hotkey table */
  65. HotkeyGridTable::hotkey_spec_vector& hotkey_vec = m_table->getHotkeys();
  66. Ki_HotkeyInfoSectionDescriptor* section;
  67. for( section = m_hotkeys; section->m_HK_InfoList; section++ )
  68. {
  69. wxString sectionTag = *section->m_SectionTag;
  70. Ki_HotkeyInfo** info_ptr;
  71. for( info_ptr = section->m_HK_InfoList; *info_ptr; info_ptr++ )
  72. {
  73. Ki_HotkeyInfo* info = *info_ptr;
  74. /* find the corresponding hotkey */
  75. HotkeyGridTable::hotkey_spec_vector::iterator i;
  76. for( i = hotkey_vec.begin(); i != hotkey_vec.end(); ++i )
  77. {
  78. if( i->first == sectionTag
  79. && i->second
  80. && i->second->m_Idcommand == info->m_Idcommand )
  81. {
  82. info->m_KeyCode = i->second->m_KeyCode;
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. /* save the hotkeys */
  89. m_parent->WriteHotkeyConfig( m_hotkeys );
  90. EndModal( wxID_OK );
  91. }
  92. void HOTKEYS_EDITOR_DIALOG::CancelClicked( wxCommandEvent& event )
  93. {
  94. EndModal( wxID_CANCEL );
  95. }
  96. /* Reinit the hotkeys to the initial state (remove all pending changes
  97. */
  98. void HOTKEYS_EDITOR_DIALOG::UndoClicked( wxCommandEvent& event )
  99. {
  100. m_table->RestoreFrom( m_hotkeys );
  101. m_curEditingRow = -1;
  102. for( int i = 0; i < m_hotkeyGrid->GetNumberRows(); ++i )
  103. SetHotkeyCellState( i, false );
  104. m_hotkeyGrid->Refresh();
  105. Update();
  106. }
  107. void HOTKEYS_EDITOR_DIALOG::SetHotkeyCellState( int aRow, bool aHightlight )
  108. {
  109. if( aHightlight )
  110. {
  111. m_hotkeyGrid->SetCellTextColour( aRow, 1, *wxRED );
  112. wxFont bold_font(m_hotkeyGrid->GetDefaultCellFont() );
  113. bold_font.SetWeight(wxFONTWEIGHT_BOLD);
  114. m_hotkeyGrid->SetCellFont( aRow, 1, bold_font );
  115. }
  116. else
  117. {
  118. m_hotkeyGrid->SetCellTextColour( aRow, 1, m_hotkeyGrid->GetDefaultCellTextColour() );
  119. m_hotkeyGrid->SetCellFont( aRow, 1, m_hotkeyGrid->GetDefaultCellFont() );
  120. }
  121. }
  122. void HOTKEYS_EDITOR_DIALOG::OnClickOnCell( wxGridEvent& event )
  123. {
  124. if( m_curEditingRow != -1 )
  125. SetHotkeyCellState( m_curEditingRow, false );
  126. int newRow = event.GetRow();
  127. if( ( event.GetCol() != 1 ) || ( m_table->isHeader( newRow ) ) )
  128. {
  129. m_curEditingRow = -1;
  130. }
  131. else
  132. {
  133. m_curEditingRow = newRow;
  134. SetHotkeyCellState( m_curEditingRow, true );
  135. }
  136. m_hotkeyGrid->Refresh();
  137. Update();
  138. }
  139. /** OnRightClickOnCell
  140. * If a cell is selected, display a list of keys for selection
  141. * The list is restricted to keys that cannot be entered:
  142. * tab, home ... because these keys have special functions in dialogs
  143. */
  144. void HOTKEYS_EDITOR_DIALOG::OnRightClickOnCell( wxGridEvent& event )
  145. {
  146. // Select the new cell if needed
  147. OnClickOnCell(event);
  148. if( m_curEditingRow == -1 )
  149. return;
  150. // Do not translate these key names. They are internally used.
  151. //ee hotkeys_basic.cpp
  152. #define C_COUNT 8
  153. wxString choices[C_COUNT] =
  154. {
  155. wxT("End")
  156. wxT("Tab"),
  157. wxT("Ctrl+Tab"),
  158. wxT("Alt+Tab"),
  159. wxT("Home"),
  160. wxT("Space"),
  161. wxT("Ctrl+Space"),
  162. wxT("Alt+Space"),
  163. };
  164. wxString keyname = wxGetSingleChoice(
  165. _("Special keys only. For others keys, use keyboard"),
  166. _("Select a key"), C_COUNT, choices, this);
  167. int key = ReturnKeyCodeFromKeyName( keyname );
  168. if( key == 0 )
  169. return;
  170. m_table->SetKeyCode( m_curEditingRow, key );
  171. m_hotkeyGrid->Refresh();
  172. Update();
  173. }
  174. void HOTKEYS_EDITOR_DIALOG::OnKeyPressed( wxKeyEvent& event )
  175. {
  176. if( m_curEditingRow != -1 )
  177. {
  178. long key = event.GetKeyCode();
  179. switch( key )
  180. {
  181. case WXK_ESCAPE:
  182. SetHotkeyCellState( m_curEditingRow, false );
  183. m_curEditingRow = -1;
  184. break;
  185. default:
  186. if( event.ControlDown() )
  187. key |= GR_KB_CTRL;
  188. if( event.AltDown() )
  189. key |= GR_KB_ALT;
  190. if( event.ShiftDown() && (key > 256) )
  191. key |= GR_KB_SHIFT;
  192. // Remap Ctrl A (=1+GR_KB_CTRL) to Ctrl Z(=26+GR_KB_CTRL)
  193. // to GR_KB_CTRL+'A' .. GR_KB_CTRL+'Z'
  194. if( (key > GR_KB_CTRL) && (key <= GR_KB_CTRL+26) )
  195. key += ('A' - 1);
  196. if( key >= 'a' && key <= 'z' ) // convert to uppercase
  197. key = key + ('A' - 'a');
  198. #if 0 // For debug only
  199. wxString msg;
  200. msg.Printf(wxT("key %X, keycode %X"),event.GetKeyCode(), key);
  201. wxMessageBox(msg);
  202. #endif
  203. // See if this key code is handled in hotkeys names list
  204. bool exists;
  205. ReturnKeyNameFromKeyCode( key, &exists );
  206. if( !exists ) // not handled, see hotkeys_basic.cpp
  207. wxMessageBox( _("Hotkey code not handled" ) );
  208. else
  209. {
  210. m_table->SetKeyCode( m_curEditingRow, key );
  211. }
  212. break;
  213. }
  214. }
  215. m_hotkeyGrid->Refresh();
  216. Update();
  217. }