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 if( !aAuxiliaryAction.IsEmpty() )
101 {
102 m_auxiliaryButton = new wxButton( this, wxID_ANY, aAuxiliaryAction );
103 m_buttonsSizer->Add( m_auxiliaryButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT, 5 );
104 }
105
106 m_buttonsSizer->AddStretchSpacer();
107
108 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
109 wxButton* sdbSizerOK = new wxButton( this, wxID_OK );
110 sdbSizer->AddButton( sdbSizerOK );
111 wxButton* sdbSizerCancel = new wxButton( this, wxID_CANCEL );
112 sdbSizer->AddButton( sdbSizerCancel );
113 sdbSizer->Realize();
114
115 m_buttonsSizer->Add( sdbSizer, 1, 0, 5 );
116 mainSizer->Add( m_buttonsSizer, 0, wxALL|wxEXPAND, 5 );
117
119
120 // We normally save the dialog size and position based on its class-name. This class
121 // substitutes the title so that each distinctly-titled dialog can have its own saved
122 // size and position.
123 m_hash_key = aTitle;
124
126 m_auxiliaryButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onAuxiliaryAction, this );
127
128 if( m_resetButton )
129 m_resetButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onResetButton, this );
130
132 m_openPrefsDirButton->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onOpenPrefsDir, this );
133
134 m_treebook->Bind( wxEVT_CHAR_HOOK, &PAGED_DIALOG::onCharHook, this );
135 m_treebook->Bind( wxEVT_TREEBOOK_PAGE_CHANGED, &PAGED_DIALOG::onPageChanged, this );
136 m_treebook->Bind( wxEVT_TREEBOOK_PAGE_CHANGING, &PAGED_DIALOG::onPageChanging, this );
137}
138
139
141{
142 for( size_t i = 1; i < m_treebook->GetPageCount(); ++i )
143 m_macHack.push_back( true );
144
145 // For some reason adding page labels to the treeCtrl doesn't invalidate its bestSize
146 // cache so we have to do it by hand
147 m_treebook->GetTreeCtrl()->InvalidateBestSize();
148
149 for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
150 m_treebook->GetPage( i )->Layout();
151
152 m_treebook->Layout();
153 m_treebook->Fit();
154
155 // Add a bit of width to the treeCtrl for scrollbars
156 wxSize ctrlSize = m_treebook->GetTreeCtrl()->GetSize();
157 ctrlSize.x += 20;
158 m_treebook->GetTreeCtrl()->SetMinSize( ctrlSize );
159 m_treebook->Layout();
160
161 // Note: do NOT call finishDialogSettings() here. At this point the lazy pages have not
162 // yet been resolved, so the sizer minimum is computed from empty placeholder pages.
163 // On GTK+X11, finishDialogSettings() stores a "resize to sizer minimum" operation that
164 // is deferred until the window is realized by the window manager. If that deferred
165 // operation fires after onPageChanged() has already grown the dialog to fit the real
166 // page content, it shrinks the dialog back to the placeholder minimum, cutting off the
167 // bottom of the page. The dialog minimum and size are correctly established by
168 // onPageChanged() once the selected page is resolved.
169}
170
171
172void PAGED_DIALOG::SetInitialPage( const wxString& aPage, const wxString& aParentPage )
173{
174 g_lastPage[ m_title ] = aPage;
175 g_lastParentPage[ m_title ] = aParentPage;
176}
177
178
180{
181 // Store the current parentPageTitle/pageTitle hierarchy so we can re-select it
182 // next time.
183 wxString lastPage = wxEmptyString;
184 wxString lastParentPage = wxEmptyString;
185
186 int selected = m_treebook->GetSelection();
187
188 if( selected != wxNOT_FOUND )
189 {
190 lastPage = m_treebook->GetPageText( (unsigned) selected );
191
192 int parent = m_treebook->GetPageParent( (unsigned) selected );
193
194 if( parent != wxNOT_FOUND )
195 lastParentPage = m_treebook->GetPageText( (unsigned) parent );
196 }
197
198 g_lastPage[ m_title ] = lastPage;
199 g_lastParentPage[ m_title ] = lastParentPage;
200
202 m_auxiliaryButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onAuxiliaryAction, this );
203
204 if( m_resetButton )
205 m_resetButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onResetButton, this );
206
208 m_openPrefsDirButton->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &PAGED_DIALOG::onOpenPrefsDir, this );
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 wxString name = m_treebook->GetPageText( aPage );
338 name.Replace( wxT( "&" ), wxT( "&&" ) );
339
340 m_resetButton->SetLabel( wxString::Format( _( "Reset %s to Defaults" ), name ) );
341 m_resetButton->SetToolTip( panel->GetHelpTextAtPoint( wxPoint( -INT_MAX, INT_MAX ),
342 wxHelpEvent::Origin_Unknown ) );
343 m_resetButton->Enable( true );
344 }
345 else
346 {
347 m_resetButton->SetLabel( _( "Reset to Defaults" ) );
348 m_resetButton->SetToolTip( wxString() );
349 m_resetButton->Enable( false );
350 }
351
352 m_resetButton->GetParent()->Layout();
353 }
354}
355
356
357void PAGED_DIALOG::onCharHook( wxKeyEvent& aEvent )
358{
359 if( dynamic_cast<wxTextEntry*>( aEvent.GetEventObject() )
360 || dynamic_cast<wxStyledTextCtrl*>( aEvent.GetEventObject() )
361 || dynamic_cast<wxListView*>( aEvent.GetEventObject() )
362 || dynamic_cast<wxGrid*>( FindFocus() ) )
363 {
364 aEvent.Skip();
365 return;
366 }
367
368 if( aEvent.GetKeyCode() == WXK_UP )
369 {
370 int page = m_treebook->GetSelection();
371
372 if( page >= 1 )
373 {
374 if( m_treebook->GetPage( page - 1 )->GetChildren().IsEmpty() )
375 m_treebook->SetSelection( std::max( page - 2, 0 ) );
376 else
377 m_treebook->SetSelection( page - 1 );
378 }
379
380 m_treebook->GetTreeCtrl()->SetFocus(); // Don't allow preview canvas to steal gridFocus
381 }
382 else if( aEvent.GetKeyCode() == WXK_DOWN )
383 {
384 int page = m_treebook->GetSelection();
385
386 m_treebook->SetSelection( std::min<int>( page + 1, m_treebook->GetPageCount() - 1 ) );
387
388 m_treebook->GetTreeCtrl()->SetFocus(); // Don't allow preview canvas to steal gridFocus
389 }
390 else
391 {
392 aEvent.Skip();
393 }
394}
395
396
397void PAGED_DIALOG::onPageChanged( wxBookCtrlEvent& event )
398{
399 size_t page = event.GetSelection();
400
401 // Use the first sub-page when a tree level node is selected.
402 if( m_treebook->GetCurrentPage()->GetChildren().IsEmpty()
403 && page + 1 < m_treebook->GetPageCount() )
404 {
405 m_treebook->ChangeSelection( ++page );
406 }
407
408 UpdateResetButton( page );
409
410 // Make sure the dialog size is enough to fit the page content.
411 m_treebook->InvalidateBestSize();
412
413 SetMinSize( wxDefaultSize );
414 wxSize minSize = GetBestSize();
415 minSize.IncTo( FromDIP( wxSize( 600, 500 ) ) );
416 minSize.DecTo( FromDIP( wxSize( 1500, 900 ) ) ); // Failsafe
417 SetMinSize( minSize );
418
419 wxSize currentSize = GetSize();
420 wxSize newSize = currentSize;
421 newSize.IncTo( minSize );
422
423 if( newSize != currentSize )
424 SetSize( newSize );
425
426#ifdef __WXMAC__
427 // Work around an OSX wxWidgets issue where the wxGrid children don't get placed correctly
428 // until the first resize event
429 if( page < m_macHack.size() && m_macHack[ page ] )
430 {
431 wxSize pageSize = m_treebook->GetPage( page )->GetSize();
432 pageSize.x += 1;
433 pageSize.y += 2;
434
435 m_treebook->GetPage( page )->SetSize( pageSize );
436 m_macHack[ page ] = false;
437 }
438#else
439 wxSizeEvent evt( wxDefaultSize );
440 wxQueueEvent( m_treebook, evt.Clone() );
441#endif
442}
443
444
445void PAGED_DIALOG::onPageChanging( wxBookCtrlEvent& aEvent )
446{
447 int currentPage = aEvent.GetOldSelection();
448
449 if( currentPage == wxNOT_FOUND )
450 return;
451
452 wxWindow* page = m_treebook->GetPage( currentPage );
453
454 wxCHECK( page, /* void */ );
455
456 // If there is a validation error on the current page, don't allow the page change.
457 if( !page->Validate() || !page->TransferDataFromWindow() )
458 {
459 aEvent.Veto();
460 return;
461 }
462}
463
464
465void PAGED_DIALOG::onResetButton( wxCommandEvent& aEvent )
466{
467 int sel = m_treebook->GetSelection();
468
469 if( sel == wxNOT_FOUND )
470 return;
471
472 // NB: dynamic_cast doesn't work over Kiway
473 wxWindow* panel = m_treebook->ResolvePage( sel );
474
475 if( panel )
476 {
477 wxCommandEvent resetCommand( wxEVT_COMMAND_BUTTON_CLICKED, ID_RESET_PANEL );
478 panel->ProcessWindowEvent( resetCommand );
479 }
480}
481
482void PAGED_DIALOG::onOpenPrefsDir( wxCommandEvent& aEvent )
483{
484 wxString dir( PATHS::GetUserSettingsPath() );
485 LaunchExternal( dir );
486}
const char * name
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
DIALOG_SHIM(wxWindow *aParent, wxWindowID id, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER, const wxString &name=wxDialogNameStr)
virtual void onOpenPrefsDir(wxCommandEvent &aEvent)
WX_INFOBAR * m_infoBar
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
std::vector< bool > m_macHack
wxButton * m_openPrefsDirButton
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)
wxString m_title
static PAGED_DIALOG * GetDialog(wxWindow *aWindow)
virtual void onResetButton(wxCommandEvent &aEvent)
WX_TREEBOOK * m_treebook
virtual void onPageChanged(wxBookCtrlEvent &aEvent)
wxBoxSizer * m_buttonsSizer
void SetError(const wxString &aMessage, const wxString &aPageName, int aCtrlId, int aRow=-1, int aCol=-1)
wxButton * m_resetButton
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
Definition paths.cpp:631
A modified version of the wxInfoBar class that allows us to:
Definition wx_infobar.h:77
void SetBorders(bool aLeft, bool aRight, bool aTop, bool aBottom)
Definition wx_panel.h:39
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.
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
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.