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
28
29class LAZY_PAGE : public wxPanel
30{
31public:
32 LAZY_PAGE( wxWindow* aParent, std::function<wxWindow*( wxWindow* aParent )> aLazyCtor ) :
33 wxPanel( aParent, wxID_ANY ),
34 m_lazyCtor( std::move( aLazyCtor ) ),
35 m_mainSizer( nullptr ),
36 m_contents( nullptr )
37 {
38 m_mainSizer = new wxBoxSizer( wxVERTICAL );
39 SetSizer( m_mainSizer );
40 }
41
42 wxWindow* Resolve()
43 {
44 if( !m_contents )
45 {
46 m_contents = m_lazyCtor( this );
47 m_mainSizer->Add( m_contents, 1, wxEXPAND, 5 );
48 m_mainSizer->Layout();
49
50 m_contents->TransferDataToWindow();
51 }
52
53 return m_contents;
54 }
55
56 bool Show( bool show ) override
57 {
58 if( show )
59 Resolve();
60
61 // m_contents has been created as a child window of LAZY_PAGE, and has been added to
62 // LAZY_PAGE's m_mainSizer. So wxPanel::Show() should call m_contents' Show() method,
63 // whether overridden or not. Only it doesn't, so we call it directly here.
64 if( show && m_contents )
65 m_contents->Show( true );
66
67 return wxPanel::Show( show );
68 }
69
70private:
71 std::function<wxWindow*( wxWindow* aParent )> m_lazyCtor;
72
73 wxSizer* m_mainSizer;
74 wxWindow* m_contents;
75};
76
77
78WX_TREEBOOK::WX_TREEBOOK( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
79 long style, const wxString& name ) :
80 wxTreebook( parent, id, pos, size, style, name )
81{
82}
83
84
85bool WX_TREEBOOK::AddLazyPage( std::function<wxWindow*( wxWindow* aParent )> aLazyCtor,
86 const wxString& text, bool bSelect, int imageId )
87{
88 return AddPage( new LAZY_PAGE( this, std::move( aLazyCtor ) ), text, bSelect, imageId );
89}
90
91
92bool WX_TREEBOOK::AddLazySubPage( std::function<wxWindow*( wxWindow* aParent )> aLazyCtor,
93 const wxString& text, bool bSelect, int imageId )
94{
95 return AddSubPage( new LAZY_PAGE( this, std::move( aLazyCtor ) ), text, bSelect, imageId );
96}
97
98
99wxWindow* WX_TREEBOOK::ResolvePage( size_t aPage )
100{
101 wxWindow* page = GetPage( aPage );
102
103 if( LAZY_PAGE* lazyPage = dynamic_cast<LAZY_PAGE*>( page ) )
104 return lazyPage->Resolve();
105
106 return page;
107}
const char * name
Definition: DXF_plotter.cpp:57
LAZY_PAGE(wxWindow *aParent, std::function< wxWindow *(wxWindow *aParent)> aLazyCtor)
Definition: wx_treebook.cpp:32
bool Show(bool show) override
Definition: wx_treebook.cpp:56
wxSizer * m_mainSizer
Definition: wx_treebook.cpp:73
wxWindow * m_contents
Definition: wx_treebook.cpp:74
wxWindow * Resolve()
Definition: wx_treebook.cpp:42
std::function< wxWindow *(wxWindow *aParent)> m_lazyCtor
Definition: wx_treebook.cpp:71
wxWindow * ResolvePage(size_t aPage)
Definition: wx_treebook.cpp:99
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:78
bool AddLazyPage(std::function< wxWindow *(wxWindow *aParent)> aLazyCtor, const wxString &text, bool bSelect=false, int imageId=NO_IMAGE)
Definition: wx_treebook.cpp:85
bool AddLazySubPage(std::function< wxWindow *(wxWindow *aParent)> aLazyCtor, const wxString &text, bool bSelect=false, int imageId=NO_IMAGE)
Definition: wx_treebook.cpp:92
STL namespace.