2018-05-30 10:52:19 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-07-15 19:26:35 +00:00
|
|
|
* Copyright (C) 2018-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-05-30 10:52:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <wx/tokenzr.h>
|
|
|
|
#include <wx/dc.h>
|
2020-12-17 13:12:18 +00:00
|
|
|
#include <widgets/wx_grid.h>
|
2021-09-10 14:59:27 +00:00
|
|
|
#include <widgets/ui_common.h>
|
2020-05-08 14:34:28 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-05-30 10:52:19 +00:00
|
|
|
|
2018-08-23 10:59:37 +00:00
|
|
|
#define MIN_GRIDCELL_MARGIN 3
|
|
|
|
|
|
|
|
|
2018-08-19 16:10:14 +00:00
|
|
|
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 ),
|
|
|
|
m_weOwnTable( false )
|
2021-08-01 10:57:15 +00:00
|
|
|
{
|
|
|
|
SetDefaultCellOverflow( false );
|
2021-09-08 21:30:21 +00:00
|
|
|
|
|
|
|
// Make sure the GUI font scales properly on GTK
|
2021-09-11 19:07:33 +00:00
|
|
|
SetDefaultCellFont( KIUI::GetControlFont( this ) );
|
2022-03-21 23:44:28 +00:00
|
|
|
|
|
|
|
#if wxCHECK_VERSION( 3, 1, 0 )
|
|
|
|
Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
|
|
|
|
#endif
|
|
|
|
|
2021-08-01 10:57:15 +00:00
|
|
|
}
|
2018-08-19 16:10:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
WX_GRID::~WX_GRID()
|
|
|
|
{
|
|
|
|
if( m_weOwnTable )
|
|
|
|
DestroyTable( GetTable() );
|
2022-03-21 23:44:28 +00:00
|
|
|
|
|
|
|
#if wxCHECK_VERSION( 3, 1, 0 )
|
|
|
|
Disconnect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
|
|
|
|
#endif
|
|
|
|
|
2018-08-19 16:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-21 23:44:28 +00:00
|
|
|
#if wxCHECK_VERSION( 3, 1, 0 )
|
|
|
|
void WX_GRID::onDPIChanged(wxDPIChangedEvent& aEvt)
|
|
|
|
{
|
|
|
|
/// This terrible hack is a way to avoid the incredibly disruptive resizing of grids that happens on Macs
|
|
|
|
/// when moving a window between monitors of different DPIs.
|
|
|
|
#ifndef __WXMAC__
|
|
|
|
aEvt.Skip();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-02-07 21:48:41 +00:00
|
|
|
void WX_GRID::SetColLabelSize( int aHeight )
|
|
|
|
{
|
2020-09-04 17:42:04 +00:00
|
|
|
if( aHeight == 0 )
|
|
|
|
{
|
|
|
|
wxGrid::SetColLabelSize( 0 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-12 11:17:22 +00:00
|
|
|
wxFont headingFont = KIUI::GetControlFont( this ).Bold();
|
|
|
|
|
2021-09-10 17:35:45 +00:00
|
|
|
// Make sure the GUI font scales properly on GTK
|
|
|
|
SetLabelFont( headingFont );
|
2019-02-07 21:48:41 +00:00
|
|
|
|
2021-09-10 17:35:45 +00:00
|
|
|
// Correct wxFormBuilder height for large fonts
|
|
|
|
int minHeight = headingFont.GetPixelSize().y + 2 * MIN_GRIDCELL_MARGIN;
|
2019-02-07 21:48:41 +00:00
|
|
|
wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-19 16:10:14 +00:00
|
|
|
void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
|
2018-05-30 10:52:19 +00:00
|
|
|
{
|
|
|
|
// wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
|
|
|
|
// and restore them.
|
2018-09-12 08:53:16 +00:00
|
|
|
int numberCols = GetNumberCols();
|
|
|
|
int* formBuilderColWidths = new int[numberCols];
|
2018-05-30 10:52:19 +00:00
|
|
|
|
2018-09-12 08:53:16 +00:00
|
|
|
for( int i = 0; i < numberCols; ++i )
|
2018-05-30 10:52:19 +00:00
|
|
|
formBuilderColWidths[ i ] = GetColSize( i );
|
|
|
|
|
|
|
|
wxGrid::SetTable( aTable );
|
2020-12-17 13:12:18 +00:00
|
|
|
|
2019-02-07 21:48:41 +00:00
|
|
|
// wxGrid::SetTable() may change the number of columns, so prevent out-of-bounds access
|
|
|
|
// to formBuilderColWidths
|
2018-09-12 08:53:16 +00:00
|
|
|
numberCols = std::min( numberCols, GetNumberCols() );
|
2018-05-30 10:52:19 +00:00
|
|
|
|
2018-09-12 08:53:16 +00:00
|
|
|
for( int i = 0; i < numberCols; ++i )
|
2018-08-23 10:59:37 +00:00
|
|
|
{
|
|
|
|
// correct wxFormBuilder width for large fonts and/or long translations
|
|
|
|
int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
|
|
|
|
|
|
|
|
SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
|
|
|
|
}
|
2018-07-17 06:53:24 +00:00
|
|
|
|
|
|
|
delete[] formBuilderColWidths;
|
2018-08-19 16:10:14 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
|
2022-02-04 11:36:27 +00:00
|
|
|
Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
|
2018-10-09 20:27:15 +00:00
|
|
|
|
2018-08-19 16:10:14 +00:00
|
|
|
m_weOwnTable = aTakeOwnership;
|
2018-05-30 10:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-04 11:36:27 +00:00
|
|
|
void WX_GRID::onGridCellSelect( wxGridEvent& aEvent )
|
|
|
|
{
|
|
|
|
// Highlight the selected cell.
|
|
|
|
// Calling SelectBlock() allows a visual effect when cells are selected
|
|
|
|
// by tab or arrow keys.
|
|
|
|
// Otherwise, one cannot really know what actual cell is selected
|
|
|
|
int row = aEvent.GetRow();
|
|
|
|
int col = aEvent.GetCol();
|
|
|
|
|
|
|
|
if( row >= 0 && col >= 0 )
|
|
|
|
SelectBlock(row,col,row,col,false);
|
|
|
|
}
|
|
|
|
|
2018-05-30 10:52:19 +00:00
|
|
|
void WX_GRID::DestroyTable( wxGridTableBase* aTable )
|
|
|
|
{
|
|
|
|
// wxGrid's destructor will crash trying to look up the cell attr if the edit control
|
|
|
|
// is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
|
2018-08-19 16:10:14 +00:00
|
|
|
CommitPendingChanges( true /* quiet mode */ );
|
2018-05-30 10:52:19 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
|
2022-02-04 11:36:27 +00:00
|
|
|
Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
|
2018-10-09 20:27:15 +00:00
|
|
|
|
2018-05-30 10:52:19 +00:00
|
|
|
wxGrid::SetTable( nullptr );
|
|
|
|
delete aTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString WX_GRID::GetShownColumns()
|
|
|
|
{
|
|
|
|
wxString shownColumns;
|
|
|
|
|
|
|
|
for( int i = 0; i < GetNumberCols(); ++i )
|
|
|
|
{
|
|
|
|
if( IsColShown( i ) )
|
|
|
|
{
|
|
|
|
if( shownColumns.Length() )
|
|
|
|
shownColumns << wxT( " " );
|
2020-12-17 13:12:18 +00:00
|
|
|
|
2018-05-30 10:52:19 +00:00
|
|
|
shownColumns << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return shownColumns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::ShowHideColumns( const wxString& shownColumns )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < GetNumberCols(); ++i )
|
|
|
|
HideCol( i );
|
|
|
|
|
|
|
|
wxStringTokenizer shownTokens( shownColumns );
|
|
|
|
|
|
|
|
while( shownTokens.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
long colNumber;
|
|
|
|
shownTokens.GetNextToken().ToLong( &colNumber );
|
|
|
|
|
|
|
|
if( colNumber >= 0 && colNumber < GetNumberCols() )
|
|
|
|
ShowCol( colNumber );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::DrawColLabel( wxDC& dc, int col )
|
|
|
|
{
|
|
|
|
if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
int colLeft = GetColLeft( col );
|
|
|
|
|
|
|
|
wxRect rect( colLeft, 0, GetColWidth( col ), m_colLabelHeight );
|
|
|
|
static wxGridColumnHeaderRendererDefault rend;
|
|
|
|
|
|
|
|
// It is reported that we need to erase the background to avoid display
|
2021-07-15 19:26:35 +00:00
|
|
|
// artifacts, see #12055.
|
2019-08-29 10:04:18 +00:00
|
|
|
// wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
|
|
|
|
#if wxCHECK_VERSION( 3, 1, 3 )
|
|
|
|
wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
|
|
|
|
#else
|
2018-05-30 10:52:19 +00:00
|
|
|
wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
|
2019-08-29 10:04:18 +00:00
|
|
|
#endif
|
2018-05-30 10:52:19 +00:00
|
|
|
dc.DrawRectangle(rect);
|
|
|
|
|
|
|
|
rend.DrawBorder( *this, dc, rect );
|
|
|
|
|
2021-09-12 11:17:22 +00:00
|
|
|
// Make sure fonts get scaled correctly on GTK HiDPI monitors
|
|
|
|
dc.SetFont( GetLabelFont() );
|
|
|
|
|
2018-05-30 10:52:19 +00:00
|
|
|
int hAlign, vAlign;
|
|
|
|
GetColLabelAlignment( &hAlign, &vAlign );
|
|
|
|
const int orient = GetColLabelTextOrientation();
|
|
|
|
|
2018-04-28 15:22:25 +00:00
|
|
|
if( col == 0 && GetRowLabelSize() == 0 )
|
2018-05-30 10:52:19 +00:00
|
|
|
hAlign = wxALIGN_LEFT;
|
|
|
|
|
|
|
|
rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
|
|
|
|
}
|
2018-08-13 17:00:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool WX_GRID::CommitPendingChanges( bool aQuietMode )
|
|
|
|
{
|
|
|
|
if( !IsCellEditControlEnabled() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
HideCellEditControl();
|
|
|
|
|
|
|
|
// do it after HideCellEditControl()
|
|
|
|
m_cellEditCtrlEnabled = false;
|
|
|
|
|
|
|
|
int row = m_currentCellCoords.GetRow();
|
|
|
|
int col = m_currentCellCoords.GetCol();
|
|
|
|
|
|
|
|
wxString oldval = GetCellValue( row, col );
|
|
|
|
wxString newval;
|
|
|
|
|
|
|
|
wxGridCellAttr* attr = GetCellAttr( row, col );
|
|
|
|
wxGridCellEditor* editor = attr->GetEditor( this, row, col );
|
|
|
|
|
|
|
|
bool changed = editor->EndEdit( row, col, this, oldval, &newval );
|
|
|
|
|
|
|
|
editor->DecRef();
|
|
|
|
attr->DecRef();
|
|
|
|
|
|
|
|
if( changed )
|
|
|
|
{
|
2021-07-15 19:26:35 +00:00
|
|
|
if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
|
2018-08-13 17:00:08 +00:00
|
|
|
return false;
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
editor->ApplyEdit( row, col, this );
|
2018-08-13 17:00:08 +00:00
|
|
|
|
|
|
|
// for compatibility reasons dating back to wx 2.8 when this event
|
|
|
|
// was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
|
|
|
|
// didn't exist we allow vetoing this one too
|
|
|
|
if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
|
|
|
|
{
|
|
|
|
// Event has been vetoed, set the data back.
|
|
|
|
SetCellValue( row, col, oldval );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-09-12 08:53:16 +00:00
|
|
|
}
|
2018-10-09 20:27:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::onGridColMove( wxGridEvent& aEvent )
|
|
|
|
{
|
|
|
|
// wxWidgets won't move an open editor, so better just to close it
|
|
|
|
CommitPendingChanges( true );
|
|
|
|
}
|
2019-02-09 23:31:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
if( aCol < 0 )
|
|
|
|
{
|
|
|
|
if( aKeep )
|
|
|
|
size = GetRowLabelSize();
|
|
|
|
|
2020-12-17 13:12:18 +00:00
|
|
|
// The 1.1 scale factor is due to the fact row labels use a bold font, bigger than
|
|
|
|
// the normal font.
|
2019-02-10 08:49:42 +00:00
|
|
|
// TODO: use a better way to evaluate the text size, for bold font
|
2019-02-10 06:59:27 +00:00
|
|
|
for( int row = 0; aContents && row < GetNumberRows(); row++ )
|
2022-02-05 00:24:26 +00:00
|
|
|
size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + wxT( "M" ) ).x * 1.1 ) );
|
2019-02-09 23:31:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( aKeep )
|
|
|
|
size = GetColSize( aCol );
|
|
|
|
|
2020-12-17 13:12:18 +00:00
|
|
|
// 'M' is generally the widest character, so we buffer the column width by default to
|
|
|
|
// ensure we don't write a continuous line of text at the column header
|
2019-02-09 23:31:31 +00:00
|
|
|
if( aHeader )
|
2019-02-10 08:49:42 +00:00
|
|
|
{
|
2019-02-18 19:34:44 +00:00
|
|
|
EnsureColLabelsVisible();
|
2020-12-17 13:12:18 +00:00
|
|
|
|
|
|
|
// The 1.1 scale factor is due to the fact headers use a bold font, bigger than
|
|
|
|
// the normal font.
|
2022-02-05 00:24:26 +00:00
|
|
|
size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + wxT( "M" ) ).x * 1.1 ) );
|
2019-02-10 08:49:42 +00:00
|
|
|
}
|
2019-02-09 23:31:31 +00:00
|
|
|
|
2019-02-10 06:59:27 +00:00
|
|
|
for( int row = 0; aContents && row < GetNumberRows(); row++ )
|
2019-02-15 06:02:34 +00:00
|
|
|
{
|
|
|
|
// If we have text, get the size. Otherwise, use a placeholder for the checkbox
|
|
|
|
if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
|
2022-02-05 00:24:26 +00:00
|
|
|
size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxT( "M" ) ).x );
|
2019-02-15 06:02:34 +00:00
|
|
|
else
|
|
|
|
size = std::max( size, GetTextExtent( "MM" ).x );
|
|
|
|
}
|
2019-02-09 23:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2019-02-18 19:34:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::EnsureColLabelsVisible()
|
|
|
|
{
|
2020-03-23 17:00:49 +00:00
|
|
|
// The 1.1 scale factor is due to the fact row labels use a bold font, bigger than
|
|
|
|
// the normal font
|
2019-02-18 19:34:44 +00:00
|
|
|
// TODO: use a better way to evaluate the text size, for bold font
|
2020-03-23 17:00:49 +00:00
|
|
|
int line_height = int( GetTextExtent( "Mj" ).y * 1.1 ) + 3;
|
|
|
|
int row_height = GetColLabelSize();
|
2021-09-14 18:08:48 +00:00
|
|
|
int initial_row_height = row_height;
|
2020-03-23 17:00:49 +00:00
|
|
|
|
2019-02-18 19:34:44 +00:00
|
|
|
// Headers can be multiline. Fix the Column Label Height to show the full header
|
|
|
|
// However GetTextExtent does not work on multiline strings,
|
|
|
|
// and do not return the full text height (only the height of one line)
|
|
|
|
for( int col = 0; col < GetNumberCols(); col++ )
|
|
|
|
{
|
2020-03-23 17:00:49 +00:00
|
|
|
int nl_count = GetColLabelValue( col ).Freq( '\n' );
|
2019-02-18 19:34:44 +00:00
|
|
|
|
|
|
|
if( nl_count )
|
|
|
|
{
|
|
|
|
// Col Label height must be able to show nl_count+1 lines
|
2020-03-23 17:00:49 +00:00
|
|
|
if( row_height < line_height * ( nl_count+1 ) )
|
|
|
|
row_height += line_height * nl_count;
|
2019-02-18 19:34:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 18:08:48 +00:00
|
|
|
// Update the column label size, but only if needed, to avoid generating useless
|
|
|
|
// and perhaps annoying UI events when the size does not change
|
|
|
|
if( initial_row_height != row_height )
|
|
|
|
SetColLabelSize( row_height );
|
2019-02-18 19:34:44 +00:00
|
|
|
}
|