KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_text_helpers.h
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 * @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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <kicommon.h>
24#include <functional>
25#include <memory>
26#include <wx/generic/gridctrl.h>
27#include <wx/combo.h>
28
29class wxGrid;
30class wxStyledTextCtrl;
31class wxStyledTextEvent;
33
34
39class KICOMMON_API GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor
40{
41public:
43
44 void SetSize( const wxRect& aRect ) override;
45
46 virtual void SetValidator( const wxValidator& validator ) override;
47 virtual void StartingKey( wxKeyEvent& event ) override;
48
49protected:
50 std::unique_ptr<wxValidator> m_validator;
51};
52
53
58class KICOMMON_API GRID_CELL_ESCAPED_TEXT_RENDERER : public wxGridCellStringRenderer
59{
60public:
62
63 void Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC, const wxRect& aRect, int aRow,
64 int aCol, bool isSelected ) override;
65
66 wxSize GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, int row, int col ) override;
67};
68
69
70class KICOMMON_API GRID_CELL_STC_EDITOR : public wxGridCellEditor
71{
72public:
73 GRID_CELL_STC_EDITOR( bool aIgnoreCase, bool aSingleLine,
74 std::function<void( wxStyledTextEvent&, SCINTILLA_TRICKS* )> onCharFn );
75
76 void SetSize( const wxRect& aRect ) override;
77 void Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) override;
78
79 wxGridCellEditor* Clone() const override
80 {
82 }
83
84 wxString GetValue() const override;
85
86 void StartingKey( wxKeyEvent& event ) override;
87 void Show( bool aShow, wxGridCellAttr *aAttr = nullptr ) override;
88 void BeginEdit( int aRow, int aCol, wxGrid* aGrid ) override;
89 bool EndEdit( int aRow, int aCol, const wxGrid*, const wxString&, wxString* aNewVal ) override;
90 void ApplyEdit( int aRow, int aCol, wxGrid* aGrid ) override;
91 void Reset() override {}
92
93protected:
94 void onFocusLoss( wxFocusEvent& aEvent );
95
96 wxStyledTextCtrl* stc_ctrl() const;
97
98protected:
102 wxString m_value;
103
104 std::function<void( wxStyledTextEvent&, SCINTILLA_TRICKS* )> m_onCharFn;
105};
106
107
108class KICOMMON_API GRID_CELL_TEXT_BUTTON : public wxGridCellEditor
109{
110public:
112 m_grid( nullptr ),
113 m_row( -1 ),
114 m_col( -1 )
115 {};
116
117 wxString GetValue() const override;
118
119 void SetSize( const wxRect& aRect ) override;
120
121 void StartingKey( wxKeyEvent& event ) override;
122 void BeginEdit( int aRow, int aCol, wxGrid* aGrid ) override;
123 bool EndEdit( int , int , const wxGrid* , const wxString& , wxString *aNewVal ) override;
124 void ApplyEdit( int aRow, int aCol, wxGrid* aGrid ) override;
125 void Reset() override;
126
127 void OnTextChange( wxCommandEvent& event );
128
129#if wxUSE_VALIDATORS
130 void SetValidator( const wxValidator& validator );
131#endif
132
133protected:
134 wxComboCtrl* Combo() const { return static_cast<wxComboCtrl*>( m_control ); }
135
136protected:
137#if wxUSE_VALIDATORS
138 std::unique_ptr< wxValidator > m_validator;
139#endif
140
141 wxGrid* m_grid;
142 int m_row;
143 int m_col;
144 wxString m_value;
145
147};
148
149
151{
152public:
154 m_isNullable( true )
155 {
156 }
157 GRID_CELL_NULLABLE_INTERFACE( bool aIsNullable ) :
158 m_isNullable( aIsNullable )
159 {
160 }
161 virtual ~GRID_CELL_NULLABLE_INTERFACE() = default;
162
163 virtual bool IsNullable() const { return m_isNullable; }
164
165protected:
166 bool m_isNullable{ false };
167};
168
169
170//---- Grid helpers: custom wxGridCellTextEditor ------------------------------------------
171//
172// Note: This is used to mark WX_GRID cell as nullable
174{
175public:
180 GRID_CELL_MARK_AS_NULLABLE( const bool aIsNullable ) :
181 GRID_CELL_NULLABLE_INTERFACE( aIsNullable )
182 {
183 }
184
185 wxGridCellEditor* Clone() const override { return new GRID_CELL_MARK_AS_NULLABLE( IsNullable() ); }
186
187 void Reset() override {}
188
189
191};
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
wxGridCellEditor * Clone() const override
GRID_CELL_MARK_AS_NULLABLE(const bool aIsNullable)
wxDECLARE_NO_COPY_CLASS(GRID_CELL_MARK_AS_NULLABLE)
virtual ~GRID_CELL_NULLABLE_INTERFACE()=default
GRID_CELL_NULLABLE_INTERFACE(bool aIsNullable)
virtual bool IsNullable() const
void SetSize(const wxRect &aRect) override
std::function< void(wxStyledTextEvent &, SCINTILLA_TRICKS *)> m_onCharFn
GRID_CELL_STC_EDITOR(bool aIgnoreCase, bool aSingleLine, std::function< void(wxStyledTextEvent &, SCINTILLA_TRICKS *)> onCharFn)
wxGridCellEditor * Clone() const override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
SCINTILLA_TRICKS * m_scintillaTricks
wxDECLARE_NO_COPY_CLASS(GRID_CELL_TEXT_BUTTON)
wxComboCtrl * Combo() const
std::unique_ptr< wxValidator > m_validator
void SetSize(const wxRect &aRect) override
virtual void StartingKey(wxKeyEvent &event) override
virtual void SetValidator(const wxValidator &validator) override
Add cut/copy/paste, dark theme, autocomplete and brace highlighting to a wxStyleTextCtrl instance.
void Reset() override
#define KICOMMON_API
Definition kicommon.h:27
JSON_SCHEMA_VALIDATOR validator(schema)