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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <wx/hyperlink.h>
27
29#include <widgets/font_choice.h>
31#include <confirm.h>
32#include <board_commit.h>
34#include <board.h>
35#include <footprint.h>
36#include <pcb_textbox.h>
37#include <project.h>
38#include <pcb_edit_frame.h>
40#include <scintilla_tricks.h>
41#include <string_utils.h>
42
43
46 m_frame( aParent ),
47 m_textBox( aTextBox ),
51 m_orientation( aParent, m_OrientLabel, m_OrientCtrl, nullptr ),
53{
54 m_MultiLineText->SetEOLMode( wxSTC_EOL_LF );
55
56#ifdef _WIN32
57 // Without this setting, on Windows, some esoteric unicode chars create display issue
58 // in a wxStyledTextCtrl.
59 // for SetTechnology() info, see https://www.scintilla.org/ScintillaDoc.html#SCI_SETTECHNOLOGY
60 m_MultiLineText->SetTechnology( wxSTC_TECHNOLOGY_DIRECTWRITE );
61#endif
62
64 m_MultiLineText, wxT( "{}" ), false,
65 // onAcceptFn
66 [this]( wxKeyEvent& aEvent )
67 {
68 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
69 },
70 // onCharFn
71 [this]( wxStyledTextEvent& aEvent )
72 {
73 m_scintillaTricks->DoTextVarAutocomplete(
74 // getTokensFn
75 [this]( const wxString& xRef, wxArrayString* tokens )
76 {
77 m_frame->GetContextualTextVars( m_textBox, xRef, tokens );
78 } );
79 } );
80
81 // A hack which causes Scintilla to auto-size the text editor canvas
82 // See: https://github.com/jacobslusser/ScintillaNET/issues/216
83 m_MultiLineText->SetScrollWidth( 1 );
84 m_MultiLineText->SetScrollWidthTracking( true );
85
86 // Add syntax help hyperlink
87 m_syntaxHelp = new wxHyperlinkCtrl( this, wxID_ANY, _( "Syntax help" ), wxEmptyString, wxDefaultPosition,
88 wxDefaultSize, wxHL_DEFAULT_STYLE );
89 m_syntaxHelp->SetToolTip( _( "Show syntax help window" ) );
90 m_MultiLineSizer->Add( m_syntaxHelp, 0, wxBOTTOM | wxRIGHT | wxLEFT, 3 );
91
92 m_syntaxHelp->Bind( wxEVT_HYPERLINK, &DIALOG_TEXTBOX_PROPERTIES::onSyntaxHelp, this );
93
94 m_helpWindow = nullptr;
95
96 if( m_textBox->GetParentFootprint() )
97 {
98 // Do not allow locking items in the footprint editor
99 m_cbLocked->Show( false );
100 }
101
103
104 m_separator0->SetIsSeparator();
105
106 m_bold->SetIsCheckButton();
108 m_italic->SetIsCheckButton();
110
111 m_separator1->SetIsSeparator();
112
113 m_hAlignLeft->SetIsRadioButton();
115 m_hAlignCenter->SetIsRadioButton();
117 m_hAlignRight->SetIsRadioButton();
119
120 m_separator2->SetIsSeparator();
121
122 m_mirrored->SetIsCheckButton();
124
125 m_separator3->SetIsSeparator();
126
127 m_vAlignTop->SetIsRadioButton();
129 m_vAlignCenter->SetIsRadioButton();
131 m_vAlignBottom->SetIsRadioButton();
133
134 m_separator4->SetIsSeparator();
135
136 m_autoTextThickness->SetIsCheckButton();
138
139 // Configure the layers list selector. Note that footprints are built outside the current
140 // board and so we may need to show all layers if the text is on an unactivated layer.
141 if( !m_frame->GetBoard()->IsLayerEnabled( m_textBox->GetLayer() ) )
142 m_LayerSelectionCtrl->ShowNonActivatedLayers( true );
143
144 m_LayerSelectionCtrl->SetLayersHotkeys( false );
145 m_LayerSelectionCtrl->SetBoardFrame( m_frame );
146 m_LayerSelectionCtrl->Resync();
147
149 m_orientation.SetPrecision( 3 );
150
151 // Set predefined rotations in combo dropdown, according to the locale floating point
152 // separator notation
153 double rot_list[] = { 0.0, 90.0, -90.0, 180.0 };
154
155 for( size_t ii = 0; ii < m_OrientCtrl->GetCount() && ii < 4; ++ii )
156 m_OrientCtrl->SetString( ii, wxString::Format( "%.1f", rot_list[ii] ) );
157
158 for( const auto& [lineStyle, lineStyleDesc] : lineTypeNames )
159 m_borderStyleCombo->Append( lineStyleDesc.name, KiBitmapBundle( lineStyleDesc.bitmap ) );
160
162
163 // wxTextCtrls fail to generate wxEVT_CHAR events when the wxTE_MULTILINE flag is set,
164 // so we have to listen to wxEVT_CHAR_HOOK events instead.
165 Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXTBOX_PROPERTIES::OnCharHook ), nullptr, this );
166
168 Layout();
169 bMainSizer->Fit( this );
170}
171
172
174{
175 Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXTBOX_PROPERTIES::OnCharHook ), nullptr, this );
176
177 delete m_scintillaTricks;
178
179 if( m_helpWindow )
180 m_helpWindow->Destroy();
181}
182
183
185{
186 DIALOG_TEXTBOX_PROPERTIES dlg( this, aTextBox );
187
188 // QuasiModal required for Scintilla auto-complete
189 return dlg.ShowQuasiModal();
190}
191
192
194{
195 BOARD* board = m_frame->GetBoard();
196 wxString converted = board->ConvertKIIDsToCrossReferences( UnescapeString( m_textBox->GetText() ) );
197
198 m_MultiLineText->SetValue( converted );
199 m_MultiLineText->SetSelection( -1, -1 );
200 m_MultiLineText->EmptyUndoBuffer();
201
202 m_cbLocked->SetValue( m_textBox->IsLocked() );
203
204 m_LayerSelectionCtrl->SetLayerSelection( m_textBox->GetLayer() );
205
206 m_fontCtrl->SetFontSelection( m_textBox->GetFont() );
207
208 m_textWidth.SetValue( m_textBox->GetTextSize().x );
209 m_textHeight.SetValue( m_textBox->GetTextSize().y );
210
211 if( m_textBox->GetAutoThickness() )
212 {
213 m_autoTextThickness->Check( m_textBox->GetAutoThickness() );
214 m_thickness.SetValue( m_textBox->GetEffectiveTextPenWidth() );
215 m_thickness.Enable( false );
216 }
217 else
218 {
219 m_thickness.SetValue( m_textBox->GetTextThickness() );
220 }
221
222 m_bold->Check( m_textBox->IsBold() );
223 m_italic->Check( m_textBox->IsItalic() );
224
225 switch( m_textBox->GetHorizJustify() )
226 {
227 case GR_TEXT_H_ALIGN_LEFT: m_hAlignLeft->Check( true ); break;
228 case GR_TEXT_H_ALIGN_CENTER: m_hAlignCenter->Check( true ); break;
229 case GR_TEXT_H_ALIGN_RIGHT: m_hAlignRight->Check( true ); break;
231 }
232
233 switch( m_textBox->GetVertJustify() )
234 {
235 case GR_TEXT_V_ALIGN_TOP: m_vAlignTop->Check( true ); break;
236 case GR_TEXT_V_ALIGN_CENTER: m_vAlignCenter->Check( true ); break;
237 case GR_TEXT_V_ALIGN_BOTTOM: m_vAlignBottom->Check( true ); break;
239 }
240
241 m_mirrored->Check( m_textBox->IsMirrored() );
242
243 EDA_ANGLE orientation = m_textBox->GetTextAngle();
244 m_orientation.SetAngleValue( orientation.Normalize180() );
245
246 STROKE_PARAMS stroke = m_textBox->GetStroke();
247 m_borderCheckbox->SetValue( m_textBox->IsBorderEnabled() );
248
249 if( m_textBox->IsBorderEnabled() )
250 m_borderWidth.SetValue( stroke.GetWidth() );
251
252 LINE_STYLE style = stroke.GetLineStyle();
253
254 if( style == LINE_STYLE::DEFAULT )
255 style = LINE_STYLE::SOLID;
256
257 if( (int) style < (int) lineTypeNames.size() )
258 m_borderStyleCombo->SetSelection( (int) style );
259
260 m_borderWidth.Enable( m_textBox->IsBorderEnabled() );
261 m_borderStyleLabel->Enable( m_textBox->IsBorderEnabled() );
262 m_borderStyleCombo->Enable( m_textBox->IsBorderEnabled() );
263
264 return DIALOG_TEXTBOX_PROPERTIES_BASE::TransferDataToWindow();
265}
266
267void DIALOG_TEXTBOX_PROPERTIES::onValignButton( wxCommandEvent& aEvent )
268{
270 {
271 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
272 btn->Check( false );
273 }
274}
275
276void DIALOG_TEXTBOX_PROPERTIES::onFontSelected( wxCommandEvent& aEvent )
277{
278 if( KIFONT::FONT::IsStroke( aEvent.GetString() ) )
279 {
280 m_thickness.Show( true );
281
282 int textSize = std::min( m_textWidth.GetValue(), m_textHeight.GetValue() );
283 int thickness = m_thickness.GetValue();
284
285 m_bold->Check( abs( thickness - GetPenSizeForBold( textSize ) )
286 < abs( thickness - GetPenSizeForNormal( textSize ) ) );
287 }
288 else
289 {
290 m_thickness.Show( false );
291 }
292}
293
294
295void DIALOG_TEXTBOX_PROPERTIES::onBoldToggle( wxCommandEvent& aEvent )
296{
297 int textSize = std::min( m_textWidth.GetValue(), m_textHeight.GetValue() );
298
299 if( aEvent.IsChecked() )
300 m_thickness.ChangeValue( GetPenSizeForBold( textSize ) );
301 else
302 m_thickness.ChangeValue( GetPenSizeForNormal( textSize ) );
303
304 aEvent.Skip();
305}
306
307
308void DIALOG_TEXTBOX_PROPERTIES::onHalignButton( wxCommandEvent& aEvent )
309{
311 {
312 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
313 btn->Check( false );
314 }
315}
316
317
318void DIALOG_TEXTBOX_PROPERTIES::onThickness( wxCommandEvent& event )
319{
320 int textSize = std::min( m_textWidth.GetValue(), m_textHeight.GetValue() );
321 int thickness = m_thickness.GetValue();
322
323 m_bold->Check( abs( thickness - GetPenSizeForBold( textSize ) )
324 < abs( thickness - GetPenSizeForNormal( textSize ) ) );
325}
326
327
328void DIALOG_TEXTBOX_PROPERTIES::onTextSize( wxCommandEvent& aEvent )
329{
330 if( m_autoTextThickness->IsChecked() )
331 {
332 int textSize = std::min( m_textWidth.GetValue(), m_textHeight.GetValue() );
333 int thickness;
334
335 // Calculate the "best" thickness from text size and bold option:
336 if( m_bold->IsChecked() )
337 thickness = GetPenSizeForBold( textSize );
338 else
339 thickness = GetPenSizeForNormal( textSize );
340
341 m_thickness.SetValue( thickness );
342 }
343}
344
345
347{
348 if( aEvent.IsChecked() )
349 {
350 m_autoTextThickness->Check( true );
351
352 wxCommandEvent dummy;
353 onTextSize( dummy );
354
355 m_thickness.Enable( false );
356 }
357 else
358 {
359 m_thickness.Enable( true );
360 }
361}
362
363
365{
366 bool border = m_borderCheckbox->GetValue();
367
368 if( border && m_borderWidth.GetValue() <= 0 )
369 {
370 BOARD_DESIGN_SETTINGS& bds = m_textBox->GetBoard()->GetDesignSettings();
371 m_borderWidth.SetValue( bds.GetLineThickness( m_textBox->GetLayer() ) );
372 }
373
374 m_borderWidth.Enable( border );
375 m_borderStyleLabel->Enable( border );
376 m_borderStyleCombo->Enable( border );
377}
378
379
381{
382 if( !DIALOG_TEXTBOX_PROPERTIES_BASE::TransferDataFromWindow() )
383 return false;
384
385 int minSize = pcbIUScale.mmToIU( TEXT_MIN_SIZE_MM );
386 int maxSize = pcbIUScale.mmToIU( TEXT_MAX_SIZE_MM );
387
388 if( !m_textWidth.Validate( minSize, maxSize ) || !m_textHeight.Validate( minSize, maxSize ) )
389 return false;
390
391 BOARD_COMMIT commit( m_frame );
392 commit.Modify( m_textBox );
393
394 // If no other command in progress, prepare undo command
395 // (for a command in progress, will be made later, at the completion of command)
396 bool pushCommit = ( m_textBox->GetEditFlags() == 0 );
397
398 // Set IN_EDIT flag to force undo/redo/abort proper operation and avoid new calls to
399 // SaveCopyInUndoList for the same text if is moved, and then rotated, edited, etc....
400 if( !pushCommit )
401 m_textBox->SetFlags( IN_EDIT );
402
403 BOARD* board = m_frame->GetBoard();
404 wxString txt = board->ConvertCrossReferencesToKIIDs( m_MultiLineText->GetValue() );
405
406#ifdef __WXMAC__
407 // On macOS CTRL+Enter produces '\r' instead of '\n' regardless of EOL setting.
408 // Replace it now.
409 txt.Replace( "\r", "\n" );
410#elif defined( __WINDOWS__ )
411 // On Windows, a new line is coded as \r\n. We use only \n in kicad files and in
412 // drawing routines so strip the \r char.
413 txt.Replace( "\r", "" );
414#endif
415
416 m_textBox->SetText( EscapeString( txt, CTX_QUOTED_STR ) );
417 m_textBox->SetLocked( m_cbLocked->GetValue() );
418 m_textBox->SetLayer( ToLAYER_ID( m_LayerSelectionCtrl->GetLayerSelection() ) );
419
420 if( m_fontCtrl->HaveFontSelection() )
421 {
422 m_textBox->SetFont( m_fontCtrl->GetFontSelection( m_bold->IsChecked(), m_italic->IsChecked() ) );
423 }
424
425 m_textBox->SetTextSize( VECTOR2I( m_textWidth.GetValue(), m_textHeight.GetValue() ) );
426
427 if( m_autoTextThickness->IsChecked() )
428 {
429 m_textBox->SetAutoThickness( true );
430 }
431 else
432 {
433 m_textBox->SetTextThickness( m_thickness.GetValue() );
434
435 // Test for acceptable values for thickness and size and clamp if fails
436 int maxPenWidth = ClampTextPenSize( m_textBox->GetTextThickness(), m_textBox->GetTextSize() );
437
438 if( m_textBox->GetTextThickness() > maxPenWidth )
439 {
440 DisplayError( this, _( "The text thickness is too large for the text size.\n"
441 "It will be clamped." ) );
442 m_textBox->SetTextThickness( maxPenWidth );
443 }
444 }
445
446 m_textBox->SetTextAngle( m_orientation.GetAngleValue().Normalize() );
447 m_textBox->SetBoldFlag( m_bold->IsChecked() );
448 m_textBox->SetItalicFlag( m_italic->IsChecked() );
449
450 if( m_hAlignLeft->IsChecked() )
451 m_textBox->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
452 else if( m_hAlignCenter->IsChecked() )
453 m_textBox->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
454 else
455 m_textBox->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
456
457 if( m_vAlignTop->IsChecked() )
458 m_textBox->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
459 else if( m_vAlignCenter->IsChecked() )
460 m_textBox->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
461 else
462 m_textBox->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
463
464 m_textBox->SetMirrored( m_mirrored->IsChecked() );
465
466 m_textBox->SetBorderEnabled( m_borderCheckbox->GetValue() );
467 STROKE_PARAMS stroke = m_textBox->GetStroke();
468
469 if( !m_borderWidth.IsIndeterminate() )
470 stroke.SetWidth( m_borderWidth.GetValue() );
471
472 auto it = lineTypeNames.begin();
473 std::advance( it, m_borderStyleCombo->GetSelection() );
474
475 if( it == lineTypeNames.end() )
477 else
478 stroke.SetLineStyle( it->first );
479
480 m_textBox->SetStroke( stroke );
481
482 m_textBox->ClearBoundingBoxCache();
483 m_textBox->ClearRenderCache();
484
485 if( pushCommit )
486 commit.Push( _( "Edit Text Box Properties" ) );
487
488 return true;
489}
490
491
493{
495 m_scintillaTricks->CancelAutocomplete();
496
497 event.Skip();
498}
499
500
501void DIALOG_TEXTBOX_PROPERTIES::onSyntaxHelp( wxHyperlinkEvent& aEvent )
502{
504}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
@ 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:322
wxString ConvertCrossReferencesToKIIDs(const wxString &aSource) const
Convert cross-references back and forth between ${refDes:field} and ${kiid:field}.
Definition board.cpp:1906
wxString ConvertKIIDsToCrossReferences(const wxString &aSource) const
Definition board.cpp:1986
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:106
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:82
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 onThickness(wxCommandEvent &aEvent) override
void onBoldToggle(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:138
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:670
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:177
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:47
#define TEXT_MAX_SIZE_MM
Maximum text size in mm (~10 inches)
Definition eda_text.h:48
int GetPenSizeForBold(int aTextSize)
Definition gr_text.cpp:37
int GetPenSizeForNormal(int aTextSize)
Definition gr_text.cpp:61
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:73
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:737
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.
@ 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:695