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 );
63
64#if defined( __WXMAC__ )
65 rect.Inflate( 2 ); // ignore FOCUS_RING
66#elif defined( __WXGTK__ )
67 rect.Inflate( -3 ); // The -3 is a very sad hack here. Some GTK themes overrun the
68 // default -1, preventing display. Unfortunately, we don't appear to
69 // have a good method of finding the current margin needed.
70 // Some GTK resize events seem to update the cell size but not all and
71 // not consistently.
72#else
73 rect.Inflate( -1 );
74#endif
75
76 Combo()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
77}
78
79
80void GRID_CELL_TEXT_BUTTON::StartingKey( wxKeyEvent& event )
81{
82 // Note: this is a copy of wxGridCellTextEditor's StartingKey()
83
84 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
85 // longer an appropriate way to get the character into the text control.
86 // Do it ourselves instead. We know that if we get this far that we have
87 // a valid character, so not a whole lot of testing needs to be done.
88
89 // wxComboCtrl inherits from wxTextEntry, so can statically cast
90 wxTextEntry* textEntry = static_cast<wxTextEntry*>( Combo() );
91 int ch;
92
93 bool isPrintable;
94
95#if wxUSE_UNICODE
96 ch = event.GetUnicodeKey();
97
98 if( ch != WXK_NONE )
99 isPrintable = true;
100 else
101#endif // wxUSE_UNICODE
102 {
103 ch = event.GetKeyCode();
104 isPrintable = ch >= WXK_SPACE && ch < WXK_START;
105 }
106
107 switch( ch )
108 {
109 case WXK_DELETE:
110 // Delete the initial character when starting to edit with DELETE.
111 textEntry->Remove( 0, 1 );
112 break;
113
114 case WXK_BACK:
115 // Delete the last character when starting to edit with BACKSPACE.
116 {
117 const long pos = textEntry->GetLastPosition();
118 textEntry->Remove( pos - 1, pos );
119 }
120 break;
121
122 default:
123 if( isPrintable )
124 textEntry->WriteText( static_cast<wxChar>( ch ) );
125 break;
126 }
127}
128
129
130void GRID_CELL_TEXT_BUTTON::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
131{
132 auto evtHandler = static_cast< wxGridCellEditorEvtHandler* >( m_control->GetEventHandler() );
133
134 // Don't immediately end if we get a kill focus event within BeginEdit
135 evtHandler->SetInSetFocus( true );
136
137 m_value = aGrid->GetTable()->GetValue( aRow, aCol );
138
139 Combo()->SetValue( m_value );
140 Combo()->SetFocus();
141}
142
143
144bool GRID_CELL_TEXT_BUTTON::EndEdit( int, int, const wxGrid*, const wxString&, wxString *aNewVal )
145{
146 const wxString value = Combo()->GetValue();
147
148 if( value == m_value )
149 return false;
150
151 m_value = value;
152
153 if( aNewVal )
154 *aNewVal = value;
155
156 return true;
157}
158
159
160void GRID_CELL_TEXT_BUTTON::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
161{
162 aGrid->GetTable()->SetValue( aRow, aCol, m_value );
163}
164
165
167{
168 Combo()->SetValue( m_value );
169}
170
171
172#if wxUSE_VALIDATORS
173void GRID_CELL_TEXT_BUTTON::SetValidator( const wxValidator& validator )
174{
175 m_validator.reset( static_cast< wxValidator* >( validator.Clone() ) );
176}
177#endif
178
179
180class TEXT_BUTTON_SYMBOL_CHOOSER : public wxComboCtrl
181{
182public:
183 TEXT_BUTTON_SYMBOL_CHOOSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg,
184 const wxString& aPreselect ) :
185 wxComboCtrl( aParent ),
186 m_dlg( aParentDlg ),
187 m_preselect( aPreselect )
188 {
189 SetButtonBitmaps( KiBitmap( BITMAPS::small_library ) );
190
191 // win32 fix, avoids drawing the "native dropdown caret"
192 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
193 }
194
195protected:
196 void DoSetPopupControl( wxComboPopup* popup ) override
197 {
198 m_popup = nullptr;
199 }
200
201 wxString escapeLibId( const wxString& aRawValue )
202 {
203 wxString itemName;
204 wxString libName = aRawValue.BeforeFirst( ':', &itemName );
205 return EscapeString( libName, CTX_LIBID ) + ':' + EscapeString( itemName, CTX_LIBID );
206 }
207
208 void OnButtonClick() override
209 {
210 // pick a symbol using the symbol picker.
211 wxString rawValue = GetValue();
212
213 if( rawValue.IsEmpty() )
214 rawValue = m_preselect;
215
216 wxString symbolId = escapeLibId( rawValue );
218
219 if( frame->ShowModal( &symbolId, m_dlg ) )
220 SetValue( UnescapeString( symbolId ) );
221
222 frame->Destroy();
223 }
224
226 wxString m_preselect;
227};
228
229
230void GRID_CELL_SYMBOL_ID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
231 wxEvtHandler* aEventHandler )
232{
233 m_control = new TEXT_BUTTON_SYMBOL_CHOOSER( aParent, m_dlg, m_preselect );
234
235 wxGridCellEditor::Create( aParent, aId, aEventHandler );
236}
237
238
239class TEXT_BUTTON_FP_CHOOSER : public wxComboCtrl
240{
241public:
242 TEXT_BUTTON_FP_CHOOSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg,
243 const wxString& aSymbolNetlist, const wxString& aPreselect ) :
244 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
245 wxTE_PROCESS_ENTER ),
246 m_dlg( aParentDlg ),
247 m_preselect( aPreselect ),
248 m_symbolNetlist( aSymbolNetlist.ToStdString() )
249 {
250 SetButtonBitmaps( KiBitmap( BITMAPS::small_library ) );
251
252 // win32 fix, avoids drawing the "native dropdown caret"
253 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
254 }
255
256protected:
257 void DoSetPopupControl( wxComboPopup* popup ) override
258 {
259 m_popup = nullptr;
260 }
261
262 void OnButtonClick() override
263 {
264 // pick a footprint using the footprint picker.
265 wxString fpid = GetValue();
266
267 if( fpid.IsEmpty() )
268 fpid = m_preselect;
269
271
272 if( !m_symbolNetlist.empty() )
273 {
275 frame->KiwayMailIn( event );
276 }
277
278 if( frame->ShowModal( &fpid, m_dlg ) )
279 SetValue( fpid );
280
281 frame->Destroy();
282 }
283
284protected:
286 wxString m_preselect;
287
288 /*
289 * Symbol netlist format:
290 * pinCount
291 * fpFilters
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 );
301
302#if wxUSE_VALIDATORS
303 // validate text in textctrl, if validator is set
304 if ( m_validator )
305 {
306 Combo()->SetValidator( *m_validator );
307 }
308#endif
309
310 wxGridCellEditor::Create( aParent, aId, aEventHandler );
311}
312
313
314class TEXT_BUTTON_URL : public wxComboCtrl
315{
316public:
317 TEXT_BUTTON_URL( wxWindow* aParent, DIALOG_SHIM* aParentDlg, SEARCH_STACK* aSearchStack ) :
318 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
319 wxTE_PROCESS_ENTER ),
320 m_dlg( aParentDlg ),
321 m_searchStack( aSearchStack )
322 {
323 SetButtonBitmaps( KiBitmap( BITMAPS::www ) );
324
325 // win32 fix, avoids drawing the "native dropdown caret"
326 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
327 }
328
329protected:
330 void DoSetPopupControl( wxComboPopup* popup ) override
331 {
332 m_popup = nullptr;
333 }
334
335 void OnButtonClick() override
336 {
337 wxString filename = GetValue();
338
339 if( !filename.IsEmpty() && filename != wxT( "~" ) )
341 }
342
345};
346
347
348void GRID_CELL_URL_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
349 wxEvtHandler* aEventHandler )
350{
351 m_control = new TEXT_BUTTON_URL( aParent, m_dlg, m_searchStack );
352
353#if wxUSE_VALIDATORS
354 // validate text in textctrl, if validator is set
355 if ( m_validator )
356 {
357 Combo()->SetValidator( *m_validator );
358 }
359#endif
360
361 wxGridCellEditor::Create( aParent, aId, aEventHandler );
362}
363
364
365class TEXT_BUTTON_FILE_BROWSER : public wxComboCtrl
366{
367public:
368 TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
369 wxString* aCurrentDir, const wxString& aFileFilter = wxEmptyString,
370 bool aNormalize = false,
371 const wxString& aNormalizeBasePath = wxEmptyString ) :
372 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
373 wxTE_PROCESS_ENTER ),
374 m_dlg( aParentDlg ),
375 m_grid( aGrid ),
376 m_currentDir( aCurrentDir ),
377 m_normalize( aNormalize ),
378 m_normalizeBasePath( aNormalizeBasePath ),
379 m_fileFilter( aFileFilter )
380 {
381 SetButtonBitmaps( KiBitmap( BITMAPS::small_folder ) );
382
383 // win32 fix, avoids drawing the "native dropdown caret"
384 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
385 }
386
387 TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
388 wxString* aCurrentDir,
389 std::function<wxString( WX_GRID* grid, int row )> aFileFilterFn,
390 bool aNormalize = false,
391 const wxString& aNormalizeBasePath = wxEmptyString ) :
392 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
393 wxTE_PROCESS_ENTER ),
394 m_dlg( aParentDlg ),
395 m_grid( aGrid ),
396 m_currentDir( aCurrentDir ),
397 m_normalize( aNormalize ),
398 m_normalizeBasePath( aNormalizeBasePath ),
399 m_fileFilterFn( std::move( aFileFilterFn ) )
400 {
401 SetButtonBitmaps( KiBitmap( BITMAPS::small_folder ) );
402
403 // win32 fix, avoids drawing the "native dropdown caret"
404 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
405 }
406
407
408protected:
409 void DoSetPopupControl( wxComboPopup* popup ) override
410 {
411 m_popup = nullptr;
412 }
413
414 void OnButtonClick() override
415 {
416 if( m_fileFilterFn )
417 m_fileFilter = m_fileFilterFn( m_grid, m_grid->GetGridCursorRow() );
418
419 wxFileName fn = GetValue();
420
421 if( fn.GetPath().IsEmpty() && m_currentDir )
422 fn.SetPath( *m_currentDir );
423 else
424 fn.SetPath( ExpandEnvVarSubstitutions( fn.GetPath(), &m_dlg->Prj() ) );
425
426 if( !m_fileFilter.IsEmpty() )
427 {
428 wxFileDialog dlg( m_dlg, _( "Select a File" ), fn.GetPath(), fn.GetFullName(),
429 m_fileFilter, wxFD_FILE_MUST_EXIST | wxFD_OPEN );
430
431 if( dlg.ShowModal() == wxID_OK )
432 {
433 wxString filePath = dlg.GetPath();
434 wxString lastPath = dlg.GetDirectory();
435 wxString relPath = wxEmptyString;
436
437 if( m_normalize )
438 {
439 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
441 lastPath = NormalizePath( dlg.GetDirectory(), &Pgm().GetLocalEnvVariables(),
443 }
444 else
445 {
446 relPath = filePath;
447 }
448
449 SetValue( relPath );
450
452 {;} // shouldn't happen, but Coverity doesn't know that
453
454 if( m_currentDir )
455 *m_currentDir = lastPath;
456 }
457 }
458 else
459 {
460 wxDirDialog dlg( m_dlg, _( "Select Path" ), fn.GetPath(),
461 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
462
463 if( dlg.ShowModal() == wxID_OK )
464 {
465 wxString filePath = dlg.GetPath();
466 wxString relPath = wxEmptyString;
467
468 if ( m_normalize )
469 {
470 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
472 }
473 else
474 {
475 relPath = filePath;
476 }
477
478 SetValue( relPath );
479
481 {;} // shouldn't happen, but Coverity doesn't know that
482
483 *m_currentDir = relPath;
484 }
485 }
486 }
487
490 wxString* m_currentDir;
493
494 wxString m_fileFilter;
495 std::function<wxString( WX_GRID* aGrid, int aRow )> m_fileFilterFn;
496};
497
498
499void GRID_CELL_PATH_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
500 wxEvtHandler* aEventHandler )
501{
502 if( m_fileFilterFn )
505 else
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:83
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:39
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:53
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:432
Look for files in a number of paths.
Definition: search_stack.h:42
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
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:462
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.
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
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