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.

628 lines
15 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
15 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-2018 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. * @file lib_field.cpp
  26. */
  27. #include <fctsys.h>
  28. #include <pgm_base.h>
  29. #include <gr_basic.h>
  30. #include <macros.h>
  31. #include <base_struct.h>
  32. #include <draw_graphic_text.h>
  33. #include <kicad_string.h>
  34. #include <sch_draw_panel.h>
  35. #include <plotter.h>
  36. #include <trigo.h>
  37. #include <base_units.h>
  38. #include <msgpanel.h>
  39. #include <bitmaps.h>
  40. #include <general.h>
  41. #include <class_libentry.h>
  42. #include <transform.h>
  43. #include <lib_field.h>
  44. #include <template_fieldnames.h>
  45. LIB_FIELD::LIB_FIELD(LIB_PART * aParent, int idfield ) :
  46. LIB_ITEM( LIB_FIELD_T, aParent )
  47. {
  48. Init( idfield );
  49. }
  50. LIB_FIELD::LIB_FIELD( int idfield ) :
  51. LIB_ITEM( LIB_FIELD_T, NULL )
  52. {
  53. Init( idfield );
  54. }
  55. LIB_FIELD::~LIB_FIELD()
  56. {
  57. }
  58. void LIB_FIELD::operator=( const LIB_FIELD& field )
  59. {
  60. m_id = field.m_id;
  61. m_Text = field.m_Text;
  62. m_name = field.m_name;
  63. m_Parent = field.m_Parent;
  64. SetEffects( field );
  65. }
  66. void LIB_FIELD::Init( int id )
  67. {
  68. m_id = id;
  69. SetTextWidth( GetDefaultTextSize() );
  70. SetTextHeight( GetDefaultTextSize() );
  71. SetTextAngle( TEXT_ANGLE_HORIZ ); // constructor already did this.
  72. m_rotate = false;
  73. m_updateText = false;
  74. // fields in RAM must always have names, because we are trying to get
  75. // less dependent on field ids and more dependent on names.
  76. // Plus assumptions are made in the field editors.
  77. m_name = TEMPLATE_FIELDNAME::GetDefaultFieldName( id );
  78. switch( id )
  79. {
  80. case DATASHEET:
  81. case FOOTPRINT:
  82. // by contrast, VALUE and REFERENCE are are always constructed as
  83. // initially visible, and template fieldsnames' initial visibility
  84. // is controlled by the template fieldname configuration record.
  85. SetVisible( false );
  86. break;
  87. }
  88. }
  89. int LIB_FIELD::GetPenSize() const
  90. {
  91. int pensize = GetThickness();
  92. if( pensize == 0 ) // Use default values for pen size
  93. {
  94. if( IsBold() )
  95. pensize = GetPenSizeForBold( GetTextWidth() );
  96. else
  97. pensize = GetDefaultLineThickness();
  98. }
  99. // Clip pen size for small texts:
  100. pensize = Clamp_Text_PenSize( pensize, GetTextSize(), IsBold() );
  101. return pensize;
  102. }
  103. void LIB_FIELD::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
  104. COLOR4D aColor, GR_DRAWMODE aDrawMode, void* aData,
  105. const TRANSFORM& aTransform )
  106. {
  107. wxPoint text_pos;
  108. COLOR4D color = COLOR4D::UNSPECIFIED;
  109. int linewidth = GetPenSize();
  110. if( !IsVisible() && ( aColor == COLOR4D::UNSPECIFIED ) )
  111. {
  112. color = GetInvisibleItemColor();
  113. }
  114. else if( IsSelected() && ( aColor == COLOR4D::UNSPECIFIED ) )
  115. {
  116. color = GetItemSelectedColor();
  117. }
  118. else
  119. {
  120. color = aColor;
  121. }
  122. if( color == COLOR4D::UNSPECIFIED )
  123. color = GetDefaultColor();
  124. text_pos = aTransform.TransformCoordinate( GetTextPos() ) + aOffset;
  125. wxString text;
  126. if( aData )
  127. text = *(wxString*)aData;
  128. else
  129. text = m_Text;
  130. GRSetDrawMode( aDC, aDrawMode );
  131. EDA_RECT* clipbox = aPanel? aPanel->GetClipBox() : NULL;
  132. DrawGraphicText( clipbox, aDC, text_pos, color, text,
  133. GetTextAngle(), GetTextSize(),
  134. GetHorizJustify(), GetVertJustify(),
  135. linewidth, IsItalic(), IsBold() );
  136. /* Set to one (1) to draw bounding box around field text to validate
  137. * bounding box calculation. */
  138. #if 0
  139. EDA_RECT bBox = GetBoundingBox();
  140. bBox.RevertYAxis();
  141. bBox = aTransform.TransformCoordinate( bBox );
  142. bBox.Move( aOffset );
  143. GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
  144. #endif
  145. }
  146. bool LIB_FIELD::HitTest( const wxPoint& aPosition ) const
  147. {
  148. // Because HitTest is mainly used to select the field
  149. // return always false if this field is void
  150. if( IsVoid() )
  151. return false;
  152. return HitTest( aPosition, 0, DefaultTransform );
  153. }
  154. bool LIB_FIELD::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
  155. {
  156. if( aThreshold < 0 )
  157. aThreshold = 0;
  158. // Build a temporary copy of the text for hit testing
  159. EDA_TEXT tmp_text( *this );
  160. // Reference designator text has one or 2 additional character (displays
  161. // U? or U?A)
  162. if( m_id == REFERENCE )
  163. {
  164. wxString extended_text = tmp_text.GetText();
  165. extended_text.Append('?');
  166. const LIB_PART* parent = static_cast<const LIB_PART*>( m_Parent );
  167. if ( parent && ( parent->GetUnitCount() > 1 ) )
  168. extended_text.Append('A');
  169. tmp_text.SetText( extended_text );
  170. }
  171. tmp_text.SetTextPos( aTransform.TransformCoordinate( GetTextPos() ) );
  172. /* The text orientation may need to be flipped if the
  173. * transformation matrix causes xy axes to be flipped.
  174. * this simple algo works only for schematic matrix (rot 90 or/and mirror)
  175. */
  176. bool t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
  177. tmp_text.SetTextAngle( t1 ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT );
  178. return tmp_text.TextHitTest( aPosition );
  179. }
  180. EDA_ITEM* LIB_FIELD::Clone() const
  181. {
  182. LIB_FIELD* newfield = new LIB_FIELD( m_id );
  183. Copy( newfield );
  184. return (EDA_ITEM*) newfield;
  185. }
  186. void LIB_FIELD::Copy( LIB_FIELD* aTarget ) const
  187. {
  188. aTarget->m_Text = m_Text;
  189. aTarget->m_name = m_name;
  190. aTarget->SetEffects( *this );
  191. aTarget->SetParent( m_Parent );
  192. }
  193. int LIB_FIELD::compare( const LIB_ITEM& other ) const
  194. {
  195. wxASSERT( other.Type() == LIB_FIELD_T );
  196. const LIB_FIELD* tmp = ( LIB_FIELD* ) &other;
  197. if( m_id != tmp->m_id )
  198. return m_id - tmp->m_id;
  199. int result = m_Text.CmpNoCase( tmp->m_Text );
  200. if( result != 0 )
  201. return result;
  202. if( GetTextPos().x != tmp->GetTextPos().x )
  203. return GetTextPos().x - tmp->GetTextPos().x;
  204. if( GetTextPos().y != tmp->GetTextPos().y )
  205. return GetTextPos().y - tmp->GetTextPos().y;
  206. if( GetTextWidth() != tmp->GetTextWidth() )
  207. return GetTextWidth() - tmp->GetTextWidth();
  208. if( GetTextHeight() != tmp->GetTextHeight() )
  209. return GetTextHeight() - tmp->GetTextHeight();
  210. return 0;
  211. }
  212. void LIB_FIELD::SetOffset( const wxPoint& aOffset )
  213. {
  214. EDA_TEXT::Offset( aOffset );
  215. }
  216. bool LIB_FIELD::Inside( EDA_RECT& rect ) const
  217. {
  218. return rect.Intersects( GetBoundingBox() );
  219. }
  220. void LIB_FIELD::Move( const wxPoint& newPosition )
  221. {
  222. EDA_TEXT::SetTextPos( newPosition );
  223. }
  224. void LIB_FIELD::MirrorHorizontal( const wxPoint& center )
  225. {
  226. int x = GetTextPos().x;
  227. x -= center.x;
  228. x *= -1;
  229. x += center.x;
  230. SetTextX( x );
  231. }
  232. void LIB_FIELD::MirrorVertical( const wxPoint& center )
  233. {
  234. int y = GetTextPos().y;
  235. y -= center.y;
  236. y *= -1;
  237. y += center.y;
  238. SetTextY( y );
  239. }
  240. void LIB_FIELD::Rotate( const wxPoint& center, bool aRotateCCW )
  241. {
  242. int rot_angle = aRotateCCW ? -900 : 900;
  243. wxPoint pt = GetTextPos();
  244. RotatePoint( &pt, center, rot_angle );
  245. SetTextPos( pt );
  246. SetTextAngle( GetTextAngle() != 0.0 ? 0 : 900 );
  247. }
  248. void LIB_FIELD::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  249. const TRANSFORM& aTransform )
  250. {
  251. if( IsVoid() )
  252. return;
  253. /* Calculate the text orientation, according to the component
  254. * orientation/mirror */
  255. int orient = GetTextAngle();
  256. if( aTransform.y1 ) // Rotate component 90 deg.
  257. {
  258. if( orient == TEXT_ANGLE_HORIZ )
  259. orient = TEXT_ANGLE_VERT;
  260. else
  261. orient = TEXT_ANGLE_HORIZ;
  262. }
  263. EDA_RECT BoundaryBox = GetBoundingBox();
  264. BoundaryBox.RevertYAxis();
  265. EDA_TEXT_HJUSTIFY_T hjustify = GR_TEXT_HJUSTIFY_CENTER;
  266. EDA_TEXT_VJUSTIFY_T vjustify = GR_TEXT_VJUSTIFY_CENTER;
  267. wxPoint textpos = aTransform.TransformCoordinate( BoundaryBox.Centre() )
  268. + aOffset;
  269. aPlotter->Text( textpos, GetDefaultColor(), GetShownText(),
  270. orient, GetTextSize(),
  271. hjustify, vjustify,
  272. GetPenSize(), IsItalic(), IsBold() );
  273. }
  274. wxString LIB_FIELD::GetFullText( int unit ) const
  275. {
  276. if( m_id != REFERENCE )
  277. return GetText();
  278. wxString text = GetText();
  279. text << wxT( "?" );
  280. if( GetParent()->IsMulti() )
  281. text << LIB_PART::SubReference( unit );
  282. return text;
  283. }
  284. const EDA_RECT LIB_FIELD::GetBoundingBox() const
  285. {
  286. /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
  287. * calling GetTextBox() that works using top to bottom Y axis orientation.
  288. */
  289. EDA_RECT rect = GetTextBox( -1, -1, true );
  290. rect.RevertYAxis();
  291. // We are using now a bottom to top Y axis.
  292. wxPoint orig = rect.GetOrigin();
  293. wxPoint end = rect.GetEnd();
  294. RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
  295. RotatePoint( &end, GetTextPos(), -GetTextAngle() );
  296. rect.SetOrigin( orig );
  297. rect.SetEnd( end );
  298. // We are using now a top to bottom Y axis:
  299. rect.RevertYAxis();
  300. return rect;
  301. }
  302. void LIB_FIELD::ViewGetLayers( int aLayers[], int& aCount ) const
  303. {
  304. aCount = 1;
  305. switch( m_id )
  306. {
  307. case REFERENCE: aLayers[0] = LAYER_REFERENCEPART; break;
  308. case VALUE: aLayers[0] = LAYER_VALUEPART; break;
  309. default: aLayers[0] = LAYER_FIELDS; break;
  310. }
  311. }
  312. COLOR4D LIB_FIELD::GetDefaultColor()
  313. {
  314. switch( m_id )
  315. {
  316. case REFERENCE: return GetLayerColor( LAYER_REFERENCEPART );
  317. case VALUE: return GetLayerColor( LAYER_VALUEPART );
  318. default: return GetLayerColor( LAYER_FIELDS );
  319. }
  320. }
  321. void LIB_FIELD::Rotate()
  322. {
  323. if( InEditMode() )
  324. {
  325. m_rotate = true;
  326. }
  327. else
  328. {
  329. SetTextAngle( GetTextAngle() == TEXT_ANGLE_VERT ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT );
  330. }
  331. }
  332. wxString LIB_FIELD::GetName( bool aTranslate ) const
  333. {
  334. wxString name;
  335. switch( m_id )
  336. {
  337. case REFERENCE:
  338. if( aTranslate )
  339. name = _( "Reference" );
  340. else
  341. name = wxT( "Reference" );
  342. break;
  343. case VALUE:
  344. if( aTranslate )
  345. name = _( "Value" );
  346. else
  347. name = wxT( "Value" );
  348. break;
  349. case FOOTPRINT:
  350. if( aTranslate )
  351. name = _( "Footprint" );
  352. else
  353. name = wxT( "Footprint" );
  354. break;
  355. case DATASHEET:
  356. if( aTranslate )
  357. name = _( "Datasheet" );
  358. else
  359. name = wxT( "Datasheet" );
  360. break;
  361. default:
  362. if( m_name.IsEmpty() )
  363. {
  364. if( aTranslate )
  365. name.Printf( _( "Field%d" ), m_id );
  366. else
  367. name.Printf( wxT( "Field%d" ), m_id );
  368. }
  369. else
  370. name = m_name;
  371. }
  372. return name;
  373. }
  374. void LIB_FIELD::SetName( const wxString& aName )
  375. {
  376. // Mandatory field names are fixed.
  377. // So what? Why should the low level code be in charge of such a policy issue?
  378. // Besides, m_id is a relic that is untrustworthy now.
  379. if( m_id >=0 && m_id < MANDATORY_FIELDS )
  380. {
  381. DBG(printf( "trying to set a MANDATORY_FIELD's name\n" );)
  382. return;
  383. }
  384. if( m_name != aName )
  385. {
  386. m_name = aName;
  387. SetModified();
  388. }
  389. }
  390. void LIB_FIELD::SetText( const wxString& aText )
  391. {
  392. if( aText == GetText() )
  393. return;
  394. wxString oldValue( m_Text );
  395. wxString newValue( aText );
  396. if( InEditMode() )
  397. {
  398. m_Text = oldValue;
  399. m_savedText = newValue;
  400. m_updateText = true;
  401. }
  402. else
  403. {
  404. m_Text = newValue;
  405. }
  406. }
  407. wxString LIB_FIELD::GetSelectMenuText( EDA_UNITS_T aUnits ) const
  408. {
  409. return wxString::Format( _( "Field %s \"%s\"" ), GetName(), ShortenedShownText() );
  410. }
  411. void LIB_FIELD::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
  412. {
  413. wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED ) ) != 0,
  414. wxT( "Invalid edit mode for LIB_FIELD object." ) );
  415. if( aEditMode == IS_MOVED )
  416. {
  417. m_initialPos = GetTextPos();
  418. m_initialCursorPos = aPosition;
  419. }
  420. else
  421. {
  422. SetTextPos( aPosition );
  423. }
  424. m_Flags = aEditMode;
  425. }
  426. bool LIB_FIELD::ContinueEdit( const wxPoint aPosition )
  427. {
  428. wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED ) ) != 0, false,
  429. wxT( "Bad call to ContinueEdit(). Text is not being edited." ) );
  430. return false;
  431. }
  432. void LIB_FIELD::EndEdit( const wxPoint& aPosition, bool aAbort )
  433. {
  434. wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED ) ) != 0,
  435. wxT( "Bad call to EndEdit(). Text is not being edited." ) );
  436. m_Flags = 0;
  437. m_rotate = false;
  438. m_updateText = false;
  439. }
  440. void LIB_FIELD::CalcEdit( const wxPoint& aPosition )
  441. {
  442. if( m_rotate )
  443. {
  444. SetTextAngle( GetTextAngle() == TEXT_ANGLE_VERT ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT );
  445. m_rotate = false;
  446. }
  447. if( m_updateText )
  448. {
  449. std::swap( m_Text, m_savedText );
  450. m_updateText = false;
  451. }
  452. if( m_Flags == IS_NEW )
  453. {
  454. SetTextPos( aPosition );
  455. }
  456. else if( m_Flags == IS_MOVED )
  457. {
  458. Move( m_initialPos + aPosition - m_initialCursorPos );
  459. }
  460. }
  461. void LIB_FIELD::GetMsgPanelInfo( EDA_UNITS_T aUnits, MSG_PANEL_ITEMS& aList )
  462. {
  463. wxString msg;
  464. LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
  465. // Display style:
  466. msg = GetTextStyleName();
  467. aList.push_back( MSG_PANEL_ITEM( _( "Style" ), msg, MAGENTA ) );
  468. msg = MessageTextFromValue( aUnits, GetTextWidth(), true );
  469. aList.push_back( MSG_PANEL_ITEM( _( "Width" ), msg, BLUE ) );
  470. msg = MessageTextFromValue( aUnits, GetTextHeight(), true );
  471. aList.push_back( MSG_PANEL_ITEM( _( "Height" ), msg, BLUE ) );
  472. // Display field name (ref, value ...)
  473. aList.push_back( MSG_PANEL_ITEM( _( "Field" ), GetName(), BROWN ) );
  474. // Display field text:
  475. aList.push_back( MSG_PANEL_ITEM( _( "Value" ), GetShownText(), BROWN ) );
  476. }
  477. BITMAP_DEF LIB_FIELD::GetMenuImage() const
  478. {
  479. return move_xpm;
  480. }