KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_textbox_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/hyperlink.h>
23
25#include <widgets/font_choice.h>
27#include <confirm.h>
28#include <board_commit.h>
30#include <board.h>
31#include <footprint.h>
32#include <pcb_textbox.h>
33#include <project.h>
34#include <pcb_edit_frame.h>
36#include <scintilla_tricks.h>
37#include <string_utils.h>
38
39
42 m_frame( aParent ),
43 m_textBox( aTextBox ),
47 m_orientation( aParent, m_OrientLabel, m_OrientCtrl, nullptr ),
49{
50 m_MultiLineText->SetEOLMode( wxSTC_EOL_LF );
51
52 // Wrapping is display-only and does not insert newlines into the stored text.
53 m_MultiLineText->SetWrapMode( wxSTC_WRAP_WORD );
54 m_MultiLineText->SetWrapVisualFlags( wxSTC_WRAPVISUALFLAG_END );
55 m_MultiLineText->SetWrapIndentMode( wxSTC_WRAPINDENT_INDENT );
56
57#ifdef _WIN32
58 // Without this setting, on Windows, some esoteric unicode chars create display issue
59 // in a wxStyledTextCtrl.
60 // for SetTechnology() info, see https://www.scintilla.org/ScintillaDoc.html#SCI_SETTECHNOLOGY
61 m_MultiLineText->SetTechnology( wxSTC_TECHNOLOGY_DIRECTWRITE );
62#endif
63
65 m_MultiLineText, wxT( "{}" ), false,
66 // onAcceptFn
67 [this]( wxKeyEvent& aEvent )
68 {
69 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
70 },
71 // onCharFn
72 [this]( wxStyledTextEvent& aEvent )
73 {
74 m_scintillaTricks->DoTextVarAutocomplete(
75 // getTokensFn
76 [this]( const wxString& xRef, wxArrayString* tokens )
77 {
78 m_frame->GetContextualTextVars( m_textBox, xRef, tokens );
79 } );
80 } );
81
82 // A hack which causes Scintilla to auto-size the text editor canvas
83 // See: https://github.com/jacobslusser/ScintillaNET/issues/216
84 m_MultiLineText->SetScrollWidth( 1 );
85 m_MultiLineText->SetScrollWidthTracking( true );
86
87 // Add syntax help hyperlink
88 m_syntaxHelp = new wxHyperlinkCtrl( this, wxID_ANY, _( "Syntax help" ), wxEmptyString, wxDefaultPosition,
89 wxDefaultSize, wxHL_DEFAULT_STYLE );
90 m_syntaxHelp->SetToolTip( _( "Show syntax help window" ) );
91 m_MultiLineSizer->Add( m_syntaxHelp, 0, wxBOTTOM | wxRIGHT | wxLEFT, 3 );
92
93 m_syntaxHelp->Bind( wxEVT_HYPERLINK, &DIALOG_TEXTBOX_PROPERTIES::onSyntaxHelp, this );
94
95 m_helpWindow = nullptr;
96
97 if( m_textBox->GetParentFootprint() )
98 {
99 // Do not allow locking items in the footprint editor
100 m_cbLocked->Show( false );
101 }
102
104
105 m_separator0->SetIsSeparator();
106
107 m_bold->SetIsCheckButton();
109 m_italic->SetIsCheckButton();
111
112 m_separator1->SetIsSeparator();
113
114 m_hAlignLeft->SetIsRadioButton();
116 m_hAlignCenter->SetIsRadioButton();
118 m_hAlignRight->SetIsRadioButton();
120
121 m_separator2->SetIsSeparator();
122
123 m_mirrored->SetIsCheckButton();
125
126 m_separator3->SetIsSeparator();
127
128 m_vAlignTop->SetIsRadioButton();
130 m_vAlignCenter->SetIsRadioButton();
132 m_vAlignBottom->SetIsRadioButton();
134
135 m_separator4->SetIsSeparator();
136
137 m_autoTextThickness->SetIsCheckButton();
139
140 // Configure the layers list selector. Note that footprints are built outside the current
141 // board and so we may need to show all layers if the text is on an unactivated layer.
142 if( !m_frame->GetBoard()->IsLayerEnabled( m_textBox->GetLayer() ) )
143 m_LayerSelectionCtrl->ShowNonActivatedLayers( true );
144
145 m_LayerSelectionCtrl->SetLayersHotkeys( false );
146 m_LayerSelectionCtrl->SetBoardFrame( m_frame );
147 m_LayerSelectionCtrl->Resync();
148
150 m_orientation.SetPrecision( 3 );
151
152 // Set predefined rotations in combo dropdown, according to the locale floating point
153 // separator notation
154 double rot_list[] = { 0.0, 90.0, -90.0, 180.0 };
155
156 for( size_t ii = 0; ii < m_OrientCtrl->GetCount() && ii < 4; ++ii )
157 m_OrientCtrl->SetString( ii, wxString::Format( "%.1f", rot_list[ii] ) );
158
159 for( const auto& [lineStyle, lineStyleDesc] : lineTypeNames )
160 m_borderStyleCombo->Append( lineStyleDesc.name, KiBitmapBundle( lineStyleDesc.bitmap ) );
161
163
164 // wxTextCtrls fail to generate wxEVT_CHAR events when the wxTE_MULTILINE flag is set,
165 // so we have to listen to wxEVT_CHAR_HOOK events instead.
166 Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXTBOX_PROPERTIES::OnCharHook ), nullptr, this );
167
169 Layout();
170 bMainSizer->Fit( this );
171}
172
173
175{
176 Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXTBOX_PROPERTIES::OnCharHook ), nullptr, this );
177
178 delete m_scintillaTricks;
179 m_scintillaTricks = nullptr;
180
181 if( m_helpWindow )
182 m_helpWindow->Destroy();
183}
184
185
187{
188 DIALOG_TEXTBOX_PROPERTIES dlg( this, aTextBox );
189
190 // QuasiModal required for Scintilla auto-complete
191 return dlg.ShowQuasiModal();
192}
193
194
196{
197 BOARD* board = m_frame->GetBoard();
198 wxString converted = board->ConvertKIIDsToCrossReferences( UnescapeString( m_textBox->GetText() ) );
199
200 m_MultiLineText->SetValue( converted );
201 m_MultiLineText->SetSelection( -1, -1 );
202 m_MultiLineText->EmptyUndoBuffer();
203
204 m_cbLocked->SetValue( m_textBox->IsLocked() );
205
206 m_LayerSelectionCtrl->SetLayerSelection( m_textBox->GetLayer() );
207
208 m_fontCtrl->SetFontSelection( m_textBox->GetFont() );
209
210 m_textWidth.SetValue( m_textBox->GetTextSize().x );
211 m_textHeight.SetValue( m_textBox->GetTextSize().y );
212
213 if( m_textBox->GetAutoThickness() )
214 {
215 // Show the base width (frozen if auto is unchecked); Bold scales it at render time.
216 // Must match EDA_TEXT::SetAutoThickness()'s formula so unchecking auto doesn't
217 // silently change the stored width.
218 m_autoTextThickness->Check( m_textBox->GetAutoThickness() );
219 m_thickness.SetValue( GetPenSizeForNormal( m_textBox->GetTextWidth() ) );
220 m_thickness.Enable( false );
221 }
222 else
223 {
224 m_thickness.SetValue( m_textBox->GetTextThickness() );
225 }
226
227 m_bold->Check( m_textBox->IsBold() );
228 m_italic->Check( m_textBox->IsItalic() );
229
230 switch( m_textBox->GetHorizJustify() )
231 {
232 case GR_TEXT_H_ALIGN_LEFT: m_hAlignLeft->Check( true ); break;
233 case GR_TEXT_H_ALIGN_CENTER: m_hAlignCenter->Check( true ); break;
234 case GR_TEXT_H_ALIGN_RIGHT: m_hAlignRight->Check( true ); break;
236 }
237
238 switch( m_textBox->GetVertJustify() )
239 {
240 case GR_TEXT_V_ALIGN_TOP: m_vAlignTop->Check( true ); break;
241 case GR_TEXT_V_ALIGN_CENTER: m_vAlignCenter->Check( true ); break;
242 case GR_TEXT_V_ALIGN_BOTTOM: m_vAlignBottom->Check( true ); break;
244 }
245
246 m_mirrored->Check( m_textBox->IsMirrored() );
247
248 EDA_ANGLE orientation = m_textBox->GetTextAngle();
249 m_orientation.SetAngleValue( orientation.Normalize180() );
250
251 STROKE_PARAMS stroke = m_textBox->GetStroke();
252 m_borderCheckbox->SetValue( m_textBox->IsBorderEnabled() );
253
254 if( m_textBox->IsBorderEnabled() )
255 m_borderWidth.SetValue( stroke.GetWidth() );
256
257 LINE_STYLE style = stroke.GetLineStyle();
258
259 if( style == LINE_STYLE::DEFAULT )
260 style = LINE_STYLE::SOLID;
261
262 if( (int) style < (int) lineTypeNames.size() )
263 m_borderStyleCombo->SetSelection( (int) style );
264
265 m_borderWidth.Enable( m_textBox->IsBorderEnabled() );
266 m_borderStyleLabel->Enable( m_textBox->IsBorderEnabled() );
267 m_borderStyleCombo->Enable( m_textBox->IsBorderEnabled() );
268
269 return DIALOG_TEXTBOX_PROPERTIES_BASE::TransferDataToWindow();
270}
271
272void DIALOG_TEXTBOX_PROPERTIES::onValignButton( wxCommandEvent& aEvent )
273{
275 {
276 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
277 btn->Check( false );
278 }
279}
280
281void DIALOG_TEXTBOX_PROPERTIES::onFontSelected( wxCommandEvent& aEvent )
282{
283 m_thickness.Show( KIFONT::FONT::IsStroke( aEvent.GetString() ) );
284}
285
286
287void DIALOG_TEXTBOX_PROPERTIES::onHalignButton( wxCommandEvent& aEvent )
288{
290 {
291 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
292 btn->Check( false );
293 }
294}
295
296
297void DIALOG_TEXTBOX_PROPERTIES::onTextSize( wxCommandEvent& aEvent )
298{
299 if( m_autoTextThickness->IsChecked() )
300 m_thickness.SetValue( GetPenSizeForNormal( m_textWidth.GetValue() ) );
301}
302
303
305{
306 if( aEvent.IsChecked() )
307 {
308 m_autoTextThickness->Check( true );
309
310 wxCommandEvent dummy;
311 onTextSize( dummy );
312
313 m_thickness.Enable( false );
314 }
315 else
316 {
317 m_thickness.Enable( true );
318 }
319}
320
321
323{
324 bool border = m_borderCheckbox->GetValue();
325
326 if( border && m_borderWidth.GetValue() <= 0 )
327 {
328 BOARD_DESIGN_SETTINGS& bds = m_textBox->GetBoard()->GetDesignSettings();
329 m_borderWidth.SetValue( bds.GetLineThickness( m_textBox->GetLayer() ) );
330 }
331
332 m_borderWidth.Enable( border );
333 m_borderStyleLabel->Enable( border );
334 m_borderStyleCombo->Enable( border );
335}
336
337
339{
340 if( !DIALOG_TEXTBOX_PROPERTIES_BASE::TransferDataFromWindow() )
341 return false;
342
343 int minSize = pcbIUScale.mmToIU( TEXT_MIN_SIZE_MM );
344 int maxSize = pcbIUScale.mmToIU( TEXT_MAX_SIZE_MM );
345
346 if( !m_textWidth.Validate( minSize, maxSize ) || !m_textHeight.Validate( minSize, maxSize ) )
347 return false;
348
349 BOARD_COMMIT commit( m_frame );
350 commit.Modify( m_textBox );
351
352 // If no other command in progress, prepare undo command
353 // (for a command in progress, will be made later, at the completion of command)
354 bool pushCommit = ( m_textBox->GetEditFlags() == 0 );
355
356 // Set IN_EDIT flag to force undo/redo/abort proper operation and avoid new calls to
357 // SaveCopyInUndoList for the same text if is moved, and then rotated, edited, etc....
358 if( !pushCommit )
359 m_textBox->SetFlags( IN_EDIT );
360
361 BOARD* board = m_frame->GetBoard();
362 wxString txt = board->ConvertCrossReferencesToKIIDs( m_MultiLineText->GetValue() );
363
364#ifdef __WXMAC__
365 // On macOS CTRL+Enter produces '\r' instead of '\n' regardless of EOL setting.
366 // Replace it now.
367 txt.Replace( "\r", "\n" );
368#elif defined( __WINDOWS__ )
369 // On Windows, a new line is coded as \r\n. We use only \n in kicad files and in
370 // drawing routines so strip the \r char.
371 txt.Replace( "\r", "" );
372#endif
373
374 m_textBox->SetText( EscapeString( txt, CTX_QUOTED_STR ) );
375 m_textBox->SetLocked( m_cbLocked->GetValue() );
376 m_textBox->SetLayer( ToLAYER_ID( m_LayerSelectionCtrl->GetLayerSelection() ) );
377
378 if( m_fontCtrl->HaveFontSelection() )
379 {
380 m_textBox->SetFont( m_fontCtrl->GetFontSelection( m_bold->IsChecked(), m_italic->IsChecked() ) );
381 }
382
383 m_textBox->SetTextSize( VECTOR2I( m_textWidth.GetValue(), m_textHeight.GetValue() ) );
384
385 if( m_autoTextThickness->IsChecked() )
386 {
387 m_textBox->SetAutoThickness( true );
388 }
389 else
390 {
391 m_textBox->SetTextThickness( m_thickness.GetValue() );
392
393 // Test for acceptable values for thickness and size and clamp if fails
394 int maxPenWidth = ClampTextPenSize( m_textBox->GetTextThickness(), m_textBox->GetTextSize() );
395
396 if( m_textBox->GetTextThickness() > maxPenWidth )
397 {
398 DisplayError( this, _( "The text thickness is too large for the text size.\n"
399 "It will be clamped." ) );
400 m_textBox->SetTextThickness( maxPenWidth );
401 }
402 }
403
404 m_textBox->SetTextAngle( m_orientation.GetAngleValue().Normalize() );
405 m_textBox->SetBoldFlag( m_bold->IsChecked() );
406 m_textBox->SetItalicFlag( m_italic->IsChecked() );
407
408 if( m_hAlignLeft->IsChecked() )
409 m_textBox->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
410 else if( m_hAlignCenter->IsChecked() )
411 m_textBox->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
412 else
413 m_textBox->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
414
415 if( m_vAlignTop->IsChecked() )
416 m_textBox->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
417 else if( m_vAlignCenter->IsChecked() )
418 m_textBox->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
419 else
420 m_textBox->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
421
422 m_textBox->SetMirrored( m_mirrored->IsChecked() );
423
424 m_textBox->SetBorderEnabled( m_borderCheckbox->GetValue() );
425 STROKE_PARAMS stroke = m_textBox->GetStroke();
426
427 if( !m_borderWidth.IsIndeterminate() )
428 stroke.SetWidth( m_borderWidth.GetValue() );
429
430 auto it = lineTypeNames.begin();
431 std::advance( it, m_borderStyleCombo->GetSelection() );
432
433 if( it == lineTypeNames.end() )
435 else
436 stroke.SetLineStyle( it->first );
437
438 m_textBox->SetStroke( stroke );
439
440 m_textBox->ClearBoundingBoxCache();
441 m_textBox->ClearRenderCache();
442
443 VECTOR2I minBoxSize = m_textBox->GetMinSize();
444 VECTOR2I start = m_textBox->GetStart();
445 VECTOR2I end = m_textBox->GetEnd();
446 bool expanded = false;
447
448 if( minBoxSize.x > 0 && std::abs( end.x - start.x ) < minBoxSize.x )
449 {
450 end.x = ( end.x >= start.x ) ? start.x + minBoxSize.x : start.x - minBoxSize.x;
451 expanded = true;
452 }
453
454 if( minBoxSize.y > 0 && std::abs( end.y - start.y ) < minBoxSize.y )
455 {
456 end.y = ( end.y >= start.y ) ? start.y + minBoxSize.y : start.y - minBoxSize.y;
457 expanded = true;
458 }
459
460 if( expanded )
461 m_textBox->SetEnd( end );
462
463 if( pushCommit )
464 commit.Push( _( "Edit Text Box Properties" ) );
465
466 return true;
467}
468
469
471{
473 m_scintillaTricks->CancelAutocomplete();
474
475 event.Skip();
476}
477
478
479void DIALOG_TEXTBOX_PROPERTIES::onSyntaxHelp( wxHyperlinkEvent& aEvent )
480{
482}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
@ text_align_right
@ text_valign_top
@ text_align_left
@ text_valign_center
@ text_align_center
@ text_valign_bottom
@ edit_cmp_symb_links
A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
Container for design settings for a BOARD object.
int GetLineThickness(PCB_LAYER_ID aLayer) const
Return the default graphic segment thickness from the layer class for the given layer.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
wxString ConvertCrossReferencesToKIIDs(const wxString &aSource) const
Convert cross-references back and forth between ${refDes:field} and ${kiid:field}.
Definition board.cpp:2551
wxString ConvertKIIDsToCrossReferences(const wxString &aSource) const
Definition board.cpp:2631
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:94
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
virtual void OnCharHook(wxKeyEvent &aEvt)
DIALOG_TEXTBOX_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Text Box Properties"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxSYSTEM_MENU)
void onHalignButton(wxCommandEvent &aEvent) override
void onTextSize(wxCommandEvent &aEvent) override
void onAutoTextThickness(wxCommandEvent &aEvent) override
DIALOG_TEXTBOX_PROPERTIES(PCB_BASE_EDIT_FRAME *aParent, PCB_TEXTBOX *aTextBox)
void onMultiLineTCLostFocus(wxFocusEvent &event) override
void onSyntaxHelp(wxHyperlinkEvent &aEvent)
void onFontSelected(wxCommandEvent &aEvent) override
void onValignButton(wxCommandEvent &aEvent) override
void onBorderChecked(wxCommandEvent &event) override
EDA_ANGLE Normalize180()
Definition eda_angle.h:268
virtual bool IsStroke() const
Definition font.h:101
Common, abstract interface for edit frames.
int ShowTextBoxPropertiesDialog(PCB_TEXTBOX *aTextBox)
static HTML_MESSAGE_BOX * ShowSyntaxHelp(wxWindow *aParentWindow)
Display a syntax help window for text variables and expressions.
Definition pcb_text.cpp:890
Add cut/copy/paste, dark theme, autocomplete and brace highlighting to a wxStyleTextCtrl instance.
Simple container to manage line stroke parameters.
int GetWidth() const
void SetLineStyle(LINE_STYLE aLineStyle)
void SetWidth(int aWidth)
LINE_STYLE GetLineStyle() const
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
const int minSize
Push and Shove router track width and via size dialog.
#define _(s)
#define IN_EDIT
Item currently edited.
#define TEXT_MIN_SIZE_MM
Minimum text size (1 micron).
Definition eda_text.h:61
#define TEXT_MAX_SIZE_MM
Maximum text size in mm (~10 inches)
Definition eda_text.h:62
int GetPenSizeForNormal(int aTextSize)
Definition gr_text.cpp:57
int ClampTextPenSize(int aPenSize, int aSize, bool aStrict)
Pen width should not allow characters to become cluttered up in their own fatness.
Definition gr_text.cpp:69
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:750
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:411
std::vector< FAB_LAYER_COLOR > dummy
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_QUOTED_STR
const std::map< LINE_STYLE, struct LINE_STYLE_DESC > lineTypeNames
Conversion map between LINE_STYLE values and style names displayed.
LINE_STYLE
Dashed line types.
VECTOR2I end
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_H_ALIGN_INDETERMINATE
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_INDETERMINATE
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683