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.

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