KiCad PCB EDA Suite
dialog_text_properties.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2004-2018 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 2010-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 #include <fctsys.h>
26 #include <gr_basic.h>
27 #include <class_drawpanel.h>
28 #include <pcbnew.h>
29 #include <pcb_edit_frame.h>
30 #include <draw_graphic_text.h>
31 #include <confirm.h>
32 #include <wx/valnum.h>
33 #include <pcb_layer_box_selector.h>
34 #include <board_commit.h>
35 #include <widgets/unit_binder.h>
36 #include <class_board.h>
37 #include <class_pcb_text.h>
38 #include <class_text_mod.h>
39 #include <class_module.h>
40 #include <class_dimension.h>
41 
42 #include <dialog_text_properties.h>
43 
44 
53  wxDC* aDC ) :
54  DIALOG_TEXT_PROPERTIES_BASE( aParent ),
55  m_Parent( aParent ), m_DC( aDC ), m_item( aItem ),
56  m_edaText( nullptr ), m_modText( nullptr ), m_pcbText( nullptr ),
57  m_textWidth( aParent, m_SizeXLabel, m_SizeXCtrl, m_SizeXUnits, true ),
58  m_textHeight( aParent, m_SizeYLabel, m_SizeYCtrl, m_SizeYUnits, true ),
59  m_thickness( aParent, m_ThicknessLabel, m_ThicknessCtrl, m_ThicknessUnits, true ),
60  m_posX( aParent, m_PositionXLabel, m_PositionXCtrl, m_PositionXUnits ),
61  m_posY( aParent, m_PositionYLabel, m_PositionYCtrl, m_PositionYUnits ),
62  m_OrientValidator( 1, &m_OrientValue )
63 {
64  wxString title;
65 
66  if( m_item->Type() == PCB_DIMENSION_T )
67  {
68  title = _( "Dimension Text Properties" );
69 
70  DIMENSION* dimension = (DIMENSION*) m_item;
71  m_edaText = &dimension->Text();
72  m_pcbText = &dimension->Text();
73 
75  m_SingleLineSizer->Show( false );
76  m_MultiLineSizer->Show( false );
77 
78  m_KeepUpright->Show( false );
79  m_statusLine->Show( false );
80  }
81  else if( m_item->Type() == PCB_MODULE_TEXT_T )
82  {
83  title = _( "Footprint Text Properties" );
84 
86  m_edaText = static_cast<EDA_TEXT*>( m_modText );
87 
88  switch( m_modText->GetType() )
89  {
90  case TEXTE_MODULE::TEXT_is_REFERENCE: m_TextLabel->SetLabel( _( "Reference:" ) ); break;
91  case TEXTE_MODULE::TEXT_is_VALUE: m_TextLabel->SetLabel( _( "Value:" ) ); break;
92  case TEXTE_MODULE::TEXT_is_DIVERS: m_TextLabel->SetLabel( _( "Text:" ) ); break;
93  }
94 
96  m_MultiLineSizer->Show( false );
97  m_DimensionTextSizer->Show( false );
98  }
99  else
100  {
101  title = _( "Text Properties" );
102 
103  m_pcbText = (TEXTE_PCB*) aItem;
104  m_edaText = static_cast<EDA_TEXT*>( m_pcbText );
105 
107  m_SingleLineSizer->Show( false );
108  m_DimensionTextSizer->Show( false );
109 
110  // This option make sense only for footprint texts,
111  // Texts on board are always visible:
112  m_Visible->SetValue( true );
113  m_Visible->Show( false );
114 
115  m_KeepUpright->Show( false );
116  m_statusLine->Show( false );
117  }
118 
119  SetTitle( title );
120  m_hash_key = title;
121 
122  // Configure the layers list selector. Note that footprints are built outside the current
123  // board and so we may need to show all layers if the text is on an unactivated layer.
126 
131 
132  m_OrientValue = 0.0;
133  m_OrientValidator.SetRange( -360.0, 360.0 );
134  m_OrientCtrl->SetValidator( m_OrientValidator );
135  m_OrientValidator.SetWindow( m_OrientCtrl );
136 
137  // Handle decimal separators in combo dropdown
138  for( size_t i = 0; i < m_OrientCtrl->GetCount(); ++i )
139  {
140  wxString item = m_OrientCtrl->GetString( i );
141  item.Replace( '.', localeconv()->decimal_point[0] );
142  m_OrientCtrl->SetString( i, item );
143  }
144 
145  // Set font sizes
146  wxFont infoFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
147  infoFont.SetSymbolicSize( wxFONTSIZE_SMALL );
148  m_statusLine->SetFont( infoFont );
149 
150  m_sdbSizerOK->SetDefault();
151 
152  // wxTextCtrls fail to generate wxEVT_CHAR events when the wxTE_MULTILINE flag is set,
153  // so we have to listen to wxEVT_CHAR_HOOK events instead.
154  Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXT_PROPERTIES::OnCharHook ), NULL, this );
155 
157 }
158 
159 
161 {
162  Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXT_PROPERTIES::OnCharHook ), NULL, this );
163 }
164 
165 
170 {
171  m_canvas->SetIgnoreMouseEvents( true );
172 #ifndef __WXMAC__
173  DIALOG_TEXT_PROPERTIES dlg( this, aText, aDC );
174 #else
175  // Avoid "writes" in the dialog, creates errors with WxOverlay and NSView
176  // Raising an Exception - Fixes #891347
177  DIALOG_TEXT_PROPERTIES dlg( this, aText, NULL );
178 #endif
179  dlg.ShowModal();
180  m_canvas->MoveCursorToCrossHair();
181  m_canvas->SetIgnoreMouseEvents( false );
182 }
183 
184 
185 void DIALOG_TEXT_PROPERTIES::OnCharHook( wxKeyEvent& aEvent )
186 {
187  if( aEvent.GetKeyCode() == WXK_TAB )
188  {
189  int flags = 0;
190 
191  if( !aEvent.ShiftDown() )
192  flags |= wxNavigationKeyEvent::IsForward;
193 
194  if( aEvent.ControlDown() )
195  flags |= wxNavigationKeyEvent::WinChange;
196 
197  NavigateIn( flags );
198  }
199  else if( aEvent.GetKeyCode() == WXK_RETURN )
200  {
201  if( FindFocus() == m_MultiLineText && !aEvent.ShiftDown() )
202  aEvent.Skip();
203  else
204  {
206  EndModal( wxID_OK );
207  }
208  }
209  else
210  {
211  aEvent.Skip();
212  }
213 }
214 
215 
217 {
218  EDA_UNITS_T units = UNSCALED_UNITS;
219  bool useMils;
220 
221  FetchUnitsFromString( m_DimensionText->GetValue(), units, useMils );
222 
223  if( units != UNSCALED_UNITS )
224  m_DimensionUnitsOpt->SetSelection( units == MILLIMETRES ? 2 : useMils ? 1 : 0 );
225 }
226 
227 
229 {
230  DIMENSION* dimension = (DIMENSION*) m_item;
231  EDA_UNITS_T units;
232  bool useMils;
233 
234  // Get default units in case dimension text doesn't contain units.
235  dimension->GetUnits( units, useMils );
236 
237  double value = ValueFromString( units, m_DimensionText->GetValue(), useMils );
238 
239  switch( event.GetSelection() )
240  {
241  case 0: units = INCHES; useMils = false; break;
242  case 1: units = INCHES; useMils = true; break;
243  case 2: units = MILLIMETRES; useMils = false; break;
244  default: break;
245  }
246 
247  m_DimensionText->SetValue( StringFromValue( units, value, true, useMils ) );
248 }
249 
250 
252 {
253  if( m_SingleLineText->IsShown() )
254  {
255  m_SingleLineText->SetValue( m_edaText->GetText() );
256 
258  SelectReferenceNumber( static_cast<wxTextEntry*>( m_SingleLineText ) );
259  else
260  m_SingleLineText->SetSelection( -1, -1 );
261  }
262  else if( m_MultiLineText->IsShown() )
263  {
264  m_MultiLineText->SetValue( m_edaText->GetText() );
265  m_MultiLineText->SetSelection( -1, -1 );
266  }
267  else if (m_DimensionText->IsShown() )
268  {
269  m_DimensionText->SetValue( m_edaText->GetText() );
270  m_DimensionText->SetSelection( -1, -1 );
271 
272  DIMENSION* dimension = (DIMENSION*) m_item;
273  EDA_UNITS_T units;
274  bool useMils;
275  dimension->GetUnits( units, useMils );
276 
277  m_DimensionUnitsOpt->SetSelection( units == MILLIMETRES ? 2 : useMils ? 1 : 0 );
278  }
279 
280  if( m_item->Type() == PCB_MODULE_TEXT_T && m_modText )
281  {
282  MODULE* module = dynamic_cast<MODULE*>( m_modText->GetParent() );
283  wxString msg;
284 
285  if( module )
286  {
287  msg.Printf( _("Footprint %s (%s), %s, rotated %.1f deg"),
288  module->GetReference(),
289  module->GetValue(),
290  module->IsFlipped() ? _( "back side (mirrored)" ) : _( "front side" ),
291  module->GetOrientation() / 10.0 );
292  }
293 
294  m_statusLine->SetLabel( msg );
295  }
296  else
297  {
298  m_statusLine->Show( false );
299  }
300 
302  {
303  wxMessageBox( _( "This item was on a non-existing or forbidden layer.\n"
304  "It has been moved to the first allowed layer." ) );
305  m_LayerSelectionCtrl->SetSelection( 0 );
306  }
307 
313 
314  m_Visible->SetValue( m_edaText->IsVisible() );
315  m_Italic->SetValue( m_edaText->IsItalic() );
317  m_JustifyChoice->SetSelection( (int) hJustify + 1 );
319  m_Mirrored->SetValue( m_edaText->IsMirrored() );
320 
321  if( m_modText )
322  m_KeepUpright->SetValue( m_modText->IsKeepUpright() );
323 
324  return DIALOG_TEXT_PROPERTIES_BASE::TransferDataToWindow();
325 }
326 
327 
329 {
330  if( !DIALOG_TEXT_PROPERTIES_BASE::TransferDataFromWindow() )
331  return false;
332 
335  return false;
336 
337  BOARD_COMMIT commit( m_Parent );
338  commit.Modify( m_item );
339 
340  // If no other command in progress, prepare undo command
341  // (for a command in progress, will be made later, at the completion of command)
342  int mask = EDA_ITEM_ALL_FLAGS - ( SELECTED | HIGHLIGHTED | BRIGHTENED );
343  bool pushCommit = ( m_item->GetFlags() & mask ) == 0;
344 
345  /* set flag in edit to force undo/redo/abort proper operation,
346  * and avoid new calls to SaveCopyInUndoList for the same text
347  * this can occurs when a text is moved, and then rotated, edited ..
348  */
349  if( !pushCommit )
350  m_item->SetFlags( IN_EDIT );
351 
352 #ifndef USE_WX_OVERLAY
353  // Erase old text on screen if context is available
354  if( m_DC )
355  {
357  }
358 #endif
359 
360  // Set the new text content
361  if( m_SingleLineText->IsShown() )
362  {
363  if( !m_SingleLineText->GetValue().IsEmpty() )
364  m_edaText->SetText( m_SingleLineText->GetValue() );
365  }
366  else if( m_MultiLineText->IsShown() )
367  {
368  if( !m_MultiLineText->GetValue().IsEmpty() )
369  m_edaText->SetText( m_MultiLineText->GetValue() );
370  }
371  else if( m_DimensionText->IsShown() )
372  {
373  if( !m_DimensionText->GetValue().IsEmpty() )
374  m_edaText->SetText( m_DimensionText->GetValue() );
375 
376  DIMENSION* dimension = (DIMENSION*) m_item;
377 
378  switch( m_DimensionUnitsOpt->GetSelection() )
379  {
380  case 0: dimension->SetUnits( INCHES, false ); break;
381  case 1: dimension->SetUnits( INCHES, true ); break;
382  case 2: dimension->SetUnits( MILLIMETRES, false ); break;
383  default: break;
384  }
385  }
386 
388 
391  m_edaText->SetTextPos( wxPoint( m_posX.GetValue(), m_posY.GetValue() ) );
392 
393  if( m_modText )
395 
396  // Test for acceptable values for thickness and size and clamp if fails
397  int maxthickness = Clamp_Text_PenSize( m_edaText->GetThickness(), m_edaText->GetTextSize() );
398 
399  if( m_edaText->GetThickness() > maxthickness )
400  {
401  DisplayError( this, _( "The text thickness is too large for the text size.\n"
402  "It will be clamped." ) );
403  m_edaText->SetThickness( maxthickness );
404  }
405 
406  m_edaText->SetVisible( m_Visible->GetValue() );
407  m_edaText->SetItalic( m_Italic->GetValue() );
409  m_edaText->SetMirrored( m_Mirrored->GetValue() );
410 
411  if( m_modText )
412  m_modText->SetKeepUpright( m_KeepUpright->GetValue() );
413 
414  switch( m_JustifyChoice->GetSelection() )
415  {
419  default: break;
420  }
421 
422 #ifndef USE_WX_OVERLAY
423  // Finally, display new text if there is a context to do so
424  if( m_DC )
425  {
427  }
428 #else
429  m_Parent->Refresh();
430 #endif
431 
432  if( pushCommit )
433  commit.Push( _( "Change text properties" ) );
434 
435  return true;
436 }
void SetMirrored(bool isMirrored)
Definition: eda_text.h:191
void SetTextAngle(double aAngle)
Definition: eda_text.h:169
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
BOARD_ITEM_CONTAINER * GetParent() const
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:258
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
void SetKeepUpright(bool aKeepUpright)
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aBold)
Function Clamp_Text_PenSize As a rule, pen width should not be >1/4em, otherwise the character will b...
#define IN_EDIT
Item currently edited.
Definition: base_struct.h:112
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:62
virtual EDA_DRAW_PANEL * GetCanvas() const
Definition: draw_frame.h:385
TEXTE_PCB class definition.
Class DIALOG_TEXT_PROPERTIES_BASE.
const wxPoint & GetTextPos() const
Definition: eda_text.h:237
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:121
bool IsItalic() const
Definition: eda_text.h:183
DIALOG_TEXT_PROPERTIES(PCB_BASE_EDIT_FRAME *aParent, BOARD_ITEM *aItem, wxDC *aDC=nullptr)
DIALOG_PCB_TEXT_PROPERTIES, derived from DIALOG_PCB_TEXT_PROPERTIES_BASE.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
This file is part of the common library.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
void GetUnits(EDA_UNITS_T &aUnits, bool &aUseMils) const
TEXT_TYPE GetType() const
std::string m_hash_key
Definition: dialog_shim.h:173
void SetNotAllowedLayerSet(LSET aMask)
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
void OnCharHook(wxKeyEvent &aEvent)
Class BOARD to handle a board.
bool IsLayerEnabled(PCB_LAYER_ID aLayer) const
Function IsLayerEnabled is a proxy function that calls the correspondent function in m_BoardSettings ...
Definition: class_board.h:446
virtual bool Validate(int aMin, int aMax, bool setFocusOnError=true)
Function Validate Validates the control against the given range, informing the user of any errors fou...
void SetItalic(bool isItalic)
Definition: eda_text.h:182
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:236
void SetVisible(bool aVisible)
Definition: eda_text.h:188
BOARD * GetBoard() const
const wxString & GetValue() const
Function GetValue.
Definition: class_module.h:496
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:227
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
double GetTextAngleDegrees() const
Definition: eda_text.h:179
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue, bool aUseMils)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application...
Definition: base_units.cpp:409
void FetchUnitsFromString(const wxString &aTextValue, EDA_UNITS_T &aUnits, bool &aUseMils)
Function FetchUnitsFromString writes any unit info found in the string to aUnits and aUseMils...
Definition: base_units.cpp:370
virtual void Draw(EDA_DRAW_PANEL *panel, wxDC *DC, GR_DRAWMODE aDrawMode, const wxPoint &offset=ZeroOffset)=0
Function Draw BOARD_ITEMs have their own color information.
void InstallTextOptionsFrame(BOARD_ITEM *aText, wxDC *aDC)
Routine for main window class to launch text properties dialog.
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:203
#define EDA_ITEM_ALL_FLAGS
Definition: base_struct.h:145
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:115
void OnDimensionUnitsChange(wxCommandEvent &event) override
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:167
virtual int GetValue()
Function GetValue Returns the current value in Internal Units.
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:127
bool IsKeepUpright()
DIMENSION class definition.
#define SELECTED
Definition: base_struct.h:121
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:155
Footprint text class description.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:256
double GetOrientation() const
Definition: class_module.h:188
void ShowNonActivatedLayers(bool aShow)
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:143
Definition: common.h:161
bool IsFlipped() const
function IsFlipped
Definition: class_module.h:258
#define BRIGHTENED
item is drawn with a bright contour
Definition: base_struct.h:138
int SetLayerSelection(LAYER_NUM layer)
#define TEXTS_MAX_SIZE
Maximum text size in internal units (10 inches)
Definition: pcbnew.h:68
void SetUnits(EDA_UNITS_T aUnits, bool aUseMils)
bool SetLayersHotkeys(bool value)
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:100
PCB_BASE_EDIT_FRAME * m_Parent
#define TEXTS_MIN_SIZE
Minimum text size in internal units (1 mil)
Definition: pcbnew.h:67
Definition: gr_basic.h:38
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void SetLocalCoord()
Set relative coordinates.
#define HIGHLIGHTED
item is drawn in normal colors, when the rest is darkened
Definition: base_struct.h:137
PCB_LAYER_BOX_SELECTOR * m_LayerSelectionCtrl
Common, abstract interface for edit frames.
const wxString & GetReference() const
Function GetReference.
Definition: class_module.h:462
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:206
bool IsMirrored() const
Definition: eda_text.h:192
bool IsVisible() const
Definition: eda_text.h:189
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
TEXTE_PCB & Text()
size_t i
Definition: json11.cpp:597
LAYER_NUM GetLayerSelection() const
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
wxString StringFromValue(EDA_UNITS_T aUnits, int aValue, bool aAddUnitSymbol, bool aUseMils)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:210
static LSET ForbiddenTextLayers()
Function ForbiddenTextLayers Layers which are now allowed to have text on them.
Definition: lset.cpp:796
Module description (excepted pads)
const wxSize & GetTextSize() const
Definition: eda_text.h:228
void OnDimensionTextChange(wxCommandEvent &event) override
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:245
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:813
wxFloatingPointValidator< double > m_OrientValidator
EDA_UNITS_T
Definition: common.h:160
Class DIMENSION.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:161
virtual void SetText(const wxString &aText)
Definition: eda_text.h:154