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,
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 {
42  SetData( aData );
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_buttonFind->SetDefault();
75  m_comboFind->SetFocus();
76  SetPosition( aPosition );
77 
78  // Adjust the height of the dialog to prevent controls from being hidden when
79  // switching between the find and find/replace modes of the dialog. This ignores
80  // the users preferred height if any of the controls would be hidden.
81  GetSizer()->SetSizeHints( this );
82  wxSize size = aSize;
83 
84  if( aSize != wxDefaultSize )
85  {
86  wxSize bestSize = GetBestSize();
87 
88  if( size.GetHeight() != bestSize.GetHeight() )
89  size.SetHeight( bestSize.GetHeight() );
90  }
91 
92  SetSize( size );
93 
94  GetSizer()->Fit( this ); // Needed on Ubuntu/Unity to display the dialog
95 }
96 
97 
98 void DIALOG_SCH_FIND::OnClose( wxCloseEvent& aEvent )
99 {
100  SendEvent( wxEVT_COMMAND_FIND_CLOSE );
101 }
102 
103 
104 void DIALOG_SCH_FIND::OnUpdateFindUI( wxUpdateUIEvent& aEvent )
105 {
106  aEvent.Enable( !m_comboFind->GetValue().empty() );
107 }
108 
109 
110 void DIALOG_SCH_FIND::OnUpdateReplaceUI( wxUpdateUIEvent& aEvent )
111 {
112  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() &&
113  (m_findReplaceData->GetFlags() & FR_REPLACE_ITEM_FOUND) );
114 }
115 
116 
117 void DIALOG_SCH_FIND::OnUpdateReplaceAllUI( wxUpdateUIEvent& aEvent )
118 {
119  aEvent.Enable( HasFlag( wxFR_REPLACEDIALOG ) && !m_comboFind->GetValue().empty() );
120 }
121 
122 
123 void DIALOG_SCH_FIND::OnUpdateWholeWordUI( wxUpdateUIEvent& aEvent )
124 {
125  aEvent.Enable( !m_checkWildcardMatch->GetValue() );
126 }
127 
128 
129 void DIALOG_SCH_FIND::OnUpdateWildcardUI( wxUpdateUIEvent& aEvent )
130 {
131  aEvent.Enable( !m_checkWholeWord->GetValue() );
132 }
133 
134 
135 void DIALOG_SCH_FIND::OnFind( wxCommandEvent& aEvent )
136 {
137  int index = m_comboFind->FindString( m_comboFind->GetValue(), true );
138 
139  if( index == wxNOT_FOUND )
140  {
141  m_comboFind->Insert( m_comboFind->GetValue(), 0 );
142  }
143  else if( index != 0 )
144  {
145  /* Move the search string to the top of the list if it isn't already there. */
146  wxString tmp = m_comboFind->GetValue();
147  m_comboFind->Delete( index );
148  m_comboFind->Insert( tmp, 0 );
149  m_comboFind->SetSelection( 0 );
150  }
151 
152  SendEvent( wxEVT_COMMAND_FIND );
153 }
154 
155 
156 void DIALOG_SCH_FIND::OnReplace( wxCommandEvent& aEvent )
157 {
158  int index = m_comboReplace->FindString( m_comboReplace->GetValue(), true );
159 
160  if( index == wxNOT_FOUND )
161  {
162  m_comboReplace->Insert( m_comboReplace->GetValue(), 0 );
163  }
164  else if( index != 0 )
165  {
166  /* Move the search string to the top of the list if it isn't already there. */
167  wxString tmp = m_comboReplace->GetValue();
168  m_comboReplace->Delete( index );
169  m_comboReplace->Insert( tmp, 0 );
170  m_comboReplace->SetSelection( 0 );
171  }
172 
173  if( aEvent.GetId() == wxID_REPLACE )
174  SendEvent( wxEVT_COMMAND_FIND_REPLACE );
175  else if( aEvent.GetId() == wxID_REPLACE_ALL )
176  SendEvent( wxEVT_COMMAND_FIND_REPLACE_ALL );
177 }
178 
179 
180 void DIALOG_SCH_FIND::OnCancel( wxCommandEvent& aEvent )
181 {
182  SendEvent( wxEVT_COMMAND_FIND_CLOSE );
183  Show( false );
184 }
185 
186 
187 void DIALOG_SCH_FIND::SendEvent( const wxEventType& aEventType )
188 {
189  wxFindDialogEvent event( aEventType, GetId() );
190  event.SetEventObject( this );
191  event.SetFindString( m_comboFind->GetValue() );
192 
193  int flags = 0;
194 
195  if ( HasFlag( wxFR_REPLACEDIALOG ) )
196  {
197  event.SetReplaceString( m_comboReplace->GetValue() );
198  flags |= FR_SEARCH_REPLACE;
199  }
200 
201  if( m_checkReplaceReferences->GetValue() )
202  flags |= FR_REPLACE_REFERENCES;
203 
204  if( m_radioForward->GetValue() )
205  flags |= wxFR_DOWN;
206 
207  if( m_checkMatchCase->GetValue() )
208  flags |= wxFR_MATCHCASE;
209 
210  if( m_checkWholeWord->GetValue() )
211  flags |= wxFR_WHOLEWORD;
212 
213  if( m_checkWildcardMatch->IsShown() && m_checkWildcardMatch->GetValue() )
214  flags |= FR_MATCH_WILDCARD;
215 
216  if( m_checkAllFields->GetValue() )
217  flags |= FR_SEARCH_ALL_FIELDS;
218 
219  if( m_checkAllPins->GetValue() )
220  flags |= FR_SEARCH_ALL_PINS;
221 
222  if( m_checkWrap->GetValue() )
223  flags |= FR_SEARCH_WRAP;
224 
225  if( m_checkCurrentSheetOnly->GetValue() )
226  flags |= FR_CURRENT_SHEET_ONLY;
227 
228  if( m_checkNoWarpCursor->GetValue() )
229  flags |= FR_NO_WARP_CURSOR;
230 
231  m_findReplaceData->SetFindString( event.GetFindString() );
232 
233  if( HasFlag( wxFR_REPLACEDIALOG )
234  && ( event.GetEventType() == wxEVT_COMMAND_FIND_REPLACE
235  || event.GetEventType() == wxEVT_COMMAND_FIND_REPLACE_ALL ) )
236  {
237  m_findReplaceData->SetReplaceString( event.GetReplaceString() );
238  }
239 
240  event.SetFlags( flags );
241 
242  m_findReplaceData->SetFlags( event.GetFlags() );
243 
244  // when we are no using the find/replace (just find)
245  // FR_REPLACE_REFERENCES flag bit is always set to 1 in event flags
246  // but not set in m_findReplaceData
247  if ( ! HasFlag( wxFR_REPLACEDIALOG ) )
248  {
249  flags |= FR_REPLACE_REFERENCES;
250  event.SetFlags( flags );
251  }
252 
253  if( !GetEventHandler()->ProcessEvent( event ) )
254  {
255  GetParent()->GetEventHandler()->ProcessEvent( event );
256  }
257 
258  if( event.GetFlags() != flags )
259  m_findReplaceData->SetFlags( event.GetFlags() );
260 }
261 
262 
263 wxArrayString DIALOG_SCH_FIND::GetFindEntries() const
264 {
265  return m_comboFind->GetStrings();
266 }
267 
268 
269 void DIALOG_SCH_FIND::SetFindEntries( const wxArrayString& aEntries )
270 {
271  m_comboFind->Append( aEntries );
272 
273  if( m_comboFind->GetCount() )
274  {
275  m_comboFind->SetSelection( 0 );
276  m_comboFind->SelectAll();
277  }
278 }
279 
280 
281 void DIALOG_SCH_FIND::SetReplaceEntries( const wxArrayString& aEntries )
282 {
283  m_comboReplace->Append( aEntries );
284 
285  if( m_comboReplace->GetCount() )
286  {
287  m_comboReplace->SetSelection( 0 );
288  m_comboFind->SelectAll();
289  }
290 }
void OnUpdateReplaceAllUI(wxUpdateUIEvent &aEvent) override
void OnUpdateWholeWordUI(wxUpdateUIEvent &aEvent) override
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 (* & ?).
void SetPosition(wxString aStr, wxString aDefaultMeasurementUnit, int *aX, int *aY, wxString aActualConversion)
Search the current sheet only.
void SetReplaceEntries(const wxArrayString &aEntries)
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'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