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.

142 lines
4.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef _GRID_TRICKS_H_
  25. #define _GRID_TRICKS_H_
  26. #include <bitset>
  27. #include <functional>
  28. #include <wx/grid.h>
  29. #include <wx/event.h>
  30. #include <wx/menu.h>
  31. #include <widgets/wx_grid.h>
  32. #define GRIDTRICKS_MAX_COL 50
  33. enum
  34. {
  35. GRIDTRICKS_FIRST_ID = 901,
  36. GRIDTRICKS_ID_CUT,
  37. GRIDTRICKS_ID_COPY,
  38. GRIDTRICKS_ID_DELETE,
  39. GRIDTRICKS_ID_PASTE,
  40. GRIDTRICKS_ID_SELECT,
  41. GRIDTRICKS_FIRST_CLIENT_ID = 1101, // reserve IDs for sub-classes
  42. GRID_TRICKS_LAST_CLIENT_ID = 2100,
  43. GRIDTRICKS_FIRST_SHOWHIDE, // reserve IDs for show/hide-column-n
  44. GRIDTRICKS_LAST_ID = GRIDTRICKS_FIRST_SHOWHIDE + GRIDTRICKS_MAX_COL
  45. };
  46. /**
  47. * Add mouse and command handling (such as cut, copy, and paste) to a #WX_GRID instance.
  48. */
  49. class GRID_TRICKS : public wxEvtHandler
  50. {
  51. public:
  52. explicit GRID_TRICKS( WX_GRID* aGrid );
  53. GRID_TRICKS( WX_GRID* aGrid, std::function<void( wxCommandEvent& )> aAddHandler );
  54. /**
  55. * Enable the tooltip for a column.
  56. *
  57. * The tooltip is read from the string contained in the cell data.
  58. *
  59. * @param aCol is the column to use
  60. * @param aEnable is true to enable the tooltip (default)
  61. */
  62. void SetTooltipEnable( int aCol, bool aEnable = true )
  63. {
  64. m_tooltipEnabled[aCol] = aEnable;
  65. }
  66. /**
  67. * Query if the tooltip for a column is enabled
  68. *
  69. * @param aCol is the column to query
  70. * @return if the tooltip is enabled for the column
  71. */
  72. bool GetTooltipEnabled( int aCol )
  73. {
  74. return m_tooltipEnabled[aCol];
  75. }
  76. protected:
  77. /// Shared initialization for various ctors.
  78. void init();
  79. /// Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above.
  80. void getSelectedArea();
  81. void onGridCellLeftClick( wxGridEvent& event );
  82. void onGridCellLeftDClick( wxGridEvent& event );
  83. void onGridCellRightClick( wxGridEvent& event );
  84. void onGridLabelLeftClick( wxGridEvent& event );
  85. void onGridLabelRightClick( wxGridEvent& event );
  86. void onPopupSelection( wxCommandEvent& event );
  87. void onKeyDown( wxKeyEvent& event );
  88. void onCharHook( wxKeyEvent& event );
  89. void onUpdateUI( wxUpdateUIEvent& event );
  90. void onGridMotion( wxMouseEvent& event );
  91. virtual bool handleDoubleClick( wxGridEvent& aEvent );
  92. virtual void showPopupMenu( wxMenu& menu, wxGridEvent& aEvent );
  93. virtual void doPopupSelection( wxCommandEvent& event );
  94. bool isTextEntry( int aRow, int aCol );
  95. bool isCheckbox( int aRow, int aCol );
  96. bool isReadOnly( int aRow, int aCol );
  97. virtual bool toggleCell( int aRow, int aCol, bool aPreserveSelection = false );
  98. bool showEditor( int aRow, int aCol );
  99. virtual void paste_clipboard();
  100. virtual void paste_text( const wxString& cb_text );
  101. virtual void cutcopy( bool doCopy, bool doDelete );
  102. protected:
  103. WX_GRID* m_grid; ///< I don't own the grid, but he owns me
  104. // row & col "selection" acquisition
  105. // selected area by cell coordinate and count
  106. int m_sel_row_start;
  107. int m_sel_col_start;
  108. int m_sel_row_count;
  109. int m_sel_col_count;
  110. std::function<void( wxCommandEvent& )> m_addHandler;
  111. std::bitset<GRIDTRICKS_MAX_COL> m_tooltipEnabled;
  112. bool m_enableSingleClickEdit;
  113. bool m_multiCellEditEnabled;
  114. };
  115. #endif // _GRID_TRICKS_H_