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.

242 lines
7.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010 Wayne Stambaugh <stambaughw@verizon.net>
  5. * Copyright (C) 2010-2019 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <dialog_schematic_find.h>
  25. #include <tool/actions.h>
  26. #include <tool/tool_manager.h>
  27. #include <sch_edit_frame.h>
  28. #include <tools/sch_editor_control.h>
  29. DIALOG_SCH_FIND::DIALOG_SCH_FIND( SCH_EDIT_FRAME* aParent, wxFindReplaceData* aData,
  30. const wxPoint& aPosition, const wxSize& aSize, int aStyle ) :
  31. DIALOG_SCH_FIND_BASE( aParent, wxID_ANY, _( "Find" ), aPosition, aSize,
  32. wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | aStyle ),
  33. m_frame( aParent ),
  34. m_editorControl( m_frame->GetToolManager()->GetTool<SCH_EDITOR_CONTROL>() ),
  35. m_findReplaceData( aData )
  36. {
  37. wxASSERT_MSG( m_findReplaceData, wxT( "can't create find dialog without data" ) );
  38. if( aStyle & wxFR_REPLACEDIALOG )
  39. {
  40. SetTitle( _( "Find and Replace" ) );
  41. m_buttonReplace->Show( true );
  42. m_buttonReplaceAll->Show( true );
  43. m_staticReplace->Show( true );
  44. m_comboReplace->Show( true );
  45. m_checkReplaceReferences->Show( true );
  46. m_checkWildcardMatch->Show( false ); // Wildcard replace is not implemented.
  47. }
  48. int flags = m_findReplaceData->GetFlags();
  49. m_radioForward->SetValue( flags & wxFR_DOWN );
  50. m_radioBackward->SetValue( ( flags & wxFR_DOWN ) == 0 );
  51. m_checkMatchCase->SetValue( flags & wxFR_MATCHCASE );
  52. m_checkWholeWord->SetValue( flags & wxFR_WHOLEWORD );
  53. /* Whole word and wild card searches are mutually exclusive. */
  54. if( !( flags & wxFR_WHOLEWORD ) )
  55. m_checkWildcardMatch->SetValue( flags & FR_MATCH_WILDCARD );
  56. m_checkAllFields->SetValue( flags & FR_SEARCH_ALL_FIELDS );
  57. m_checkReplaceReferences->SetValue( flags & FR_REPLACE_REFERENCES );
  58. m_checkAllPins->SetValue( flags & FR_SEARCH_ALL_PINS );
  59. m_checkCurrentSheetOnly->SetValue( flags & FR_CURRENT_SHEET_ONLY );
  60. m_buttonFind->SetDefault();
  61. SetInitialFocus( m_comboFind );
  62. SetPosition( aPosition );
  63. // Adjust the height of the dialog to prevent controls from being hidden when
  64. // switching between the find and find/replace modes of the dialog. This ignores
  65. // the users preferred height if any of the controls would be hidden.
  66. GetSizer()->SetSizeHints( this );
  67. wxSize size = aSize;
  68. if( aSize != wxDefaultSize )
  69. {
  70. wxSize bestSize = GetBestSize();
  71. if( size.GetHeight() != bestSize.GetHeight() )
  72. size.SetHeight( bestSize.GetHeight() );
  73. }
  74. SetSize( size );
  75. GetSizer()->Fit( this ); // Needed on Ubuntu/Unity to display the dialog
  76. Connect( wxEVT_CHAR, wxKeyEventHandler( DIALOG_SCH_FIND::OnChar ), nullptr, this );
  77. }
  78. void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
  79. {
  80. // Notify the SCH_EDIT_FRAME
  81. m_frame->OnFindDialogClose();
  82. // Notify the controller
  83. m_editorControl->UpdateFind( ACTIONS::updateFind.MakeEvent() );
  84. }
  85. void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
  86. {
  87. aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() &&
  88. m_editorControl->HasMatch() );
  89. }
  90. void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
  91. {
  92. aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
  93. }
  94. void DIALOG_SCH_FIND::OnChar( wxKeyEvent& aEvent )
  95. {
  96. if( aEvent.GetKeyCode() == WXK_RETURN )
  97. {
  98. wxCommandEvent dummyCommand;
  99. OnFind( dummyCommand );
  100. }
  101. }
  102. void DIALOG_SCH_FIND::OnSearchForText( wxCommandEvent& aEvent )
  103. {
  104. m_findReplaceData->SetFindString( m_comboFind->GetValue() );
  105. m_editorControl->UpdateFind( ACTIONS::updateFind.MakeEvent() );
  106. }
  107. void DIALOG_SCH_FIND::OnTextEnter( wxCommandEvent& aEvent )
  108. {
  109. OnFind( aEvent );
  110. }
  111. void DIALOG_SCH_FIND::OnOptions( wxCommandEvent& aEvent )
  112. {
  113. int flags = 0;
  114. if( m_radioForward->GetValue() )
  115. flags |= wxFR_DOWN;
  116. if( m_checkMatchCase->GetValue() )
  117. flags |= wxFR_MATCHCASE;
  118. if( m_checkWholeWord->GetValue() )
  119. flags |= wxFR_WHOLEWORD;
  120. if( m_checkWildcardMatch->IsShown() && m_checkWildcardMatch->GetValue() )
  121. flags |= FR_MATCH_WILDCARD;
  122. if( m_checkAllFields->GetValue() )
  123. flags |= FR_SEARCH_ALL_FIELDS;
  124. if( m_checkAllPins->GetValue() )
  125. flags |= FR_SEARCH_ALL_PINS;
  126. if( m_checkCurrentSheetOnly->GetValue() )
  127. flags |= FR_CURRENT_SHEET_ONLY;
  128. m_findReplaceData->SetFlags( flags );
  129. m_editorControl->UpdateFind( ACTIONS::updateFind.MakeEvent() );
  130. }
  131. void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
  132. {
  133. int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
  134. if( index == wxNOT_FOUND )
  135. {
  136. m_comboFind->Insert( m_comboFind->GetValue(), 0 );
  137. }
  138. else if( index != 0 )
  139. {
  140. /* Move the search string to the top of the list if it isn't already there. */
  141. wxString tmp = m_comboFind->GetValue();
  142. m_comboFind->Delete( index );
  143. m_comboFind->Insert( tmp, 0 );
  144. m_comboFind->SetSelection( 0 );
  145. }
  146. m_editorControl->FindNext( ACTIONS::findNext.MakeEvent());
  147. }
  148. void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
  149. {
  150. int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
  151. if( index == wxNOT_FOUND )
  152. {
  153. m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
  154. }
  155. else if( index != 0 )
  156. {
  157. /* Move the search string to the top of the list if it isn't already there. */
  158. wxString tmp = m_comboReplace->GetValue();
  159. m_comboReplace->Delete( index );
  160. m_comboReplace->Insert( tmp, 0 );
  161. m_comboReplace->SetSelection( 0 );
  162. }
  163. if( aEvent.GetId() == wxID_REPLACE )
  164. m_editorControl->FindNext( ACTIONS::replaceAndFindNext.MakeEvent());
  165. else if( aEvent.GetId() == wxID_REPLACE_ALL )
  166. m_editorControl->FindNext( ACTIONS::replaceAll.MakeEvent());
  167. }
  168. wxArrayString DIALOG_SCH_FIND::GetFindEntries() const
  169. {
  170. return m_comboFind->GetStrings();
  171. }
  172. void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries )
  173. {
  174. m_comboFind->Append( aEntries );
  175. if( m_comboFind->GetCount() )
  176. {
  177. m_comboFind->SetSelection( 0 );
  178. m_comboFind->SelectAll();
  179. }
  180. }
  181. void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
  182. {
  183. m_comboReplace->Append( aEntries );
  184. if( m_comboReplace->GetCount() )
  185. {
  186. m_comboReplace->SetSelection( 0 );
  187. m_comboFind->SelectAll();
  188. }
  189. }