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