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-2022 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>
31#include <wx/stc/stc.h>
32
33#include <algorithm>
34#include "wx/listctrl.h"
35
36// Maps from dialogTitle <-> pageTitle for keeping track of last-selected pages.
37// This is not a simple page index because some dialogs have dynamic page sets.
38std::map<wxString, wxString> g_lastPage;
39std::map<wxString, wxString> g_lastParentPage;
40
41
42PAGED_DIALOG::PAGED_DIALOG( wxWindow* aParent, const wxString& aTitle, bool aShowReset,
43 const wxString& aAuxiliaryAction ) :
44 DIALOG_SHIM( aParent, wxID_ANY, aTitle, wxDefaultPosition, wxDefaultSize,
45 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
46 m_auxiliaryButton( nullptr ),
47 m_resetButton( nullptr ),
48 m_cancelButton( nullptr ),
49 m_title( aTitle )
50{
51 auto mainSizer = new wxBoxSizer( wxVERTICAL );
52 SetSizer( mainSizer );
53
54 m_infoBar = new WX_INFOBAR( this );
55 mainSizer->Add( m_infoBar, 0, wxEXPAND, 0 );
56
57 m_treebook = new wxTreebook( this, wxID_ANY );
58 m_treebook->SetFont( KIUI::GetControlFont( this ) );
59 mainSizer->Add( m_treebook, 1, wxEXPAND|wxLEFT|wxTOP, 10 );
60
61 auto line = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
62 wxLI_HORIZONTAL );
63 mainSizer->Add( line, 0, wxEXPAND|wxLEFT|wxTOP|wxRIGHT, 10 );
64
65 m_buttonsSizer = new wxBoxSizer( wxHORIZONTAL );
66
67 if( aShowReset )
68 {
69 m_resetButton = new wxButton( this, wxID_ANY, _( "Reset to Defaults" ) );
70 m_buttonsSizer->Add( m_resetButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT, 5 );
71 }
72
73 if( !aAuxiliaryAction.IsEmpty() )
74 {
75 m_auxiliaryButton = new wxButton( this, wxID_ANY, aAuxiliaryAction );
76 m_buttonsSizer->Add( m_auxiliaryButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT, 5 );
77 }
78
79 m_buttonsSizer->AddStretchSpacer();
80
81 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
82 wxButton* sdbSizerOK = new wxButton( this, wxID_OK );
83 sdbSizer->AddButton( sdbSizerOK );
84 wxButton* sdbSizerCancel = new wxButton( this, wxID_CANCEL );
85 sdbSizer->AddButton( sdbSizerCancel );
86 sdbSizer->Realize();
87
88 m_buttonsSizer->Add( sdbSizer, 1, 0, 5 );
89 mainSizer->Add( m_buttonsSizer, 0, wxALL|wxEXPAND, 5 );
90
92
93 // We normally save the dialog size and position based on its class-name. This class
94 // substitutes the title so that each distinctly-titled dialog can have its own saved
95 // size and position.
96 m_hash_key = aTitle;
97
99 {
100 m_auxiliaryButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onAuxiliaryAction,
101 this );
102 }
103
104 if( m_resetButton )
105 {
106 m_resetButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onResetButton, this );
107 }
108
109 m_treebook->Bind( wxEVT_CHAR_HOOK, &PAGED_DIALOG::onCharHook, this );
110 m_treebook->Bind( wxEVT_TREEBOOK_PAGE_CHANGED, &PAGED_DIALOG::onPageChanged, this );
111 m_treebook->Bind( wxEVT_TREEBOOK_PAGE_CHANGING, &PAGED_DIALOG::onPageChanging, this );
112}
113
114
116{
117 for( size_t i = 1; i < m_treebook->GetPageCount(); ++i )
118 m_macHack.push_back( true );
119
120 // For some reason adding page labels to the treeCtrl doesn't invalidate its bestSize
121 // cache so we have to do it by hand
122 m_treebook->GetTreeCtrl()->InvalidateBestSize();
123
124 for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
125 m_treebook->GetPage( i )->Layout();
126
127 m_treebook->Layout();
128 m_treebook->Fit();
129
131
132 Centre( wxBOTH );
133}
134
135
136void PAGED_DIALOG::SetInitialPage( const wxString& aPage, const wxString& aParentPage )
137{
138 g_lastPage[ m_title ] = aPage;
139 g_lastParentPage[ m_title ] = aParentPage;
140}
141
142
144{
145 // Store the current parentPageTitle/pageTitle hierarchy so we can re-select it
146 // next time.
147 wxString lastPage = wxEmptyString;
148 wxString lastParentPage = wxEmptyString;
149
150 int selected = m_treebook->GetSelection();
151
152 if( selected != wxNOT_FOUND )
153 {
154 lastPage = m_treebook->GetPageText( (unsigned) selected );
155
156 int parent = m_treebook->GetPageParent( (unsigned) selected );
157
158 if( parent != wxNOT_FOUND )
159 lastParentPage = m_treebook->GetPageText( (unsigned) parent );
160 }
161
162 g_lastPage[ m_title ] = lastPage;
163 g_lastParentPage[ m_title ] = lastParentPage;
164
166 {
167 m_auxiliaryButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onAuxiliaryAction,
168 this );
169 }
170
171 if( m_resetButton )
172 {
173 m_resetButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onResetButton, this );
174 }
175
176 m_treebook->Unbind( wxEVT_CHAR_HOOK, &PAGED_DIALOG::onCharHook, this );
177 m_treebook->Unbind( wxEVT_TREEBOOK_PAGE_CHANGED, &PAGED_DIALOG::onPageChanged, this );
178 m_treebook->Unbind( wxEVT_TREEBOOK_PAGE_CHANGING, &PAGED_DIALOG::onPageChanging, this );
179}
180
181
183{
185
186 // Call TransferDataToWindow() only once:
187 // this is enough on wxWidgets 3.1
188 if( !DIALOG_SHIM::TransferDataToWindow() )
189 return false;
190
191 // On wxWidgets 3.0, TransferDataFromWindow() is not called recursively
192 // so we have to call it for each page
193#if !wxCHECK_VERSION( 3, 1, 0 )
194 for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
195 {
196 wxWindow* page = m_treebook->GetPage( i );
197
198 if( !page->TransferDataToWindow() )
199 return false;
200 }
201#endif
202
203 // Search for a page matching the lastParentPageTitle/lastPageTitle hierarchy
204 wxString lastPage = g_lastPage[ m_title ];
205 wxString lastParentPage = g_lastParentPage[ m_title ];
206 int lastPageIndex = wxNOT_FOUND;
207
208 for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
209 {
210 if( m_treebook->GetPageText( i ) == lastPage )
211 {
212 if( lastParentPage.IsEmpty() )
213 {
214 lastPageIndex = i;
215 break;
216 }
217
218 if( m_treebook->GetPageParent( i ) >= 0
219 && m_treebook->GetPageText( (unsigned) m_treebook->GetPageParent( i ) ) == lastParentPage )
220 {
221 lastPageIndex = i;
222 break;
223 }
224 }
225 }
226
227 lastPageIndex = std::max( 0, lastPageIndex );
228 m_treebook->ChangeSelection( lastPageIndex );
229 UpdateResetButton( lastPageIndex );
230
231 return true;
232}
233
234
236{
237 bool ret = true;
238
239 // Call TransferDataFromWindow() only once:
240 // this is enough on wxWidgets 3.1
241 if( !DIALOG_SHIM::TransferDataFromWindow() )
242 ret = false;
243
244 // On wxWidgets 3.0, TransferDataFromWindow() is not called recursively
245 // so we have to call it for each page
246#if !wxCHECK_VERSION( 3, 1, 0 )
247 for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
248 {
249 wxWindow* page = m_treebook->GetPage( i );
250
251 if( !page->TransferDataFromWindow() )
252 {
253 m_treebook->ChangeSelection( i );
254 ret = false;
255 break;
256 }
257 }
258#endif
259
260 return ret;
261}
262
263
264void PAGED_DIALOG::SetError( const wxString& aMessage, const wxString& aPageName, int aCtrlId,
265 int aRow, int aCol )
266{
267 SetError( aMessage, FindWindow( aPageName ), FindWindow( aCtrlId ), aRow, aCol );
268}
269
270
271void PAGED_DIALOG::SetError( const wxString& aMessage, wxWindow* aPage, wxWindow* aCtrl,
272 int aRow, int aCol )
273{
274 if( aCtrl )
275 {
276 m_infoBar->ShowMessageFor( aMessage, 10000, wxICON_WARNING );
277
278 if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( aCtrl ) )
279 {
280 textCtrl->SetSelection( -1, -1 );
281 textCtrl->SetFocus();
282 return;
283 }
284
285 if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( aCtrl ) )
286 {
287 if( aRow > 0 )
288 {
289 int pos = scintilla->PositionFromLine( aRow - 1 ) + ( aCol - 1 );
290 scintilla->GotoPos( pos );
291 }
292
293 scintilla->SetFocus();
294 return;
295 }
296
297 if( wxGrid* grid = dynamic_cast<wxGrid*>( aCtrl ) )
298 {
299 grid->SetFocus();
300 grid->MakeCellVisible( aRow, aCol );
301 grid->SetGridCursor( aRow, aCol );
302
303 grid->EnableCellEditControl( true );
304 grid->ShowCellEditControl();
305 return;
306 }
307 }
308}
309
310
312{
313 wxWindow* panel = m_treebook->GetPage( aPage );
314
315 // Enable the reset button only if the page is re-settable
316 if( m_resetButton )
317 {
318 if( panel && ( panel->GetWindowStyle() & wxRESETTABLE ) )
319 {
320 m_resetButton->SetLabel( wxString::Format( _( "Reset %s to Defaults" ),
321 m_treebook->GetPageText( aPage ) ) );
322 m_resetButton->SetToolTip( panel->GetHelpTextAtPoint( wxPoint( -INT_MAX, INT_MAX ),
323 wxHelpEvent::Origin_Unknown ) );
324 m_resetButton->Enable( true );
325 }
326 else
327 {
328 m_resetButton->SetLabel( _( "Reset to Defaults" ) );
329 m_resetButton->SetToolTip( wxString() );
330 m_resetButton->Enable( false );
331 }
332
333 m_resetButton->GetParent()->Layout();
334 }
335}
336
337
338void PAGED_DIALOG::onCharHook( wxKeyEvent& aEvent )
339{
340 if( dynamic_cast<wxTextEntry*>( aEvent.GetEventObject() )
341 || dynamic_cast<wxStyledTextCtrl*>( aEvent.GetEventObject() )
342 || dynamic_cast<wxListView*>( aEvent.GetEventObject() ) )
343 {
344 aEvent.Skip();
345 return;
346 }
347
348 if( aEvent.GetKeyCode() == WXK_UP )
349 {
350 int page = m_treebook->GetSelection();
351
352 if( page >= 1 )
353 {
354 if( m_treebook->GetPage( page - 1 )->GetChildren().IsEmpty() )
355 m_treebook->SetSelection( std::max( page - 2, 0 ) );
356 else
357 m_treebook->SetSelection( page - 1 );
358 }
359
360 m_treebook->GetTreeCtrl()->SetFocus(); // Don't allow preview canvas to steal focus
361 }
362 else if( aEvent.GetKeyCode() == WXK_DOWN )
363 {
364 int page = m_treebook->GetSelection();
365
366 m_treebook->SetSelection( std::min<int>( page + 1, m_treebook->GetPageCount() - 1 ) );
367
368 m_treebook->GetTreeCtrl()->SetFocus(); // Don't allow preview canvas to steal focus
369 }
370 else
371 {
372 aEvent.Skip();
373 }
374}
375
376
377void PAGED_DIALOG::onPageChanged( wxBookCtrlEvent& event )
378{
379 size_t page = event.GetSelection();
380
381 // Use the first sub-page when a tree level node is selected.
382 if( m_treebook->GetCurrentPage()->GetChildren().IsEmpty()
383 && page + 1 < m_treebook->GetPageCount() )
384 {
385 m_treebook->ChangeSelection( ++page );
386 }
387
388 UpdateResetButton( page );
389
390#ifdef __WXMAC__
391 // Work around an OSX wxWidgets issue where the wxGrid children don't get placed correctly
392 // until the first resize event
393 if( page < m_macHack.size() && m_macHack[ page ] )
394 {
395 wxSize pageSize = m_treebook->GetPage( page )->GetSize();
396 pageSize.x -= 5;
397 pageSize.y += 2;
398
399 m_treebook->GetPage( page )->SetSize( pageSize );
400 m_macHack[ page ] = false;
401 }
402#else
403 wxSizeEvent evt( wxDefaultSize );
404 wxQueueEvent( m_treebook, evt.Clone() );
405#endif
406}
407
408
409void PAGED_DIALOG::onPageChanging( wxBookCtrlEvent& aEvent )
410{
411 int currentPage = aEvent.GetOldSelection();
412
413 if( currentPage == wxNOT_FOUND )
414 return;
415
416 wxWindow* page = m_treebook->GetPage( currentPage );
417
418 wxCHECK( page, /* void */ );
419
420 // If there is a validation error on the current page, don't allow the page change.
421 if( !page->Validate() || !page->TransferDataFromWindow() )
422 {
423 aEvent.Veto();
424 return;
425 }
426}
427
428
429void PAGED_DIALOG::onResetButton( wxCommandEvent& aEvent )
430{
431 int sel = m_treebook->GetSelection();
432
433 if( sel == wxNOT_FOUND )
434 return;
435
436 // NB: dynamic_cast doesn't work over Kiway
437 wxWindow* panel = m_treebook->GetPage( sel );
438
439 if( panel )
440 {
441 wxCommandEvent resetCommand( wxEVT_COMMAND_BUTTON_CLICKED, ID_RESET_PANEL );
442 panel->ProcessWindowEvent( resetCommand );
443 }
444}
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:83
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
Definition: dialog_shim.h:203
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
WX_INFOBAR * m_infoBar
Definition: paged_dialog.h:67
bool TransferDataToWindow() override
~PAGED_DIALOG() override
wxButton * m_auxiliaryButton
Definition: paged_dialog.h:64
std::vector< bool > m_macHack
Definition: paged_dialog.h:74
wxTreebook * m_treebook
Definition: paged_dialog.h:63
bool TransferDataFromWindow() override
void finishInitialization()
void UpdateResetButton(int aPage)
PAGED_DIALOG(wxWindow *aParent, const wxString &aTitle, bool aShowReset, const wxString &aAuxiliaryAction=wxEmptyString)
void SetInitialPage(const wxString &aPage, const wxString &aParentPage=wxEmptyString)
virtual void onPageChanging(wxBookCtrlEvent &aEvent)
virtual void onCharHook(wxKeyEvent &aEvent)
virtual void onAuxiliaryAction(wxCommandEvent &aEvent)
Definition: paged_dialog.h:57
wxString m_title
Definition: paged_dialog.h:70
virtual void onResetButton(wxCommandEvent &aEvent)
virtual void onPageChanged(wxBookCtrlEvent &aEvent)
wxBoxSizer * m_buttonsSizer
Definition: paged_dialog.h:72
void SetError(const wxString &aMessage, const wxString &aPageName, int aCtrlId, int aRow=-1, int aCol=-1)
wxButton * m_resetButton
Definition: paged_dialog.h:65
A modified version of the wxInfoBar class that allows us to:
Definition: infobar.h:75
void ShowMessageFor(const wxString &aMessage, int aTime, int aFlags=wxICON_INFORMATION, MESSAGE_TYPE aType=WX_INFOBAR::MESSAGE_TYPE::GENERIC)
Show the infobar with the provided message and icon for a specific period of time.
Definition: infobar.cpp:128
This file is part of the common library.
#define _(s)
wxFont GetControlFont(wxWindow *aWindow)
Definition: ui_common.cpp:150
std::map< wxString, wxString > g_lastPage
std::map< wxString, wxString > g_lastParentPage
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
#define wxRESETTABLE
#define ID_RESET_PANEL