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.

342 lines
10 KiB

12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
  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-2011 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. /**
  25. * @file dialog_eeschema_options.cpp
  26. */
  27. #include <fctsys.h>
  28. #include <class_base_screen.h>
  29. #include <dialog_eeschema_options.h>
  30. #include "wx/settings.h"
  31. DIALOG_EESCHEMA_OPTIONS::DIALOG_EESCHEMA_OPTIONS( wxWindow* parent ) :
  32. DIALOG_EESCHEMA_OPTIONS_BASE( parent )
  33. {
  34. m_choiceUnits->SetFocus();
  35. m_sdbSizer1OK->SetDefault();
  36. // Dialog should not shrink beyond it's minimal size.
  37. GetSizer()->SetSizeHints( this );
  38. wxListItem col0;
  39. col0.SetId( 0 );
  40. col0.SetText( _( "Field Name" ) );
  41. wxListItem col1;
  42. col1.SetId( 1 );
  43. col1.SetText( _( "Default Value" ) );
  44. wxListItem col2;
  45. col2.SetId( 2 );
  46. col2.SetText( _( "Visible" ) );
  47. templateFieldListCtrl->InsertColumn( 0, col0 );
  48. templateFieldListCtrl->InsertColumn( 1, col1 );
  49. templateFieldListCtrl->InsertColumn( 2, col2 );
  50. templateFieldListCtrl->SetColumnWidth( 0, templateFieldListCtrl->GetSize().GetWidth() / 3.5 );
  51. templateFieldListCtrl->SetColumnWidth( 1, templateFieldListCtrl->GetSize().GetWidth() / 3.5 );
  52. templateFieldListCtrl->SetColumnWidth( 2, templateFieldListCtrl->GetSize().GetWidth() / 3.5 );
  53. // Invalid field selected and don't ignore selection events because
  54. // they'll be from the user
  55. selectedField = 0;
  56. selectionValid = false;
  57. ignoreSelection = false;
  58. // Make sure we select the first tab of the options tab page
  59. m_notebook1->SetSelection( 0 );
  60. // Connect the edit controls for the template field names to the kill focus event which
  61. // doesn't propogate, hence the need to connect it here.
  62. fieldNameTextCtrl->Connect( wxEVT_KILL_FOCUS,
  63. wxFocusEventHandler( DIALOG_EESCHEMA_OPTIONS::OnEditControlKillFocus ), NULL, this );
  64. fieldDefaultValueTextCtrl->Connect( wxEVT_KILL_FOCUS,
  65. wxFocusEventHandler( DIALOG_EESCHEMA_OPTIONS::OnEditControlKillFocus ), NULL, this );
  66. fieldVisibleCheckbox->Connect( wxEVT_KILL_FOCUS,
  67. wxFocusEventHandler( DIALOG_EESCHEMA_OPTIONS::OnEditControlKillFocus ), NULL, this );
  68. }
  69. void DIALOG_EESCHEMA_OPTIONS::SetUnits( const wxArrayString& units, int select )
  70. {
  71. wxASSERT( units.GetCount() > 0
  72. && ( select >= 0 && (size_t) select < units.GetCount() ) );
  73. m_choiceUnits->Append( units );
  74. m_choiceUnits->SetSelection( select );
  75. }
  76. void DIALOG_EESCHEMA_OPTIONS::SetRefIdSeparator( wxChar aSep, wxChar aFirstId)
  77. {
  78. // m_choiceSeparatorRefId displays one of
  79. // "A" ".A" "-A" "_A" ".1" "-1" "_1" option
  80. int sel = 0;
  81. switch( aSep )
  82. {
  83. default:
  84. case 0:
  85. aFirstId = 'A'; // cannot use a number without separator
  86. break;
  87. case '.':
  88. sel = 1;
  89. break;
  90. case '-':
  91. sel = 2;
  92. break;
  93. case '_':
  94. sel = 3;
  95. break;
  96. }
  97. if( aFirstId == '1' )
  98. sel = 4;
  99. m_choiceSeparatorRefId->SetSelection( sel );
  100. }
  101. void DIALOG_EESCHEMA_OPTIONS::GetRefIdSeparator( int& aSep, int& aFirstId)
  102. {
  103. // m_choiceSeparatorRefId displays one of
  104. // "A" ".A" "-A" "_A" ".1" "-1" "_1" option
  105. aFirstId = 'A';
  106. switch( m_choiceSeparatorRefId->GetSelection() )
  107. {
  108. default:
  109. case 0: aSep = 0; break;
  110. case 1: aSep = '.'; break;
  111. case 2: aSep = '-'; break;
  112. case 3: aSep = '_'; break;
  113. case 4: aFirstId = '1'; aSep = '.'; break;
  114. case 5: aFirstId = '1'; aSep = '-'; break;
  115. case 6: aFirstId = '1'; aSep = '_'; break;
  116. }
  117. }
  118. void DIALOG_EESCHEMA_OPTIONS::SetGridSizes( const GRIDS& aGridSizes, int aGridId )
  119. {
  120. wxASSERT( aGridSizes.size() > 0 );
  121. int select = wxNOT_FOUND;
  122. for( size_t i = 0; i < aGridSizes.size(); i++ )
  123. {
  124. wxString tmp;
  125. tmp.Printf( wxT( "%0.1f" ), aGridSizes[i].m_Size.x );
  126. m_choiceGridSize->Append( tmp );
  127. if( aGridSizes[i].m_Id == aGridId )
  128. select = (int) i;
  129. }
  130. m_choiceGridSize->SetSelection( select );
  131. }
  132. void DIALOG_EESCHEMA_OPTIONS::RefreshTemplateFieldView( void )
  133. {
  134. // Delete all items in the template field list control and add all of the
  135. // current template fields
  136. templateFieldListCtrl->DeleteAllItems();
  137. // Loop through the template fieldnames and add then to the list control
  138. for( TEMPLATE_FIELDNAMES::iterator fld = templateFields.begin();
  139. fld != templateFields.end(); ++fld )
  140. {
  141. long itemindex = templateFieldListCtrl->InsertItem(
  142. templateFieldListCtrl->GetItemCount(), fld->m_Name );
  143. templateFieldListCtrl->SetItem( itemindex, 1, fld->m_Value );
  144. templateFieldListCtrl->SetItem( itemindex, 2,
  145. ( fld->m_Visible == true ) ? _( "Visible" ) : _( "Hidden" ) );
  146. }
  147. }
  148. void DIALOG_EESCHEMA_OPTIONS::SelectTemplateField( int aItem )
  149. {
  150. // Only select valid items!
  151. if( !selectionValid || ( aItem >= templateFieldListCtrl->GetItemCount() ) )
  152. return;
  153. // Make sure we select the new item
  154. ignoreSelection = true;
  155. templateFieldListCtrl->SetItemState( aItem, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
  156. }
  157. void DIALOG_EESCHEMA_OPTIONS::OnAddButtonClick( wxCommandEvent& event )
  158. {
  159. // If there is currently a valid selection, copy the edit panel to the
  160. // selected field so as not to lose the data
  161. if( selectionValid && ( selectedField < templateFields.size() ) )
  162. copyPanelToSelected();
  163. // Add a new fieldname to the fieldname list
  164. TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( "Fieldname" );
  165. newFieldname.m_Value = wxT( "Value" );
  166. newFieldname.m_Visible = false;
  167. templateFields.push_back( newFieldname );
  168. // Select the newly added field and then copy that data to the edit panel.
  169. // Make sure any previously selected state is cleared and then select the
  170. // new field
  171. selectedField = templateFields.size() - 1;
  172. selectionValid = true;
  173. // Update the display to reflect the new data
  174. RefreshTemplateFieldView();
  175. copySelectedToPanel();
  176. // Make sure we select the new item
  177. SelectTemplateField( selectedField );
  178. event.Skip();
  179. }
  180. void DIALOG_EESCHEMA_OPTIONS::OnDeleteButtonClick( wxCommandEvent& event )
  181. {
  182. // If there is currently a valid selection, delete the template field from
  183. // the template field list
  184. if( selectionValid && ( selectedField < templateFields.size() ) )
  185. {
  186. // Delete the fieldname from the fieldname list
  187. templateFields.erase( templateFields.begin() + selectedField );
  188. // If the selectedField is still not in the templateField range now,
  189. // make sure we stay in range and when there are no fields present
  190. // move to -1
  191. if( selectedField >= templateFields.size() )
  192. selectedField = templateFields.size() - 1;
  193. // Update the display to reflect the new data
  194. RefreshTemplateFieldView();
  195. copySelectedToPanel();
  196. // Make sure after the refresh that the selected item is correct
  197. SelectTemplateField( selectedField );
  198. }
  199. event.Skip();
  200. }
  201. void DIALOG_EESCHEMA_OPTIONS::copyPanelToSelected( void )
  202. {
  203. if( !selectionValid || ( selectedField >= templateFields.size() ) )
  204. return;
  205. // Update the template field from the edit panel
  206. templateFields[selectedField].m_Name = fieldNameTextCtrl->GetValue();
  207. templateFields[selectedField].m_Value = fieldDefaultValueTextCtrl->GetValue();
  208. templateFields[selectedField].m_Visible = fieldVisibleCheckbox->GetValue();
  209. }
  210. void DIALOG_EESCHEMA_OPTIONS::OnEditControlKillFocus( wxFocusEvent& event )
  211. {
  212. // Update the data + UI
  213. copyPanelToSelected();
  214. RefreshTemplateFieldView();
  215. SelectTemplateField( selectedField );
  216. event.Skip();
  217. }
  218. void DIALOG_EESCHEMA_OPTIONS::copySelectedToPanel( void )
  219. {
  220. if( !selectionValid || ( selectedField >= templateFields.size() ) )
  221. return;
  222. // Update the panel data from the selected template field
  223. fieldNameTextCtrl->SetValue( templateFields[selectedField].m_Name );
  224. fieldDefaultValueTextCtrl->SetValue( templateFields[selectedField].m_Value );
  225. fieldVisibleCheckbox->SetValue( templateFields[selectedField].m_Visible );
  226. }
  227. void DIALOG_EESCHEMA_OPTIONS::OnTemplateFieldSelected( wxListEvent& event )
  228. {
  229. // If the class has generated the event and asked to ignore it, honour that and reset the
  230. // ignore flag for the next user event.
  231. if( ignoreSelection )
  232. {
  233. ignoreSelection = false;
  234. event.Skip();
  235. return;
  236. }
  237. // Before getting the new field data, make sure we save the old!
  238. copyPanelToSelected();
  239. // Now update the selected field and copy the data from the field to the
  240. // edit panel
  241. selectedField = event.GetIndex();
  242. selectionValid = true;
  243. copySelectedToPanel();
  244. // Refresh the template field view - this deletes all fields and then
  245. // re-fills the entire data grid. It then re-selects the currently
  246. // selected field. This will be recursive, so disable this event while
  247. // we refresh the view
  248. RefreshTemplateFieldView();
  249. // If an item was selected, make sure we re-select it, or at least the
  250. // same position in the grid
  251. SelectTemplateField( selectedField );
  252. event.Skip();
  253. }
  254. void DIALOG_EESCHEMA_OPTIONS::SetTemplateFields( const TEMPLATE_FIELDNAMES& aFields )
  255. {
  256. // Set the template fields object
  257. templateFields = aFields;
  258. // Refresh the view
  259. RefreshTemplateFieldView();
  260. }
  261. TEMPLATE_FIELDNAMES DIALOG_EESCHEMA_OPTIONS::GetTemplateFields( void )
  262. {
  263. return templateFields;
  264. }