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-2024 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/checkbox.h>
26#include <wx/combo.h>
27#include <wx/filedlg.h>
28#include <wx/dirdlg.h>
29#include <wx/textctrl.h>
30
31#include <bitmaps.h>
32#include <embedded_files.h>
33#include <kiway.h>
34#include <kiway_player.h>
35#include <kiway_express.h>
36#include <string_utils.h>
37#include <dialog_shim.h>
38#include <common.h>
39#include <env_paths.h>
40#include <pgm_base.h>
41#include <widgets/wx_grid.h>
44#include <eda_doc.h>
45
46
47//-------- Renderer ---------------------------------------------------------------------
48// None required; just render as normal text.
49
50
51
52//-------- Editor Base Class ------------------------------------------------------------
53//
54// Note: this implementation is an adaptation of wxGridCellChoiceEditor
55
56
58{
59 return Combo()->GetValue();
60}
61
62
63void GRID_CELL_TEXT_BUTTON::SetSize( const wxRect& aRect )
64{
65 wxRect rect( aRect );
67
68 wxGridCellEditor::SetSize( rect );
69}
70
71
72void GRID_CELL_TEXT_BUTTON::StartingKey( wxKeyEvent& event )
73{
74 // Note: this is a copy of wxGridCellTextEditor's StartingKey()
75
76 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
77 // longer an appropriate way to get the character into the text control.
78 // Do it ourselves instead. We know that if we get this far that we have
79 // a valid character, so not a whole lot of testing needs to be done.
80
81 // wxComboCtrl inherits from wxTextEntry, so can statically cast
82 wxTextEntry* textEntry = static_cast<wxTextEntry*>( Combo() );
83 int ch;
84
85 bool isPrintable;
86
87#if wxUSE_UNICODE
88 ch = event.GetUnicodeKey();
89
90 if( ch != WXK_NONE )
91 isPrintable = true;
92 else
93#endif // wxUSE_UNICODE
94 {
95 ch = event.GetKeyCode();
96 isPrintable = ch >= WXK_SPACE && ch < WXK_START;
97 }
98
99 switch( ch )
100 {
101 case WXK_DELETE:
102 // Delete the initial character when starting to edit with DELETE.
103 textEntry->Remove( 0, 1 );
104 break;
105
106 case WXK_BACK:
107 // Delete the last character when starting to edit with BACKSPACE.
108 {
109 const long pos = textEntry->GetLastPosition();
110 textEntry->Remove( pos - 1, pos );
111 }
112 break;
113
114 default:
115 if( isPrintable )
116 textEntry->WriteText( static_cast<wxChar>( ch ) );
117 break;
118 }
119}
120
121
122void GRID_CELL_TEXT_BUTTON::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
123{
124 auto evtHandler = static_cast< wxGridCellEditorEvtHandler* >( m_control->GetEventHandler() );
125
126 // Don't immediately end if we get a kill focus event within BeginEdit
127 evtHandler->SetInSetFocus( true );
128
129 m_value = aGrid->GetTable()->GetValue( aRow, aCol );
130
131 Combo()->SetValue( m_value );
132 Combo()->SetFocus();
133}
134
135
136bool GRID_CELL_TEXT_BUTTON::EndEdit( int, int, const wxGrid*, const wxString&, wxString *aNewVal )
137{
138 const wxString value = Combo()->GetValue();
139
140 if( value == m_value )
141 return false;
142
143 m_value = value;
144
145 if( aNewVal )
146 *aNewVal = value;
147
148 return true;
149}
150
151
152void GRID_CELL_TEXT_BUTTON::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
153{
154 aGrid->GetTable()->SetValue( aRow, aCol, m_value );
155}
156
157
159{
160 Combo()->SetValue( m_value );
161}
162
163
164#if wxUSE_VALIDATORS
165void GRID_CELL_TEXT_BUTTON::SetValidator( const wxValidator& validator )
166{
167 m_validator.reset( static_cast< wxValidator* >( validator.Clone() ) );
168}
169#endif
170
171
172class TEXT_BUTTON_SYMBOL_CHOOSER : public wxComboCtrl
173{
174public:
175 TEXT_BUTTON_SYMBOL_CHOOSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg,
176 const wxString& aPreselect ) :
177 wxComboCtrl( aParent ),
178 m_dlg( aParentDlg ),
179 m_preselect( aPreselect )
180 {
181 SetButtonBitmaps( KiBitmapBundle( BITMAPS::small_library ) );
182
183 // win32 fix, avoids drawing the "native dropdown caret"
184 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
185 }
186
187protected:
188 void DoSetPopupControl( wxComboPopup* popup ) override
189 {
190 m_popup = nullptr;
191 }
192
193 wxString escapeLibId( const wxString& aRawValue )
194 {
195 wxString itemName;
196 wxString libName = aRawValue.BeforeFirst( ':', &itemName );
197 return EscapeString( libName, CTX_LIBID ) + ':' + EscapeString( itemName, CTX_LIBID );
198 }
199
200 void OnButtonClick() override
201 {
202 // pick a symbol using the symbol picker.
203 wxString rawValue = GetValue();
204
205 if( rawValue.IsEmpty() )
206 rawValue = m_preselect;
207
208 wxString symbolId = escapeLibId( rawValue );
209
210 if( KIWAY_PLAYER* frame = m_dlg->Kiway().Player( FRAME_SYMBOL_CHOOSER, true, m_dlg ) )
211 {
212 if( frame->ShowModal( &symbolId, m_dlg ) )
213 SetValue( UnescapeString( symbolId ) );
214
215 frame->Destroy();
216 }
217 }
218
220 wxString m_preselect;
221};
222
223
224void GRID_CELL_SYMBOL_ID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
225 wxEvtHandler* aEventHandler )
226{
227 m_control = new TEXT_BUTTON_SYMBOL_CHOOSER( aParent, m_dlg, m_preselect );
229
230 wxGridCellEditor::Create( aParent, aId, aEventHandler );
231}
232
233
234class TEXT_BUTTON_FP_CHOOSER : public wxComboCtrl
235{
236public:
237 TEXT_BUTTON_FP_CHOOSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg,
238 const wxString& aSymbolNetlist, const wxString& aPreselect ) :
239 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
240 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
241 m_dlg( aParentDlg ),
242 m_preselect( aPreselect ),
243 m_symbolNetlist( aSymbolNetlist.ToStdString() )
244 {
245 m_buttonFpChooserLock = false;
246 SetButtonBitmaps( KiBitmapBundle( BITMAPS::small_library ) );
247
248 // win32 fix, avoids drawing the "native dropdown caret"
249 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
250 }
251
252protected:
253 void DoSetPopupControl( wxComboPopup* popup ) override
254 {
255 m_popup = nullptr;
256 }
257
258 void OnButtonClick() override
259 {
260 if( m_buttonFpChooserLock ) // The button to show the FP chooser is clicked, but
261 // a previous click is currently in progress.
262 return;
263
264 // Disable the button until we have finished processing it. Normally this is not an issue
265 // but if the footprint chooser is loading for the first time, it can be slow enough that
266 // multiple clicks will cause multiple instances of the footprint loader process to start
268
269 // pick a footprint using the footprint picker.
270 wxString fpid = GetValue();
271
272 if( fpid.IsEmpty() )
273 fpid = m_preselect;
274
275 if( KIWAY_PLAYER* frame = m_dlg->Kiway().Player( FRAME_FOOTPRINT_CHOOSER, true, m_dlg ) )
276 {
277 if( !m_symbolNetlist.empty() )
278 {
280 frame->KiwayMailIn( event );
281 }
282
283 if( frame->ShowModal( &fpid, m_dlg ) )
284 SetValue( fpid );
285
286 frame->Destroy();
287 }
288
289 m_buttonFpChooserLock = false;
290 }
291
292protected:
294 wxString m_preselect;
295 // Lock flag to lock the button to show the FP chooser
296 // true when the button is busy, waiting all footprints loaded to
297 // avoid running more than once the FP chooser
299 /*
300 * Symbol netlist format:
301 * pinNumber pinName <tab> pinNumber pinName...
302 * fpFilter fpFilter...
303 */
304 std::string m_symbolNetlist;
305};
306
307
308void GRID_CELL_FPID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
309 wxEvtHandler* aEventHandler )
310{
311 m_control = new TEXT_BUTTON_FP_CHOOSER( aParent, m_dlg, m_symbolNetlist, m_preselect );
313
314#if wxUSE_VALIDATORS
315 // validate text in textctrl, if validator is set
316 if ( m_validator )
317 {
318 Combo()->SetValidator( *m_validator );
319 }
320#endif
321
322 wxGridCellEditor::Create( aParent, aId, aEventHandler );
323}
324
325
326class TEXT_BUTTON_URL : public wxComboCtrl
327{
328public:
329 TEXT_BUTTON_URL( wxWindow* aParent, DIALOG_SHIM* aParentDlg, SEARCH_STACK* aSearchStack, EMBEDDED_FILES* aFiles ) :
330 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
331 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
332 m_dlg( aParentDlg ),
333 m_searchStack( aSearchStack ),
334 m_files( aFiles )
335 {
337
338 // win32 fix, avoids drawing the "native dropdown caret"
339 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
340
341 // Bind event to handle text changes
342 Bind(wxEVT_TEXT, &TEXT_BUTTON_URL::OnTextChange, this);
343 }
344
346 {
347 Unbind(wxEVT_TEXT, &TEXT_BUTTON_URL::OnTextChange, this);
348 }
349
350protected:
351 void DoSetPopupControl( wxComboPopup* popup ) override
352 {
353 m_popup = nullptr;
354 }
355
356 void OnButtonClick() override
357 {
358 wxString filename = GetValue();
359
360 if (filename.IsEmpty() || filename == wxT("~"))
361 {
362 FILEDLG_OPEN_EMBED_FILE customize;
363 wxFileDialog openFileDialog( this, _( "Open file" ), "", "", "All files (*.*)|*.*",
364 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
365 openFileDialog.SetCustomizeHook( customize );
366
367 if( openFileDialog.ShowModal() == wxID_OK )
368 {
369 filename = openFileDialog.GetPath();
370 wxFileName fn( filename );
371
372 if( customize.GetEmbed() )
373 {
374 EMBEDDED_FILES::EMBEDDED_FILE* result = m_files->AddFile( fn, false );
375 SetValue( result->GetLink() );
376 }
377 else
378 {
379 SetValue( "file://" + filename );
380 }
381 }
382 }
383 else
384 {
386 }
387 }
388
389 void OnTextChange(wxCommandEvent& event)
390 {
392 event.Skip(); // Ensure that other handlers can process this event too
393 }
394
396 {
397 if (GetValue().IsEmpty())
398 SetButtonBitmaps(KiBitmapBundle(BITMAPS::small_folder));
399 else
400 SetButtonBitmaps(KiBitmapBundle(BITMAPS::www));
401 }
402
406};
407
408
409void GRID_CELL_URL_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
410 wxEvtHandler* aEventHandler )
411{
412 m_control = new TEXT_BUTTON_URL( aParent, m_dlg, m_searchStack, m_files );
414
415#if wxUSE_VALIDATORS
416 // validate text in textctrl, if validator is set
417 if ( m_validator )
418 {
419 Combo()->SetValidator( *m_validator );
420 }
421#endif
422
423 wxGridCellEditor::Create( aParent, aId, aEventHandler );
424}
425
426
427class TEXT_BUTTON_FILE_BROWSER : public wxComboCtrl
428{
429public:
430 TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
431 wxString* aCurrentDir, const wxString& aFileFilter = wxEmptyString,
432 bool aNormalize = false,
433 const wxString& aNormalizeBasePath = wxEmptyString ) :
434 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
435 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
436 m_dlg( aParentDlg ),
437 m_grid( aGrid ),
438 m_currentDir( aCurrentDir ),
439 m_normalize( aNormalize ),
440 m_normalizeBasePath( aNormalizeBasePath ),
441 m_fileFilter( aFileFilter )
442 {
443 SetButtonBitmaps( KiBitmapBundle( BITMAPS::small_folder ) );
444
445 // win32 fix, avoids drawing the "native dropdown caret"
446 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
447 }
448
449 TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
450 wxString* aCurrentDir,
451 std::function<wxString( WX_GRID* grid, int row )> aFileFilterFn,
452 bool aNormalize = false,
453 const wxString& aNormalizeBasePath = wxEmptyString ) :
454 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
455 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
456 m_dlg( aParentDlg ),
457 m_grid( aGrid ),
458 m_currentDir( aCurrentDir ),
459 m_normalize( aNormalize ),
460 m_normalizeBasePath( aNormalizeBasePath ),
461 m_fileFilterFn( std::move( aFileFilterFn ) )
462 {
463 SetButtonBitmaps( KiBitmapBundle( BITMAPS::small_folder ) );
464
465 // win32 fix, avoids drawing the "native dropdown caret"
466 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
467 }
468
469
470protected:
471 void DoSetPopupControl( wxComboPopup* popup ) override
472 {
473 m_popup = nullptr;
474 }
475
476 void OnButtonClick() override
477 {
478 if( m_fileFilterFn )
479 m_fileFilter = m_fileFilterFn( m_grid, m_grid->GetGridCursorRow() );
480
481 wxFileName fn = GetValue();
482
483 if( fn.GetPath().IsEmpty() && m_currentDir )
484 fn.SetPath( *m_currentDir );
485 else
486 fn.SetPath( ExpandEnvVarSubstitutions( fn.GetPath(), &m_dlg->Prj() ) );
487
488 if( !m_fileFilter.IsEmpty() )
489 {
490 wxFileDialog dlg( m_dlg, _( "Select a File" ), fn.GetPath(), fn.GetFullName(),
491 m_fileFilter, wxFD_FILE_MUST_EXIST | wxFD_OPEN );
492
493 if( dlg.ShowModal() == wxID_OK )
494 {
495 wxString filePath = dlg.GetPath();
496 wxString lastPath = dlg.GetDirectory();
497 wxString relPath = wxEmptyString;
498
499 if( m_normalize )
500 {
501 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
503 lastPath = NormalizePath( dlg.GetDirectory(), &Pgm().GetLocalEnvVariables(),
505 }
506 else
507 {
508 relPath = filePath;
509 }
510
511 SetValue( relPath );
512
514 {;} // shouldn't happen, but Coverity doesn't know that
515
516 if( m_currentDir )
517 *m_currentDir = lastPath;
518 }
519 }
520 else
521 {
522 wxDirDialog dlg( m_dlg, _( "Select Path" ), fn.GetPath(),
523 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
524
525 if( dlg.ShowModal() == wxID_OK )
526 {
527 wxString filePath = dlg.GetPath();
528 wxString relPath = wxEmptyString;
529
530 if ( m_normalize )
531 {
532 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
534 }
535 else
536 {
537 relPath = filePath;
538 }
539
540 SetValue( relPath );
541
543 {;} // shouldn't happen, but Coverity doesn't know that
544
545 *m_currentDir = relPath;
546 }
547 }
548 }
549
552 wxString* m_currentDir;
555
556 wxString m_fileFilter;
557 std::function<wxString( WX_GRID* aGrid, int aRow )> m_fileFilterFn;
558};
559
560
561void GRID_CELL_PATH_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
562 wxEvtHandler* aEventHandler )
563{
564 if( m_fileFilterFn )
567 else
570
572
573#if wxUSE_VALIDATORS
574 // validate text in textctrl, if validator is set
575 if ( m_validator )
576 {
577 Combo()->SetValidator( *m_validator );
578 }
579#endif
580
581 wxGridCellEditor::Create( aParent, aId, aEventHandler );
582}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:88
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Loads a file from disk and adds it to the collection.
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, EMBEDDED_FILES *aFiles)
void DoSetPopupControl(wxComboPopup *popup) override
void OnTextChange(wxCommandEvent &event)
static void CellEditorSetMargins(wxTextEntryBase *aEntry)
A helper function to set OS-specific margins for text-based cell editors.
Definition: wx_grid.cpp:77
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:637
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
Definition: wx_grid.cpp:84
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:348
The common library.
#define _(s)
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths, EMBEDDED_FILES *aFiles)
Open a document (file) with the suitable browser.
Definition: eda_doc.cpp:62
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:1060
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