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 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
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:65
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 * 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)
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.