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.

302 lines
11 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2001 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2004-2023 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. #include <widgets/bitmap_button.h>
  25. #include <widgets/font_choice.h>
  26. #include <widgets/color_swatch.h>
  27. #include <symbol_edit_frame.h>
  28. #include <lib_text.h>
  29. #include <settings/settings_manager.h>
  30. #include <dialog_lib_text_properties.h>
  31. #include <symbol_editor/symbol_editor_settings.h>
  32. #include <tools/symbol_editor_drawing_tools.h>
  33. #include <scintilla_tricks.h>
  34. #include "confirm.h"
  35. DIALOG_LIB_TEXT_PROPERTIES::DIALOG_LIB_TEXT_PROPERTIES( SYMBOL_EDIT_FRAME* aParent,
  36. LIB_TEXT* aText ) :
  37. DIALOG_LIB_TEXT_PROPERTIES_BASE( aParent ),
  38. m_parent( aParent ),
  39. m_graphicText( aText ),
  40. m_textSize( aParent, m_textSizeLabel, m_textSizeCtrl, m_textSizeUnits, true )
  41. {
  42. COLOR_SETTINGS* colorSettings = m_parent->GetColorSettings();
  43. COLOR4D schematicBackground = colorSettings->GetColor( LAYER_SCHEMATIC_BACKGROUND );
  44. m_scintillaTricks = new SCINTILLA_TRICKS( m_StyledTextCtrl, wxT( "{}" ), false,
  45. [this]()
  46. {
  47. wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
  48. } );
  49. SetInitialFocus( m_StyledTextCtrl );
  50. m_separator1->SetIsSeparator();
  51. m_horizontal->SetIsRadioButton();
  52. m_horizontal->SetBitmap( KiBitmap( BITMAPS::text_horizontal ) );
  53. m_vertical->SetIsRadioButton();
  54. m_vertical->SetBitmap( KiBitmap( BITMAPS::text_vertical ) );
  55. m_separator2->SetIsSeparator();
  56. m_bold->SetIsCheckButton();
  57. m_bold->SetBitmap( KiBitmap( BITMAPS::text_bold ) );
  58. m_italic->SetIsCheckButton();
  59. m_italic->SetBitmap( KiBitmap( BITMAPS::text_italic ) );
  60. m_separator3->SetIsSeparator();
  61. m_hAlignLeft->SetIsRadioButton();
  62. m_hAlignLeft->SetBitmap( KiBitmap( BITMAPS::text_align_left ) );
  63. m_hAlignCenter->SetIsRadioButton();
  64. m_hAlignCenter->SetBitmap( KiBitmap( BITMAPS::text_align_center ) );
  65. m_hAlignRight->SetIsRadioButton();
  66. m_hAlignRight->SetBitmap( KiBitmap( BITMAPS::text_align_right ) );
  67. m_separator4->SetIsSeparator();
  68. m_vAlignTop->SetIsRadioButton();
  69. m_vAlignTop->SetBitmap( KiBitmap( BITMAPS::text_valign_top ) );
  70. m_vAlignCenter->SetIsRadioButton();
  71. m_vAlignCenter->SetBitmap( KiBitmap( BITMAPS::text_valign_center ) );
  72. m_vAlignBottom->SetIsRadioButton();
  73. m_vAlignBottom->SetBitmap( KiBitmap( BITMAPS::text_valign_bottom ) );
  74. m_separator5->SetIsSeparator();
  75. m_textColorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
  76. m_textColorSwatch->SetSwatchBackground( schematicBackground );
  77. m_horizontal->Bind( wxEVT_BUTTON, &DIALOG_LIB_TEXT_PROPERTIES::onOrientButton, this );
  78. m_vertical->Bind( wxEVT_BUTTON, &DIALOG_LIB_TEXT_PROPERTIES::onOrientButton, this );
  79. m_hAlignLeft->Bind( wxEVT_BUTTON, &DIALOG_LIB_TEXT_PROPERTIES::onHAlignButton, this );
  80. m_hAlignCenter->Bind( wxEVT_BUTTON, &DIALOG_LIB_TEXT_PROPERTIES::onHAlignButton, this );
  81. m_hAlignRight->Bind( wxEVT_BUTTON, &DIALOG_LIB_TEXT_PROPERTIES::onHAlignButton, this );
  82. m_vAlignTop->Bind( wxEVT_BUTTON, &DIALOG_LIB_TEXT_PROPERTIES::onVAlignButton, this );
  83. m_vAlignCenter->Bind( wxEVT_BUTTON, &DIALOG_LIB_TEXT_PROPERTIES::onVAlignButton, this );
  84. m_vAlignBottom->Bind( wxEVT_BUTTON, &DIALOG_LIB_TEXT_PROPERTIES::onVAlignButton, this );
  85. SetupStandardButtons();
  86. if( !aParent->IsSymbolEditable() || aParent->IsSymbolAlias() )
  87. {
  88. m_sdbSizerButtonsCancel->SetDefault();
  89. m_sdbSizerButtonsOK->SetLabel( _( "Read Only" ) );
  90. m_sdbSizerButtonsOK->Enable( false );
  91. }
  92. // Now all widgets have the size fixed, call FinishDialogSettings
  93. finishDialogSettings();
  94. }
  95. DIALOG_LIB_TEXT_PROPERTIES::~DIALOG_LIB_TEXT_PROPERTIES()
  96. {
  97. delete m_scintillaTricks;
  98. };
  99. bool DIALOG_LIB_TEXT_PROPERTIES::TransferDataToWindow()
  100. {
  101. wxCHECK( m_CommonUnit, false );
  102. LIB_SYMBOL* symbol = nullptr;
  103. if( m_graphicText )
  104. {
  105. symbol = m_graphicText->GetParent();
  106. wxCHECK( symbol, false );
  107. m_textSize.SetValue( m_graphicText->GetTextWidth() );
  108. m_StyledTextCtrl->SetValue( m_graphicText->GetText() );
  109. m_StyledTextCtrl->EmptyUndoBuffer();
  110. m_fontCtrl->SetFontSelection( m_graphicText->GetFont() );
  111. m_textColorSwatch->SetSwatchColor( m_graphicText->GetTextColor(), false );
  112. m_italic->Check( m_graphicText->IsItalic() );
  113. m_bold->Check( m_graphicText->IsBold() );
  114. m_privateCheckbox->SetValue( m_graphicText->IsPrivate() );
  115. m_CommonUnit->SetValue(
  116. symbol && symbol->GetUnitCount() > 1 && m_graphicText->GetUnit() == 0 );
  117. m_CommonConvert->SetValue( m_graphicText->GetConvert() == 0 );
  118. if( m_graphicText->GetTextAngle().IsHorizontal() )
  119. m_horizontal->Check();
  120. else
  121. m_vertical->Check();
  122. switch ( m_graphicText->GetHorizJustify() )
  123. {
  124. case GR_TEXT_H_ALIGN_LEFT: m_hAlignLeft->Check( true ); break;
  125. case GR_TEXT_H_ALIGN_CENTER: m_hAlignCenter->Check( true ); break;
  126. case GR_TEXT_H_ALIGN_RIGHT: m_hAlignRight->Check( true ); break;
  127. }
  128. switch ( m_graphicText->GetVertJustify() )
  129. {
  130. case GR_TEXT_V_ALIGN_TOP: m_vAlignTop->Check( true ); break;
  131. case GR_TEXT_V_ALIGN_CENTER: m_vAlignCenter->Check( true ); break;
  132. case GR_TEXT_V_ALIGN_BOTTOM: m_vAlignBottom->Check( true ); break;
  133. }
  134. }
  135. else
  136. {
  137. SYMBOL_EDITOR_SETTINGS* cfg = m_parent->GetSettings();
  138. auto* tools = m_parent->GetToolManager()->GetTool<SYMBOL_EDITOR_DRAWING_TOOLS>();
  139. symbol = m_parent->GetCurSymbol();
  140. wxCHECK( cfg && symbol && tools, false );
  141. m_textSize.SetValue( schIUScale.MilsToIU( cfg->m_Defaults.text_size ) );
  142. m_CommonUnit->SetValue( symbol->GetUnitCount() > 1 && !tools->GetDrawSpecificUnit() );
  143. m_CommonConvert->SetValue( !tools->GetDrawSpecificConvert() );
  144. if( tools->GetLastTextAngle().IsHorizontal() )
  145. m_horizontal->Check();
  146. else
  147. m_vertical->Check();
  148. }
  149. m_CommonUnit->Enable( symbol && symbol->GetUnitCount() > 1 );
  150. return true;
  151. }
  152. void DIALOG_LIB_TEXT_PROPERTIES::onOrientButton( wxCommandEvent& aEvent )
  153. {
  154. for( BITMAP_BUTTON* btn : { m_horizontal, m_vertical } )
  155. {
  156. if( btn->IsChecked() && btn != aEvent.GetEventObject() )
  157. btn->Check( false );
  158. }
  159. }
  160. void DIALOG_LIB_TEXT_PROPERTIES::onHAlignButton( wxCommandEvent& aEvent )
  161. {
  162. for( BITMAP_BUTTON* btn : { m_hAlignLeft, m_hAlignCenter, m_hAlignRight } )
  163. {
  164. if( btn->IsChecked() && btn != aEvent.GetEventObject() )
  165. btn->Check( false );
  166. }
  167. }
  168. void DIALOG_LIB_TEXT_PROPERTIES::onVAlignButton( wxCommandEvent& aEvent )
  169. {
  170. for( BITMAP_BUTTON* btn : { m_vAlignTop, m_vAlignCenter, m_vAlignBottom } )
  171. {
  172. if( btn->IsChecked() && btn != aEvent.GetEventObject() )
  173. btn->Check( false );
  174. }
  175. }
  176. bool DIALOG_LIB_TEXT_PROPERTIES::TransferDataFromWindow()
  177. {
  178. if( m_graphicText )
  179. {
  180. if( m_StyledTextCtrl->GetValue().IsEmpty() )
  181. {
  182. // Other text items do not have defined extents, and so will disappear if empty
  183. DisplayError( this, _( "Text can not be empty." ) );
  184. return false;
  185. }
  186. else
  187. {
  188. m_graphicText->SetText( m_StyledTextCtrl->GetValue() );
  189. }
  190. if( m_fontCtrl->HaveFontSelection() )
  191. {
  192. m_graphicText->SetFont( m_fontCtrl->GetFontSelection( m_bold->IsChecked(),
  193. m_italic->IsChecked() ) );
  194. }
  195. if( m_textSize.GetValue() != m_graphicText->GetTextWidth() )
  196. m_graphicText->SetTextSize( VECTOR2I( m_textSize.GetValue(), m_textSize.GetValue() ) );
  197. if( m_horizontal->IsChecked() )
  198. m_graphicText->SetTextAngle( ANGLE_HORIZONTAL );
  199. else
  200. m_graphicText->SetTextAngle( ANGLE_VERTICAL );
  201. m_graphicText->SetPrivate( m_privateCheckbox->GetValue() );
  202. if( !m_CommonUnit->GetValue() )
  203. m_graphicText->SetUnit( m_parent->GetUnit() );
  204. else
  205. m_graphicText->SetUnit( 0 );
  206. if( !m_CommonConvert->GetValue() )
  207. m_graphicText->SetConvert( m_parent->GetConvert() );
  208. else
  209. m_graphicText->SetConvert( 0 );
  210. m_graphicText->SetItalic( m_italic->IsChecked() );
  211. m_graphicText->SetBold( m_bold->IsChecked() );
  212. m_graphicText->SetTextColor( m_textColorSwatch->GetSwatchColor() );
  213. if( m_hAlignLeft->IsChecked() )
  214. m_graphicText->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
  215. else if( m_hAlignCenter->IsChecked() )
  216. m_graphicText->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
  217. else
  218. m_graphicText->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
  219. if( m_vAlignTop->IsChecked() )
  220. m_graphicText->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
  221. else if( m_vAlignCenter->IsChecked() )
  222. m_graphicText->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
  223. else
  224. m_graphicText->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
  225. // Record settings used for next time:
  226. auto* tools = m_parent->GetToolManager()->GetTool<SYMBOL_EDITOR_DRAWING_TOOLS>();
  227. tools->SetLastTextAngle( m_graphicText->GetTextAngle() );
  228. tools->SetDrawSpecificConvert( !m_CommonConvert->GetValue() );
  229. tools->SetDrawSpecificUnit( !m_CommonUnit->GetValue() );
  230. }
  231. m_parent->SetMsgPanel( m_graphicText );
  232. return true;
  233. }
  234. void DIALOG_LIB_TEXT_PROPERTIES::onMultiLineTCLostFocus( wxFocusEvent& event )
  235. {
  236. if( m_scintillaTricks )
  237. m_scintillaTricks->CancelAutocomplete();
  238. event.Skip();
  239. }