KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_text_button_helpers.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 (C) 2021 CERN
5 * Copyright (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <wx/combo.h>
26#include <wx/filedlg.h>
27#include <wx/dirdlg.h>
28#include <wx/textctrl.h>
29
30#include <bitmaps.h>
31#include <kiway.h>
32#include <kiway_player.h>
33#include <kiway_express.h>
34#include <string_utils.h>
35#include <dialog_shim.h>
36#include <common.h>
37#include <env_paths.h>
38#include <pgm_base.h>
39#include <widgets/wx_grid.h>
41#include <eda_doc.h>
42
43
44//-------- Renderer ---------------------------------------------------------------------
45// None required; just render as normal text.
46
47
48
49//-------- Editor Base Class ------------------------------------------------------------
50//
51// Note: this implementation is an adaptation of wxGridCellChoiceEditor
52
53
55{
56 return Combo()->GetValue();
57}
58
59
60void GRID_CELL_TEXT_BUTTON::SetSize( const wxRect& aRect )
61{
62 wxRect rect( aRect );
64
65 wxGridCellEditor::SetSize( rect );
66}
67
68
69void GRID_CELL_TEXT_BUTTON::StartingKey( wxKeyEvent& event )
70{
71 // Note: this is a copy of wxGridCellTextEditor's StartingKey()
72
73 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
74 // longer an appropriate way to get the character into the text control.
75 // Do it ourselves instead. We know that if we get this far that we have
76 // a valid character, so not a whole lot of testing needs to be done.
77
78 // wxComboCtrl inherits from wxTextEntry, so can statically cast
79 wxTextEntry* textEntry = static_cast<wxTextEntry*>( Combo() );
80 int ch;
81
82 bool isPrintable;
83
84#if wxUSE_UNICODE
85 ch = event.GetUnicodeKey();
86
87 if( ch != WXK_NONE )
88 isPrintable = true;
89 else
90#endif // wxUSE_UNICODE
91 {
92 ch = event.GetKeyCode();
93 isPrintable = ch >= WXK_SPACE && ch < WXK_START;
94 }
95
96 switch( ch )
97 {
98 case WXK_DELETE:
99 // Delete the initial character when starting to edit with DELETE.
100 textEntry->Remove( 0, 1 );
101 break;
102
103 case WXK_BACK:
104 // Delete the last character when starting to edit with BACKSPACE.
105 {
106 const long pos = textEntry->GetLastPosition();
107 textEntry->Remove( pos - 1, pos );
108 }
109 break;
110
111 default:
112 if( isPrintable )
113 textEntry->WriteText( static_cast<wxChar>( ch ) );
114 break;
115 }
116}
117
118
119void GRID_CELL_TEXT_BUTTON::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
120{
121 auto evtHandler = static_cast< wxGridCellEditorEvtHandler* >( m_control->GetEventHandler() );
122
123 // Don't immediately end if we get a kill focus event within BeginEdit
124 evtHandler->SetInSetFocus( true );
125
126 m_value = aGrid->GetTable()->GetValue( aRow, aCol );
127
128 Combo()->SetValue( m_value );
129 Combo()->SetFocus();
130}
131
132
133bool GRID_CELL_TEXT_BUTTON::EndEdit( int, int, const wxGrid*, const wxString&, wxString *aNewVal )
134{
135 const wxString value = Combo()->GetValue();
136
137 if( value == m_value )
138 return false;
139
140 m_value = value;
141
142 if( aNewVal )
143 *aNewVal = value;
144
145 return true;
146}
147
148
149void GRID_CELL_TEXT_BUTTON::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
150{
151 aGrid->GetTable()->SetValue( aRow, aCol, m_value );
152}
153
154
156{
157 Combo()->SetValue( m_value );
158}
159
160
161#if wxUSE_VALIDATORS
162void GRID_CELL_TEXT_BUTTON::SetValidator( const wxValidator& validator )
163{
164 m_validator.reset( static_cast< wxValidator* >( validator.Clone() ) );
165}
166#endif
167
168
169class TEXT_BUTTON_SYMBOL_CHOOSER : public wxComboCtrl
170{
171public:
172 TEXT_BUTTON_SYMBOL_CHOOSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg,
173 const wxString& aPreselect ) :
174 wxComboCtrl( aParent ),
175 m_dlg( aParentDlg ),
176 m_preselect( aPreselect )
177 {
178 SetButtonBitmaps( KiBitmap( BITMAPS::small_library ) );
179
180 // win32 fix, avoids drawing the "native dropdown caret"
181 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
182 }
183
184protected:
185 void DoSetPopupControl( wxComboPopup* popup ) override
186 {
187 m_popup = nullptr;
188 }
189
190 wxString escapeLibId( const wxString& aRawValue )
191 {
192 wxString itemName;
193 wxString libName = aRawValue.BeforeFirst( ':', &itemName );
194 return EscapeString( libName, CTX_LIBID ) + ':' + EscapeString( itemName, CTX_LIBID );
195 }
196
197 void OnButtonClick() override
198 {
199 // pick a symbol using the symbol picker.
200 wxString rawValue = GetValue();
201
202 if( rawValue.IsEmpty() )
203 rawValue = m_preselect;
204
205 wxString symbolId = escapeLibId( rawValue );
206
207 if( KIWAY_PLAYER* frame = m_dlg->Kiway().Player( FRAME_SYMBOL_CHOOSER, true, m_dlg ) )
208 {
209 if( frame->ShowModal( &symbolId, m_dlg ) )
210 SetValue( UnescapeString( symbolId ) );
211
212 frame->Destroy();
213 }
214 }
215
217 wxString m_preselect;
218};
219
220
221void GRID_CELL_SYMBOL_ID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
222 wxEvtHandler* aEventHandler )
223{
224 m_control = new TEXT_BUTTON_SYMBOL_CHOOSER( aParent, m_dlg, m_preselect );
226
227 wxGridCellEditor::Create( aParent, aId, aEventHandler );
228}
229
230
231class TEXT_BUTTON_FP_CHOOSER : public wxComboCtrl
232{
233public:
234 TEXT_BUTTON_FP_CHOOSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg,
235 const wxString& aSymbolNetlist, const wxString& aPreselect ) :
236 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
237 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
238 m_dlg( aParentDlg ),
239 m_preselect( aPreselect ),
240 m_symbolNetlist( aSymbolNetlist.ToStdString() )
241 {
242 SetButtonBitmaps( KiBitmap( BITMAPS::small_library ) );
243
244 // win32 fix, avoids drawing the "native dropdown caret"
245 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
246 }
247
248protected:
249 void DoSetPopupControl( wxComboPopup* popup ) override
250 {
251 m_popup = nullptr;
252 }
253
254 void OnButtonClick() override
255 {
256 // pick a footprint using the footprint picker.
257 wxString fpid = GetValue();
258
259 if( fpid.IsEmpty() )
260 fpid = m_preselect;
261
262 // Disable the button until we have finished processing it. Normally this is not an issue
263 // but if the footprint chooser is loading for the first time, it can be slow enough that
264 // multiple clicks will cause multiple instances of the footprint loader process to start
265 Disable();
266
267 if( KIWAY_PLAYER* frame = m_dlg->Kiway().Player( FRAME_FOOTPRINT_CHOOSER, true, m_dlg ) )
268 {
269 if( !m_symbolNetlist.empty() )
270 {
272 frame->KiwayMailIn( event );
273 }
274
275 if( frame->ShowModal( &fpid, m_dlg ) )
276 SetValue( fpid );
277
278 frame->Destroy();
279 }
280
281 Enable();
282 }
283
284protected:
286 wxString m_preselect;
287
288 /*
289 * Symbol netlist format:
290 * pinNumber pinName <tab> pinNumber pinName...
291 * fpFilter fpFilter...
292 */
293 std::string m_symbolNetlist;
294};
295
296
297void GRID_CELL_FPID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
298 wxEvtHandler* aEventHandler )
299{
300 m_control = new TEXT_BUTTON_FP_CHOOSER( aParent, m_dlg, m_symbolNetlist, m_preselect );
302
303#if wxUSE_VALIDATORS
304 // validate text in textctrl, if validator is set
305 if ( m_validator )
306 {
307 Combo()->SetValidator( *m_validator );
308 }
309#endif
310
311 wxGridCellEditor::Create( aParent, aId, aEventHandler );
312}
313
314
315class TEXT_BUTTON_URL : public wxComboCtrl
316{
317public:
318 TEXT_BUTTON_URL( wxWindow* aParent, DIALOG_SHIM* aParentDlg, SEARCH_STACK* aSearchStack ) :
319 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
320 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
321 m_dlg( aParentDlg ),
322 m_searchStack( aSearchStack )
323 {
324 SetButtonBitmaps( KiBitmap( BITMAPS::www ) );
325
326 // win32 fix, avoids drawing the "native dropdown caret"
327 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
328 }
329
330protected:
331 void DoSetPopupControl( wxComboPopup* popup ) override
332 {
333 m_popup = nullptr;
334 }
335
336 void OnButtonClick() override
337 {
338 wxString filename = GetValue();
339
340 if( !filename.IsEmpty() && filename != wxT( "~" ) )
342 }
343
346};
347
348
349void GRID_CELL_URL_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
350 wxEvtHandler* aEventHandler )
351{
352 m_control = new TEXT_BUTTON_URL( aParent, m_dlg, m_searchStack );
354
355#if wxUSE_VALIDATORS
356 // validate text in textctrl, if validator is set
357 if ( m_validator )
358 {
359 Combo()->SetValidator( *m_validator );
360 }
361#endif
362
363 wxGridCellEditor::Create( aParent, aId, aEventHandler );
364}
365
366
367class TEXT_BUTTON_FILE_BROWSER : public wxComboCtrl
368{
369public:
370 TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
371 wxString* aCurrentDir, const wxString& aFileFilter = wxEmptyString,
372 bool aNormalize = false,
373 const wxString& aNormalizeBasePath = wxEmptyString ) :
374 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
375 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
376 m_dlg( aParentDlg ),
377 m_grid( aGrid ),
378 m_currentDir( aCurrentDir ),
379 m_normalize( aNormalize ),
380 m_normalizeBasePath( aNormalizeBasePath ),
381 m_fileFilter( aFileFilter )
382 {
383 SetButtonBitmaps( KiBitmap( BITMAPS::small_folder ) );
384
385 // win32 fix, avoids drawing the "native dropdown caret"
386 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
387 }
388
389 TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
390 wxString* aCurrentDir,
391 std::function<wxString( WX_GRID* grid, int row )> aFileFilterFn,
392 bool aNormalize = false,
393 const wxString& aNormalizeBasePath = wxEmptyString ) :
394 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
395 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
396 m_dlg( aParentDlg ),
397 m_grid( aGrid ),
398 m_currentDir( aCurrentDir ),
399 m_normalize( aNormalize ),
400 m_normalizeBasePath( aNormalizeBasePath ),
401 m_fileFilterFn( std::move( aFileFilterFn ) )
402 {
403 SetButtonBitmaps( KiBitmap( BITMAPS::small_folder ) );
404
405 // win32 fix, avoids drawing the "native dropdown caret"
406 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
407 }
408
409
410protected:
411 void DoSetPopupControl( wxComboPopup* popup ) override
412 {
413 m_popup = nullptr;
414 }
415
416 void OnButtonClick() override
417 {
418 if( m_fileFilterFn )
419 m_fileFilter = m_fileFilterFn( m_grid, m_grid->GetGridCursorRow() );
420
421 wxFileName fn = GetValue();
422
423 if( fn.GetPath().IsEmpty() && m_currentDir )
424 fn.SetPath( *m_currentDir );
425 else
426 fn.SetPath( ExpandEnvVarSubstitutions( fn.GetPath(), &m_dlg->Prj() ) );
427
428 if( !m_fileFilter.IsEmpty() )
429 {
430 wxFileDialog dlg( m_dlg, _( "Select a File" ), fn.GetPath(), fn.GetFullName(),
431 m_fileFilter, wxFD_FILE_MUST_EXIST | wxFD_OPEN );
432
433 if( dlg.ShowModal() == wxID_OK )
434 {
435 wxString filePath = dlg.GetPath();
436 wxString lastPath = dlg.GetDirectory();
437 wxString relPath = wxEmptyString;
438
439 if( m_normalize )
440 {
441 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
443 lastPath = NormalizePath( dlg.GetDirectory(), &Pgm().GetLocalEnvVariables(),
445 }
446 else
447 {
448 relPath = filePath;
449 }
450
451 SetValue( relPath );
452
454 {;} // shouldn't happen, but Coverity doesn't know that
455
456 if( m_currentDir )
457 *m_currentDir = lastPath;
458 }
459 }
460 else
461 {
462 wxDirDialog dlg( m_dlg, _( "Select Path" ), fn.GetPath(),
463 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
464
465 if( dlg.ShowModal() == wxID_OK )
466 {
467 wxString filePath = dlg.GetPath();
468 wxString relPath = wxEmptyString;
469
470 if ( m_normalize )
471 {
472 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
474 }
475 else
476 {
477 relPath = filePath;
478 }
479
480 SetValue( relPath );
481
483 {;} // shouldn't happen, but Coverity doesn't know that
484
485 *m_currentDir = relPath;
486 }
487 }
488 }
489
492 wxString* m_currentDir;
495
496 wxString m_fileFilter;
497 std::function<wxString( WX_GRID* aGrid, int aRow )> m_fileFilterFn;
498};
499
500
501void GRID_CELL_PATH_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
502 wxEvtHandler* aEventHandler )
503{
504 if( m_fileFilterFn )
507 else
510
512
513#if wxUSE_VALIDATORS
514 // validate text in textctrl, if validator is set
515 if ( m_validator )
516 {
517 Combo()->SetValidator( *m_validator );
518 }
519#endif
520
521 wxGridCellEditor::Create( aParent, aId, aEventHandler );
522}
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:104
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:88
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
std::function< wxString(WX_GRID *aGrid, int aRow)> m_fileFilterFn
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
wxString GetValue() const override
wxComboCtrl * Combo() const
void StartingKey(wxKeyEvent &event) override
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
bool EndEdit(int, int, const wxGrid *, const wxString &, wxString *aNewVal) override
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
void SetSize(const wxRect &aRect) override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
Carry a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition: kiway_express.h:40
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:55
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:65
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:406
Look for files in a number of paths.
Definition: search_stack.h:43
void DoSetPopupControl(wxComboPopup *popup) override
std::function< wxString(WX_GRID *aGrid, int aRow)> m_fileFilterFn
TEXT_BUTTON_FILE_BROWSER(wxWindow *aParent, DIALOG_SHIM *aParentDlg, WX_GRID *aGrid, wxString *aCurrentDir, std::function< wxString(WX_GRID *grid, int row)> aFileFilterFn, bool aNormalize=false, const wxString &aNormalizeBasePath=wxEmptyString)
TEXT_BUTTON_FILE_BROWSER(wxWindow *aParent, DIALOG_SHIM *aParentDlg, WX_GRID *aGrid, wxString *aCurrentDir, const wxString &aFileFilter=wxEmptyString, bool aNormalize=false, const wxString &aNormalizeBasePath=wxEmptyString)
void DoSetPopupControl(wxComboPopup *popup) override
TEXT_BUTTON_FP_CHOOSER(wxWindow *aParent, DIALOG_SHIM *aParentDlg, const wxString &aSymbolNetlist, const wxString &aPreselect)
void DoSetPopupControl(wxComboPopup *popup) override
wxString escapeLibId(const wxString &aRawValue)
TEXT_BUTTON_SYMBOL_CHOOSER(wxWindow *aParent, DIALOG_SHIM *aParentDlg, const wxString &aPreselect)
TEXT_BUTTON_URL(wxWindow *aParent, DIALOG_SHIM *aParentDlg, SEARCH_STACK *aSearchStack)
void DoSetPopupControl(wxComboPopup *popup) override
static void CellEditorSetMargins(wxTextEntryBase *aEntry)
A helper function to set OS-specific margins for text-based cell editors.
Definition: wx_grid.cpp:74
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:590
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
Definition: wx_grid.cpp:81
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:334
The common library.
#define _(s)
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths)
Open a document (file) with the suitable browser.
Definition: eda_doc.cpp:61
This file is part of the common library.
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalize a file path to an environmental variable, if possible.
Definition: env_paths.cpp:71
@ FRAME_FOOTPRINT_CHOOSER
Definition: frame_type.h:44
@ FRAME_SYMBOL_CHOOSER
Definition: frame_type.h:37
@ MAIL_SYMBOL_NETLIST
Definition: mail_type.h:45
STL namespace.
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
see class PGM_BASE
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LIBID
Definition: string_utils.h:54