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.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Wayne Stambaugh <stambaughw@verizon.net>
  5. * Copyright (C) 1992-2018 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. #include <fctsys.h>
  25. #include <base_screen.h>
  26. #include <widgets/wx_grid.h>
  27. #include <template_fieldnames.h>
  28. #include <grid_tricks.h>
  29. #include <sch_edit_frame.h>
  30. #include <bitmaps.h>
  31. #include <panel_eeschema_template_fieldnames.h>
  32. PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( SCH_EDIT_FRAME* aFrame,
  33. wxWindow* aWindow ) :
  34. PANEL_EESCHEMA_TEMPLATE_FIELDNAMES_BASE( aWindow ),
  35. m_frame( aFrame )
  36. {
  37. m_addFieldButton->SetBitmap( KiBitmap( small_plus_xpm ) );
  38. m_deleteFieldButton->SetBitmap( KiBitmap( trash_xpm ) );
  39. m_checkboxColWidth = m_grid->GetColSize( 1 );
  40. m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
  41. }
  42. PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::~PANEL_EESCHEMA_TEMPLATE_FIELDNAMES()
  43. {
  44. // Delete the GRID_TRICKS.
  45. m_grid->PopEventHandler( true );
  46. }
  47. bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataToWindow()
  48. {
  49. m_fields = m_frame->GetTemplateFieldNames();
  50. return TransferDataToGrid();
  51. }
  52. void PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::OnAddButtonClick( wxCommandEvent& event )
  53. {
  54. if( !m_grid->CommitPendingChanges() )
  55. return;
  56. int row = m_grid->GetNumberRows();
  57. TransferDataFromGrid();
  58. TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( "Fieldname" );
  59. newFieldname.m_Visible = false;
  60. m_fields.insert( m_fields.end(), newFieldname );
  61. TransferDataToGrid();
  62. // wx documentation is wrong, SetGridCursor does not make visible.
  63. m_grid->MakeCellVisible( row, 0 );
  64. m_grid->SetGridCursor( row, 0 );
  65. }
  66. void PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::OnDeleteButtonClick( wxCommandEvent& event )
  67. {
  68. if( !m_grid->CommitPendingChanges() )
  69. return;
  70. int curRow = m_grid->GetGridCursorRow();
  71. if( curRow >= 0 && curRow < (int)m_fields.size() )
  72. {
  73. m_fields.erase( m_fields.begin() + curRow );
  74. m_grid->DeleteRows( curRow );
  75. }
  76. m_grid->MakeCellVisible( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
  77. m_grid->SetGridCursor( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
  78. }
  79. bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataToGrid()
  80. {
  81. m_grid->Freeze();
  82. if( m_grid->GetNumberRows() )
  83. m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
  84. m_grid->AppendRows( m_fields.size() );
  85. for( int row = 0; row < m_grid->GetNumberRows(); ++row )
  86. {
  87. m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
  88. m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxT( "1" ) : wxEmptyString );
  89. m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxT( "1" ) : wxEmptyString );
  90. // Set cell properties
  91. m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
  92. // Render the Visible and URL columns as check boxes
  93. m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
  94. m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
  95. m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
  96. m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
  97. m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
  98. m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
  99. }
  100. m_grid->Thaw();
  101. return true;
  102. }
  103. bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataFromGrid()
  104. {
  105. if( !m_grid->CommitPendingChanges() )
  106. return false;
  107. for( int row = 0; row < m_grid->GetNumberRows(); ++row )
  108. {
  109. m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
  110. m_fields[row].m_Visible = ( m_grid->GetCellValue( row, 1 ) != wxEmptyString );
  111. m_fields[row].m_URL = ( m_grid->GetCellValue( row, 2 ) != wxEmptyString );
  112. }
  113. return true;
  114. }
  115. bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataFromWindow()
  116. {
  117. if( !TransferDataFromGrid() )
  118. return false;
  119. m_frame->DeleteAllTemplateFieldNames();
  120. for( const TEMPLATE_FIELDNAME& field : m_fields )
  121. m_frame->AddTemplateFieldName( field );
  122. return true;
  123. }
  124. void PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::AdjustGridColumns( int aWidth )
  125. {
  126. if( aWidth <= 0 )
  127. return;
  128. // Account for scroll bars
  129. aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
  130. m_grid->SetColSize( 0, aWidth - 2 * m_checkboxColWidth );
  131. m_grid->SetColSize( 1, m_checkboxColWidth );
  132. m_grid->SetColSize( 2, m_checkboxColWidth );
  133. }
  134. void PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::OnSizeGrid( wxSizeEvent& event )
  135. {
  136. AdjustGridColumns( event.GetSize().GetX() );
  137. event.Skip();
  138. }