You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

585 lines
17 KiB

3 years ago
3 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 3
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <wx/tokenzr.h>
  24. #include <wx/dc.h>
  25. #include <wx/settings.h>
  26. #include <widgets/wx_grid.h>
  27. #include <widgets/ui_common.h>
  28. #include <algorithm>
  29. #include <core/kicad_algo.h>
  30. #include <gal/color4d.h>
  31. #define MIN_GRIDCELL_MARGIN 3
  32. wxColour getBorderColour()
  33. {
  34. KIGFX::COLOR4D bg = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
  35. KIGFX::COLOR4D fg = wxSystemSettings::GetColour( wxSYS_COLOUR_ACTIVEBORDER );
  36. KIGFX::COLOR4D border = fg.Mix( bg, 0.50 );
  37. return border.ToColour();
  38. }
  39. class WX_GRID_CORNER_HEADER_RENDERER : public wxGridCornerHeaderRendererDefault
  40. {
  41. public:
  42. void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
  43. {
  44. wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
  45. wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
  46. rect.SetTop( rect.GetTop() + 1 );
  47. rect.SetLeft( rect.GetLeft() + 1 );
  48. rect.SetBottom( rect.GetBottom() - 1 );
  49. rect.SetRight( rect.GetRight() - 1 );
  50. dc.DrawRectangle( rect );
  51. }
  52. };
  53. class WX_GRID_COLUMN_HEADER_RENDERER : public wxGridColumnHeaderRendererDefault
  54. {
  55. public:
  56. void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
  57. {
  58. wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
  59. wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
  60. rect.SetTop( rect.GetTop() + 1 );
  61. rect.SetLeft( rect.GetLeft() );
  62. rect.SetBottom( rect.GetBottom() - 1 );
  63. rect.SetRight( rect.GetRight() - 1 );
  64. dc.DrawRectangle( rect );
  65. }
  66. };
  67. class WX_GRID_ROW_HEADER_RENDERER : public wxGridRowHeaderRendererDefault
  68. {
  69. public:
  70. void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
  71. {
  72. wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
  73. wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
  74. rect.SetTop( rect.GetTop() + 1 );
  75. rect.SetLeft( rect.GetLeft() + 1 );
  76. rect.SetBottom( rect.GetBottom() - 1 );
  77. rect.SetRight( rect.GetRight() );
  78. dc.DrawRectangle( rect );
  79. }
  80. };
  81. WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
  82. long style, const wxString& name ) :
  83. wxGrid( parent, id, pos, size, style, name ),
  84. m_weOwnTable( false )
  85. {
  86. SetDefaultCellOverflow( false );
  87. // Make sure the GUI font scales properly
  88. SetDefaultCellFont( KIUI::GetControlFont( this ) );
  89. SetLabelFont( KIUI::GetControlFont( this ) );
  90. if( GetColLabelSize() > 0 )
  91. SetColLabelSize( GetColLabelSize() + 4 );
  92. #if wxCHECK_VERSION( 3, 1, 3 )
  93. Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
  94. #endif
  95. Connect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr, this );
  96. Connect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ), nullptr, this );
  97. }
  98. WX_GRID::~WX_GRID()
  99. {
  100. if( m_weOwnTable )
  101. DestroyTable( GetTable() );
  102. #if wxCHECK_VERSION( 3, 1, 3 )
  103. Disconnect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
  104. #endif
  105. }
  106. #if wxCHECK_VERSION( 3, 1, 3 )
  107. void WX_GRID::onDPIChanged(wxDPIChangedEvent& aEvt)
  108. {
  109. /// This terrible hack is a way to avoid the incredibly disruptive resizing of grids that happens on Macs
  110. /// when moving a window between monitors of different DPIs.
  111. #ifndef __WXMAC__
  112. aEvt.Skip();
  113. #endif
  114. }
  115. #endif
  116. void WX_GRID::SetColLabelSize( int aHeight )
  117. {
  118. if( aHeight == 0 )
  119. {
  120. wxGrid::SetColLabelSize( 0 );
  121. return;
  122. }
  123. // Correct wxFormBuilder height for large fonts
  124. int minHeight = GetLabelFont().GetPixelSize().y + 2 * MIN_GRIDCELL_MARGIN;
  125. wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
  126. }
  127. void WX_GRID::SetLabelFont( const wxFont& aFont )
  128. {
  129. wxGrid::SetLabelFont( KIUI::GetControlFont( this ) );
  130. }
  131. void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
  132. {
  133. // wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
  134. // and restore them.
  135. int numberCols = GetNumberCols();
  136. int* formBuilderColWidths = new int[numberCols];
  137. for( int i = 0; i < numberCols; ++i )
  138. formBuilderColWidths[ i ] = GetColSize( i );
  139. wxGrid::SetTable( aTable );
  140. // wxGrid::SetTable() may change the number of columns, so prevent out-of-bounds access
  141. // to formBuilderColWidths
  142. numberCols = std::min( numberCols, GetNumberCols() );
  143. for( int i = 0; i < numberCols; ++i )
  144. {
  145. // correct wxFormBuilder width for large fonts and/or long translations
  146. int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
  147. SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
  148. }
  149. delete[] formBuilderColWidths;
  150. Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
  151. Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
  152. m_weOwnTable = aTakeOwnership;
  153. }
  154. void WX_GRID::onGridCellSelect( wxGridEvent& aEvent )
  155. {
  156. // Highlight the selected cell.
  157. // Calling SelectBlock() allows a visual effect when cells are selected by tab or arrow keys.
  158. // Otherwise, one cannot really know what actual cell is selected.
  159. int row = aEvent.GetRow();
  160. int col = aEvent.GetCol();
  161. if( row >= 0 && col >= 0 )
  162. SelectBlock( row, col, row, col, false );
  163. }
  164. void WX_GRID::onCellEditorShown( wxGridEvent& aEvent )
  165. {
  166. if( alg::contains( m_autoEvalCols, aEvent.GetCol() ) )
  167. {
  168. int row = aEvent.GetRow();
  169. int col = aEvent.GetCol();
  170. const std::pair<wxString, wxString>& beforeAfter = m_evalBeforeAfter[ { row, col } ];
  171. if( GetCellValue( row, col ) == beforeAfter.second )
  172. SetCellValue( row, col, beforeAfter.first );
  173. }
  174. }
  175. void WX_GRID::onCellEditorHidden( wxGridEvent& aEvent )
  176. {
  177. if( alg::contains( m_autoEvalCols, aEvent.GetCol() ) )
  178. {
  179. UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aEvent.GetCol() ];
  180. if( !unitsProvider )
  181. unitsProvider = m_unitsProviders.begin()->second;
  182. m_eval->SetDefaultUnits( unitsProvider->GetUserUnits() );
  183. int row = aEvent.GetRow();
  184. int col = aEvent.GetCol();
  185. CallAfter(
  186. [this, row, col, unitsProvider]()
  187. {
  188. wxString stringValue = GetCellValue( row, col );
  189. if( m_eval->Process( stringValue ) )
  190. {
  191. int val = unitsProvider->ValueFromString( m_eval->Result() );
  192. wxString evalValue = unitsProvider->StringFromValue( val, true );
  193. if( stringValue != evalValue )
  194. {
  195. SetCellValue( row, col, evalValue );
  196. m_evalBeforeAfter[ { row, col } ] = { stringValue, evalValue };
  197. }
  198. }
  199. } );
  200. }
  201. aEvent.Skip();
  202. }
  203. void WX_GRID::DestroyTable( wxGridTableBase* aTable )
  204. {
  205. // wxGrid's destructor will crash trying to look up the cell attr if the edit control
  206. // is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
  207. CommitPendingChanges( true /* quiet mode */ );
  208. Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
  209. Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
  210. wxGrid::SetTable( nullptr );
  211. delete aTable;
  212. }
  213. wxString WX_GRID::GetShownColumns()
  214. {
  215. wxString shownColumns;
  216. for( int i = 0; i < GetNumberCols(); ++i )
  217. {
  218. if( IsColShown( i ) )
  219. {
  220. if( shownColumns.Length() )
  221. shownColumns << wxT( " " );
  222. shownColumns << i;
  223. }
  224. }
  225. return shownColumns;
  226. }
  227. void WX_GRID::ShowHideColumns( const wxString& shownColumns )
  228. {
  229. for( int i = 0; i < GetNumberCols(); ++i )
  230. HideCol( i );
  231. wxStringTokenizer shownTokens( shownColumns );
  232. while( shownTokens.HasMoreTokens() )
  233. {
  234. long colNumber;
  235. shownTokens.GetNextToken().ToLong( &colNumber );
  236. if( colNumber >= 0 && colNumber < GetNumberCols() )
  237. ShowCol( (int) colNumber );
  238. }
  239. }
  240. void WX_GRID::DrawCornerLabel( wxDC& dc )
  241. {
  242. if( m_nativeColumnLabels )
  243. wxGrid::DrawCornerLabel( dc );
  244. wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
  245. static WX_GRID_CORNER_HEADER_RENDERER rend;
  246. // It is reported that we need to erase the background to avoid display
  247. // artifacts, see #12055.
  248. {
  249. // wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
  250. #if wxCHECK_VERSION( 3, 1, 3 )
  251. wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
  252. wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
  253. #else
  254. wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
  255. wxDCPenChanger setPen( dc, m_colWindow->GetBackgroundColour() );
  256. #endif
  257. dc.DrawRectangle( rect.Inflate( 1 ) );
  258. }
  259. rend.DrawBorder( *this, dc, rect );
  260. }
  261. void WX_GRID::DrawColLabel( wxDC& dc, int col )
  262. {
  263. if( m_nativeColumnLabels )
  264. wxGrid::DrawColLabel( dc, col );
  265. if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
  266. return;
  267. wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
  268. static WX_GRID_COLUMN_HEADER_RENDERER rend;
  269. // It is reported that we need to erase the background to avoid display
  270. // artifacts, see #12055.
  271. {
  272. // wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
  273. #if wxCHECK_VERSION( 3, 1, 3 )
  274. wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
  275. wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
  276. #else
  277. wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
  278. wxDCPenChanger setPen( dc, m_colWindow->GetBackgroundColour() );
  279. #endif
  280. dc.DrawRectangle( rect.Inflate( 1 ) );
  281. }
  282. rend.DrawBorder( *this, dc, rect );
  283. // Make sure fonts get scaled correctly on GTK HiDPI monitors
  284. dc.SetFont( GetLabelFont() );
  285. int hAlign, vAlign;
  286. GetColLabelAlignment( &hAlign, &vAlign );
  287. const int orient = GetColLabelTextOrientation();
  288. if( col == 0 )
  289. hAlign = wxALIGN_LEFT;
  290. if( hAlign == wxALIGN_LEFT )
  291. rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
  292. rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
  293. }
  294. void WX_GRID::DrawRowLabel( wxDC& dc, int row )
  295. {
  296. if ( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
  297. return;
  298. wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
  299. static WX_GRID_ROW_HEADER_RENDERER rend;
  300. // It is reported that we need to erase the background to avoid display
  301. // artifacts, see #12055.
  302. {
  303. // wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
  304. #if wxCHECK_VERSION( 3, 1, 3 )
  305. wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
  306. wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
  307. #else
  308. wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
  309. wxDCPenChanger setPen( dc, m_colWindow->GetBackgroundColour() );
  310. #endif
  311. dc.DrawRectangle( rect.Inflate( 1 ) );
  312. }
  313. rend.DrawBorder( *this, dc, rect );
  314. // Make sure fonts get scaled correctly on GTK HiDPI monitors
  315. dc.SetFont( GetLabelFont() );
  316. int hAlign, vAlign;
  317. GetRowLabelAlignment(&hAlign, &vAlign);
  318. if( hAlign == wxALIGN_LEFT )
  319. rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
  320. rend.DrawLabel( *this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
  321. }
  322. bool WX_GRID::CommitPendingChanges( bool aQuietMode )
  323. {
  324. if( !IsCellEditControlEnabled() )
  325. return true;
  326. if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
  327. return false;
  328. HideCellEditControl();
  329. // do it after HideCellEditControl()
  330. m_cellEditCtrlEnabled = false;
  331. int row = m_currentCellCoords.GetRow();
  332. int col = m_currentCellCoords.GetCol();
  333. wxString oldval = GetCellValue( row, col );
  334. wxString newval;
  335. wxGridCellAttr* attr = GetCellAttr( row, col );
  336. wxGridCellEditor* editor = attr->GetEditor( this, row, col );
  337. bool changed = editor->EndEdit( row, col, this, oldval, &newval );
  338. editor->DecRef();
  339. attr->DecRef();
  340. if( changed )
  341. {
  342. if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
  343. return false;
  344. editor->ApplyEdit( row, col, this );
  345. // for compatibility reasons dating back to wx 2.8 when this event
  346. // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
  347. // didn't exist we allow vetoing this one too
  348. if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
  349. {
  350. // Event has been vetoed, set the data back.
  351. SetCellValue( row, col, oldval );
  352. return false;
  353. }
  354. }
  355. return true;
  356. }
  357. void WX_GRID::SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol )
  358. {
  359. m_unitsProviders[ aCol ] = aProvider;
  360. if( !m_eval )
  361. m_eval = std::make_unique<NUMERIC_EVALUATOR>( aProvider->GetUserUnits() );
  362. }
  363. int WX_GRID::GetUnitValue( int aRow, int aCol )
  364. {
  365. UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aCol ];
  366. if( !unitsProvider )
  367. unitsProvider = m_unitsProviders.begin()->second;
  368. wxString stringValue = GetCellValue( aRow, aCol );
  369. if( alg::contains( m_autoEvalCols, aCol ) )
  370. {
  371. m_eval->SetDefaultUnits( unitsProvider->GetUserUnits() );
  372. if( m_eval->Process( stringValue ) )
  373. stringValue = m_eval->Result();
  374. }
  375. return unitsProvider->ValueFromString( stringValue );
  376. }
  377. void WX_GRID::SetUnitValue( int aRow, int aCol, int aValue )
  378. {
  379. UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aCol ];
  380. if( !unitsProvider )
  381. unitsProvider = m_unitsProviders.begin()->second;
  382. SetCellValue( aRow, aCol, unitsProvider->StringFromValue( aValue, true ) );
  383. }
  384. void WX_GRID::onGridColMove( wxGridEvent& aEvent )
  385. {
  386. // wxWidgets won't move an open editor, so better just to close it
  387. CommitPendingChanges( true );
  388. }
  389. int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
  390. {
  391. int size = 0;
  392. if( aCol < 0 )
  393. {
  394. if( aKeep )
  395. size = GetRowLabelSize();
  396. for( int row = 0; aContents && row < GetNumberRows(); row++ )
  397. size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + wxS( "M" ) ).x ) );
  398. }
  399. else
  400. {
  401. if( aKeep )
  402. size = GetColSize( aCol );
  403. // 'M' is generally the widest character, so we buffer the column width by default to
  404. // ensure we don't write a continuous line of text at the column header
  405. if( aHeader )
  406. {
  407. EnsureColLabelsVisible();
  408. size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + wxS( "M" ) ).x ) );
  409. }
  410. for( int row = 0; aContents && row < GetNumberRows(); row++ )
  411. {
  412. // If we have text, get the size. Otherwise, use a placeholder for the checkbox
  413. if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
  414. size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxS( "M" ) ).x );
  415. else
  416. size = std::max( size, GetTextExtent( "MM" ).x );
  417. }
  418. }
  419. return size;
  420. }
  421. void WX_GRID::EnsureColLabelsVisible()
  422. {
  423. int line_height = int( GetTextExtent( "Mj" ).y ) + 3;
  424. int row_height = GetColLabelSize();
  425. int initial_row_height = row_height;
  426. // Headers can be multiline. Fix the Column Label Height to show the full header
  427. // However GetTextExtent does not work on multiline strings,
  428. // and do not return the full text height (only the height of one line)
  429. for( int col = 0; col < GetNumberCols(); col++ )
  430. {
  431. int nl_count = GetColLabelValue( col ).Freq( '\n' );
  432. if( nl_count )
  433. {
  434. // Col Label height must be able to show nl_count+1 lines
  435. if( row_height < line_height * ( nl_count+1 ) )
  436. row_height += line_height * nl_count;
  437. }
  438. }
  439. // Update the column label size, but only if needed, to avoid generating useless
  440. // and perhaps annoying UI events when the size does not change
  441. if( initial_row_height != row_height )
  442. SetColLabelSize( row_height );
  443. }