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.

166 lines
5.7 KiB

3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018-2022 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. #ifndef KICAD_WX_GRID_H
  24. #define KICAD_WX_GRID_H
  25. #include <memory>
  26. #include <vector>
  27. #include <memory>
  28. #include <wx/event.h>
  29. #include <wx/grid.h>
  30. #include <wx/version.h>
  31. #include <units_provider.h>
  32. #include <libeval/numeric_evaluator.h>
  33. class WX_GRID : public wxGrid
  34. {
  35. public:
  36. // Constructor has to be wxFormBuilder-compatible
  37. WX_GRID( wxWindow *parent, wxWindowID id,
  38. const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
  39. long style = wxWANTS_CHARS, const wxString& name = wxGridNameStr );
  40. ~WX_GRID() override;
  41. /**
  42. * Hide wxGrid's SetColLabelSize() method with one which makes sure the size is tall
  43. * enough for the system GUI font.
  44. * @param height
  45. */
  46. void SetColLabelSize( int aHeight );
  47. /**
  48. * Get a tokenized string containing the shown column indexes.
  49. * Tokens are separated by spaces.
  50. */
  51. wxString GetShownColumns();
  52. /**
  53. * Show/hide the grid columns based on a tokenized string of shown column indexes.
  54. */
  55. void ShowHideColumns( const wxString& shownColumns );
  56. /**
  57. * Hide wxGrid's SetTable() method with one which doesn't mess up the grid column
  58. * widths when setting the table.
  59. */
  60. void SetTable( wxGridTableBase* table, bool aTakeOwnership = false );
  61. /**
  62. * Work-around for a bug in wxGrid which crashes when deleting the table if the
  63. * cell edit control was not closed.
  64. */
  65. void DestroyTable( wxGridTableBase* aTable );
  66. /**
  67. * Close any open cell edit controls.
  68. * @param aQuietMode if true don't send events (ie: for row/col delete operations)
  69. * @return false if validation failed
  70. */
  71. bool CommitPendingChanges( bool aQuietMode = false );
  72. /**
  73. * Set a UNITS_PROVIDER to enable use of unit- and eval-based Getters.
  74. * @param aProvider
  75. */
  76. void SetUnitsProvider( UNITS_PROVIDER* aProvider );
  77. void SetAutoEvalCols( const std::vector<int>& aCols ) { m_autoEvalCols = aCols; }
  78. /**
  79. * Apply standard KiCad unit and eval services to a numeric cell.
  80. * @return the value held by the cell in internal units
  81. */
  82. int GetUnitValue( int aRow, int aCol );
  83. /**
  84. * Set a unitized cell's value.
  85. */
  86. void SetUnitValue( int aRow, int aCol, int aValue );
  87. /**
  88. * Calculates the specified column based on the actual size of the text
  89. * on screen. Will return the maximum value of all calculated widths.
  90. * @param aCol - Integer value of the column to resize. Specify -1 for the row labels.
  91. * @param aHeader - Include the header in the width calculation
  92. * @param aContents - Include the full contents of the column
  93. * @param aKeep - Use the current size as a minimum value
  94. * @return The new size of the column
  95. */
  96. int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = true, bool aKeep = false );
  97. /**
  98. * Ensure the height of the row displaying the column labels is enough, even
  99. * if labels are multiline texts
  100. */
  101. void EnsureColLabelsVisible();
  102. /**
  103. * WxWidgets has a bunch of bugs in its handling of wxGrid mouse events which close cell
  104. * editors right after opening them. Helpfully, it already has a bunch of work-arounds in
  105. * place (such as the SetInSetFocus() hack), including one to make slow clicks work. We
  106. * re-purpose this hack to work-around the bugs when we want to open an editor.
  107. */
  108. void ShowEditorOnMouseUp() { m_waitForSlowClick = true; }
  109. /**
  110. * wxWidgets recently added an ASSERT which fires if the position is greater than or equal
  111. * to the number of rows (even if the delete count is 0). Needless to say, this makes using
  112. * DeleteRows for clearing a lot more cumbersome so we add a helper here.
  113. */
  114. void ClearRows()
  115. {
  116. if( GetNumberRows() )
  117. DeleteRows( 0, GetNumberRows() );
  118. }
  119. protected:
  120. /**
  121. * A re-implementation of wxGrid::DrawColLabel which left-aligns the first column when
  122. * there are no row labels.
  123. */
  124. void DrawColLabel( wxDC& dc, int col ) override;
  125. void onGridColMove( wxGridEvent& aEvent );
  126. void onGridCellSelect( wxGridEvent& aEvent );
  127. void onCellEditorShown( wxGridEvent& aEvent );
  128. void onCellEditorHidden( wxGridEvent& aEvent );
  129. #if wxCHECK_VERSION( 3, 1, 3 )
  130. void onDPIChanged(wxDPIChangedEvent& event);
  131. #endif
  132. protected:
  133. bool m_weOwnTable;
  134. UNITS_PROVIDER* m_unitsProvider;
  135. std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
  136. std::vector<int> m_autoEvalCols;
  137. std::map< std::pair<int, int>, std::pair<wxString, wxString> > m_evalBeforeAfter;
  138. };
  139. #endif //KICAD_WX_GRID_H