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
129
130void DIALOG_TABLECELL_PROPERTIES::getContextualTextVars( const wxString& aCrossRef, wxArrayString* aTokens )
131{
132 SCHEMATIC* schematic = m_table->Schematic();
133
134 if( !aCrossRef.IsEmpty() )
135 {
136 SCH_SYMBOL* refSymbol = nullptr;
137
138 if( schematic )
139 {
141 schematic->Hierarchy().GetSymbols( refs );
142
143 for( int jj = 0; jj < (int) refs.GetCount(); jj++ )
144 {
145 SCH_REFERENCE& ref = refs[jj];
146
147 if( ref.GetSymbol()->GetRef( &ref.GetSheetPath(), true ) == aCrossRef )
148 {
149 refSymbol = ref.GetSymbol();
150 break;
151 }
152 }
153 }
154
155 if( refSymbol )
156 refSymbol->GetContextualTextVars( aTokens );
157 }
158 else
159 {
160 if( schematic && schematic->CurrentSheet().Last() )
161 {
162 schematic->CurrentSheet().Last()->GetContextualTextVars( aTokens );
163 }
164 else
165 {
166 for( std::pair<wxString, wxString> entry : Prj().GetTextVars() )
167 aTokens->push_back( entry.first );
168 }
169 }
170}
171
172
174{
175 if( !wxDialog::TransferDataToWindow() )
176 return false;
177
178 bool firstCell = true;
181
182 for( SCH_TABLECELL* cell : m_cells )
183 {
184 wxString text = cell->GetText();
185
186 m_cellText->SetValue( text );
187 m_cellText->EmptyUndoBuffer();
188
189 if( firstCell )
190 {
191 m_fontCtrl->SetFontSelection( cell->GetFont() );
192 m_textSize.SetValue( cell->GetTextWidth() );
193
194 m_bold->Set3StateValue( cell->IsBold() ? wxCHK_CHECKED : wxCHK_UNCHECKED );
195 m_italic->Set3StateValue( cell->IsItalic() ? wxCHK_CHECKED : wxCHK_UNCHECKED );
196
197 hAlign = cell->GetHorizJustify();
198 vAlign = cell->GetVertJustify();
199
200 m_textColorBook->SetSelection( 1 );
201 m_textColorSwatch->SetSwatchColor( cell->GetTextColor(), false );
202 m_textColorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
203
204 m_fillColorBook->SetSelection( 1 );
205
206 if( cell->IsSolidFill() )
207 m_fillColorSwatch->SetSwatchColor( cell->GetFillColor(), false );
208 else
209 m_fillColorSwatch->SetSwatchColor( COLOR4D::UNSPECIFIED, false );
210
211 m_fillColorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
212
213 m_marginLeft.SetValue( cell->GetMarginLeft() );
214 m_marginTop.SetValue( cell->GetMarginTop() );
215 m_marginRight.SetValue( cell->GetMarginRight() );
216 m_marginBottom.SetValue( cell->GetMarginBottom() );
217
218 firstCell = false;
219 }
220 else
221 {
222 if( cell->GetFont() != m_fontCtrl->GetFontSelection( cell->IsBold(), cell->IsItalic() ) )
223 m_fontCtrl->SetSelection( -1 );
224
225 if( cell->GetTextWidth() != m_textSize.GetValue() )
227
228 wxCheckBoxState bold = cell->IsBold() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
229
230 if( bold != m_bold->Get3StateValue() )
231 m_bold->Set3StateValue( wxCHK_UNDETERMINED );
232
233 wxCheckBoxState italic = cell->IsItalic() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
234
235 if( italic != m_italic->Get3StateValue() )
236 m_italic->Set3StateValue( wxCHK_UNDETERMINED );
237
238 if( cell->GetHorizJustify() != hAlign )
240
241 if( cell->GetVertJustify() != vAlign )
243
244 if( cell->GetTextColor() != m_textColorSwatch->GetSwatchColor() )
245 {
246 m_textColorBook->SetSelection( 0 );
247 m_textColorPopup->SetSelection( 0 );
248 }
249
250 COLOR4D fillColor = cell->IsSolidFill() ? cell->GetFillColor() : COLOR4D::UNSPECIFIED;
251
252 if( fillColor != m_fillColorSwatch->GetSwatchColor() )
253 {
254 m_fillColorBook->SetSelection( 0 );
255 m_fillColorPopup->SetSelection( 0 );
256 }
257
258 if( fillColor != m_fillColorSwatch->GetSwatchColor() )
259 fillColor = COLOR4D::UNSPECIFIED;
260
261 if( cell->GetMarginLeft() != m_marginLeft.GetIntValue() )
263
264 if( cell->GetMarginTop() != m_marginTop.GetIntValue() )
266
267 if( cell->GetMarginRight() != m_marginRight.GetIntValue() )
269
270 if( cell->GetMarginBottom() != m_marginBottom.GetIntValue() )
272 }
273
274 switch( hAlign )
275 {
276 case GR_TEXT_H_ALIGN_LEFT: m_hAlignLeft->Check(); break;
277 case GR_TEXT_H_ALIGN_CENTER: m_hAlignCenter->Check(); break;
278 case GR_TEXT_H_ALIGN_RIGHT: m_hAlignRight->Check(); break;
280 }
281
282 switch( vAlign )
283 {
284 case GR_TEXT_V_ALIGN_TOP: m_vAlignTop->Check(); break;
285 case GR_TEXT_V_ALIGN_CENTER: m_vAlignCenter->Check(); break;
286 case GR_TEXT_V_ALIGN_BOTTOM: m_vAlignBottom->Check(); break;
288 }
289 }
290
291 return true;
292}
293
294
296{
298 {
299 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
300 btn->Check( false );
301 }
302}
303
304
306{
308 {
309 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
310 btn->Check( false );
311 }
312}
313
314
316{
317 if( aEvent.GetSelection() == 1 )
318 {
319 m_textColorBook->SetSelection( 1 );
320 m_textColorSwatch->GetNewSwatchColor();
321 }
322}
323
324
326{
327 if( aEvent.GetSelection() == 1 )
328 {
329 m_fillColorBook->SetSelection( 1 );
330 m_fillColorSwatch->GetNewSwatchColor();
331 }
332}
333
334
336{
337 if( !wxDialog::TransferDataFromWindow() )
338 return false;
339
340 SCH_COMMIT commit( m_frame );
341
342 /* save table in undo list if not already in edit */
343 if( m_table->GetEditFlags() == 0 )
344 commit.Modify( m_table, m_frame->GetScreen() );
345
346 for( SCH_TABLECELL* cell : m_cells )
347 {
348 wxString text = m_cellTextCtrl->GetValue();
349
350#ifdef __WXMAC__
351 // On macOS CTRL+Enter produces '\r' instead of '\n' regardless of EOL setting
352 text.Replace( "\r", "\n" );
353#elif defined( __WINDOWS__ )
354 // On Windows, a new line is coded as \r\n. We use only \n in KiCad files and in
355 // drawing routines so strip the \r char.
356 text.Replace( "\r", "" );
357#endif
358
359 cell->SetText( text );
360
361 if( m_bold->Get3StateValue() == wxCHK_CHECKED )
362 cell->SetBold( true );
363 else if( m_bold->Get3StateValue() == wxCHK_UNCHECKED )
364 cell->SetBold( false );
365
366 if( m_italic->Get3StateValue() == wxCHK_CHECKED )
367 cell->SetItalic( true );
368 else if( m_italic->Get3StateValue() == wxCHK_UNCHECKED )
369 cell->SetItalic( false );
370
371 if( m_fontCtrl->HaveFontSelection() )
372 cell->SetFont( m_fontCtrl->GetFontSelection( cell->IsBold(), cell->IsItalic() ) );
373
374 if( !m_textSize.IsIndeterminate() )
375 cell->SetTextSize( VECTOR2I( m_textSize.GetIntValue(), m_textSize.GetIntValue() ) );
376
377 if( m_hAlignLeft->IsChecked() )
378 cell->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
379 else if( m_hAlignRight->IsChecked() )
380 cell->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
381 else if( m_hAlignCenter->IsChecked() )
382 cell->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
383
384 if( m_vAlignTop->IsChecked() )
385 cell->SetVertJustify( GR_TEXT_V_ALIGN_TOP );
386 else if( m_vAlignBottom->IsChecked() )
387 cell->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
388 else if( m_vAlignCenter->IsChecked() )
389 cell->SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
390
391 if( m_textColorBook->GetSelection() == 1 )
392 cell->SetTextColor( m_textColorSwatch->GetSwatchColor() );
393
394 if( m_fillColorBook->GetSelection() == 1 )
395 {
396 COLOR4D fillColor = m_fillColorSwatch->GetSwatchColor();
397
398 if( fillColor == COLOR4D::UNSPECIFIED )
399 {
400 cell->SetFillMode( FILL_T::NO_FILL );
401 }
402 else
403 {
404 cell->SetFillMode( FILL_T::FILLED_WITH_COLOR );
405 cell->SetFillColor( fillColor );
406 }
407 }
408
409 if( !m_marginLeft.IsIndeterminate() )
410 cell->SetMarginLeft( m_marginLeft.GetIntValue() );
411
412 if( !m_marginTop.IsIndeterminate() )
413 cell->SetMarginTop( m_marginTop.GetIntValue() );
414
415 if( !m_marginRight.IsIndeterminate() )
416 cell->SetMarginRight( m_marginRight.GetIntValue() );
417
418 if( !m_marginBottom.IsIndeterminate() )
419 cell->SetMarginBottom( m_marginBottom.GetIntValue() );
420 }
421
422 if( !commit.Empty() )
423 commit.Push( _( "Edit Table Cell Properties" ) );
424
426 return true;
427}
428
429
430void DIALOG_TABLECELL_PROPERTIES::onEditTable( wxCommandEvent& aEvent )
431{
433 {
435 Close();
436 }
437}
438
440{
442 m_scintillaTricks->CancelAutocomplete();
443
444 event.Skip();
445}
446
447
448void DIALOG_TABLECELL_PROPERTIES::OnFormattingHelp( wxHyperlinkEvent& aEvent )
449{
450 if( m_helpWindow )
451 {
452 m_helpWindow->Raise();
453 m_helpWindow->Show( true );
454 return;
455 }
456
458}
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