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 <[email protected]>
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
26#include <tool/actions.h>
27#include <sch_edit_frame.h>
29
30
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_findReplaceTool( m_frame->GetToolManager()->GetTool<SCH_FIND_REPLACE_TOOL>() ),
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_checkSelectedOnly->Show( true );
50 m_checkReplaceReferences->Show( true );
51 m_checkWildcardMatch->Show( false ); // Wildcard replace is not implemented.
52 }
53
58
65
66 m_buttonFind->SetDefault();
68
69 // Adjust the height of the dialog to prevent controls from being hidden when
70 // switching between the find and find/replace modes of the dialog. This ignores
71 // the users preferred height if any of the controls would be hidden.
72 GetSizer()->SetSizeHints( this );
73 wxSize size = aSize;
74
75 if( aSize != wxDefaultSize )
76 {
77 wxSize bestSize = GetBestSize();
78
79 if( size.GetHeight() != bestSize.GetHeight() )
80 size.SetHeight( bestSize.GetHeight() );
81 }
82
83 SetSize( size );
84
85 GetSizer()->Fit( this ); // Needed on Ubuntu/Unity to display the dialog
86
87 Connect( wxEVT_CHAR, wxKeyEventHandler( DIALOG_SCH_FIND::OnChar ), nullptr, this );
88}
89
90
91void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
92{
93 // Notify the SCH_EDIT_FRAME
95}
96
97
98void DIALOG_SCH_FIND::OnIdle( wxIdleEvent& aEvent )
99{
100 if( m_findDirty )
101 {
103 m_findDirty = false;
104 }
105}
106
107
108void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
109{
110 wxCloseEvent dummy;
111 OnClose( dummy );
112}
113
114
115void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
116{
117 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty()
119}
120
121
122void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
123{
124 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
125}
126
127
128void DIALOG_SCH_FIND::OnChar( wxKeyEvent& aEvent )
129{
130 if( aEvent.GetKeyCode() == WXK_RETURN )
131 {
132 wxCommandEvent dummyCommand;
133 OnFind( dummyCommand );
134 }
135}
136
137
138void DIALOG_SCH_FIND::OnSearchForText( wxCommandEvent& aEvent )
139{
141 m_findDirty = true;
142}
143
144
145void DIALOG_SCH_FIND::OnSearchForSelect( wxCommandEvent& aEvent )
146{
148
149 // Move the search string to the top of the list if it isn't already there.
150 if( aEvent.GetSelection() != 0 )
151 {
152 wxString tmp = m_comboFind->GetValue();
153 m_comboFind->Delete( aEvent.GetSelection() );
154 m_comboFind->Insert( tmp, 0 );
155 m_comboFind->SetSelection( 0 );
156 }
157
159}
160
161
162void DIALOG_SCH_FIND::OnReplaceWithText( wxCommandEvent& aEvent )
163{
165}
166
167
168void DIALOG_SCH_FIND::OnReplaceWithSelect( wxCommandEvent& aEvent )
169{
171
172 // Move the replace string to the top of the list if it isn't already there.
173 if( aEvent.GetSelection() != 0 )
174 {
175 wxString tmp = m_comboReplace->GetValue();
176 m_comboReplace->Delete( aEvent.GetSelection() );
177 m_comboReplace->Insert( tmp, 0 );
178 m_comboReplace->SetSelection( 0 );
179 }
180}
181
182
183void DIALOG_SCH_FIND::OnSearchForEnter( wxCommandEvent& aEvent )
184{
185 OnFind( aEvent );
186}
187
188
189void DIALOG_SCH_FIND::OnReplaceWithEnter( wxCommandEvent& aEvent )
190{
191 OnFind( aEvent );
192}
193
194
195void DIALOG_SCH_FIND::OnOptions( wxCommandEvent& aEvent )
196{
197 updateFlags();
198 m_findDirty = true;
199}
200
202{
203 // Rebuild the search flags in m_findReplaceData from dialog settings
209
210 if( m_checkWholeWord->GetValue() )
212 else if( m_checkWildcardMatch->IsShown() && m_checkWildcardMatch->GetValue() )
214 else
216
217 if( m_checkSelectedOnly->GetValue() )
218 {
219 m_checkCurrentSheetOnly->SetValue( true );
220 m_checkCurrentSheetOnly->Enable( false );
222 }
223 else
224 {
225 m_checkCurrentSheetOnly->Enable( true );
227 }
228}
229
230
231void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
232{
233 updateFlags(); // Ensure search flags are up to date
234
235 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
236
237 if( index == wxNOT_FOUND )
238 {
239 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
240 }
241 else if( index != 0 )
242 {
243 /* Move the search string to the top of the list if it isn't already there. */
244 wxString tmp = m_comboFind->GetValue();
245 m_comboFind->Delete( index );
246 m_comboFind->Insert( tmp, 0 );
247 m_comboFind->SetSelection( 0 );
248 }
249
251}
252
253
254void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
255{
256 updateFlags(); // Ensure search flags are up to date
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
281{
282 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
283
284 if( index == wxNOT_FOUND )
285 {
286 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
287 }
288 else if( index != 0 )
289 {
290 /* Move the search string to the top of the list if it isn't already there. */
291 wxString tmp = m_comboFind->GetValue();
292 m_comboFind->Delete( index );
293 m_comboFind->Insert( tmp, 0 );
294 }
295
296 return m_comboFind->GetStrings();
297}
298
299
300void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries, const wxString& aFindString )
301{
302 m_comboFind->Append( aEntries );
303
304 while( m_comboFind->GetCount() > 10 )
305 {
306 m_frame->GetFindHistoryList().pop_back();
307 m_comboFind->Delete( 9 );
308 }
309
310 if( !aFindString.IsEmpty() )
311 {
312 m_comboFind->SetValue( aFindString );
313 m_comboFind->SelectAll();
314 }
315 else if( m_comboFind->GetCount() )
316 {
317 m_comboFind->SetSelection( 0 );
318 m_comboFind->SelectAll();
319 }
320}
321
322
323void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
324{
325 m_comboReplace->Append( aEntries );
326
327 while( m_comboReplace->GetCount() > 10 )
328 {
329 m_frame->GetFindHistoryList().pop_back();
330 m_comboReplace->Delete( 9 );
331 }
332
333 if( m_comboReplace->GetCount() )
334 {
335 m_comboReplace->SetSelection( 0 );
336 m_comboFind->SelectAll();
337 }
338}
static TOOL_ACTION replaceAll
Definition: actions.h:84
static TOOL_ACTION updateFind
Definition: actions.h:85
static TOOL_ACTION replaceAndFindNext
Definition: actions.h:83
static TOOL_ACTION findNext
Definition: actions.h:80
Class DIALOG_SCH_FIND_BASE.
void OnSearchForSelect(wxCommandEvent &aEvent) override
SCH_EDIT_FRAME * m_frame
DIALOG_SCH_FIND(SCH_EDIT_FRAME *aParent, SCH_SEARCH_DATA *aData, const wxPoint &aPosition=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, int aStyle=0)
void OnReplaceWithEnter(wxCommandEvent &event) override
wxArrayString GetFindEntries() const
void OnIdle(wxIdleEvent &event) override
SCH_FIND_REPLACE_TOOL * m_findReplaceTool
SCH_SEARCH_DATA * m_findReplaceData
void OnUpdateReplaceUI(wxUpdateUIEvent &aEvent) override
void SetReplaceEntries(const wxArrayString &aEntries)
void SetFindEntries(const wxArrayString &aEntries, const wxString &aFindString)
void OnChar(wxKeyEvent &aEvent)
void OnReplaceWithText(wxCommandEvent &aEvent) override
void OnReplaceWithSelect(wxCommandEvent &aEvent) override
void OnUpdateReplaceAllUI(wxUpdateUIEvent &aEvent) override
void OnSearchForText(wxCommandEvent &aEvent) override
void OnClose(wxCloseEvent &aEvent) override
void OnReplace(wxCommandEvent &aEvent) override
void OnSearchForEnter(wxCommandEvent &event) override
void OnFind(wxCommandEvent &aEvent) override
void OnCancel(wxCommandEvent &aEvent) override
void OnOptions(wxCommandEvent &event) override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:97
wxArrayString & GetFindHistoryList()
Schematic editor (Eeschema) main window.
void OnFindDialogClose()
Notification that the Find dialog has closed.
Handle actions specific to the schematic editor.
int UpdateFind(const TOOL_EVENT &aEvent)
int FindNext(const TOOL_EVENT &aEvent)
int ReplaceAll(const TOOL_EVENT &aEvent)
int ReplaceAndFindNext(const TOOL_EVENT &aEvent)
#define _(s)
std::vector< FAB_LAYER_COLOR > dummy
EDA_SEARCH_MATCH_MODE matchMode
wxString replaceString