From 76b7cdd128a2897da1e8bc49f3db065b4d0020c3 Mon Sep 17 00:00:00 2001 From: Alex Shvartzkop Date: Sat, 13 Apr 2024 23:25:57 +0300 Subject: [PATCH] Improve grid cell editors appearance. --- common/dialogs/dialog_configure_paths.cpp | 1 + common/dialogs/panel_text_variables.cpp | 1 + common/validators.cpp | 29 ---------- common/widgets/grid_text_button_helpers.cpp | 23 +++----- common/widgets/grid_text_helpers.cpp | 63 ++++++++++++++++++++- common/widgets/wx_grid.cpp | 17 ++++++ include/validators.h | 17 ------ include/widgets/grid_text_helpers.h | 21 +++++++ include/widgets/wx_grid.h | 12 ++++ pcbnew/fp_text_grid_table.cpp | 3 +- 10 files changed, 124 insertions(+), 63 deletions(-) diff --git a/common/dialogs/dialog_configure_paths.cpp b/common/dialogs/dialog_configure_paths.cpp index bd5361a305..135f18e3c6 100644 --- a/common/dialogs/dialog_configure_paths.cpp +++ b/common/dialogs/dialog_configure_paths.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include diff --git a/common/dialogs/panel_text_variables.cpp b/common/dialogs/panel_text_variables.cpp index 0f81a88e20..c58eae4e82 100644 --- a/common/dialogs/panel_text_variables.cpp +++ b/common/dialogs/panel_text_variables.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include diff --git a/common/validators.cpp b/common/validators.cpp index 50f735269d..bb8d85dfba 100644 --- a/common/validators.cpp +++ b/common/validators.cpp @@ -39,35 +39,6 @@ #include #include -GRID_CELL_TEXT_EDITOR::GRID_CELL_TEXT_EDITOR() : wxGridCellTextEditor() -{ -} - - -void GRID_CELL_TEXT_EDITOR::SetValidator( const wxValidator& validator ) -{ - // keep our own copy because wxGridCellTextEditor's is annoyingly private - m_validator.reset( static_cast( validator.Clone() ) ); - - wxGridCellTextEditor::SetValidator( *m_validator ); -} - - -void GRID_CELL_TEXT_EDITOR::StartingKey( wxKeyEvent& event ) -{ - if( m_validator ) - { - m_validator.get()->SetWindow( Text() ); - m_validator.get()->ProcessEvent( event ); - } - - if( event.GetSkipped() ) - { - wxGridCellTextEditor::StartingKey( event ); - event.Skip( false ); - } -} - FOOTPRINT_NAME_VALIDATOR::FOOTPRINT_NAME_VALIDATOR( wxString* aValue ) : wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue ) diff --git a/common/widgets/grid_text_button_helpers.cpp b/common/widgets/grid_text_button_helpers.cpp index 7ae8f675f8..aa7af7c17d 100644 --- a/common/widgets/grid_text_button_helpers.cpp +++ b/common/widgets/grid_text_button_helpers.cpp @@ -41,16 +41,6 @@ #include -static void setTextMargins( wxTextEntryBase* aEntry ) -{ -#if defined( __WXMSW__ ) - aEntry->SetMargins( 2, 2 ); -#elif defined( __WXGTK__ ) - aEntry->SetMargins( 0, 0 ); -#endif -} - - //-------- Renderer --------------------------------------------------------------------- // None required; just render as normal text. @@ -69,7 +59,10 @@ wxString GRID_CELL_TEXT_BUTTON::GetValue() const void GRID_CELL_TEXT_BUTTON::SetSize( const wxRect& aRect ) { - Combo()->SetSize( aRect, wxSIZE_ALLOW_MINUS_ONE ); + wxRect rect( aRect ); + WX_GRID::CellEditorTransformSizeRect( rect ); + + wxGridCellEditor::SetSize( rect ); } @@ -227,7 +220,7 @@ void GRID_CELL_SYMBOL_ID_EDITOR::Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) { m_control = new TEXT_BUTTON_SYMBOL_CHOOSER( aParent, m_dlg, m_preselect ); - setTextMargins( Combo() ); + WX_GRID::CellEditorSetMargins( Combo() ); wxGridCellEditor::Create( aParent, aId, aEventHandler ); } @@ -295,7 +288,7 @@ void GRID_CELL_FPID_EDITOR::Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) { m_control = new TEXT_BUTTON_FP_CHOOSER( aParent, m_dlg, m_symbolNetlist, m_preselect ); - setTextMargins( Combo() ); + WX_GRID::CellEditorSetMargins( Combo() ); #if wxUSE_VALIDATORS // validate text in textctrl, if validator is set @@ -347,7 +340,7 @@ void GRID_CELL_URL_EDITOR::Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) { m_control = new TEXT_BUTTON_URL( aParent, m_dlg, m_searchStack ); - setTextMargins( Combo() ); + WX_GRID::CellEditorSetMargins( Combo() ); #if wxUSE_VALIDATORS // validate text in textctrl, if validator is set @@ -505,7 +498,7 @@ void GRID_CELL_PATH_EDITOR::Create( wxWindow* aParent, wxWindowID aId, m_control = new TEXT_BUTTON_FILE_BROWSER( aParent, m_dlg, m_grid, m_currentDir, m_fileFilter, m_normalize, m_normalizeBasePath ); - setTextMargins( Combo() ); + WX_GRID::CellEditorSetMargins( Combo() ); #if wxUSE_VALIDATORS // validate text in textctrl, if validator is set diff --git a/common/widgets/grid_text_helpers.cpp b/common/widgets/grid_text_helpers.cpp index d7963e98e5..f4cd1c1d70 100644 --- a/common/widgets/grid_text_helpers.cpp +++ b/common/widgets/grid_text_helpers.cpp @@ -21,9 +21,56 @@ #include #include #include +#include #include +//-------- GRID_CELL_TEXT_EDITOR ------------------------------------------------------ +// + +GRID_CELL_TEXT_EDITOR::GRID_CELL_TEXT_EDITOR() : wxGridCellTextEditor() +{ +} + + +void GRID_CELL_TEXT_EDITOR::SetValidator( const wxValidator& validator ) +{ + // keep our own copy because wxGridCellTextEditor's is annoyingly private + m_validator.reset( static_cast( validator.Clone() ) ); + + wxGridCellTextEditor::SetValidator( *m_validator ); +} + + +void GRID_CELL_TEXT_EDITOR::StartingKey( wxKeyEvent& event ) +{ + if( m_validator ) + { + m_validator.get()->SetWindow( Text() ); + m_validator.get()->ProcessEvent( event ); + } + + if( event.GetSkipped() ) + { + wxGridCellTextEditor::StartingKey( event ); + event.Skip( false ); + } +} + + +void GRID_CELL_TEXT_EDITOR::SetSize( const wxRect& aRect ) +{ + wxRect rect( aRect ); + WX_GRID::CellEditorTransformSizeRect( rect ); + +#if defined( __WXMSW__ ) + rect.Offset( 0, 1 ); +#endif + + wxGridCellEditor::SetSize( rect ); +} + + //-------- GRID_CELL_ESCAPED_TEXT_RENDERER ------------------------------------------------------ // @@ -69,9 +116,23 @@ GRID_CELL_STC_EDITOR::GRID_CELL_STC_EDITOR( { } +void GRID_CELL_STC_EDITOR::SetSize( const wxRect& aRect ) +{ + wxRect rect( aRect ); + WX_GRID::CellEditorTransformSizeRect( rect ); + +#if defined( __WXMSW__ ) + rect.Offset( -1, 1 ); +#endif + + wxGridCellEditor::SetSize( rect ); +} + + void GRID_CELL_STC_EDITOR::Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) { - m_control = new wxStyledTextCtrl( aParent ); + m_control = new wxStyledTextCtrl( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, + wxBORDER_NONE ); stc_ctrl()->SetTabIndents( false ); stc_ctrl()->SetBackSpaceUnIndents( false ); diff --git a/common/widgets/wx_grid.cpp b/common/widgets/wx_grid.cpp index fd249fbd61..bcca29f155 100644 --- a/common/widgets/wx_grid.cpp +++ b/common/widgets/wx_grid.cpp @@ -25,6 +25,8 @@ #include #include #include +#include // Needed for textentry.h on MSW +#include #include #include @@ -39,6 +41,21 @@ #define MIN_GRIDCELL_MARGIN FromDIP( 3 ) +void WX_GRID::CellEditorSetMargins( wxTextEntryBase* aEntry ) +{ + // This is consistent with wxGridCellTextEditor. But works differently across platforms or course. + aEntry->SetMargins( 0, 0 ); +} + + +void WX_GRID::CellEditorTransformSizeRect( wxRect& aRect ) +{ +#if defined( __WXMSW__ ) || defined( __WXGTK__ ) + aRect.Deflate( 2 ); +#endif +} + + wxColour getBorderColour() { KIGFX::COLOR4D bg = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK ); diff --git a/include/validators.h b/include/validators.h index 9e3b872a53..2b7b11b5b5 100644 --- a/include/validators.h +++ b/include/validators.h @@ -50,23 +50,6 @@ #define LABELUSERFIELD_V 200 -/** - * This class works around a bug in wxGrid where the first keystroke doesn't get sent through - * the validator if the editor wasn't already open. - */ -class GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor -{ -public: - GRID_CELL_TEXT_EDITOR(); - - virtual void SetValidator( const wxValidator& validator ) override; - virtual void StartingKey( wxKeyEvent& event ) override; - -protected: - std::unique_ptr m_validator; -}; - - /** * This class provides a custom wxValidator object for limiting the allowable characters when * defining footprint names. Since the introduction of the PRETTY footprint library format, diff --git a/include/widgets/grid_text_helpers.h b/include/widgets/grid_text_helpers.h index cd4edb2311..2813aee4da 100644 --- a/include/widgets/grid_text_helpers.h +++ b/include/widgets/grid_text_helpers.h @@ -29,6 +29,26 @@ class wxStyledTextCtrl; class wxStyledTextEvent; class SCINTILLA_TRICKS; + +/** + * This class works around a bug in wxGrid where the first keystroke doesn't get sent through + * the validator if the editor wasn't already open. + */ +class GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor +{ +public: + GRID_CELL_TEXT_EDITOR(); + + void SetSize( const wxRect& aRect ) override; + + virtual void SetValidator( const wxValidator& validator ) override; + virtual void StartingKey( wxKeyEvent& event ) override; + +protected: + std::unique_ptr m_validator; +}; + + /** * A text renderer that can unescape text for display * This is useful where it's desired to keep the underlying storage escaped. @@ -51,6 +71,7 @@ public: GRID_CELL_STC_EDITOR( bool aIgnoreCase, std::function onCharFn ); + void SetSize( const wxRect& aRect ) override; void Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) override; wxGridCellEditor* Clone() const override diff --git a/include/widgets/wx_grid.h b/include/widgets/wx_grid.h index 584f10e2b4..b79d448019 100644 --- a/include/widgets/wx_grid.h +++ b/include/widgets/wx_grid.h @@ -34,6 +34,8 @@ #include #include +class wxTextEntryBase; + class WX_GRID : public wxGrid { @@ -158,6 +160,16 @@ public: DeleteRows( 0, GetNumberRows() ); } + /** + * A helper function to set OS-specific margins for text-based cell editors. + */ + static void CellEditorSetMargins( wxTextEntryBase* aEntry ); + + /** + * A helper function to tweak sizes of text-based cell editors depending on OS. + */ + static void CellEditorTransformSizeRect( wxRect& aRect ); + protected: /** * A re-implementation of wxGrid::DrawColLabel which left-aligns the first column and draws diff --git a/pcbnew/fp_text_grid_table.cpp b/pcbnew/fp_text_grid_table.cpp index 4ffeb0105b..dc7feaf46f 100644 --- a/pcbnew/fp_text_grid_table.cpp +++ b/pcbnew/fp_text_grid_table.cpp @@ -30,7 +30,8 @@ #include #include #include "grid_layer_box_helpers.h" -#include "widgets/grid_text_button_helpers.h" +#include +#include enum {