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 <dialog_text_properties.h>
26 
27 #include <class_drawpanel.h>
28 #include <confirm.h>
29 #include <draw_graphic_text.h>
30 #include <fctsys.h>
31 #include <gr_basic.h>
32 
33 #include <widgets/tab_traversal.h>
34 #include <widgets/unit_binder.h>
35 
36 #include <board_commit.h>
37 #include <class_board.h>
38 #include <class_dimension.h>
39 #include <class_module.h>
40 #include <class_pcb_text.h>
41 #include <class_text_mod.h>
42 #include <pcb_edit_frame.h>
43 #include <pcb_layer_box_selector.h>
44 #include <pcbnew.h>
45 
46 #include <wx/valnum.h>
47 
48 
57  wxDC* aDC ) :
58  DIALOG_TEXT_PROPERTIES_BASE( aParent ),
59  m_Parent( aParent ), m_DC( aDC ), m_item( aItem ),
60  m_edaText( nullptr ), m_modText( nullptr ), m_pcbText( nullptr ),
61  m_textWidth( aParent, m_SizeXLabel, m_SizeXCtrl, m_SizeXUnits, true ),
62  m_textHeight( aParent, m_SizeYLabel, m_SizeYCtrl, m_SizeYUnits, true ),
63  m_thickness( aParent, m_ThicknessLabel, m_ThicknessCtrl, m_ThicknessUnits, true ),
64  m_posX( aParent, m_PositionXLabel, m_PositionXCtrl, m_PositionXUnits ),
65  m_posY( aParent, m_PositionYLabel, m_PositionYCtrl, m_PositionYUnits ),
66  m_OrientValidator( 1, &m_OrientValue )
67 {
68  wxString title;
69 
70  if( m_item->Type() == PCB_DIMENSION_T )
71  {
72  title = _( "Dimension Text Properties" );
73 
74  DIMENSION* dimension = (DIMENSION*) m_item;
75  m_edaText = &dimension->Text();
76  m_pcbText = &dimension->Text();
77 
79  m_SingleLineSizer->Show( false );
80  m_MultiLineSizer->Show( false );
81 
82  m_KeepUpright->Show( false );
83  m_statusLine->Show( false );
84  }
85  else if( m_item->Type() == PCB_MODULE_TEXT_T )
86  {
87  title = _( "Footprint Text Properties" );
88 
90  m_edaText = static_cast<EDA_TEXT*>( m_modText );
91 
92  switch( m_modText->GetType() )
93  {
94  case TEXTE_MODULE::TEXT_is_REFERENCE: m_TextLabel->SetLabel( _( "Reference:" ) ); break;
95  case TEXTE_MODULE::TEXT_is_VALUE: m_TextLabel->SetLabel( _( "Value:" ) ); break;
96  case TEXTE_MODULE::TEXT_is_DIVERS: m_TextLabel->SetLabel( _( "Text:" ) ); break;
97  }
98 
100  m_MultiLineSizer->Show( false );
101  m_DimensionTextSizer->Show( false );
102  }
103  else
104  {
105  title = _( "Text Properties" );
106 
107  m_pcbText = (TEXTE_PCB*) aItem;
108  m_edaText = static_cast<EDA_TEXT*>( m_pcbText );
109 
111  m_SingleLineSizer->Show( false );
112  m_DimensionTextSizer->Show( false );
113 
114  // This option make sense only for footprint texts,
115  // Texts on board are always visible:
116  m_Visible->SetValue( true );
117  m_Visible->Show( false );
118 
119  m_KeepUpright->Show( false );
120  m_statusLine->Show( false );
121  }
122 
123  SetTitle( title );
124  m_hash_key = title;
125 
126  // Configure the layers list selector. Note that footprints are built outside the current
127  // board and so we may need to show all layers if the text is on an unactivated layer.
130 
135 
136  m_OrientValue = 0.0;
137  m_OrientValidator.SetRange( -360.0, 360.0 );
138  m_OrientCtrl->SetValidator( m_OrientValidator );
139  m_OrientValidator.SetWindow( m_OrientCtrl );
140 
141  // Handle decimal separators in combo dropdown
142  for( size_t i = 0; i < m_OrientCtrl->GetCount(); ++i )
143  {
144  wxString item = m_OrientCtrl->GetString( i );
145  item.Replace( '.', localeconv()->decimal_point[0] );
146  m_OrientCtrl->SetString( i, item );
147  }
148 
149  // Set font sizes
150  wxFont infoFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
151  infoFont.SetSymbolicSize( wxFONTSIZE_SMALL );
152  m_statusLine->SetFont( infoFont );
153 
154  m_sdbSizerOK->SetDefault();
155 
156  // Tab down the left side first
158  m_LayerLabel,
160  m_SizeXCtrl,
161  m_SizeYCtrl,
165  m_Visible,
166  m_Italic,
168  m_OrientCtrl,
169  m_Mirrored,
171  } );
172 
173  // wxTextCtrls fail to generate wxEVT_CHAR events when the wxTE_MULTILINE flag is set,
174  // so we have to listen to wxEVT_CHAR_HOOK events instead.
175  Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXT_PROPERTIES::OnCharHook ), NULL, this );
176 
178 }
179 
180 
182 {
183  Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXT_PROPERTIES::OnCharHook ), NULL, this );
184 }
185 
186 
191 {
193 #ifndef __WXMAC__
194  DIALOG_TEXT_PROPERTIES dlg( this, aText, aDC );
195 #else
196  // Avoid "writes" in the dialog, creates errors with WxOverlay and NSView
197  // Raising an Exception - Fixes #891347
198  DIALOG_TEXT_PROPERTIES dlg( this, aText, NULL );
199 #endif
200  dlg.ShowModal();
202  m_canvas->SetIgnoreMouseEvents( false );
203 }
204 
205 
206 void DIALOG_TEXT_PROPERTIES::OnCharHook( wxKeyEvent& aEvent )
207 {
208  if( aEvent.GetKeyCode() == WXK_TAB )
209  {
210  int flags = 0;
211 
212  if( !aEvent.ShiftDown() )
213  flags |= wxNavigationKeyEvent::IsForward;
214 
215  if( aEvent.ControlDown() )
216  flags |= wxNavigationKeyEvent::WinChange;
217 
218  NavigateIn( flags );
219  }
220  else if( aEvent.GetKeyCode() == WXK_RETURN )
221  {
222  if( FindFocus() == m_MultiLineText && !aEvent.ShiftDown() )
223  aEvent.Skip();
224  else
225  {
227  EndModal( wxID_OK );
228  }
229  }
230  else
231  {
232  aEvent.Skip();
233  }
234 }
235 
236 
238 {
239  EDA_UNITS_T units = UNSCALED_UNITS;
240  bool useMils;
241 
242  FetchUnitsFromString( m_DimensionText->GetValue(), units, useMils );
243 
244  if( units != UNSCALED_UNITS )
245  m_DimensionUnitsOpt->SetSelection( units == MILLIMETRES ? 2 : useMils ? 1 : 0 );
246 }
247 
248 
250 {
251  DIMENSION* dimension = (DIMENSION*) m_item;
252  EDA_UNITS_T units;
253  bool useMils;
254 
255  // Get default units in case dimension text doesn't contain units.
256  dimension->GetUnits( units, useMils );
257 
258  double value = ValueFromString( units, m_DimensionText->GetValue(), useMils );
259 
260  switch( event.GetSelection() )
261  {
262  case 0: units = INCHES; useMils = false; break;
263  case 1: units = INCHES; useMils = true; break;
264  case 2: units = MILLIMETRES; useMils = false; break;
265  default: break;
266  }
267 
268  m_DimensionText->SetValue( StringFromValue( units, value, true, useMils ) );
269 }
270 
271 
273 {
274  if( m_SingleLineText->IsShown() )
275  {
276  m_SingleLineText->SetValue( m_edaText->GetText() );
277 
279  SelectReferenceNumber( static_cast<wxTextEntry*>( m_SingleLineText ) );
280  else
281  m_SingleLineText->SetSelection( -1, -1 );
282  }
283  else if( m_MultiLineText->IsShown() )
284  {
285  m_MultiLineText->SetValue( m_edaText->GetText() );
286  m_MultiLineText->SetSelection( -1, -1 );
287  }
288  else if (m_DimensionText->IsShown() )
289  {
290  m_DimensionText->SetValue( m_edaText->GetText() );
291  m_DimensionText->SetSelection( -1, -1 );
292 
293  DIMENSION* dimension = (DIMENSION*) m_item;
294  EDA_UNITS_T units;
295  bool useMils;
296  dimension->GetUnits( units, useMils );
297 
298  m_DimensionUnitsOpt->SetSelection( units == MILLIMETRES ? 2 : useMils ? 1 : 0 );
299  }
300 
301  if( m_item->Type() == PCB_MODULE_TEXT_T && m_modText )
302  {
303  MODULE* module = dynamic_cast<MODULE*>( m_modText->GetParent() );
304  wxString msg;
305 
306  if( module )
307  {
308  msg.Printf( _("Footprint %s (%s), %s, rotated %.1f deg"),
309  module->GetReference(),
310  module->GetValue(),
311  module->IsFlipped() ? _( "back side (mirrored)" ) : _( "front side" ),
312  module->GetOrientation() / 10.0 );
313  }
314 
315  m_statusLine->SetLabel( msg );
316  }
317  else
318  {
319  m_statusLine->Show( false );
320  }
321 
323  {
324  wxMessageBox( _( "This item was on a non-existing or forbidden layer.\n"
325  "It has been moved to the first allowed layer." ) );
326  m_LayerSelectionCtrl->SetSelection( 0 );
327  }
328 
334 
335  m_Visible->SetValue( m_edaText->IsVisible() );
336  m_Italic->SetValue( m_edaText->IsItalic() );
338  m_JustifyChoice->SetSelection( (int) hJustify + 1 );
340  m_Mirrored->SetValue( m_edaText->IsMirrored() );
341 
342  if( m_modText )
343  m_KeepUpright->SetValue( m_modText->IsKeepUpright() );
344 
345  return DIALOG_TEXT_PROPERTIES_BASE::TransferDataToWindow();
346 }
347 
348 
350 {
351  if( !DIALOG_TEXT_PROPERTIES_BASE::TransferDataFromWindow() )
352  return false;
353 
356  return false;
357 
358  BOARD_COMMIT commit( m_Parent );
359  commit.Modify( m_item );
360 
361  // If no other command in progress, prepare undo command
362  // (for a command in progress, will be made later, at the completion of command)
363  bool pushCommit = ( m_item->GetEditFlags() == 0 );
364 
365  /* set flag in edit to force undo/redo/abort proper operation,
366  * and avoid new calls to SaveCopyInUndoList for the same text
367  * this can occurs when a text is moved, and then rotated, edited ..
368  */
369  if( !pushCommit )
370  m_item->SetFlags( IN_EDIT );
371 
372 #ifndef USE_WX_OVERLAY
373  // Erase old text on screen if context is available
374  if( m_DC )
375  {
377  }
378 #endif
379 
380  // Set the new text content
381  if( m_SingleLineText->IsShown() )
382  {
383  if( !m_SingleLineText->GetValue().IsEmpty() )
384  m_edaText->SetText( m_SingleLineText->GetValue() );
385  }
386  else if( m_MultiLineText->IsShown() )
387  {
388  if( !m_MultiLineText->GetValue().IsEmpty() )
389  m_edaText->SetText( m_MultiLineText->GetValue() );
390  }
391  else if( m_DimensionText->IsShown() )
392  {
393  if( !m_DimensionText->GetValue().IsEmpty() )
394  m_edaText->SetText( m_DimensionText->GetValue() );
395 
396  DIMENSION* dimension = (DIMENSION*) m_item;
397 
398  switch( m_DimensionUnitsOpt->GetSelection() )
399  {
400  case 0: dimension->SetUnits( INCHES, false ); break;
401  case 1: dimension->SetUnits( INCHES, true ); break;
402  case 2: dimension->SetUnits( MILLIMETRES, false ); break;
403  default: break;
404  }
405  }
406 
408 
411  m_edaText->SetTextPos( wxPoint( m_posX.GetValue(), m_posY.GetValue() ) );
412 
413  if( m_modText )
415 
416  // Test for acceptable values for thickness and size and clamp if fails
417  int maxthickness = Clamp_Text_PenSize( m_edaText->GetThickness(), m_edaText->GetTextSize() );
418 
419  if( m_edaText->GetThickness() > maxthickness )
420  {
421  DisplayError( this, _( "The text thickness is too large for the text size.\n"
422  "It will be clamped." ) );
423  m_edaText->SetThickness( maxthickness );
424  }
425 
426  m_edaText->SetVisible( m_Visible->GetValue() );
427  m_edaText->SetItalic( m_Italic->GetValue() );
429  m_edaText->SetMirrored( m_Mirrored->GetValue() );
430 
431  if( m_modText )
432  m_modText->SetKeepUpright( m_KeepUpright->GetValue() );
433 
434  switch( m_JustifyChoice->GetSelection() )
435  {
439  default: break;
440  }
441 
442 #ifndef USE_WX_OVERLAY
443  // Finally, display new text if there is a context to do so
444  if( m_DC )
445  {
447  }
448 #else
449  m_Parent->Refresh();
450 #endif
451 
452  if( pushCommit )
453  commit.Push( _( "Change text properties" ) );
454 
455  return true;
456 }
void SetMirrored(bool isMirrored)
Definition: eda_text.h:195
void SetTextAngle(double aAngle)
Definition: eda_text.h:173
Functions for manipulating tab traversal in forms and dialogs.
double GetOrientation() const
Definition: class_module.h:188
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:63
bool IsMirrored() const
Definition: eda_text.h:196
TEXTE_PCB class definition.
Class DIALOG_TEXT_PROPERTIES_BASE.
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:118
bool IsFlipped() const
function IsFlipped
Definition: class_module.h:258
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,...
std::string m_hash_key
Definition: dialog_shim.h:174
void SetNotAllowedLayerSet(LSET aMask)
void OnCharHook(wxKeyEvent &aEvent)
bool IsVisible() const
Definition: eda_text.h:193
Class BOARD to handle a board.
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:186
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:240
void SetVisible(bool aVisible)
Definition: eda_text.h:192
const wxString GetValue() const
Function GetValue.
Definition: class_module.h:487
void SetControlsTabOrder(const std::vector< wxWindow * > &aControlsInTabOrder)
Set a list of controls to have a defined sequential tab order.
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:171
virtual const wxString GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:147
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:231
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
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 MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
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:441
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.
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:116
void OnDimensionUnitsChange(wxCommandEvent &event) override
const wxString GetReference() const
Function GetReference.
Definition: class_module.h:462
virtual int GetValue()
Function GetValue Returns the current value in Internal Units.
bool IsItalic() const
Definition: eda_text.h:187
bool IsKeepUpright()
DIMENSION class definition.
LAYER_NUM GetLayerSelection() const
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:159
Footprint text class description.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:259
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:207
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:64
STATUS_FLAGS GetEditFlags() const
Definition: base_struct.h:263
void ShowNonActivatedLayers(bool aShow)
const wxSize & GetTextSize() const
Definition: eda_text.h:232
Definition: common.h:158
TEXT_TYPE GetType() const
int SetLayerSelection(LAYER_NUM layer)
#define TEXTS_MAX_SIZE
Maximum text size in internal units (10 inches)
Definition: pcbnew.h:62
virtual EDA_DRAW_PANEL * GetCanvas() const
Definition: draw_frame.h:399
void SetUnits(EDA_UNITS_T aUnits, bool aUseMils)
bool SetLayersHotkeys(bool value)
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:98
PCB_BASE_EDIT_FRAME * m_Parent
#define TEXTS_MIN_SIZE
Minimum text size in internal units (1 mil)
Definition: pcbnew.h:61
Definition: gr_basic.h:38
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:128
void SetLocalCoord()
Set relative coordinates.
double GetTextAngleDegrees() const
Definition: eda_text.h:183
PCB_LAYER_BOX_SELECTOR * m_LayerSelectionCtrl
Common, abstract interface for edit frames.
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:210
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
void SetIgnoreMouseEvents(bool aIgnore)
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
void GetUnits(EDA_UNITS_T &aUnits, bool &aUseMils) const
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
const wxPoint & GetTextPos() const
Definition: eda_text.h:241
static LSET ForbiddenTextLayers()
Function ForbiddenTextLayers Layers which are now allowed to have text on them.
Definition: lset.cpp:796
Module description (excepted pads)
BOARD * GetBoard() const
BOARD_ITEM_CONTAINER * GetParent() const
void OnDimensionTextChange(wxCommandEvent &event) override
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:244
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:813
wxFloatingPointValidator< double > m_OrientValidator
EDA_UNITS_T
Definition: common.h:157
Class DIMENSION.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:165
KICAD_T Type() const
Function Type()
Definition: base_struct.h:204