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.

191 lines
6.2 KiB

1 year ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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. m_messagePanel->Hide();
  63. wxBoxSizer* frameSizer = new wxBoxSizer( wxVERTICAL );
  64. std::vector<PICKED_SYMBOL> dummyAlreadyPlaced;
  65. m_chooserPanel = new PANEL_SYMBOL_CHOOSER( this, this, nullptr /* no filter */,
  66. s_SymbolHistoryList,
  67. dummyAlreadyPlaced, false, false,
  68. [this]()
  69. {
  70. wxCommandEvent dummy;
  71. OnOK( dummy );
  72. },
  73. [this]()
  74. {
  75. DismissModal( false );
  76. } );
  77. frameSizer->Add( m_chooserPanel, 1, wxEXPAND );
  78. wxPanel* bottomPanel = new wxPanel( this );
  79. wxBoxSizer* bottomSizer = new wxBoxSizer( wxVERTICAL );
  80. wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
  81. wxButton* okButton = new wxButton( bottomPanel, wxID_OK );
  82. wxButton* cancelButton = new wxButton( bottomPanel, wxID_CANCEL );
  83. sdbSizer->AddButton( okButton );
  84. sdbSizer->AddButton( cancelButton );
  85. sdbSizer->Realize();
  86. bottomSizer->Add( sdbSizer, 1, wxEXPAND | wxALL, 5 );
  87. bottomPanel->SetSizer( bottomSizer );
  88. frameSizer->Add( bottomPanel, 0, wxEXPAND );
  89. SetSizer( frameSizer );
  90. SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ),
  91. m_chooserPanel->GetItemCount() ) );
  92. Layout();
  93. m_chooserPanel->FinishSetup();
  94. }
  95. bool SYMBOL_CHOOSER_FRAME::ShowModal( wxString* aSymbol, wxWindow* aParent )
  96. {
  97. if( aSymbol && !aSymbol->IsEmpty() )
  98. {
  99. LIB_ID libid;
  100. libid.Parse( *aSymbol, true );
  101. if( libid.IsValid() )
  102. m_chooserPanel->SetPreselect( libid );
  103. }
  104. return KIWAY_PLAYER::ShowModal( aSymbol, aParent );
  105. }
  106. void SYMBOL_CHOOSER_FRAME::doCloseWindow()
  107. {
  108. // Only dismiss a modal frame once, so that the return values set by
  109. // the prior DismissModal() are not bashed for ShowModal().
  110. if( !IsDismissed() )
  111. DismissModal( false );
  112. // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
  113. }
  114. void SYMBOL_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
  115. {
  116. if( m_firstPaintEvent )
  117. {
  118. KIPLATFORM::UI::FixupCancelButtonCmdKeyCollision( this );
  119. KIPLATFORM::UI::ForceFocus( m_chooserPanel->GetFocusTarget() );
  120. m_firstPaintEvent = false;
  121. }
  122. aEvent.Skip();
  123. }
  124. void SYMBOL_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
  125. {
  126. LIB_ID libId = m_chooserPanel->GetSelectedLibId();
  127. if( libId.IsValid() )
  128. {
  129. PICKED_SYMBOL symbol;
  130. symbol.LibId = libId;
  131. AddSymbolToHistory( symbol );
  132. DismissModal( true, libId.Format() );
  133. }
  134. else
  135. {
  136. DismissModal( false );
  137. }
  138. }
  139. WINDOW_SETTINGS* SYMBOL_CHOOSER_FRAME::GetWindowSettings( APP_SETTINGS_BASE* aCfg )
  140. {
  141. EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
  142. wxASSERT( cfg );
  143. return &cfg->m_LibViewPanel.window;
  144. }
  145. void SYMBOL_CHOOSER_FRAME::CloseSymbolChooser( wxCommandEvent& event )
  146. {
  147. Close( false );
  148. }