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