KiCad PCB EDA Suite
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-2022 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
285 wxString m_preselect;
286
287 /*
288 * Symbol netlist format:
289 * library:footprint
290 * reference
291 * value
292 * pinName,netName,pinFunction,pinType
293 * pinName,netName,pinFunction,pinType
294 * ...
295 */
296 std::string m_symbolNetlist;
297};
298
299
300void GRID_CELL_FPID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
301 wxEvtHandler* aEventHandler )
302{
303 m_control = new TEXT_BUTTON_FP_CHOOSER( aParent, m_dlg, m_symbolNetlist, m_preselect );
304
305#if wxUSE_VALIDATORS
306 // validate text in textctrl, if validator is set
307 if ( m_validator )
308 {
309 Combo()->SetValidator( *m_validator );
310 }
311#endif
312
313 wxGridCellEditor::Create( aParent, aId, aEventHandler );
314}
315
316
317class TEXT_BUTTON_URL : public wxComboCtrl
318{
319public:
320 TEXT_BUTTON_URL( wxWindow* aParent, DIALOG_SHIM* aParentDlg, SEARCH_STACK* aSearchStack ) :
321 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
322 wxTE_PROCESS_ENTER ),
323 m_dlg( aParentDlg ),
324 m_searchStack( aSearchStack )
325 {
326 SetButtonBitmaps( KiBitmap( BITMAPS::www ) );
327
328 // win32 fix, avoids drawing the "native dropdown caret"
329 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
330 }
331
332protected:
333 void DoSetPopupControl( wxComboPopup* popup ) override
334 {
335 m_popup = nullptr;
336 }
337
338 void OnButtonClick() override
339 {
340 wxString filename = GetValue();
341
342 if( !filename.IsEmpty() && filename != wxT( "~" ) )
344 }
345
348};
349
350
351void GRID_CELL_URL_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
352 wxEvtHandler* aEventHandler )
353{
354 m_control = new TEXT_BUTTON_URL( aParent, m_dlg, m_searchStack );
355
356#if wxUSE_VALIDATORS
357 // validate text in textctrl, if validator is set
358 if ( m_validator )
359 {
360 Combo()->SetValidator( *m_validator );
361 }
362#endif
363
364 wxGridCellEditor::Create( aParent, aId, aEventHandler );
365}
366
367
368class TEXT_BUTTON_FILE_BROWSER : public wxComboCtrl
369{
370public:
371 TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
372 wxString* aCurrentDir, wxString* aExt = nullptr,
373 bool aNormalize = false,
374 wxString aNormalizeBasePath = wxEmptyString ) :
375 wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
376 wxTE_PROCESS_ENTER ),
377 m_dlg( aParentDlg ),
378 m_grid( aGrid ),
379 m_currentDir( aCurrentDir ),
380 m_ext( aExt ),
381 m_normalize( aNormalize ),
382 m_normalizeBasePath( aNormalizeBasePath )
383 {
384 SetButtonBitmaps( KiBitmap( BITMAPS::small_folder ) );
385
386 // win32 fix, avoids drawing the "native dropdown caret"
387 Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
388 }
389
390protected:
391 void DoSetPopupControl( wxComboPopup* popup ) override
392 {
393 m_popup = nullptr;
394 }
395
396 void OnButtonClick() override
397 {
398 wxFileName fn = GetValue();
399
400 if( fn.GetPath().IsEmpty() && m_currentDir )
401 fn.SetPath( *m_currentDir );
402 else
403 fn.SetPath( ExpandEnvVarSubstitutions( fn.GetPath(), &m_dlg->Prj() ) );
404
405 if( m_ext )
406 {
407 wxFileDialog dlg( m_dlg, _( "Select a File" ), fn.GetPath(), fn.GetFullName(), *m_ext,
408 wxFD_FILE_MUST_EXIST | wxFD_OPEN );
409
410 if( dlg.ShowModal() == wxID_OK )
411 {
412 wxString filePath = dlg.GetPath();
413 wxString lastPath = dlg.GetDirectory();
414 wxString relPath = wxEmptyString;
415
416 if( m_normalize )
417 {
418 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
420 lastPath = NormalizePath( dlg.GetDirectory(), &Pgm().GetLocalEnvVariables(),
422 }
423 else
424 {
425 relPath = filePath;
426 }
427
428 SetValue( relPath );
429
431 {;} // shouldn't happen, but Coverity doesn't know that
432
433 if( m_currentDir )
434 *m_currentDir = lastPath;
435 }
436 }
437 else
438 {
439 wxDirDialog dlg( m_dlg, _( "Select Path" ), fn.GetPath(),
440 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
441
442 if( dlg.ShowModal() == wxID_OK )
443 {
444 wxString filePath = dlg.GetPath();
445 wxString relPath = wxEmptyString;
446
447 if ( m_normalize )
448 {
449 relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
451 }
452 else
453 {
454 relPath = filePath;
455 }
456
457 SetValue( relPath );
458
460 {;} // shouldn't happen, but Coverity doesn't know that
461
462 *m_currentDir = relPath;
463 }
464 }
465 }
466
469 wxString* m_currentDir;
470 wxString* m_ext;
473};
474
475
476void GRID_CELL_PATH_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
477 wxEvtHandler* aEventHandler )
478{
479 if( m_ext.IsEmpty() )
480 m_control = new TEXT_BUTTON_FILE_BROWSER( aParent, m_dlg, m_grid, m_currentDir, nullptr,
482 else
483 m_control = new TEXT_BUTTON_FILE_BROWSER( aParent, m_dlg, m_grid, m_currentDir, &m_ext,
485
486#if wxUSE_VALIDATORS
487 // validate text in textctrl, if validator is set
488 if ( m_validator )
489 {
490 Combo()->SetValidator( *m_validator );
491 }
492#endif
493
494 wxGridCellEditor::Create( aParent, aId, aEventHandler );
495}
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:105
@ small_folder
@ small_library
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
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:66
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:394
Look for files in a number of paths.
Definition: search_stack.h:42
void DoSetPopupControl(wxComboPopup *popup) override
TEXT_BUTTON_FILE_BROWSER(wxWindow *aParent, DIALOG_SHIM *aParentDlg, WX_GRID *aGrid, wxString *aCurrentDir, wxString *aExt=nullptr, bool aNormalize=false, 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:280
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:267
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:74
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_VIEWER_MODAL
Definition: frame_type.h:43
@ FRAME_FOOTPRINT_VIEWER
Definition: frame_type.h:42
@ FRAME_SCH_VIEWER_MODAL
Definition: frame_type.h:37
@ MAIL_SYMBOL_NETLIST
Definition: mail_type.h:45
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111
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:55