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  SetPosition( aPosition );
72 
73  // Adjust the height of the dialog to prevent controls from being hidden when
74  // switching between the find and find/replace modes of the dialog. This ignores
75  // the users preferred height if any of the controls would be hidden.
76  GetSizer()->SetSizeHints( this );
77  wxSize size = aSize;
78 
79  if( aSize != wxDefaultSize )
80  {
81  wxSize bestSize = GetBestSize();
82 
83  if( size.GetHeight() != bestSize.GetHeight() )
84  size.SetHeight( bestSize.GetHeight() );
85  }
86 
87  SetSize( size );
88 
89  GetSizer()->Fit( this ); // Needed on Ubuntu/Unity to display the dialog
90 
91  Connect( wxEVT_CHAR, wxKeyEventHandler( DIALOG_SCH_FIND::OnChar ), nullptr, this );
92 }
93 
94 
95 void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
96 {
97  // Notify the SCH_EDIT_FRAME
99 }
100 
101 
102 void DIALOG_SCH_FIND::OnIdle( wxIdleEvent& aEvent )
103 {
104  if( m_findDirty )
105  {
107  m_findDirty = false;
108  }
109 }
110 
111 
112 void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
113 {
114  wxCloseEvent dummy;
115  OnClose( dummy );
116 }
117 
118 
119 void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
120 {
121  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() &&
123 }
124 
125 
126 void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
127 {
128  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
129 }
130 
131 
132 void DIALOG_SCH_FIND::OnChar( wxKeyEvent& aEvent )
133 {
134  if( aEvent.GetKeyCode() == WXK_RETURN )
135  {
136  wxCommandEvent dummyCommand;
137  OnFind( dummyCommand );
138  }
139 }
140 
141 
142 void DIALOG_SCH_FIND::OnSearchForText( wxCommandEvent& aEvent )
143 {
144  m_findReplaceData->SetFindString( m_comboFind->GetValue() );
145  m_findDirty = true;
146 }
147 
148 
149 void DIALOG_SCH_FIND::OnSearchForSelect( wxCommandEvent& aEvent )
150 {
151  m_findReplaceData->SetFindString( m_comboFind->GetValue() );
152 
153  // Move the search string to the top of the list if it isn't already there.
154  if( aEvent.GetSelection() != 0 )
155  {
156  wxString tmp = m_comboFind->GetValue();
157  m_comboFind->Delete( aEvent.GetSelection() );
158  m_comboFind->Insert( tmp, 0 );
159  m_comboFind->SetSelection( 0 );
160  }
161 
163 }
164 
165 
166 void DIALOG_SCH_FIND::OnReplaceWithText( wxCommandEvent& aEvent )
167 {
168  m_findReplaceData->SetReplaceString( m_comboReplace->GetValue() );
169 }
170 
171 
172 void DIALOG_SCH_FIND::OnReplaceWithSelect( wxCommandEvent& aEvent )
173 {
174  m_findReplaceData->SetReplaceString( m_comboReplace->GetValue() );
175 
176  // Move the replace string to the top of the list if it isn't already there.
177  if( aEvent.GetSelection() != 0 )
178  {
179  wxString tmp = m_comboReplace->GetValue();
180  m_comboReplace->Delete( aEvent.GetSelection() );
181  m_comboReplace->Insert( tmp, 0 );
182  m_comboReplace->SetSelection( 0 );
183  }
184 }
185 
186 
187 void DIALOG_SCH_FIND::OnSearchForEnter( wxCommandEvent& aEvent )
188 {
189  OnFind( aEvent );
190 }
191 
192 
193 void DIALOG_SCH_FIND::OnReplaceWithEnter( wxCommandEvent& aEvent )
194 {
195  OnFind( aEvent );
196 }
197 
198 
199 void DIALOG_SCH_FIND::OnOptions( wxCommandEvent& aEvent )
200 {
201  int flags = 0;
202 
203  if( m_radioForward->GetValue() )
204  flags |= wxFR_DOWN;
205 
206  if( m_checkMatchCase->GetValue() )
207  flags |= wxFR_MATCHCASE;
208 
209  if( m_checkWholeWord->GetValue() )
210  flags |= wxFR_WHOLEWORD;
211 
212  if( m_checkWildcardMatch->IsShown() && m_checkWildcardMatch->GetValue() )
213  flags |= FR_MATCH_WILDCARD;
214 
215  if( m_checkAllFields->GetValue() )
216  flags |= FR_SEARCH_ALL_FIELDS;
217 
218  if( m_checkAllPins->GetValue() )
219  flags |= FR_SEARCH_ALL_PINS;
220 
221  if( m_checkCurrentSheetOnly->GetValue() )
222  flags |= FR_CURRENT_SHEET_ONLY;
223 
224  if( m_checkReplaceReferences->GetValue() )
225  flags |= FR_REPLACE_REFERENCES;
226 
227  m_findReplaceData->SetFlags( flags );
228  m_findDirty = true;
229 }
230 
231 
232 void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
233 {
234  int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
235 
236  if( index == wxNOT_FOUND )
237  {
238  m_comboFind->Insert( m_comboFind->GetValue(), 0 );
239  }
240  else if( index != 0 )
241  {
242  /* Move the search string to the top of the list if it isn't already there. */
243  wxString tmp = m_comboFind->GetValue();
244  m_comboFind->Delete( index );
245  m_comboFind->Insert( tmp, 0 );
246  m_comboFind->SetSelection( 0 );
247  }
248 
250 }
251 
252 
253 void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
254 {
255  int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
256 
257  if( index == wxNOT_FOUND )
258  {
259  m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
260  }
261  else if( index != 0 )
262  {
263  /* Move the search string to the top of the list if it isn't already there. */
264  wxString tmp = m_comboReplace->GetValue();
265  m_comboReplace->Delete( index );
266  m_comboReplace->Insert( tmp, 0 );
267  m_comboReplace->SetSelection( 0 );
268  }
269 
270  if( aEvent.GetId() == wxID_REPLACE )
272  else if( aEvent.GetId() == wxID_REPLACE_ALL )
274 }
275 
276 
277 wxArrayString DIALOG_SCH_FIND::GetFindEntries() const
278 {
279  int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
280 
281  if( index == wxNOT_FOUND )
282  {
283  m_comboFind->Insert( m_comboFind->GetValue(), 0 );
284  }
285  else if( index != 0 )
286  {
287  /* Move the search string to the top of the list if it isn't already there. */
288  wxString tmp = m_comboFind->GetValue();
289  m_comboFind->Delete( index );
290  m_comboFind->Insert( tmp, 0 );
291  }
292 
293  return m_comboFind->GetStrings();
294 }
295 
296 
297 void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries )
298 {
299  m_comboFind->Append( aEntries );
300 
301  if( m_comboFind->GetCount() )
302  {
303  m_comboFind->SetSelection( 0 );
304  m_comboFind->SelectAll();
305  }
306 }
307 
308 
309 void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
310 {
311  m_comboReplace->Append( aEntries );
312 
313  if( m_comboReplace->GetCount() )
314  {
315  m_comboReplace->SetSelection( 0 );
316  m_comboFind->SelectAll();
317  }
318 }
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:71
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
void SetPosition(const wxPoint &aNewPosition)
Force the position of the dialog to a new position.
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
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