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.

169 lines
5.5 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2018 Kicad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <panel_hotkeys_editor.h>
  24. #include <eda_base_frame.h>
  25. #include <wx/srchctrl.h>
  26. #include <wx/panel.h>
  27. #include <wx/button.h>
  28. #include <wx/sizer.h>
  29. #include <widgets/button_row_panel.h>
  30. #include <widgets/ui_common.h>
  31. static const wxSize default_dialog_size { 500, 350 };
  32. static const wxSize min_dialog_size { -1, 350 };
  33. /**
  34. * Helper function to add a filter box to a panel, with some
  35. * sensible defaults for that purpose.
  36. *
  37. * @param aParent The panrent widget/panel
  38. * @param aDescriptiveText The text to show when the box is empty.
  39. * @return A newly constructed filter box - the caller owns it
  40. */
  41. static wxSearchCtrl* CreateTextFilterBox( wxWindow* aParent, const wxString& aDescriptiveText )
  42. {
  43. auto search_widget = new wxSearchCtrl( aParent, wxID_ANY );
  44. search_widget->ShowSearchButton( false );
  45. search_widget->ShowCancelButton( true );
  46. search_widget->SetDescriptiveText( aDescriptiveText);
  47. return search_widget;
  48. }
  49. PANEL_HOTKEYS_EDITOR::PANEL_HOTKEYS_EDITOR( EDA_BASE_FRAME* aFrame, wxWindow* aWindow,
  50. bool aReadOnly,
  51. EDA_HOTKEY_CONFIG* aHotkeys,
  52. EDA_HOTKEY_CONFIG* aShowHotkeys,
  53. const wxString& aNickname ) :
  54. wxPanel( aWindow, wxID_ANY, wxDefaultPosition, default_dialog_size ),
  55. m_frame( aFrame ),
  56. m_readOnly( aReadOnly ),
  57. m_hotkeys( aHotkeys ),
  58. m_nickname( aNickname ),
  59. m_hotkeyStore( aShowHotkeys )
  60. {
  61. const auto margin = KIUI::GetStdMargin();
  62. auto mainSizer = new wxBoxSizer( wxVERTICAL );
  63. // Sub-sizer for setting a wider side margin
  64. // TODO: Can this be set by the parent widget- doesn't seem to be
  65. // this panel's responsibility?
  66. const int side_margins = 10; // seems to be hardcoded in wxFB
  67. auto bMargins = new wxBoxSizer( wxVERTICAL );
  68. auto filterSearch = CreateTextFilterBox( this, _( "Type filter text" ) );
  69. bMargins->Add( filterSearch, 0, wxBOTTOM | wxEXPAND | wxTOP, margin );
  70. m_hotkeyListCtrl = new WIDGET_HOTKEY_LIST( this, m_hotkeyStore, m_readOnly );
  71. bMargins->Add( m_hotkeyListCtrl, 1, wxALL | wxEXPAND, margin );
  72. if( !m_readOnly )
  73. installButtons( bMargins );
  74. mainSizer->Add( bMargins, 1, wxEXPAND | wxRIGHT | wxLEFT, side_margins );
  75. this->SetSizer( mainSizer );
  76. this->Layout();
  77. // Connect Events
  78. filterSearch->Bind( wxEVT_COMMAND_TEXT_UPDATED,
  79. &PANEL_HOTKEYS_EDITOR::OnFilterSearch, this );
  80. }
  81. void PANEL_HOTKEYS_EDITOR::installButtons( wxSizer* aSizer )
  82. {
  83. const BUTTON_ROW_PANEL::BTN_DEF_LIST l_btn_defs = {
  84. {
  85. wxID_RESET,
  86. _( "Reset Hotkeys" ),
  87. _( "Undo all changes made so far in this dialog" ),
  88. [this]( wxCommandEvent& ){
  89. m_hotkeyListCtrl->ResetAllHotkeys( false );
  90. }
  91. },
  92. {
  93. wxID_ANY,
  94. _( "Set to Defaults" ),
  95. _( "Set all hotkeys to the built-in KiCad defaults" ),
  96. [this]( wxCommandEvent& ){
  97. m_hotkeyListCtrl->ResetAllHotkeys( true );
  98. }
  99. }
  100. };
  101. const BUTTON_ROW_PANEL::BTN_DEF_LIST r_btn_defs = {
  102. {
  103. wxID_ANY,
  104. _( "Import..." ),
  105. _( "Import hotkey definitions from an external file, replacing the current values" ),
  106. [this]( wxCommandEvent& ){
  107. m_frame->ImportHotkeyConfigFromFile( m_hotkeys, m_nickname );
  108. }
  109. },
  110. {
  111. wxID_ANY,
  112. _( "Export..." ),
  113. _( "Export these hotkey definitions to an external file" ),
  114. [this]( wxCommandEvent& ){
  115. m_frame->ExportHotkeyConfigToFile( m_hotkeys, m_nickname );
  116. }
  117. },
  118. };
  119. auto btnPanel = std::make_unique<BUTTON_ROW_PANEL>( this, l_btn_defs, r_btn_defs );
  120. aSizer->Add( btnPanel.release(), 0, wxEXPAND | wxTOP, KIUI::GetStdMargin() );
  121. }
  122. bool PANEL_HOTKEYS_EDITOR::TransferDataToWindow()
  123. {
  124. return m_hotkeyListCtrl->TransferDataToControl();
  125. }
  126. bool PANEL_HOTKEYS_EDITOR::TransferDataFromWindow()
  127. {
  128. if( !m_hotkeyListCtrl->TransferDataFromControl() )
  129. return false;
  130. // save the hotkeys
  131. m_frame->WriteHotkeyConfig( m_hotkeys );
  132. return true;
  133. }
  134. void PANEL_HOTKEYS_EDITOR::OnFilterSearch( wxCommandEvent& aEvent )
  135. {
  136. const auto searchStr = aEvent.GetString();
  137. m_hotkeyListCtrl->ApplyFilterString( searchStr );
  138. }