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
32
33static std::vector<PICKED_SYMBOL> s_SymbolHistoryList;
34static unsigned s_SymbolHistoryMaxCount = 8;
35
36static void AddSymbolToHistory( const PICKED_SYMBOL& aSymbol )
37{
38 // Remove duplicates
40 [&]( const PICKED_SYMBOL& candidate ) -> bool
41 {
42 return candidate.LibId == aSymbol.LibId
43 && candidate.Unit == aSymbol.Unit
44 && candidate.Convert == aSymbol.Convert;
45 } );
46
47 // Add the new name at the beginning of the history list
48 s_SymbolHistoryList.insert( s_SymbolHistoryList.begin(), aSymbol );
49
50 // Remove extra names
53}
54
55
56BEGIN_EVENT_TABLE( SYMBOL_CHOOSER_FRAME, SCH_BASE_FRAME )
57 // Menu (and/or hotkey) events
58 EVT_MENU( wxID_CLOSE, SYMBOL_CHOOSER_FRAME::CloseSymbolChooser )
59 EVT_BUTTON( wxID_OK, SYMBOL_CHOOSER_FRAME::OnOK )
60 EVT_BUTTON( wxID_CANCEL, SYMBOL_CHOOSER_FRAME::CloseSymbolChooser )
62END_EVENT_TABLE()
63
64
65#define PARENT_STYLE ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
66 | wxWANTS_CHARS | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT )
67#define MODAL_STYLE ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
68 | wxWANTS_CHARS | wxFRAME_NO_TASKBAR )
69
70SYMBOL_CHOOSER_FRAME::SYMBOL_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent, bool& aCancelled ) :
71 SCH_BASE_FRAME( aKiway, aParent, FRAME_SYMBOL_CHOOSER, _( "Symbol Chooser" ),
72 wxDefaultPosition, wxDefaultSize, aParent ? PARENT_STYLE : MODAL_STYLE,
74{
75 SetModal( true );
76
77 m_messagePanel->Hide();
78
79 wxBoxSizer* frameSizer = new wxBoxSizer( wxVERTICAL );
80
81 std::vector<PICKED_SYMBOL> dummyAlreadyPlaced;
82 m_chooserPanel = new PANEL_SYMBOL_CHOOSER( this, this, nullptr /* no filter */,
84 dummyAlreadyPlaced, false, false, aCancelled,
85 // Accept handler
86 [this]()
87 {
88 wxCommandEvent dummy;
89 OnOK( dummy );
90 },
91 // Escape handler
92 [this]()
93 {
94 DismissModal( false );
95 } );
96
97
98 frameSizer->Add( m_chooserPanel, 1, wxEXPAND );
99
100 wxPanel* bottomPanel = new wxPanel( this );
101 wxBoxSizer* bottomSizer = new wxBoxSizer( wxVERTICAL );
102
103 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
104 wxButton* okButton = new wxButton( bottomPanel, wxID_OK );
105 wxButton* cancelButton = new wxButton( bottomPanel, wxID_CANCEL );
106 sdbSizer->AddButton( okButton );
107 sdbSizer->AddButton( cancelButton );
108 sdbSizer->Realize();
109
110 bottomSizer->Add( sdbSizer, 1, wxEXPAND | wxALL, 5 );
111
112 bottomPanel->SetSizer( bottomSizer );
113 frameSizer->Add( bottomPanel, 0, wxEXPAND );
114
115 SetSizer( frameSizer );
116 SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ), m_chooserPanel->GetItemCount() ) );
117 Layout();
119
120 Bind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnChar, m_chooserPanel );
121}
122
123
125{
126 Unbind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnChar, m_chooserPanel );
127}
128
129
130bool SYMBOL_CHOOSER_FRAME::ShowModal( wxString* aSymbol, wxWindow* aParent )
131{
132 if( aSymbol && !aSymbol->IsEmpty() )
133 {
134 LIB_ID libid;
135
136 libid.Parse( *aSymbol, true );
137
138 if( libid.IsValid() )
140 }
141
142 return KIWAY_PLAYER::ShowModal( aSymbol, aParent );
143}
144
145
147{
149
150 // Only dismiss a modal frame once, so that the return values set by
151 // the prior DismissModal() are not bashed for ShowModal().
152 if( !IsDismissed() )
153 DismissModal( false );
154
155 // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
156}
157
158
159void SYMBOL_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
160{
162 {
165
166 m_firstPaintEvent = false;
167 }
168
169 aEvent.Skip();
170}
171
172
173void SYMBOL_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
174{
176
177 if( libId.IsValid() )
178 {
179 PICKED_SYMBOL symbol;
180 symbol.LibId = libId;
181
182 AddSymbolToHistory( symbol );
183 DismissModal( true, libId.Format() );
184 }
185 else
186 {
187 DismissModal( false );
188 }
189}
190
191
193{
194 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg ) )
195 return &cfg->m_LibViewPanel.window;
196
197 wxFAIL_MSG( wxT( "SYMBOL_CHOOSER not running with EESCHEMA_SETTINGS" ) );
198 return &aCfg->m_Window; // non-null fail-safe
199}
200
201
202void SYMBOL_CHOOSER_FRAME::CloseSymbolChooser( wxCommandEvent& event )
203{
204 Close( false );
205}
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:234
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:151
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition: wxgtk/ui.cpp:124
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:174
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