KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_text_helpers.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 (C) 2020-2023 KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <string_utils.h>
22#include <wx/stc/stc.h>
24#include <scintilla_tricks.h>
25
26
27//-------- GRID_CELL_ESCAPED_TEXT_RENDERER ------------------------------------------------------
28//
29
31 wxGridCellStringRenderer()
32{
33}
34
35void GRID_CELL_ESCAPED_TEXT_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
36 const wxRect& aRect, int aRow, int aCol,
37 bool isSelected )
38{
39 wxString unescaped = UnescapeString( aGrid.GetCellValue( aRow, aCol ) );
40
41 wxRect rect = aRect;
42 rect.Inflate( -1 );
43
44 // erase background
45 wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
46
47 SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
48 aGrid.DrawTextRectangle( aDC, unescaped, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
49}
50
51
52wxSize GRID_CELL_ESCAPED_TEXT_RENDERER::GetBestSize( wxGrid & aGrid, wxGridCellAttr & aAttr,
53 wxDC & aDC, int aRow, int aCol )
54{
55 wxString unescaped = UnescapeString( aGrid.GetCellValue( aRow, aCol ) );
56 return wxGridCellStringRenderer::DoGetBestSize( aAttr, aDC, unescaped );
57}
58
59
60//-------- GRID_CELL_STC_EDITOR -----------------------------------------------------------------
61//
62
64 std::function<void( wxStyledTextEvent&,
65 SCINTILLA_TRICKS* )> aOnChar ) :
66 m_scintillaTricks( nullptr ),
67 m_ignoreCase( aIgnoreCase ),
68 m_onChar( aOnChar )
69{ }
70
71
72void GRID_CELL_STC_EDITOR::Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler )
73{
74 m_control = new wxStyledTextCtrl( aParent );
75
76 stc_ctrl()->SetTabIndents( false );
77 stc_ctrl()->SetBackSpaceUnIndents( false );
78 stc_ctrl()->SetViewEOL( false );
79 stc_ctrl()->SetViewWhiteSpace( false );
80 stc_ctrl()->SetIndentationGuides( false );
81 stc_ctrl()->SetMarginWidth( 0, 0 ); // Symbol margin
82 stc_ctrl()->SetMarginWidth( 1, 0 ); // Line-number margin
83 stc_ctrl()->SetEOLMode( wxSTC_EOL_LF );
84 stc_ctrl()->AutoCompSetMaxWidth( 25 );
85 stc_ctrl()->AutoCompSetIgnoreCase( m_ignoreCase );
86 stc_ctrl()->UsePopUp( 0 );
87
88 // A hack which causes Scintilla to auto-size the text editor canvas
89 // See: https://github.com/jacobslusser/ScintillaNET/issues/216
90 stc_ctrl()->SetScrollWidth( 1 );
91 stc_ctrl()->SetScrollWidthTracking( true );
92
94 stc_ctrl(), wxEmptyString, true,
95 // onAccept handler
96 [this]( wxKeyEvent& aEvent )
97 {
98 HandleReturn( aEvent );
99 },
100 // onCharAdded handler
101 [this]( wxStyledTextEvent& aEvent )
102 {
103 m_onChar( aEvent, m_scintillaTricks );
104 } );
105
106 stc_ctrl()->Bind( wxEVT_KILL_FOCUS, &GRID_CELL_STC_EDITOR::onFocusLoss, this );
107
108 wxGridCellEditor::Create( aParent, aId, aEventHandler );
109}
110
111
112wxStyledTextCtrl* GRID_CELL_STC_EDITOR::stc_ctrl() const
113{
114 return static_cast<wxStyledTextCtrl*>( m_control );
115}
116
117
119{
120 return stc_ctrl()->GetText();
121}
122
123
124void GRID_CELL_STC_EDITOR::Show( bool aShow, wxGridCellAttr* aAttr )
125{
126 if( !aShow )
127 stc_ctrl()->AutoCompCancel();
128
129 wxGridCellEditor::Show( aShow, aAttr );
130}
131
132
133void GRID_CELL_STC_EDITOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
134{
135 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler()
136 );
137
138 // Don't immediately end if we get a kill focus event within BeginEdit
139 evtHandler->SetInSetFocus( true );
140
141 m_value = aGrid->GetTable()->GetValue( aRow, aCol );
142
143 stc_ctrl()->SetFocus();
144 stc_ctrl()->SetText( m_value );
145 stc_ctrl()->SelectAll();
146}
147
148
149bool GRID_CELL_STC_EDITOR::EndEdit( int, int, const wxGrid*, const wxString&, wxString *aNewVal )
150{
151 const wxString value = stc_ctrl()->GetText();
152
153 if( value == m_value )
154 return false;
155
156 m_value = value;
157
158 if( aNewVal )
159 *aNewVal = value;
160
161 return true;
162}
163
164
165void GRID_CELL_STC_EDITOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
166{
167 aGrid->GetTable()->SetValue( aRow, aCol, m_value );
168}
169
170
171void GRID_CELL_STC_EDITOR::onFocusLoss( wxFocusEvent& aEvent )
172{
173 if( stc_ctrl() )
174 stc_ctrl()->AutoCompCancel();
175
176 aEvent.Skip();
177}
wxSize GetBestSize(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col) override
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, const wxRect &aRect, int aRow, int aCol, bool isSelected) override
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
void onFocusLoss(wxFocusEvent &aEvent)
GRID_CELL_STC_EDITOR(bool aIgnoreCase, std::function< void(wxStyledTextEvent &, SCINTILLA_TRICKS *)> aOnChar)
bool EndEdit(int aRow, int aCol, const wxGrid *, const wxString &, wxString *aNewVal) override
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
wxString GetValue() const override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
void Show(bool aShow, wxGridCellAttr *aAttr=nullptr) override
wxStyledTextCtrl * stc_ctrl() const
std::function< void(wxStyledTextEvent &, SCINTILLA_TRICKS *)> m_onChar
SCINTILLA_TRICKS * m_scintillaTricks
Add cut/copy/paste, dark theme, autocomplete and brace highlighting to a wxStyleTextCtrl instance.
wxString UnescapeString(const wxString &aSource)