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 <bitmaps.h>
21#include <scintilla_tricks.h>
22#include <gr_text.h>
23#include <sch_edit_frame.h>
26#include <widgets/font_choice.h>
28#include <widgets/wx_infobar.h>
29#include <sch_table.h>
30#include <sch_text.h>
31#include <sch_commit.h>
32#include <tool/tool_manager.h>
35
36DIALOG_TABLECELL_PROPERTIES::DIALOG_TABLECELL_PROPERTIES( SCH_EDIT_FRAME* aFrame, std::vector<SCH_TABLECELL*> aCells ) :
38 m_frame( aFrame ),
39 m_table( nullptr ),
40 m_cells( std::move( aCells ) ),
41 m_scintillaTricks( nullptr ),
42 m_helpWindow( nullptr ),
44 m_marginLeft( aFrame, nullptr, m_marginLeftCtrl, nullptr ),
45 m_marginTop( aFrame, nullptr, m_marginTopCtrl, m_marginTopUnits ),
46 m_marginRight( aFrame, nullptr, m_marginRightCtrl, nullptr ),
47 m_marginBottom( aFrame, nullptr, m_marginBottomCtrl, nullptr ),
50{
51 wxASSERT( m_cells.size() > 0 && m_cells[0] );
52
53 m_cellText->SetEOLMode( wxSTC_EOL_LF );
54
55 // Wrapping is display-only and does not insert newlines into the stored text.
56 m_cellText->SetWrapMode( wxSTC_WRAP_WORD );
57 m_cellText->SetWrapVisualFlags( wxSTC_WRAPVISUALFLAG_END );
58 m_cellText->SetWrapIndentMode( wxSTC_WRAPINDENT_INDENT );
59
60#ifdef _WIN32
61 // Without this setting, on Windows, some esoteric unicode chars create display issue
62 // in a wxStyledTextCtrl.
63 // for SetTechnology() info, see https://www.scintilla.org/ScintillaDoc.html#SCI_SETTECHNOLOGY
64 m_cellText->SetTechnology( wxSTC_TECHNOLOGY_DIRECTWRITE );
65#endif
66
68 m_cellText, wxT( "{}" ), false,
69 // onAcceptFn
70 [this]( wxKeyEvent& aEvent )
71 {
72 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
73 },
74
75 // onCharFn
76 [this]( wxStyledTextEvent& aEvent )
77 {
78 m_scintillaTricks->DoTextVarAutocomplete(
79 // getTokensFn
80 [this]( const wxString& xRef, wxArrayString* tokens )
81 {
82 getContextualTextVars( xRef, tokens );
83 } );
84 } );
85
87
88 m_table = static_cast<SCH_TABLE*>( m_cells[0]->GetParent() );
89
90 if( m_frame->GetColorSettings()->GetOverrideSchItemColors() )
91 m_infoBar->ShowMessage( _( "Note: individual item colors overridden in Preferences." ) );
92
93 m_hAlignLeft->SetIsRadioButton();
95 m_hAlignCenter->SetIsRadioButton();
97 m_hAlignRight->SetIsRadioButton();
99
100 m_vAlignTop->SetIsRadioButton();
102 m_vAlignCenter->SetIsRadioButton();
104 m_vAlignBottom->SetIsRadioButton();
106
108 m_textColorSwatch->SetSwatchBackground( canvas );
109 m_fillColorSwatch->SetSwatchBackground( canvas );
110
112 Layout();
113
114 m_hAlignLeft->Bind( wxEVT_BUTTON, &DIALOG_TABLECELL_PROPERTIES::onHAlignButton, this );
117 m_vAlignTop->Bind( wxEVT_BUTTON, &DIALOG_TABLECELL_PROPERTIES::onVAlignButton, this );
120
121 // Now all widgets have the size fixed, call FinishDialogSettings
123}
124
133
134void DIALOG_TABLECELL_PROPERTIES::getContextualTextVars( const wxString& aCrossRef, wxArrayString* aTokens )
135{
136 SCHEMATIC* schematic = m_table->Schematic();
137
138 if( !aCrossRef.IsEmpty() )
139 {
140 SCH_SYMBOL* refSymbol = nullptr;
141
142 if( schematic )
143 {
145 schematic->Hierarchy().GetSymbols( refs, SYMBOL_FILTER_NON_POWER );
146
147 for( int jj = 0; jj < (int) refs.GetCount(); jj++ )
148 {
149 SCH_REFERENCE& ref = refs[jj];
150
151 if( ref.GetSymbol()->GetRef( &ref.GetSheetPath(), true ) == aCrossRef )
152 {
153 refSymbol = ref.GetSymbol();
154 break;
155 }
156 }
157 }
158
159 if( refSymbol )
160 refSymbol->GetContextualTextVars( aTokens );
161 }
162 else
163 {
164 if( schematic && schematic->CurrentSheet().Last() )
165 {
166 schematic->CurrentSheet().Last()->GetContextualTextVars( aTokens );
167 }
168 else
169 {
170 for( std::pair<wxString, wxString> entry : Prj().GetTextVars() )
171 aTokens->push_back( entry.first );
172 }
173 }
174}
175
176
178{
179 if( !wxDialog::TransferDataToWindow() )
180 return false;
181
182 bool firstCell = true;
185
186 for( SCH_TABLECELL* cell : m_cells )
187 {
188 wxString text = cell->GetText();
189
190 // show text variable cross-references in a human-readable format
191 if( SCHEMATIC* schematic = cell->Schematic() )
192 text = schematic->ConvertKIIDsToRefs( text );
193
194 if( firstCell )
195 {
196 m_cellText->SetValue( text );
197 m_cellText->EmptyUndoBuffer();
198
199 m_fontCtrl->SetFontSelection( cell->GetFont() );
200 m_textSize.SetValue( cell->GetTextWidth() );
201
202 m_bold->Set3StateValue( cell->IsBold() ? wxCHK_CHECKED : wxCHK_UNCHECKED );
203 m_italic->Set3StateValue( cell->IsItalic() ? wxCHK_CHECKED : wxCHK_UNCHECKED );
204
205 hAlign = cell->GetHorizJustify();
206 vAlign = cell->GetVertJustify();
207
208 m_textColorBook->SetSelection( 1 );
209 m_textColorSwatch->SetSwatchColor( cell->GetTextColor(), false );
210 m_textColorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
211
212 m_fillColorBook->SetSelection( 1 );
213
214 if( cell->IsSolidFill() )
215 m_fillColorSwatch->SetSwatchColor( cell->GetFillColor(), false );
216 else
217 m_fillColorSwatch->SetSwatchColor( COLOR4D::UNSPECIFIED, false );
218
219 m_fillColorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
220
221 m_marginLeft.SetValue( cell->GetMarginLeft() );
222 m_marginTop.SetValue( cell->GetMarginTop() );
223 m_marginRight.SetValue( cell->GetMarginRight() );
224 m_marginBottom.SetValue( cell->GetMarginBottom() );
225
226 firstCell = false;
227 }
228 else
229 {
230 if( text != m_cellText->GetValue() )
231 {
232 m_cellText->SetValue( INDETERMINATE_STATE );
233 m_cellText->EmptyUndoBuffer();
234 }
235
236 if( cell->GetFont() != m_fontCtrl->GetFontSelection( cell->IsBold(), cell->IsItalic() ) )
237 m_fontCtrl->SetSelection( -1 );
238
239 if( cell->GetTextWidth() != m_textSize.GetValue() )
241
242 wxCheckBoxState bold = cell->IsBold() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
243
244 if( bold != m_bold->Get3StateValue() )
245 m_bold->Set3StateValue( wxCHK_UNDETERMINED );
246
247 wxCheckBoxState italic = cell->IsItalic() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
248
249 if( italic != m_italic->Get3StateValue() )
250 m_italic->Set3StateValue( wxCHK_UNDETERMINED );
251
252 if( cell->GetHorizJustify() != hAlign )
254
255 if( cell->GetVertJustify() != vAlign )
257
258 if( cell->GetTextColor() != m_textColorSwatch->GetSwatchColor() )
259 {
260 m_textColorBook->SetSelection( 0 );
261 m_textColorPopup->SetSelection( 0 );
262 }
263
264 COLOR4D fillColor = cell->IsSolidFill() ? cell->GetFillColor() : COLOR4D::UNSPECIFIED;
265
266 if( fillColor != m_fillColorSwatch->GetSwatchColor() )
267 {
268 m_fillColorBook->SetSelection( 0 );
269 m_fillColorPopup->SetSelection( 0 );
270 }
271
272 if( fillColor != m_fillColorSwatch->GetSwatchColor() )
273 fillColor = COLOR4D::UNSPECIFIED;
274
275 if( cell->GetMarginLeft() != m_marginLeft.GetIntValue() )
277
278 if( cell->GetMarginTop() != m_marginTop.GetIntValue() )
280
281 if( cell->GetMarginRight() != m_marginRight.GetIntValue() )
283
284 if( cell->GetMarginBottom() != m_marginBottom.GetIntValue() )
286 }
287
288 m_hAlignLeft->Check( false );
289 m_hAlignCenter->Check( false );
290 m_hAlignRight->Check( false );
291
292 switch( hAlign )
293 {
294 case GR_TEXT_H_ALIGN_LEFT: m_hAlignLeft->Check(); break;
295 case GR_TEXT_H_ALIGN_CENTER: m_hAlignCenter->Check(); break;
296 case GR_TEXT_H_ALIGN_RIGHT: m_hAlignRight->Check(); break;
298 }
299
300 m_vAlignTop->Check( false );
301 m_vAlignCenter->Check( false );
302 m_vAlignBottom->Check( false );
303
304 switch( vAlign )
305 {
306 case GR_TEXT_V_ALIGN_TOP: m_vAlignTop->Check(); break;
307 case GR_TEXT_V_ALIGN_CENTER: m_vAlignCenter->Check(); break;
308 case GR_TEXT_V_ALIGN_BOTTOM: m_vAlignBottom->Check(); break;
310 }
311 }
312
313 return true;
314}
315
316
318{
320 {
321 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
322 btn->Check( false );
323 }
324}
325
326
328{
330 {
331 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
332 btn->Check( false );
333 }
334}
335
336
338{
339 if( aEvent.GetSelection() == 1 )
340 {
341 m_textColorBook->SetSelection( 1 );
342 m_textColorSwatch->GetNewSwatchColor();
343 }
344}
345
346
348{
349 if( aEvent.GetSelection() == 1 )
350 {
351 m_fillColorBook->SetSelection( 1 );
352 m_fillColorSwatch->GetNewSwatchColor();
353 }
354}
355
356
358{
359 if( !wxDialog::TransferDataFromWindow() )
360 return false;
361
362 SCH_COMMIT commit( m_frame );
363
364 /* save table in undo list if not already in edit */
365 if( m_table->GetEditFlags() == 0 )
366 commit.Modify( m_table, m_frame->GetScreen() );
367
368 for( SCH_TABLECELL* cell : m_cells )
369 {
370 if( m_cellTextCtrl->GetValue() != INDETERMINATE_STATE )
371 {
372 wxString text = m_cellTextCtrl->GetValue();
373
374 // convert any text variable cross-references to their UUIDs
375 if( SCHEMATIC* schematic = cell->Schematic() )
376 text = schematic->ConvertRefsToKIIDs( text );
377
378#ifdef __WXMAC__
379 // On macOS CTRL+Enter produces '\r' instead of '\n' regardless of EOL setting
380 text.Replace( "\r", "\n" );
381#elif defined( __WINDOWS__ )
382 // On Windows, a new line is coded as \r\n. We use only \n in KiCad files and in
383 // drawing routines so strip the \r char.
384 text.Replace( "\r", "" );
385#endif
386
387 cell->SetText( text );
388 }
389
390 if( m_bold->Get3StateValue() == wxCHK_CHECKED )
391 cell->SetBold( true );
392 else if( m_bold->Get3StateValue() == wxCHK_UNCHECKED )
393 cell->SetBold( false );
394
395 if( m_italic->Get3StateValue() == wxCHK_CHECKED )
396 cell->SetItalic( true );
397 else if( m_italic->Get3StateValue() == wxCHK_UNCHECKED )
398 cell->SetItalic( false );
399
400 if( m_fontCtrl->HaveFontSelection() )
401 cell->SetFont( m_fontCtrl->GetFontSelection( cell->IsBold(), cell->IsItalic() ) );
402
403 if( !m_textSize.IsIndeterminate() )
404 cell->SetTextSize( VECTOR2I( m_textSize.GetIntValue(), m_textSize.GetIntValue() ) );
405
406 if( m_hAlignLeft->IsChecked() )
407 cell->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
408 else if( m_hAlignRight->IsChecked() )
409 cell->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
410 else if( m_hAlignCenter->IsChecked() )
411 cell->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
412
413 if( m_vAlignTop->IsChecked() )
414 cell->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
415 else if( m_vAlignBottom->IsChecked() )
416 cell->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
417 else if( m_vAlignCenter->IsChecked() )
418 cell->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
419
420 if( m_textColorBook->GetSelection() == 1 )
421 cell->SetTextColor( m_textColorSwatch->GetSwatchColor() );
422
423 if( m_fillColorBook->GetSelection() == 1 )
424 {
425 COLOR4D fillColor = m_fillColorSwatch->GetSwatchColor();
426
427 if( fillColor == COLOR4D::UNSPECIFIED )
428 {
429 cell->SetFillMode( FILL_T::NO_FILL );
430 }
431 else
432 {
433 cell->SetFillMode( FILL_T::FILLED_WITH_COLOR );
434 cell->SetFillColor( fillColor );
435 }
436 }
437
438 if( !m_marginLeft.IsIndeterminate() )
439 cell->SetMarginLeft( m_marginLeft.GetIntValue() );
440
441 if( !m_marginTop.IsIndeterminate() )
442 cell->SetMarginTop( m_marginTop.GetIntValue() );
443
444 if( !m_marginRight.IsIndeterminate() )
445 cell->SetMarginRight( m_marginRight.GetIntValue() );
446
447 if( !m_marginBottom.IsIndeterminate() )
448 cell->SetMarginBottom( m_marginBottom.GetIntValue() );
449 }
450
451 if( !commit.Empty() )
452 commit.Push( _( "Edit Table Cell Properties" ) );
453
455 return true;
456}
457
458
459void DIALOG_TABLECELL_PROPERTIES::onEditTable( wxCommandEvent& aEvent )
460{
462 {
464 Close();
465 }
466}
467
469{
471 m_scintillaTricks->CancelAutocomplete();
472
473 event.Skip();
474}
475
476
477void DIALOG_TABLECELL_PROPERTIES::OnFormattingHelp( wxHyperlinkEvent& aEvent )
478{
479 if( m_helpWindow )
480 {
481 m_helpWindow->Raise();
482 m_helpWindow->Show( true );
483 return;
484 }
485
487}
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:95
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:155
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:399
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:76
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:519
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:48
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708