KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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 <widgets/wx_infobar.h>
23#include <widgets/wx_panel.h>
25#include <widgets/wx_treebook.h>
26#include <widgets/ui_common.h>
27
28#include <wx/button.h>
29#include <wx/grid.h>
30#include <wx/sizer.h>
31#include <wx/treebook.h>
32#include <wx/treectrl.h>
33#include <wx/listctrl.h>
34#include <wx/stc/stc.h>
35
36#include <paths.h>
37
38#include <launch_ext.h>
39
40#include <algorithm>
41
42// Maps from dialogTitle <-> pageTitle for keeping track of last-selected pages.
43// This is not a simple page index because some dialogs have dynamic page sets.
44std::map<wxString, wxString> g_lastPage;
45std::map<wxString, wxString> g_lastParentPage;
46
47
48PAGED_DIALOG::PAGED_DIALOG( wxWindow* aParent, const wxString& aTitle, bool aShowReset,
49 bool aShowOpenFolder, const wxString& aAuxiliaryAction,
50 const wxSize& aInitialSize ) :
51 DIALOG_SHIM( aParent, wxID_ANY, aTitle, wxDefaultPosition, aInitialSize,
52 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
53 m_auxiliaryButton( nullptr ),
54 m_resetButton( nullptr ),
55 m_openPrefsDirButton( nullptr ),
56 m_title( aTitle )
57{
58 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
59 SetSizer( mainSizer );
60
61 m_infoBar = new WX_INFOBAR( this );
62 mainSizer->Add( m_infoBar, 0, wxEXPAND, 0 );
63
64 WX_PANEL* treebookPanel = new WX_PANEL( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
65 wxBORDER_NONE | wxTAB_TRAVERSAL );
66 treebookPanel->SetBorders( false, false, false, true );
67 wxBoxSizer* treebookSizer = new wxBoxSizer( wxVERTICAL );
68 treebookPanel->SetSizer( treebookSizer );
69
70 m_treebook = new WX_TREEBOOK( treebookPanel, wxID_ANY );
71 m_treebook->SetFont( KIUI::GetControlFont( this ) );
72 m_treebook->SetFitToCurrentPage( true );
73
74 long treeCtrlFlags = m_treebook->GetTreeCtrl()->GetWindowStyleFlag();
75 treeCtrlFlags = ( treeCtrlFlags & ~wxBORDER_MASK ) | wxBORDER_NONE;
76 m_treebook->GetTreeCtrl()->SetWindowStyleFlag( treeCtrlFlags );
77
78 treebookSizer->Add( m_treebook, 1, wxEXPAND|wxBOTTOM, 2 );
79 mainSizer->Add( treebookPanel, 1, wxEXPAND, 0 );
80
81 m_buttonsSizer = new wxBoxSizer( wxHORIZONTAL );
82
83 if( aShowReset )
84 {
85 m_resetButton = new wxButton( this, wxID_ANY, _( "Reset to Defaults" ) );
86 m_buttonsSizer->Add( m_resetButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT, 5 );
87 }
88
89 if( aShowOpenFolder )
90 {
91#ifdef __WXMAC__
92 m_openPrefsDirButton = new wxButton( this, wxID_ANY, _( "Reveal Preferences in Finder" ) );
93#else
94 m_openPrefsDirButton = new wxButton( this, wxID_ANY, _( "Open Preferences Directory" ) );
95#endif
97 wxALIGN_CENTER_VERTICAL | wxRIGHT | wxLEFT, 5 );
98 }
99
100
101 if( !aAuxiliaryAction.IsEmpty() )
102 {
103 m_auxiliaryButton = new wxButton( this, wxID_ANY, aAuxiliaryAction );
104 m_buttonsSizer->Add( m_auxiliaryButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT, 5 );
105 }
106
107 m_buttonsSizer->AddStretchSpacer();
108
109 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
110 wxButton* sdbSizerOK = new wxButton( this, wxID_OK );
111 sdbSizer->AddButton( sdbSizerOK );
112 wxButton* sdbSizerCancel = new wxButton( this, wxID_CANCEL );
113 sdbSizer->AddButton( sdbSizerCancel );
114 sdbSizer->Realize();
115
116 m_buttonsSizer->Add( sdbSizer, 1, 0, 5 );
117 mainSizer->Add( m_buttonsSizer, 0, wxALL|wxEXPAND, 5 );
118
120
121 // We normally save the dialog size and position based on its class-name. This class
122 // substitutes the title so that each distinctly-titled dialog can have its own saved
123 // size and position.
124 m_hash_key = aTitle;
125
127 {
128 m_auxiliaryButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onAuxiliaryAction,
129 this );
130 }
131
132 if( m_resetButton )
133 {
134 m_resetButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onResetButton, this );
135 }
136
138 {
139 m_openPrefsDirButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED,
141 }
142
143 m_treebook->Bind( wxEVT_CHAR_HOOK, &PAGED_DIALOG::onCharHook, this );
144 m_treebook->Bind( wxEVT_TREEBOOK_PAGE_CHANGED, &PAGED_DIALOG::onPageChanged, this );
145 m_treebook->Bind( wxEVT_TREEBOOK_PAGE_CHANGING, &PAGED_DIALOG::onPageChanging, this );
146}
147
148
150{
151 for( size_t i = 1; i < m_treebook->GetPageCount(); ++i )
152 m_macHack.push_back( true );
153
154 // For some reason adding page labels to the treeCtrl doesn't invalidate its bestSize
155 // cache so we have to do it by hand
156 m_treebook->GetTreeCtrl()->InvalidateBestSize();
157
158 for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
159 m_treebook->GetPage( i )->Layout();
160
161 m_treebook->Layout();
162 m_treebook->Fit();
163
165}
166
167
168void PAGED_DIALOG::SetInitialPage( const wxString& aPage, const wxString& aParentPage )
169{
170 g_lastPage[ m_title ] = aPage;
171 g_lastParentPage[ m_title ] = aParentPage;
172}
173
174
176{
177 // Store the current parentPageTitle/pageTitle hierarchy so we can re-select it
178 // next time.
179 wxString lastPage = wxEmptyString;
180 wxString lastParentPage = wxEmptyString;
181
182 int selected = m_treebook->GetSelection();
183
184 if( selected != wxNOT_FOUND )
185 {
186 lastPage = m_treebook->GetPageText( (unsigned) selected );
187
188 int parent = m_treebook->GetPageParent( (unsigned) selected );
189
190 if( parent != wxNOT_FOUND )
191 lastParentPage = m_treebook->GetPageText( (unsigned) parent );
192 }
193
194 g_lastPage[ m_title ] = lastPage;
195 g_lastParentPage[ m_title ] = lastParentPage;
196
198 {
199 m_auxiliaryButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onAuxiliaryAction,
200 this );
201 }
202
203 if( m_resetButton )
204 {
205 m_resetButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onResetButton, this );
206 }
207
209 {
210 m_openPrefsDirButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED,
212 }
213
214 m_treebook->Unbind( wxEVT_CHAR_HOOK, &PAGED_DIALOG::onCharHook, this );
215 m_treebook->Unbind( wxEVT_TREEBOOK_PAGE_CHANGED, &PAGED_DIALOG::onPageChanged, this );
216 m_treebook->Unbind( wxEVT_TREEBOOK_PAGE_CHANGING, &PAGED_DIALOG::onPageChanging, this );
217}
218
219
221{
223
224 // Call TransferDataToWindow() only once:
225 // this is enough on wxWidgets 3.1
226 if( !DIALOG_SHIM::TransferDataToWindow() )
227 return false;
228
229 // Search for a page matching the lastParentPageTitle/lastPageTitle hierarchy
230 wxString lastPage = g_lastPage[ m_title ];
231 wxString lastParentPage = g_lastParentPage[ m_title ];
232 int lastPageIndex = wxNOT_FOUND;
233
234 for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
235 {
236 if( m_treebook->GetPageText( i ) == lastPage )
237 {
238 if( lastParentPage.IsEmpty() )
239 {
240 lastPageIndex = i;
241 break;
242 }
243
244 if( m_treebook->GetPageParent( i ) >= 0
245 && m_treebook->GetPageText( (unsigned) m_treebook->GetPageParent( i ) ) == lastParentPage )
246 {
247 lastPageIndex = i;
248 break;
249 }
250 }
251 }
252
253 lastPageIndex = std::max( 0, lastPageIndex );
254 m_treebook->SetSelection( lastPageIndex );
255 UpdateResetButton( lastPageIndex );
256
257 return true;
258}
259
260
262{
263 bool ret = true;
264
265 // Call TransferDataFromWindow() only once:
266 // this is enough on wxWidgets 3.1
267 if( !DIALOG_SHIM::TransferDataFromWindow() )
268 ret = false;
269
270 return ret;
271}
272
273
275{
276 while( aParent )
277 {
278 if( PAGED_DIALOG* parentDialog = dynamic_cast<PAGED_DIALOG*>( aParent ) )
279 return parentDialog;
280
281 aParent = aParent->GetParent();
282 }
283
284 return nullptr;
285}
286
287
288void PAGED_DIALOG::SetError( const wxString& aMessage, const wxString& aPageName, int aCtrlId,
289 int aRow, int aCol )
290{
291 SetError( aMessage, FindWindow( aPageName ), FindWindow( aCtrlId ), aRow, aCol );
292}
293
294
295void PAGED_DIALOG::SetError( const wxString& aMessage, wxWindow* aPage, wxWindow* aCtrl,
296 int aRow, int aCol )
297{
298 m_infoBar->ShowMessageFor( aMessage, 10000, wxICON_WARNING );
299
300 if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( aCtrl ) )
301 {
302 textCtrl->SetSelection( -1, -1 );
303 textCtrl->SetFocus();
304 return;
305 }
306
307 if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( aCtrl ) )
308 {
309 if( aRow > 0 )
310 {
311 int pos = scintilla->PositionFromLine( aRow - 1 ) + ( aCol - 1 );
312 scintilla->GotoPos( pos );
313 }
314
315 scintilla->SetFocus();
316 return;
317 }
318
319 if( wxGrid* grid = dynamic_cast<wxGrid*>( aCtrl ) )
320 {
321 grid->SetFocus();
322 grid->MakeCellVisible( aRow, aCol );
323 grid->SetGridCursor( aRow, aCol );
324
325 grid->EnableCellEditControl( true );
326 grid->ShowCellEditControl();
327 return;
328 }
329}
330
331
333{
334 wxWindow* panel = m_treebook->ResolvePage( aPage );
335
336 // Enable the reset button only if the page is re-settable
337 if( m_resetButton )
338 {
339 if( panel && ( panel->GetWindowStyle() & wxRESETTABLE ) )
340 {
341 wxString name = m_treebook->GetPageText( aPage );
342 name.Replace( wxT( "&" ), wxT( "&&" ) );
343
344 m_resetButton->SetLabel( wxString::Format( _( "Reset %s to Defaults" ), name ) );
345 m_resetButton->SetToolTip( panel->GetHelpTextAtPoint( wxPoint( -INT_MAX, INT_MAX ),
346 wxHelpEvent::Origin_Unknown ) );
347 m_resetButton->Enable( true );
348 }
349 else
350 {
351 m_resetButton->SetLabel( _( "Reset to Defaults" ) );
352 m_resetButton->SetToolTip( wxString() );
353 m_resetButton->Enable( false );
354 }
355
356 m_resetButton->GetParent()->Layout();
357 }
358}
359
360
361void PAGED_DIALOG::onCharHook( wxKeyEvent& aEvent )
362{
363 if( dynamic_cast<wxTextEntry*>( aEvent.GetEventObject() )
364 || dynamic_cast<wxStyledTextCtrl*>( aEvent.GetEventObject() )
365 || dynamic_cast<wxListView*>( aEvent.GetEventObject() )
366 || dynamic_cast<wxGrid*>( FindFocus() ) )
367 {
368 aEvent.Skip();
369 return;
370 }
371
372 if( aEvent.GetKeyCode() == WXK_UP )
373 {
374 int page = m_treebook->GetSelection();
375
376 if( page >= 1 )
377 {
378 if( m_treebook->GetPage( page - 1 )->GetChildren().IsEmpty() )
379 m_treebook->SetSelection( std::max( page - 2, 0 ) );
380 else
381 m_treebook->SetSelection( page - 1 );
382 }
383
384 m_treebook->GetTreeCtrl()->SetFocus(); // Don't allow preview canvas to steal gridFocus
385 }
386 else if( aEvent.GetKeyCode() == WXK_DOWN )
387 {
388 int page = m_treebook->GetSelection();
389
390 m_treebook->SetSelection( std::min<int>( page + 1, m_treebook->GetPageCount() - 1 ) );
391
392 m_treebook->GetTreeCtrl()->SetFocus(); // Don't allow preview canvas to steal gridFocus
393 }
394 else
395 {
396 aEvent.Skip();
397 }
398}
399
400
401void PAGED_DIALOG::onPageChanged( wxBookCtrlEvent& event )
402{
403 size_t page = event.GetSelection();
404
405 // Use the first sub-page when a tree level node is selected.
406 if( m_treebook->GetCurrentPage()->GetChildren().IsEmpty()
407 && page + 1 < m_treebook->GetPageCount() )
408 {
409 m_treebook->ChangeSelection( ++page );
410 }
411
412 UpdateResetButton( page );
413
414 // Make sure the dialog size is enough to fit the page content.
415 m_treebook->InvalidateBestSize();
416
417 SetMinSize( wxDefaultSize );
418 wxSize minSize = GetBestSize();
419 minSize.IncTo( FromDIP( wxSize( 600, 500 ) ) );
420 minSize.DecTo( FromDIP( wxSize( 1500, 900 ) ) ); // Failsafe
421 SetMinSize( minSize );
422
423 wxSize currentSize = GetSize();
424 wxSize newSize = currentSize;
425 newSize.IncTo( minSize );
426
427 if( newSize != currentSize )
428 SetSize( newSize );
429
430#ifdef __WXMAC__
431 // Work around an OSX wxWidgets issue where the wxGrid children don't get placed correctly
432 // until the first resize event
433 if( page < m_macHack.size() && m_macHack[ page ] )
434 {
435 wxSize pageSize = m_treebook->GetPage( page )->GetSize();
436 pageSize.x += 1;
437 pageSize.y += 2;
438
439 m_treebook->GetPage( page )->SetSize( pageSize );
440 m_macHack[ page ] = false;
441 }
442#else
443 wxSizeEvent evt( wxDefaultSize );
444 wxQueueEvent( m_treebook, evt.Clone() );
445#endif
446}
447
448
449void PAGED_DIALOG::onPageChanging( wxBookCtrlEvent& aEvent )
450{
451 int currentPage = aEvent.GetOldSelection();
452
453 if( currentPage == wxNOT_FOUND )
454 return;
455
456 wxWindow* page = m_treebook->GetPage( currentPage );
457
458 wxCHECK( page, /* void */ );
459
460 // If there is a validation error on the current page, don't allow the page change.
461 if( !page->Validate() || !page->TransferDataFromWindow() )
462 {
463 aEvent.Veto();
464 return;
465 }
466}
467
468
469void PAGED_DIALOG::onResetButton( wxCommandEvent& aEvent )
470{
471 int sel = m_treebook->GetSelection();
472
473 if( sel == wxNOT_FOUND )
474 return;
475
476 // NB: dynamic_cast doesn't work over Kiway
477 wxWindow* panel = m_treebook->ResolvePage( sel );
478
479 if( panel )
480 {
481 wxCommandEvent resetCommand( wxEVT_COMMAND_BUTTON_CLICKED, ID_RESET_PANEL );
482 panel->ProcessWindowEvent( resetCommand );
483 }
484}
485
486void PAGED_DIALOG::onOpenPreferencesButton( wxCommandEvent& aEvent )
487{
488 wxString dir( PATHS::GetUserSettingsPath() );
489 LaunchExternal( dir );
490}
const char * name
Definition: DXF_plotter.cpp:59
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:88
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
Definition: dialog_shim.h:230
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:72
bool TransferDataToWindow() override
~PAGED_DIALOG() override
PAGED_DIALOG(wxWindow *aParent, const wxString &aTitle, bool aShowReset, bool aShowOpenFolder, const wxString &aAuxiliaryAction=wxEmptyString, const wxSize &aInitialSize=wxDefaultSize)
wxButton * m_auxiliaryButton
Definition: paged_dialog.h:69
std::vector< bool > m_macHack
Definition: paged_dialog.h:79
wxButton * m_openPrefsDirButton
Definition: paged_dialog.h:71
bool TransferDataFromWindow() override
void finishInitialization()
void UpdateResetButton(int aPage)
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:61
wxString m_title
Definition: paged_dialog.h:75
virtual void onOpenPreferencesButton(wxCommandEvent &aEvent)
static PAGED_DIALOG * GetDialog(wxWindow *aWindow)
virtual void onResetButton(wxCommandEvent &aEvent)
WX_TREEBOOK * m_treebook
Definition: paged_dialog.h:68
virtual void onPageChanged(wxBookCtrlEvent &aEvent)
wxBoxSizer * m_buttonsSizer
Definition: paged_dialog.h:77
void SetError(const wxString &aMessage, const wxString &aPageName, int aCtrlId, int aRow=-1, int aCol=-1)
wxButton * m_resetButton
Definition: paged_dialog.h:70
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: paths.cpp:582
A modified version of the wxInfoBar class that allows us to:
Definition: wx_infobar.h:76
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: wx_infobar.cpp:139
void SetBorders(bool aLeft, bool aRight, bool aTop, bool aBottom)
Definition: wx_panel.h:39
wxWindow * ResolvePage(size_t aPage)
This file is part of the common library.
const int minSize
Push and Shove router track width and via size dialog.
#define _(s)
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
Definition: launch_ext.cpp:25
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
Definition: ui_common.cpp:161
std::map< wxString, wxString > g_lastPage
std::map< wxString, wxString > g_lastParentPage
#define wxRESETTABLE
#define ID_RESET_PANEL
Functions to provide common constants and other functions to assist in making a consistent UI.