KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eeschema/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 <scintilla_tricks.h>
21#include <gr_text.h>
22#include <sch_edit_frame.h>
25#include <widgets/font_choice.h>
27#include <widgets/wx_infobar.h>
28#include <sch_table.h>
29#include <sch_text.h>
30#include <sch_commit.h>
31#include <tool/tool_manager.h>
34
35DIALOG_TABLECELL_PROPERTIES::DIALOG_TABLECELL_PROPERTIES( SCH_EDIT_FRAME* aFrame, std::vector<SCH_TABLECELL*> aCells ) :
37 m_frame( aFrame ),
38 m_table( nullptr ),
39 m_cells( std::move( aCells ) ),
40 m_scintillaTricks( nullptr ),
41 m_helpWindow( nullptr ),
43 m_marginLeft( aFrame, nullptr, m_marginLeftCtrl, nullptr ),
44 m_marginTop( aFrame, nullptr, m_marginTopCtrl, m_marginTopUnits ),
45 m_marginRight( aFrame, nullptr, m_marginRightCtrl, nullptr ),
46 m_marginBottom( aFrame, nullptr, m_marginBottomCtrl, nullptr ),
49{
50 wxASSERT( m_cells.size() > 0 && m_cells[0] );
51
52 m_cellText->SetEOLMode( wxSTC_EOL_LF );
53
54 // Wrapping is display-only and does not insert newlines into the stored text.
55 m_cellText->SetWrapMode( wxSTC_WRAP_WORD );
56 m_cellText->SetWrapVisualFlags( wxSTC_WRAPVISUALFLAG_END );
57 m_cellText->SetWrapIndentMode( wxSTC_WRAPINDENT_INDENT );
58
59#ifdef _WIN32
60 // Without this setting, on Windows, some esoteric unicode chars create display issue
61 // in a wxStyledTextCtrl.
62 // for SetTechnology() info, see https://www.scintilla.org/ScintillaDoc.html#SCI_SETTECHNOLOGY
63 m_cellText->SetTechnology( wxSTC_TECHNOLOGY_DIRECTWRITE );
64#endif
65
67 m_cellText, wxT( "{}" ), false,
68 // onAcceptFn
69 [this]( wxKeyEvent& aEvent )
70 {
71 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
72 },
73
74 // onCharFn
75 [this]( wxStyledTextEvent& aEvent )
76 {
77 m_scintillaTricks->DoTextVarAutocomplete(
78 // getTokensFn
79 [this]( const wxString& xRef, wxArrayString* tokens )
80 {
81 getContextualTextVars( xRef, tokens );
82 } );
83 } );
84
86
87 m_table = static_cast<SCH_TABLE*>( m_cells[0]->GetParent() );
88
89 if( m_frame->GetColorSettings()->GetOverrideSchItemColors() )
90 m_infoBar->ShowMessage( _( "Note: individual item colors overridden in Preferences." ) );
91
92 m_hAlignLeft->SetIsRadioButton();
94 m_hAlignCenter->SetIsRadioButton();
96 m_hAlignRight->SetIsRadioButton();
98
99 m_vAlignTop->SetIsRadioButton();
101 m_vAlignCenter->SetIsRadioButton();
103 m_vAlignBottom->SetIsRadioButton();
105
107 m_textColorSwatch->SetSwatchBackground( canvas );
108 m_fillColorSwatch->SetSwatchBackground( canvas );
109
111 Layout();
112
113 m_hAlignLeft->Bind( wxEVT_BUTTON, &DIALOG_TABLECELL_PROPERTIES::onHAlignButton, this );
116 m_vAlignTop->Bind( wxEVT_BUTTON, &DIALOG_TABLECELL_PROPERTIES::onVAlignButton, this );
119
120 // Now all widgets have the size fixed, call FinishDialogSettings
122}
123
132
133void DIALOG_TABLECELL_PROPERTIES::getContextualTextVars( const wxString& aCrossRef, wxArrayString* aTokens )
134{
135 SCHEMATIC* schematic = m_table->Schematic();
136
137 if( !aCrossRef.IsEmpty() )
138 {
139 SCH_SYMBOL* refSymbol = nullptr;
140
141 if( schematic )
142 {
144 schematic->Hierarchy().GetSymbols( refs, SYMBOL_FILTER_NON_POWER );
145
146 for( int jj = 0; jj < (int) refs.GetCount(); jj++ )
147 {
148 SCH_REFERENCE& ref = refs[jj];
149
150 if( ref.GetSymbol()->GetRef( &ref.GetSheetPath(), true ) == aCrossRef )
151 {
152 refSymbol = ref.GetSymbol();
153 break;
154 }
155 }
156 }
157
158 if( refSymbol )
159 refSymbol->GetContextualTextVars( aTokens );
160 }
161 else
162 {
163 if( schematic && schematic->CurrentSheet().Last() )
164 {
165 schematic->CurrentSheet().Last()->GetContextualTextVars( aTokens );
166 }
167 else
168 {
169 for( std::pair<wxString, wxString> entry : Prj().GetTextVars() )
170 aTokens->push_back( entry.first );
171 }
172 }
173}
174
175
177{
178 if( !wxDialog::TransferDataToWindow() )
179 return false;
180
181 bool firstCell = true;
184
185 for( SCH_TABLECELL* cell : m_cells )
186 {
187 wxString text = cell->GetText();
188
189 // show text variable cross-references in a human-readable format
190 if( SCHEMATIC* schematic = cell->Schematic() )
191 text = schematic->ConvertKIIDsToRefs( text );
192
193 m_cellText->SetValue( text );
194 m_cellText->EmptyUndoBuffer();
195
196 if( firstCell )
197 {
198 m_fontCtrl->SetFontSelection( cell->GetFont() );
199 m_textSize.SetValue( cell->GetTextWidth() );
200
201 m_bold->Set3StateValue( cell->IsBold() ? wxCHK_CHECKED : wxCHK_UNCHECKED );
202 m_italic->Set3StateValue( cell->IsItalic() ? wxCHK_CHECKED : wxCHK_UNCHECKED );
203
204 hAlign = cell->GetHorizJustify();
205 vAlign = cell->GetVertJustify();
206
207 m_textColorBook->SetSelection( 1 );
208 m_textColorSwatch->SetSwatchColor( cell->GetTextColor(), false );
209 m_textColorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
210
211 m_fillColorBook->SetSelection( 1 );
212
213 if( cell->IsSolidFill() )
214 m_fillColorSwatch->SetSwatchColor( cell->GetFillColor(), false );
215 else
216 m_fillColorSwatch->SetSwatchColor( COLOR4D::UNSPECIFIED, false );
217
218 m_fillColorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
219
220 m_marginLeft.SetValue( cell->GetMarginLeft() );
221 m_marginTop.SetValue( cell->GetMarginTop() );
222 m_marginRight.SetValue( cell->GetMarginRight() );
223 m_marginBottom.SetValue( cell->GetMarginBottom() );
224
225 firstCell = false;
226 }
227 else
228 {
229 if( cell->GetFont() != m_fontCtrl->GetFontSelection( cell->IsBold(), cell->IsItalic() ) )
230 m_fontCtrl->SetSelection( -1 );
231
232 if( cell->GetTextWidth() != m_textSize.GetValue() )
234
235 wxCheckBoxState bold = cell->IsBold() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
236
237 if( bold != m_bold->Get3StateValue() )
238 m_bold->Set3StateValue( wxCHK_UNDETERMINED );
239
240 wxCheckBoxState italic = cell->IsItalic() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
241
242 if( italic != m_italic->Get3StateValue() )
243 m_italic->Set3StateValue( wxCHK_UNDETERMINED );
244
245 if( cell->GetHorizJustify() != hAlign )
247
248 if( cell->GetVertJustify() != vAlign )
250
251 if( cell->GetTextColor() != m_textColorSwatch->GetSwatchColor() )
252 {
253 m_textColorBook->SetSelection( 0 );
254 m_textColorPopup->SetSelection( 0 );
255 }
256
257 COLOR4D fillColor = cell->IsSolidFill() ? cell->GetFillColor() : COLOR4D::UNSPECIFIED;
258
259 if( fillColor != m_fillColorSwatch->GetSwatchColor() )
260 {
261 m_fillColorBook->SetSelection( 0 );
262 m_fillColorPopup->SetSelection( 0 );
263 }
264
265 if( fillColor != m_fillColorSwatch->GetSwatchColor() )
266 fillColor = COLOR4D::UNSPECIFIED;
267
268 if( cell->GetMarginLeft() != m_marginLeft.GetIntValue() )
270
271 if( cell->GetMarginTop() != m_marginTop.GetIntValue() )
273
274 if( cell->GetMarginRight() != m_marginRight.GetIntValue() )
276
277 if( cell->GetMarginBottom() != m_marginBottom.GetIntValue() )
279 }
280
281 m_hAlignLeft->Check( false );
282 m_hAlignCenter->Check( false );
283 m_hAlignRight->Check( false );
284
285 switch( hAlign )
286 {
287 case GR_TEXT_H_ALIGN_LEFT: m_hAlignLeft->Check(); break;
288 case GR_TEXT_H_ALIGN_CENTER: m_hAlignCenter->Check(); break;
289 case GR_TEXT_H_ALIGN_RIGHT: m_hAlignRight->Check(); break;
291 }
292
293 m_vAlignTop->Check( false );
294 m_vAlignCenter->Check( false );
295 m_vAlignBottom->Check( false );
296
297 switch( vAlign )
298 {
299 case GR_TEXT_V_ALIGN_TOP: m_vAlignTop->Check(); break;
300 case GR_TEXT_V_ALIGN_CENTER: m_vAlignCenter->Check(); break;
301 case GR_TEXT_V_ALIGN_BOTTOM: m_vAlignBottom->Check(); break;
303 }
304 }
305
306 return true;
307}
308
309
311{
313 {
314 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
315 btn->Check( false );
316 }
317}
318
319
321{
323 {
324 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
325 btn->Check( false );
326 }
327}
328
329
331{
332 if( aEvent.GetSelection() == 1 )
333 {
334 m_textColorBook->SetSelection( 1 );
335 m_textColorSwatch->GetNewSwatchColor();
336 }
337}
338
339
341{
342 if( aEvent.GetSelection() == 1 )
343 {
344 m_fillColorBook->SetSelection( 1 );
345 m_fillColorSwatch->GetNewSwatchColor();
346 }
347}
348
349
351{
352 if( !wxDialog::TransferDataFromWindow() )
353 return false;
354
355 SCH_COMMIT commit( m_frame );
356
357 /* save table in undo list if not already in edit */
358 if( m_table->GetEditFlags() == 0 )
359 commit.Modify( m_table, m_frame->GetScreen() );
360
361 for( SCH_TABLECELL* cell : m_cells )
362 {
363 wxString text = m_cellTextCtrl->GetValue();
364
365 // convert any text variable cross-references to their UUIDs
366 if( SCHEMATIC* schematic = cell->Schematic() )
367 text = schematic->ConvertRefsToKIIDs( text );
368
369#ifdef __WXMAC__
370 // On macOS CTRL+Enter produces '\r' instead of '\n' regardless of EOL setting
371 text.Replace( "\r", "\n" );
372#elif defined( __WINDOWS__ )
373 // On Windows, a new line is coded as \r\n. We use only \n in KiCad files and in
374 // drawing routines so strip the \r char.
375 text.Replace( "\r", "" );
376#endif
377
378 cell->SetText( text );
379
380 if( m_bold->Get3StateValue() == wxCHK_CHECKED )
381 cell->SetBold( true );
382 else if( m_bold->Get3StateValue() == wxCHK_UNCHECKED )
383 cell->SetBold( false );
384
385 if( m_italic->Get3StateValue() == wxCHK_CHECKED )
386 cell->SetItalic( true );
387 else if( m_italic->Get3StateValue() == wxCHK_UNCHECKED )
388 cell->SetItalic( false );
389
390 if( m_fontCtrl->HaveFontSelection() )
391 cell->SetFont( m_fontCtrl->GetFontSelection( cell->IsBold(), cell->IsItalic() ) );
392
393 if( !m_textSize.IsIndeterminate() )
394 cell->SetTextSize( VECTOR2I( m_textSize.GetIntValue(), m_textSize.GetIntValue() ) );
395
396 if( m_hAlignLeft->IsChecked() )
397 cell->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
398 else if( m_hAlignRight->IsChecked() )
399 cell->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
400 else if( m_hAlignCenter->IsChecked() )
401 cell->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
402
403 if( m_vAlignTop->IsChecked() )
404 cell->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
405 else if( m_vAlignBottom->IsChecked() )
406 cell->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
407 else if( m_vAlignCenter->IsChecked() )
408 cell->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
409
410 if( m_textColorBook->GetSelection() == 1 )
411 cell->SetTextColor( m_textColorSwatch->GetSwatchColor() );
412
413 if( m_fillColorBook->GetSelection() == 1 )
414 {
415 COLOR4D fillColor = m_fillColorSwatch->GetSwatchColor();
416
417 if( fillColor == COLOR4D::UNSPECIFIED )
418 {
419 cell->SetFillMode( FILL_T::NO_FILL );
420 }
421 else
422 {
423 cell->SetFillMode( FILL_T::FILLED_WITH_COLOR );
424 cell->SetFillColor( fillColor );
425 }
426 }
427
428 if( !m_marginLeft.IsIndeterminate() )
429 cell->SetMarginLeft( m_marginLeft.GetIntValue() );
430
431 if( !m_marginTop.IsIndeterminate() )
432 cell->SetMarginTop( m_marginTop.GetIntValue() );
433
434 if( !m_marginRight.IsIndeterminate() )
435 cell->SetMarginRight( m_marginRight.GetIntValue() );
436
437 if( !m_marginBottom.IsIndeterminate() )
438 cell->SetMarginBottom( m_marginBottom.GetIntValue() );
439 }
440
441 if( !commit.Empty() )
442 commit.Push( _( "Edit Table Cell Properties" ) );
443
445 return true;
446}
447
448
449void DIALOG_TABLECELL_PROPERTIES::onEditTable( wxCommandEvent& aEvent )
450{
452 {
454 Close();
455 }
456}
457
459{
461 m_scintillaTricks->CancelAutocomplete();
462
463 event.Skip();
464}
465
466
467void DIALOG_TABLECELL_PROPERTIES::OnFormattingHelp( wxHyperlinkEvent& aEvent )
468{
469 if( m_helpWindow )
470 {
471 m_helpWindow->Raise();
472 m_helpWindow->Show( true );
473 return;
474 }
475
477}
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
A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:400
COLOR4D GetColor(int aLayer) const
bool Empty() const
Definition commit.h:145
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...
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)
void onTextColorPopup(wxCommandEvent &aEvent) override
void onFillColorPopup(wxCommandEvent &aEvent) override
DIALOG_TABLECELL_PROPERTIES(SCH_EDIT_FRAME *aParentFrame, std::vector< SCH_TABLECELL * > aCells)
void OnFormattingHelp(wxHyperlinkEvent &aEvent) override
void getContextualTextVars(const wxString &aCrossRef, wxArrayString *aTokens)
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
Holds all the data relating to one schematic.
Definition schematic.h:149
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:304
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Returns a pointer to the active color theme settings.
Commit for the schematic and symbol editors.
Definition sch_commit.h:54
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
Schematic editor (Eeschema) main window.
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
A helper to define a symbol's reference designator in a schematic.
const SCH_SHEET_PATH & GetSheetPath() const
SCH_SYMBOL * GetSymbol() const
void GetSymbols(SCH_REFERENCE_LIST &aReferences, SYMBOL_FILTER aSymbolFilter, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets.
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
void GetContextualTextVars(wxArrayString *aVars) const
Return the list of system text vars & fields for this sheet.
Schematic symbol object.
Definition sch_symbol.h:73
void GetContextualTextVars(wxArrayString *aVars) const
Return the list of system text vars & fields for this symbol.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
static HTML_MESSAGE_BOX * ShowSyntaxHelp(wxWindow *aParentWindow)
Add cut/copy/paste, dark theme, autocomplete and brace highlighting to a wxStyleTextCtrl instance.
#define _(s)
@ FILLED_WITH_COLOR
Definition eda_fill.h:33
@ NO_FILL
Definition eda_fill.h:30
@ LAYER_SCHEMATIC_BACKGROUND
Definition layer_ids.h:510
STL namespace.
@ SYMBOL_FILTER_NON_POWER
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
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition ui_common.h:46
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708