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.

517 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
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jean-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2004-2011 KiCad Developers, see change_log.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 eda_text.cpp
  26. * @brief Implementation of base KiCad text object.
  27. */
  28. #include <eda_text.h>
  29. #include <drawtxt.h>
  30. #include <macros.h>
  31. #include <trigo.h> // RotatePoint
  32. #include <class_drawpanel.h> // EDA_DRAW_PANEL
  33. // Conversion to application internal units defined at build time.
  34. #if defined( PCBNEW )
  35. #include <class_board_item.h>
  36. #elif defined( EESCHEMA )
  37. #include <sch_item_struct.h>
  38. #elif defined( GERBVIEW )
  39. #elif defined( PL_EDITOR )
  40. #include <base_units.h>
  41. #define FMT_IU Double2Str
  42. #else
  43. #error "Cannot resolve units formatting due to no definition of EESCHEMA or PCBNEW."
  44. #endif
  45. EDA_TEXT::EDA_TEXT( const wxString& text )
  46. {
  47. m_Size.x = m_Size.y = Mils2iu( DEFAULT_SIZE_TEXT ); // Width and height of font.
  48. m_Orient = 0; // Rotation angle in 0.1 degrees.
  49. m_Attributs = 0;
  50. m_Mirror = false; // display mirror if true
  51. m_HJustify = GR_TEXT_HJUSTIFY_CENTER; // Default horizontal justification is centered.
  52. m_VJustify = GR_TEXT_VJUSTIFY_CENTER; // Default vertical justification is centered.
  53. m_Thickness = 0; // thickness
  54. m_Italic = false; // true = italic shape.
  55. m_Bold = false;
  56. m_MultilineAllowed = false; // Set to true for multiline text.
  57. m_Text = text;
  58. }
  59. EDA_TEXT::EDA_TEXT( const EDA_TEXT& aText )
  60. {
  61. m_Pos = aText.m_Pos;
  62. m_Size = aText.m_Size;
  63. m_Orient = aText.m_Orient;
  64. m_Attributs = aText.m_Attributs;
  65. m_Mirror = aText.m_Mirror;
  66. m_HJustify = aText.m_HJustify;
  67. m_VJustify = aText.m_VJustify;
  68. m_Thickness = aText.m_Thickness;
  69. m_Italic = aText.m_Italic;
  70. m_Bold = aText.m_Bold;
  71. m_MultilineAllowed = aText.m_MultilineAllowed;
  72. m_Text = aText.m_Text;
  73. }
  74. EDA_TEXT::~EDA_TEXT()
  75. {
  76. }
  77. int EDA_TEXT::LenSize( const wxString& aLine ) const
  78. {
  79. return GraphicTextWidth( aLine, m_Size.x, m_Italic, m_Bold );
  80. }
  81. wxString EDA_TEXT::ShortenedShownText() const
  82. {
  83. wxString tmp = GetShownText();
  84. tmp.Replace( wxT( "\n" ), wxT( " " ) );
  85. tmp.Replace( wxT( "\r" ), wxT( " " ) );
  86. tmp.Replace( wxT( "\t" ), wxT( " " ) );
  87. if( tmp.Length() > 15 )
  88. tmp = tmp.Left( 12 ) + wxT( "..." );
  89. return tmp;
  90. }
  91. /**
  92. * Function GetInterline
  93. * return the distance between 2 text lines
  94. * has meaning only for multiline texts
  95. */
  96. int EDA_TEXT::GetInterline( int aTextThickness ) const
  97. {
  98. int thickness = aTextThickness <= 0 ? m_Thickness : aTextThickness;
  99. return (( m_Size.y * 14 ) / 10) + thickness;
  100. }
  101. EDA_RECT EDA_TEXT::GetTextBox( int aLine, int aThickness, bool aInvertY ) const
  102. {
  103. EDA_RECT rect;
  104. wxPoint pos;
  105. wxArrayString* list = NULL;
  106. wxString text = GetShownText();
  107. int thickness = ( aThickness < 0 ) ? m_Thickness : aThickness;
  108. int linecount = 1;
  109. if( m_MultilineAllowed )
  110. {
  111. list = wxStringSplit( text, '\n' );
  112. if ( list->GetCount() ) // GetCount() == 0 for void strings
  113. {
  114. if( aLine >= 0 && (aLine < (int)list->GetCount()) )
  115. text = list->Item( aLine );
  116. else
  117. text = list->Item( 0 );
  118. linecount = list->GetCount();
  119. }
  120. }
  121. // calculate the H and V size
  122. int dx = LenSize( text );
  123. int dy = GetInterline( aThickness );
  124. // Creates bounding box (rectangle) for an horizontal text
  125. wxSize textsize = wxSize( dx, dy );
  126. if( aInvertY )
  127. rect.SetOrigin( m_Pos.x, -m_Pos.y );
  128. else
  129. rect.SetOrigin( m_Pos );
  130. // extra dy interval for letters like j and y and ]
  131. int extra_dy = dy - m_Size.y;
  132. rect.Move( wxPoint( 0, -extra_dy / 2 ) ); // move origin by the half extra interval
  133. // for multiline texts and aLine < 0, merge all rectangles
  134. if( m_MultilineAllowed && list && aLine < 0 )
  135. {
  136. for( unsigned ii = 1; ii < list->GetCount(); ii++ )
  137. {
  138. text = list->Item( ii );
  139. dx = LenSize( text );
  140. textsize.x = std::max( textsize.x, dx );
  141. textsize.y += dy;
  142. }
  143. }
  144. delete list;
  145. rect.SetSize( textsize );
  146. /* Now, calculate the rect origin, according to text justification
  147. * At this point the rectangle origin is the text origin (m_Pos).
  148. * This is true only for left and top text justified texts (using top to bottom Y axis
  149. * orientation). and must be recalculated for others justifications
  150. * also, note the V justification is relative to the first line
  151. */
  152. switch( m_HJustify )
  153. {
  154. case GR_TEXT_HJUSTIFY_LEFT:
  155. if( m_Mirror )
  156. rect.SetX( rect.GetX() - rect.GetWidth() );
  157. break;
  158. case GR_TEXT_HJUSTIFY_CENTER:
  159. rect.SetX( rect.GetX() - (rect.GetWidth() / 2) );
  160. break;
  161. case GR_TEXT_HJUSTIFY_RIGHT:
  162. if( !m_Mirror )
  163. rect.SetX( rect.GetX() - rect.GetWidth() );
  164. break;
  165. }
  166. dy = m_Size.y + thickness;
  167. switch( m_VJustify )
  168. {
  169. case GR_TEXT_VJUSTIFY_TOP:
  170. break;
  171. case GR_TEXT_VJUSTIFY_CENTER:
  172. rect.SetY( rect.GetY() - ( dy / 2) );
  173. break;
  174. case GR_TEXT_VJUSTIFY_BOTTOM:
  175. rect.SetY( rect.GetY() - dy );
  176. break;
  177. }
  178. if( linecount > 1 )
  179. {
  180. int yoffset;
  181. linecount -= 1;
  182. switch( m_VJustify )
  183. {
  184. case GR_TEXT_VJUSTIFY_TOP:
  185. break;
  186. case GR_TEXT_VJUSTIFY_CENTER:
  187. yoffset = linecount * GetInterline() / 2;
  188. rect.SetY( rect.GetY() - yoffset );
  189. break;
  190. case GR_TEXT_VJUSTIFY_BOTTOM:
  191. yoffset = linecount * GetInterline( aThickness );
  192. rect.SetY( rect.GetY() - yoffset );
  193. break;
  194. }
  195. }
  196. rect.Inflate( thickness / 2 );
  197. rect.Normalize(); // Make h and v sizes always >= 0
  198. return rect;
  199. }
  200. bool EDA_TEXT::TextHitTest( const wxPoint& aPoint, int aAccuracy ) const
  201. {
  202. EDA_RECT rect = GetTextBox( -1 ); // Get the full text area.
  203. wxPoint location = aPoint;
  204. rect.Inflate( aAccuracy );
  205. RotatePoint( &location, m_Pos, -m_Orient );
  206. return rect.Contains( location );
  207. }
  208. bool EDA_TEXT::TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy ) const
  209. {
  210. EDA_RECT rect = aRect;
  211. rect.Inflate( aAccuracy );
  212. if( aContains )
  213. return rect.Contains( GetTextBox( -1 ) );
  214. return rect.Intersects( GetTextBox( -1 ) );
  215. }
  216. void EDA_TEXT::Draw( EDA_RECT* aClipBox, wxDC* aDC, const wxPoint& aOffset,
  217. EDA_COLOR_T aColor, GR_DRAWMODE aDrawMode,
  218. EDA_DRAW_MODE_T aFillMode, EDA_COLOR_T aAnchor_color )
  219. {
  220. if( m_MultilineAllowed )
  221. {
  222. std::vector<wxPoint> positions;
  223. wxArrayString* list = wxStringSplit( GetShownText(), '\n' );
  224. positions.reserve( list->Count() );
  225. GetPositionsOfLinesOfMultilineText(positions, list->Count() );
  226. for( unsigned ii = 0; ii < list->Count(); ii++ )
  227. {
  228. wxString& txt = list->Item( ii );
  229. drawOneLineOfText( aClipBox, aDC, aOffset, aColor,
  230. aDrawMode, aFillMode, txt, positions[ii] );
  231. }
  232. delete (list);
  233. }
  234. else
  235. drawOneLineOfText( aClipBox, aDC, aOffset, aColor,
  236. aDrawMode, aFillMode, GetShownText(), m_Pos );
  237. // Draw text anchor, if requested
  238. if( aAnchor_color != UNSPECIFIED_COLOR )
  239. {
  240. GRDrawAnchor( aClipBox, aDC,
  241. m_Pos.x + aOffset.x, m_Pos.y + aOffset.y,
  242. DIM_ANCRE_TEXTE, aAnchor_color );
  243. }
  244. }
  245. void EDA_TEXT::GetPositionsOfLinesOfMultilineText(
  246. std::vector<wxPoint>& aPositions, int aLineCount ) const
  247. {
  248. wxPoint pos = m_Pos; // Position of first line of the
  249. // multiline text according to
  250. // the center of the multiline text block
  251. wxPoint offset; // Offset to next line.
  252. offset.y = GetInterline();
  253. if( aLineCount > 1 )
  254. {
  255. switch( m_VJustify )
  256. {
  257. case GR_TEXT_VJUSTIFY_TOP:
  258. break;
  259. case GR_TEXT_VJUSTIFY_CENTER:
  260. pos.y -= ( aLineCount - 1 ) * offset.y / 2;
  261. break;
  262. case GR_TEXT_VJUSTIFY_BOTTOM:
  263. pos.y -= ( aLineCount - 1 ) * offset.y;
  264. break;
  265. }
  266. }
  267. // Rotate the position of the first line
  268. // around the center of the multiline text block
  269. RotatePoint( &pos, m_Pos, m_Orient );
  270. // Rotate the offset lines to increase happened in the right direction
  271. RotatePoint( &offset, m_Orient );
  272. for( int ii = 0; ii < aLineCount; ii++ )
  273. {
  274. aPositions.push_back( pos );
  275. pos += offset;
  276. }
  277. }
  278. void EDA_TEXT::drawOneLineOfText( EDA_RECT* aClipBox, wxDC* aDC,
  279. const wxPoint& aOffset, EDA_COLOR_T aColor,
  280. GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aFillMode,
  281. const wxString& aText, const wxPoint &aPos )
  282. {
  283. int width = m_Thickness;
  284. if( aFillMode == LINE )
  285. width = 0;
  286. if( aDrawMode != UNSPECIFIED_DRAWMODE )
  287. GRSetDrawMode( aDC, aDrawMode );
  288. if( aFillMode == SKETCH )
  289. width = -width;
  290. wxSize size = m_Size;
  291. if( m_Mirror )
  292. size.x = -size.x;
  293. DrawGraphicText( aClipBox, aDC, aOffset + aPos, aColor, aText, m_Orient, size,
  294. m_HJustify, m_VJustify, width, m_Italic, m_Bold );
  295. }
  296. wxString EDA_TEXT::GetTextStyleName()
  297. {
  298. int style = 0;
  299. if( m_Italic )
  300. style = 1;
  301. if( m_Bold )
  302. style += 2;
  303. wxString stylemsg[4] = {
  304. _("Normal"),
  305. _("Italic"),
  306. _("Bold"),
  307. _("Bold+Italic")
  308. };
  309. return stylemsg[style];
  310. }
  311. bool EDA_TEXT::IsDefaultFormatting() const
  312. {
  313. return ( ( m_Size.x == Mils2iu( DEFAULT_SIZE_TEXT ) )
  314. && ( m_Size.y == Mils2iu( DEFAULT_SIZE_TEXT ) )
  315. && ( m_Attributs == 0 )
  316. && ( m_Mirror == false )
  317. && ( m_HJustify == GR_TEXT_HJUSTIFY_CENTER )
  318. && ( m_VJustify == GR_TEXT_VJUSTIFY_CENTER )
  319. && ( m_Thickness == 0 )
  320. && ( m_Italic == false )
  321. && ( m_Bold == false )
  322. && ( m_MultilineAllowed == false ) );
  323. }
  324. void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
  325. throw( IO_ERROR )
  326. {
  327. #ifndef GERBVIEW // Gerbview does not use EDA_TEXT::Format
  328. // and does not define FMT_IU, used here
  329. // however this function should exist
  330. if( !IsDefaultFormatting() )
  331. {
  332. aFormatter->Print( aNestLevel+1, "(effects" );
  333. if( ( m_Size.x != Mils2iu( DEFAULT_SIZE_TEXT ) )
  334. || ( m_Size.y != Mils2iu( DEFAULT_SIZE_TEXT ) )
  335. || ( m_Thickness != 0 ) || m_Bold || m_Italic )
  336. {
  337. aFormatter->Print( 0, " (font" );
  338. // Add font support here at some point in the future.
  339. if( ( m_Size.x != Mils2iu( DEFAULT_SIZE_TEXT ) )
  340. || ( m_Size.y != Mils2iu( DEFAULT_SIZE_TEXT ) ) )
  341. aFormatter->Print( 0, " (size %s %s)", FMT_IU( m_Size.GetHeight() ).c_str(),
  342. FMT_IU( m_Size.GetWidth() ).c_str() );
  343. if( m_Thickness != 0 )
  344. aFormatter->Print( 0, " (thickness %s)", FMT_IU( GetThickness() ).c_str() );
  345. if( m_Bold )
  346. aFormatter->Print( 0, " bold" );
  347. if( IsItalic() )
  348. aFormatter->Print( 0, " italic" );
  349. aFormatter->Print( 0, ")");
  350. }
  351. if( m_Mirror || ( m_HJustify != GR_TEXT_HJUSTIFY_CENTER )
  352. || ( m_VJustify != GR_TEXT_VJUSTIFY_CENTER ) )
  353. {
  354. aFormatter->Print( 0, " (justify");
  355. if( m_HJustify != GR_TEXT_HJUSTIFY_CENTER )
  356. aFormatter->Print( 0, (m_HJustify == GR_TEXT_HJUSTIFY_LEFT) ? " left" : " right" );
  357. if( m_VJustify != GR_TEXT_VJUSTIFY_CENTER )
  358. aFormatter->Print( 0, (m_VJustify == GR_TEXT_VJUSTIFY_TOP) ? " top" : " bottom" );
  359. if( m_Mirror )
  360. aFormatter->Print( 0, " mirror" );
  361. aFormatter->Print( 0, ")" );
  362. }
  363. // As of now the only place this is used is in Eeschema to hide or show the text.
  364. if( m_Attributs )
  365. aFormatter->Print( 0, " hide" );
  366. aFormatter->Print( 0, ")\n" );
  367. }
  368. #endif
  369. }
  370. // Convert the text shape to a list of segment
  371. // each segment is stored as 2 wxPoints: its starting point and its ending point
  372. // we are using DrawGraphicText to create the segments.
  373. // and therefore a call-back function is needed
  374. static std::vector<wxPoint>* s_cornerBuffer;
  375. // This is a call back function, used by DrawGraphicText to put each segment in buffer
  376. static void addTextSegmToBuffer( int x0, int y0, int xf, int yf )
  377. {
  378. s_cornerBuffer->push_back( wxPoint( x0, y0 ) );
  379. s_cornerBuffer->push_back( wxPoint( xf, yf ) );
  380. }
  381. void EDA_TEXT::TransformTextShapeToSegmentList( std::vector<wxPoint>& aCornerBuffer ) const
  382. {
  383. wxSize size = GetSize();
  384. if( IsMirrored() )
  385. NEGATE( size.x );
  386. s_cornerBuffer = &aCornerBuffer;
  387. EDA_COLOR_T color = BLACK; // not actually used, but needed by DrawGraphicText
  388. if( IsMultilineAllowed() )
  389. {
  390. wxArrayString* list = wxStringSplit( GetShownText(), '\n' );
  391. std::vector<wxPoint> positions;
  392. positions.reserve( list->Count() );
  393. GetPositionsOfLinesOfMultilineText( positions, list->Count() );
  394. for( unsigned ii = 0; ii < list->Count(); ii++ )
  395. {
  396. wxString txt = list->Item( ii );
  397. DrawGraphicText( NULL, NULL, positions[ii], color,
  398. txt, GetOrientation(), size,
  399. GetHorizJustify(), GetVertJustify(),
  400. GetThickness(), IsItalic(),
  401. true, addTextSegmToBuffer );
  402. }
  403. delete list;
  404. }
  405. else
  406. {
  407. DrawGraphicText( NULL, NULL, GetTextPosition(), color,
  408. GetText(), GetOrientation(), size,
  409. GetHorizJustify(), GetVertJustify(),
  410. GetThickness(), IsItalic(),
  411. true, addTextSegmToBuffer );
  412. }
  413. }