KiCad PCB EDA Suite
paged_dialog.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) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <confirm.h>
22 #include <wx/button.h>
23 #include <wx/grid.h>
24 #include <wx/sizer.h>
25 #include <wx/statline.h>
26 #include <wx/treebook.h>
27 #include <wx/treectrl.h>
28 
29 #include <widgets/infobar.h>
30 #include <widgets/paged_dialog.h>
31 #include <wx/stc/stc.h>
32 
33 #include <algorithm>
34 
35 // Maps from dialogTitle <-> pageTitle for keeping track of last-selected pages.
36 // This is not a simple page index because some dialogs have dynamic page sets.
37 std::map<wxString, wxString> g_lastPage;
38 std::map<wxString, wxString> g_lastParentPage;
39 
40 
41 PAGED_DIALOG::PAGED_DIALOG( wxWindow* aParent, const wxString& aTitle, bool aShowReset,
42  const wxString& aAuxiliaryAction ) :
43  DIALOG_SHIM( aParent, wxID_ANY, aTitle, wxDefaultPosition, wxDefaultSize,
44  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
45  m_auxiliaryButton( nullptr ),
46  m_resetButton( nullptr ),
47  m_cancelButton( nullptr ),
48  m_title( aTitle ),
49  m_dirty( false ),
50  m_errorCtrl( nullptr ),
51  m_errorRow( 0 ),
52  m_errorCol( 0 )
53 {
54  auto mainSizer = new wxBoxSizer( wxVERTICAL );
55  SetSizer( mainSizer );
56 
57  m_infoBar = new WX_INFOBAR( this );
58  mainSizer->Add( m_infoBar, 0, wxEXPAND, 0 );
59 
60  m_treebook = new wxTreebook( this, wxID_ANY );
61  mainSizer->Add( m_treebook, 1, wxEXPAND|wxLEFT|wxTOP, 10 );
62 
63  auto line = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
64  wxLI_HORIZONTAL );
65  mainSizer->Add( line, 0, wxEXPAND|wxLEFT|wxTOP|wxRIGHT, 10 );
66 
67  m_buttonsSizer = new wxBoxSizer( wxHORIZONTAL );
68 
69  if( aShowReset )
70  {
71  m_resetButton = new wxButton( this, wxID_ANY, _( "Reset to Defaults" ) );
72  m_buttonsSizer->Add( m_resetButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT, 5 );
73  }
74 
75  if( !aAuxiliaryAction.IsEmpty() )
76  {
77  m_auxiliaryButton = new wxButton( this, wxID_ANY, aAuxiliaryAction );
78  m_buttonsSizer->Add( m_auxiliaryButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT, 5 );
79  }
80 
81  m_buttonsSizer->AddStretchSpacer();
82 
83  wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
84  wxButton* sdbSizerOK = new wxButton( this, wxID_OK );
85  sdbSizer->AddButton( sdbSizerOK );
86  wxButton* sdbSizerCancel = new wxButton( this, wxID_CANCEL );
87  sdbSizer->AddButton( sdbSizerCancel );
88  sdbSizer->Realize();
89 
90  m_buttonsSizer->Add( sdbSizer, 1, 0, 5 );
91  mainSizer->Add( m_buttonsSizer, 0, wxALL|wxEXPAND, 5 );
92 
93  sdbSizerOK->SetDefault();
94 
95  // We normally save the dialog size and position based on its class-name. This class
96  // substitutes the title so that each distinctly-titled dialog can have its own saved
97  // size and position.
98  m_hash_key = aTitle;
99 
100  if( m_auxiliaryButton )
101  {
102  m_auxiliaryButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED,
103  wxCommandEventHandler( PAGED_DIALOG::OnAuxiliaryAction ),
104  nullptr, this );
105  }
106 
107  if( m_resetButton )
108  {
109  m_resetButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED,
110  wxCommandEventHandler( PAGED_DIALOG::OnResetButton ), nullptr,
111  this );
112  }
113 
114  m_treebook->Connect( wxEVT_TREEBOOK_PAGE_CHANGED,
115  wxBookCtrlEventHandler( PAGED_DIALOG::OnPageChange ), nullptr, this );
116  Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( PAGED_DIALOG::OnUpdateUI ), nullptr, this );
117 }
118 
119 
121 {
122  for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
123  m_macHack.push_back( true );
124 
125  // For some reason adding page labels to the treeCtrl doesn't invalidate its bestSize
126  // cache so we have to do it by hand
127  m_treebook->GetTreeCtrl()->InvalidateBestSize();
128 
129  for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
130  {
131  m_treebook->ExpandNode( i );
132  m_treebook->GetPage( i )->Layout();
133  }
134 
135  m_treebook->Fit();
136  m_treebook->Layout();
137 
139 }
140 
141 
142 void PAGED_DIALOG::SetInitialPage( const wxString& aPage, const wxString& aParentPage )
143 {
144  g_lastPage[ m_title ] = aPage;
145  g_lastParentPage[ m_title ] = aParentPage;
146 }
147 
148 
150 {
151  // Store the current parentPageTitle/pageTitle hierarchy so we can re-select it
152  // next time.
153  wxString lastPage = wxEmptyString;
154  wxString lastParentPage = wxEmptyString;
155 
156  int selected = m_treebook->GetSelection();
157 
158  if( selected != wxNOT_FOUND )
159  {
160  lastPage = m_treebook->GetPageText( (unsigned) selected );
161 
162  int parent = m_treebook->GetPageParent( (unsigned) selected );
163 
164  if( parent != wxNOT_FOUND )
165  lastParentPage = m_treebook->GetPageText( (unsigned) parent );
166  }
167 
168  g_lastPage[ m_title ] = lastPage;
169  g_lastParentPage[ m_title ] = lastParentPage;
170 
171  if( m_auxiliaryButton )
172  {
173  m_auxiliaryButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED,
174  wxCommandEventHandler( PAGED_DIALOG::OnAuxiliaryAction ),
175  nullptr, this );
176  }
177 
178  if( m_resetButton )
179  {
180  m_resetButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED,
181  wxCommandEventHandler( PAGED_DIALOG::OnResetButton ), nullptr,
182  this );
183  }
184 
185  m_treebook->Disconnect( wxEVT_TREEBOOK_PAGE_CHANGED,
186  wxBookCtrlEventHandler( PAGED_DIALOG::OnPageChange ), nullptr, this );
187  Disconnect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( PAGED_DIALOG::OnUpdateUI ),
188  nullptr, this );
189 }
190 
191 
193 {
195 
196  // Call TransferDataToWindow() only once:
197  // this is enough on wxWidgets 3.1
198  if( !DIALOG_SHIM::TransferDataToWindow() )
199  return false;
200 
201  // On wxWidgets 3.0, TransferDataFromWindow() is not called recursively
202  // so we have to call it for each page
203 #if !wxCHECK_VERSION( 3, 1, 0 )
204  for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
205  {
206  wxWindow* page = m_treebook->GetPage( i );
207 
208  if( !page->TransferDataToWindow() )
209  return false;
210  }
211 #endif
212 
213  // Search for a page matching the lastParentPageTitle/lastPageTitle hierarchy
214  wxString lastPage = g_lastPage[ m_title ];
215  wxString lastParentPage = g_lastParentPage[ m_title ];
216  int lastPageIndex = wxNOT_FOUND;
217 
218  for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
219  {
220  if( m_treebook->GetPageText( i ) == lastPage )
221  {
222  if( lastParentPage.IsEmpty() )
223  {
224  lastPageIndex = i;
225  break;
226  }
227 
228  if( m_treebook->GetPageParent( i ) >= 0
229  && m_treebook->GetPageText( (unsigned) m_treebook->GetPageParent( i ) ) == lastParentPage )
230  {
231  lastPageIndex = i;
232  break;
233  }
234  }
235  }
236 
237  m_treebook->SetSelection( (unsigned) std::max( 0, lastPageIndex ) );
238 
239  return true;
240 }
241 
242 
244 {
245  bool ret = true;
246 
247  // Call TransferDataFromWindow() only once:
248  // this is enough on wxWidgets 3.1
249  if( !DIALOG_SHIM::TransferDataFromWindow() )
250  ret = false;
251 
252  // On wxWidgets 3.0, TransferDataFromWindow() is not called recursively
253  // so we have to call it for each page
254 #if !wxCHECK_VERSION( 3, 1, 0 )
255  for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
256  {
257  wxWindow* page = m_treebook->GetPage( i );
258 
259  if( !page->TransferDataFromWindow() )
260  {
261  ret = false;
262  break;
263  }
264  }
265 #endif
266 
267  if( !ret && !m_errorMessage.IsEmpty() )
268  m_infoBar->ShowMessage( m_errorMessage, wxICON_WARNING );
269 
270  return ret;
271 }
272 
273 
274 void PAGED_DIALOG::SetError( const wxString& aMessage, const wxString& aPageName, int aCtrlId,
275  int aRow, int aCol )
276 {
277  SetError( aMessage, FindWindow( aPageName ), FindWindow( aCtrlId ), aRow, aCol );
278 }
279 
280 
281 void PAGED_DIALOG::SetError( const wxString& aMessage, wxWindow* aPage, wxWindow* aCtrl,
282  int aRow, int aCol )
283 {
284  for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
285  {
286  if( m_treebook->GetPage( i ) == aPage )
287  {
288  m_treebook->SetSelection( i );
289  break;
290  }
291  }
292 
293  // Once the page has been changed we want to wait for it to update before displaying
294  // the error dialog. So store the rest of the error info and wait for OnUpdateUI.
295  m_errorMessage = aMessage;
296  m_errorCtrl = aCtrl;
297  m_errorRow = aRow;
298  m_errorCol = aCol;
299 }
300 
301 
302 void PAGED_DIALOG::OnUpdateUI( wxUpdateUIEvent& event )
303 {
304  // Handle an error. This is delayed to OnUpdateUI so that we can change the focus
305  // even when the original validation was triggered from a killFocus event, and so
306  // that the corresponding notebook page can be shown in the background when triggered
307  // from an OK.
308  if( m_errorCtrl )
309  {
310  // We will re-enter this routine when the error dialog is displayed, so make
311  // sure we don't keep putting up more dialogs.
312  wxWindow* ctrl = m_errorCtrl;
313  m_errorCtrl = nullptr;
314 
315  m_infoBar->ShowMessageFor( m_errorMessage, 10000, wxICON_WARNING );
316 
317  if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( ctrl ) )
318  {
319  textCtrl->SetSelection( -1, -1 );
320  textCtrl->SetFocus();
321  return;
322  }
323 
324  if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( ctrl ) )
325  {
326  if( m_errorRow > 0 )
327  {
328  int pos = scintilla->PositionFromLine( m_errorRow - 1 ) + ( m_errorCol - 1 );
329  scintilla->GotoPos( pos );
330  }
331 
332  scintilla->SetFocus();
333  return;
334  }
335 
336  if( wxGrid* grid = dynamic_cast<wxGrid*>( ctrl ) )
337  {
338  grid->SetFocus();
339  grid->MakeCellVisible( m_errorRow, m_errorCol );
340  grid->SetGridCursor( m_errorRow, m_errorCol );
341 
342  grid->EnableCellEditControl( true );
343  grid->ShowCellEditControl();
344  return;
345  }
346  }
347 
348  if( m_treebook->GetCurrentPage()->GetChildren().IsEmpty() )
349  {
350  unsigned next = m_treebook->GetSelection() + 1;
351 
352  if( next < m_treebook->GetPageCount() )
353  m_treebook->SetSelection( next );
354  }
355 }
356 
357 
358 void PAGED_DIALOG::OnPageChange( wxBookCtrlEvent& event )
359 {
360  size_t page = event.GetSelection();
361 
362  // Enable the reset button only if the page is re-settable
363  if( m_resetButton )
364  {
365  if( auto panel = dynamic_cast<RESETTABLE_PANEL*>( m_treebook->GetPage( page ) ) )
366  {
367  m_resetButton->SetToolTip( panel->GetResetTooltip() );
368  m_resetButton->Enable( true );
369  }
370  else
371  {
372  m_resetButton->SetToolTip( wxString() );
373  m_resetButton->Enable( false );
374  }
375 
376  }
377 
378  // Work around an OSX bug where the wxGrid children don't get placed correctly until
379  // the first resize event
380 #ifdef __WXMAC__
381  if( page + 1 <= m_macHack.size() && m_macHack[ page ] )
382  {
383  wxSize pageSize = m_treebook->GetPage( page )->GetSize();
384  pageSize.x -= 5;
385  pageSize.y += 2;
386 
387  m_treebook->GetPage( page )->SetSize( pageSize );
388  m_macHack[ page ] = false;
389  }
390 #endif
391 
392  Layout();
393 }
394 
395 
396 void PAGED_DIALOG::OnResetButton( wxCommandEvent& aEvent )
397 {
398  int sel = m_treebook->GetSelection();
399 
400  if( sel == wxNOT_FOUND )
401  return;
402 
403  RESETTABLE_PANEL* panel = dynamic_cast<RESETTABLE_PANEL*>( m_treebook->GetPage( sel ) );
404 
405  if( panel )
406  panel->ResetPanel();
407 }
CITER next(CITER it)
Definition: ptree.cpp:126
void ShowMessageFor(const wxString &aMessage, int aTime, int aFlags=wxICON_INFORMATION)
Show the infobar with the provided message and icon for a specific period of time.
Definition: infobar.cpp:123
void ShowMessage(const wxString &aMessage, int aFlags=wxICON_INFORMATION) override
Show the info bar with the provided message and icon.
Definition: infobar.cpp:134
std::map< wxString, wxString > g_lastPage
This file is part of the common library.
std::vector< bool > m_macHack
Definition: paged_dialog.h:80
std::string m_hash_key
Definition: dialog_shim.h:199
void SetInitialPage(const wxString &aPage, const wxString &aParentPage=wxEmptyString)
~PAGED_DIALOG() override
std::map< wxString, wxString > g_lastParentPage
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:82
void SetError(const wxString &aMessage, const wxString &aPageName, int aCtrlId, int aRow=-1, int aCol=-1)
void OnPageChange(wxBookCtrlEvent &event)
bool TransferDataToWindow() override
wxWindow * m_errorCtrl
Definition: paged_dialog.h:74
wxButton * m_auxiliaryButton
Definition: paged_dialog.h:63
bool TransferDataFromWindow() override
void OnUpdateUI(wxUpdateUIEvent &event)
WX_INFOBAR * m_infoBar
Definition: paged_dialog.h:66
wxTreebook * m_treebook
Definition: paged_dialog.h:62
#define _(s)
wxBoxSizer * m_buttonsSizer
Definition: paged_dialog.h:78
virtual void ResetPanel()=0
Reset the contents of this panel.
PAGED_DIALOG(wxWindow *aParent, const wxString &aTitle, bool aShowReset, const wxString &aAuxiliaryAction=wxEmptyString)
wxString m_errorMessage
Definition: paged_dialog.h:73
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
A modified version of the wxInfoBar class that allows us to:
Definition: infobar.h:73
virtual void OnAuxiliaryAction(wxCommandEvent &event)
Definition: paged_dialog.h:57
wxButton * m_resetButton
Definition: paged_dialog.h:64
void OnResetButton(wxCommandEvent &aEvent)
void finishInitialization()
wxString m_title
Definition: paged_dialog.h:69
A wxPanel that is designed to be reset in a standard manner.