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.

559 lines
14 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-2012 KiCad Developers, see change_log.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file lib_text.cpp
  25. */
  26. #include <fctsys.h>
  27. #include <gr_basic.h>
  28. #include <macros.h>
  29. #include <class_drawpanel.h>
  30. #include <plot_common.h>
  31. #include <drawtxt.h>
  32. #include <trigo.h>
  33. #include <wxstruct.h>
  34. #include <richio.h>
  35. #include <base_units.h>
  36. #include <msgpanel.h>
  37. #include <lib_draw_item.h>
  38. #include <general.h>
  39. #include <transform.h>
  40. #include <lib_text.h>
  41. LIB_TEXT::LIB_TEXT( LIB_PART * aParent ) :
  42. LIB_ITEM( LIB_TEXT_T, aParent ),
  43. EDA_TEXT()
  44. {
  45. m_Size = wxSize( 50, 50 );
  46. m_typeName = _( "Text" );
  47. m_rotate = false;
  48. m_updateText = false;
  49. }
  50. bool LIB_TEXT::Save( OUTPUTFORMATTER& aFormatter )
  51. {
  52. wxString text = m_Text;
  53. if( text.Contains( wxT( "~" ) ) || text.Contains( wxT( "\"" ) ) )
  54. {
  55. // convert double quote to similar-looking two apostrophes
  56. text.Replace( wxT( "\"" ), wxT( "''" ) );
  57. text = wxT( "\"" ) + text + wxT( "\"" );
  58. }
  59. else
  60. {
  61. // Spaces are not allowed in text because it is not double quoted:
  62. // changed to '~'
  63. text.Replace( wxT( " " ), wxT( "~" ) );
  64. }
  65. aFormatter.Print( 0, "T %g %d %d %d %d %d %d %s ", GetOrientation(), m_Pos.x, m_Pos.y,
  66. m_Size.x, m_Attributs, m_Unit, m_Convert, TO_UTF8( text ) );
  67. aFormatter.Print( 0, " %s %d", m_Italic ? "Italic" : "Normal", ( m_Bold > 0 ) ? 1 : 0 );
  68. char hjustify = 'C';
  69. if( m_HJustify == GR_TEXT_HJUSTIFY_LEFT )
  70. hjustify = 'L';
  71. else if( m_HJustify == GR_TEXT_HJUSTIFY_RIGHT )
  72. hjustify = 'R';
  73. char vjustify = 'C';
  74. if( m_VJustify == GR_TEXT_VJUSTIFY_BOTTOM )
  75. vjustify = 'B';
  76. else if( m_VJustify == GR_TEXT_VJUSTIFY_TOP )
  77. vjustify = 'T';
  78. aFormatter.Print( 0, " %c %c\n", hjustify, vjustify );
  79. return true;
  80. }
  81. bool LIB_TEXT::Load( LINE_READER& aLineReader, wxString& errorMsg )
  82. {
  83. int cnt, thickness = 0;
  84. char hjustify = 'C', vjustify = 'C';
  85. char buf[256];
  86. char tmp[256];
  87. char* line = (char*) aLineReader;
  88. double angle;
  89. buf[0] = 0;
  90. tmp[0] = 0; // For italic option, Not in old versions
  91. cnt = sscanf( line + 2, "%lf %d %d %d %d %d %d \"%[^\"]\" %255s %d %c %c",
  92. &angle, &m_Pos.x, &m_Pos.y, &m_Size.x, &m_Attributs,
  93. &m_Unit, &m_Convert, buf, tmp, &thickness, &hjustify,
  94. &vjustify );
  95. if( cnt >= 8 ) // if quoted loading failed, load as not quoted
  96. {
  97. m_Text = FROM_UTF8( buf );
  98. // convert two apostrophes back to double quote
  99. m_Text.Replace( wxT( "''" ), wxT( "\"" ) );
  100. }
  101. else
  102. {
  103. cnt = sscanf( line + 2, "%lf %d %d %d %d %d %d %255s %255s %d %c %c",
  104. &angle, &m_Pos.x, &m_Pos.y, &m_Size.x, &m_Attributs,
  105. &m_Unit, &m_Convert, buf, tmp, &thickness, &hjustify,
  106. &vjustify );
  107. if( cnt < 8 )
  108. {
  109. errorMsg.Printf( _( "Text only had %d parameters of the required 8" ), cnt );
  110. return false;
  111. }
  112. /* Convert '~' to spaces (only if text is not quoted). */
  113. m_Text = FROM_UTF8( buf );
  114. m_Text.Replace( wxT( "~" ), wxT( " " ) );
  115. }
  116. SetOrientation( angle );
  117. m_Size.y = m_Size.x;
  118. if( strnicmp( tmp, "Italic", 6 ) == 0 )
  119. m_Italic = true;
  120. if( thickness > 0 )
  121. {
  122. m_Bold = true;
  123. }
  124. switch( hjustify )
  125. {
  126. case 'L':
  127. m_HJustify = GR_TEXT_HJUSTIFY_LEFT;
  128. break;
  129. case 'C':
  130. m_HJustify = GR_TEXT_HJUSTIFY_CENTER;
  131. break;
  132. case 'R':
  133. m_HJustify = GR_TEXT_HJUSTIFY_RIGHT;
  134. break;
  135. }
  136. switch( vjustify )
  137. {
  138. case 'T':
  139. m_VJustify = GR_TEXT_VJUSTIFY_TOP;
  140. break;
  141. case 'C':
  142. m_VJustify = GR_TEXT_VJUSTIFY_CENTER;
  143. break;
  144. case 'B':
  145. m_VJustify = GR_TEXT_VJUSTIFY_BOTTOM;
  146. break;
  147. }
  148. return true;
  149. }
  150. bool LIB_TEXT::HitTest( const wxPoint& aPosition ) const
  151. {
  152. return HitTest( aPosition, 0, DefaultTransform );
  153. }
  154. bool LIB_TEXT::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
  155. {
  156. if( aThreshold < 0 )
  157. aThreshold = 0;
  158. EDA_TEXT tmp_text( *this );
  159. tmp_text.SetTextPosition( aTransform.TransformCoordinate( m_Pos ) );
  160. /* The text orientation may need to be flipped if the
  161. * transformation matrix causes xy axes to be flipped.
  162. * this simple algo works only for schematic matrix (rot 90 or/and mirror)
  163. */
  164. int t1 = ( aTransform.x1 != 0 ) ^ ( m_Orient != 0 );
  165. tmp_text.SetOrientation( t1 ? TEXT_ORIENT_HORIZ : TEXT_ORIENT_VERT );
  166. return tmp_text.TextHitTest( aPosition );
  167. }
  168. EDA_ITEM* LIB_TEXT::Clone() const
  169. {
  170. LIB_TEXT* newitem = new LIB_TEXT(NULL);
  171. newitem->m_Pos = m_Pos;
  172. newitem->m_Orient = m_Orient;
  173. newitem->m_Size = m_Size;
  174. newitem->m_Attributs = m_Attributs;
  175. newitem->m_Unit = m_Unit;
  176. newitem->m_Convert = m_Convert;
  177. newitem->m_Flags = m_Flags;
  178. newitem->m_Text = m_Text;
  179. newitem->m_Thickness = m_Thickness;
  180. newitem->m_Italic = m_Italic;
  181. newitem->m_Bold = m_Bold;
  182. newitem->m_HJustify = m_HJustify;
  183. newitem->m_VJustify = m_VJustify;
  184. return newitem;
  185. }
  186. int LIB_TEXT::compare( const LIB_ITEM& other ) const
  187. {
  188. wxASSERT( other.Type() == LIB_TEXT_T );
  189. const LIB_TEXT* tmp = ( LIB_TEXT* ) &other;
  190. int result = m_Text.CmpNoCase( tmp->m_Text );
  191. if( result != 0 )
  192. return result;
  193. if( m_Pos.x != tmp->m_Pos.x )
  194. return m_Pos.x - tmp->m_Pos.x;
  195. if( m_Pos.y != tmp->m_Pos.y )
  196. return m_Pos.y - tmp->m_Pos.y;
  197. if( m_Size.x != tmp->m_Size.x )
  198. return m_Size.x - tmp->m_Size.x;
  199. if( m_Size.y != tmp->m_Size.y )
  200. return m_Size.y - tmp->m_Size.y;
  201. return 0;
  202. }
  203. void LIB_TEXT::SetOffset( const wxPoint& aOffset )
  204. {
  205. m_Pos += aOffset;
  206. }
  207. bool LIB_TEXT::Inside( EDA_RECT& rect ) const
  208. {
  209. /*
  210. * FIXME: This should calculate the text size and justification and
  211. * use rectangle intersect.
  212. */
  213. return rect.Contains( m_Pos.x, -m_Pos.y );
  214. }
  215. void LIB_TEXT::Move( const wxPoint& newPosition )
  216. {
  217. m_Pos = newPosition;
  218. }
  219. void LIB_TEXT::MirrorHorizontal( const wxPoint& center )
  220. {
  221. m_Pos.x -= center.x;
  222. m_Pos.x *= -1;
  223. m_Pos.x += center.x;
  224. }
  225. void LIB_TEXT::MirrorVertical( const wxPoint& center )
  226. {
  227. m_Pos.y -= center.y;
  228. m_Pos.y *= -1;
  229. m_Pos.y += center.y;
  230. }
  231. void LIB_TEXT::Rotate( const wxPoint& center, bool aRotateCCW )
  232. {
  233. int rot_angle = aRotateCCW ? -900 : 900;
  234. RotatePoint( &m_Pos, center, rot_angle );
  235. m_Orient = m_Orient ? 0 : 900;
  236. }
  237. void LIB_TEXT::Plot( PLOTTER* plotter, const wxPoint& offset, bool fill,
  238. const TRANSFORM& aTransform )
  239. {
  240. wxASSERT( plotter != NULL );
  241. EDA_RECT bBox = GetBoundingBox();
  242. // convert coordinates from draw Y axis to libedit Y axis
  243. bBox.RevertYAxis();
  244. wxPoint txtpos = bBox.Centre();
  245. /* The text orientation may need to be flipped if the
  246. * transformation matrix causes xy axes to be flipped. */
  247. int t1 = ( aTransform.x1 != 0 ) ^ ( m_Orient != 0 );
  248. wxPoint pos = aTransform.TransformCoordinate( txtpos ) + offset;
  249. // Get color
  250. EDA_COLOR_T color;
  251. if( plotter->GetColorMode() ) // Used normal color or selected color
  252. color = IsSelected() ? GetItemSelectedColor() : GetDefaultColor();
  253. else
  254. color = BLACK;
  255. plotter->Text( pos, color, GetShownText(),
  256. t1 ? TEXT_ORIENT_HORIZ : TEXT_ORIENT_VERT,
  257. m_Size, GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_CENTER,
  258. GetPenSize(), m_Italic, m_Bold );
  259. }
  260. int LIB_TEXT::GetPenSize() const
  261. {
  262. int pensize = m_Thickness;
  263. if( pensize == 0 ) // Use default values for pen size
  264. {
  265. if( m_Bold )
  266. pensize = GetPenSizeForBold( m_Size.x );
  267. else
  268. pensize = GetDefaultLineThickness();
  269. }
  270. // Clip pen size for small texts:
  271. pensize = Clamp_Text_PenSize( pensize, m_Size, m_Bold );
  272. return pensize;
  273. }
  274. void LIB_TEXT::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
  275. EDA_COLOR_T aColor, GR_DRAWMODE aDrawMode, void* aData,
  276. const TRANSFORM& aTransform )
  277. {
  278. EDA_COLOR_T color = GetDefaultColor();
  279. if( aColor < 0 ) // Used normal color or selected color
  280. {
  281. if( IsSelected() )
  282. color = GetItemSelectedColor();
  283. }
  284. else
  285. {
  286. color = aColor;
  287. }
  288. GRSetDrawMode( aDC, aDrawMode );
  289. /* Calculate the text orientation, according to the component
  290. * orientation/mirror (needed when draw text in schematic)
  291. */
  292. int orient = m_Orient;
  293. if( aTransform.y1 ) // Rotate component 90 degrees.
  294. {
  295. if( orient == TEXT_ORIENT_HORIZ )
  296. orient = TEXT_ORIENT_VERT;
  297. else
  298. orient = TEXT_ORIENT_HORIZ;
  299. }
  300. /* Calculate the text justification, according to the component
  301. * orientation/mirror this is a bit complicated due to cumulative
  302. * calculations:
  303. * - numerous cases (mirrored or not, rotation)
  304. * - the DrawGraphicText function recalculate also H and H justifications
  305. * according to the text orientation.
  306. * - When a component is mirrored, the text is not mirrored and
  307. * justifications are complicated to calculate
  308. * so the more easily way is to use no justifications ( Centered text )
  309. * and use GetBoundaryBox to know the text coordinate considered as centered
  310. */
  311. EDA_RECT bBox = GetBoundingBox();
  312. // convert coordinates from draw Y axis to libedit Y axis:
  313. bBox.RevertYAxis();
  314. wxPoint txtpos = bBox.Centre();
  315. // Calculate pos according to mirror/rotation.
  316. txtpos = aTransform.TransformCoordinate( txtpos ) + aOffset;
  317. EDA_RECT* clipbox = aPanel? aPanel->GetClipBox() : NULL;
  318. DrawGraphicText( clipbox, aDC, txtpos, color, GetShownText(), orient, m_Size,
  319. GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_CENTER, GetPenSize(),
  320. m_Italic, m_Bold );
  321. /* Enable this to draw the bounding box around the text field to validate
  322. * the bounding box calculations.
  323. */
  324. #if 0
  325. // bBox already uses libedit Y axis.
  326. bBox = aTransform.TransformCoordinate( bBox );
  327. bBox.Move( aOffset );
  328. GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
  329. #endif
  330. }
  331. void LIB_TEXT::GetMsgPanelInfo( MSG_PANEL_ITEMS& aList )
  332. {
  333. wxString msg;
  334. LIB_ITEM::GetMsgPanelInfo( aList );
  335. msg = StringFromValue( g_UserUnit, m_Thickness, true );
  336. aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
  337. }
  338. const EDA_RECT LIB_TEXT::GetBoundingBox() const
  339. {
  340. /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
  341. * calling GetTextBox() that works using top to bottom Y axis orientation.
  342. */
  343. EDA_RECT rect = GetTextBox( -1, -1, true );
  344. rect.RevertYAxis();
  345. // We are using now a bottom to top Y axis.
  346. wxPoint orig = rect.GetOrigin();
  347. wxPoint end = rect.GetEnd();
  348. RotatePoint( &orig, m_Pos, -m_Orient );
  349. RotatePoint( &end, m_Pos, -m_Orient );
  350. rect.SetOrigin( orig );
  351. rect.SetEnd( end );
  352. // We are using now a top to bottom Y axis:
  353. rect.RevertYAxis();
  354. return rect;
  355. }
  356. void LIB_TEXT::Rotate()
  357. {
  358. if( InEditMode() )
  359. {
  360. m_rotate = true;
  361. }
  362. else
  363. {
  364. m_Orient = ( m_Orient == TEXT_ORIENT_VERT ) ? TEXT_ORIENT_HORIZ : TEXT_ORIENT_VERT;
  365. }
  366. }
  367. void LIB_TEXT::SetText( const wxString& aText )
  368. {
  369. if( aText == m_Text )
  370. return;
  371. if( InEditMode() )
  372. {
  373. m_savedText = aText;
  374. m_updateText = true;
  375. }
  376. else
  377. {
  378. m_Text = aText;
  379. }
  380. }
  381. wxString LIB_TEXT::GetSelectMenuText() const
  382. {
  383. wxString msg;
  384. msg.Printf( _( "Graphic Text %s" ), GetChars( ShortenedShownText() ) );
  385. return msg;
  386. }
  387. void LIB_TEXT::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
  388. {
  389. wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED ) ) != 0,
  390. wxT( "Invalid edit mode for LIB_TEXT object." ) );
  391. if( aEditMode == IS_MOVED )
  392. {
  393. m_initialPos = m_Pos;
  394. m_initialCursorPos = aPosition;
  395. SetEraseLastDrawItem();
  396. }
  397. else
  398. {
  399. m_Pos = aPosition;
  400. }
  401. m_Flags = aEditMode;
  402. }
  403. bool LIB_TEXT::ContinueEdit( const wxPoint aPosition )
  404. {
  405. wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED ) ) != 0, false,
  406. wxT( "Bad call to ContinueEdit(). Text is not being edited." ) );
  407. return false;
  408. }
  409. void LIB_TEXT::EndEdit( const wxPoint& aPosition, bool aAbort )
  410. {
  411. wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED ) ) != 0,
  412. wxT( "Bad call to EndEdit(). Text is not being edited." ) );
  413. m_Flags = 0;
  414. m_rotate = false;
  415. m_updateText = false;
  416. SetEraseLastDrawItem( false );
  417. }
  418. void LIB_TEXT::calcEdit( const wxPoint& aPosition )
  419. {
  420. if( m_rotate )
  421. {
  422. m_Orient = ( m_Orient == TEXT_ORIENT_VERT ) ? TEXT_ORIENT_HORIZ : TEXT_ORIENT_VERT;
  423. m_rotate = false;
  424. }
  425. if( m_updateText )
  426. {
  427. std::swap( m_Text, m_savedText );
  428. m_updateText = false;
  429. }
  430. if( m_Flags == IS_NEW )
  431. {
  432. SetEraseLastDrawItem();
  433. m_Pos = aPosition;
  434. }
  435. else if( m_Flags == IS_MOVED )
  436. {
  437. Move( m_initialPos + aPosition - m_initialCursorPos );
  438. }
  439. }