KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_sch_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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <dialog_sch_find.h>
22#include <tool/actions.h>
23#include <sch_edit_frame.h>
25
26
28 const wxPoint& aPosition, const wxSize& aSize, int aStyle ) :
29 DIALOG_SCH_FIND_BASE( aParent, wxID_ANY, _( "Find" ), aPosition, aSize,
30 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | aStyle ),
31 m_frame( aParent ),
32 m_findReplaceTool( m_frame->GetToolManager()->GetTool<SCH_FIND_REPLACE_TOOL>() ),
33 m_findReplaceData( aData ),
34 m_findDirty( true )
35{
36 wxASSERT_MSG( m_findReplaceData, wxT( "can't create find dialog without data" ) );
37
40
41 if( aStyle & wxFR_REPLACEDIALOG )
42 {
43 SetTitle( _( "Find and Replace" ) );
44 m_buttonReplace->Show( true );
45 m_buttonReplaceAll->Show( true );
46 m_staticReplace->Show( true );
47 m_comboReplace->Show( true );
48 m_cbSelectedOnly->Show( true );
49 m_cbReplaceReferences->Show( true );
50 m_checkRegexMatch->Show( true );
51 }
52
53 if( m_frame->GetFrameType() == FRAME_SCH_SYMBOL_EDITOR )
54 {
55 m_findReplaceData->searchAllPins = true;
56
58 m_cbSearchPins->Hide();
59 m_cbSearchNetNames->Hide();
61
62 m_staticline1->Hide();
63 m_searchPanelLink->Hide();
64 }
65
66 m_checkMatchCase->SetValue( m_findReplaceData->matchCase );
69
70 m_cbSearchHiddenFields->SetValue( m_findReplaceData->searchAllFields );
71 m_cbReplaceReferences->SetValue( m_findReplaceData->replaceReferences );
72 m_cbSearchPins->SetValue( m_findReplaceData->searchAllPins );
73 m_cbSelectedOnly->SetValue( m_findReplaceData->searchSelectedOnly );
74
75 m_cbCurrentSheetOnly->SetValue( m_findReplaceData->searchCurrentSheetOnly );
76 m_cbCurrentSheetOnly->Enable( !m_findReplaceData->searchSelectedOnly );
77 m_cbSearchNetNames->SetValue( m_findReplaceData->searchNetNames );
78
79 if( int hotkey = ACTIONS::showSearch.GetHotKey() )
80 {
81 wxString hotkeyHint = wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( hotkey ) );
82 m_searchPanelLink->SetLabel( m_searchPanelLink->GetLabel() + hotkeyHint );
83 m_searchPanelLink->GetParent()->Layout();
84 }
85
86 m_buttonFind->SetDefault();
88
89 // Adjust the height of the dialog to prevent controls from being hidden when
90 // switching between the find and find/replace modes of the dialog. This ignores
91 // the users preferred height if any of the controls would be hidden.
92 GetSizer()->SetSizeHints( this );
93 wxSize size = aSize;
94
95 if( aSize != wxDefaultSize )
96 {
97 wxSize bestSize = GetBestSize();
98
99 if( size.GetHeight() != bestSize.GetHeight() )
100 size.SetHeight( bestSize.GetHeight() );
101 }
102
103 SetSize( size );
104
105 GetSizer()->Fit( this ); // Needed on Ubuntu/Unity to display the dialog
106
107 // Set up tab order for proper keyboard navigation.
108 // This is needed because wxComboBox on GTK has issues with Tab key navigation.
109 m_tabOrder = {
127 };
128}
129
130
131void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
132{
133 // Notify the SCH_BASE_FRAME
134 m_frame->OnFindDialogClose();
135}
136
137
138void DIALOG_SCH_FIND::OnIdle( wxIdleEvent& aEvent )
139{
140 if( m_findDirty )
141 {
142 m_findReplaceTool->UpdateFind( ACTIONS::updateFind.MakeEvent() );
143 m_findDirty = false;
144 }
145}
146
147
148void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
149{
150 wxCloseEvent dummy;
151 OnClose( dummy );
152}
153
154
155void DIALOG_SCH_FIND::OnCharHook( wxKeyEvent& aEvent )
156{
157 if( aEvent.GetKeyCode() == WXK_F3 )
158 {
159 updateFlags();
160 m_findReplaceData->findString = m_comboFind->GetValue();
161
162 if( aEvent.ShiftDown() )
163 m_findReplaceTool->FindNext( ACTIONS::findPrevious.MakeEvent() );
164 else
165 m_findReplaceTool->FindNext( ACTIONS::findNext.MakeEvent() );
166
167 return;
168 }
169
171}
172
173
174void DIALOG_SCH_FIND::onShowSearchPanel( wxHyperlinkEvent& event )
175{
176 wxCHECK2( m_frame->GetFrameType() == FRAME_SCH, /* void */ );
177
178 m_frame->GetToolManager()->RunAction( ACTIONS::showSearch );
179
180 Show( false );
181
182 CallAfter(
183 []()
184 {
185 if( wxWindow* frame = wxWindow::FindWindowByName( SCH_EDIT_FRAME_NAME ) )
186 static_cast<SCH_EDIT_FRAME*>( frame )->FocusSearch();
187 } );
188}
189
190
191void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
192{
193 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty()
194 && m_findReplaceTool->HasMatch() );
195}
196
197
198void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
199{
200 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
201}
202
203
204void DIALOG_SCH_FIND::OnSearchForText( wxCommandEvent& aEvent )
205{
206 m_findReplaceData->findString = m_comboFind->GetValue();
207 m_findDirty = true;
208}
209
210
211void DIALOG_SCH_FIND::OnSearchForSelect( wxCommandEvent& aEvent )
212{
213 m_findReplaceData->findString = m_comboFind->GetValue();
214
215 // Move the search string to the top of the list if it isn't already there.
216 if( aEvent.GetSelection() != 0 )
217 {
218 wxString tmp = m_comboFind->GetValue();
219 m_comboFind->Delete( aEvent.GetSelection() );
220 m_comboFind->Insert( tmp, 0 );
221 m_comboFind->SetSelection( 0 );
222 }
223
224 m_findReplaceTool->UpdateFind( ACTIONS::updateFind.MakeEvent() );
225}
226
227
228void DIALOG_SCH_FIND::OnReplaceWithText( wxCommandEvent& aEvent )
229{
230 m_findReplaceData->replaceString = m_comboReplace->GetValue();
231}
232
233
234void DIALOG_SCH_FIND::OnReplaceWithSelect( wxCommandEvent& aEvent )
235{
236 m_findReplaceData->replaceString = m_comboReplace->GetValue();
237
238 // Move the replace string to the top of the list if it isn't already there.
239 if( aEvent.GetSelection() != 0 )
240 {
241 wxString tmp = m_comboReplace->GetValue();
242 m_comboReplace->Delete( aEvent.GetSelection() );
243 m_comboReplace->Insert( tmp, 0 );
244 m_comboReplace->SetSelection( 0 );
245 }
246}
247
248
249void DIALOG_SCH_FIND::OnSearchForEnter( wxCommandEvent& aEvent )
250{
251 OnFind( aEvent );
252}
253
254
255void DIALOG_SCH_FIND::OnReplaceWithEnter( wxCommandEvent& aEvent )
256{
257 OnFind( aEvent );
258}
259
260
261void DIALOG_SCH_FIND::OnOptions( wxCommandEvent& aEvent )
262{
263 updateFlags();
264 m_findDirty = true;
265}
266
268{
269 // Rebuild the search flags in m_findReplaceData from dialog settings
270 m_findReplaceData->matchCase = m_checkMatchCase->GetValue();
271 m_findReplaceData->searchAllFields = m_cbSearchHiddenFields->GetValue();
272 m_findReplaceData->searchAllPins = m_cbSearchPins->GetValue();
273 m_findReplaceData->replaceReferences = m_cbReplaceReferences->GetValue();
274 m_findReplaceData->searchNetNames = m_cbSearchNetNames->GetValue();
275
276 // Only read the current-sheet-only widget when the user can actually toggle it.
277 // While selection-only forces it on, its value doesn't reflect user intent.
278 if( m_cbCurrentSheetOnly->IsEnabled() )
279 m_findReplaceData->searchCurrentSheetOnly = m_cbCurrentSheetOnly->GetValue();
280
281 if( m_checkWholeWord->GetValue() )
283 else if( m_checkRegexMatch->IsShown() && m_checkRegexMatch->GetValue() )
285 else
287
288 if( m_cbSelectedOnly->GetValue() )
289 {
290 m_cbCurrentSheetOnly->Enable( false );
291 m_findReplaceData->searchSelectedOnly = true;
292 }
293 else
294 {
295 m_cbCurrentSheetOnly->SetValue( m_findReplaceData->searchCurrentSheetOnly );
296 m_cbCurrentSheetOnly->Enable( true );
297 m_findReplaceData->searchSelectedOnly = false;
298 }
299}
300
301
302void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
303{
304 updateFlags(); // Ensure search flags are up to date
305
306 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
307
308 if( index == wxNOT_FOUND )
309 {
310 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
311 }
312 else if( index != 0 )
313 {
314 /* Move the search string to the top of the list if it isn't already there. */
315 wxString tmp = m_comboFind->GetValue();
316 m_comboFind->Delete( index );
317 m_comboFind->Insert( tmp, 0 );
318 m_comboFind->SetSelection( 0 );
319 }
320
321 m_findReplaceTool->FindNext( ACTIONS::findNext.MakeEvent() );
322}
323
324
325void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
326{
327 updateFlags(); // Ensure search flags are up to date
328
329 int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
330
331 if( index == wxNOT_FOUND )
332 {
333 m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
334 }
335 else if( index != 0 )
336 {
337 /* Move the search string to the top of the list if it isn't already there. */
338 wxString tmp = m_comboReplace->GetValue();
339 m_comboReplace->Delete( index );
340 m_comboReplace->Insert( tmp, 0 );
341 m_comboReplace->SetSelection( 0 );
342 }
343
344 if( aEvent.GetId() == wxID_REPLACE )
345 m_findReplaceTool->ReplaceAndFindNext( ACTIONS::replaceAndFindNext.MakeEvent() );
346 else if( aEvent.GetId() == wxID_REPLACE_ALL )
347 m_findReplaceTool->ReplaceAll( ACTIONS::replaceAll.MakeEvent() );
348}
349
350
352{
353 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
354
355 if( index == wxNOT_FOUND )
356 {
357 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
358 }
359 else if( index != 0 )
360 {
361 /* Move the search string to the top of the list if it isn't already there. */
362 wxString tmp = m_comboFind->GetValue();
363 m_comboFind->Delete( index );
364 m_comboFind->Insert( tmp, 0 );
365 }
366
367 return m_comboFind->GetStrings();
368}
369
370
371void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries, const wxString& aFindString )
372{
373 m_comboFind->Append( aEntries );
374
375 while( m_comboFind->GetCount() > 10 )
376 {
377 m_frame->GetFindHistoryList().pop_back();
378 m_comboFind->Delete( 9 );
379 }
380
381 if( !aFindString.IsEmpty() )
382 {
383 m_comboFind->SetValue( aFindString );
384 m_comboFind->SelectAll();
385 }
386 else if( m_comboFind->GetCount() )
387 {
388 m_comboFind->SetSelection( 0 );
389 m_comboFind->SelectAll();
390 }
391}
392
393
394void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
395{
396 m_comboReplace->Append( aEntries );
397
398 while( m_comboReplace->GetCount() > 10 )
399 {
400 m_frame->GetFindHistoryList().pop_back();
401 m_comboReplace->Delete( 9 );
402 }
403
404 if( m_comboReplace->GetCount() )
405 {
406 m_comboReplace->SetSelection( 0 );
407 m_comboFind->SelectAll();
408 }
409}
int index
static TOOL_ACTION replaceAll
Definition actions.h:119
static TOOL_ACTION updateFind
Definition actions.h:120
static TOOL_ACTION findPrevious
Definition actions.h:116
static TOOL_ACTION replaceAndFindNext
Definition actions.h:118
static TOOL_ACTION findNext
Definition actions.h:115
static TOOL_ACTION showSearch
Definition actions.h:112
wxCheckBox * m_cbSearchHiddenFields
wxRadioButton * m_radioForward
DIALOG_SCH_FIND_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Find"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
wxHyperlinkCtrl * m_searchPanelLink
wxRadioButton * m_radioBackward
void OnSearchForSelect(wxCommandEvent &aEvent) override
void OnReplaceWithEnter(wxCommandEvent &event) override
wxArrayString GetFindEntries() const
void OnIdle(wxIdleEvent &event) override
SCH_FIND_REPLACE_TOOL * m_findReplaceTool
SCH_SEARCH_DATA * m_findReplaceData
SCH_BASE_FRAME * m_frame
void OnUpdateReplaceUI(wxUpdateUIEvent &aEvent) override
void SetReplaceEntries(const wxArrayString &aEntries)
void SetFindEntries(const wxArrayString &aEntries, const wxString &aFindString)
void OnReplaceWithText(wxCommandEvent &aEvent) override
void OnCharHook(wxKeyEvent &aEvent) override
void OnReplaceWithSelect(wxCommandEvent &aEvent) override
DIALOG_SCH_FIND(SCH_BASE_FRAME *aParent, SCH_SEARCH_DATA *aData, const wxPoint &aPosition=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, int aStyle=0)
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 onShowSearchPanel(wxHyperlinkEvent &event) override
std::vector< wxWindow * > m_tabOrder
bool Show(bool show) override
void OptOut(wxWindow *aWindow)
Opt out of control state saving.
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:94
virtual void OnCharHook(wxKeyEvent &aEvt)
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
Schematic editor (Eeschema) main window.
Handle actions specific to the schematic editor.
#define _(s)
#define SCH_EDIT_FRAME_NAME
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
@ FRAME_SCH
Definition frame_type.h:30
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
std::vector< FAB_LAYER_COLOR > dummy