KiCad PCB EDA Suite
dialog_schematic_find.cpp
Go to the documentation of this file.
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 
25 #include <dialog_schematic_find.h>
26 #include <tool/actions.h>
27 #include <sch_edit_frame.h>
29 
30 
31 DIALOG_SCH_FIND::DIALOG_SCH_FIND( SCH_EDIT_FRAME* aParent, wxFindReplaceData* aData,
32  const wxPoint& aPosition, const wxSize& aSize, int aStyle ) :
33  DIALOG_SCH_FIND_BASE( aParent, wxID_ANY, _( "Find" ), aPosition, aSize,
34  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | aStyle ),
35  m_frame( aParent ),
36  m_editorControl( m_frame->GetToolManager()->GetTool<SCH_EDITOR_CONTROL>() ),
37  m_findReplaceData( aData ),
38  m_findDirty( true )
39 {
40  wxASSERT_MSG( m_findReplaceData, wxT( "can't create find dialog without data" ) );
41 
42  if( aStyle & wxFR_REPLACEDIALOG )
43  {
44  SetTitle( _( "Find and Replace" ) );
45  m_buttonReplace->Show( true );
46  m_buttonReplaceAll->Show( true );
47  m_staticReplace->Show( true );
48  m_comboReplace->Show( true );
49  m_checkReplaceReferences->Show( true );
50  m_checkWildcardMatch->Show( false ); // Wildcard replace is not implemented.
51  }
52 
53  int flags = m_findReplaceData->GetFlags();
54  m_radioForward->SetValue( flags & wxFR_DOWN );
55  m_radioBackward->SetValue( ( flags & wxFR_DOWN ) == 0 );
56  m_checkMatchCase->SetValue( flags & wxFR_MATCHCASE );
57  m_checkWholeWord->SetValue( flags & wxFR_WHOLEWORD );
58 
59  /* Whole word and wild card searches are mutually exclusive. */
60  if( !( flags & wxFR_WHOLEWORD ) )
61  m_checkWildcardMatch->SetValue( flags & FR_MATCH_WILDCARD );
62 
63  m_checkAllFields->SetValue( flags & FR_SEARCH_ALL_FIELDS );
65  m_checkAllPins->SetValue( flags & FR_SEARCH_ALL_PINS );
67 
68  m_buttonFind->SetDefault();
70 
71  // Adjust the height of the dialog to prevent controls from being hidden when
72  // switching between the find and find/replace modes of the dialog. This ignores
73  // the users preferred height if any of the controls would be hidden.
74  GetSizer()->SetSizeHints( this );
75  wxSize size = aSize;
76 
77  if( aSize != wxDefaultSize )
78  {
79  wxSize bestSize = GetBestSize();
80 
81  if( size.GetHeight() != bestSize.GetHeight() )
82  size.SetHeight( bestSize.GetHeight() );
83  }
84 
85  SetSize( size );
86 
87  GetSizer()->Fit( this ); // Needed on Ubuntu/Unity to display the dialog
88 
89  Connect( wxEVT_CHAR, wxKeyEventHandler( DIALOG_SCH_FIND::OnChar ), nullptr, this );
90 }
91 
92 
93 void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
94 {
95  // Notify the SCH_EDIT_FRAME
97 }
98 
99 
100 void DIALOG_SCH_FIND::OnIdle( wxIdleEvent& aEvent )
101 {
102  if( m_findDirty )
103  {
105  m_findDirty = false;
106  }
107 }
108 
109 
110 void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
111 {
112  wxCloseEvent dummy;
113  OnClose( dummy );
114 }
115 
116 
117 void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
118 {
119  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() &&
121 }
122 
123 
124 void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
125 {
126  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
127 }
128 
129 
130 void DIALOG_SCH_FIND::OnChar( wxKeyEvent& aEvent )
131 {
132  if( aEvent.GetKeyCode() == WXK_RETURN )
133  {
134  wxCommandEvent dummyCommand;
135  OnFind( dummyCommand );
136  }
137 }
138 
139 
140 void DIALOG_SCH_FIND::OnSearchForText( wxCommandEvent& aEvent )
141 {
142  m_findReplaceData->SetFindString( m_comboFind->GetValue() );
143  m_findDirty = true;
144 }
145 
146 
147 void DIALOG_SCH_FIND::OnSearchForSelect( wxCommandEvent& aEvent )
148 {
149  m_findReplaceData->SetFindString( m_comboFind->GetValue() );
150 
151  // Move the search string to the top of the list if it isn't already there.
152  if( aEvent.GetSelection() != 0 )
153  {
154  wxString tmp = m_comboFind->GetValue();
155  m_comboFind->Delete( aEvent.GetSelection() );
156  m_comboFind->Insert( tmp, 0 );
157  m_comboFind->SetSelection( 0 );
158  }
159 
161 }
162 
163 
164 void DIALOG_SCH_FIND::OnReplaceWithText( wxCommandEvent& aEvent )
165 {
166  m_findReplaceData->SetReplaceString( m_comboReplace->GetValue() );
167 }
168 
169 
170 void DIALOG_SCH_FIND::OnReplaceWithSelect( wxCommandEvent& aEvent )
171 {
172  m_findReplaceData->SetReplaceString( m_comboReplace->GetValue() );
173 
174  // Move the replace string to the top of the list if it isn't already there.
175  if( aEvent.GetSelection() != 0 )
176  {
177  wxString tmp = m_comboReplace->GetValue();
178  m_comboReplace->Delete( aEvent.GetSelection() );
179  m_comboReplace->Insert( tmp, 0 );
180  m_comboReplace->SetSelection( 0 );
181  }
182 }
183 
184 
185 void DIALOG_SCH_FIND::OnSearchForEnter( wxCommandEvent& aEvent )
186 {
187  OnFind( aEvent );
188 }
189 
190 
191 void DIALOG_SCH_FIND::OnReplaceWithEnter( wxCommandEvent& aEvent )
192 {
193  OnFind( aEvent );
194 }
195 
196 
197 void DIALOG_SCH_FIND::OnOptions( wxCommandEvent& aEvent )
198 {
199  int flags = 0;
200 
201  if( m_radioForward->GetValue() )
202  flags |= wxFR_DOWN;
203 
204  if( m_checkMatchCase->GetValue() )
205  flags |= wxFR_MATCHCASE;
206 
207  if( m_checkWholeWord->GetValue() )
208  flags |= wxFR_WHOLEWORD;
209 
210  if( m_checkWildcardMatch->IsShown() && m_checkWildcardMatch->GetValue() )
211  flags |= FR_MATCH_WILDCARD;
212 
213  if( m_checkAllFields->GetValue() )
214  flags |= FR_SEARCH_ALL_FIELDS;
215 
216  if( m_checkAllPins->GetValue() )
217  flags |= FR_SEARCH_ALL_PINS;
218 
219  if( m_checkCurrentSheetOnly->GetValue() )
220  flags |= FR_CURRENT_SHEET_ONLY;
221 
222  if( m_checkReplaceReferences->GetValue() )
223  flags |= FR_REPLACE_REFERENCES;
224 
225  m_findReplaceData->SetFlags( flags );
226  m_findDirty = true;
227 }
228 
229 
230 void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
231 {
232  int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
233 
234  if( index == wxNOT_FOUND )
235  {
236  m_comboFind->Insert( m_comboFind->GetValue(), 0 );
237  }
238  else if( index != 0 )
239  {
240  /* Move the search string to the top of the list if it isn't already there. */
241  wxString tmp = m_comboFind->GetValue();
242  m_comboFind->Delete( index );
243  m_comboFind->Insert( tmp, 0 );
244  m_comboFind->SetSelection( 0 );
245  }
246 
248 }
249 
250 
251 void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
252 {
253  int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
254 
255  if( index == wxNOT_FOUND )
256  {
257  m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
258  }
259  else if( index != 0 )
260  {
261  /* Move the search string to the top of the list if it isn't already there. */
262  wxString tmp = m_comboReplace->GetValue();
263  m_comboReplace->Delete( index );
264  m_comboReplace->Insert( tmp, 0 );
265  m_comboReplace->SetSelection( 0 );
266  }
267 
268  if( aEvent.GetId() == wxID_REPLACE )
270  else if( aEvent.GetId() == wxID_REPLACE_ALL )
272 }
273 
274 
275 wxArrayString DIALOG_SCH_FIND::GetFindEntries() const
276 {
277  int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
278 
279  if( index == wxNOT_FOUND )
280  {
281  m_comboFind->Insert( m_comboFind->GetValue(), 0 );
282  }
283  else if( index != 0 )
284  {
285  /* Move the search string to the top of the list if it isn't already there. */
286  wxString tmp = m_comboFind->GetValue();
287  m_comboFind->Delete( index );
288  m_comboFind->Insert( tmp, 0 );
289  }
290 
291  return m_comboFind->GetStrings();
292 }
293 
294 
295 void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries )
296 {
297  m_comboFind->Append( aEntries );
298 
299  while( m_comboFind->GetCount() > 10 )
300  {
301  m_frame->GetFindHistoryList().pop_back();
302  m_comboFind->Delete( 9 );
303  }
304 
305  if( m_comboFind->GetCount() )
306  {
307  m_comboFind->SetSelection( 0 );
308  m_comboFind->SelectAll();
309  }
310 }
311 
312 
313 void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
314 {
315  m_comboReplace->Append( aEntries );
316 
317  while( m_comboReplace->GetCount() > 10 )
318  {
319  m_frame->GetFindHistoryList().pop_back();
320  m_comboReplace->Delete( 9 );
321  }
322 
323  if( m_comboReplace->GetCount() )
324  {
325  m_comboReplace->SetSelection( 0 );
326  m_comboFind->SelectAll();
327  }
328 }
void OnUpdateReplaceAllUI(wxUpdateUIEvent &aEvent) override
wxArrayString GetFindEntries() const
void OnChar(wxKeyEvent &aEvent)
Class DIALOG_SCH_FIND_BASE.
void OnReplaceWithEnter(wxCommandEvent &event) override
void OnSearchForText(wxCommandEvent &aEvent) override
static TOOL_ACTION replaceAll
Definition: actions.h:81
int ReplaceAndFindNext(const TOOL_EVENT &aEvent)
void OnIdle(wxIdleEvent &event) override
Schematic editor (Eeschema) main window.
int FindNext(const TOOL_EVENT &aEvent)
void OnReplace(wxCommandEvent &aEvent) override
void OnFindDialogClose()
Notification that the Find dialog has closed.
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:97
static TOOL_ACTION updateFind
Definition: actions.h:82
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:72
Handle actions specific to the schematic editor.
wxFindReplaceData * m_findReplaceData
DIALOG_SCH_FIND(SCH_EDIT_FRAME *aParent, wxFindReplaceData *aData, const wxPoint &aPosition=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, int aStyle=0)
SCH_EDITOR_CONTROL * m_editorControl
void OnReplaceWithSelect(wxCommandEvent &aEvent) override
void OnSearchForEnter(wxCommandEvent &event) override
int ReplaceAll(const TOOL_EVENT &aEvent)
Subclass of DIALOG_SCH_FIND_BASE, which is generated by wxFormBuilder.
void OnReplaceWithText(wxCommandEvent &aEvent) override
void OnSearchForSelect(wxCommandEvent &aEvent) override
int UpdateFind(const TOOL_EVENT &aEvent)
Notifies pcbnew about the selected item.
void OnClose(wxCloseEvent &aEvent) override
void OnUpdateReplaceUI(wxUpdateUIEvent &aEvent) override
#define _(s)
static TOOL_ACTION replaceAndFindNext
Definition: actions.h:80
void SetReplaceEntries(const wxArrayString &aEntries)
void OnOptions(wxCommandEvent &event) override
void SetFindEntries(const wxArrayString &aEntries)
void OnFind(wxCommandEvent &aEvent) override
wxArrayString & GetFindHistoryList()
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.cpp:72
SCH_EDIT_FRAME * m_frame
void OnCancel(wxCommandEvent &aEvent) override
static TOOL_ACTION findNext
Definition: actions.h:78