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  // Notify the controller
101  m_findDirty = true;
102 }
103 
104 
105 void DIALOG_SCH_FIND::OnIdle( wxIdleEvent& aEvent )
106 {
107  if( m_findDirty )
108  {
110  m_findDirty = false;
111  }
112 }
113 
114 
115 void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
116 {
117  wxCloseEvent dummy;
118  OnClose( dummy );
119 }
120 
121 
122 void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
123 {
124  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() &&
126 }
127 
128 
129 void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
130 {
131  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
132 }
133 
134 
135 void DIALOG_SCH_FIND::OnChar( wxKeyEvent& aEvent )
136 {
137  if( aEvent.GetKeyCode() == WXK_RETURN )
138  {
139  wxCommandEvent dummyCommand;
140  OnFind( dummyCommand );
141  }
142 }
143 
144 
145 void DIALOG_SCH_FIND::OnSearchForText( wxCommandEvent& aEvent )
146 {
147  m_findReplaceData->SetFindString( m_comboFind->GetValue() );
148  m_findDirty = true;
149 }
150 
151 
152 void DIALOG_SCH_FIND::OnSearchForSelect( wxCommandEvent& aEvent )
153 {
154  m_findReplaceData->SetFindString( m_comboFind->GetValue() );
155 
156  // Move the search string to the top of the list if it isn't already there.
157  if( aEvent.GetSelection() != 0 )
158  {
159  wxString tmp = m_comboFind->GetValue();
160  m_comboFind->Delete( aEvent.GetSelection() );
161  m_comboFind->Insert( tmp, 0 );
162  m_comboFind->SetSelection( 0 );
163  }
164 
166 }
167 
168 
169 void DIALOG_SCH_FIND::OnReplaceWithText( wxCommandEvent& aEvent )
170 {
171  m_findReplaceData->SetReplaceString( m_comboReplace->GetValue() );
172 }
173 
174 
175 void DIALOG_SCH_FIND::OnReplaceWithSelect( wxCommandEvent& aEvent )
176 {
177  m_findReplaceData->SetReplaceString( m_comboReplace->GetValue() );
178 
179  // Move the replace string to the top of the list if it isn't already there.
180  if( aEvent.GetSelection() != 0 )
181  {
182  wxString tmp = m_comboReplace->GetValue();
183  m_comboReplace->Delete( aEvent.GetSelection() );
184  m_comboReplace->Insert( tmp, 0 );
185  m_comboReplace->SetSelection( 0 );
186  }
187 }
188 
189 
190 void DIALOG_SCH_FIND::OnSearchForEnter( wxCommandEvent& aEvent )
191 {
192  OnFind( aEvent );
193 }
194 
195 
196 void DIALOG_SCH_FIND::OnReplaceWithEnter( wxCommandEvent& aEvent )
197 {
198  OnFind( aEvent );
199 }
200 
201 
202 void DIALOG_SCH_FIND::OnOptions( wxCommandEvent& aEvent )
203 {
204  int flags = 0;
205 
206  if( m_radioForward->GetValue() )
207  flags |= wxFR_DOWN;
208 
209  if( m_checkMatchCase->GetValue() )
210  flags |= wxFR_MATCHCASE;
211 
212  if( m_checkWholeWord->GetValue() )
213  flags |= wxFR_WHOLEWORD;
214 
215  if( m_checkWildcardMatch->IsShown() && m_checkWildcardMatch->GetValue() )
216  flags |= FR_MATCH_WILDCARD;
217 
218  if( m_checkAllFields->GetValue() )
219  flags |= FR_SEARCH_ALL_FIELDS;
220 
221  if( m_checkAllPins->GetValue() )
222  flags |= FR_SEARCH_ALL_PINS;
223 
224  if( m_checkCurrentSheetOnly->GetValue() )
225  flags |= FR_CURRENT_SHEET_ONLY;
226 
227  if( m_checkReplaceReferences->GetValue() )
228  flags |= FR_REPLACE_REFERENCES;
229 
230  m_findReplaceData->SetFlags( flags );
231  m_findDirty = true;
232 }
233 
234 
235 void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
236 {
237  int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
238 
239  if( index == wxNOT_FOUND )
240  {
241  m_comboFind->Insert( m_comboFind->GetValue(), 0 );
242  }
243  else if( index != 0 )
244  {
245  /* Move the search string to the top of the list if it isn't already there. */
246  wxString tmp = m_comboFind->GetValue();
247  m_comboFind->Delete( index );
248  m_comboFind->Insert( tmp, 0 );
249  m_comboFind->SetSelection( 0 );
250  }
251 
253 }
254 
255 
256 void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
257 {
258  int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
259 
260  if( index == wxNOT_FOUND )
261  {
262  m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
263  }
264  else if( index != 0 )
265  {
266  /* Move the search string to the top of the list if it isn't already there. */
267  wxString tmp = m_comboReplace->GetValue();
268  m_comboReplace->Delete( index );
269  m_comboReplace->Insert( tmp, 0 );
270  m_comboReplace->SetSelection( 0 );
271  }
272 
273  if( aEvent.GetId() == wxID_REPLACE )
275  else if( aEvent.GetId() == wxID_REPLACE_ALL )
277 }
278 
279 
280 wxArrayString DIALOG_SCH_FIND::GetFindEntries() const
281 {
282  return m_comboFind->GetStrings();
283 }
284 
285 
286 void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries )
287 {
288  m_comboFind->Append( aEntries );
289 
290  if( m_comboFind->GetCount() )
291  {
292  m_comboFind->SetSelection( 0 );
293  m_comboFind->SelectAll();
294  }
295 }
296 
297 
298 void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
299 {
300  m_comboReplace->Append( aEntries );
301 
302  if( m_comboReplace->GetCount() )
303  {
304  m_comboReplace->SetSelection( 0 );
305  m_comboFind->SelectAll();
306  }
307 }
void OnUpdateReplaceAllUI(wxUpdateUIEvent &aEvent) override
wxArrayString GetFindEntries() const
void SetPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aX, int *aY, const wxString &aActualConversion)
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:83
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:114
static TOOL_ACTION updateFind
Definition: actions.h:84
SCH_EDITOR_CONTROL.
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)
void OnClose(wxCloseEvent &aEvent) override
void OnUpdateReplaceUI(wxUpdateUIEvent &aEvent) override
static TOOL_ACTION replaceAndFindNext
Definition: actions.h:82
void SetReplaceEntries(const wxArrayString &aEntries)
void OnOptions(wxCommandEvent &event) override
void SetFindEntries(const wxArrayString &aEntries)
void OnFind(wxCommandEvent &aEvent) override
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
#define _(s)
Definition: 3d_actions.cpp:33
TOOL_EVENT MakeEvent() const
Function MakeEvent() Returns the event associated with the action (i.e.
Definition: tool_action.h:107
SCH_EDIT_FRAME * m_frame
void OnCancel(wxCommandEvent &aEvent) override
static TOOL_ACTION findNext
Definition: actions.h:80