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 );
207
208 if( frame->ShowModal( &symbolId, m_dlg ) )
209 SetValue( UnescapeString( symbolId ) );
210
211 frame->Destroy();
212 }
213
215 wxString m_preselect;
216};
217
218
219void GRID_CELL_SYMBOL_ID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
220 wxEvtHandler* aEventHandler )
221{
222 m_control = new TEXT_BUTTON_SYMBOL_CHOOSER( aParent, m_dlg, m_preselect );
224
225 wxGridCellEditor::Create( aParent, aId, aEventHandler );
226}
227
228
229class TEXT_BUTTON_FP_CHOOSER : public wxComboCtrl
230{
231public:
232 TEXT_BUTTON_FP_CHOOSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg,
233 const wxString& aSymbolNetlist, const wxString& aPreselect ) :
234 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
235 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
236 m_dlg( aParentDlg ),
237 m_preselect( aPreselect ),
238 m_symbolNetlist( aSymbolNetlist.ToStdString() )
239 {
240 SetButtonBitmaps( KiBitmap( BITMAPS::small_library ) );
241
242 // win32 fix, avoids drawing the "native dropdown caret"
243 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
244 }
245
246protected:
247 void DoSetPopupControl( wxComboPopup* popup ) override
248 {
249 m_popup = nullptr;
250 }
251
252 void OnButtonClick() override
253 {
254 // pick a footprint using the footprint picker.
255 wxString fpid = GetValue();
256
257 if( fpid.IsEmpty() )
258 fpid = m_preselect;
259
260 // Disable the button until we have finished processing it. Normally this is not an issue
261 // but if the footprint chooser is loading for the first time, it can be slow enough that
262 // multiple clicks will cause multiple instances of the footprint loader process to start
263 Disable();
264
266
267 if( !m_symbolNetlist.empty() )
268 {
270 frame->KiwayMailIn( event );
271 }
272
273 if( frame->ShowModal( &fpid, m_dlg ) )
274 SetValue( fpid );
275
276 frame->Destroy();
277 Enable();
278 }
279
280protected:
282 wxString m_preselect;
283
284 /*
285 * Symbol netlist format:
286 * pinNumber pinName <tab> pinNumber pinName...
287 * fpFilter fpFilter...
288 */
289 std::string m_symbolNetlist;
290};
291
292
293void GRID_CELL_FPID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
294 wxEvtHandler* aEventHandler )
295{
296 m_control = new TEXT_BUTTON_FP_CHOOSER( aParent, m_dlg, m_symbolNetlist, m_preselect );
298
299#if wxUSE_VALIDATORS
300 // validate text in textctrl, if validator is set
301 if ( m_validator )
302 {
303 Combo()->SetValidator( *m_validator );
304 }
305#endif
306
307 wxGridCellEditor::Create( aParent, aId, aEventHandler );
308}
309
310
311class TEXT_BUTTON_URL : public wxComboCtrl
312{
313public:
314 TEXT_BUTTON_URL( wxWindow* aParent, DIALOG_SHIM* aParentDlg, SEARCH_STACK* aSearchStack ) :
315 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
316 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
317 m_dlg( aParentDlg ),
318 m_searchStack( aSearchStack )
319 {
320 SetButtonBitmaps( KiBitmap( BITMAPS::www ) );
321
322 // win32 fix, avoids drawing the "native dropdown caret"
323 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
324 }
325
326protected:
327 void DoSetPopupControl( wxComboPopup* popup ) override
328 {
329 m_popup = nullptr;
330 }
331
332 void OnButtonClick() override
333 {
334 wxString filename = GetValue();
335
336 if( !filename.IsEmpty() && filename != wxT( "~" ) )
338 }
339
342};
343
344
345void GRID_CELL_URL_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
346 wxEvtHandler* aEventHandler )
347{
348 m_control = new TEXT_BUTTON_URL( aParent, m_dlg, m_searchStack );
350
351#if wxUSE_VALIDATORS
352 // validate text in textctrl, if validator is set
353 if ( m_validator )
354 {
355 Combo()->SetValidator( *m_validator );
356 }
357#endif
358
359 wxGridCellEditor::Create( aParent, aId, aEventHandler );
360}
361
362
363class TEXT_BUTTON_FILE_BROWSER : public wxComboCtrl
364{
365public:
366 TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
367 wxString* aCurrentDir, const wxString& aFileFilter = wxEmptyString,
368 bool aNormalize = false,
369 const wxString& aNormalizeBasePath = wxEmptyString ) :
370 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
371 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
372 m_dlg( aParentDlg ),
373 m_grid( aGrid ),
374 m_currentDir( aCurrentDir ),
375 m_normalize( aNormalize ),
376 m_normalizeBasePath( aNormalizeBasePath ),
377 m_fileFilter( aFileFilter )
378 {
379 SetButtonBitmaps( KiBitmap( BITMAPS::small_folder ) );
380
381 // win32 fix, avoids drawing the "native dropdown caret"
382 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
383 }
384
385 TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
386 wxString* aCurrentDir,
387 std::function<wxString( WX_GRID* grid, int row )> aFileFilterFn,
388 bool aNormalize = false,
389 const wxString& aNormalizeBasePath = wxEmptyString ) :
390 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
391 wxTE_PROCESS_ENTER | wxBORDER_NONE ),
392 m_dlg( aParentDlg ),
393 m_grid( aGrid ),
394 m_currentDir( aCurrentDir ),
395 m_normalize( aNormalize ),
396 m_normalizeBasePath( aNormalizeBasePath ),
397 m_fileFilterFn( std::move( aFileFilterFn ) )
398 {
399 SetButtonBitmaps( KiBitmap( BITMAPS::small_folder ) );
400
401 // win32 fix, avoids drawing the "native dropdown caret"
402 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
403 }
404
405
406protected:
407 void DoSetPopupControl( wxComboPopup* popup ) override
408 {
409 m_popup = nullptr;
410 }
411
412 void OnButtonClick() override
413 {
414 if( m_fileFilterFn )
415 m_fileFilter = m_fileFilterFn( m_grid, m_grid->GetGridCursorRow() );
416
417 wxFileName fn = GetValue();
418
419 if( fn.GetPath().IsEmpty() && m_currentDir )
420 fn.SetPath( *m_currentDir );
421 else
422 fn.SetPath( ExpandEnvVarSubstitutions( fn.GetPath(), &m_dlg->Prj() ) );
423
424 if( !m_fileFilter.IsEmpty() )
425 {
426 wxFileDialog dlg( m_dlg, _( "Select a File" ), fn.GetPath(), fn.GetFullName(),
427 m_fileFilter, wxFD_FILE_MUST_EXIST | wxFD_OPEN );
428
429 if( dlg.ShowModal() == wxID_OK )
430 {
431 wxString filePath = dlg.GetPath();
432 wxString lastPath = dlg.GetDirectory();
433 wxString relPath = wxEmptyString;
434
435 if( m_normalize )
436 {
437 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
439 lastPath = NormalizePath( dlg.GetDirectory(), &Pgm().GetLocalEnvVariables(),
441 }
442 else
443 {
444 relPath = filePath;
445 }
446
447 SetValue( relPath );
448
450 {;} // shouldn't happen, but Coverity doesn't know that
451
452 if( m_currentDir )
453 *m_currentDir = lastPath;
454 }
455 }
456 else
457 {
458 wxDirDialog dlg( m_dlg, _( "Select Path" ), fn.GetPath(),
459 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
460
461 if( dlg.ShowModal() == wxID_OK )
462 {
463 wxString filePath = dlg.GetPath();
464 wxString relPath = wxEmptyString;
465
466 if ( m_normalize )
467 {
468 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
470 }
471 else
472 {
473 relPath = filePath;
474 }
475
476 SetValue( relPath );
477
479 {;} // shouldn't happen, but Coverity doesn't know that
480
481 *m_currentDir = relPath;
482 }
483 }
484 }
485
488 wxString* m_currentDir;
491
492 wxString m_fileFilter;
493 std::function<wxString( WX_GRID* aGrid, int aRow )> m_fileFilterFn;
494};
495
496
497void GRID_CELL_PATH_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
498 wxEvtHandler* aEventHandler )
499{
500 if( m_fileFilterFn )
503 else
506
508
509#if wxUSE_VALIDATORS
510 // validate text in textctrl, if validator is set
511 if ( m_validator )
512 {
513 Combo()->SetValidator( *m_validator );
514 }
515#endif
516
517 wxGridCellEditor::Create( aParent, aId, aEventHandler );
518}
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:84
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:67
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...
virtual void KiwayMailIn(KIWAY_EXPRESS &aEvent)
Receive KIWAY_EXPRESS messages from other players.
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
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:44
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:558
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
Definition: wx_grid.cpp:51
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:60
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