KiCad PCB EDA Suite
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 (C) 2010-2011 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 
30 #include <dialog_schematic_find.h>
31 
32 
33 DEFINE_EVENT_TYPE( EVT_COMMAND_FIND_DRC_MARKER )
34 DEFINE_EVENT_TYPE( EVT_COMMAND_FIND_COMPONENT_IN_LIB )
35 
36 
37 DIALOG_SCH_FIND::DIALOG_SCH_FIND( wxWindow* aParent, wxFindReplaceData* aData, wxString* aStatus,
38  const wxPoint& aPosition, const wxSize& aSize, int aStyle ) :
39  DIALOG_SCH_FIND_BASE( aParent, wxID_ANY, _( "Find" ), aPosition, aSize,
40  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | aStyle ),
41  m_findReplaceData( aData ),
42  m_status( aStatus )
43 {
44  wxASSERT_MSG( m_findReplaceData, wxT( "can't create find dialog without data" ) );
45 
46  if( aStyle & wxFR_REPLACEDIALOG )
47  {
48  SetTitle( _( "Find and Replace" ) );
49  m_buttonReplace->Show( true );
50  m_buttonReplaceAll->Show( true );
51  m_staticReplace->Show( true );
52  m_comboReplace->Show( true );
53  m_checkReplaceReferences->Show( true );
54  m_checkWildcardMatch->Show( false ); // Wildcard replace is not implemented.
55  }
56 
57  int flags = m_findReplaceData->GetFlags();
58  m_radioForward->SetValue( flags & wxFR_DOWN );
59  m_radioBackward->SetValue( ( flags & wxFR_DOWN ) == 0 );
60  m_checkMatchCase->SetValue( flags & wxFR_MATCHCASE );
61  m_checkWholeWord->SetValue( flags & wxFR_WHOLEWORD );
62  m_checkNoWarpCursor->SetValue( flags & FR_NO_WARP_CURSOR );
63 
64  /* Whole word and wild card searches are mutually exclusive. */
65  if( !( flags & wxFR_WHOLEWORD ) )
66  m_checkWildcardMatch->SetValue( flags & FR_MATCH_WILDCARD );
67 
68  m_checkAllFields->SetValue( flags & FR_SEARCH_ALL_FIELDS );
69  m_checkReplaceReferences->SetValue( flags & FR_REPLACE_REFERENCES );
70  m_checkAllPins->SetValue( flags & FR_SEARCH_ALL_PINS );
71  m_checkWrap->SetValue( flags & FR_SEARCH_WRAP );
72  m_checkCurrentSheetOnly->SetValue( flags & FR_CURRENT_SHEET_ONLY );
73 
74  m_statusLine->SetLabel( wxEmptyString );
75  m_buttonFind->SetDefault();
76  SetInitialFocus( m_comboFind );
77 
78  SetPosition( aPosition );
79 
80  // Adjust the height of the dialog to prevent controls from being hidden when
81  // switching between the find and find/replace modes of the dialog. This ignores
82  // the users preferred height if any of the controls would be hidden.
83  GetSizer()->SetSizeHints( this );
84  wxSize size = aSize;
85 
86  if( aSize != wxDefaultSize )
87  {
88  wxSize bestSize = GetBestSize();
89 
90  if( size.GetHeight() != bestSize.GetHeight() )
91  size.SetHeight( bestSize.GetHeight() );
92  }
93 
94  SetSize( size );
95 
96  GetSizer()->Fit( this ); // Needed on Ubuntu/Unity to display the dialog
97 }
98 
99 
100 void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
101 {
102  SendEvent( wxEVT_COMMAND_FIND_CLOSE );
103 }
104 
105 
106 void DIALOG_SCH_FIND::OnUpdateFindUI( wxUpdateUIEvent& aEvent )
107 {
108  aEvent.Enable( !m_comboFind->GetValue().empty() );
109 }
110 
111 
112 void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
113 {
114  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() &&
115  (m_findReplaceData->GetFlags() & FR_REPLACE_ITEM_FOUND) );
116 }
117 
118 
119 void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
120 {
121  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
122 }
123 
124 
125 void DIALOG_SCH_FIND::OnUpdateWholeWordUI( wxUpdateUIEvent& aEvent )
126 {
127  aEvent.Enable( !m_checkWildcardMatch->GetValue() );
128 }
129 
130 
131 void DIALOG_SCH_FIND::OnUpdateWildcardUI( wxUpdateUIEvent& aEvent )
132 {
133  aEvent.Enable( !m_checkWholeWord->GetValue() );
134 }
135 
136 
137 void DIALOG_SCH_FIND::OnTextEnter( wxCommandEvent& aEvent )
138 {
139  OnFind( aEvent );
140 }
141 
142 
143 void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
144 {
145  int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
146 
147  if( index == wxNOT_FOUND )
148  {
149  m_comboFind->Insert( m_comboFind->GetValue(), 0 );
150  }
151  else if( index != 0 )
152  {
153  /* Move the search string to the top of the list if it isn't already there. */
154  wxString tmp = m_comboFind->GetValue();
155  m_comboFind->Delete( index );
156  m_comboFind->Insert( tmp, 0 );
157  m_comboFind->SetSelection( 0 );
158  }
159 
160  SendEvent( wxEVT_COMMAND_FIND );
161 }
162 
163 
164 void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
165 {
166  int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
167 
168  if( index == wxNOT_FOUND )
169  {
170  m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
171  }
172  else if( index != 0 )
173  {
174  /* Move the search string to the top of the list if it isn't already there. */
175  wxString tmp = m_comboReplace->GetValue();
176  m_comboReplace->Delete( index );
177  m_comboReplace->Insert( tmp, 0 );
178  m_comboReplace->SetSelection( 0 );
179  }
180 
181  if( aEvent.GetId() == wxID_REPLACE )
182  SendEvent( wxEVT_COMMAND_FIND_REPLACE );
183  else if( aEvent.GetId() == wxID_REPLACE_ALL )
184  SendEvent( wxEVT_COMMAND_FIND_REPLACE_ALL );
185 }
186 
187 
188 void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
189 {
190  SendEvent( wxEVT_COMMAND_FIND_CLOSE );
191  Show( false );
192 }
193 
194 
195 void DIALOG_SCH_FIND::SendEvent( const wxEventType& aEventType )
196 {
197  wxFindDialogEvent event( aEventType, GetId() );
198  event.SetEventObject( this );
199  event.SetFindString( m_comboFind->GetValue() );
200 
201  int flags = 0;
202 
203  if ( HasFlag( wxFR_REPLACEDIALOG ) )
204  {
205  event.SetReplaceString( m_comboReplace->GetValue() );
206  flags |= FR_SEARCH_REPLACE;
207  }
208 
209  if( m_checkReplaceReferences->GetValue() )
210  flags |= FR_REPLACE_REFERENCES;
211 
212  if( m_radioForward->GetValue() )
213  flags |= wxFR_DOWN;
214 
215  if( m_checkMatchCase->GetValue() )
216  flags |= wxFR_MATCHCASE;
217 
218  if( m_checkWholeWord->GetValue() )
219  flags |= wxFR_WHOLEWORD;
220 
221  if( m_checkWildcardMatch->IsShown() && m_checkWildcardMatch->GetValue() )
222  flags |= FR_MATCH_WILDCARD;
223 
224  if( m_checkAllFields->GetValue() )
225  flags |= FR_SEARCH_ALL_FIELDS;
226 
227  if( m_checkAllPins->GetValue() )
228  flags |= FR_SEARCH_ALL_PINS;
229 
230  if( m_checkWrap->GetValue() )
231  flags |= FR_SEARCH_WRAP;
232 
233  if( m_checkCurrentSheetOnly->GetValue() )
234  flags |= FR_CURRENT_SHEET_ONLY;
235 
236  if( m_checkNoWarpCursor->GetValue() )
237  flags |= FR_NO_WARP_CURSOR;
238 
239  m_findReplaceData->SetFindString( event.GetFindString() );
240 
241  if( HasFlag( wxFR_REPLACEDIALOG )
242  && ( event.GetEventType() == wxEVT_COMMAND_FIND_REPLACE
243  || event.GetEventType() == wxEVT_COMMAND_FIND_REPLACE_ALL ) )
244  {
245  m_findReplaceData->SetReplaceString( event.GetReplaceString() );
246  }
247 
248  event.SetFlags( flags );
249 
250  m_findReplaceData->SetFlags( event.GetFlags() );
251 
252  // when we are no using the find/replace (just find)
253  // FR_REPLACE_REFERENCES flag bit is always set to 1 in event flags
254  // but not set in m_findReplaceData
255  if ( ! HasFlag( wxFR_REPLACEDIALOG ) )
256  {
257  flags |= FR_REPLACE_REFERENCES;
258  event.SetFlags( flags );
259  }
260 
261  if( !GetEventHandler()->ProcessEvent( event ) )
262  {
263  GetParent()->GetEventHandler()->ProcessEvent( event );
264  }
265 
266  if( event.GetFlags() != flags )
267  m_findReplaceData->SetFlags( event.GetFlags() );
268 
269  m_statusLine->SetLabel( *m_status );
270 }
271 
272 
273 wxArrayString DIALOG_SCH_FIND::GetFindEntries() const
274 {
275  return m_comboFind->GetStrings();
276 }
277 
278 
279 void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries )
280 {
281  m_comboFind->Append( aEntries );
282 
283  if( m_comboFind->GetCount() )
284  {
285  m_comboFind->SetSelection( 0 );
286  m_comboFind->SelectAll();
287  }
288 }
289 
290 
291 void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
292 {
293  m_comboReplace->Append( aEntries );
294 
295  if( m_comboReplace->GetCount() )
296  {
297  m_comboReplace->SetSelection( 0 );
298  m_comboFind->SelectAll();
299  }
300 }
void OnUpdateReplaceAllUI(wxUpdateUIEvent &aEvent) override
void OnUpdateWholeWordUI(wxUpdateUIEvent &aEvent) override
void SetPosition(wxString aStr, const wxString &aDefaultMeasurementUnit, int *aX, int *aY, const wxString &aActualConversion)
Class DIALOG_SCH_FIND_BASE.
Search texts (name and number (a 4 letters text) )in pins.
Used by the search event handler to let the dialog know that a replaceable item has been found...
Wrap around the beginning or end of search list.
Search all fields in component, not just the value and reference fields.
void OnReplace(wxCommandEvent &aEvent) override
void SendEvent(const wxEventType &aEventType)
Implementing DIALOG_SCH_FIND_BASE.
wxFindReplaceData * m_findReplaceData
Subclass of DIALOG_SCH_FIND_BASE, which is generated by wxFormBuilder.
void OnClose(wxCloseEvent &aEvent) override
void OnUpdateReplaceUI(wxUpdateUIEvent &aEvent) override
Perform search using simple wild card matching (* & ?).
Search the current sheet only.
void SetReplaceEntries(const wxArrayString &aEntries)
void OnTextEnter(wxCommandEvent &event) override
void SetFindEntries(const wxArrayString &aEntries)
bool Show(bool show) override
void OnUpdateFindUI(wxUpdateUIEvent &aEvent) override
void OnUpdateWildcardUI(wxUpdateUIEvent &aEvent) override
void OnFind(wxCommandEvent &aEvent) override
Don&#39;t warp cursor to found item until the dialog is closed.
Used by replace to ignore the component reference designator field.
Perform a search for a item that has replaceable text.
wxArrayString GetFindEntries() const
void OnCancel(wxCommandEvent &aEvent) override