KiCad PCB EDA Suite
Loading...
Searching...
No Matches
wx_treebook.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) 2023 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <widgets/wx_treebook.h>
25#include <wx/panel.h>
26#include <wx/sizer.h>
27#include <dialog_shim.h>
28
29
30class LAZY_PAGE : public wxPanel
31{
32public:
33 LAZY_PAGE( wxWindow* aParent, std::function<wxWindow*( wxWindow* aParent )> aLazyCtor ) :
34 wxPanel( aParent, wxID_ANY ),
35 m_lazyCtor( std::move( aLazyCtor ) ),
36 m_mainSizer( nullptr ),
37 m_contents( nullptr )
38 {
39 m_mainSizer = new wxBoxSizer( wxVERTICAL );
40 SetSizer( m_mainSizer );
41 }
42
43 wxWindow* Resolve()
44 {
45 if( !m_contents )
46 {
47 m_contents = m_lazyCtor( this );
48 m_mainSizer->Add( m_contents, 1, wxEXPAND, 5 );
49 m_mainSizer->Layout();
50
51 m_contents->TransferDataToWindow();
52
53 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
54 dlg->SelectAllInTextCtrls( GetChildren() );
55 }
56
57 return m_contents;
58 }
59
60 bool Show( bool show ) override
61 {
62 if( show )
63 Resolve();
64
65 // m_contents has been created as a child window of LAZY_PAGE, and has been added to
66 // LAZY_PAGE's m_mainSizer. So wxPanel::Show() should call m_contents' Show() method,
67 // whether overridden or not. Only it doesn't, so we call it directly here.
68 if( show && m_contents )
69 m_contents->Show( true );
70
71 return wxPanel::Show( show );
72 }
73
74private:
75 std::function<wxWindow*( wxWindow* aParent )> m_lazyCtor;
76
77 wxSizer* m_mainSizer;
78 wxWindow* m_contents;
79};
80
81
82WX_TREEBOOK::WX_TREEBOOK( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
83 long style, const wxString& name ) :
84 wxTreebook( parent, id, pos, size, style, name )
85{
86}
87
88
89bool WX_TREEBOOK::AddLazyPage( std::function<wxWindow*( wxWindow* aParent )> aLazyCtor,
90 const wxString& text, bool bSelect, int imageId )
91{
92 return AddPage( new LAZY_PAGE( this, std::move( aLazyCtor ) ), text, bSelect, imageId );
93}
94
95
96bool WX_TREEBOOK::AddLazySubPage( std::function<wxWindow*( wxWindow* aParent )> aLazyCtor,
97 const wxString& text, bool bSelect, int imageId )
98{
99 return AddSubPage( new LAZY_PAGE( this, std::move( aLazyCtor ) ), text, bSelect, imageId );
100}
101
102
103wxWindow* WX_TREEBOOK::ResolvePage( size_t aPage )
104{
105 wxWindow* page = GetPage( aPage );
106
107 if( LAZY_PAGE* lazyPage = dynamic_cast<LAZY_PAGE*>( page ) )
108 return lazyPage->Resolve();
109
110 return page;
111}
const char * name
Definition: DXF_plotter.cpp:57
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:88
LAZY_PAGE(wxWindow *aParent, std::function< wxWindow *(wxWindow *aParent)> aLazyCtor)
Definition: wx_treebook.cpp:33
bool Show(bool show) override
Definition: wx_treebook.cpp:60
wxSizer * m_mainSizer
Definition: wx_treebook.cpp:77
wxWindow * m_contents
Definition: wx_treebook.cpp:78
wxWindow * Resolve()
Definition: wx_treebook.cpp:43
std::function< wxWindow *(wxWindow *aParent)> m_lazyCtor
Definition: wx_treebook.cpp:75
wxWindow * ResolvePage(size_t aPage)
WX_TREEBOOK(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxBK_DEFAULT, const wxString &name=wxEmptyString)
Definition: wx_treebook.cpp:82
bool AddLazyPage(std::function< wxWindow *(wxWindow *aParent)> aLazyCtor, const wxString &text, bool bSelect=false, int imageId=NO_IMAGE)
Definition: wx_treebook.cpp:89
bool AddLazySubPage(std::function< wxWindow *(wxWindow *aParent)> aLazyCtor, const wxString &text, bool bSelect=false, int imageId=NO_IMAGE)
Definition: wx_treebook.cpp:96
STL namespace.