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
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_cbSelectedOnly->Show( true );
50 m_cbReplaceReferences->Show( true );
51 m_checkRegexMatch->Show( true );
52 }
53
54 if( m_frame->GetFrameType() == FRAME_SCH_SYMBOL_EDITOR )
55 {
56 m_findReplaceData->searchAllPins = true;
57
59 m_cbSearchPins->Hide();
60 m_cbSearchNetNames->Hide();
62
63 m_staticline1->Hide();
64 m_searchPanelLink->Hide();
65 }
66
67 m_checkMatchCase->SetValue( m_findReplaceData->matchCase );
70
71 m_cbSearchHiddenFields->SetValue( m_findReplaceData->searchAllFields );
72 m_cbReplaceReferences->SetValue( m_findReplaceData->replaceReferences );
73 m_cbSearchPins->SetValue( m_findReplaceData->searchAllPins );
74 m_cbCurrentSheetOnly->SetValue( m_findReplaceData->searchCurrentSheetOnly );
75 m_cbCurrentSheetOnly->Enable( !m_findReplaceData->searchSelectedOnly );
76 m_cbSelectedOnly->SetValue( 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
107
108void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
109{
110 // Notify the SCH_BASE_FRAME
111 m_frame->OnFindDialogClose();
112}
113
114
115void DIALOG_SCH_FIND::OnIdle( wxIdleEvent& aEvent )
116{
117 if( m_findDirty )
118 {
119 m_findReplaceTool->UpdateFind( ACTIONS::updateFind.MakeEvent() );
120 m_findDirty = false;
121 }
122}
123
124
125void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
126{
127 wxCloseEvent dummy;
128 OnClose( dummy );
129}
130
131
132void DIALOG_SCH_FIND::onShowSearchPanel( wxHyperlinkEvent& event )
133{
134 wxCHECK2( m_frame->GetFrameType() == FRAME_SCH, /* void */ );
135
136 m_frame->GetToolManager()->RunAction( ACTIONS::showSearch );
137
138 EndModal( wxID_CANCEL );
139
140 CallAfter(
141 []()
142 {
143 if( wxWindow* frame = wxWindow::FindWindowByName( SCH_EDIT_FRAME_NAME ) )
144 static_cast<SCH_EDIT_FRAME*>( frame )->FocusSearch();
145 } );
146}
147
148
149void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
150{
151 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty()
152 && m_findReplaceTool->HasMatch() );
153}
154
155
156void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
157{
158 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
159}
160
161
162void DIALOG_SCH_FIND::OnSearchForText( wxCommandEvent& aEvent )
163{
164 m_findReplaceData->findString = m_comboFind->GetValue();
165 m_findDirty = true;
166}
167
168
169void DIALOG_SCH_FIND::OnSearchForSelect( wxCommandEvent& aEvent )
170{
171 m_findReplaceData->findString = m_comboFind->GetValue();
172
173 // Move the search string to the top of the list if it isn't already there.
174 if( aEvent.GetSelection() != 0 )
175 {
176 wxString tmp = m_comboFind->GetValue();
177 m_comboFind->Delete( aEvent.GetSelection() );
178 m_comboFind->Insert( tmp, 0 );
179 m_comboFind->SetSelection( 0 );
180 }
181
182 m_findReplaceTool->UpdateFind( ACTIONS::updateFind.MakeEvent() );
183}
184
185
186void DIALOG_SCH_FIND::OnReplaceWithText( wxCommandEvent& aEvent )
187{
188 m_findReplaceData->replaceString = m_comboReplace->GetValue();
189}
190
191
192void DIALOG_SCH_FIND::OnReplaceWithSelect( wxCommandEvent& aEvent )
193{
194 m_findReplaceData->replaceString = m_comboReplace->GetValue();
195
196 // Move the replace string to the top of the list if it isn't already there.
197 if( aEvent.GetSelection() != 0 )
198 {
199 wxString tmp = m_comboReplace->GetValue();
200 m_comboReplace->Delete( aEvent.GetSelection() );
201 m_comboReplace->Insert( tmp, 0 );
202 m_comboReplace->SetSelection( 0 );
203 }
204}
205
206
207void DIALOG_SCH_FIND::OnSearchForEnter( wxCommandEvent& aEvent )
208{
209 OnFind( aEvent );
210}
211
212
213void DIALOG_SCH_FIND::OnReplaceWithEnter( wxCommandEvent& aEvent )
214{
215 OnFind( aEvent );
216}
217
218
219void DIALOG_SCH_FIND::OnOptions( wxCommandEvent& aEvent )
220{
221 updateFlags();
222 m_findDirty = true;
223}
224
226{
227 // Rebuild the search flags in m_findReplaceData from dialog settings
228 m_findReplaceData->matchCase = m_checkMatchCase->GetValue();
229 m_findReplaceData->searchAllFields = m_cbSearchHiddenFields->GetValue();
230 m_findReplaceData->searchAllPins = m_cbSearchPins->GetValue();
231 m_findReplaceData->searchCurrentSheetOnly = m_cbCurrentSheetOnly->GetValue();
232 m_findReplaceData->replaceReferences = m_cbReplaceReferences->GetValue();
233 m_findReplaceData->searchNetNames = m_cbSearchNetNames->GetValue();
234
235 if( m_checkWholeWord->GetValue() )
237 else if( m_checkRegexMatch->IsShown() && m_checkRegexMatch->GetValue() )
239 else
241
242 if( m_cbSelectedOnly->GetValue() )
243 {
244 m_cbCurrentSheetOnly->SetValue( true );
245 m_cbCurrentSheetOnly->Enable( false );
246 m_findReplaceData->searchSelectedOnly = true;
247 }
248 else
249 {
250 m_cbCurrentSheetOnly->Enable( true );
251 m_findReplaceData->searchSelectedOnly = false;
252 }
253}
254
255
256void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
257{
258 updateFlags(); // Ensure search flags are up to date
259
260 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
261
262 if( index == wxNOT_FOUND )
263 {
264 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
265 }
266 else if( index != 0 )
267 {
268 /* Move the search string to the top of the list if it isn't already there. */
269 wxString tmp = m_comboFind->GetValue();
270 m_comboFind->Delete( index );
271 m_comboFind->Insert( tmp, 0 );
272 m_comboFind->SetSelection( 0 );
273 }
274
275 m_findReplaceTool->FindNext( ACTIONS::findNext.MakeEvent() );
276}
277
278
279void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
280{
281 updateFlags(); // Ensure search flags are up to date
282
283 int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
284
285 if( index == wxNOT_FOUND )
286 {
287 m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
288 }
289 else if( index != 0 )
290 {
291 /* Move the search string to the top of the list if it isn't already there. */
292 wxString tmp = m_comboReplace->GetValue();
293 m_comboReplace->Delete( index );
294 m_comboReplace->Insert( tmp, 0 );
295 m_comboReplace->SetSelection( 0 );
296 }
297
298 if( aEvent.GetId() == wxID_REPLACE )
299 m_findReplaceTool->ReplaceAndFindNext( ACTIONS::replaceAndFindNext.MakeEvent() );
300 else if( aEvent.GetId() == wxID_REPLACE_ALL )
301 m_findReplaceTool->ReplaceAll( ACTIONS::replaceAll.MakeEvent() );
302}
303
304
306{
307 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
308
309 if( index == wxNOT_FOUND )
310 {
311 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
312 }
313 else if( index != 0 )
314 {
315 /* Move the search string to the top of the list if it isn't already there. */
316 wxString tmp = m_comboFind->GetValue();
317 m_comboFind->Delete( index );
318 m_comboFind->Insert( tmp, 0 );
319 }
320
321 return m_comboFind->GetStrings();
322}
323
324
325void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries, const wxString& aFindString )
326{
327 m_comboFind->Append( aEntries );
328
329 while( m_comboFind->GetCount() > 10 )
330 {
331 m_frame->GetFindHistoryList().pop_back();
332 m_comboFind->Delete( 9 );
333 }
334
335 if( !aFindString.IsEmpty() )
336 {
337 m_comboFind->SetValue( aFindString );
338 m_comboFind->SelectAll();
339 }
340 else if( m_comboFind->GetCount() )
341 {
342 m_comboFind->SetSelection( 0 );
343 m_comboFind->SelectAll();
344 }
345}
346
347
348void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
349{
350 m_comboReplace->Append( aEntries );
351
352 while( m_comboReplace->GetCount() > 10 )
353 {
354 m_frame->GetFindHistoryList().pop_back();
355 m_comboReplace->Delete( 9 );
356 }
357
358 if( m_comboReplace->GetCount() )
359 {
360 m_comboReplace->SetSelection( 0 );
361 m_comboFind->SelectAll();
362 }
363}
static TOOL_ACTION replaceAll
Definition actions.h:122
static TOOL_ACTION updateFind
Definition actions.h:123
static TOOL_ACTION replaceAndFindNext
Definition actions.h:121
static TOOL_ACTION findNext
Definition actions.h:118
static TOOL_ACTION showSearch
Definition actions.h:115
wxCheckBox * m_cbSearchHiddenFields
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
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
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