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.

289 lines
9.5 KiB

3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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 <bitset>
  26. #include <memory>
  27. #include <utility>
  28. #include <vector>
  29. #include <wx/event.h>
  30. #include <wx/grid.h>
  31. #include <wx/version.h>
  32. #include <libeval/numeric_evaluator.h>
  33. #include <units_provider.h>
  34. class wxTextEntryBase;
  35. class WX_GRID_TABLE_BASE : public wxGridTableBase
  36. {
  37. protected:
  38. wxGridCellAttr* enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
  39. wxGridCellAttr::wxAttrKind aKind );
  40. };
  41. class WX_GRID : public wxGrid
  42. {
  43. public:
  44. // Constructor has to be wxFormBuilder-compatible
  45. WX_GRID( wxWindow *parent, wxWindowID id,
  46. const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
  47. long style = wxWANTS_CHARS, const wxString& name = wxGridNameStr );
  48. ~WX_GRID() override;
  49. /**
  50. * Hide wxGrid's SetColLabelSize() method with one which makes sure the size is tall
  51. * enough for the system GUI font.
  52. *
  53. * @param height
  54. */
  55. void SetColLabelSize( int aHeight ); // Yes, we're hiding a non-virtual method
  56. /**
  57. * Hide wxGrid's SetLabelFont() because for some reason on MSW it's a one-shot and
  58. * subsequent calls to it have no effect.
  59. */
  60. void SetLabelFont( const wxFont& aFont ); // Yes, we're hiding a non-virtual method
  61. /**
  62. * Enable alternate row highlighting, where every odd row has a different background
  63. * color than the even rows.
  64. *
  65. * @param aEnable flag to specify to enable alternate row striping in the grid.
  66. */
  67. void EnableAlternateRowColors( bool aEnable = true );
  68. /**
  69. * Get a tokenized string containing the shown column indexes.
  70. *
  71. * Tokens are separated by spaces.
  72. */
  73. wxString GetShownColumnsAsString();
  74. std::bitset<64> GetShownColumns();
  75. /**
  76. * Show/hide the grid columns based on a tokenized string of shown column indexes.
  77. */
  78. void ShowHideColumns( const wxString& shownColumns );
  79. /**
  80. * A more performant version of ShowHideColumns (primarily for OnUpdateUI handlers).
  81. */
  82. void ShowHideColumns( const std::bitset<64>& aShownColumns );
  83. /**
  84. * Hide wxGrid's SetTable() method with one which doesn't mess up the grid column
  85. * widths when setting the table.
  86. */
  87. void SetTable( wxGridTableBase* table, bool aTakeOwnership = false );
  88. /**
  89. * Work-around for a bug in wxGrid which crashes when deleting the table if the
  90. * cell edit control was not closed.
  91. */
  92. void DestroyTable( wxGridTableBase* aTable );
  93. /**
  94. * Close any open cell edit controls.
  95. *
  96. * @param aQuietMode if true don't send events (ie: for row/col delete operations).
  97. * @return false if validation failed.
  98. */
  99. bool CommitPendingChanges( bool aQuietMode = false );
  100. bool CancelPendingChanges();
  101. /**
  102. * Set a EUNITS_PROVIDER to enable use of unit- and eval-based Getters.
  103. *
  104. * @param aProvider
  105. */
  106. void SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol = 0 );
  107. void SetAutoEvalCols( const std::vector<int>& aCols ) { m_autoEvalCols = aCols; }
  108. /**
  109. * Set the unit and unit data type to use for a given column
  110. */
  111. void SetAutoEvalColUnits( int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType );
  112. /**
  113. * Set the unit to use for a given column. The unit data type is inferred from the unit type
  114. */
  115. void SetAutoEvalColUnits( int col, EDA_UNITS aUnit );
  116. /**
  117. * Apply standard KiCad unit and eval services to a numeric cell.
  118. *
  119. * @param aRow the cell row index to fetch.
  120. * @param aCol the cell column index to fetch.
  121. * @param aIsOptional if true, indicates to the unit provider the value is optional.
  122. * @return the value held by the cell in internal units.
  123. */
  124. int GetUnitValue( int aRow, int aCol );
  125. /**
  126. * Apply standard KiCad unit and eval services to a numeric cell.
  127. *
  128. * @param aRow the cell row index to fetch.
  129. * @param aCol the cell column index to fetch.
  130. * @return the value held by the cell in internal units.
  131. */
  132. std::optional<int> GetOptionalUnitValue( int aRow, int aCol );
  133. /**
  134. * Set a unitized cell's value.
  135. */
  136. void SetUnitValue( int aRow, int aCol, int aValue );
  137. /**
  138. * Set a unitized cell's optional value.
  139. */
  140. void SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue );
  141. /**
  142. * Calculate the specified column based on the actual size of the text on screen.
  143. *
  144. * @param aCol is the index of the column to resize. Specify -1 for the row labels.
  145. * @param aHeader is the header in the width calculation.
  146. * @param aContents is the full contents of the column.
  147. * @param aKeep is the current size as a minimum value.
  148. * @return The maximum value of all calculated widths.
  149. */
  150. int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = true, bool aKeep = false );
  151. /**
  152. * Ensure the height of the row displaying the column labels is enough, even
  153. * if labels are multiline texts.
  154. */
  155. void EnsureColLabelsVisible();
  156. /**
  157. * WxWidgets has a bunch of bugs in its handling of wxGrid mouse events which close cell
  158. * editors right after opening them.
  159. *
  160. * Helpfully, it already has a bunch of work-arounds in place (such as the SetInSetFocus()
  161. * hack), including one to make slow clicks work. We re-purpose this hack to work-around
  162. * the bugs when we want to open an editor.
  163. */
  164. void ShowEditorOnMouseUp() { m_waitForSlowClick = true; }
  165. void CancelShowEditorOnMouseUp() { m_waitForSlowClick = false; }
  166. /**
  167. * wxWidgets recently added an ASSERT which fires if the position is greater than or equal
  168. * to the number of rows (even if the delete count is 0). Needless to say, this makes using
  169. * DeleteRows for clearing a lot more cumbersome so we add a helper here.
  170. */
  171. void ClearRows()
  172. {
  173. if( GetNumberRows() )
  174. DeleteRows( 0, GetNumberRows() );
  175. }
  176. /**
  177. * A helper function to set OS-specific margins for text-based cell editors.
  178. */
  179. static void CellEditorSetMargins( wxTextEntryBase* aEntry );
  180. /**
  181. * A helper function to tweak sizes of text-based cell editors depending on OS.
  182. */
  183. static void CellEditorTransformSizeRect( wxRect& aRect );
  184. /**
  185. * Grids that have column sizes automatically set to fill the available width don't want
  186. * to shrink afterwards (because wxGrid reports the aggregate column size as the bestSize.
  187. *
  188. * @param aSize
  189. */
  190. void OverrideMinSize( double aXPct, double aYPct )
  191. {
  192. wxSize size = DoGetBestSize();
  193. m_minSizeOverride = wxSize( KiROUND( size.x * aXPct ), KiROUND( size.y * aYPct ) );
  194. }
  195. wxSize DoGetBestSize() const override
  196. {
  197. if( m_minSizeOverride )
  198. return m_minSizeOverride.value();
  199. else
  200. return wxGrid::DoGetBestSize();
  201. }
  202. protected:
  203. /**
  204. * A re-implementation of wxGrid::DrawColLabel which left-aligns the first column and draws
  205. * flat borders.
  206. */
  207. void DrawColLabel( wxDC& dc, int col ) override;
  208. /**
  209. * A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
  210. */
  211. void DrawRowLabel( wxDC& dc, int row ) override;
  212. /**
  213. * A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
  214. */
  215. void DrawCornerLabel( wxDC& dc ) override;
  216. void onGridColMove( wxGridEvent& aEvent );
  217. void onGridCellSelect( wxGridEvent& aEvent );
  218. void onCellEditorShown( wxGridEvent& aEvent );
  219. void onCellEditorHidden( wxGridEvent& aEvent );
  220. void onDPIChanged(wxDPIChangedEvent& event);
  221. UNITS_PROVIDER* getUnitsProvider( int aCol ) const
  222. {
  223. if( m_unitsProviders.contains( aCol ) )
  224. return m_unitsProviders.at( aCol );
  225. else
  226. return m_unitsProviders.begin()->second;
  227. }
  228. /**
  229. * Returns the units and data type associated with a given column
  230. */
  231. std::pair<EDA_UNITS, EDA_DATA_TYPE> getColumnUnits( int aCol ) const;
  232. protected:
  233. bool m_weOwnTable;
  234. std::map<int, UNITS_PROVIDER*> m_unitsProviders;
  235. std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
  236. std::vector<int> m_autoEvalCols;
  237. std::unordered_map<int, std::pair<EDA_UNITS, EDA_DATA_TYPE>> m_autoEvalColsUnits;
  238. std::map< std::pair<int, int>, std::pair<wxString, wxString> > m_evalBeforeAfter;
  239. std::optional<wxSize> m_minSizeOverride;
  240. };
  241. #endif //KICAD_WX_GRID_H