Ensure good fonts in grid headers.

Also moves rendering to a flat look for headers (instead of a beveled
border).

Fixes https://gitlab.com/kicad/code/kicad/issues/13547
This commit is contained in:
Jeff Young 2023-01-16 02:46:09 +00:00
parent 789db0cecb
commit 62649b868c
17 changed files with 185 additions and 44 deletions

View File

@ -38,7 +38,7 @@ DIALOG_CONFIGURE_PATHS_BASE::DIALOG_CONFIGURE_PATHS_BASE( wxWindow* parent, wxWi
m_EnvVars->EnableDragColSize( true );
m_EnvVars->SetColLabelValue( 0, _("Name") );
m_EnvVars->SetColLabelValue( 1, _("Path") );
m_EnvVars->SetColLabelSize( 22 );
m_EnvVars->SetColLabelSize( wxGRID_AUTOSIZE );
m_EnvVars->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
// Rows

View File

@ -101,7 +101,7 @@
<property name="center_pane">0</property>
<property name="close_button">1</property>
<property name="col_label_horiz_alignment">wxALIGN_CENTER</property>
<property name="col_label_size">22</property>
<property name="col_label_size">wxGRID_AUTOSIZE</property>
<property name="col_label_values">&quot;Name&quot; &quot;Path&quot;</property>
<property name="col_label_vert_alignment">wxALIGN_CENTER</property>
<property name="cols">2</property>

View File

@ -309,7 +309,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>
@ -788,7 +788,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>

View File

@ -120,7 +120,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>

View File

@ -23,14 +23,77 @@
#include <wx/tokenzr.h>
#include <wx/dc.h>
#include <wx/settings.h>
#include <widgets/wx_grid.h>
#include <widgets/ui_common.h>
#include <algorithm>
#include <core/kicad_algo.h>
#include <gal/color4d.h>
#define MIN_GRIDCELL_MARGIN 3
wxColour getBorderColour()
{
KIGFX::COLOR4D bg = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
KIGFX::COLOR4D fg = wxSystemSettings::GetColour( wxSYS_COLOUR_ACTIVEBORDER );
KIGFX::COLOR4D border = fg.Mix( bg, 0.50 );
return border.ToColour();
}
class WX_GRID_CORNER_HEADER_RENDERER : public wxGridCornerHeaderRendererDefault
{
public:
void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
{
wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
rect.SetTop( rect.GetTop() + 1 );
rect.SetLeft( rect.GetLeft() + 1 );
rect.SetBottom( rect.GetBottom() - 1 );
rect.SetRight( rect.GetRight() - 1 );
dc.DrawRectangle( rect );
}
};
class WX_GRID_COLUMN_HEADER_RENDERER : public wxGridColumnHeaderRendererDefault
{
public:
void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
{
wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
rect.SetTop( rect.GetTop() + 1 );
rect.SetLeft( rect.GetLeft() );
rect.SetBottom( rect.GetBottom() - 1 );
rect.SetRight( rect.GetRight() - 1 );
dc.DrawRectangle( rect );
}
};
class WX_GRID_ROW_HEADER_RENDERER : public wxGridRowHeaderRendererDefault
{
public:
void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
{
wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
rect.SetTop( rect.GetTop() + 1 );
rect.SetLeft( rect.GetLeft() + 1 );
rect.SetBottom( rect.GetBottom() - 1 );
rect.SetRight( rect.GetRight() );
dc.DrawRectangle( rect );
}
};
WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
long style, const wxString& name ) :
wxGrid( parent, id, pos, size, style, name ),
@ -38,9 +101,12 @@ WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxS
{
SetDefaultCellOverflow( false );
// Make sure the GUI font scales properly on GTK
// Make sure the GUI font scales properly
SetDefaultCellFont( KIUI::GetControlFont( this ) );
if( GetColLabelSize() > 0 )
SetColLabelSize( GetColLabelSize() + 4 );
#if wxCHECK_VERSION( 3, 1, 3 )
Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
#endif
@ -81,7 +147,7 @@ void WX_GRID::SetColLabelSize( int aHeight )
return;
}
wxFont headingFont = KIUI::GetControlFont( this ).Bold();
wxFont headingFont = KIUI::GetControlFont( this );
// Make sure the GUI font scales properly on GTK
SetLabelFont( headingFont );
@ -236,30 +302,63 @@ void WX_GRID::ShowHideColumns( const wxString& shownColumns )
shownTokens.GetNextToken().ToLong( &colNumber );
if( colNumber >= 0 && colNumber < GetNumberCols() )
ShowCol( colNumber );
ShowCol( (int) colNumber );
}
}
void WX_GRID::DrawCornerLabel( wxDC& dc )
{
if( m_nativeColumnLabels )
wxGrid::DrawCornerLabel( dc );
wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
static WX_GRID_CORNER_HEADER_RENDERER rend;
// It is reported that we need to erase the background to avoid display
// artifacts, see #12055.
{
// wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
#if wxCHECK_VERSION( 3, 1, 3 )
wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
#else
wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
wxDCPenChanger setPen( dc, m_colWindow->GetBackgroundColour() );
#endif
dc.DrawRectangle( rect.Inflate( 1 ) );
}
rend.DrawBorder( *this, dc, rect );
}
void WX_GRID::DrawColLabel( wxDC& dc, int col )
{
if( m_nativeColumnLabels )
wxGrid::DrawColLabel( dc, col );
if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
return;
int colLeft = GetColLeft( col );
wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
wxRect rect( colLeft, 0, GetColWidth( col ), m_colLabelHeight );
static wxGridColumnHeaderRendererDefault rend;
static WX_GRID_COLUMN_HEADER_RENDERER rend;
// It is reported that we need to erase the background to avoid display
// artifacts, see #12055.
// wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
{
// wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
#if wxCHECK_VERSION( 3, 1, 3 )
wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
#else
wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
wxDCPenChanger setPen( dc, m_colWindow->GetBackgroundColour() );
#endif
dc.DrawRectangle(rect);
dc.DrawRectangle( rect.Inflate( 1 ) );
}
rend.DrawBorder( *this, dc, rect );
@ -270,13 +369,54 @@ void WX_GRID::DrawColLabel( wxDC& dc, int col )
GetColLabelAlignment( &hAlign, &vAlign );
const int orient = GetColLabelTextOrientation();
if( col == 0 && GetRowLabelSize() == 0 )
if( col == 0 )
hAlign = wxALIGN_LEFT;
if( hAlign == wxALIGN_LEFT )
rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
}
void WX_GRID::DrawRowLabel( wxDC& dc, int row )
{
if ( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
return;
wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
static WX_GRID_ROW_HEADER_RENDERER rend;
// It is reported that we need to erase the background to avoid display
// artifacts, see #12055.
{
// wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
#if wxCHECK_VERSION( 3, 1, 3 )
wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
#else
wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
wxDCPenChanger setPen( dc, m_colWindow->GetBackgroundColour() );
#endif
dc.DrawRectangle( rect.Inflate( 1 ) );
}
rend.DrawBorder( *this, dc, rect );
// Make sure fonts get scaled correctly on GTK HiDPI monitors
dc.SetFont( GetLabelFont() );
int hAlign, vAlign;
GetRowLabelAlignment(&hAlign, &vAlign);
if( hAlign == wxALIGN_LEFT )
rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
rend.DrawLabel( *this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
}
bool WX_GRID::CommitPendingChanges( bool aQuietMode )
{
if( !IsCellEditControlEnabled() )
@ -383,11 +523,8 @@ int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep
if( aKeep )
size = GetRowLabelSize();
// The 1.1 scale factor is due to the fact row labels use a bold font, bigger than
// the normal font.
// TODO: use a better way to evaluate the text size, for bold font
for( int row = 0; aContents && row < GetNumberRows(); row++ )
size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + "M" ).x * 1.1 ) );
size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + "M" ).x ) );
}
else
{
@ -400,9 +537,7 @@ int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep
{
EnsureColLabelsVisible();
// The 1.1 scale factor is due to the fact headers use a bold font, bigger than
// the normal font.
size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + "M" ).x * 1.1 ) );
size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + "M" ).x ) );
}
for( int row = 0; aContents && row < GetNumberRows(); row++ )
@ -421,10 +556,7 @@ int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep
void WX_GRID::EnsureColLabelsVisible()
{
// The 1.1 scale factor is due to the fact row labels use a bold font, bigger than
// the normal font
// TODO: use a better way to evaluate the text size, for bold font
int line_height = int( GetTextExtent( "Mj" ).y * 1.1 ) + 3;
int line_height = int( GetTextExtent( "Mj" ).y ) + 3;
int row_height = GetColLabelSize();
int initial_row_height = row_height;

View File

@ -200,7 +200,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>
@ -725,7 +725,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>

View File

@ -51,7 +51,6 @@ PANEL_TEMPLATE_FIELDNAMES_BASE::PANEL_TEMPLATE_FIELDNAMES_BASE( wxWindow* parent
m_grid->SetRowLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
// Label Appearance
m_grid->SetLabelFont( wxFont( 12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, wxEmptyString ) );
// Cell Defaults
m_grid->SetDefaultCellAlignment( wxALIGN_LEFT, wxALIGN_TOP );

View File

@ -181,7 +181,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>

View File

@ -138,11 +138,21 @@ public:
protected:
/**
* A re-implementation of wxGrid::DrawColLabel which left-aligns the first column when
* there are no row labels.
* A re-implementation of wxGrid::DrawColLabel which left-aligns the first column and draws
* flat borders.
*/
void DrawColLabel( wxDC& dc, int col ) override;
/**
* A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
*/
void DrawRowLabel( wxDC& dc, int row ) override;
/**
* A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
*/
void DrawCornerLabel( wxDC& dc ) override;
void onGridColMove( wxGridEvent& aEvent );
void onGridCellSelect( wxGridEvent& aEvent );
void onCellEditorShown( wxGridEvent& aEvent );

View File

@ -61,7 +61,7 @@ DIALOG_FOOTPRINT_PROPERTIES_BASE::DIALOG_FOOTPRINT_PROPERTIES_BASE( wxWindow* pa
m_itemsGrid->SetColLabelValue( 8, _("Keep Upright") );
m_itemsGrid->SetColLabelValue( 9, _("X Offset") );
m_itemsGrid->SetColLabelValue( 10, _("Y Offset") );
m_itemsGrid->SetColLabelSize( 24 );
m_itemsGrid->SetColLabelSize( wxGRID_AUTOSIZE );
m_itemsGrid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
// Rows

View File

@ -220,7 +220,7 @@
<property name="center_pane">0</property>
<property name="close_button">1</property>
<property name="col_label_horiz_alignment">wxALIGN_CENTER</property>
<property name="col_label_size">24</property>
<property name="col_label_size">wxGRID_AUTOSIZE</property>
<property name="col_label_values">&quot;Text Items&quot; &quot;Show&quot; &quot;Width&quot; &quot;Height&quot; &quot;Thickness&quot; &quot;Italic&quot; &quot;Layer&quot; &quot;Orientation&quot; &quot;Keep Upright&quot; &quot;X Offset&quot; &quot;Y Offset&quot;</property>
<property name="col_label_vert_alignment">wxALIGN_CENTER</property>
<property name="cols">11</property>

View File

@ -61,7 +61,7 @@ DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR_BASE::DIALOG_FOOTPRINT_PROPERTIES_FP_EDITO
m_itemsGrid->SetColLabelValue( 8, _("Unconstrained") );
m_itemsGrid->SetColLabelValue( 9, _("X Offset") );
m_itemsGrid->SetColLabelValue( 10, _("Y Offset") );
m_itemsGrid->SetColLabelSize( 24 );
m_itemsGrid->SetColLabelSize( wxGRID_AUTOSIZE );
m_itemsGrid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
// Rows

View File

@ -219,7 +219,7 @@
<property name="center_pane">0</property>
<property name="close_button">1</property>
<property name="col_label_horiz_alignment">wxALIGN_CENTER</property>
<property name="col_label_size">24</property>
<property name="col_label_size">wxGRID_AUTOSIZE</property>
<property name="col_label_values">&quot;Text Items&quot; &quot;Show&quot; &quot;Width&quot; &quot;Height&quot; &quot;Thickness&quot; &quot;Italic&quot; &quot;Layer&quot; &quot;Orientation&quot; &quot;Unconstrained&quot; &quot;X Offset&quot; &quot;Y Offset&quot;</property>
<property name="col_label_vert_alignment">wxALIGN_CENTER</property>
<property name="cols">11</property>

View File

@ -199,7 +199,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>
@ -620,7 +620,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>

View File

@ -119,7 +119,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>

View File

@ -190,7 +190,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>

View File

@ -180,7 +180,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>
@ -583,7 +583,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>
@ -986,7 +986,7 @@
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label_bg"></property>
<property name="label_font">,90,400,12,70,0</property>
<property name="label_font"></property>
<property name="label_text"></property>
<property name="margin_height">0</property>
<property name="margin_width">0</property>