KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_chooser_frame.cpp
Go to the documentation of this file.
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
24#include <pgm_base.h>
25#include <kiplatform/ui.h>
26#include <wx/button.h>
27#include <sch_base_frame.h>
28#include <eeschema_settings.h>
31#include <algorithm>
32
33
34static std::vector<PICKED_SYMBOL> s_SymbolHistoryList;
35static unsigned s_SymbolHistoryMaxCount = 8;
36
37static void AddSymbolToHistory( const PICKED_SYMBOL& aSymbol )
38{
39 // Remove duplicates
40 std::erase_if( s_SymbolHistoryList,
41 [&]( const PICKED_SYMBOL& candidate ) -> bool
42 {
43 return candidate.LibId == aSymbol.LibId
44 && candidate.Unit == aSymbol.Unit
45 && candidate.Convert == aSymbol.Convert;
46 } );
47
48 // Add the new name at the beginning of the history list
49 s_SymbolHistoryList.insert( s_SymbolHistoryList.begin(), aSymbol );
50
51 // Remove extra names
54}
55
56
57BEGIN_EVENT_TABLE( SYMBOL_CHOOSER_FRAME, SCH_BASE_FRAME )
58 // Menu (and/or hotkey) events
59 EVT_MENU( wxID_CLOSE, SYMBOL_CHOOSER_FRAME::CloseSymbolChooser )
60 EVT_BUTTON( wxID_OK, SYMBOL_CHOOSER_FRAME::OnOK )
61 EVT_BUTTON( wxID_CANCEL, SYMBOL_CHOOSER_FRAME::CloseSymbolChooser )
63END_EVENT_TABLE()
64
65
66#define PARENT_STYLE ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
67 | wxWANTS_CHARS | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT )
68#define MODAL_STYLE ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
69 | wxWANTS_CHARS | wxFRAME_NO_TASKBAR )
70
71SYMBOL_CHOOSER_FRAME::SYMBOL_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent, bool& aCancelled ) :
72 SCH_BASE_FRAME( aKiway, aParent, FRAME_SYMBOL_CHOOSER, _( "Symbol Chooser" ),
73 wxDefaultPosition, wxDefaultSize, aParent ? PARENT_STYLE : MODAL_STYLE,
75{
76 SetModal( true );
77
78 m_messagePanel->Hide();
79
80 wxBoxSizer* frameSizer = new wxBoxSizer( wxVERTICAL );
81
82 std::vector<PICKED_SYMBOL> dummyAlreadyPlaced;
83 m_chooserPanel = new PANEL_SYMBOL_CHOOSER( this, this, nullptr /* no filter */,
85 dummyAlreadyPlaced, false, false, aCancelled,
86 // Accept handler
87 [this]()
88 {
89 wxCommandEvent dummy;
90 OnOK( dummy );
91 },
92 // Escape handler
93 [this]()
94 {
95 DismissModal( false );
96 } );
97
98
99 frameSizer->Add( m_chooserPanel, 1, wxEXPAND );
100
101 wxPanel* bottomPanel = new wxPanel( this );
102 wxBoxSizer* bottomSizer = new wxBoxSizer( wxVERTICAL );
103
104 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
105 wxButton* okButton = new wxButton( bottomPanel, wxID_OK );
106 wxButton* cancelButton = new wxButton( bottomPanel, wxID_CANCEL );
107 sdbSizer->AddButton( okButton );
108 sdbSizer->AddButton( cancelButton );
109 sdbSizer->Realize();
110
111 bottomSizer->Add( sdbSizer, 1, wxEXPAND | wxALL, 5 );
112
113 bottomPanel->SetSizer( bottomSizer );
114 frameSizer->Add( bottomPanel, 0, wxEXPAND );
115
116 SetSizer( frameSizer );
117 SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ), m_chooserPanel->GetItemCount() ) );
118 Layout();
120
121 Bind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnChar, m_chooserPanel );
122}
123
124
126{
127 Unbind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnChar, m_chooserPanel );
128}
129
130
131bool SYMBOL_CHOOSER_FRAME::ShowModal( wxString* aSymbol, wxWindow* aParent )
132{
133 if( aSymbol && !aSymbol->IsEmpty() )
134 {
135 LIB_ID libid;
136
137 libid.Parse( *aSymbol, true );
138
139 if( libid.IsValid() )
141 }
142
143 return KIWAY_PLAYER::ShowModal( aSymbol, aParent );
144}
145
146
148{
150
151 // Only dismiss a modal frame once, so that the return values set by
152 // the prior DismissModal() are not bashed for ShowModal().
153 if( !IsDismissed() )
154 DismissModal( false );
155
156 // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
157}
158
159
160void SYMBOL_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
161{
163 {
166
167 m_firstPaintEvent = false;
168 }
169
170 aEvent.Skip();
171}
172
173
174void SYMBOL_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
175{
177
178 if( libId.IsValid() )
179 {
180 PICKED_SYMBOL symbol;
181 symbol.LibId = libId;
182
183 AddSymbolToHistory( symbol );
184 DismissModal( true, libId.Format() );
185 }
186 else
187 {
188 DismissModal( false );
189 }
190}
191
192
194{
195 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg ) )
196 return &cfg->m_LibViewPanel.window;
197
198 wxFAIL_MSG( wxT( "SYMBOL_CHOOSER not running with EESCHEMA_SETTINGS" ) );
199 return &aCfg->m_Window; // non-null fail-safe
200}
201
202
203void SYMBOL_CHOOSER_FRAME::CloseSymbolChooser( wxCommandEvent& event )
204{
205 Close( false );
206}
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:108
WINDOW_SETTINGS m_Window
Definition: app_settings.h:233
EDA_MSG_PANEL * m_messagePanel
virtual bool ShowModal(wxString *aResult=nullptr, wxWindow *aResultantFocusWindow=nullptr)
Show this wxFrame as if it were a modal dialog, with all other instantiated wxFrames disabled until t...
bool IsDismissed()
void SetModal(bool aIsModal)
Definition: kiway_player.h:155
void DismissModal(bool aRetVal, const wxString &aResult=wxEmptyString)
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:286
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:52
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
UTF8 Format() const
Definition: lib_id.cpp:119
wxWindow * GetFocusTarget() const
void OnChar(wxKeyEvent &aEvent)
void SetPreselect(const LIB_ID &aPreselect)
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
Symbol library viewer main window.
void OnOK(wxCommandEvent &aEvent)
SYMBOL_CHOOSER_FRAME(KIWAY *aKiway, wxWindow *aParent, bool &aCancelled)
void OnPaint(wxPaintEvent &aEvent)
WINDOW_SETTINGS * GetWindowSettings(APP_SETTINGS_BASE *aCfg) override
Return a pointer to the window settings for this frame.
bool ShowModal(wxString *aSymbol, wxWindow *aParent) override
Runs the symbol viewer as a modal dialog.
PANEL_SYMBOL_CHOOSER * m_chooserPanel
void CloseSymbolChooser(wxCommandEvent &aEvent)
#define _(s)
#define SYMBOL_CHOOSER_FRAME_NAME
@ FRAME_SYMBOL_CHOOSER
Definition: frame_type.h:37
void FixupCancelButtonCmdKeyCollision(wxWindow *aWindow)
Definition: wxgtk/ui.cpp:157
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition: wxgtk/ui.cpp:124
see class PGM_BASE
std::vector< FAB_LAYER_COLOR > dummy
LIB_ID LibId
Definition: sch_screen.h:80
Store the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:90
#define PARENT_STYLE
static void AddSymbolToHistory(const PICKED_SYMBOL &aSymbol)
#define MODAL_STYLE
static std::vector< PICKED_SYMBOL > s_SymbolHistoryList
static unsigned s_SymbolHistoryMaxCount