KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbnew/dialogs/dialog_tablecell_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
20#include <wx/hyperlink.h>
21
23#include <widgets/font_choice.h>
25#include <confirm.h>
26#include <board_commit.h>
28#include <board.h>
29#include <footprint.h>
30#include <pcb_textbox.h>
31#include <pcb_tablecell.h>
32#include <pcb_table.h>
33#include <project.h>
34#include <pcb_edit_frame.h>
36#include <tool/tool_manager.h>
37#include <tools/pcb_actions.h>
38#include <scintilla_tricks.h>
40
42 std::vector<PCB_TABLECELL*> aCells ) :
44 m_frame( aFrame ),
45 m_table( nullptr ),
46 m_cells( std::move( aCells ) ),
50 m_marginLeft( aFrame, nullptr, m_marginLeftCtrl, nullptr ),
51 m_marginTop( aFrame, nullptr, m_marginTopCtrl, m_marginTopUnits ),
52 m_marginRight( aFrame, nullptr, m_marginRightCtrl, nullptr ),
53 m_marginBottom( aFrame, nullptr, m_marginBottomCtrl, nullptr ),
55 m_cellTextIsGenerated( false ),
57{
58 wxASSERT( m_cells.size() > 0 && m_cells[0] );
59
60 m_cellText->SetEOLMode( wxSTC_EOL_LF );
61
62 // Wrapping is display-only and does not insert newlines into the stored text.
63 m_cellText->SetWrapMode( wxSTC_WRAP_WORD );
64 m_cellText->SetWrapVisualFlags( wxSTC_WRAPVISUALFLAG_END );
65 m_cellText->SetWrapIndentMode( wxSTC_WRAPINDENT_INDENT );
66
67#ifdef _WIN32
68 // Without this setting, on Windows, some esoteric unicode chars create display issue
69 // in a wxStyledTextCtrl.
70 // for SetTechnology() info, see https://www.scintilla.org/ScintillaDoc.html#SCI_SETTECHNOLOGY
71 m_cellText->SetTechnology( wxSTC_TECHNOLOGY_DIRECTWRITE );
72#endif
73
75 m_cellText, wxT( "{}" ), false,
76 // onAcceptFn
77 [this]( wxKeyEvent& aEvent )
78 {
79 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
80 },
81 // onCharFn
82 [this]( wxStyledTextEvent& aEvent )
83 {
84 m_scintillaTricks->DoTextVarAutocomplete(
85 // getTokensFn
86 [this]( const wxString& xRef, wxArrayString* tokens )
87 {
88 m_frame->GetContextualTextVars( m_table, xRef, tokens );
89 } );
90 } );
91
92 // A hack which causes Scintilla to auto-size the text editor canvas
93 // See: https://github.com/jacobslusser/ScintillaNET/issues/216
94 m_cellText->SetScrollWidth( 1 );
95 m_cellText->SetScrollWidthTracking( true );
96
98
99 m_table = static_cast<PCB_TABLE*>( m_cells[0]->GetParent() );
100
101 // A drill chart reports the board. Its text is generated, so only the formatting on this
102 // page is the user's to change
103 if( m_table->Type() == PCB_DRILL_CHART_T )
104 {
106 m_cellText->SetReadOnly( true );
108 }
109
110 m_hAlignLeft->SetIsRadioButton();
112 m_hAlignCenter->SetIsRadioButton();
114 m_hAlignRight->SetIsRadioButton();
116
117 m_separator0->SetIsSeparator();
118
119 m_vAlignTop->SetIsRadioButton();
121 m_vAlignCenter->SetIsRadioButton();
123 m_vAlignBottom->SetIsRadioButton();
125
126 m_separator1->SetIsSeparator();
127
128 m_bold->SetIsCheckButton();
130 m_italic->SetIsCheckButton();
132
133 m_autoTextThickness->SetIsCheckButton();
135
137 Layout();
138
139 m_helpWindow = nullptr;
140
141 Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TABLECELL_PROPERTIES::OnCharHook ), nullptr, this );
142
143 m_hAlignLeft->Bind( wxEVT_BUTTON, &DIALOG_TABLECELL_PROPERTIES::onHAlignButton, this );
146 m_vAlignTop->Bind( wxEVT_BUTTON, &DIALOG_TABLECELL_PROPERTIES::onVAlignButton, this );
149
150 // Now all widgets have the size fixed, call FinishDialogSettings
152}
153
155{
156 Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TABLECELL_PROPERTIES::OnCharHook ), nullptr, this );
157
158 delete m_scintillaTricks;
159 m_scintillaTricks = nullptr;
160
161 if( m_helpWindow )
162 m_helpWindow->Destroy();
163}
164
165
167{
168 if( !wxDialog::TransferDataToWindow() )
169 return false;
170
171 // Scintilla drops every write while it is read-only, so generated text would arrive here
172 // as an empty control and be written back over the cell on OK
173 m_cellText->SetReadOnly( false );
174
175 bool firstCell = true;
178 int textThickness = 0;
179 int basePenWidth = 0;
180
181 for( PCB_TABLECELL* cell : m_cells )
182 {
183 if( firstCell )
184 {
185 m_cellTextCtrl->SetValue( cell->GetText() );
186
187 m_fontCtrl->SetFontSelection( cell->GetFont() );
188 m_textWidth.SetValue( cell->GetTextWidth() );
189 m_textHeight.SetValue( cell->GetTextHeight() );
190 textThickness = cell->GetTextThickness();
191 basePenWidth = GetPenSizeForNormal( cell->GetTextWidth() );
192
193 hAlign = cell->GetHorizJustify();
194 vAlign = cell->GetVertJustify();
195
196 m_marginLeft.SetValue( cell->GetMarginLeft() );
197 m_marginTop.SetValue( cell->GetMarginTop() );
198 m_marginRight.SetValue( cell->GetMarginRight() );
199 m_marginBottom.SetValue( cell->GetMarginBottom() );
200
201 m_bold->Check( cell->IsBold() );
202 m_mixedBoldSetting = false;
203
204 m_italic->Check( cell->IsItalic() );
205 m_mixedItalicSetting = false;
206
207 m_cbKnockout->SetValue( cell->IsKnockout() );
208
209 firstCell = false;
210 }
211 else
212 {
213 if( cell->GetText() != m_cellTextCtrl->GetValue() )
215
216 if( cell->GetFont() != m_fontCtrl->GetFontSelection( cell->IsBold(), cell->IsItalic() ) )
217 m_fontCtrl->SetSelection( -1 );
218
219 if( cell->GetTextWidth() != m_textWidth.GetValue() )
221
222 if( cell->GetTextHeight() != m_textHeight.GetValue() )
224
225 if( cell->GetTextThickness() != textThickness )
226 textThickness = -1;
227
228 if( GetPenSizeForNormal( cell->GetTextWidth() ) != basePenWidth )
229 basePenWidth = -1;
230
231 if( cell->IsBold() != m_bold->IsChecked() )
232 {
233 m_mixedBoldSetting = true;
234 m_bold->Check( false );
235 }
236
237 if( cell->IsItalic() != m_italic->IsChecked() )
238 {
240 m_italic->Check( false );
241 }
242
243 if( cell->GetHorizJustify() != hAlign )
245
246 if( cell->GetVertJustify() != vAlign )
248
249 if( cell->GetMarginLeft() != m_marginLeft.GetIntValue() )
251
252 if( cell->GetMarginTop() != m_marginTop.GetIntValue() )
254
255 if( cell->GetMarginRight() != m_marginRight.GetIntValue() )
257
258 if( cell->GetMarginBottom() != m_marginBottom.GetIntValue() )
260
261 if( cell->IsKnockout() != m_cbKnockout->GetValue() )
262 m_cbKnockout->Set3StateValue( wxCHK_UNDETERMINED );
263 }
264
265 switch( hAlign )
266 {
267 case GR_TEXT_H_ALIGN_LEFT: m_hAlignLeft->Check(); break;
268 case GR_TEXT_H_ALIGN_CENTER: m_hAlignCenter->Check(); break;
269 case GR_TEXT_H_ALIGN_RIGHT: m_hAlignRight->Check(); break;
271 }
272
273 switch( vAlign )
274 {
275 case GR_TEXT_V_ALIGN_TOP: m_vAlignTop->Check(); break;
276 case GR_TEXT_V_ALIGN_CENTER: m_vAlignCenter->Check(); break;
277 case GR_TEXT_V_ALIGN_BOTTOM: m_vAlignBottom->Check(); break;
279 }
280 }
281
283 m_autoTextThickness->Check( false );
284
285 if( textThickness == 0 )
286 {
287 // Show the base width (frozen if auto is unchecked); Bold scales it at render time.
288 if( basePenWidth > 0 )
289 m_textThickness.SetValue( basePenWidth );
290
291 m_autoTextThickness->Check( true );
292 m_textThickness.Enable( false );
293 }
294 else if( textThickness > 0 )
295 {
296 m_textThickness.SetValue( textThickness );
297 }
298
299 m_cellText->SetReadOnly( m_cellTextIsGenerated );
300
301 return true;
302}
303
304void DIALOG_TABLECELL_PROPERTIES::onTextSize( wxCommandEvent& aEvent )
305{
306 if( m_autoTextThickness->IsChecked() )
307 m_textThickness.SetValue( GetPenSizeForNormal( m_textWidth.GetIntValue() ) );
308}
309
310
312{
313 if( aEvent.IsChecked() )
314 {
315 m_autoTextThickness->Check( true );
316
317 if( !m_textWidth.IsIndeterminate() && !m_textHeight.IsIndeterminate() )
318 {
319 wxCommandEvent dummy;
320 onTextSize( dummy );
321 }
322
323 m_textThickness.Enable( false );
324 }
325 else
326 {
327 m_textThickness.Enable( true );
328 }
329}
330
331
332void DIALOG_TABLECELL_PROPERTIES::onBold(wxCommandEvent& aEvent)
333{
334 m_mixedBoldSetting = false;
335 aEvent.Skip();
336}
337
338
339void DIALOG_TABLECELL_PROPERTIES::onItalic(wxCommandEvent& aEvent)
340{
341 m_mixedItalicSetting = false;
342 aEvent.Skip();
343}
344
345
346void DIALOG_TABLECELL_PROPERTIES::onHAlignButton( wxCommandEvent& aEvent )
347{
349 {
350 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
351 btn->Check( false );
352 }
353}
354
355
356void DIALOG_TABLECELL_PROPERTIES::onVAlignButton( wxCommandEvent& aEvent )
357{
358 for( BITMAP_BUTTON* btn : { m_vAlignTop, m_vAlignCenter, m_vAlignBottom } )
359 {
360 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
361 btn->Check( false );
362 }
363}
364
365
367{
368 if( !wxDialog::TransferDataFromWindow() )
369 return false;
370
371 BOARD_COMMIT commit( m_frame );
372 commit.Modify( m_table );
373
374 // If no other command in progress, prepare undo command
375 // (for a command in progress, will be made later, at the completion of command)
376 bool pushCommit = ( m_table->GetEditFlags() == 0 );
377
378 // Set IN_EDIT flag to force undo/redo/abort proper operation and avoid new calls to
379 // SaveCopyInUndoList for the same text if is moved, and then rotated, edited, etc....
380 if( !pushCommit )
381 m_table->SetFlags( IN_EDIT );
382
383 for( PCB_TABLECELL* cell : m_cells )
384 {
386 {
387 wxString txt = m_cellTextCtrl->GetValue();
388
389#ifdef __WXMAC__
390 // On macOS CTRL+Enter produces '\r' instead of '\n' regardless of EOL setting.
391 // Replace it now.
392 txt.Replace( "\r", "\n" );
393#elif defined( __WINDOWS__ )
394 // On Windows, a new line is coded as \r\n. We use only \n in kicad files and in
395 // drawing routines so strip the \r char.
396 txt.Replace( "\r", "" );
397#endif
398
399 cell->SetText( txt );
400 }
401
402 if( !m_mixedBoldSetting )
403 cell->SetBold( m_bold->IsChecked() );
404
406 cell->SetItalic( m_italic->IsChecked() );
407
408 if( m_cbKnockout->Get3StateValue() == wxCHK_CHECKED )
409 cell->SetIsKnockout( true );
410 else if( m_cbKnockout->Get3StateValue() == wxCHK_UNCHECKED )
411 cell->SetIsKnockout( false );
412
413 if( m_fontCtrl->HaveFontSelection() )
414 cell->SetFont( m_fontCtrl->GetFontSelection( cell->IsBold(), cell->IsItalic() ) );
415
416 if( !m_textWidth.IsIndeterminate() )
417 cell->SetTextWidth( m_textWidth.GetIntValue() );
418
419 if( !m_textHeight.IsIndeterminate() )
420 cell->SetTextHeight( m_textHeight.GetIntValue() );
421
422 if( m_autoTextThickness->IsChecked() )
423 cell->SetAutoThickness( true );
424 else if( !m_textThickness.IsIndeterminate() )
425 cell->SetTextThickness( m_textThickness.GetIntValue() );
426
427 if( m_hAlignLeft->IsChecked() )
428 cell->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
429 else if( m_hAlignRight->IsChecked() )
430 cell->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
431 else if( m_hAlignCenter->IsChecked() )
432 cell->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
433
434 if( m_vAlignTop->IsChecked() )
435 cell->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
436 else if( m_vAlignBottom->IsChecked() )
437 cell->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
438 else if( m_vAlignCenter->IsChecked() )
439 cell->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
440
441 if( !m_marginLeft.IsIndeterminate() )
442 cell->SetMarginLeft( m_marginLeft.GetIntValue() );
443
444 if( !m_marginTop.IsIndeterminate() )
445 cell->SetMarginTop( m_marginTop.GetIntValue() );
446
447 if( !m_marginRight.IsIndeterminate() )
448 cell->SetMarginRight( m_marginRight.GetIntValue() );
449
450 if( !m_marginBottom.IsIndeterminate() )
451 cell->SetMarginBottom( m_marginBottom.GetIntValue() );
452 }
453
454 if( !commit.Empty() )
455 commit.Push( _( "Edit Table Cell Properties" ), SKIP_CONNECTIVITY );
456
458 return true;
459}
460
461
462void DIALOG_TABLECELL_PROPERTIES::onEditTable( wxCommandEvent& aEvent )
463{
465 {
467 Close();
468 }
469}
470
471
472void DIALOG_TABLECELL_PROPERTIES::onSyntaxHelp( wxHyperlinkEvent& aEvent )
473{
474 if( m_helpWindow )
475 {
476 m_helpWindow->Raise();
477 m_helpWindow->Show( true );
478 return;
479 }
480
482}
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.
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_TABLECELL_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Table Cell Properties"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
bool m_cellTextIsGenerated
Generated cell text, shown so the user can read it but not theirs to change.
void onAutoTextThickness(wxCommandEvent &aEvent) override
DIALOG_TABLECELL_PROPERTIES(SCH_EDIT_FRAME *aParentFrame, std::vector< SCH_TABLECELL * > aCells)
void onSyntaxHelp(wxHyperlinkEvent &aEvent) override
Common, abstract interface for edit frames.
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.
This file is part of the common library.
#define _(s)
#define IN_EDIT
Item currently edited.
int GetPenSizeForNormal(int aTextSize)
Definition gr_text.cpp:57
STL namespace.
#define SKIP_CONNECTIVITY
Definition sch_commit.h:41
std::vector< FAB_LAYER_COLOR > dummy
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_H_ALIGN_INDETERMINATE
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_INDETERMINATE
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
@ PCB_DRILL_CHART_T
class PCB_DRILL_CHART, a live drill chart derived from PCB_TABLE
Definition typeinfo.h:239
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition ui_common.h:46