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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <widgets/wx_treebook.h>
21#include <wx/panel.h>
22#include <wx/sizer.h>
23#include <dialog_shim.h>
24
25
26class LAZY_PAGE : public wxPanel
27{
28public:
29 LAZY_PAGE( wxWindow* aParent, std::function<wxWindow*( wxWindow* aParent )> aLazyCtor ) :
30 wxPanel( aParent, wxID_ANY ),
31 m_lazyCtor( std::move( aLazyCtor ) ),
32 m_mainSizer( nullptr ),
33 m_contents( nullptr )
34 {
35 m_mainSizer = new wxBoxSizer( wxVERTICAL );
36 SetSizer( m_mainSizer );
37 }
38
39 wxWindow* Resolve()
40 {
41 if( !m_contents )
42 {
43 m_contents = m_lazyCtor( this );
44 m_mainSizer->Add( m_contents, 1, wxEXPAND, 5 );
45 m_mainSizer->Layout();
46
47 m_contents->TransferDataToWindow();
48
49 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
50 dlg->SelectAllInTextCtrls( GetChildren() );
51 }
52
53 return m_contents;
54 }
55
56 wxWindow* Contents() const { return m_contents; }
57
58 bool Show( bool show ) override
59 {
60 if( show )
61 Resolve();
62
63 // m_contents has been created as a child window of LAZY_PAGE, and has been added to
64 // LAZY_PAGE's m_mainSizer. So wxPanel::Show() should call m_contents' Show() method,
65 // whether overridden or not. Only it doesn't, so we call it directly here.
66 if( show && m_contents )
67 m_contents->Show( true );
68
69 return wxPanel::Show( show );
70 }
71
72private:
73 std::function<wxWindow*( wxWindow* aParent )> m_lazyCtor;
74
75 wxSizer* m_mainSizer;
76 wxWindow* m_contents;
77};
78
79
80WX_TREEBOOK::WX_TREEBOOK( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
81 long style, const wxString& name ) :
82 wxTreebook( parent, id, pos, size, style, name )
83{
84}
85
86
87bool WX_TREEBOOK::AddLazyPage( std::function<wxWindow*( wxWindow* aParent )> aLazyCtor,
88 const wxString& text, bool bSelect, int imageId )
89{
90 return AddPage( new LAZY_PAGE( this, std::move( aLazyCtor ) ), text, bSelect, imageId );
91}
92
93
94bool WX_TREEBOOK::AddLazySubPage( std::function<wxWindow*( wxWindow* aParent )> aLazyCtor,
95 const wxString& text, bool bSelect, int imageId )
96{
97 return AddSubPage( new LAZY_PAGE( this, std::move( aLazyCtor ) ), text, bSelect, imageId );
98}
99
100
101wxWindow* WX_TREEBOOK::ResolvePage( size_t aPage )
102{
103 wxWindow* page = GetPage( aPage );
104
105 if( LAZY_PAGE* lazyPage = dynamic_cast<LAZY_PAGE*>( page ) )
106 return lazyPage->Resolve();
107
108 return page;
109}
110
111
112wxWindow* WX_TREEBOOK::GetResolvedPage( size_t aPage )
113{
114 wxWindow* page = GetPage( aPage );
115
116 if( LAZY_PAGE* lazyPage = dynamic_cast<LAZY_PAGE*>( page ) )
117 return lazyPage->Contents();
118
119 return page;
120}
const char * name
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:80
LAZY_PAGE(wxWindow *aParent, std::function< wxWindow *(wxWindow *aParent)> aLazyCtor)
bool Show(bool show) override
wxSizer * m_mainSizer
wxWindow * m_contents
wxWindow * Resolve()
std::function< wxWindow *(wxWindow *aParent)> m_lazyCtor
wxWindow * Contents() const
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)
wxWindow * GetResolvedPage(size_t aPage)
bool AddLazyPage(std::function< wxWindow *(wxWindow *aParent)> aLazyCtor, const wxString &text, bool bSelect=false, int imageId=NO_IMAGE)
bool AddLazySubPage(std::function< wxWindow *(wxWindow *aParent)> aLazyCtor, const wxString &text, bool bSelect=false, int imageId=NO_IMAGE)
STL namespace.