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 m_scintillaTricks = nullptr;
179
180 if( m_helpWindow )
181 m_helpWindow->Destroy();
182}
183
184
186{
187 DIALOG_TEXTBOX_PROPERTIES dlg( this, aTextBox );
188
189 // QuasiModal required for Scintilla auto-complete
190 return dlg.ShowQuasiModal();
191}
192
193
195{
196 BOARD* board = m_frame->GetBoard();
197 wxString converted = board->ConvertKIIDsToCrossReferences( UnescapeString( m_textBox->GetText() ) );
198
199 m_MultiLineText->SetValue( converted );
200 m_MultiLineText->SetSelection( -1, -1 );
201 m_MultiLineText->EmptyUndoBuffer();
202
203 m_cbLocked->SetValue( m_textBox->IsLocked() );
204
205 m_LayerSelectionCtrl->SetLayerSelection( m_textBox->GetLayer() );
206
207 m_fontCtrl->SetFontSelection( m_textBox->GetFont() );
208
209 m_textWidth.SetValue( m_textBox->GetTextSize().x );
210 m_textHeight.SetValue( m_textBox->GetTextSize().y );
211
212 if( m_textBox->GetAutoThickness() )
213 {
214 m_autoTextThickness->Check( m_textBox->GetAutoThickness() );
215 m_thickness.SetValue( m_textBox->GetEffectiveTextPenWidth() );
216 m_thickness.Enable( false );
217 }
218 else
219 {
220 m_thickness.SetValue( m_textBox->GetTextThickness() );
221 }
222
223 m_bold->Check( m_textBox->IsBold() );
224 m_italic->Check( m_textBox->IsItalic() );
225
226 switch( m_textBox->GetHorizJustify() )
227 {
228 case GR_TEXT_H_ALIGN_LEFT: m_hAlignLeft->Check( true ); break;
229 case GR_TEXT_H_ALIGN_CENTER: m_hAlignCenter->Check( true ); break;
230 case GR_TEXT_H_ALIGN_RIGHT: m_hAlignRight->Check( true ); break;
232 }
233
234 switch( m_textBox->GetVertJustify() )
235 {
236 case GR_TEXT_V_ALIGN_TOP: m_vAlignTop->Check( true ); break;
237 case GR_TEXT_V_ALIGN_CENTER: m_vAlignCenter->Check( true ); break;
238 case GR_TEXT_V_ALIGN_BOTTOM: m_vAlignBottom->Check( true ); break;
240 }
241
242 m_mirrored->Check( m_textBox->IsMirrored() );
243
244 EDA_ANGLE orientation = m_textBox->GetTextAngle();
245 m_orientation.SetAngleValue( orientation.Normalize180() );
246
247 STROKE_PARAMS stroke = m_textBox->GetStroke();
248 m_borderCheckbox->SetValue( m_textBox->IsBorderEnabled() );
249
250 if( m_textBox->IsBorderEnabled() )
251 m_borderWidth.SetValue( stroke.GetWidth() );
252
253 LINE_STYLE style = stroke.GetLineStyle();
254
255 if( style == LINE_STYLE::DEFAULT )
256 style = LINE_STYLE::SOLID;
257
258 if( (int) style < (int) lineTypeNames.size() )
259 m_borderStyleCombo->SetSelection( (int) style );
260
261 m_borderWidth.Enable( m_textBox->IsBorderEnabled() );
262 m_borderStyleLabel->Enable( m_textBox->IsBorderEnabled() );
263 m_borderStyleCombo->Enable( m_textBox->IsBorderEnabled() );
264
265 return DIALOG_TEXTBOX_PROPERTIES_BASE::TransferDataToWindow();
266}
267
268void DIALOG_TEXTBOX_PROPERTIES::onValignButton( wxCommandEvent& aEvent )
269{
271 {
272 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
273 btn->Check( false );
274 }
275}
276
277void DIALOG_TEXTBOX_PROPERTIES::onFontSelected( wxCommandEvent& aEvent )
278{
279 if( KIFONT::FONT::IsStroke( aEvent.GetString() ) )
280 {
281 m_thickness.Show( true );
282
283 int textSize = std::min( m_textWidth.GetValue(), m_textHeight.GetValue() );
284 int thickness = m_thickness.GetValue();
285
286 m_bold->Check( abs( thickness - GetPenSizeForBold( textSize ) )
287 < abs( thickness - GetPenSizeForNormal( textSize ) ) );
288 }
289 else
290 {
291 m_thickness.Show( false );
292 }
293}
294
295
296void DIALOG_TEXTBOX_PROPERTIES::onBoldToggle( wxCommandEvent& aEvent )
297{
298 int textSize = std::min( m_textWidth.GetValue(), m_textHeight.GetValue() );
299
300 if( aEvent.IsChecked() )
301 m_thickness.ChangeValue( GetPenSizeForBold( textSize ) );
302 else
303 m_thickness.ChangeValue( GetPenSizeForNormal( textSize ) );
304
305 aEvent.Skip();
306}
307
308
309void DIALOG_TEXTBOX_PROPERTIES::onHalignButton( wxCommandEvent& aEvent )
310{
312 {
313 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
314 btn->Check( false );
315 }
316}
317
318
319void DIALOG_TEXTBOX_PROPERTIES::onThickness( wxCommandEvent& event )
320{
321 int textSize = std::min( m_textWidth.GetValue(), m_textHeight.GetValue() );
322 int thickness = m_thickness.GetValue();
323
324 m_bold->Check( abs( thickness - GetPenSizeForBold( textSize ) )
325 < abs( thickness - GetPenSizeForNormal( textSize ) ) );
326}
327
328
329void DIALOG_TEXTBOX_PROPERTIES::onTextSize( wxCommandEvent& aEvent )
330{
331 if( m_autoTextThickness->IsChecked() )
332 {
333 int textSize = std::min( m_textWidth.GetValue(), m_textHeight.GetValue() );
334 int thickness;
335
336 // Calculate the "best" thickness from text size and bold option:
337 if( m_bold->IsChecked() )
338 thickness = GetPenSizeForBold( textSize );
339 else
340 thickness = GetPenSizeForNormal( textSize );
341
342 m_thickness.SetValue( thickness );
343 }
344}
345
346
348{
349 if( aEvent.IsChecked() )
350 {
351 m_autoTextThickness->Check( true );
352
353 wxCommandEvent dummy;
354 onTextSize( dummy );
355
356 m_thickness.Enable( false );
357 }
358 else
359 {
360 m_thickness.Enable( true );
361 }
362}
363
364
366{
367 bool border = m_borderCheckbox->GetValue();
368
369 if( border && m_borderWidth.GetValue() <= 0 )
370 {
371 BOARD_DESIGN_SETTINGS& bds = m_textBox->GetBoard()->GetDesignSettings();
372 m_borderWidth.SetValue( bds.GetLineThickness( m_textBox->GetLayer() ) );
373 }
374
375 m_borderWidth.Enable( border );
376 m_borderStyleLabel->Enable( border );
377 m_borderStyleCombo->Enable( border );
378}
379
380
382{
383 if( !DIALOG_TEXTBOX_PROPERTIES_BASE::TransferDataFromWindow() )
384 return false;
385
386 int minSize = pcbIUScale.mmToIU( TEXT_MIN_SIZE_MM );
387 int maxSize = pcbIUScale.mmToIU( TEXT_MAX_SIZE_MM );
388
389 if( !m_textWidth.Validate( minSize, maxSize ) || !m_textHeight.Validate( minSize, maxSize ) )
390 return false;
391
392 BOARD_COMMIT commit( m_frame );
393 commit.Modify( m_textBox );
394
395 // If no other command in progress, prepare undo command
396 // (for a command in progress, will be made later, at the completion of command)
397 bool pushCommit = ( m_textBox->GetEditFlags() == 0 );
398
399 // Set IN_EDIT flag to force undo/redo/abort proper operation and avoid new calls to
400 // SaveCopyInUndoList for the same text if is moved, and then rotated, edited, etc....
401 if( !pushCommit )
402 m_textBox->SetFlags( IN_EDIT );
403
404 BOARD* board = m_frame->GetBoard();
405 wxString txt = board->ConvertCrossReferencesToKIIDs( m_MultiLineText->GetValue() );
406
407#ifdef __WXMAC__
408 // On macOS CTRL+Enter produces '\r' instead of '\n' regardless of EOL setting.
409 // Replace it now.
410 txt.Replace( "\r", "\n" );
411#elif defined( __WINDOWS__ )
412 // On Windows, a new line is coded as \r\n. We use only \n in kicad files and in
413 // drawing routines so strip the \r char.
414 txt.Replace( "\r", "" );
415#endif
416
417 m_textBox->SetText( EscapeString( txt, CTX_QUOTED_STR ) );
418 m_textBox->SetLocked( m_cbLocked->GetValue() );
419 m_textBox->SetLayer( ToLAYER_ID( m_LayerSelectionCtrl->GetLayerSelection() ) );
420
421 if( m_fontCtrl->HaveFontSelection() )
422 {
423 m_textBox->SetFont( m_fontCtrl->GetFontSelection( m_bold->IsChecked(), m_italic->IsChecked() ) );
424 }
425
426 m_textBox->SetTextSize( VECTOR2I( m_textWidth.GetValue(), m_textHeight.GetValue() ) );
427
428 if( m_autoTextThickness->IsChecked() )
429 {
430 m_textBox->SetAutoThickness( true );
431 }
432 else
433 {
434 m_textBox->SetTextThickness( m_thickness.GetValue() );
435
436 // Test for acceptable values for thickness and size and clamp if fails
437 int maxPenWidth = ClampTextPenSize( m_textBox->GetTextThickness(), m_textBox->GetTextSize() );
438
439 if( m_textBox->GetTextThickness() > maxPenWidth )
440 {
441 DisplayError( this, _( "The text thickness is too large for the text size.\n"
442 "It will be clamped." ) );
443 m_textBox->SetTextThickness( maxPenWidth );
444 }
445 }
446
447 m_textBox->SetTextAngle( m_orientation.GetAngleValue().Normalize() );
448 m_textBox->SetBoldFlag( m_bold->IsChecked() );
449 m_textBox->SetItalicFlag( m_italic->IsChecked() );
450
451 if( m_hAlignLeft->IsChecked() )
452 m_textBox->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
453 else if( m_hAlignCenter->IsChecked() )
454 m_textBox->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
455 else
456 m_textBox->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
457
458 if( m_vAlignTop->IsChecked() )
459 m_textBox->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
460 else if( m_vAlignCenter->IsChecked() )
461 m_textBox->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
462 else
463 m_textBox->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
464
465 m_textBox->SetMirrored( m_mirrored->IsChecked() );
466
467 m_textBox->SetBorderEnabled( m_borderCheckbox->GetValue() );
468 STROKE_PARAMS stroke = m_textBox->GetStroke();
469
470 if( !m_borderWidth.IsIndeterminate() )
471 stroke.SetWidth( m_borderWidth.GetValue() );
472
473 auto it = lineTypeNames.begin();
474 std::advance( it, m_borderStyleCombo->GetSelection() );
475
476 if( it == lineTypeNames.end() )
478 else
479 stroke.SetLineStyle( it->first );
480
481 m_textBox->SetStroke( stroke );
482
483 m_textBox->ClearBoundingBoxCache();
484 m_textBox->ClearRenderCache();
485
486 if( pushCommit )
487 commit.Push( _( "Edit Text Box Properties" ) );
488
489 return true;
490}
491
492
494{
496 m_scintillaTricks->CancelAutocomplete();
497
498 event.Skip();
499}
500
501
502void DIALOG_TEXTBOX_PROPERTIES::onSyntaxHelp( wxHyperlinkEvent& aEvent )
503{
505}
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:1933
wxString ConvertKIIDsToCrossReferences(const wxString &aSource) const
Definition board.cpp:2013
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