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.

245 lines
6.2 KiB

16 years ago
16 years ago
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dialog_edit_label.cpp
  3. // Author: jean-pierre Charras
  4. // Modified by:
  5. // Created: 18/12/2008 15:46:26
  6. // Licence: GPL
  7. /////////////////////////////////////////////////////////////////////////////
  8. #include "fctsys.h"
  9. #include "wx/valgen.h"
  10. #include "wxEeschemaStruct.h"
  11. #include "common.h"
  12. #include "class_drawpanel.h"
  13. #include "general.h"
  14. #include "drawtxt.h"
  15. #include "confirm.h"
  16. #include "sch_text.h"
  17. #include "dialog_edit_label.h"
  18. /* Edit the properties of the text (Label, Global label, graphic text).. )
  19. * pointed by "aTextStruct"
  20. */
  21. void SCH_EDIT_FRAME::EditSchematicText( SCH_TEXT* aTextItem )
  22. {
  23. if( aTextItem == NULL )
  24. return;
  25. DialogLabelEditor dialog( this, aTextItem );
  26. dialog.ShowModal();
  27. }
  28. DialogLabelEditor::DialogLabelEditor( SCH_EDIT_FRAME* aParent, SCH_TEXT* aTextItem ) :
  29. DialogLabelEditor_Base( aParent )
  30. {
  31. m_Parent = aParent;
  32. m_CurrentText = aTextItem;
  33. InitDialog();
  34. GetSizer()->SetSizeHints( this );
  35. Layout();
  36. Fit();
  37. SetMinSize( GetBestSize() );
  38. Centre();
  39. }
  40. void DialogLabelEditor::InitDialog()
  41. {
  42. wxString msg;
  43. bool multiLine = false;
  44. if( m_CurrentText->m_MultilineAllowed )
  45. {
  46. m_textLabel = m_textLabelMultiLine;
  47. m_textLabelSingleLine->Show(false);
  48. multiLine = true;
  49. }
  50. else
  51. {
  52. m_textLabel = m_textLabelSingleLine;
  53. m_textLabelMultiLine->Show(false);
  54. }
  55. m_textLabel->SetValue( m_CurrentText->m_Text );
  56. m_textLabel->SetFocus();
  57. switch( m_CurrentText->Type() )
  58. {
  59. case SCH_GLOBAL_LABEL_T:
  60. SetTitle( _( "Global Label Properties" ) );
  61. break;
  62. case SCH_HIERARCHICAL_LABEL_T:
  63. SetTitle( _( "Hierarchical Label Properties" ) );
  64. break;
  65. case SCH_LABEL_T:
  66. SetTitle( _( "Label Properties" ) );
  67. break;
  68. case SCH_SHEET_PIN_T:
  69. SetTitle( _( "Hierarchical Sheet Pin Properties." ) );
  70. break;
  71. default:
  72. SetTitle( _( "Text Properties" ) );
  73. m_textLabel->Disconnect( wxEVT_COMMAND_TEXT_ENTER,
  74. wxCommandEventHandler ( DialogLabelEditor::OnEnterKey ),
  75. NULL, this );
  76. break;
  77. }
  78. int MINTEXTWIDTH = 40; // M's are big characters, a few establish a lot of width
  79. int max_len = 0;
  80. if ( !multiLine )
  81. {
  82. max_len =m_CurrentText->m_Text.Length();
  83. }
  84. else
  85. {
  86. // calculate the length of the biggest line
  87. // we cannot use the length of the entire text that has no meaning
  88. int curr_len = MINTEXTWIDTH;
  89. int imax = m_CurrentText->m_Text.Len();
  90. for( int count = 0; count < imax; count++ )
  91. {
  92. if( m_CurrentText->m_Text[count] == '\n' ||
  93. m_CurrentText->m_Text[count] == '\r' ) // new line
  94. {
  95. curr_len = 0;
  96. }
  97. else
  98. {
  99. curr_len++;
  100. if ( max_len < curr_len )
  101. max_len = curr_len;
  102. }
  103. }
  104. }
  105. if( max_len < MINTEXTWIDTH )
  106. max_len = MINTEXTWIDTH;
  107. wxString textWidth;
  108. textWidth.Append( 'M', MINTEXTWIDTH );
  109. EnsureTextCtrlWidth( m_textLabel, &textWidth );
  110. // Set validators
  111. m_TextOrient->SetSelection( m_CurrentText->GetOrientation() );
  112. m_TextShape->SetSelection( m_CurrentText->m_Shape );
  113. int style = 0;
  114. if( m_CurrentText->m_Italic )
  115. style = 1;
  116. if( m_CurrentText->m_Bold )
  117. style += 2;
  118. m_TextStyle->SetSelection( style );
  119. wxString units = ReturnUnitSymbol( g_UserUnit, wxT( "(%s)" ) );
  120. msg = _( "H" ) + units + _( " x W" ) + units;
  121. m_staticSizeUnits->SetLabel( msg );
  122. msg = ReturnStringFromValue( g_UserUnit, m_CurrentText->m_Size.x,
  123. m_Parent->m_InternalUnits );
  124. m_TextSize->SetValue( msg );
  125. if( m_CurrentText->Type() != SCH_GLOBAL_LABEL_T
  126. && m_CurrentText->Type() != SCH_HIERARCHICAL_LABEL_T )
  127. {
  128. m_TextShape->Show( false );
  129. }
  130. m_sdbSizer1OK->SetDefault();
  131. }
  132. /*!
  133. * wxTE_PROCESS_ENTER event handler for m_textLabel
  134. */
  135. void DialogLabelEditor::OnEnterKey( wxCommandEvent& aEvent )
  136. {
  137. TextPropertiesAccept( aEvent );
  138. }
  139. /*!
  140. * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
  141. */
  142. void DialogLabelEditor::OnOkClick( wxCommandEvent& aEvent )
  143. {
  144. TextPropertiesAccept( aEvent );
  145. }
  146. /*!
  147. * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
  148. */
  149. void DialogLabelEditor::OnCancelClick( wxCommandEvent& aEvent )
  150. {
  151. m_Parent->DrawPanel->MoveCursorToCrossHair();
  152. EndModal( wxID_CANCEL );
  153. }
  154. void DialogLabelEditor::TextPropertiesAccept( wxCommandEvent& aEvent )
  155. {
  156. wxString text;
  157. int value;
  158. /* save old text in undo list if not already in edit */
  159. if( m_CurrentText->m_Flags == 0 )
  160. m_Parent->SaveCopyInUndoList( m_CurrentText, UR_CHANGED );
  161. m_Parent->DrawPanel->RefreshDrawingRect( m_CurrentText->GetBoundingBox() );
  162. text = m_textLabel->GetValue();
  163. if( !text.IsEmpty() )
  164. m_CurrentText->m_Text = text;
  165. else if( (m_CurrentText->m_Flags & IS_NEW) == 0 )
  166. DisplayError( this, _( "Empty Text!" ) );
  167. m_CurrentText->SetOrientation( m_TextOrient->GetSelection() );
  168. text = m_TextSize->GetValue();
  169. value = ReturnValueFromString( g_UserUnit, text, m_Parent->m_InternalUnits );
  170. m_CurrentText->m_Size.x = m_CurrentText->m_Size.y = value;
  171. if( m_TextShape )
  172. m_CurrentText->m_Shape = m_TextShape->GetSelection();
  173. int style = m_TextStyle->GetSelection();
  174. if( ( style & 1 ) )
  175. m_CurrentText->m_Italic = 1;
  176. else
  177. m_CurrentText->m_Italic = 0;
  178. if( ( style & 2 ) )
  179. {
  180. m_CurrentText->m_Bold = true;
  181. m_CurrentText->m_Thickness = GetPenSizeForBold( m_CurrentText->m_Size.x );
  182. }
  183. else
  184. {
  185. m_CurrentText->m_Bold = false;
  186. m_CurrentText->m_Thickness = 0;
  187. }
  188. m_Parent->OnModify();
  189. /* Make the text size as new default size if it is a new text */
  190. if( (m_CurrentText->m_Flags & IS_NEW) != 0 )
  191. g_DefaultTextLabelSize = m_CurrentText->m_Size.x;
  192. m_Parent->DrawPanel->RefreshDrawingRect( m_CurrentText->GetBoundingBox() );
  193. m_Parent->DrawPanel->MoveCursorToCrossHair();
  194. EndModal( wxID_OK );
  195. }