1 /*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@gipsa-lab.inpg.com
5 * Copyright (C) 2016 Wayne Stambaugh, stambaughw@gmail.com
6 * Copyright (C) 2004-2021 KiCad Developers, see AITHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26 #include <bitmaps.h>
27 #include <kiway.h>
28 #include <confirm.h>
29 #include <string_utils.h>
30 #include <sch_base_frame.h>
31 #include <sch_edit_frame.h>
32 #include <ee_collectors.h>
33 #include <sch_symbol.h>
34 #include <lib_field.h>
35 #include <template_fieldnames.h>
36 #include <symbol_library.h>
37 #include <sch_validators.h>
38 #include <schematic.h>
39 #include <dialog_field_properties.h>
40 #include <sch_text.h>
41 #include <scintilla_tricks.h>
42 #include <wildcards_and_files_ext.h>
43
44
DIALOG_FIELD_PROPERTIES(SCH_BASE_FRAME * aParent,const wxString & aTitle,const EDA_TEXT * aTextItem)45 DIALOG_FIELD_PROPERTIES::DIALOG_FIELD_PROPERTIES( SCH_BASE_FRAME* aParent, const wxString& aTitle,
46 const EDA_TEXT* aTextItem ) :
47 DIALOG_LIB_TEXT_PROPERTIES_BASE( aParent ),
48 m_posX( aParent, m_xPosLabel, m_xPosCtrl, m_xPosUnits, true ),
49 m_posY( aParent, m_yPosLabel, m_yPosCtrl, m_yPosUnits, true ),
50 m_textSize( aParent, m_textSizeLabel, m_textSizeCtrl, m_textSizeUnits, true ),
51 m_firstFocus( true ),
52 m_scintillaTricks( nullptr )
53 {
54 wxASSERT( aTextItem );
55
56 SetTitle( aTitle );
57
58 m_note->SetFont( KIUI::GetInfoFont( this ).Italic() );
59 m_note->Show( false );
60
61 // The field ID and power status are Initialized in the derived object's ctor.
62 m_fieldId = VALUE_FIELD;
63 m_isPower = false;
64
65 m_scintillaTricks = new SCINTILLA_TRICKS( m_StyledTextCtrl, wxT( "{}" ), true,
66 [this]()
67 {
68 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
69 } );
70 m_StyledTextCtrl->SetEOLMode( wxSTC_EOL_LF ); // Normalize EOL across platforms
71
72 m_text = aTextItem->GetText();
73 m_isItalic = aTextItem->IsItalic();
74 m_isBold = aTextItem->IsBold();
75 m_position = aTextItem->GetTextPos();
76 m_size = aTextItem->GetTextWidth();
77 m_isVertical = ( aTextItem->GetTextAngle() == TEXT_ANGLE_VERT );
78 m_verticalJustification = aTextItem->GetVertJustify() + 1;
79 m_horizontalJustification = aTextItem->GetHorizJustify() + 1;
80 m_isVisible = aTextItem->IsVisible();
81 }
82
83
~DIALOG_FIELD_PROPERTIES()84 DIALOG_FIELD_PROPERTIES::~DIALOG_FIELD_PROPERTIES()
85 {
86 delete m_scintillaTricks;
87 }
88
89
init()90 void DIALOG_FIELD_PROPERTIES::init()
91 {
92 SCH_BASE_FRAME* parent = GetParent();
93 bool isSymbolEditor = parent && parent->IsType( FRAME_SCH_SYMBOL_EDITOR );
94
95 // Disable options for graphic text editing which are not needed for fields.
96 m_CommonConvert->Show( false );
97 m_CommonUnit->Show( false );
98
99 // Predefined fields cannot contain some chars, or cannot be empty,
100 // and need a SCH_FIELD_VALIDATOR (m_StyledTextCtrl cannot use a SCH_FIELD_VALIDATOR).
101 bool use_validator = m_fieldId == REFERENCE_FIELD
102 || m_fieldId == VALUE_FIELD
103 || m_fieldId == FOOTPRINT_FIELD
104 || m_fieldId == DATASHEET_FIELD
105 || m_fieldId == SHEETNAME_V
106 || m_fieldId == SHEETFILENAME_V;
107
108 if( use_validator )
109 {
110 m_TextCtrl->SetValidator( SCH_FIELD_VALIDATOR( isSymbolEditor, m_fieldId, &m_text ) );
111 SetInitialFocus( m_TextCtrl );
112
113 m_StyledTextCtrl->Show( false );
114 }
115 else
116 {
117 SetInitialFocus( m_StyledTextCtrl );
118
119 m_TextCtrl->Show( false );
120 }
121
122 // Show the footprint selection dialog if this is the footprint field.
123 m_TextValueSelectButton->SetBitmap( KiBitmap( BITMAPS::small_library ) );
124 m_TextValueSelectButton->Show( m_fieldId == FOOTPRINT_FIELD );
125
126 // Value fields of power symbols cannot be modified. This will grey out
127 // the text box and display an explanation.
128 if( m_fieldId == VALUE_FIELD && m_isPower )
129 {
130 m_note->SetLabel( wxString::Format( m_note->GetLabel(),
131 _( "Power symbol value field text cannot be changed." ) ) );
132 m_note->Show( true );
133 m_TextCtrl->Enable( false );
134 }
135 else
136 {
137 m_TextCtrl->Enable( true );
138 }
139
140 m_sdbSizerButtonsOK->SetDefault();
141
142 GetSizer()->SetSizeHints( this );
143
144 // Adjust the height of the scintilla text editor after the first layout
145 // To show only one line
146 // (multiline text are is supported in fields and will be removed)
147 if( m_StyledTextCtrl->IsShown() )
148 {
149 wxSize maxSize = m_StyledTextCtrl->GetSize();
150 maxSize.x = -1; // Do not fix the max width
151 maxSize.y = m_xPosCtrl->GetSize().y;
152 m_StyledTextCtrl->SetMaxSize( maxSize );
153 m_StyledTextCtrl->SetUseVerticalScrollBar( false );
154 m_StyledTextCtrl->SetUseHorizontalScrollBar( false );
155 }
156
157 // Now all widgets have the size fixed, call FinishDialogSettings
158 finishDialogSettings();
159 }
160
161
OnTextValueSelectButtonClick(wxCommandEvent & aEvent)162 void DIALOG_FIELD_PROPERTIES::OnTextValueSelectButtonClick( wxCommandEvent& aEvent )
163 {
164 // pick a footprint using the footprint picker.
165 wxString fpid;
166
167 if( m_StyledTextCtrl->IsShown() )
168 fpid = m_StyledTextCtrl->GetValue();
169 else
170 fpid = m_TextCtrl->GetValue();
171
172 KIWAY_PLAYER* frame = Kiway().Player( FRAME_FOOTPRINT_VIEWER_MODAL, true );
173
174 if( frame->ShowModal( &fpid, this ) )
175 {
176 if( m_StyledTextCtrl->IsShown() )
177 m_StyledTextCtrl->SetValue( fpid );
178 else
179 m_TextCtrl->SetValue( fpid );
180 }
181
182 frame->Destroy();
183 }
184
185
OnSetFocusText(wxFocusEvent & event)186 void DIALOG_FIELD_PROPERTIES::OnSetFocusText( wxFocusEvent& event )
187 {
188 if( m_firstFocus )
189 {
190 #ifdef __WXGTK__
191 // Force an update of the text control before setting the text selection
192 // This is needed because GTK seems to ignore the selection on first update
193 //
194 // Note that we can't do this on OSX as it tends to provoke Apple's
195 // "[NSAlert runModal] may not be invoked inside of transaction begin/commit pair"
196 // bug. See: https://bugs.launchpad.net/kicad/+bug/1837225
197 if( m_fieldId == REFERENCE_FIELD || m_fieldId == VALUE_FIELD || m_fieldId == SHEETNAME_V )
198 m_TextCtrl->Update();
199 #endif
200
201 if( m_fieldId == REFERENCE_FIELD )
202 KIUI::SelectReferenceNumber( static_cast<wxTextEntry*>( m_TextCtrl ) );
203 else if( m_fieldId == VALUE_FIELD || m_fieldId == SHEETNAME_V )
204 m_TextCtrl->SetSelection( -1, -1 );
205
206 m_firstFocus = false;
207 }
208
209 event.Skip();
210 }
211
212
TransferDataToWindow()213 bool DIALOG_FIELD_PROPERTIES::TransferDataToWindow()
214 {
215 if( m_TextCtrl->IsShown() )
216 m_TextCtrl->SetValue( m_text );
217 else if( m_StyledTextCtrl->IsShown() )
218 m_StyledTextCtrl->SetValue( m_text );
219
220 m_posX.SetValue( m_position.x );
221 m_posY.SetValue( m_position.y );
222 m_textSize.SetValue( m_size );
223 m_orientChoice->SetSelection( m_isVertical ? 1 : 0 );
224 m_hAlignChoice->SetSelection( m_horizontalJustification );
225 m_vAlignChoice->SetSelection( m_verticalJustification );
226 m_visible->SetValue( m_isVisible );
227 m_italic->SetValue( m_isItalic );
228 m_bold->SetValue( m_isBold );
229
230 return true;
231 }
232
233
TransferDataFromWindow()234 bool DIALOG_FIELD_PROPERTIES::TransferDataFromWindow()
235 {
236 if( m_TextCtrl->IsShown() )
237 m_text = m_TextCtrl->GetValue();
238 else if( m_StyledTextCtrl->IsShown() )
239 m_text = m_StyledTextCtrl->GetValue();
240
241 if( m_fieldId == REFERENCE_FIELD )
242 {
243 // Test if the reference string is valid:
244 if( !SCH_SYMBOL::IsReferenceStringValid( m_text ) )
245 {
246 DisplayError( this, _( "Illegal reference designator value!" ) );
247 return false;
248 }
249 }
250 else if( m_fieldId == VALUE_FIELD )
251 {
252 if( m_text.IsEmpty() )
253 {
254 DisplayError( this, _( "Value may not be empty." ) );
255 return false;
256 }
257 }
258 else if( m_fieldId == SHEETFILENAME_V )
259 {
260 wxFileName fn( m_text );
261
262 // It's annoying to throw up nag dialogs when the extension isn't right. Just
263 // fix it.
264 if( fn.GetExt().CmpNoCase( KiCadSchematicFileExtension ) != 0 )
265 {
266 fn.SetExt( KiCadSchematicFileExtension );
267 m_text = fn.GetFullPath();
268 }
269 }
270
271 m_isVertical = m_orientChoice->GetSelection() == 1;
272 m_position = wxPoint( m_posX.GetValue(), m_posY.GetValue() );
273 m_size = m_textSize.GetValue();
274 m_horizontalJustification = m_hAlignChoice->GetSelection();
275 m_verticalJustification = m_vAlignChoice->GetSelection();
276 m_isVisible = m_visible->GetValue();
277 m_isItalic = m_italic->GetValue();
278 m_isBold = m_bold->GetValue();
279
280 return true;
281 }
282
283
updateText(EDA_TEXT * aText)284 void DIALOG_FIELD_PROPERTIES::updateText( EDA_TEXT* aText )
285 {
286 if( aText->GetTextWidth() != m_size )
287 aText->SetTextSize( wxSize( m_size, m_size ) );
288
289 aText->SetVisible( m_isVisible );
290 aText->SetTextAngle( m_isVertical ? TEXT_ANGLE_VERT : TEXT_ANGLE_HORIZ );
291 aText->SetItalic( m_isItalic );
292 aText->SetBold( m_isBold );
293 }
294
295
DIALOG_LIB_FIELD_PROPERTIES(SCH_BASE_FRAME * aParent,const wxString & aTitle,const LIB_FIELD * aField)296 DIALOG_LIB_FIELD_PROPERTIES::DIALOG_LIB_FIELD_PROPERTIES( SCH_BASE_FRAME* aParent,
297 const wxString& aTitle,
298 const LIB_FIELD* aField ) :
299 DIALOG_FIELD_PROPERTIES( aParent, aTitle, aField )
300 {
301 m_fieldId = aField->GetId();
302
303 if( m_fieldId == VALUE_FIELD )
304 m_text = UnescapeString( aField->GetText() );
305
306 // When in the library editor, power symbols can be renamed.
307 m_isPower = false;
308 init();
309 }
310
311
DIALOG_SCH_FIELD_PROPERTIES(SCH_BASE_FRAME * aParent,const wxString & aTitle,const SCH_FIELD * aField)312 DIALOG_SCH_FIELD_PROPERTIES::DIALOG_SCH_FIELD_PROPERTIES( SCH_BASE_FRAME* aParent,
313 const wxString& aTitle,
314 const SCH_FIELD* aField ) :
315 DIALOG_FIELD_PROPERTIES( aParent, aTitle, aField ),
316 m_field( aField )
317 {
318 m_isSheetFilename = false;
319
320 if( aField->GetParent() && aField->GetParent()->Type() == SCH_SYMBOL_T )
321 {
322 m_fieldId = aField->GetId();
323 }
324 else if( aField->GetParent() && aField->GetParent()->Type() == SCH_SHEET_T )
325 {
326 switch( aField->GetId() )
327 {
328 case SHEETNAME:
329 m_fieldId = SHEETNAME_V;
330 break;
331
332 case SHEETFILENAME:
333 m_isSheetFilename = true;
334 m_fieldId = SHEETFILENAME_V;
335 m_note->SetLabel( wxString::Format( m_note->GetLabel(),
336 _( "Sheet filename can only be modified in Sheet Properties dialog." ) ) );
337 m_note->Show( true );
338 break;
339
340 default:
341 m_fieldId = SHEETUSERFIELD_V;
342 break;
343 }
344 }
345
346 // show text variable cross-references in a human-readable format
347 m_text = aField->Schematic()->ConvertKIIDsToRefs( aField->GetText() );
348
349 m_isPower = false;
350
351 m_textLabel->SetLabel( m_field->GetName() + ":" );
352
353 m_position = m_field->GetPosition();
354
355 m_horizontalJustification = m_field->GetEffectiveHorizJustify() + 1;
356 m_verticalJustification = m_field->GetEffectiveVertJustify() + 1;
357
358 // The library symbol may have been removed so using SCH_SYMBOL::GetLibSymbolRef() here
359 // could result in a segfault. If the library symbol is no longer available, the
360 // schematic fields can still edit so set the power symbol flag to false. This may not
361 // be entirely accurate if the power library is missing but it's better then a segfault.
362 if( aField->GetParent() && aField->GetParent()->Type() == SCH_SYMBOL_T )
363 {
364 const SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( aField->GetParent() );
365 const LIB_SYMBOL* libSymbol = GetParent()->GetLibSymbol( symbol->GetLibId(), true );
366
367 if( libSymbol && libSymbol->IsPower() )
368 m_isPower = true;
369 }
370
371 m_StyledTextCtrl->Bind( wxEVT_STC_CHARADDED,
372 &DIALOG_SCH_FIELD_PROPERTIES::onScintillaCharAdded, this );
373
374 init();
375
376 if( m_isSheetFilename )
377 {
378 m_StyledTextCtrl->Enable( false );
379 m_TextCtrl->Enable( false );
380 }
381 }
382
383
onScintillaCharAdded(wxStyledTextEvent & aEvent)384 void DIALOG_SCH_FIELD_PROPERTIES::onScintillaCharAdded( wxStyledTextEvent &aEvent )
385 {
386 int key = aEvent.GetKey();
387
388 SCH_EDIT_FRAME* editFrame = static_cast<SCH_EDIT_FRAME*>( GetParent() );
389 wxArrayString autocompleteTokens;
390 int pos = m_StyledTextCtrl->GetCurrentPos();
391 int start = m_StyledTextCtrl->WordStartPosition( pos, true );
392 wxString partial;
393
394 // Currently, '\n' is not allowed in fields. So remove it when entered
395 // TODO: see if we must close the dialog. However this is not obvious, as
396 // if a \n is typed (and removed) when a text is selected, this text is deleted
397 // (in fact replaced by \n, that is removed by the filter)
398 if( key == '\n' )
399 {
400 wxString text = m_StyledTextCtrl->GetText();
401 int currpos = m_StyledTextCtrl->GetCurrentPos();
402 text.Replace( "\n", "" );
403 m_StyledTextCtrl->SetText( text );
404 m_StyledTextCtrl->GotoPos( currpos-1 );
405 return;
406 }
407
408 auto textVarRef =
409 [&]( int pt )
410 {
411 return pt >= 2
412 && m_StyledTextCtrl->GetCharAt( pt - 2 ) == '$'
413 && m_StyledTextCtrl->GetCharAt( pt - 1 ) == '{';
414 };
415
416 // Check for cross-reference
417 if( start > 1 && m_StyledTextCtrl->GetCharAt( start - 1 ) == ':' )
418 {
419 int refStart = m_StyledTextCtrl->WordStartPosition( start - 1, true );
420
421 if( textVarRef( refStart ) )
422 {
423 partial = m_StyledTextCtrl->GetRange( start, pos );
424
425 wxString ref = m_StyledTextCtrl->GetRange( refStart, start - 1 );
426 SCH_SHEET_LIST sheets = editFrame->Schematic().GetSheets();
427 SCH_REFERENCE_LIST refs;
428 SCH_SYMBOL* refSymbol = nullptr;
429
430 sheets.GetSymbols( refs );
431
432 for( size_t jj = 0; jj < refs.GetCount(); jj++ )
433 {
434 if( refs[ jj ].GetSymbol()->GetRef( &refs[ jj ].GetSheetPath(), true ) == ref )
435 {
436 refSymbol = refs[ jj ].GetSymbol();
437 break;
438 }
439 }
440
441 if( refSymbol )
442 refSymbol->GetContextualTextVars( &autocompleteTokens );
443 }
444 }
445 else if( textVarRef( start ) )
446 {
447 partial = m_StyledTextCtrl->GetTextRange( start, pos );
448
449 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( m_field->GetParent() );
450 SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( m_field->GetParent() );
451
452 if( symbol )
453 {
454 symbol->GetContextualTextVars( &autocompleteTokens );
455
456 SCHEMATIC* schematic = symbol->Schematic();
457
458 if( schematic && schematic->CurrentSheet().Last() )
459 schematic->CurrentSheet().Last()->GetContextualTextVars( &autocompleteTokens );
460 }
461
462 if( sheet )
463 sheet->GetContextualTextVars( &autocompleteTokens );
464
465 for( std::pair<wxString, wxString> entry : Prj().GetTextVars() )
466 autocompleteTokens.push_back( entry.first );
467 }
468
469 m_scintillaTricks->DoAutocomplete( partial, autocompleteTokens );
470 m_StyledTextCtrl->SetFocus();
471 }
472
473
UpdateField(SCH_FIELD * aField,SCH_SHEET_PATH * aSheetPath)474 void DIALOG_SCH_FIELD_PROPERTIES::UpdateField( SCH_FIELD* aField, SCH_SHEET_PATH* aSheetPath )
475 {
476 SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
477 SCH_ITEM* parent = dynamic_cast<SCH_ITEM*>( aField->GetParent() );
478 int fieldType = aField->GetId();
479
480 if( parent && parent->Type() == SCH_SYMBOL_T )
481 {
482 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( parent );
483
484 if( fieldType == REFERENCE_FIELD )
485 symbol->SetRef( aSheetPath, m_text );
486 else if( fieldType == VALUE_FIELD )
487 symbol->SetValue( m_text );
488 else if( fieldType == FOOTPRINT_FIELD )
489 symbol->SetFootprint( m_text );
490 }
491
492 EDA_TEXT_HJUSTIFY_T hJustify = EDA_TEXT::MapHorizJustify( m_horizontalJustification - 1 );
493 EDA_TEXT_VJUSTIFY_T vJustify = EDA_TEXT::MapVertJustify( m_verticalJustification - 1 );
494 bool positioningModified = false;
495
496 if( aField->GetPosition() != m_position )
497 positioningModified = true;
498
499 if( ( aField->GetTextAngle() == TEXT_ANGLE_VERT ) != m_isVertical )
500 positioningModified = true;
501
502 if( aField->GetEffectiveHorizJustify() != hJustify )
503 positioningModified = true;
504
505 if( aField->GetEffectiveVertJustify() != vJustify )
506 positioningModified = true;
507
508 // convert any text variable cross-references to their UUIDs
509 m_text = aField->Schematic()->ConvertRefsToKIIDs( m_text );
510
511 aField->SetText( m_text );
512 updateText( aField );
513 aField->SetPosition( m_position );
514
515 // Note that we must set justifications before we can ask if they're flipped. If the old
516 // justification is center then it won't know (whereas if the new justification is center
517 // the we don't care).
518 aField->SetHorizJustify( hJustify );
519 aField->SetVertJustify( vJustify );
520
521 if( aField->IsHorizJustifyFlipped() )
522 aField->SetHorizJustify( EDA_TEXT::MapHorizJustify( -hJustify ) );
523
524 if( aField->IsVertJustifyFlipped() )
525 aField->SetVertJustify( EDA_TEXT::MapVertJustify( -vJustify ) );
526
527 // The value, footprint and datasheet fields should be kept in sync in multi-unit parts.
528 // Of course the symbol must be annotated to collect other units.
529 if( editFrame && parent && parent->Type() == SCH_SYMBOL_T )
530 {
531 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( parent );
532
533 if( symbol->IsAnnotated( aSheetPath ) && ( fieldType == VALUE_FIELD
534 || fieldType == FOOTPRINT_FIELD
535 || fieldType == DATASHEET_FIELD ) )
536 {
537 wxString ref = symbol->GetRef( aSheetPath );
538 int unit = symbol->GetUnit();
539 LIB_ID libId = symbol->GetLibId();
540
541 for( SCH_SHEET_PATH& sheet : editFrame->Schematic().GetSheets() )
542 {
543 SCH_SCREEN* screen = sheet.LastScreen();
544 std::vector<SCH_SYMBOL*> otherUnits;
545 constexpr bool appendUndo = true;
546
547 CollectOtherUnits( ref, unit, libId, sheet, &otherUnits );
548
549 for( SCH_SYMBOL* otherUnit : otherUnits )
550 {
551 editFrame->SaveCopyInUndoList( screen, otherUnit, UNDO_REDO::CHANGED,
552 appendUndo );
553
554 if( fieldType == VALUE_FIELD )
555 otherUnit->SetValue( m_text );
556 else if( fieldType == FOOTPRINT_FIELD )
557 otherUnit->SetFootprint( m_text );
558 else
559 otherUnit->GetField( DATASHEET_FIELD )->SetText( m_text );
560
561 editFrame->UpdateItem( otherUnit, false, true );
562 }
563 }
564 }
565 }
566
567 if( positioningModified && parent )
568 parent->ClearFieldsAutoplaced();
569 }
570