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.

482 lines
14 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <confirm.h>
  20. #include <widgets/resettable_panel.h>
  21. #include <widgets/wx_infobar.h>
  22. #include <widgets/wx_panel.h>
  23. #include <widgets/paged_dialog.h>
  24. #include <widgets/wx_treebook.h>
  25. #include <wx/button.h>
  26. #include <wx/grid.h>
  27. #include <wx/sizer.h>
  28. #include <wx/treebook.h>
  29. #include <wx/treectrl.h>
  30. #include <wx/listctrl.h>
  31. #include <wx/stc/stc.h>
  32. #include <paths.h>
  33. #include <launch_ext.h>
  34. #include <algorithm>
  35. // Maps from dialogTitle <-> pageTitle for keeping track of last-selected pages.
  36. // This is not a simple page index because some dialogs have dynamic page sets.
  37. std::map<wxString, wxString> g_lastPage;
  38. std::map<wxString, wxString> g_lastParentPage;
  39. PAGED_DIALOG::PAGED_DIALOG( wxWindow* aParent, const wxString& aTitle, bool aShowReset, bool aShowOpenFolder,
  40. const wxString& aAuxiliaryAction, const wxSize& aInitialSize ) :
  41. DIALOG_SHIM( aParent, wxID_ANY, aTitle, wxDefaultPosition, aInitialSize,
  42. wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
  43. m_auxiliaryButton( nullptr ),
  44. m_resetButton( nullptr ),
  45. m_openPrefsDirButton( nullptr ),
  46. m_title( aTitle )
  47. {
  48. wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
  49. SetSizer( mainSizer );
  50. m_infoBar = new WX_INFOBAR( this );
  51. mainSizer->Add( m_infoBar, 0, wxEXPAND, 0 );
  52. WX_PANEL* treebookPanel = new WX_PANEL( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  53. wxBORDER_NONE | wxTAB_TRAVERSAL );
  54. treebookPanel->SetBorders( false, false, false, true );
  55. wxBoxSizer* treebookSizer = new wxBoxSizer( wxVERTICAL );
  56. treebookPanel->SetSizer( treebookSizer );
  57. m_treebook = new WX_TREEBOOK( treebookPanel, wxID_ANY );
  58. m_treebook->SetFont( KIUI::GetControlFont( this ) );
  59. m_treebook->SetFitToCurrentPage( true );
  60. long treeCtrlFlags = m_treebook->GetTreeCtrl()->GetWindowStyleFlag();
  61. treeCtrlFlags = ( treeCtrlFlags & ~wxBORDER_MASK ) | wxBORDER_NONE;
  62. m_treebook->GetTreeCtrl()->SetWindowStyleFlag( treeCtrlFlags );
  63. treebookSizer->Add( m_treebook, 1, wxEXPAND|wxBOTTOM, 2 );
  64. mainSizer->Add( treebookPanel, 1, wxEXPAND, 0 );
  65. m_buttonsSizer = new wxBoxSizer( wxHORIZONTAL );
  66. if( aShowReset )
  67. {
  68. m_resetButton = new wxButton( this, wxID_ANY, _( "Reset to Defaults" ) );
  69. m_buttonsSizer->Add( m_resetButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT, 5 );
  70. }
  71. if( aShowOpenFolder )
  72. {
  73. #ifdef __WXMAC__
  74. m_openPrefsDirButton = new wxButton( this, wxID_ANY, _( "Reveal Preferences in Finder" ) );
  75. #else
  76. m_openPrefsDirButton = new wxButton( this, wxID_ANY, _( "Open Preferences Directory" ) );
  77. #endif
  78. m_buttonsSizer->Add( m_openPrefsDirButton, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxLEFT, 5 );
  79. }
  80. if( !aAuxiliaryAction.IsEmpty() )
  81. {
  82. m_auxiliaryButton = new wxButton( this, wxID_ANY, aAuxiliaryAction );
  83. m_buttonsSizer->Add( m_auxiliaryButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT, 5 );
  84. }
  85. m_buttonsSizer->AddStretchSpacer();
  86. wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
  87. wxButton* sdbSizerOK = new wxButton( this, wxID_OK );
  88. sdbSizer->AddButton( sdbSizerOK );
  89. wxButton* sdbSizerCancel = new wxButton( this, wxID_CANCEL );
  90. sdbSizer->AddButton( sdbSizerCancel );
  91. sdbSizer->Realize();
  92. m_buttonsSizer->Add( sdbSizer, 1, 0, 5 );
  93. mainSizer->Add( m_buttonsSizer, 0, wxALL|wxEXPAND, 5 );
  94. SetupStandardButtons();
  95. // We normally save the dialog size and position based on its class-name. This class
  96. // substitutes the title so that each distinctly-titled dialog can have its own saved
  97. // size and position.
  98. m_hash_key = aTitle;
  99. if( m_auxiliaryButton )
  100. {
  101. m_auxiliaryButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onAuxiliaryAction,
  102. this );
  103. }
  104. if( m_resetButton )
  105. {
  106. m_resetButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onResetButton, this );
  107. }
  108. if( m_openPrefsDirButton )
  109. {
  110. m_openPrefsDirButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onOpenPreferencesButton, this );
  111. }
  112. m_treebook->Bind( wxEVT_CHAR_HOOK, &PAGED_DIALOG::onCharHook, this );
  113. m_treebook->Bind( wxEVT_TREEBOOK_PAGE_CHANGED, &PAGED_DIALOG::onPageChanged, this );
  114. m_treebook->Bind( wxEVT_TREEBOOK_PAGE_CHANGING, &PAGED_DIALOG::onPageChanging, this );
  115. }
  116. void PAGED_DIALOG::finishInitialization()
  117. {
  118. for( size_t i = 1; i < m_treebook->GetPageCount(); ++i )
  119. m_macHack.push_back( true );
  120. // For some reason adding page labels to the treeCtrl doesn't invalidate its bestSize
  121. // cache so we have to do it by hand
  122. m_treebook->GetTreeCtrl()->InvalidateBestSize();
  123. for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
  124. m_treebook->GetPage( i )->Layout();
  125. m_treebook->Layout();
  126. m_treebook->Fit();
  127. finishDialogSettings();
  128. }
  129. void PAGED_DIALOG::SetInitialPage( const wxString& aPage, const wxString& aParentPage )
  130. {
  131. g_lastPage[ m_title ] = aPage;
  132. g_lastParentPage[ m_title ] = aParentPage;
  133. }
  134. PAGED_DIALOG::~PAGED_DIALOG()
  135. {
  136. // Store the current parentPageTitle/pageTitle hierarchy so we can re-select it
  137. // next time.
  138. wxString lastPage = wxEmptyString;
  139. wxString lastParentPage = wxEmptyString;
  140. int selected = m_treebook->GetSelection();
  141. if( selected != wxNOT_FOUND )
  142. {
  143. lastPage = m_treebook->GetPageText( (unsigned) selected );
  144. int parent = m_treebook->GetPageParent( (unsigned) selected );
  145. if( parent != wxNOT_FOUND )
  146. lastParentPage = m_treebook->GetPageText( (unsigned) parent );
  147. }
  148. g_lastPage[ m_title ] = lastPage;
  149. g_lastParentPage[ m_title ] = lastParentPage;
  150. if( m_auxiliaryButton )
  151. {
  152. m_auxiliaryButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onAuxiliaryAction,
  153. this );
  154. }
  155. if( m_resetButton )
  156. {
  157. m_resetButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onResetButton, this );
  158. }
  159. if( m_openPrefsDirButton )
  160. {
  161. m_openPrefsDirButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onOpenPreferencesButton, this );
  162. }
  163. m_treebook->Unbind( wxEVT_CHAR_HOOK, &PAGED_DIALOG::onCharHook, this );
  164. m_treebook->Unbind( wxEVT_TREEBOOK_PAGE_CHANGED, &PAGED_DIALOG::onPageChanged, this );
  165. m_treebook->Unbind( wxEVT_TREEBOOK_PAGE_CHANGING, &PAGED_DIALOG::onPageChanging, this );
  166. }
  167. bool PAGED_DIALOG::TransferDataToWindow()
  168. {
  169. finishInitialization();
  170. // Call TransferDataToWindow() only once:
  171. // this is enough on wxWidgets 3.1
  172. if( !DIALOG_SHIM::TransferDataToWindow() )
  173. return false;
  174. // Search for a page matching the lastParentPageTitle/lastPageTitle hierarchy
  175. wxString lastPage = g_lastPage[ m_title ];
  176. wxString lastParentPage = g_lastParentPage[ m_title ];
  177. int lastPageIndex = wxNOT_FOUND;
  178. for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
  179. {
  180. if( m_treebook->GetPageText( i ) == lastPage )
  181. {
  182. if( lastParentPage.IsEmpty() )
  183. {
  184. lastPageIndex = i;
  185. break;
  186. }
  187. if( m_treebook->GetPageParent( i ) >= 0
  188. && m_treebook->GetPageText( (unsigned) m_treebook->GetPageParent( i ) ) == lastParentPage )
  189. {
  190. lastPageIndex = i;
  191. break;
  192. }
  193. }
  194. }
  195. lastPageIndex = std::max( 0, lastPageIndex );
  196. m_treebook->SetSelection( lastPageIndex );
  197. UpdateResetButton( lastPageIndex );
  198. return true;
  199. }
  200. bool PAGED_DIALOG::TransferDataFromWindow()
  201. {
  202. bool ret = true;
  203. // Call TransferDataFromWindow() only once:
  204. // this is enough on wxWidgets 3.1
  205. if( !DIALOG_SHIM::TransferDataFromWindow() )
  206. ret = false;
  207. return ret;
  208. }
  209. PAGED_DIALOG* PAGED_DIALOG::GetDialog( wxWindow* aParent )
  210. {
  211. while( aParent )
  212. {
  213. if( PAGED_DIALOG* parentDialog = dynamic_cast<PAGED_DIALOG*>( aParent ) )
  214. return parentDialog;
  215. aParent = aParent->GetParent();
  216. }
  217. return nullptr;
  218. }
  219. void PAGED_DIALOG::SetError( const wxString& aMessage, const wxString& aPageName, int aCtrlId,
  220. int aRow, int aCol )
  221. {
  222. SetError( aMessage, FindWindow( aPageName ), FindWindow( aCtrlId ), aRow, aCol );
  223. }
  224. void PAGED_DIALOG::SetError( const wxString& aMessage, wxWindow* aPage, wxWindow* aCtrl,
  225. int aRow, int aCol )
  226. {
  227. m_infoBar->ShowMessageFor( aMessage, 10000, wxICON_WARNING );
  228. if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( aCtrl ) )
  229. {
  230. textCtrl->SetSelection( -1, -1 );
  231. textCtrl->SetFocus();
  232. return;
  233. }
  234. if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( aCtrl ) )
  235. {
  236. if( aRow > 0 )
  237. {
  238. int pos = scintilla->PositionFromLine( aRow - 1 ) + ( aCol - 1 );
  239. scintilla->GotoPos( pos );
  240. }
  241. scintilla->SetFocus();
  242. return;
  243. }
  244. if( wxGrid* grid = dynamic_cast<wxGrid*>( aCtrl ) )
  245. {
  246. grid->SetFocus();
  247. grid->MakeCellVisible( aRow, aCol );
  248. grid->SetGridCursor( aRow, aCol );
  249. grid->EnableCellEditControl( true );
  250. grid->ShowCellEditControl();
  251. return;
  252. }
  253. }
  254. void PAGED_DIALOG::UpdateResetButton( int aPage )
  255. {
  256. wxWindow* panel = m_treebook->ResolvePage( aPage );
  257. // Enable the reset button only if the page is re-settable
  258. if( m_resetButton )
  259. {
  260. if( panel && ( panel->GetWindowStyle() & wxRESETTABLE ) )
  261. {
  262. m_resetButton->SetLabel( wxString::Format( _( "Reset %s to Defaults" ),
  263. m_treebook->GetPageText( aPage ) ) );
  264. m_resetButton->SetToolTip( panel->GetHelpTextAtPoint( wxPoint( -INT_MAX, INT_MAX ),
  265. wxHelpEvent::Origin_Unknown ) );
  266. m_resetButton->Enable( true );
  267. }
  268. else
  269. {
  270. m_resetButton->SetLabel( _( "Reset to Defaults" ) );
  271. m_resetButton->SetToolTip( wxString() );
  272. m_resetButton->Enable( false );
  273. }
  274. m_resetButton->GetParent()->Layout();
  275. }
  276. }
  277. void PAGED_DIALOG::onCharHook( wxKeyEvent& aEvent )
  278. {
  279. if( dynamic_cast<wxTextEntry*>( aEvent.GetEventObject() )
  280. || dynamic_cast<wxStyledTextCtrl*>( aEvent.GetEventObject() )
  281. || dynamic_cast<wxListView*>( aEvent.GetEventObject() ) )
  282. {
  283. aEvent.Skip();
  284. return;
  285. }
  286. if( aEvent.GetKeyCode() == WXK_UP )
  287. {
  288. int page = m_treebook->GetSelection();
  289. if( page >= 1 )
  290. {
  291. if( m_treebook->GetPage( page - 1 )->GetChildren().IsEmpty() )
  292. m_treebook->SetSelection( std::max( page - 2, 0 ) );
  293. else
  294. m_treebook->SetSelection( page - 1 );
  295. }
  296. m_treebook->GetTreeCtrl()->SetFocus(); // Don't allow preview canvas to steal focus
  297. }
  298. else if( aEvent.GetKeyCode() == WXK_DOWN )
  299. {
  300. int page = m_treebook->GetSelection();
  301. m_treebook->SetSelection( std::min<int>( page + 1, m_treebook->GetPageCount() - 1 ) );
  302. m_treebook->GetTreeCtrl()->SetFocus(); // Don't allow preview canvas to steal focus
  303. }
  304. else
  305. {
  306. aEvent.Skip();
  307. }
  308. }
  309. void PAGED_DIALOG::onPageChanged( wxBookCtrlEvent& event )
  310. {
  311. size_t page = event.GetSelection();
  312. // Use the first sub-page when a tree level node is selected.
  313. if( m_treebook->GetCurrentPage()->GetChildren().IsEmpty()
  314. && page + 1 < m_treebook->GetPageCount() )
  315. {
  316. m_treebook->ChangeSelection( ++page );
  317. }
  318. UpdateResetButton( page );
  319. // Make sure the dialog size is enough to fit the page content.
  320. m_treebook->InvalidateBestSize();
  321. SetMinSize( wxDefaultSize );
  322. wxSize minSize = GetBestSize();
  323. minSize.IncTo( wxSize( 600, 500 ) );
  324. minSize.DecTo( wxSize( 1500, 900 ) ); // Failsafe
  325. SetMinSize( minSize );
  326. wxSize currentSize = GetSize();
  327. wxSize newSize = currentSize;
  328. newSize.IncTo( minSize );
  329. if( newSize != currentSize )
  330. SetSize( newSize );
  331. #ifdef __WXMAC__
  332. // Work around an OSX wxWidgets issue where the wxGrid children don't get placed correctly
  333. // until the first resize event
  334. if( page < m_macHack.size() && m_macHack[ page ] )
  335. {
  336. wxSize pageSize = m_treebook->GetPage( page )->GetSize();
  337. pageSize.x += 1;
  338. pageSize.y += 2;
  339. m_treebook->GetPage( page )->SetSize( pageSize );
  340. m_macHack[ page ] = false;
  341. }
  342. #else
  343. wxSizeEvent evt( wxDefaultSize );
  344. wxQueueEvent( m_treebook, evt.Clone() );
  345. #endif
  346. }
  347. void PAGED_DIALOG::onPageChanging( wxBookCtrlEvent& aEvent )
  348. {
  349. int currentPage = aEvent.GetOldSelection();
  350. if( currentPage == wxNOT_FOUND )
  351. return;
  352. wxWindow* page = m_treebook->GetPage( currentPage );
  353. wxCHECK( page, /* void */ );
  354. // If there is a validation error on the current page, don't allow the page change.
  355. if( !page->Validate() || !page->TransferDataFromWindow() )
  356. {
  357. aEvent.Veto();
  358. return;
  359. }
  360. }
  361. void PAGED_DIALOG::onResetButton( wxCommandEvent& aEvent )
  362. {
  363. int sel = m_treebook->GetSelection();
  364. if( sel == wxNOT_FOUND )
  365. return;
  366. // NB: dynamic_cast doesn't work over Kiway
  367. wxWindow* panel = m_treebook->ResolvePage( sel );
  368. if( panel )
  369. {
  370. wxCommandEvent resetCommand( wxEVT_COMMAND_BUTTON_CLICKED, ID_RESET_PANEL );
  371. panel->ProcessWindowEvent( resetCommand );
  372. }
  373. }
  374. void PAGED_DIALOG::onOpenPreferencesButton( wxCommandEvent& aEvent )
  375. {
  376. wxString dir( PATHS::GetUserSettingsPath() );
  377. LaunchExternal( dir );
  378. }