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-2021 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 <dialog_shim.h>
34 #include <common.h>
35 #include <env_paths.h>
36 #include <pgm_base.h>
37 #include <widgets/wx_grid.h>
39 #include <eda_doc.h>
40 
41 
42 //-------- Renderer ---------------------------------------------------------------------
43 // None required; just render as normal text.
44 
45 
46 
47 //-------- Editor Base Class ------------------------------------------------------------
48 //
49 // Note: this implementation is an adaptation of wxGridCellChoiceEditor
50 
51 
53 {
54  return Combo()->GetValue();
55 }
56 
57 
58 void GRID_CELL_TEXT_BUTTON::SetSize( const wxRect& aRect )
59 {
60  wxRect rect( aRect );
61  rect.Inflate( -1 );
62 
63 #if defined( __WXMAC__ )
64  rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
65 #endif
66 
67  Combo()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
68 }
69 
70 
71 void GRID_CELL_TEXT_BUTTON::StartingKey( wxKeyEvent& event )
72 {
73  // Note: this is a copy of wxGridCellTextEditor's StartingKey()
74 
75  // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
76  // longer an appropriate way to get the character into the text control.
77  // Do it ourselves instead. We know that if we get this far that we have
78  // a valid character, so not a whole lot of testing needs to be done.
79 
80  // wxComboCtrl inherits from wxTextEntry, so can statically cast
81  wxTextEntry* textEntry = static_cast<wxTextEntry*>( Combo() );
82  int ch;
83 
84  bool isPrintable;
85 
86 #if wxUSE_UNICODE
87  ch = event.GetUnicodeKey();
88 
89  if( ch != WXK_NONE )
90  isPrintable = true;
91  else
92 #endif // wxUSE_UNICODE
93  {
94  ch = event.GetKeyCode();
95  isPrintable = ch >= WXK_SPACE && ch < WXK_START;
96  }
97 
98  switch( ch )
99  {
100  case WXK_DELETE:
101  // Delete the initial character when starting to edit with DELETE.
102  textEntry->Remove( 0, 1 );
103  break;
104 
105  case WXK_BACK:
106  // Delete the last character when starting to edit with BACKSPACE.
107  {
108  const long pos = textEntry->GetLastPosition();
109  textEntry->Remove( pos - 1, pos );
110  }
111  break;
112 
113  default:
114  if( isPrintable )
115  textEntry->WriteText( static_cast<wxChar>( ch ) );
116  break;
117  }
118 }
119 
120 
121 void GRID_CELL_TEXT_BUTTON::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
122 {
123  auto evtHandler = static_cast< wxGridCellEditorEvtHandler* >( m_control->GetEventHandler() );
124 
125  // Don't immediately end if we get a kill focus event within BeginEdit
126  evtHandler->SetInSetFocus( true );
127 
128  m_value = aGrid->GetTable()->GetValue( aRow, aCol );
129 
130  Combo()->SetValue( m_value );
131  Combo()->SetFocus();
132 }
133 
134 
135 bool GRID_CELL_TEXT_BUTTON::EndEdit( int, int, const wxGrid*, const wxString&, wxString *aNewVal )
136 {
137  const wxString value = Combo()->GetValue();
138 
139  if( value == m_value )
140  return false;
141 
142  m_value = value;
143 
144  if( aNewVal )
145  *aNewVal = value;
146 
147  return true;
148 }
149 
150 
151 void GRID_CELL_TEXT_BUTTON::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
152 {
153  aGrid->GetTable()->SetValue( aRow, aCol, m_value );
154 }
155 
156 
158 {
159  Combo()->SetValue( m_value );
160 }
161 
162 
163 #if wxUSE_VALIDATORS
164 void GRID_CELL_TEXT_BUTTON::SetValidator( const wxValidator& validator )
165 {
166  m_validator.reset( static_cast< wxValidator* >( validator.Clone() ) );
167 }
168 #endif
169 
170 
171 class TEXT_BUTTON_SYMBOL_CHOOSER : public wxComboCtrl
172 {
173 public:
174  TEXT_BUTTON_SYMBOL_CHOOSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg,
175  const wxString& aPreselect ) :
176  wxComboCtrl( aParent ),
177  m_dlg( aParentDlg ),
178  m_preselect( aPreselect )
179  {
180  SetButtonBitmaps( KiBitmap( BITMAPS::small_library ) );
181 
182  // win32 fix, avoids drawing the "native dropdown caret"
183  Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
184  }
185 
186 protected:
187  void DoSetPopupControl( wxComboPopup* popup ) override
188  {
189  m_popup = nullptr;
190  }
191 
192  void OnButtonClick() override
193  {
194  // pick a footprint using the footprint picker.
195  wxString compid = GetValue();
196 
197  if( compid.IsEmpty() )
198  compid = m_preselect;
199 
201 
202  if( frame->ShowModal( &compid, m_dlg ) )
203  SetValue( compid );
204 
205  frame->Destroy();
206  }
207 
209  wxString m_preselect;
210 };
211 
212 
213 void GRID_CELL_SYMBOL_ID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
214  wxEvtHandler* aEventHandler )
215 {
216  m_control = new TEXT_BUTTON_SYMBOL_CHOOSER( aParent, m_dlg, m_preselect );
217 
218  wxGridCellEditor::Create( aParent, aId, aEventHandler );
219 }
220 
221 
222 class TEXT_BUTTON_FP_CHOOSER : public wxComboCtrl
223 {
224 public:
225  TEXT_BUTTON_FP_CHOOSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg,
226  const wxString& aPreselect ) :
227  wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
228  wxTE_PROCESS_ENTER ),
229  m_dlg( aParentDlg ),
230  m_preselect( aPreselect )
231  {
232  SetButtonBitmaps( KiBitmap( BITMAPS::small_library ) );
233 
234  // win32 fix, avoids drawing the "native dropdown caret"
235  Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
236  }
237 
238 protected:
239  void DoSetPopupControl( wxComboPopup* popup ) override
240  {
241  m_popup = nullptr;
242  }
243 
244  void OnButtonClick() override
245  {
246  // pick a footprint using the footprint picker.
247  wxString fpid = GetValue();
248 
249  if( fpid.IsEmpty() )
250  fpid = m_preselect;
251 
253 
254  if( frame->ShowModal( &fpid, m_dlg ) )
255  SetValue( fpid );
256 
257  frame->Destroy();
258  }
259 
261  wxString m_preselect;
262 };
263 
264 
265 void GRID_CELL_FOOTPRINT_ID_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
266  wxEvtHandler* aEventHandler )
267 {
268  m_control = new TEXT_BUTTON_FP_CHOOSER( aParent, m_dlg, m_preselect );
269 
270 #if wxUSE_VALIDATORS
271  // validate text in textctrl, if validator is set
272  if ( m_validator )
273  {
274  Combo()->SetValidator( *m_validator );
275  }
276 #endif
277 
278  wxGridCellEditor::Create( aParent, aId, aEventHandler );
279 }
280 
281 
282 class TEXT_BUTTON_URL : public wxComboCtrl
283 {
284 public:
285  TEXT_BUTTON_URL( wxWindow* aParent, DIALOG_SHIM* aParentDlg ) :
286  wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
287  wxTE_PROCESS_ENTER ),
288  m_dlg( aParentDlg )
289  {
290  SetButtonBitmaps( KiBitmap( BITMAPS::www ) );
291 
292  // win32 fix, avoids drawing the "native dropdown caret"
293  Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
294  }
295 
296 protected:
297  void DoSetPopupControl( wxComboPopup* popup ) override
298  {
299  m_popup = nullptr;
300  }
301 
302  void OnButtonClick() override
303  {
304  wxString filename = GetValue();
305 
306  if( !filename.IsEmpty() && filename != wxT( "~" ) )
307  GetAssociatedDocument( m_dlg, GetValue(), &m_dlg->Prj() );
308  }
309 
311 };
312 
313 
314 void GRID_CELL_URL_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
315  wxEvtHandler* aEventHandler )
316 {
317  m_control = new TEXT_BUTTON_URL( aParent, m_dlg );
318 
319 #if wxUSE_VALIDATORS
320  // validate text in textctrl, if validator is set
321  if ( m_validator )
322  {
323  Combo()->SetValidator( *m_validator );
324  }
325 #endif
326 
327  wxGridCellEditor::Create( aParent, aId, aEventHandler );
328 }
329 
330 
331 class TEXT_BUTTON_FILE_BROWSER : public wxComboCtrl
332 {
333 public:
334  TEXT_BUTTON_FILE_BROWSER( wxWindow* aParent, DIALOG_SHIM* aParentDlg, WX_GRID* aGrid,
335  wxString* aCurrentDir, wxString* aExt = nullptr,
336  bool aNormalize = false,
337  wxString aNormalizeBasePath = wxEmptyString ) :
338  wxComboCtrl( aParent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
339  wxTE_PROCESS_ENTER ),
340  m_dlg( aParentDlg ),
341  m_grid( aGrid ),
342  m_currentDir( aCurrentDir ),
343  m_ext( aExt ),
344  m_normalize( aNormalize ),
345  m_normalizeBasePath( aNormalizeBasePath )
346  {
347  SetButtonBitmaps( KiBitmap( BITMAPS::small_folder ) );
348 
349  // win32 fix, avoids drawing the "native dropdown caret"
350  Customize( wxCC_IFLAG_HAS_NONSTANDARD_BUTTON );
351  }
352 
353 protected:
354  void DoSetPopupControl( wxComboPopup* popup ) override
355  {
356  m_popup = nullptr;
357  }
358 
359  void OnButtonClick() override
360  {
361  wxFileName fn = GetValue();
362 
363  if( fn.GetPath().IsEmpty() && m_currentDir )
364  fn.SetPath( *m_currentDir );
365  else
366  fn.SetPath( ExpandEnvVarSubstitutions( fn.GetPath(), &m_dlg->Prj() ) );
367 
368  if( m_ext )
369  {
370  wxFileDialog dlg( m_dlg, _( "Select a File" ), fn.GetPath(), fn.GetFullName(), *m_ext,
371  wxFD_FILE_MUST_EXIST | wxFD_OPEN );
372 
373  if( dlg.ShowModal() == wxID_OK )
374  {
375  wxString filePath = dlg.GetPath();
376  wxString lastPath = dlg.GetDirectory();
377  wxString relPath = wxEmptyString;
378 
379  if( m_normalize )
380  {
381  relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
383  lastPath = NormalizePath( dlg.GetDirectory(), &Pgm().GetLocalEnvVariables(),
385  }
386 
387  if( relPath.IsEmpty() )
388  relPath = filePath;
389 
390  SetValue( relPath );
391 
392  if( !m_grid->CommitPendingChanges() )
393  {;} // shouldn't happen, but Coverity doesn't know that
394 
395  if( m_currentDir )
396  *m_currentDir = lastPath;
397  }
398  }
399  else
400  {
401  wxDirDialog dlg( m_dlg, _( "Select Path" ), fn.GetPath(),
402  wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
403 
404  if( dlg.ShowModal() == wxID_OK )
405  {
406  wxString filePath = dlg.GetPath();
407  wxString relPath = wxEmptyString;
408 
409  if ( m_normalize )
410  {
411  relPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(),
413  }
414 
415  if( relPath.IsEmpty() )
416  relPath = filePath;
417 
418  SetValue( relPath );
419 
420  if( !m_grid->CommitPendingChanges() )
421  {;} // shouldn't happen, but Coverity doesn't know that
422 
423  *m_currentDir = relPath;
424  }
425  }
426  }
427 
430  wxString* m_currentDir;
431  wxString* m_ext;
434 };
435 
436 
437 void GRID_CELL_PATH_EDITOR::Create( wxWindow* aParent, wxWindowID aId,
438  wxEvtHandler* aEventHandler )
439 {
440  if( m_ext.IsEmpty() )
441  m_control = new TEXT_BUTTON_FILE_BROWSER( aParent, m_dlg, m_grid, m_currentDir, nullptr,
443  else
444  m_control = new TEXT_BUTTON_FILE_BROWSER( aParent, m_dlg, m_grid, m_currentDir, &m_ext,
446 
447 #if wxUSE_VALIDATORS
448  // validate text in textctrl, if validator is set
449  if ( m_validator )
450  {
451  Combo()->SetValidator( *m_validator );
452  }
453 #endif
454 
455  wxGridCellEditor::Create( aParent, aId, aEventHandler );
456 }
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...
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject)
Open a document (file) with the suitable browser.
Definition: eda_doc.cpp:79
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:64
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
TEXT_BUTTON_FILE_BROWSER(wxWindow *aParent, DIALOG_SHIM *aParentDlg, WX_GRID *aGrid, wxString *aCurrentDir, wxString *aExt=nullptr, bool aNormalize=false, wxString aNormalizeBasePath=wxEmptyString)
void StartingKey(wxKeyEvent &event) override
This file is part of the common library.
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:373
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:82
TEXT_BUTTON_URL(wxWindow *aParent, DIALOG_SHIM *aParentDlg)
void DoSetPopupControl(wxComboPopup *popup) override
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:279
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
void SetSize(const wxRect &aRect) override
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
TEXT_BUTTON_FP_CHOOSER(wxWindow *aParent, DIALOG_SHIM *aParentDlg, const wxString &aPreselect)
TEXT_BUTTON_SYMBOL_CHOOSER(wxWindow *aParent, DIALOG_SHIM *aParentDlg, const wxString &aPreselect)
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
wxComboCtrl * Combo() const
void DoSetPopupControl(wxComboPopup *popup) override
#define _(s)
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:182
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
void DoSetPopupControl(wxComboPopup *popup) override
wxString GetValue() const override
see class PGM_BASE
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
The common library.
void DoSetPopupControl(wxComboPopup *popup) override
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
bool EndEdit(int, int, const wxGrid *, const wxString &, wxString *aNewVal) override
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:69