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.

405 lines
9.8 KiB

Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 Miguel Angel Ajo Pelayo, miguelangel@nbee.es
  5. * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
  6. * Copyright (C) 2004-2017 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. /**
  26. * @file footprint_wizard_frame_functions.cpp
  27. */
  28. #include <fctsys.h>
  29. #include <gr_basic.h>
  30. #include <class_drawpanel.h>
  31. #include <pcb_edit_frame.h>
  32. #include <dialog_helpers.h>
  33. #include <class_board.h>
  34. #include <class_module.h>
  35. #include <pcbnew.h>
  36. #include <pcbnew_id.h>
  37. #include "footprint_wizard_frame.h"
  38. #include <wildcards_and_files_ext.h>
  39. #include <dialogs/dialog_footprint_wizard_list.h>
  40. #include <base_units.h>
  41. void FOOTPRINT_WIZARD_FRAME::Process_Special_Functions( wxCommandEvent& event )
  42. {
  43. wxString msg;
  44. int page;
  45. switch( event.GetId() )
  46. {
  47. case ID_FOOTPRINT_WIZARD_NEXT:
  48. m_pageList->SetSelection( m_pageList->GetSelection() + 1, true );
  49. ClickOnPageList( event );
  50. break;
  51. case ID_FOOTPRINT_WIZARD_PREVIOUS:
  52. page = m_pageList->GetSelection() - 1;
  53. if( page < 0 )
  54. page = 0;
  55. m_pageList->SetSelection( page, true );
  56. ClickOnPageList( event );
  57. break;
  58. default:
  59. msg << wxT( "FOOTPRINT_WIZARD_FRAME::Process_Special_Functions error: id = " )
  60. << event.GetId();
  61. wxMessageBox( msg );
  62. break;
  63. }
  64. }
  65. /* Function OnLeftClick
  66. * Captures a left click event in the dialog
  67. *
  68. */
  69. void FOOTPRINT_WIZARD_FRAME::OnLeftClick( wxDC* DC, const wxPoint& MousePos )
  70. {
  71. }
  72. /* Function OnRightClick
  73. * Captures a right click event in the dialog
  74. *
  75. */
  76. bool FOOTPRINT_WIZARD_FRAME::OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu )
  77. {
  78. return true;
  79. }
  80. /* Displays the name of the current opened library in the caption */
  81. void FOOTPRINT_WIZARD_FRAME::DisplayWizardInfos()
  82. {
  83. wxString msg;
  84. msg = _( "Footprint Wizard" );
  85. msg << wxT( " [" );
  86. if( !m_wizardName.IsEmpty() )
  87. msg << m_wizardName;
  88. else
  89. msg += _( "no wizard selected" );
  90. msg << wxT( "]" );
  91. SetTitle( msg );
  92. }
  93. void FOOTPRINT_WIZARD_FRAME::ReloadFootprint()
  94. {
  95. FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
  96. if( !footprintWizard )
  97. return;
  98. SetCurItem( NULL );
  99. // Delete the current footprint
  100. GetBoard()->m_Modules.DeleteAll();
  101. // Creates the module
  102. wxString msg;
  103. MODULE* module = footprintWizard->GetFootprint( &msg );
  104. DisplayBuildMessage( msg );
  105. if( module )
  106. {
  107. // Add the object to board
  108. GetBoard()->Add( module, ADD_APPEND );
  109. module->SetPosition( wxPoint( 0, 0 ) );
  110. }
  111. else
  112. {
  113. DBG(printf( "footprintWizard->GetFootprint() returns NULL\n" );)
  114. }
  115. m_canvas->Refresh();
  116. }
  117. void FOOTPRINT_WIZARD_FRAME::DisplayBuildMessage( wxString& aMessage )
  118. {
  119. if( m_messagesFrame == NULL )
  120. {
  121. // Prepare the window to display the message generated by the footprint script builder
  122. m_messagesFrame = new FOOTPRINT_WIZARD_MESSAGES( this, config() );
  123. m_messagesFrame->Show( true );
  124. }
  125. m_messagesFrame->ClearScreen();
  126. if( !aMessage.IsEmpty() )
  127. m_messagesFrame->PrintMessage( aMessage );
  128. }
  129. FOOTPRINT_WIZARD* FOOTPRINT_WIZARD_FRAME::GetMyWizard()
  130. {
  131. if( m_wizardName.Length() == 0 )
  132. return NULL;
  133. FOOTPRINT_WIZARD* footprintWizard = FOOTPRINT_WIZARD_LIST::GetWizard( m_wizardName );
  134. if( !footprintWizard )
  135. {
  136. wxMessageBox( _( "Couldn't reload footprint wizard" ) );
  137. return NULL;
  138. }
  139. return footprintWizard;
  140. }
  141. MODULE* FOOTPRINT_WIZARD_FRAME::GetBuiltFootprint()
  142. {
  143. FOOTPRINT_WIZARD* footprintWizard = FOOTPRINT_WIZARD_LIST::GetWizard( m_wizardName );
  144. if( footprintWizard && m_modal_ret_val )
  145. {
  146. wxString msg;
  147. MODULE * footprint = footprintWizard->GetFootprint( &msg );
  148. DisplayBuildMessage( msg );
  149. return footprint;
  150. }
  151. return NULL;
  152. }
  153. void FOOTPRINT_WIZARD_FRAME::SelectFootprintWizard()
  154. {
  155. DIALOG_FOOTPRINT_WIZARD_LIST wizardSelector( this );
  156. if( wizardSelector.ShowModal() != wxID_OK )
  157. return;
  158. FOOTPRINT_WIZARD* footprintWizard = wizardSelector.GetWizard();
  159. if( footprintWizard )
  160. {
  161. m_wizardName = footprintWizard->GetName();
  162. m_wizardDescription = footprintWizard->GetDescription();
  163. footprintWizard->ResetParameters();
  164. }
  165. else
  166. {
  167. m_wizardName.Empty();
  168. m_wizardDescription.Empty();
  169. }
  170. ReloadFootprint();
  171. Zoom_Automatique( false );
  172. DisplayWizardInfos();
  173. ReCreatePageList();
  174. ReCreateParameterList();
  175. }
  176. void FOOTPRINT_WIZARD_FRAME::SelectCurrentWizard( wxCommandEvent& event )
  177. {
  178. SelectFootprintWizard();
  179. }
  180. void FOOTPRINT_WIZARD_FRAME::DefaultParameters( wxCommandEvent& event )
  181. {
  182. FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
  183. if ( footprintWizard == NULL )
  184. return;
  185. footprintWizard->ResetParameters();
  186. // Reload
  187. ReCreateParameterList();
  188. ReloadFootprint();
  189. DisplayWizardInfos();
  190. }
  191. void FOOTPRINT_WIZARD_FRAME::ParametersUpdated( wxGridEvent& event )
  192. {
  193. FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
  194. if( !footprintWizard )
  195. return;
  196. if( m_parameterGridPage < 0 )
  197. return;
  198. wxArrayString prmValues = footprintWizard->GetParameterValues( m_parameterGridPage );
  199. wxArrayString ptList = footprintWizard->GetParameterTypes( m_parameterGridPage );
  200. bool has_changed = false;
  201. int count = m_parameterGrid->GetNumberRows();
  202. // Skip extra event, useless
  203. if( event.GetString() == m_parameterGrid->GetCellValue( event.GetRow(), WIZ_COL_VALUE ) )
  204. return;
  205. for( int prm_id = 0; prm_id < count; ++prm_id )
  206. {
  207. wxString value = m_parameterGrid->GetCellValue( prm_id, WIZ_COL_VALUE );
  208. if( prmValues[prm_id] != value )
  209. {
  210. has_changed = true;
  211. prmValues[prm_id] = value;
  212. }
  213. }
  214. if( has_changed )
  215. {
  216. wxString res = footprintWizard->SetParameterValues( m_parameterGridPage, prmValues );
  217. if( !res.IsEmpty() )
  218. wxMessageBox( res );
  219. ReloadFootprint();
  220. DisplayWizardInfos();
  221. }
  222. }
  223. void FOOTPRINT_WIZARD_FRAME::OnParameterCellClick( wxGridEvent& event )
  224. {
  225. auto footprintWizard = GetMyWizard();
  226. if( !footprintWizard )
  227. return;
  228. if( m_parameterGridPage < 0 )
  229. return;
  230. if( event.GetCol() != WIZ_COL_VALUE )
  231. return;
  232. auto types = footprintWizard->GetParameterTypes( m_parameterGridPage );
  233. auto values = footprintWizard->GetParameterValues( m_parameterGridPage );
  234. int row = event.GetRow();
  235. bool has_changed = false;
  236. // Handle toggling of boolean parameters
  237. if( types[row] == WIZARD_PARAM_UNITS_BOOL )
  238. {
  239. has_changed = true;
  240. values[row] = ( values[row] == "1" ) ? "0" : "1";
  241. m_parameterGrid->SetCellValue( row, WIZ_COL_VALUE, values[row] );
  242. }
  243. else
  244. {
  245. event.Skip();
  246. }
  247. if( has_changed )
  248. {
  249. wxString res = footprintWizard->SetParameterValues( m_parameterGridPage, values );
  250. if( !res.IsEmpty() )
  251. wxMessageBox( res );
  252. ReloadFootprint();
  253. DisplayWizardInfos();
  254. }
  255. }
  256. void FOOTPRINT_WIZARD_FRAME::OnParameterGridKeyPress( wxKeyEvent& event )
  257. {
  258. auto footprintWizard = GetMyWizard();
  259. int row = m_parameterGrid->GetGridCursorRow();
  260. int col = m_parameterGrid->GetGridCursorCol();
  261. bool has_changed = false;
  262. if( !footprintWizard || m_parameterGridPage < 0 ||
  263. col != WIZ_COL_VALUE || event.GetKeyCode() != ' ' )
  264. {
  265. event.Skip();
  266. return;
  267. }
  268. auto types = footprintWizard->GetParameterTypes( m_parameterGridPage );
  269. auto values = footprintWizard->GetParameterValues( m_parameterGridPage );
  270. // Handle toggling of boolean parameters when user presses space
  271. if( types[row] == WIZARD_PARAM_UNITS_BOOL )
  272. {
  273. has_changed = true;
  274. values[row] = ( values[row] == "1" ) ? "0" : "1";
  275. m_parameterGrid->SetCellValue( row, WIZ_COL_VALUE, values[row] );
  276. }
  277. else
  278. {
  279. m_parameterGrid->EnableCellEditControl();
  280. }
  281. if( has_changed )
  282. {
  283. wxString res = footprintWizard->SetParameterValues( m_parameterGridPage, values );
  284. if( !res.IsEmpty() )
  285. wxMessageBox( res );
  286. ReloadFootprint();
  287. DisplayWizardInfos();
  288. }
  289. }
  290. /**
  291. * Function RedrawActiveWindow
  292. * Display the current selected component.
  293. * If the component is an alias, the ROOT component is displayed
  294. *
  295. */
  296. void FOOTPRINT_WIZARD_FRAME::RedrawActiveWindow( wxDC* DC, bool EraseBg )
  297. {
  298. if( !GetBoard() )
  299. return;
  300. m_canvas->DrawBackGround( DC );
  301. GetBoard()->Draw( m_canvas, DC, GR_COPY );
  302. MODULE* module = GetBoard()->m_Modules;
  303. if( module )
  304. SetMsgPanel( module );
  305. m_canvas->DrawCrossHair( DC );
  306. ClearMsgPanel();
  307. if( module )
  308. SetMsgPanel( module );
  309. }