KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <stambaughw@verizon.net>
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
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_checkRegexMatch->Show( true );
52 }
53
55 m_checkWholeWord->SetValue( m_findReplaceData->matchMode == EDA_SEARCH_MATCH_MODE::WHOLEWORD );
56 m_checkRegexMatch->SetValue( m_findReplaceData->matchMode == EDA_SEARCH_MATCH_MODE::REGEX );
57
65
66 if( int hotkey = ACTIONS::showSearch.GetHotKey() )
67 {
68 wxString hotkeyHint = wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( hotkey ) );
69 m_searchPanelLink->SetLabel( m_searchPanelLink->GetLabel() + hotkeyHint );
70 }
71
72 m_buttonFind->SetDefault();
74
75 // Adjust the height of the dialog to prevent controls from being hidden when
76 // switching between the find and find/replace modes of the dialog. This ignores
77 // the users preferred height if any of the controls would be hidden.
78 GetSizer()->SetSizeHints( this );
79 wxSize size = aSize;
80
81 if( aSize != wxDefaultSize )
82 {
83 wxSize bestSize = GetBestSize();
84
85 if( size.GetHeight() != bestSize.GetHeight() )
86 size.SetHeight( bestSize.GetHeight() );
87 }
88
89 SetSize( size );
90
91 GetSizer()->Fit( this ); // Needed on Ubuntu/Unity to display the dialog
92}
93
94
96{
97}
98
99
100void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
101{
102 // Notify the SCH_EDIT_FRAME
104}
105
106
107void DIALOG_SCH_FIND::OnIdle( wxIdleEvent& aEvent )
108{
109 if( m_findDirty )
110 {
112 m_findDirty = false;
113 }
114}
115
116
117void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
118{
119 wxCloseEvent dummy;
120 OnClose( dummy );
121}
122
123
124void DIALOG_SCH_FIND::onShowSearchPanel( wxHyperlinkEvent& event )
125{
127 {
128 EndModal( wxID_CANCEL );
129
130 CallAfter(
131 []()
132 {
133 if( wxWindow* frame = wxWindow::FindWindowByName( SCH_EDIT_FRAME_NAME ) )
134 static_cast<SCH_EDIT_FRAME*>( frame )->FocusSearch();
135 } );
136 }
137 else
138 {
140 }
141}
142
143
144void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
145{
146 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty()
148}
149
150
151void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
152{
153 aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
154}
155
156
157void DIALOG_SCH_FIND::OnSearchForText( wxCommandEvent& aEvent )
158{
160 m_findDirty = true;
161}
162
163
164void DIALOG_SCH_FIND::OnSearchForSelect( wxCommandEvent& aEvent )
165{
167
168 // Move the search string to the top of the list if it isn't already there.
169 if( aEvent.GetSelection() != 0 )
170 {
171 wxString tmp = m_comboFind->GetValue();
172 m_comboFind->Delete( aEvent.GetSelection() );
173 m_comboFind->Insert( tmp, 0 );
174 m_comboFind->SetSelection( 0 );
175 }
176
178}
179
180
181void DIALOG_SCH_FIND::OnReplaceWithText( wxCommandEvent& aEvent )
182{
184}
185
186
187void DIALOG_SCH_FIND::OnReplaceWithSelect( wxCommandEvent& aEvent )
188{
190
191 // Move the replace string to the top of the list if it isn't already there.
192 if( aEvent.GetSelection() != 0 )
193 {
194 wxString tmp = m_comboReplace->GetValue();
195 m_comboReplace->Delete( aEvent.GetSelection() );
196 m_comboReplace->Insert( tmp, 0 );
197 m_comboReplace->SetSelection( 0 );
198 }
199}
200
201
202void DIALOG_SCH_FIND::OnSearchForEnter( wxCommandEvent& aEvent )
203{
204 OnFind( aEvent );
205}
206
207
208void DIALOG_SCH_FIND::OnReplaceWithEnter( wxCommandEvent& aEvent )
209{
210 OnFind( aEvent );
211}
212
213
214void DIALOG_SCH_FIND::OnOptions( wxCommandEvent& aEvent )
215{
216 updateFlags();
217 m_findDirty = true;
218}
219
221{
222 // Rebuild the search flags in m_findReplaceData from dialog settings
229
230 if( m_checkWholeWord->GetValue() )
231 m_findReplaceData->matchMode = EDA_SEARCH_MATCH_MODE::WHOLEWORD;
232 else if( m_checkRegexMatch->IsShown() && m_checkRegexMatch->GetValue() )
233 m_findReplaceData->matchMode = EDA_SEARCH_MATCH_MODE::REGEX;
234 else
235 m_findReplaceData->matchMode = EDA_SEARCH_MATCH_MODE::PLAIN;
236
237 if( m_checkSelectedOnly->GetValue() )
238 {
239 m_checkCurrentSheetOnly->SetValue( true );
240 m_checkCurrentSheetOnly->Enable( false );
242 }
243 else
244 {
245 m_checkCurrentSheetOnly->Enable( true );
247 }
248}
249
250
251void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
252{
253 updateFlags(); // Ensure search flags are up to date
254
255 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
256
257 if( index == wxNOT_FOUND )
258 {
259 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
260 }
261 else if( index != 0 )
262 {
263 /* Move the search string to the top of the list if it isn't already there. */
264 wxString tmp = m_comboFind->GetValue();
265 m_comboFind->Delete( index );
266 m_comboFind->Insert( tmp, 0 );
267 m_comboFind->SetSelection( 0 );
268 }
269
271}
272
273
274void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
275{
276 updateFlags(); // Ensure search flags are up to date
277
278 int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
279
280 if( index == wxNOT_FOUND )
281 {
282 m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
283 }
284 else if( index != 0 )
285 {
286 /* Move the search string to the top of the list if it isn't already there. */
287 wxString tmp = m_comboReplace->GetValue();
288 m_comboReplace->Delete( index );
289 m_comboReplace->Insert( tmp, 0 );
290 m_comboReplace->SetSelection( 0 );
291 }
292
293 if( aEvent.GetId() == wxID_REPLACE )
295 else if( aEvent.GetId() == wxID_REPLACE_ALL )
297}
298
299
301{
302 int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
303
304 if( index == wxNOT_FOUND )
305 {
306 m_comboFind->Insert( m_comboFind->GetValue(), 0 );
307 }
308 else if( index != 0 )
309 {
310 /* Move the search string to the top of the list if it isn't already there. */
311 wxString tmp = m_comboFind->GetValue();
312 m_comboFind->Delete( index );
313 m_comboFind->Insert( tmp, 0 );
314 }
315
316 return m_comboFind->GetStrings();
317}
318
319
320void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries, const wxString& aFindString )
321{
322 m_comboFind->Append( aEntries );
323
324 while( m_comboFind->GetCount() > 10 )
325 {
326 m_frame->GetFindHistoryList().pop_back();
327 m_comboFind->Delete( 9 );
328 }
329
330 if( !aFindString.IsEmpty() )
331 {
332 m_comboFind->SetValue( aFindString );
333 m_comboFind->SelectAll();
334 }
335 else if( m_comboFind->GetCount() )
336 {
337 m_comboFind->SetSelection( 0 );
338 m_comboFind->SelectAll();
339 }
340}
341
342
343void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
344{
345 m_comboReplace->Append( aEntries );
346
347 while( m_comboReplace->GetCount() > 10 )
348 {
349 m_frame->GetFindHistoryList().pop_back();
350 m_comboReplace->Delete( 9 );
351 }
352
353 if( m_comboReplace->GetCount() )
354 {
355 m_comboReplace->SetSelection( 0 );
356 m_comboFind->SelectAll();
357 }
358}
static TOOL_ACTION replaceAll
Definition: actions.h:115
static TOOL_ACTION updateFind
Definition: actions.h:116
static TOOL_ACTION replaceAndFindNext
Definition: actions.h:114
static TOOL_ACTION findNext
Definition: actions.h:111
static TOOL_ACTION showSearch
Definition: actions.h:108
Class DIALOG_SCH_FIND_BASE.
wxHyperlinkCtrl * m_searchPanelLink
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 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 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:66
wxArrayString & GetFindHistoryList()
Schematic editor (Eeschema) main window.
void OnFindDialogClose()
Notification that the Find dialog has closed.
bool IsSearchPaneShown()
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)
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
#define _(s)
#define SCH_EDIT_FRAME_NAME
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
std::vector< FAB_LAYER_COLOR > dummy
EDA_SEARCH_MATCH_MODE matchMode
wxString replaceString