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