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.

661 lines
21 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  6. * Copyright (C) 2015 Dick Hollenbeck, dick@softplc.com
  7. * Copyright (C) 2004-2021 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <3d_viewer/eda_3d_viewer_frame.h>
  27. #include <bitmaps.h>
  28. #include <board_commit.h>
  29. #include <board_design_settings.h>
  30. #include <footprint.h>
  31. #include <confirm.h>
  32. #include <dialogs/dialog_text_entry.h>
  33. #include <filename_resolver.h>
  34. #include <pcb_edit_frame.h>
  35. #include <pcbnew_settings.h>
  36. #include <pgm_base.h>
  37. #include <validators.h>
  38. #include <kiplatform/ui.h>
  39. #include <widgets/grid_text_button_helpers.h>
  40. #include <widgets/text_ctrl_eval.h>
  41. #include <widgets/wx_grid.h>
  42. #include <settings/settings_manager.h>
  43. #include <panel_fp_properties_3d_model.h>
  44. #include "dialogs/3d_cache_dialogs.h"
  45. #include "dialogs/panel_preview_3d_model.h"
  46. #include <dialog_footprint_properties.h>
  47. int DIALOG_FOOTPRINT_PROPERTIES::m_page = 0; // remember the last open page during session
  48. DIALOG_FOOTPRINT_PROPERTIES::DIALOG_FOOTPRINT_PROPERTIES( PCB_EDIT_FRAME* aParent,
  49. FOOTPRINT* aFootprint ) :
  50. DIALOG_FOOTPRINT_PROPERTIES_BASE( aParent ),
  51. m_frame( aParent ),
  52. m_footprint( aFootprint ),
  53. m_posX( aParent, m_XPosLabel, m_ModPositionX, m_XPosUnit ),
  54. m_posY( aParent, m_YPosLabel, m_ModPositionY, m_YPosUnit ),
  55. m_orientation( aParent, m_orientationLabel, m_orientationCtrl, nullptr ),
  56. m_netClearance( aParent, m_NetClearanceLabel, m_NetClearanceCtrl, m_NetClearanceUnits ),
  57. m_solderMask( aParent, m_SolderMaskMarginLabel, m_SolderMaskMarginCtrl,
  58. m_SolderMaskMarginUnits ),
  59. m_solderPaste( aParent, m_SolderPasteMarginLabel, m_SolderPasteMarginCtrl,
  60. m_SolderPasteMarginUnits ),
  61. m_solderPasteRatio( aParent, m_PasteMarginRatioLabel, m_PasteMarginRatioCtrl,
  62. m_PasteMarginRatioUnits ),
  63. m_returnValue( FP_PROPS_CANCEL ),
  64. m_initialized( false )
  65. {
  66. // Create the 3D models page
  67. m_3dPanel = new PANEL_FP_PROPERTIES_3D_MODEL( m_frame, m_footprint, this, m_NoteBook );
  68. m_NoteBook->AddPage( m_3dPanel, _("3D Models"), false );
  69. // Configure display origin transforms
  70. m_posX.SetCoordType( ORIGIN_TRANSFORMS::ABS_X_COORD );
  71. m_posY.SetCoordType( ORIGIN_TRANSFORMS::ABS_Y_COORD );
  72. m_texts = new FP_TEXT_GRID_TABLE( m_frame );
  73. m_delayedErrorMessage = wxEmptyString;
  74. m_delayedFocusGrid = nullptr;
  75. m_delayedFocusRow = -1;
  76. m_delayedFocusColumn = -1;
  77. m_initialFocus = false;
  78. // Give an icon
  79. wxIcon icon;
  80. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_modedit ) );
  81. SetIcon( icon );
  82. // Give a bit more room for combobox editors
  83. m_itemsGrid->SetDefaultRowSize( m_itemsGrid->GetDefaultRowSize() + 4 );
  84. m_itemsGrid->SetTable( m_texts );
  85. m_itemsGrid->PushEventHandler( new GRID_TRICKS( m_itemsGrid ) );
  86. PCBNEW_SETTINGS* cfg = m_frame->GetPcbNewSettings();
  87. // Show/hide text item columns according to the user's preference
  88. m_itemsGrid->ShowHideColumns( cfg->m_FootprintTextShownColumns );
  89. m_orientation.SetUnits( EDA_UNITS::DEGREES );
  90. m_orientation.SetPrecision( 3 );
  91. // Set predefined rotations in combo dropdown, according to the locale floating point
  92. // separator notation
  93. double rot_list[] = { 0.0, 90.0, -90.0, 180.0 };
  94. for( size_t ii = 0; ii < m_orientationCtrl->GetCount() && ii < 4; ++ii )
  95. m_orientationCtrl->SetString( ii, wxString::Format( "%.1f", rot_list[ii] ) );
  96. // Set font size for items showing long strings:
  97. wxFont infoFont = KIUI::GetInfoFont( this );
  98. m_libraryIDLabel->SetFont( infoFont );
  99. m_tcLibraryID->SetFont( infoFont );
  100. infoFont.SetStyle( wxFONTSTYLE_ITALIC );
  101. m_staticTextInfoCopper->SetFont( infoFont );
  102. m_staticTextInfoPaste->SetFont( infoFont );
  103. m_NoteBook->SetSelection( m_page );
  104. if( m_page == 0 )
  105. {
  106. m_delayedFocusGrid = m_itemsGrid;
  107. m_delayedFocusRow = 0;
  108. m_delayedFocusColumn = 0;
  109. }
  110. else if( m_page == 1 )
  111. {
  112. SetInitialFocus( m_NetClearanceCtrl );
  113. }
  114. m_solderPaste.SetNegativeZero();
  115. m_solderPasteRatio.SetUnits( EDA_UNITS::PERCENT );
  116. m_solderPasteRatio.SetNegativeZero();
  117. // Configure button logos
  118. m_bpAdd->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
  119. m_bpDelete->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
  120. SetupStandardButtons();
  121. finishDialogSettings();
  122. m_initialized = true;
  123. }
  124. DIALOG_FOOTPRINT_PROPERTIES::~DIALOG_FOOTPRINT_PROPERTIES()
  125. {
  126. m_frame->Settings().m_FootprintTextShownColumns = m_itemsGrid->GetShownColumns().ToStdString();
  127. // Prevents crash bug in wxGrid's d'tor
  128. m_itemsGrid->DestroyTable( m_texts );
  129. // Delete the GRID_TRICKS.
  130. m_itemsGrid->PopEventHandler( true );
  131. // free the memory used by all models, otherwise models which were
  132. // browsed but not used would consume memory
  133. Prj().Get3DCacheManager()->FlushCache( false );
  134. // the GL canvas has to be visible before it is destroyed
  135. m_page = m_NoteBook->GetSelection();
  136. m_NoteBook->SetSelection( 1 );
  137. }
  138. void DIALOG_FOOTPRINT_PROPERTIES::EditFootprint( wxCommandEvent& )
  139. {
  140. if( TransferDataFromWindow() )
  141. {
  142. m_returnValue = FP_PROPS_EDIT_BOARD_FP;
  143. Close();
  144. }
  145. }
  146. void DIALOG_FOOTPRINT_PROPERTIES::EditLibraryFootprint( wxCommandEvent& )
  147. {
  148. if( TransferDataFromWindow() )
  149. {
  150. m_returnValue = FP_PROPS_EDIT_LIBRARY_FP;
  151. Close();
  152. }
  153. }
  154. void DIALOG_FOOTPRINT_PROPERTIES::UpdateFootprint( wxCommandEvent& )
  155. {
  156. if( TransferDataFromWindow() )
  157. {
  158. m_returnValue = FP_PROPS_UPDATE_FP;
  159. Close();
  160. }
  161. }
  162. void DIALOG_FOOTPRINT_PROPERTIES::ChangeFootprint( wxCommandEvent& )
  163. {
  164. if( TransferDataFromWindow() )
  165. {
  166. m_returnValue = FP_PROPS_CHANGE_FP;
  167. Close();
  168. }
  169. }
  170. bool DIALOG_FOOTPRINT_PROPERTIES::TransferDataToWindow()
  171. {
  172. if( !wxDialog::TransferDataToWindow() )
  173. return false;
  174. if( !m_PanelGeneral->TransferDataToWindow() )
  175. return false;
  176. // Add the models to the panel
  177. if( !m_3dPanel->TransferDataToWindow() )
  178. return false;
  179. // Footprint Texts
  180. m_texts->push_back( m_footprint->Reference() );
  181. m_texts->push_back( m_footprint->Value() );
  182. for( BOARD_ITEM* item : m_footprint->GraphicalItems() )
  183. {
  184. FP_TEXT* textItem = dyn_cast<FP_TEXT*>( item );
  185. if( textItem )
  186. m_texts->push_back( *textItem );
  187. }
  188. // notify the grid
  189. wxGridTableMessage tmsg( m_texts, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_texts->GetNumberRows() );
  190. m_itemsGrid->ProcessTableMessage( tmsg );
  191. // Footprint Properties
  192. m_posX.SetValue( m_footprint->GetPosition().x );
  193. m_posY.SetValue( m_footprint->GetPosition().y );
  194. m_BoardSideCtrl->SetSelection( (m_footprint->GetLayer() == B_Cu) ? 1 : 0 );
  195. m_orientation.SetDoubleValue( m_footprint->GetOrientation() );
  196. m_cbLocked->SetValue( m_footprint->IsLocked() );
  197. m_cbLocked->SetToolTip( _( "Locked footprints cannot be freely moved and oriented on the "
  198. "canvas and can only be selected when the 'Locked items' checkbox "
  199. "is enabled in the selection filter." ) );
  200. if( m_footprint->GetAttributes() & FP_THROUGH_HOLE )
  201. m_componentType->SetSelection( 0 );
  202. else if( m_footprint->GetAttributes() & FP_SMD )
  203. m_componentType->SetSelection( 1 );
  204. else
  205. m_componentType->SetSelection( 2 );
  206. m_boardOnly->SetValue( m_footprint->GetAttributes() & FP_BOARD_ONLY );
  207. m_excludeFromPosFiles->SetValue( m_footprint->GetAttributes() & FP_EXCLUDE_FROM_POS_FILES );
  208. m_excludeFromBOM->SetValue( m_footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM );
  209. m_noCourtyards->SetValue( m_footprint->GetAttributes() & FP_ALLOW_MISSING_COURTYARD );
  210. // Local Clearances
  211. m_netClearance.SetValue( m_footprint->GetLocalClearance() );
  212. m_solderMask.SetValue( m_footprint->GetLocalSolderMaskMargin() );
  213. m_solderPaste.SetValue( m_footprint->GetLocalSolderPasteMargin() );
  214. m_solderPasteRatio.SetDoubleValue( m_footprint->GetLocalSolderPasteMarginRatio() * 100.0 );
  215. m_allowSolderMaskBridges->SetValue( m_footprint->GetAttributes() & FP_ALLOW_SOLDERMASK_BRIDGES );
  216. switch( m_footprint->GetZoneConnection() )
  217. {
  218. default:
  219. case ZONE_CONNECTION::INHERITED: m_ZoneConnectionChoice->SetSelection( 0 ); break;
  220. case ZONE_CONNECTION::FULL: m_ZoneConnectionChoice->SetSelection( 1 ); break;
  221. case ZONE_CONNECTION::THERMAL: m_ZoneConnectionChoice->SetSelection( 2 ); break;
  222. case ZONE_CONNECTION::NONE: m_ZoneConnectionChoice->SetSelection( 3 ); break;
  223. }
  224. // Show the footprint's FPID.
  225. m_tcLibraryID->SetValue( m_footprint->GetFPID().Format() );
  226. for( int col = 0; col < m_itemsGrid->GetNumberCols(); col++ )
  227. {
  228. m_itemsGrid->SetColMinimalWidth( col, m_itemsGrid->GetVisibleWidth( col, true, false,
  229. false ) );
  230. // Adjust the column size.
  231. int col_size = m_itemsGrid->GetVisibleWidth( col, true, true, false );
  232. if( col == FPT_LAYER ) // This one's a drop-down. Check all possible values.
  233. {
  234. BOARD* board = m_footprint->GetBoard();
  235. for( PCB_LAYER_ID layer : board->GetEnabledLayers().Seq() )
  236. col_size = std::max( col_size, GetTextExtent( board->GetLayerName( layer ) ).x );
  237. // And the swatch:
  238. col_size += 20;
  239. }
  240. if( m_itemsGrid->IsColShown( col ) )
  241. m_itemsGrid->SetColSize( col, col_size );
  242. }
  243. m_itemsGrid->SetRowLabelSize( m_itemsGrid->GetVisibleWidth( -1, false, true, true ) );
  244. Layout();
  245. adjustGridColumns();
  246. return true;
  247. }
  248. bool DIALOG_FOOTPRINT_PROPERTIES::Validate()
  249. {
  250. if( !m_itemsGrid->CommitPendingChanges() )
  251. return false;
  252. if( !DIALOG_SHIM::Validate() )
  253. return false;
  254. // Check for empty texts.
  255. for( size_t i = 2; i < m_texts->size(); ++i )
  256. {
  257. FP_TEXT& text = m_texts->at( i );
  258. if( text.GetText().IsEmpty() )
  259. {
  260. if( m_NoteBook->GetSelection() != 0 )
  261. m_NoteBook->SetSelection( 0 );
  262. m_delayedFocusGrid = m_itemsGrid;
  263. m_delayedErrorMessage = _( "Text items must have some content." );
  264. m_delayedFocusColumn = FPT_TEXT;
  265. m_delayedFocusRow = i;
  266. return false;
  267. }
  268. }
  269. if( !m_netClearance.Validate( 0, INT_MAX ) )
  270. return false;
  271. return true;
  272. }
  273. bool DIALOG_FOOTPRINT_PROPERTIES::TransferDataFromWindow()
  274. {
  275. if( !Validate() )
  276. return false;
  277. if( !m_itemsGrid->CommitPendingChanges() )
  278. return false;
  279. // This only commits the editor, model updating is done below so it is inside
  280. // the commit
  281. if( !m_3dPanel->TransferDataFromWindow() )
  282. return false;
  283. auto view = m_frame->GetCanvas()->GetView();
  284. BOARD_COMMIT commit( m_frame );
  285. commit.Modify( m_footprint );
  286. // copy reference and value
  287. m_footprint->Reference() = m_texts->at( 0 );
  288. m_footprint->Value() = m_texts->at( 1 );
  289. size_t i = 2;
  290. for( BOARD_ITEM* item : m_footprint->GraphicalItems() )
  291. {
  292. FP_TEXT* textItem = dyn_cast<FP_TEXT*>( item );
  293. if( textItem )
  294. {
  295. // copy grid table entries till we run out, then delete any remaining texts
  296. if( i < m_texts->size() )
  297. *textItem = m_texts->at( i++ );
  298. else
  299. textItem->DeleteStructure();
  300. }
  301. }
  302. // if there are still grid table entries, create new texts for them
  303. while( i < m_texts->size() )
  304. {
  305. auto newText = new FP_TEXT( m_texts->at( i++ ) );
  306. m_footprint->Add( newText, ADD_MODE::APPEND );
  307. view->Add( newText );
  308. }
  309. // Initialize masks clearances
  310. m_footprint->SetLocalClearance( m_netClearance.GetValue() );
  311. m_footprint->SetLocalSolderMaskMargin( m_solderMask.GetValue() );
  312. m_footprint->SetLocalSolderPasteMargin( m_solderPaste.GetValue() );
  313. m_footprint->SetLocalSolderPasteMarginRatio( m_solderPasteRatio.GetDoubleValue() / 100.0 );
  314. switch( m_ZoneConnectionChoice->GetSelection() )
  315. {
  316. default:
  317. case 0: m_footprint->SetZoneConnection( ZONE_CONNECTION::INHERITED ); break;
  318. case 1: m_footprint->SetZoneConnection( ZONE_CONNECTION::FULL ); break;
  319. case 2: m_footprint->SetZoneConnection( ZONE_CONNECTION::THERMAL ); break;
  320. case 3: m_footprint->SetZoneConnection( ZONE_CONNECTION::NONE ); break;
  321. }
  322. // Set Footprint Position
  323. wxPoint pos( m_posX.GetValue(), m_posY.GetValue() );
  324. m_footprint->SetPosition( pos );
  325. m_footprint->SetLocked( m_cbLocked->GetValue() );
  326. int attributes = 0;
  327. switch( m_componentType->GetSelection() )
  328. {
  329. case 0: attributes |= FP_THROUGH_HOLE; break;
  330. case 1: attributes |= FP_SMD; break;
  331. default: break;
  332. }
  333. if( m_boardOnly->GetValue() )
  334. attributes |= FP_BOARD_ONLY;
  335. if( m_excludeFromPosFiles->GetValue() )
  336. attributes |= FP_EXCLUDE_FROM_POS_FILES;
  337. if( m_excludeFromBOM->GetValue() )
  338. attributes |= FP_EXCLUDE_FROM_BOM;
  339. if( m_noCourtyards->GetValue() )
  340. attributes |= FP_ALLOW_MISSING_COURTYARD;
  341. if( m_allowSolderMaskBridges->GetValue() )
  342. attributes |= FP_ALLOW_SOLDERMASK_BRIDGES;
  343. m_footprint->SetAttributes( attributes );
  344. double orient = m_orientation.GetDoubleValue();
  345. if( m_footprint->GetOrientation() != orient )
  346. m_footprint->Rotate( m_footprint->GetPosition(), orient - m_footprint->GetOrientation() );
  347. // Set component side, that also have effect on the fields positions on board
  348. bool change_layer = false;
  349. if( m_BoardSideCtrl->GetSelection() == 0 ) // layer req = COMPONENT
  350. {
  351. if( m_footprint->GetLayer() == B_Cu )
  352. change_layer = true;
  353. }
  354. else if( m_footprint->GetLayer() == F_Cu )
  355. change_layer = true;
  356. if( change_layer )
  357. m_footprint->Flip( m_footprint->GetPosition(), m_frame->Settings().m_FlipLeftRight );
  358. // Copy the models from the panel to the footprint
  359. std::vector<FP_3DMODEL>& panelList = m_3dPanel->GetModelList();
  360. std::vector<FP_3DMODEL>* fpList = &m_footprint->Models();
  361. fpList->clear();
  362. fpList->insert( fpList->end(), panelList.begin(), panelList.end() );
  363. // This is a simple edit, we must create an undo entry
  364. if( m_footprint->GetEditFlags() == 0 ) // i.e. not edited, or moved
  365. commit.Push( _( "Modify footprint properties" ) );
  366. m_returnValue = FP_PROPS_OK;
  367. return true;
  368. }
  369. void DIALOG_FOOTPRINT_PROPERTIES::OnAddField( wxCommandEvent& )
  370. {
  371. if( !m_itemsGrid->CommitPendingChanges() )
  372. return;
  373. const BOARD_DESIGN_SETTINGS& dsnSettings = m_frame->GetDesignSettings();
  374. FP_TEXT textItem( m_footprint );
  375. // Set active layer if legal; otherwise copy layer from previous text item
  376. if( LSET::AllTechMask().test( m_frame->GetActiveLayer() ) )
  377. textItem.SetLayer( m_frame->GetActiveLayer() );
  378. else
  379. textItem.SetLayer( m_texts->at( m_texts->size() - 1 ).GetLayer() );
  380. textItem.SetTextSize( dsnSettings.GetTextSize( textItem.GetLayer() ) );
  381. textItem.SetTextThickness( dsnSettings.GetTextThickness( textItem.GetLayer() ) );
  382. textItem.SetItalic( dsnSettings.GetTextItalic( textItem.GetLayer() ) );
  383. textItem.SetKeepUpright( dsnSettings.GetTextUpright( textItem.GetLayer() ) );
  384. textItem.SetMirrored( IsBackLayer( textItem.GetLayer() ) );
  385. m_texts->push_back( textItem );
  386. // notify the grid
  387. wxGridTableMessage msg( m_texts, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
  388. m_itemsGrid->ProcessTableMessage( msg );
  389. m_itemsGrid->SetFocus();
  390. m_itemsGrid->MakeCellVisible( m_texts->size() - 1, 0 );
  391. m_itemsGrid->SetGridCursor( m_texts->size() - 1, 0 );
  392. m_itemsGrid->EnableCellEditControl( true );
  393. m_itemsGrid->ShowCellEditControl();
  394. }
  395. void DIALOG_FOOTPRINT_PROPERTIES::OnDeleteField( wxCommandEvent& )
  396. {
  397. if( !m_itemsGrid->CommitPendingChanges() )
  398. return;
  399. wxArrayInt selectedRows = m_itemsGrid->GetSelectedRows();
  400. if( selectedRows.empty() && m_itemsGrid->GetGridCursorRow() >= 0 )
  401. selectedRows.push_back( m_itemsGrid->GetGridCursorRow() );
  402. if( selectedRows.empty() )
  403. return;
  404. for( int row : selectedRows )
  405. {
  406. if( row < 2 )
  407. {
  408. DisplayError( nullptr, _( "Reference and value are mandatory." ) );
  409. return;
  410. }
  411. }
  412. // Reverse sort so deleting a row doesn't change the indexes of the other rows.
  413. selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
  414. for( int row : selectedRows )
  415. {
  416. m_texts->erase( m_texts->begin() + row );
  417. // notify the grid
  418. wxGridTableMessage msg( m_texts, wxGRIDTABLE_NOTIFY_ROWS_DELETED, row, 1 );
  419. m_itemsGrid->ProcessTableMessage( msg );
  420. if( m_itemsGrid->GetNumberRows() > 0 )
  421. {
  422. m_itemsGrid->MakeCellVisible( std::max( 0, row-1 ), m_itemsGrid->GetGridCursorCol() );
  423. m_itemsGrid->SetGridCursor( std::max( 0, row-1 ), m_itemsGrid->GetGridCursorCol() );
  424. }
  425. }
  426. }
  427. void DIALOG_FOOTPRINT_PROPERTIES::adjustGridColumns()
  428. {
  429. // Account for scroll bars
  430. int itemsWidth = KIPLATFORM::UI::GetUnobscuredSize( m_itemsGrid ).x;
  431. itemsWidth -= m_itemsGrid->GetRowLabelSize();
  432. for( int i = 1; i < m_itemsGrid->GetNumberCols(); i++ )
  433. itemsWidth -= m_itemsGrid->GetColSize( i );
  434. if( itemsWidth > 0 )
  435. {
  436. m_itemsGrid->SetColSize( 0, std::max( itemsWidth,
  437. m_itemsGrid->GetVisibleWidth( 0, true, false, false ) ) );
  438. }
  439. // Update the width of the 3D panel
  440. m_3dPanel->AdjustGridColumnWidths();
  441. }
  442. void DIALOG_FOOTPRINT_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& )
  443. {
  444. if( !m_initialized )
  445. return;
  446. if( !m_itemsGrid->IsCellEditControlShown() )
  447. adjustGridColumns();
  448. // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
  449. // even when the original validation was triggered from a killFocus event, and so
  450. // that the corresponding notebook page can be shown in the background when triggered
  451. // from an OK.
  452. if( m_delayedFocusRow >= 0 )
  453. {
  454. // We will re-enter this routine if an error dialog is displayed, so make sure we
  455. // zero out our member variables first.
  456. wxGrid* grid = m_delayedFocusGrid;
  457. int row = m_delayedFocusRow;
  458. int col = m_delayedFocusColumn;
  459. wxString msg = m_delayedErrorMessage;
  460. m_delayedFocusGrid = nullptr;
  461. m_delayedFocusRow = -1;
  462. m_delayedFocusColumn = -1;
  463. m_delayedErrorMessage = wxEmptyString;
  464. if( !msg.IsEmpty() )
  465. {
  466. // Do not use DisplayErrorMessage(); it screws up window order on Mac
  467. DisplayError( nullptr, msg );
  468. }
  469. grid->SetFocus();
  470. grid->MakeCellVisible( row, col );
  471. // Selecting the first grid item only makes sense for the
  472. // items grid
  473. if( !m_initialFocus || grid == m_itemsGrid )
  474. {
  475. grid->SetGridCursor( row, col );
  476. grid->EnableCellEditControl( true );
  477. grid->ShowCellEditControl();
  478. if( grid == m_itemsGrid && row == 0 && col == 0 )
  479. {
  480. auto referenceEditor = grid->GetCellEditor( 0, 0 );
  481. if( auto textEntry = dynamic_cast<wxTextEntry*>( referenceEditor->GetControl() ) )
  482. KIUI::SelectReferenceNumber( textEntry );
  483. referenceEditor->DecRef();
  484. }
  485. }
  486. m_initialFocus = false;
  487. }
  488. }
  489. void DIALOG_FOOTPRINT_PROPERTIES::OnGridSize( wxSizeEvent& aEvent )
  490. {
  491. // A trick to fix a cosmetic issue: when, in m_itemsGrid, a layer selector widget
  492. // has the focus (is activated in column 6) when resizing the grid, the widget
  493. // is not moved. So just change the widget having the focus in this case
  494. if( m_NoteBook->GetSelection() == 0 && !m_itemsGrid->HasFocus() )
  495. {
  496. int col = m_itemsGrid->GetGridCursorCol();
  497. if( col == 6 ) // a layer selector widget can be activated
  498. m_itemsGrid->SetFocus();
  499. }
  500. adjustGridColumns();
  501. aEvent.Skip();
  502. }
  503. void DIALOG_FOOTPRINT_PROPERTIES::OnPageChange( wxNotebookEvent& aEvent )
  504. {
  505. int page = aEvent.GetSelection();
  506. // Shouldn't be necessary, but is on at least OSX
  507. if( page >= 0 )
  508. m_NoteBook->ChangeSelection( (unsigned) page );
  509. }