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