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