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