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 }
84
85 m_buttonFind->SetDefault();
87
88 // Adjust the height of the dialog to prevent controls from being hidden when
89 // switching between the find and find/replace modes of the dialog. This ignores
90 // the users preferred height if any of the controls would be hidden.
91 GetSizer()->SetSizeHints( this );
92 wxSize size = aSize;
93
94 if( aSize != wxDefaultSize )
95 {
96 wxSize bestSize = GetBestSize();
97
98 if( size.GetHeight() != bestSize.GetHeight() )
99 size.SetHeight( bestSize.GetHeight() );
100 }
101
102 SetSize( size );
103
104 GetSizer()->Fit( this ); // Needed on Ubuntu/Unity to display the dialog
105
106 // Set up tab order for proper keyboard navigation.
107 // This is needed because wxComboBox on GTK has issues with Tab key navigation.
108 m_tabOrder = {
126 };
127}
128
129
130void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
131{
132 // Notify the SCH_BASE_FRAME
133 m_frame->OnFindDialogClose();
134}
135
136
137void DIALOG_SCH_FIND::OnIdle( wxIdleEvent& aEvent )
138{
139 if( m_findDirty )
140 {
141 m_findReplaceTool->UpdateFind( ACTIONS::updateFind.MakeEvent() );
142 m_findDirty = false;
143 }
144}
145
146
147void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
148{
149 wxCloseEvent dummy;
150 OnClose( dummy );
151}
152
153
154void DIALOG_SCH_FIND::OnCharHook( wxKeyEvent& aEvent )
155{
156 if( aEvent.GetKeyCode() == WXK_F3 )
157 {
158 updateFlags();
159 m_findReplaceData->findString = m_comboFind->GetValue();
160
161 if( aEvent.ShiftDown() )
162 m_findReplaceTool->FindNext( ACTIONS::findPrevious.MakeEvent() );
163 else
164 m_findReplaceTool->FindNext( ACTIONS::findNext.MakeEvent() );
165
166 return;
167 }
168
170}
171
172
173void DIALOG_SCH_FIND::onShowSearchPanel( wxHyperlinkEvent& event )
174{
175 wxCHECK2( m_frame->GetFrameType() == FRAME_SCH, /* void */ );
176
177 m_frame->GetToolManager()->RunAction( ACTIONS::showSearch );
178
179 Show( false );
180
181 CallAfter(
182 []()
183 {
184 if( wxWindow* frame = wxWindow::FindWindowByName( SCH_EDIT_FRAME_NAME ) )
185 static_cast<SCH_EDIT_FRAME*>( frame )->FocusSearch();
186 } );
187}
188
189
190void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
191{
192 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty()
193 && m_findReplaceTool->HasMatch() );
194}
195
196
197void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
198{
199 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
200}
201
202
203void DIALOG_SCH_FIND::OnSearchForText( wxCommandEvent& aEvent )
204{
205 m_findReplaceData->findString = m_comboFind->GetValue();
206 m_findDirty = true;
207}
208
209
210void DIALOG_SCH_FIND::OnSearchForSelect( wxCommandEvent& aEvent )
211{
212 m_findReplaceData->findString = m_comboFind->GetValue();
213
214 // Move the search string to the top of the list if it isn't already there.
215 if( aEvent.GetSelection() != 0 )
216 {
217 wxString tmp = m_comboFind->GetValue();
218 m_comboFind->Delete( aEvent.GetSelection() );
219 m_comboFind->Insert( tmp, 0 );
220 m_comboFind->SetSelection( 0 );
221 }
222
223 m_findReplaceTool->UpdateFind( ACTIONS::updateFind.MakeEvent() );
224}
225
226
227void DIALOG_SCH_FIND::OnReplaceWithText( wxCommandEvent& aEvent )
228{
229 m_findReplaceData->replaceString = m_comboReplace->GetValue();
230}
231
232
233void DIALOG_SCH_FIND::OnReplaceWithSelect( wxCommandEvent& aEvent )
234{
235 m_findReplaceData->replaceString = m_comboReplace->GetValue();
236
237 // Move the replace string to the top of the list if it isn't already there.
238 if( aEvent.GetSelection() != 0 )
239 {
240 wxString tmp = m_comboReplace->GetValue();
241 m_comboReplace->Delete( aEvent.GetSelection() );
242 m_comboReplace->Insert( tmp, 0 );
243 m_comboReplace->SetSelection( 0 );
244 }
245}
246
247
248void DIALOG_SCH_FIND::OnSearchForEnter( wxCommandEvent& aEvent )
249{
250 OnFind( aEvent );
251}
252
253
254void DIALOG_SCH_FIND::OnReplaceWithEnter( wxCommandEvent& aEvent )
255{
256 OnFind( aEvent );
257}
258
259
260void DIALOG_SCH_FIND::OnOptions( wxCommandEvent& aEvent )
261{
262 updateFlags();
263 m_findDirty = true;
264}
265
267{
268 // Rebuild the search flags in m_findReplaceData from dialog settings
269 m_findReplaceData->matchCase = m_checkMatchCase->GetValue();
270 m_findReplaceData->searchAllFields = m_cbSearchHiddenFields->GetValue();
271 m_findReplaceData->searchAllPins = m_cbSearchPins->GetValue();
272 m_findReplaceData->replaceReferences = m_cbReplaceReferences->GetValue();
273 m_findReplaceData->searchNetNames = m_cbSearchNetNames->GetValue();
274
275 // Only read the current-sheet-only widget when the user can actually toggle it.
276 // While selection-only forces it on, its value doesn't reflect user intent.
277 if( m_cbCurrentSheetOnly->IsEnabled() )
278 m_findReplaceData->searchCurrentSheetOnly = m_cbCurrentSheetOnly->GetValue();
279
280 if( m_checkWholeWord->GetValue() )
282 else if( m_checkRegexMatch->IsShown() && m_checkRegexMatch->GetValue() )
284 else
286
287 if( m_cbSelectedOnly->GetValue() )
288 {
289 m_cbCurrentSheetOnly->Enable( false );
290 m_findReplaceData->searchSelectedOnly = true;
291 }
292 else
293 {
294 m_cbCurrentSheetOnly->SetValue( m_findReplaceData->searchCurrentSheetOnly );
295 m_cbCurrentSheetOnly->Enable( true );
296 m_findReplaceData->searchSelectedOnly = false;
297 }
298}
299
300
301void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
302{
303 updateFlags(); // Ensure search flags are up to date
304
305 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
306
307 if( index == wxNOT_FOUND )
308 {
309 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
310 }
311 else if( index != 0 )
312 {
313 /* Move the search string to the top of the list if it isn't already there. */
314 wxString tmp = m_comboFind->GetValue();
315 m_comboFind->Delete( index );
316 m_comboFind->Insert( tmp, 0 );
317 m_comboFind->SetSelection( 0 );
318 }
319
320 m_findReplaceTool->FindNext( ACTIONS::findNext.MakeEvent() );
321}
322
323
324void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
325{
326 updateFlags(); // Ensure search flags are up to date
327
328 int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
329
330 if( index == wxNOT_FOUND )
331 {
332 m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
333 }
334 else if( index != 0 )
335 {
336 /* Move the search string to the top of the list if it isn't already there. */
337 wxString tmp = m_comboReplace->GetValue();
338 m_comboReplace->Delete( index );
339 m_comboReplace->Insert( tmp, 0 );
340 m_comboReplace->SetSelection( 0 );
341 }
342
343 if( aEvent.GetId() == wxID_REPLACE )
344 m_findReplaceTool->ReplaceAndFindNext( ACTIONS::replaceAndFindNext.MakeEvent() );
345 else if( aEvent.GetId() == wxID_REPLACE_ALL )
346 m_findReplaceTool->ReplaceAll( ACTIONS::replaceAll.MakeEvent() );
347}
348
349
351{
352 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
353
354 if( index == wxNOT_FOUND )
355 {
356 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
357 }
358 else if( index != 0 )
359 {
360 /* Move the search string to the top of the list if it isn't already there. */
361 wxString tmp = m_comboFind->GetValue();
362 m_comboFind->Delete( index );
363 m_comboFind->Insert( tmp, 0 );
364 }
365
366 return m_comboFind->GetStrings();
367}
368
369
370void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries, const wxString& aFindString )
371{
372 m_comboFind->Append( aEntries );
373
374 while( m_comboFind->GetCount() > 10 )
375 {
376 m_frame->GetFindHistoryList().pop_back();
377 m_comboFind->Delete( 9 );
378 }
379
380 if( !aFindString.IsEmpty() )
381 {
382 m_comboFind->SetValue( aFindString );
383 m_comboFind->SelectAll();
384 }
385 else if( m_comboFind->GetCount() )
386 {
387 m_comboFind->SetSelection( 0 );
388 m_comboFind->SelectAll();
389 }
390}
391
392
393void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
394{
395 m_comboReplace->Append( aEntries );
396
397 while( m_comboReplace->GetCount() > 10 )
398 {
399 m_frame->GetFindHistoryList().pop_back();
400 m_comboReplace->Delete( 9 );
401 }
402
403 if( m_comboReplace->GetCount() )
404 {
405 m_comboReplace->SetSelection( 0 );
406 m_comboFind->SelectAll();
407 }
408}
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:79
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