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.

443 lines
14 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Wayne Stambaugh <stambaughw@gmail.com>
  5. * Copyright (C) 2015-2022 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 <dialogs/dialog_configure_paths.h>
  25. #include <bitmaps.h>
  26. #include <confirm.h>
  27. #include <validators.h>
  28. #include <dialogs/html_message_box.h>
  29. #include <filename_resolver.h>
  30. #include <env_vars.h>
  31. #include <grid_tricks.h>
  32. #include <pgm_base.h>
  33. #include <widgets/wx_grid.h>
  34. #include <widgets/grid_text_button_helpers.h>
  35. #include <widgets/grid_text_helpers.h>
  36. #include <widgets/std_bitmap_button.h>
  37. #include <algorithm>
  38. #include <wx/dirdlg.h>
  39. enum TEXT_VAR_GRID_COLUMNS
  40. {
  41. TV_NAME_COL = 0,
  42. TV_VALUE_COL,
  43. TV_FLAG_COL
  44. };
  45. enum SEARCH_PATH_GRID_COLUMNS
  46. {
  47. SP_ALIAS_COL = 0,
  48. SP_PATH_COL,
  49. SP_DESC_COL
  50. };
  51. DIALOG_CONFIGURE_PATHS::DIALOG_CONFIGURE_PATHS( wxWindow* aParent ) :
  52. DIALOG_CONFIGURE_PATHS_BASE( aParent ),
  53. m_errorGrid( nullptr ),
  54. m_errorRow( -1 ),
  55. m_errorCol( -1 ),
  56. m_gridWidth( 0 ),
  57. m_gridWidthsDirty( true ),
  58. m_helpBox( nullptr ),
  59. m_heightBeforeHelp( 400 )
  60. {
  61. m_btnAddEnvVar->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
  62. m_btnDeleteEnvVar->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
  63. m_EnvVars->ClearRows();
  64. m_EnvVars->AppendCols( 1 ); // for the isExternal flags
  65. m_EnvVars->HideCol( TV_FLAG_COL );
  66. m_EnvVars->UseNativeColHeader( true );
  67. wxGridCellAttr* attr = new wxGridCellAttr;
  68. attr->SetEditor( new GRID_CELL_PATH_EDITOR( this, m_EnvVars, &m_curdir, wxEmptyString ) );
  69. m_EnvVars->SetColAttr( TV_VALUE_COL, attr );
  70. // Give a bit more room for combobox editors
  71. m_EnvVars->SetDefaultRowSize( m_EnvVars->GetDefaultRowSize() + 4 );
  72. m_EnvVars->PushEventHandler( new GRID_TRICKS( m_EnvVars,
  73. [this]( wxCommandEvent& aEvent )
  74. {
  75. OnAddEnvVar( aEvent );
  76. } ) );
  77. m_EnvVars->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
  78. SetInitialFocus( m_EnvVars );
  79. SetupStandardButtons();
  80. // wxFormBuilder doesn't include this event...
  81. m_EnvVars->Connect( wxEVT_GRID_CELL_CHANGING,
  82. wxGridEventHandler( DIALOG_CONFIGURE_PATHS::OnGridCellChanging ),
  83. nullptr, this );
  84. GetSizer()->SetSizeHints( this );
  85. Centre();
  86. }
  87. DIALOG_CONFIGURE_PATHS::~DIALOG_CONFIGURE_PATHS()
  88. {
  89. // Delete the GRID_TRICKS.
  90. m_EnvVars->PopEventHandler( true );
  91. m_EnvVars->Disconnect( wxEVT_GRID_CELL_CHANGING,
  92. wxGridEventHandler( DIALOG_CONFIGURE_PATHS::OnGridCellChanging ),
  93. nullptr, this );
  94. }
  95. bool DIALOG_CONFIGURE_PATHS::TransferDataToWindow()
  96. {
  97. if( !wxDialog::TransferDataToWindow() )
  98. return false;
  99. const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
  100. for( auto it = envVars.begin(); it != envVars.end(); ++it )
  101. {
  102. const wxString& path = it->second.GetValue();
  103. AppendEnvVar( it->first, path, it->second.GetDefinedExternally() );
  104. if( m_curdir.IsEmpty() && !path.StartsWith( "${" ) && !path.StartsWith( "$(" ) )
  105. m_curdir = path;
  106. }
  107. return true;
  108. }
  109. void DIALOG_CONFIGURE_PATHS::AppendEnvVar( const wxString& aName, const wxString& aPath,
  110. bool isExternal )
  111. {
  112. int i = m_EnvVars->GetNumberRows();
  113. m_EnvVars->AppendRows( 1 );
  114. m_EnvVars->SetCellValue( i, TV_NAME_COL, aName );
  115. wxGridCellAttr* nameCellAttr = m_EnvVars->GetOrCreateCellAttr( i, TV_NAME_COL );
  116. wxGridCellTextEditor* nameTextEditor = new GRID_CELL_TEXT_EDITOR();
  117. nameTextEditor->SetValidator( ENV_VAR_NAME_VALIDATOR() );
  118. nameCellAttr->SetEditor( nameTextEditor );
  119. nameCellAttr->SetReadOnly( ENV_VAR::IsEnvVarImmutable( aName ) );
  120. nameCellAttr->DecRef();
  121. m_EnvVars->SetCellValue( i, TV_VALUE_COL, aPath );
  122. wxGridCellAttr* pathCellAttr = m_EnvVars->GetOrCreateCellAttr( i, TV_VALUE_COL );
  123. wxSystemColour c = isExternal ? wxSYS_COLOUR_MENU : wxSYS_COLOUR_LISTBOX;
  124. pathCellAttr->SetBackgroundColour( wxSystemSettings::GetColour( c ) );
  125. pathCellAttr->DecRef();
  126. m_EnvVars->SetCellValue( i, TV_FLAG_COL, isExternal ? wxT( "external" ) : wxEmptyString );
  127. }
  128. bool DIALOG_CONFIGURE_PATHS::TransferDataFromWindow()
  129. {
  130. if( !m_EnvVars->CommitPendingChanges() )
  131. return false;
  132. if( !wxDialog::TransferDataFromWindow() )
  133. return false;
  134. // Update environment variables
  135. ENV_VAR_MAP& envVarMap = Pgm().GetLocalEnvVariables();
  136. for( int row = 0; row < m_EnvVars->GetNumberRows(); ++row )
  137. {
  138. wxString name = m_EnvVars->GetCellValue( row, TV_NAME_COL );
  139. wxString path = m_EnvVars->GetCellValue( row, TV_VALUE_COL );
  140. bool external = !m_EnvVars->GetCellValue( row, TV_FLAG_COL ).IsEmpty();
  141. if( external )
  142. {
  143. // Don't check for consistency on external variables, just use them as-is
  144. }
  145. else if( name.empty() && path.empty() )
  146. {
  147. // Skip empty rows altogether
  148. continue;
  149. }
  150. else if( name.IsEmpty() )
  151. {
  152. m_errorGrid = m_EnvVars;
  153. m_errorRow = row;
  154. m_errorCol = TV_NAME_COL;
  155. m_errorMsg = _( "Environment variable name cannot be empty." );
  156. return false;
  157. }
  158. else if( path.IsEmpty() )
  159. {
  160. m_errorGrid = m_EnvVars;
  161. m_errorRow = row;
  162. m_errorCol = TV_VALUE_COL;
  163. m_errorMsg = _( "Environment variable path cannot be empty." );
  164. return false;
  165. }
  166. if( envVarMap.count( name ) )
  167. envVarMap.at( name ).SetValue( path );
  168. else
  169. envVarMap[ name ] = ENV_VAR_ITEM( name, path );
  170. }
  171. // Remove deleted env vars
  172. for( auto it = envVarMap.begin(); it != envVarMap.end(); )
  173. {
  174. bool found = false;
  175. for( int row = 0; row < m_EnvVars->GetNumberRows(); ++row )
  176. {
  177. wxString name = m_EnvVars->GetCellValue( row, TV_NAME_COL );
  178. if( it->first == name )
  179. {
  180. found = true;
  181. break;
  182. }
  183. }
  184. if( found )
  185. ++it;
  186. else
  187. it = envVarMap.erase( it );
  188. }
  189. Pgm().SetLocalEnvVariables();
  190. return true;
  191. }
  192. void DIALOG_CONFIGURE_PATHS::OnGridCellChanging( wxGridEvent& event )
  193. {
  194. wxGrid* grid = dynamic_cast<wxGrid*>( event.GetEventObject() );
  195. int row = event.GetRow();
  196. int col = event.GetCol();
  197. wxString text = event.GetString();
  198. if( text.IsEmpty() )
  199. {
  200. if( grid == m_EnvVars )
  201. {
  202. if( col == TV_NAME_COL )
  203. m_errorMsg = _( "Environment variable name cannot be empty." );
  204. else
  205. m_errorMsg = _( "Environment variable path cannot be empty." );
  206. }
  207. else
  208. {
  209. if( col == SP_ALIAS_COL )
  210. m_errorMsg = _( "3D search path alias cannot be empty." );
  211. else
  212. m_errorMsg = _( "3D search path cannot be empty." );
  213. }
  214. m_errorGrid = dynamic_cast<wxGrid*>( event.GetEventObject() );
  215. m_errorRow = row;
  216. m_errorCol = col;
  217. event.Veto();
  218. }
  219. if( grid == m_EnvVars )
  220. {
  221. if( col == TV_VALUE_COL && m_EnvVars->GetCellValue( row, TV_FLAG_COL ).Length()
  222. && !Pgm().GetCommonSettings()->m_DoNotShowAgain.env_var_overwrite_warning )
  223. {
  224. wxString msg1 = _( "This path was defined externally to the running process and\n"
  225. "will only be temporarily overwritten." );
  226. wxString msg2 = _( "The next time KiCad is launched, any paths that have already\n"
  227. "been defined are honored and any settings defined in the path\n"
  228. "configuration dialog are ignored. If you did not intend for\n"
  229. "this behavior, either rename any conflicting entries or remove\n"
  230. "the external environment variable(s) from your system." );
  231. KIDIALOG dlg( this, msg1, KIDIALOG::KD_WARNING );
  232. dlg.ShowDetailedText( msg2 );
  233. dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
  234. dlg.ShowModal();
  235. if( dlg.DoNotShowAgain() )
  236. Pgm().GetCommonSettings()->m_DoNotShowAgain.env_var_overwrite_warning = true;
  237. }
  238. else if( col == TV_NAME_COL && m_EnvVars->GetCellValue( row, TV_NAME_COL ) != text )
  239. {
  240. // This env var name is reserved and cannot be added here.
  241. if( text == PROJECT_VAR_NAME )
  242. {
  243. wxMessageBox( wxString::Format( _( "The name %s is reserved, and cannot be used." ),
  244. PROJECT_VAR_NAME ) );
  245. event.Veto();
  246. }
  247. else // Changing name; clear external flag
  248. {
  249. m_EnvVars->SetCellValue( row, TV_FLAG_COL, wxEmptyString );
  250. }
  251. }
  252. }
  253. }
  254. void DIALOG_CONFIGURE_PATHS::OnAddEnvVar( wxCommandEvent& event )
  255. {
  256. if( !m_EnvVars->CommitPendingChanges() )
  257. return;
  258. AppendEnvVar( wxEmptyString, wxEmptyString, false );
  259. m_EnvVars->MakeCellVisible( m_EnvVars->GetNumberRows() - 1, TV_NAME_COL );
  260. m_EnvVars->SetGridCursor( m_EnvVars->GetNumberRows() - 1, TV_NAME_COL );
  261. m_EnvVars->EnableCellEditControl( true );
  262. m_EnvVars->ShowCellEditControl();
  263. }
  264. void DIALOG_CONFIGURE_PATHS::OnRemoveEnvVar( wxCommandEvent& event )
  265. {
  266. int curRow = m_EnvVars->GetGridCursorRow();
  267. if( curRow < 0 || m_EnvVars->GetNumberRows() <= curRow )
  268. {
  269. return;
  270. }
  271. else if( ENV_VAR::IsEnvVarImmutable( m_EnvVars->GetCellValue( curRow, TV_NAME_COL ) ) )
  272. {
  273. wxBell();
  274. return;
  275. }
  276. m_EnvVars->CommitPendingChanges( true /* silent mode; we don't care if it's valid */ );
  277. m_EnvVars->DeleteRows( curRow, 1 );
  278. m_EnvVars->MakeCellVisible( std::max( 0, curRow-1 ), m_EnvVars->GetGridCursorCol() );
  279. m_EnvVars->SetGridCursor( std::max( 0, curRow-1 ), m_EnvVars->GetGridCursorCol() );
  280. }
  281. void DIALOG_CONFIGURE_PATHS::OnUpdateUI( wxUpdateUIEvent& event )
  282. {
  283. if( m_gridWidthsDirty )
  284. {
  285. int width = m_EnvVars->GetClientRect().GetWidth();
  286. m_EnvVars->AutoSizeColumn( TV_NAME_COL );
  287. m_EnvVars->SetColSize( TV_NAME_COL, std::max( 72, m_EnvVars->GetColSize( TV_NAME_COL ) ) );
  288. m_EnvVars->SetColSize( TV_VALUE_COL, std::max( 120, width - m_EnvVars->GetColSize( TV_NAME_COL ) ) );
  289. m_gridWidth = m_EnvVars->GetSize().GetX();
  290. m_gridWidthsDirty = false;
  291. }
  292. // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus even when
  293. // the original validation was triggered from a killFocus event.
  294. if( m_errorGrid )
  295. {
  296. // We will re-enter this routine when the error dialog is displayed, so make sure we don't
  297. // keep putting up more dialogs.
  298. wxGrid* grid = m_errorGrid;
  299. m_errorGrid = nullptr;
  300. DisplayErrorMessage( this, m_errorMsg );
  301. grid->SetFocus();
  302. grid->MakeCellVisible( m_errorRow, m_errorCol );
  303. grid->SetGridCursor( m_errorRow, m_errorCol );
  304. grid->EnableCellEditControl( true );
  305. grid->ShowCellEditControl();
  306. }
  307. }
  308. void DIALOG_CONFIGURE_PATHS::OnGridSize( wxSizeEvent& event )
  309. {
  310. if( event.GetSize().GetX() != m_gridWidth )
  311. m_gridWidthsDirty = true;
  312. event.Skip();
  313. }
  314. void DIALOG_CONFIGURE_PATHS::OnHelp( wxCommandEvent& event )
  315. {
  316. wxSizer* sizerMain = GetSizer();
  317. if( !m_helpBox )
  318. {
  319. m_helpBox = new HTML_WINDOW( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  320. wxHW_SCROLLBAR_AUTO );
  321. wxString msg = _( "Enter the name and value for each environment variable. Grey entries "
  322. "are names that have been defined externally at the system or user "
  323. "level. Environment variables defined at the system or user level "
  324. "take precedence over the ones defined in this table. This means the "
  325. "values in this table are ignored." );
  326. msg << "<br><br><b>";
  327. msg << _( "To ensure environment variable names are valid on all platforms, the name field "
  328. "will only accept upper case letters, digits, and the underscore characters." );
  329. msg << "</b>";
  330. for( const wxString& var : ENV_VAR::GetPredefinedEnvVars() )
  331. {
  332. msg << "<br><br><b>" << var << "</b>";
  333. const auto desc = ENV_VAR::LookUpEnvVarHelp( var );
  334. if( desc.size() > 0 )
  335. msg << ": " << desc;
  336. }
  337. m_helpBox->SetPage( msg );
  338. m_helpBox->Show( false );
  339. sizerMain->Insert( sizerMain->GetItemCount() - 1, m_helpBox, 1, wxALL|wxEXPAND, 10 );
  340. }
  341. if( m_helpBox->IsShown() )
  342. {
  343. m_helpBox->Show( false );
  344. SetClientSize( wxSize( GetClientSize().x, m_heightBeforeHelp ) );
  345. }
  346. else
  347. {
  348. m_helpBox->Show( true );
  349. m_heightBeforeHelp = GetClientSize().y;
  350. int minHelpBoxHeight = GetTextExtent( wxT( "T" ) ).y * 20;
  351. if( GetClientSize().y < minHelpBoxHeight * 2 )
  352. SetClientSize( wxSize( GetClientSize().x, GetClientSize().y + minHelpBoxHeight ) );
  353. }
  354. Layout();
  355. }