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.

177 lines
5.6 KiB

2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <pgm_base.h>
  24. #include <kiplatform/ui.h>
  25. #include <wx/button.h>
  26. #include <sch_base_frame.h>
  27. #include <eeschema_settings.h>
  28. #include <widgets/panel_symbol_chooser.h>
  29. #include <symbol_chooser_frame.h>
  30. static std::vector<PICKED_SYMBOL> s_SymbolHistoryList;
  31. static unsigned s_SymbolHistoryMaxCount = 8;
  32. static void AddSymbolToHistory( const PICKED_SYMBOL& aSymbol )
  33. {
  34. // Remove duplicates
  35. alg::delete_if( s_SymbolHistoryList,
  36. [&]( const PICKED_SYMBOL& candidate ) -> bool
  37. {
  38. return candidate.LibId == aSymbol.LibId
  39. && candidate.Unit == aSymbol.Unit
  40. && candidate.Convert == aSymbol.Convert;
  41. } );
  42. // Add the new name at the beginning of the history list
  43. s_SymbolHistoryList.insert( s_SymbolHistoryList.begin(), aSymbol );
  44. // Remove extra names
  45. while( s_SymbolHistoryList.size() > s_SymbolHistoryMaxCount )
  46. s_SymbolHistoryList.resize( s_SymbolHistoryMaxCount );
  47. }
  48. BEGIN_EVENT_TABLE( SYMBOL_CHOOSER_FRAME, SCH_BASE_FRAME )
  49. // Menu (and/or hotkey) events
  50. EVT_MENU( wxID_CLOSE, SYMBOL_CHOOSER_FRAME::CloseSymbolChooser )
  51. EVT_BUTTON( wxID_OK, SYMBOL_CHOOSER_FRAME::OnOK )
  52. EVT_BUTTON( wxID_CANCEL, SYMBOL_CHOOSER_FRAME::CloseSymbolChooser )
  53. EVT_PAINT( SYMBOL_CHOOSER_FRAME::OnPaint )
  54. END_EVENT_TABLE()
  55. #define MODAL_FRAME ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
  56. | wxWANTS_CHARS | wxFRAME_NO_TASKBAR | wxSTAY_ON_TOP )
  57. SYMBOL_CHOOSER_FRAME::SYMBOL_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
  58. SCH_BASE_FRAME( aKiway, aParent, FRAME_SYMBOL_CHOOSER, _( "Symbol Chooser" ),
  59. wxDefaultPosition, wxDefaultSize, MODAL_FRAME, SYMBOL_CHOOSER_FRAME_NAME )
  60. {
  61. SetModal( true );
  62. wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
  63. std::vector<PICKED_SYMBOL> dummyAlreadyPlaced;
  64. m_chooserPanel =
  65. new PANEL_SYMBOL_CHOOSER( this, this, nullptr /* no filter */, s_SymbolHistoryList,
  66. dummyAlreadyPlaced, false, false,
  67. [this]()
  68. {
  69. wxCommandEvent dummy;
  70. OnOK( dummy );
  71. } );
  72. sizer->Add( m_chooserPanel, 1, wxEXPAND, 5 );
  73. wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
  74. wxButton* okButton = new wxButton( this, wxID_OK );
  75. wxButton* cancelButton = new wxButton( this, wxID_CANCEL );
  76. sdbSizer->AddButton( okButton );
  77. sdbSizer->AddButton( cancelButton );
  78. sdbSizer->Realize();
  79. sizer->Add( sdbSizer, 0, wxEXPAND | wxALL, 5 );
  80. SetSizer( sizer );
  81. SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ),
  82. m_chooserPanel->GetItemCount() ) );
  83. Layout();
  84. m_chooserPanel->FinishSetup();
  85. }
  86. bool SYMBOL_CHOOSER_FRAME::ShowModal( wxString* aSymbol, wxWindow* aParent )
  87. {
  88. if( aSymbol && !aSymbol->IsEmpty() )
  89. {
  90. LIB_ID libid;
  91. libid.Parse( *aSymbol, true );
  92. if( libid.IsValid() )
  93. m_chooserPanel->SetPreselect( libid );
  94. }
  95. return KIWAY_PLAYER::ShowModal( aSymbol, aParent );
  96. }
  97. void SYMBOL_CHOOSER_FRAME::doCloseWindow()
  98. {
  99. // Only dismiss a modal frame once, so that the return values set by
  100. // the prior DismissModal() are not bashed for ShowModal().
  101. if( !IsDismissed() )
  102. DismissModal( false );
  103. // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
  104. }
  105. void SYMBOL_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
  106. {
  107. if( m_firstPaintEvent )
  108. {
  109. KIPLATFORM::UI::FixupCancelButtonCmdKeyCollision( this );
  110. KIPLATFORM::UI::ForceFocus( m_chooserPanel->GetFocusTarget() );
  111. m_firstPaintEvent = false;
  112. }
  113. aEvent.Skip();
  114. }
  115. void SYMBOL_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
  116. {
  117. LIB_ID libId = m_chooserPanel->GetSelectedLibId();
  118. if( libId.IsValid() )
  119. {
  120. PICKED_SYMBOL symbol;
  121. symbol.LibId = libId;
  122. AddSymbolToHistory( symbol );
  123. DismissModal( true, libId.Format() );
  124. }
  125. else
  126. {
  127. DismissModal( false );
  128. }
  129. }
  130. WINDOW_SETTINGS* SYMBOL_CHOOSER_FRAME::GetWindowSettings( APP_SETTINGS_BASE* aCfg )
  131. {
  132. EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
  133. wxASSERT( cfg );
  134. return &cfg->m_LibViewPanel.window;
  135. }
  136. void SYMBOL_CHOOSER_FRAME::CloseSymbolChooser( wxCommandEvent& event )
  137. {
  138. Close( false );
  139. }