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.

183 lines
5.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021 CERN
  5. * Copyright (C) 2018-2022 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_TEXT_BUTTON_HELPERS_H
  25. #define GRID_TEXT_BUTTON_HELPERS_H
  26. #include <memory>
  27. #include <wx/combo.h>
  28. #include <wx/generic/gridctrl.h>
  29. #include <wx/generic/grideditors.h>
  30. class wxGrid;
  31. class WX_GRID;
  32. class DIALOG_SHIM;
  33. class GRID_CELL_TEXT_BUTTON : public wxGridCellEditor
  34. {
  35. public:
  36. GRID_CELL_TEXT_BUTTON() {};
  37. wxString GetValue() const override;
  38. void SetSize( const wxRect& aRect ) override;
  39. void StartingKey( wxKeyEvent& event ) override;
  40. void BeginEdit( int aRow, int aCol, wxGrid* aGrid ) override;
  41. bool EndEdit( int , int , const wxGrid* , const wxString& , wxString *aNewVal ) override;
  42. void ApplyEdit( int aRow, int aCol, wxGrid* aGrid ) override;
  43. void Reset() override;
  44. #if wxUSE_VALIDATORS
  45. void SetValidator( const wxValidator& validator );
  46. #endif
  47. protected:
  48. wxComboCtrl* Combo() const { return static_cast<wxComboCtrl*>( m_control ); }
  49. #if wxUSE_VALIDATORS
  50. std::unique_ptr< wxValidator > m_validator;
  51. #endif
  52. wxString m_value;
  53. wxDECLARE_NO_COPY_CLASS( GRID_CELL_TEXT_BUTTON );
  54. };
  55. class GRID_CELL_SYMBOL_ID_EDITOR : public GRID_CELL_TEXT_BUTTON
  56. {
  57. public:
  58. GRID_CELL_SYMBOL_ID_EDITOR( DIALOG_SHIM* aParent,
  59. const wxString& aPreselect = wxEmptyString ) :
  60. m_dlg( aParent ),
  61. m_preselect( aPreselect )
  62. { }
  63. wxGridCellEditor* Clone() const override
  64. {
  65. return new GRID_CELL_SYMBOL_ID_EDITOR( m_dlg, m_preselect );
  66. }
  67. void Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) override;
  68. protected:
  69. DIALOG_SHIM* m_dlg;
  70. wxString m_preselect;
  71. };
  72. class GRID_CELL_FPID_EDITOR : public GRID_CELL_TEXT_BUTTON
  73. {
  74. public:
  75. GRID_CELL_FPID_EDITOR( DIALOG_SHIM* aParent, const wxString& aSymbolNetlist,
  76. const wxString& aPreselect = wxEmptyString ) :
  77. m_dlg( aParent ),
  78. m_preselect( aPreselect ),
  79. m_symbolNetlist( aSymbolNetlist )
  80. { }
  81. wxGridCellEditor* Clone() const override
  82. {
  83. return new GRID_CELL_FPID_EDITOR( m_dlg, m_symbolNetlist );
  84. }
  85. void Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) override;
  86. protected:
  87. DIALOG_SHIM* m_dlg;
  88. wxString m_preselect;
  89. wxString m_symbolNetlist;
  90. };
  91. class GRID_CELL_URL_EDITOR : public GRID_CELL_TEXT_BUTTON
  92. {
  93. public:
  94. GRID_CELL_URL_EDITOR( DIALOG_SHIM* aParent, SEARCH_STACK* aSearchStack = nullptr ) :
  95. m_dlg( aParent ), m_searchStack( aSearchStack )
  96. { }
  97. wxGridCellEditor* Clone() const override
  98. {
  99. return new GRID_CELL_URL_EDITOR( m_dlg );
  100. }
  101. void Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) override;
  102. protected:
  103. DIALOG_SHIM* m_dlg;
  104. SEARCH_STACK* m_searchStack;
  105. };
  106. /**
  107. * Editor for wxGrid cells that adds a file/folder browser to the grid input field
  108. */
  109. class GRID_CELL_PATH_EDITOR : public GRID_CELL_TEXT_BUTTON
  110. {
  111. public:
  112. /**
  113. * Constructor
  114. *
  115. * @param aCurrentDir is current directory the path editor will open at
  116. * @param aExt is the file extension(s) to filter by. If empty, the path editor will switch
  117. * to folder mode instead of file.
  118. * @param aNormalize indicates whether to normalize the selected path (replace part of path
  119. * with variables or relative path)
  120. * @param aNormalizeBasePath is the path to use when trying to base variables (generally
  121. * current project path)
  122. */
  123. GRID_CELL_PATH_EDITOR( DIALOG_SHIM* aParentDialog, WX_GRID* aGrid, wxString* aCurrentDir,
  124. const wxString& aExt, bool aNormalize = false,
  125. const wxString& aNormalizeBasePath = wxEmptyString ) :
  126. m_dlg( aParentDialog ),
  127. m_grid( aGrid ),
  128. m_currentDir( aCurrentDir ),
  129. m_ext( aExt ),
  130. m_normalize( aNormalize ),
  131. m_normalizeBasePath( aNormalizeBasePath )
  132. { }
  133. wxGridCellEditor* Clone() const override
  134. {
  135. return new GRID_CELL_PATH_EDITOR( m_dlg, m_grid, m_currentDir, m_ext );
  136. }
  137. void Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) override;
  138. protected:
  139. DIALOG_SHIM* m_dlg;
  140. WX_GRID* m_grid;
  141. wxString* m_currentDir;
  142. wxString m_ext;
  143. bool m_normalize;
  144. wxString m_normalizeBasePath;
  145. };
  146. #endif // GRID_TEXT_BUTTON_HELPERS_H