You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

514 lines
16 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
16 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2008-2016 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2016 KiCad Developers, see change_log.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file eeschema/find.cpp
  27. * @brief Functions for searching for a schematic item.
  28. */
  29. /*
  30. * Search a text (text, value, reference) within a component or
  31. * search a component in libraries, a marker ...,
  32. * in current sheet or whole the project
  33. */
  34. #include <fctsys.h>
  35. #include <pgm_base.h>
  36. #include <class_drawpanel.h>
  37. #include <confirm.h>
  38. #include <kicad_string.h>
  39. #include <gestfich.h>
  40. #include <schframe.h>
  41. #include <base_units.h>
  42. #include <general.h>
  43. #include <class_library.h>
  44. #include <lib_pin.h>
  45. #include <sch_marker.h>
  46. #include <sch_component.h>
  47. #include <sch_sheet.h>
  48. #include <sch_sheet_path.h>
  49. #include <kicad_device_context.h>
  50. #include <dialogs/dialog_schematic_find.h>
  51. void SCH_EDIT_FRAME::OnFindDrcMarker( wxFindDialogEvent& event )
  52. {
  53. static SCH_MARKER* lastMarker = NULL;
  54. wxString msg;
  55. SCH_SHEET_LIST schematic( g_RootSheet );
  56. SCH_SHEET_PATH* sheetFoundIn = NULL;
  57. bool wrap = ( event.GetFlags() & FR_SEARCH_WRAP ) != 0;
  58. bool warpCursor = ( ( event.GetId() == wxEVT_COMMAND_FIND_CLOSE ) ||
  59. !( event.GetFlags() & FR_NO_WARP_CURSOR ) );
  60. if( event.GetFlags() & FR_CURRENT_SHEET_ONLY )
  61. {
  62. sheetFoundIn = m_CurrentSheet;
  63. lastMarker = (SCH_MARKER*) m_CurrentSheet->FindNextItem( SCH_MARKER_T, lastMarker, wrap );
  64. }
  65. else
  66. {
  67. lastMarker = (SCH_MARKER*) schematic.FindNextItem( SCH_MARKER_T, &sheetFoundIn,
  68. lastMarker, wrap );
  69. }
  70. if( lastMarker != NULL )
  71. {
  72. if( *sheetFoundIn != *m_CurrentSheet )
  73. {
  74. sheetFoundIn->LastScreen()->SetZoom( GetScreen()->GetZoom() );
  75. *m_CurrentSheet = *sheetFoundIn;
  76. m_CurrentSheet->UpdateAllScreenReferences();
  77. }
  78. SetCrossHairPosition( lastMarker->GetPosition() );
  79. RedrawScreen( lastMarker->GetPosition(), warpCursor );
  80. wxString path = sheetFoundIn->Path();
  81. wxString units = GetAbbreviatedUnitsLabel();
  82. double x = To_User_Unit( g_UserUnit, (double) lastMarker->GetPosition().x );
  83. double y = To_User_Unit( g_UserUnit, (double) lastMarker->GetPosition().y );
  84. msg.Printf( _( "Design rule check marker found in sheet %s at %0.3f%s, %0.3f%s" ),
  85. GetChars( path ), x, GetChars( units ), y, GetChars( units) );
  86. SetStatusText( msg );
  87. }
  88. else
  89. {
  90. SetStatusText( _( "No more markers were found." ) );
  91. }
  92. }
  93. SCH_ITEM* SCH_EDIT_FRAME::FindComponentAndItem( const wxString& aReference,
  94. bool aSearchHierarchy,
  95. SCH_SEARCH_T aSearchType,
  96. const wxString& aSearchText,
  97. bool aWarpMouse )
  98. {
  99. SCH_SHEET_PATH* sheet = NULL;
  100. SCH_SHEET_PATH* sheetWithComponentFound = NULL;
  101. SCH_ITEM* item = NULL;
  102. SCH_COMPONENT* Component = NULL;
  103. wxPoint pos;
  104. bool centerAndRedraw = false;
  105. bool notFound = true;
  106. LIB_PIN* pin;
  107. SCH_SHEET_LIST sheetList( g_RootSheet );
  108. if( !aSearchHierarchy )
  109. sheetList.push_back( *m_CurrentSheet );
  110. else
  111. sheetList.BuildSheetList( g_RootSheet );
  112. for( SCH_SHEET_PATHS_ITER it = sheetList.begin(); it != sheetList.end(); ++it )
  113. {
  114. sheet = &(*it);
  115. item = (*it).LastDrawList();
  116. for( ; ( item != NULL ) && ( notFound == true ); item = item->Next() )
  117. {
  118. if( item->Type() != SCH_COMPONENT_T )
  119. continue;
  120. SCH_COMPONENT* pSch = (SCH_COMPONENT*) item;
  121. if( aReference.CmpNoCase( pSch->GetRef( sheet ) ) == 0 )
  122. {
  123. Component = pSch;
  124. sheetWithComponentFound = sheet;
  125. switch( aSearchType )
  126. {
  127. default:
  128. case FIND_COMPONENT_ONLY: // Find component only
  129. notFound = false;
  130. pos = pSch->GetPosition();
  131. break;
  132. case FIND_PIN: // find a pin
  133. pos = pSch->GetPosition(); // temporary: will be changed if the pin is found.
  134. pin = pSch->GetPin( aSearchText );
  135. if( pin == NULL )
  136. break;
  137. notFound = false;
  138. pos += pin->GetPosition();
  139. break;
  140. case FIND_REFERENCE: // find reference
  141. notFound = false;
  142. pos = pSch->GetField( REFERENCE )->GetPosition();
  143. break;
  144. case FIND_VALUE: // find value
  145. pos = pSch->GetPosition();
  146. if( aSearchText.CmpNoCase( pSch->GetField( VALUE )->GetShownText() ) != 0 )
  147. break;
  148. notFound = false;
  149. pos = pSch->GetField( VALUE )->GetPosition();
  150. break;
  151. }
  152. }
  153. }
  154. if( notFound == false )
  155. break;
  156. }
  157. if( Component )
  158. {
  159. sheet = sheetWithComponentFound;
  160. if( *sheet != *m_CurrentSheet )
  161. {
  162. sheet->LastScreen()->SetZoom( GetScreen()->GetZoom() );
  163. *m_CurrentSheet = *sheet;
  164. m_CurrentSheet->UpdateAllScreenReferences();
  165. centerAndRedraw = true;
  166. }
  167. wxPoint delta;
  168. pos -= Component->GetPosition();
  169. delta = Component->GetTransform().TransformCoordinate( pos );
  170. pos = delta + Component->GetPosition();
  171. /* There may be need to reframe the drawing */
  172. if( ! m_canvas->IsPointOnDisplay( pos ) )
  173. {
  174. centerAndRedraw = true;
  175. }
  176. if( centerAndRedraw )
  177. {
  178. SetCrossHairPosition( pos );
  179. RedrawScreen( pos, aWarpMouse );
  180. }
  181. else
  182. {
  183. INSTALL_UNBUFFERED_DC( dc, m_canvas );
  184. m_canvas->CrossHairOff( &dc );
  185. if( aWarpMouse )
  186. m_canvas->MoveCursor( pos );
  187. SetCrossHairPosition( pos );
  188. m_canvas->CrossHairOn( &dc );
  189. }
  190. }
  191. /* Print diag */
  192. wxString msg_item;
  193. wxString msg;
  194. switch( aSearchType )
  195. {
  196. default:
  197. case FIND_COMPONENT_ONLY: // Find component only
  198. msg_item = _( "component" );
  199. break;
  200. case FIND_PIN: // find a pin
  201. msg_item.Printf( _( "pin %s" ), GetChars( aSearchText ) );
  202. break;
  203. case FIND_REFERENCE: // find reference
  204. msg_item.Printf( _( "reference %s" ), GetChars( aSearchText ) );
  205. break;
  206. case FIND_VALUE: // find value
  207. msg_item.Printf( _( "value %s" ), GetChars( aSearchText ) );
  208. break;
  209. case FIND_FIELD: // find field. todo
  210. msg_item.Printf( _( "field %s" ), GetChars( aSearchText ) );
  211. break;
  212. }
  213. if( Component )
  214. {
  215. if( !notFound )
  216. {
  217. msg.Printf( _( "%s %s found" ),
  218. GetChars( aReference ), GetChars( msg_item ) );
  219. }
  220. else
  221. {
  222. msg.Printf( _( "%s found but %s not found" ),
  223. GetChars( aReference ), GetChars( msg_item ) );
  224. }
  225. }
  226. else
  227. {
  228. msg.Printf( _( "Component %s not found" ),
  229. GetChars( aReference ) );
  230. }
  231. SetStatusText( msg );
  232. return item;
  233. }
  234. bool SCH_EDIT_FRAME::IsSearchCacheObsolete( const SCH_FIND_REPLACE_DATA& aSearchCriteria )
  235. {
  236. PART_LIBS* libs = Prj().SchLibs();
  237. int mod_hash = libs->GetModifyHash();
  238. // the cache is obsolete whenever any library changes.
  239. if( mod_hash != m_foundItems.GetLibHash() )
  240. {
  241. m_foundItems.SetForceSearch();
  242. m_foundItems.SetLibHash( mod_hash );
  243. return true;
  244. }
  245. else if( m_foundItems.IsSearchRequired( aSearchCriteria ) )
  246. return true;
  247. else
  248. return false;
  249. }
  250. void SCH_EDIT_FRAME::OnFindSchematicItem( wxFindDialogEvent& aEvent )
  251. {
  252. SCH_FIND_REPLACE_DATA searchCriteria;
  253. SCH_FIND_COLLECTOR_DATA data;
  254. searchCriteria.SetFlags( aEvent.GetFlags() );
  255. searchCriteria.SetFindString( aEvent.GetFindString() );
  256. searchCriteria.SetReplaceString( aEvent.GetReplaceString() );
  257. if( aEvent.GetEventType() == wxEVT_COMMAND_FIND_CLOSE )
  258. {
  259. if( m_foundItems.GetCount() == 0 )
  260. return;
  261. // Refresh the search cache in case something has changed. This prevents any stale
  262. // pointers from crashing Eeschema when the wxEVT_FIND_CLOSE event is handled.
  263. if( IsSearchCacheObsolete( searchCriteria ) )
  264. {
  265. if( aEvent.GetFlags() & FR_CURRENT_SHEET_ONLY && g_RootSheet->CountSheets() > 1 )
  266. {
  267. m_foundItems.Collect( searchCriteria, m_CurrentSheet );
  268. }
  269. else
  270. {
  271. m_foundItems.Collect( searchCriteria );
  272. }
  273. }
  274. }
  275. else if( IsSearchCacheObsolete( searchCriteria ) )
  276. {
  277. if( aEvent.GetFlags() & FR_CURRENT_SHEET_ONLY && g_RootSheet->CountSheets() > 1 )
  278. {
  279. m_foundItems.Collect( searchCriteria, m_CurrentSheet );
  280. }
  281. else
  282. {
  283. m_foundItems.Collect( searchCriteria );
  284. }
  285. }
  286. else
  287. {
  288. EDA_ITEM* currentItem = m_foundItems.GetItem( data );
  289. if( currentItem != NULL )
  290. currentItem->SetForceVisible( false );
  291. m_foundItems.UpdateIndex();
  292. }
  293. updateFindReplaceView( aEvent );
  294. }
  295. void SCH_EDIT_FRAME::OnFindReplace( wxFindDialogEvent& aEvent )
  296. {
  297. static int nextFoundIndex = 0;
  298. SCH_ITEM* item;
  299. SCH_SHEET_PATH* sheet;
  300. SCH_SHEET_LIST schematic( g_RootSheet );
  301. SCH_FIND_COLLECTOR_DATA data;
  302. SCH_FIND_REPLACE_DATA searchCriteria;
  303. searchCriteria.SetFlags( aEvent.GetFlags() );
  304. searchCriteria.SetFindString( aEvent.GetFindString() );
  305. searchCriteria.SetReplaceString( aEvent.GetReplaceString() );
  306. m_foundItems.SetReplaceString( aEvent.GetReplaceString() );
  307. if( IsSearchCacheObsolete( searchCriteria ) )
  308. {
  309. if( aEvent.GetFlags() & FR_CURRENT_SHEET_ONLY && g_RootSheet->CountSheets() > 1 )
  310. {
  311. m_foundItems.Collect( searchCriteria, m_CurrentSheet );
  312. }
  313. else
  314. {
  315. m_foundItems.Collect( searchCriteria );
  316. }
  317. // Restore the next found index on cache refresh. Prevents single replace events
  318. // from starting back at the beginning of the cache.
  319. m_foundItems.SetFoundIndex( nextFoundIndex );
  320. }
  321. if( aEvent.GetEventType() == wxEVT_COMMAND_FIND_REPLACE_ALL )
  322. {
  323. while( ( item = (SCH_ITEM*) m_foundItems.GetItem( data ) ) != NULL )
  324. {
  325. SCH_ITEM* undoItem = data.GetParent();
  326. // Don't save child items in undo list.
  327. if( undoItem == NULL )
  328. undoItem = item;
  329. SetUndoItem( undoItem );
  330. sheet = schematic.GetSheetByPath( data.GetSheetPath() );
  331. wxCHECK_RET( sheet != NULL, wxT( "Could not find sheet path " ) + data.GetSheetPath() );
  332. if( m_foundItems.ReplaceItem( sheet ) )
  333. {
  334. OnModify();
  335. SaveUndoItemInUndoList( undoItem );
  336. updateFindReplaceView( aEvent );
  337. }
  338. m_foundItems.IncrementIndex();
  339. if( m_foundItems.PassedEnd() )
  340. break;
  341. }
  342. }
  343. else
  344. {
  345. item = (SCH_ITEM*) m_foundItems.GetItem( data );
  346. wxCHECK_RET( item != NULL, wxT( "Invalid replace item in find collector list." ) );
  347. SCH_ITEM* undoItem = data.GetParent();
  348. if( undoItem == NULL )
  349. undoItem = item;
  350. SetUndoItem( undoItem );
  351. sheet = schematic.GetSheetByPath( data.GetSheetPath() );
  352. wxCHECK_RET( sheet != NULL, wxT( "Could not find sheet path " ) + data.GetSheetPath() );
  353. if( m_foundItems.ReplaceItem( sheet ) )
  354. {
  355. OnModify();
  356. SaveUndoItemInUndoList( undoItem );
  357. updateFindReplaceView( aEvent );
  358. }
  359. m_foundItems.IncrementIndex();
  360. nextFoundIndex = m_foundItems.GetFoundIndex();
  361. }
  362. // End the replace if we are at the end if the list. This prevents an infinite loop if
  363. // wrap search is selected and all of the items have been replaced with a value that
  364. // still satisfies the search criteria.
  365. if( m_foundItems.PassedEnd() )
  366. aEvent.SetFlags( aEvent.GetFlags() & ~FR_REPLACE_ITEM_FOUND );
  367. }
  368. void SCH_EDIT_FRAME::updateFindReplaceView( wxFindDialogEvent& aEvent )
  369. {
  370. wxString msg;
  371. SCH_SHEET_LIST schematic( g_RootSheet );
  372. SCH_FIND_COLLECTOR_DATA data;
  373. SCH_FIND_REPLACE_DATA searchCriteria;
  374. bool warpCursor = !( aEvent.GetFlags() & FR_NO_WARP_CURSOR );
  375. searchCriteria.SetFlags( aEvent.GetFlags() );
  376. searchCriteria.SetFindString( aEvent.GetFindString() );
  377. searchCriteria.SetReplaceString( aEvent.GetReplaceString() );
  378. if( m_foundItems.GetItem( data ) != NULL )
  379. {
  380. wxLogTrace( traceFindReplace, wxT( "Found " ) + m_foundItems.GetText() );
  381. SCH_SHEET_PATH* sheet = schematic.GetSheetByPath( data.GetSheetPath() );
  382. wxCHECK_RET( sheet != NULL, wxT( "Could not find sheet path " ) +
  383. data.GetSheetPath() );
  384. SCH_ITEM* item = (SCH_ITEM*)m_foundItems.GetItem( data );
  385. // Make the item temporarily visible just in case it's hide flag is set. This
  386. // has no effect on objects that don't support hiding. If this is a close find
  387. // dialog event, clear the temporary visibility flag.
  388. if( item )
  389. {
  390. if( aEvent.GetEventType() == wxEVT_COMMAND_FIND_CLOSE )
  391. item->SetForceVisible( false );
  392. else if( item->Type() == SCH_FIELD_T && !( (SCH_FIELD*) item )->IsVisible() )
  393. item->SetForceVisible( true );
  394. }
  395. if( sheet->PathHumanReadable() != m_CurrentSheet->PathHumanReadable() )
  396. {
  397. sheet->LastScreen()->SetZoom( GetScreen()->GetZoom() );
  398. *m_CurrentSheet = *sheet;
  399. m_CurrentSheet->UpdateAllScreenReferences();
  400. SetScreen( sheet->LastScreen() );
  401. }
  402. // careful here
  403. SetCrossHairPosition( data.GetPosition() );
  404. RedrawScreen( data.GetPosition(), warpCursor );
  405. msg = m_foundItems.GetText();
  406. if( aEvent.GetFlags() & FR_SEARCH_REPLACE )
  407. aEvent.SetFlags( aEvent.GetFlags() | FR_REPLACE_ITEM_FOUND );
  408. }
  409. else
  410. {
  411. if( aEvent.GetFlags() & FR_SEARCH_REPLACE )
  412. aEvent.SetFlags( aEvent.GetFlags() & ~FR_REPLACE_ITEM_FOUND );
  413. msg.Printf( _( "No item found matching %s." ), GetChars( aEvent.GetFindString() ) );
  414. }
  415. SetStatusText( msg );
  416. }