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.

282 lines
7.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2013 CERN
  6. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <dialogs/dialog_plugin_options.h>
  26. #include <grid_tricks.h>
  27. #include <lib_table_base.h>
  28. #include <widgets/wx_grid.h>
  29. #include <widgets/std_bitmap_button.h>
  30. #include <bitmaps.h>
  31. #include <string_utils.h>
  32. #define INITIAL_HELP \
  33. _( "Select an <b>Option Choice</b> in the listbox above, and then click the " \
  34. "<b>Append Selected Option</b> button." )
  35. DIALOG_PLUGIN_OPTIONS::DIALOG_PLUGIN_OPTIONS( wxWindow* aParent,
  36. const wxString& aNickname,
  37. const std::map<std::string, UTF8>& aPluginOptions,
  38. const wxString& aFormattedOptions,
  39. wxString* aResult ) :
  40. DIALOG_PLUGIN_OPTIONS_BASE( aParent ),
  41. m_callers_options( aFormattedOptions ),
  42. m_result( aResult ),
  43. m_choices( aPluginOptions ),
  44. m_initial_help( INITIAL_HELP ),
  45. m_grid_widths_dirty( true )
  46. {
  47. SetTitle( wxString::Format( _( "Options for Library '%s'" ), aNickname ) );
  48. // Give a bit more room for combobox editors
  49. m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
  50. m_grid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
  51. // add Cut, Copy, and Paste to wxGrid
  52. m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
  53. // Option Choices Panel:
  54. if( m_choices.size() )
  55. {
  56. unsigned int row = 0;
  57. for( std::map<std::string, UTF8>::const_iterator it = m_choices.begin();
  58. it != m_choices.end(); ++it, ++row )
  59. {
  60. wxString item = From_UTF8( it->first.c_str() );
  61. m_listbox->InsertItems( 1, &item, row );
  62. }
  63. }
  64. m_html->SetPage( m_initial_help );
  65. // Configure button logos
  66. m_append_button->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
  67. m_delete_button->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
  68. // initial focus on the grid please.
  69. SetInitialFocus( m_grid );
  70. SetupStandardButtons();
  71. }
  72. DIALOG_PLUGIN_OPTIONS ::~DIALOG_PLUGIN_OPTIONS()
  73. {
  74. // destroy GRID_TRICKS before m_grid.
  75. m_grid->PopEventHandler( true );
  76. }
  77. bool DIALOG_PLUGIN_OPTIONS::TransferDataToWindow()
  78. {
  79. if( !DIALOG_SHIM::TransferDataToWindow() )
  80. return false;
  81. // Fill the grid with existing aOptions
  82. std::string options = TO_UTF8( m_callers_options );
  83. std::map<std::string, UTF8>* props = LIB_TABLE::ParseOptions( options );
  84. if( props )
  85. {
  86. if( (int) props->size() > m_grid->GetNumberRows() )
  87. m_grid->AppendRows( props->size() - m_grid->GetNumberRows() );
  88. int row = 0;
  89. for( std::map<std::string, UTF8>::const_iterator it = props->begin(); it != props->end();
  90. ++it, ++row )
  91. {
  92. m_grid->SetCellValue( row, 0, From_UTF8( it->first.c_str() ) );
  93. m_grid->SetCellValue( row, 1, it->second );
  94. }
  95. delete props;
  96. }
  97. return true;
  98. }
  99. bool DIALOG_PLUGIN_OPTIONS::TransferDataFromWindow()
  100. {
  101. if( !m_grid->CommitPendingChanges() )
  102. return false;
  103. if( !DIALOG_SHIM::TransferDataFromWindow() )
  104. return false;
  105. std::map<std::string, UTF8> props;
  106. const int rowCount = m_grid->GetNumberRows();
  107. for( int row = 0; row<rowCount; ++row )
  108. {
  109. std::string name = TO_UTF8( m_grid->GetCellValue( row, 0 ).Trim( false ).Trim() );
  110. UTF8 value = m_grid->GetCellValue( row, 1 ).Trim( false ).Trim();
  111. if( name.size() )
  112. {
  113. props[name] = value;
  114. }
  115. }
  116. *m_result = LIB_TABLE::FormatOptions( &props ).wx_str();
  117. return true;
  118. }
  119. int DIALOG_PLUGIN_OPTIONS::appendRow()
  120. {
  121. int row = m_grid->GetNumberRows();
  122. m_grid->AppendRows( 1 );
  123. // wx documentation is wrong, SetGridCursor does not make visible.
  124. m_grid->MakeCellVisible( row, 0 );
  125. m_grid->SetGridCursor( row, 0 );
  126. return row;
  127. }
  128. void DIALOG_PLUGIN_OPTIONS::appendOption()
  129. {
  130. int selected_row = m_listbox->GetSelection();
  131. if( selected_row != wxNOT_FOUND )
  132. {
  133. wxString option = m_listbox->GetString( selected_row );
  134. int row_count = m_grid->GetNumberRows();
  135. int row;
  136. for( row=0; row<row_count; ++row )
  137. {
  138. wxString col0 = m_grid->GetCellValue( row, 0 );
  139. if( !col0 ) // empty col0
  140. break;
  141. }
  142. if( row == row_count )
  143. row = appendRow();
  144. m_grid->SetCellValue( row, 0, option );
  145. m_grid_widths_dirty = true;
  146. }
  147. }
  148. //-----<event handlers>------------------------------------------------------
  149. void DIALOG_PLUGIN_OPTIONS::onListBoxItemSelected( wxCommandEvent& event )
  150. {
  151. // change the help text based on the m_listbox selection:
  152. if( event.IsSelection() )
  153. {
  154. std::string option = TO_UTF8( event.GetString() );
  155. if( auto it = m_choices.find( option ); it != m_choices.end() )
  156. m_html->SetPage( it->second );
  157. else
  158. m_html->SetPage( m_initial_help );
  159. }
  160. }
  161. void DIALOG_PLUGIN_OPTIONS::onListBoxItemDoubleClicked( wxCommandEvent& event )
  162. {
  163. appendOption();
  164. }
  165. void DIALOG_PLUGIN_OPTIONS::onAppendOption( wxCommandEvent& )
  166. {
  167. if( !m_grid->CommitPendingChanges() )
  168. return;
  169. appendOption();
  170. }
  171. void DIALOG_PLUGIN_OPTIONS::onAppendRow( wxCommandEvent& )
  172. {
  173. if( !m_grid->CommitPendingChanges() )
  174. return;
  175. appendRow();
  176. }
  177. void DIALOG_PLUGIN_OPTIONS::onDeleteRow( wxCommandEvent& )
  178. {
  179. if( !m_grid->CommitPendingChanges() )
  180. return;
  181. int curRow = m_grid->GetGridCursorRow();
  182. m_grid->DeleteRows( curRow );
  183. m_grid_widths_dirty = true;
  184. curRow = std::max( 0, curRow - 1 );
  185. m_grid->MakeCellVisible( curRow, m_grid->GetGridCursorCol() );
  186. m_grid->SetGridCursor( curRow, m_grid->GetGridCursorCol() );
  187. }
  188. void DIALOG_PLUGIN_OPTIONS::onGridCellChange( wxGridEvent& aEvent )
  189. {
  190. m_grid_widths_dirty = true;
  191. aEvent.Skip();
  192. }
  193. void DIALOG_PLUGIN_OPTIONS::onUpdateUI( wxUpdateUIEvent& )
  194. {
  195. if( m_grid_widths_dirty && !m_grid->IsCellEditControlShown() )
  196. {
  197. int width = m_grid->GetClientRect().GetWidth();
  198. m_grid->AutoSizeColumn( 0 );
  199. m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
  200. m_grid->SetColSize( 1, std::max( 120, width - m_grid->GetColSize( 0 ) ) );
  201. m_grid_widths_dirty = false;
  202. }
  203. }
  204. void DIALOG_PLUGIN_OPTIONS::onSize( wxSizeEvent& aEvent )
  205. {
  206. m_grid_widths_dirty = true;
  207. aEvent.Skip();
  208. }