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.

355 lines
11 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018-2021 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 <widgets/wx_grid.h>
  26. #include <widgets/ui_common.h>
  27. #include <algorithm>
  28. #define MIN_GRIDCELL_MARGIN 3
  29. WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
  30. long style, const wxString& name ) :
  31. wxGrid( parent, id, pos, size, style, name ),
  32. m_weOwnTable( false )
  33. {
  34. SetDefaultCellOverflow( false );
  35. // Make sure the GUI font scales properly on GTK
  36. SetDefaultCellFont( KIUI::GetControlFont( this ) );
  37. #if wxCHECK_VERSION( 3, 1, 3 )
  38. Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
  39. #endif
  40. }
  41. WX_GRID::~WX_GRID()
  42. {
  43. if( m_weOwnTable )
  44. DestroyTable( GetTable() );
  45. #if wxCHECK_VERSION( 3, 1, 3 )
  46. Disconnect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
  47. #endif
  48. }
  49. #if wxCHECK_VERSION( 3, 1, 3 )
  50. void WX_GRID::onDPIChanged(wxDPIChangedEvent& aEvt)
  51. {
  52. /// This terrible hack is a way to avoid the incredibly disruptive resizing of grids that happens on Macs
  53. /// when moving a window between monitors of different DPIs.
  54. #ifndef __WXMAC__
  55. aEvt.Skip();
  56. #endif
  57. }
  58. #endif
  59. void WX_GRID::SetColLabelSize( int aHeight )
  60. {
  61. if( aHeight == 0 )
  62. {
  63. wxGrid::SetColLabelSize( 0 );
  64. return;
  65. }
  66. wxFont headingFont = KIUI::GetControlFont( this ).Bold();
  67. // Make sure the GUI font scales properly on GTK
  68. SetLabelFont( headingFont );
  69. // Correct wxFormBuilder height for large fonts
  70. int minHeight = headingFont.GetPixelSize().y + 2 * MIN_GRIDCELL_MARGIN;
  71. wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
  72. }
  73. void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
  74. {
  75. // wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
  76. // and restore them.
  77. int numberCols = GetNumberCols();
  78. int* formBuilderColWidths = new int[numberCols];
  79. for( int i = 0; i < numberCols; ++i )
  80. formBuilderColWidths[ i ] = GetColSize( i );
  81. wxGrid::SetTable( aTable );
  82. // wxGrid::SetTable() may change the number of columns, so prevent out-of-bounds access
  83. // to formBuilderColWidths
  84. numberCols = std::min( numberCols, GetNumberCols() );
  85. for( int i = 0; i < numberCols; ++i )
  86. {
  87. // correct wxFormBuilder width for large fonts and/or long translations
  88. int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
  89. SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
  90. }
  91. delete[] formBuilderColWidths;
  92. Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
  93. Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
  94. m_weOwnTable = aTakeOwnership;
  95. }
  96. void WX_GRID::onGridCellSelect( wxGridEvent& aEvent )
  97. {
  98. // Highlight the selected cell.
  99. // Calling SelectBlock() allows a visual effect when cells are selected
  100. // by tab or arrow keys.
  101. // Otherwise, one cannot really know what actual cell is selected
  102. int row = aEvent.GetRow();
  103. int col = aEvent.GetCol();
  104. if( row >= 0 && col >= 0 )
  105. SelectBlock(row,col,row,col,false);
  106. }
  107. void WX_GRID::DestroyTable( wxGridTableBase* aTable )
  108. {
  109. // wxGrid's destructor will crash trying to look up the cell attr if the edit control
  110. // is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
  111. CommitPendingChanges( true /* quiet mode */ );
  112. Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
  113. Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
  114. wxGrid::SetTable( nullptr );
  115. delete aTable;
  116. }
  117. wxString WX_GRID::GetShownColumns()
  118. {
  119. wxString shownColumns;
  120. for( int i = 0; i < GetNumberCols(); ++i )
  121. {
  122. if( IsColShown( i ) )
  123. {
  124. if( shownColumns.Length() )
  125. shownColumns << wxT( " " );
  126. shownColumns << i;
  127. }
  128. }
  129. return shownColumns;
  130. }
  131. void WX_GRID::ShowHideColumns( const wxString& shownColumns )
  132. {
  133. for( int i = 0; i < GetNumberCols(); ++i )
  134. HideCol( i );
  135. wxStringTokenizer shownTokens( shownColumns );
  136. while( shownTokens.HasMoreTokens() )
  137. {
  138. long colNumber;
  139. shownTokens.GetNextToken().ToLong( &colNumber );
  140. if( colNumber >= 0 && colNumber < GetNumberCols() )
  141. ShowCol( colNumber );
  142. }
  143. }
  144. void WX_GRID::DrawColLabel( wxDC& dc, int col )
  145. {
  146. if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
  147. return;
  148. int colLeft = GetColLeft( col );
  149. wxRect rect( colLeft, 0, GetColWidth( col ), m_colLabelHeight );
  150. static wxGridColumnHeaderRendererDefault rend;
  151. // It is reported that we need to erase the background to avoid display
  152. // artifacts, see #12055.
  153. // wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
  154. #if wxCHECK_VERSION( 3, 1, 3 )
  155. wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
  156. #else
  157. wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
  158. #endif
  159. dc.DrawRectangle(rect);
  160. rend.DrawBorder( *this, dc, rect );
  161. // Make sure fonts get scaled correctly on GTK HiDPI monitors
  162. dc.SetFont( GetLabelFont() );
  163. int hAlign, vAlign;
  164. GetColLabelAlignment( &hAlign, &vAlign );
  165. const int orient = GetColLabelTextOrientation();
  166. if( col == 0 && GetRowLabelSize() == 0 )
  167. hAlign = wxALIGN_LEFT;
  168. rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
  169. }
  170. bool WX_GRID::CommitPendingChanges( bool aQuietMode )
  171. {
  172. if( !IsCellEditControlEnabled() )
  173. return true;
  174. if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
  175. return false;
  176. HideCellEditControl();
  177. // do it after HideCellEditControl()
  178. m_cellEditCtrlEnabled = false;
  179. int row = m_currentCellCoords.GetRow();
  180. int col = m_currentCellCoords.GetCol();
  181. wxString oldval = GetCellValue( row, col );
  182. wxString newval;
  183. wxGridCellAttr* attr = GetCellAttr( row, col );
  184. wxGridCellEditor* editor = attr->GetEditor( this, row, col );
  185. bool changed = editor->EndEdit( row, col, this, oldval, &newval );
  186. editor->DecRef();
  187. attr->DecRef();
  188. if( changed )
  189. {
  190. if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
  191. return false;
  192. editor->ApplyEdit( row, col, this );
  193. // for compatibility reasons dating back to wx 2.8 when this event
  194. // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
  195. // didn't exist we allow vetoing this one too
  196. if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
  197. {
  198. // Event has been vetoed, set the data back.
  199. SetCellValue( row, col, oldval );
  200. return false;
  201. }
  202. }
  203. return true;
  204. }
  205. void WX_GRID::onGridColMove( wxGridEvent& aEvent )
  206. {
  207. // wxWidgets won't move an open editor, so better just to close it
  208. CommitPendingChanges( true );
  209. }
  210. int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
  211. {
  212. int size = 0;
  213. if( aCol < 0 )
  214. {
  215. if( aKeep )
  216. size = GetRowLabelSize();
  217. // The 1.1 scale factor is due to the fact row labels use a bold font, bigger than
  218. // the normal font.
  219. // TODO: use a better way to evaluate the text size, for bold font
  220. for( int row = 0; aContents && row < GetNumberRows(); row++ )
  221. size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + "M" ).x * 1.1 ) );
  222. }
  223. else
  224. {
  225. if( aKeep )
  226. size = GetColSize( aCol );
  227. // 'M' is generally the widest character, so we buffer the column width by default to
  228. // ensure we don't write a continuous line of text at the column header
  229. if( aHeader )
  230. {
  231. EnsureColLabelsVisible();
  232. // The 1.1 scale factor is due to the fact headers use a bold font, bigger than
  233. // the normal font.
  234. size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + "M" ).x * 1.1 ) );
  235. }
  236. for( int row = 0; aContents && row < GetNumberRows(); row++ )
  237. {
  238. // If we have text, get the size. Otherwise, use a placeholder for the checkbox
  239. if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
  240. size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + "M" ).x );
  241. else
  242. size = std::max( size, GetTextExtent( "MM" ).x );
  243. }
  244. }
  245. return size;
  246. }
  247. void WX_GRID::EnsureColLabelsVisible()
  248. {
  249. // The 1.1 scale factor is due to the fact row labels use a bold font, bigger than
  250. // the normal font
  251. // TODO: use a better way to evaluate the text size, for bold font
  252. int line_height = int( GetTextExtent( "Mj" ).y * 1.1 ) + 3;
  253. int row_height = GetColLabelSize();
  254. int initial_row_height = row_height;
  255. // Headers can be multiline. Fix the Column Label Height to show the full header
  256. // However GetTextExtent does not work on multiline strings,
  257. // and do not return the full text height (only the height of one line)
  258. for( int col = 0; col < GetNumberCols(); col++ )
  259. {
  260. int nl_count = GetColLabelValue( col ).Freq( '\n' );
  261. if( nl_count )
  262. {
  263. // Col Label height must be able to show nl_count+1 lines
  264. if( row_height < line_height * ( nl_count+1 ) )
  265. row_height += line_height * nl_count;
  266. }
  267. }
  268. // Update the column label size, but only if needed, to avoid generating useless
  269. // and perhaps annoying UI events when the size does not change
  270. if( initial_row_height != row_height )
  271. SetColLabelSize( row_height );
  272. }