From 83edc0a574d4311c536c9e263f512b494130fef3 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Sat, 11 Dec 1999 15:07:14 +0000 Subject: [PATCH] Added wxNotebookSizer git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4902 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/sizer.h | 21 +++++++++++ samples/layout/layout.cpp | 73 +++++++++++++++++++++++++++++++++------ samples/layout/layout.h | 40 +++++++++++---------- src/common/sizer.cpp | 69 ++++++++++++++++++++++++++++++++++-- 4 files changed, 172 insertions(+), 31 deletions(-) diff --git a/include/wx/sizer.h b/include/wx/sizer.h index c8e4503f51..7a2ce17ff3 100644 --- a/include/wx/sizer.h +++ b/include/wx/sizer.h @@ -27,6 +27,7 @@ //--------------------------------------------------------------------------- class wxStaticBox; +class wxNotebook; class wxSizerItem; class wxSizer; @@ -193,5 +194,25 @@ protected: wxStaticBox *m_staticBox; }; +//--------------------------------------------------------------------------- +// wxNotebookSizer +//--------------------------------------------------------------------------- + +class WXDLLEXPORT wxNotebookSizer: public wxSizer +{ + DECLARE_CLASS(wxNotebookSizer); +public: + wxNotebookSizer( wxNotebook *nb ); + + void RecalcSizes(); + wxSize CalcMin(); + + wxNotebook *GetNotebook() + { return m_notebook; } + +protected: + wxNotebook *m_notebook; +}; + #endif // __WXSIZER_H__ diff --git a/samples/layout/layout.cpp b/samples/layout/layout.cpp index 9754aeb872..312c164692 100644 --- a/samples/layout/layout.cpp +++ b/samples/layout/layout.cpp @@ -25,8 +25,10 @@ #endif #include + #include "wx/sizer.h" #include "wx/statline.h" +#include "wx/notebook.h" #include "layout.h" @@ -40,7 +42,7 @@ MyApp::MyApp() { } -bool MyApp::OnInit(void) +bool MyApp::OnInit() { // Create the main frame window frame = new MyFrame((MyFrame *) NULL, (char *) "wxWindows Layout Demo", 0, 0, 550, 500); @@ -54,7 +56,8 @@ bool MyApp::OnInit(void) wxMenu *file_menu = new wxMenu; file_menu->Append(LAYOUT_LOAD_FILE, "&Load file", "Load a text file"); - file_menu->Append(LAYOUT_TEST_NEW, "&Test new sizers", "Test new sizer code"); + file_menu->Append(LAYOUT_TEST_SIZER, "&Test sizers", "Test sizer"); + file_menu->Append(LAYOUT_TEST_NB, "&Test notebook sizers", "Test notebook sizer"); file_menu->AppendSeparator(); file_menu->Append(LAYOUT_QUIT, "E&xit", "Quit program"); @@ -171,7 +174,8 @@ MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h): BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(LAYOUT_LOAD_FILE, MyFrame::LoadFile) EVT_MENU(LAYOUT_QUIT, MyFrame::Quit) - EVT_MENU(LAYOUT_TEST_NEW, MyFrame::TestNewSizers) + EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestSizers) + EVT_MENU(LAYOUT_TEST_NB, MyFrame::TestNotebookSizers) EVT_MENU(LAYOUT_ABOUT, MyFrame::About) EVT_SIZE(MyFrame::OnSize) END_EVENT_TABLE() @@ -190,18 +194,65 @@ void MyFrame::LoadFile(wxCommandEvent& WXUNUSED(event) ) void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) ) { - this->Close(TRUE); + this->Close(TRUE); } -void MyFrame::TestNewSizers(wxCommandEvent& WXUNUSED(event) ) +void MyFrame::TestSizers(wxCommandEvent& WXUNUSED(event) ) { - NewSizerFrame *newFrame = new NewSizerFrame((MyFrame *) NULL, "Sizer Test Frame", 50, 50 ); - newFrame->Show(TRUE); + MySizerFrame *newFrame = new MySizerFrame((MyFrame *) NULL, "Sizer Test Frame", 50, 50 ); + newFrame->Show(TRUE); } +void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) ) +{ + wxDialog dialog( this, -1, "Notebook Sizer Test Dialog" ); + + // Begin with first hierarchy: a notebook at the top and + // and OK button at the bottom. + + wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); + + wxNotebook *notebook = new wxNotebook( &dialog, -1 ); + wxNotebookSizer *nbs = new wxNotebookSizer( notebook ); + topsizer->Add( nbs, 1, wxGROW ); + + wxButton *button = new wxButton( &dialog, wxID_OK, "OK" ); + topsizer->Add( button, 0, wxALIGN_RIGHT | wxALL, 10 ); + + // First page: one big text ctrl + wxTextCtrl *multi = new wxTextCtrl( notebook, -1, "TextCtrl.", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ); + notebook->AddPage( multi, "Page One" ); + + // Second page: a text ctrl and a button + wxPanel *panel = new wxPanel( notebook, -1 ); + notebook->AddPage( panel, "Page Two" ); + + wxBoxSizer *panelsizer = new wxBoxSizer( wxVERTICAL ); + + wxTextCtrl *text = new wxTextCtrl( panel, -1, "TextLine 1.", wxDefaultPosition, wxSize(250,-1) ); + panelsizer->Add( text, 0, wxGROW|wxALL, 30 ); + text = new wxTextCtrl( panel, -1, "TextLine 2.", wxDefaultPosition, wxSize(250,-1) ); + panelsizer->Add( text, 0, wxGROW|wxALL, 30 ); + wxButton *button2 = new wxButton( panel, -1, "Hallo" ); + panelsizer->Add( button2, 0, wxALIGN_RIGHT | wxLEFT|wxRIGHT|wxBOTTOM, 30 ); + + panel->SetAutoLayout( TRUE ); + panel->SetSizer( panelsizer ); + + // Tell dialog to use sizer + + dialog.SetAutoLayout( TRUE ); + topsizer->Fit( &dialog ); + topsizer->SetSizeHints( &dialog ); + dialog.SetSizer( topsizer ); + + dialog.ShowModal(); +} + + void MyFrame::About(wxCommandEvent& WXUNUSED(event) ) { - (void)wxMessageBox("wxWindows GUI library layout demo\n", + (void)wxMessageBox("wxWindows GUI library layout demo\n", "About Layout Demo", wxOK|wxCENTRE); } @@ -245,7 +296,7 @@ MyWindow::MyWindow(wxFrame *frame, int x, int y, int w, int h, long style): { } -MyWindow::~MyWindow(void) +MyWindow::~MyWindow() { } @@ -257,10 +308,10 @@ void MyWindow::OnPaint(wxPaintEvent& WXUNUSED(event) ) } //----------------------------------------------------------------- -// NewSizerFrame +// MySizerFrame //----------------------------------------------------------------- -NewSizerFrame::NewSizerFrame(wxFrame *frame, char *title, int x, int y ): +MySizerFrame::MySizerFrame(wxFrame *frame, char *title, int x, int y ): wxFrame(frame, -1, title, wxPoint(x, y) ) { // we want to get a dialog that is stretchable because it diff --git a/samples/layout/layout.h b/samples/layout/layout.h index 7aad45fd82..dae135e135 100644 --- a/samples/layout/layout.h +++ b/samples/layout/layout.h @@ -12,9 +12,9 @@ // Define a new application class MyApp: public wxApp { - public: - MyApp(void) ; - bool OnInit(void); +public: + MyApp(); + bool OnInit(); }; // Define a new frame @@ -23,7 +23,7 @@ class MyWindow; class MyFrame: public wxFrame { - public: +public: wxPanel *panel; MyTextWindow *text_window; MyWindow *canvas; @@ -33,44 +33,48 @@ class MyFrame: public wxFrame void LoadFile(wxCommandEvent& event); void Quit(wxCommandEvent& event); - void TestNewSizers(wxCommandEvent& event); + void TestSizers(wxCommandEvent& event); + void TestNotebookSizers(wxCommandEvent& event); void About(wxCommandEvent& event); - DECLARE_EVENT_TABLE() +private: + DECLARE_EVENT_TABLE() }; // Define a new text subwindow that can respond to drag-and-drop class MyTextWindow: public wxTextCtrl { - public: - MyTextWindow(wxFrame *frame, int x=-1, int y=-1, int width=-1, int height=-1, +public: + MyTextWindow(wxFrame *frame, int x=-1, int y=-1, int width=-1, int height=-1, long style=wxTE_MULTILINE): wxTextCtrl(frame, -1, "", wxPoint(x, y), wxSize(width, height), style) - { - } + { + } + }; // Define a new canvas which can receive some events class MyWindow: public wxWindow { - public: +public: MyWindow(wxFrame *frame, int x, int y, int w, int h, long style = wxRETAINED); - ~MyWindow(void) ; + ~MyWindow(); void OnPaint(wxPaintEvent& event); - + +private: DECLARE_EVENT_TABLE() }; -class NewSizerFrame: public wxFrame +class MySizerFrame: public wxFrame { - public: +public: wxPanel *panel; - NewSizerFrame(wxFrame *frame, char *title, int x, int y ); - + MySizerFrame(wxFrame *frame, char *title, int x, int y ); }; #define LAYOUT_QUIT 100 #define LAYOUT_TEST 101 #define LAYOUT_ABOUT 102 #define LAYOUT_LOAD_FILE 103 -#define LAYOUT_TEST_NEW 104 +#define LAYOUT_TEST_SIZER 104 +#define LAYOUT_TEST_NB 105 diff --git a/src/common/sizer.cpp b/src/common/sizer.cpp index ea20ee7168..96415ce65f 100644 --- a/src/common/sizer.cpp +++ b/src/common/sizer.cpp @@ -23,6 +23,7 @@ #include "wx/sizer.h" #include "wx/utils.h" #include "wx/statbox.h" +#include "wx/notebook.h" //--------------------------------------------------------------------------- @@ -30,6 +31,7 @@ IMPLEMENT_ABSTRACT_CLASS(wxSizerItem, wxObject); IMPLEMENT_ABSTRACT_CLASS(wxSizer, wxObject); IMPLEMENT_ABSTRACT_CLASS(wxBoxSizer, wxSizer); IMPLEMENT_ABSTRACT_CLASS(wxStaticBoxSizer, wxBoxSizer); +IMPLEMENT_ABSTRACT_CLASS(wxNotebookSizer, wxSizer); //--------------------------------------------------------------------------- // wxSizerItem @@ -542,9 +544,10 @@ void wxStaticBoxSizer::RecalcSizes() wxSize wxStaticBoxSizer::CalcMin() { - // this will have to be done platform by platform + // This will have to be done platform by platform // as there is no way to guess the thickness of - // a wxStaticBox border + // a wxStaticBox border. + int top_border = 15; if (m_staticBox->GetLabel().IsEmpty()) top_border = 5; int other_border = 5; @@ -555,3 +558,65 @@ wxSize wxStaticBoxSizer::CalcMin() return ret; } + +//--------------------------------------------------------------------------- +// wxNotebookSizer +//--------------------------------------------------------------------------- + +wxNotebookSizer::wxNotebookSizer( wxNotebook *nb ) +{ + wxASSERT_MSG( nb, wxT("wxNotebookSizer needs a notebook") ); + + m_notebook = nb; +} + +void wxNotebookSizer::RecalcSizes() +{ + m_notebook->SetSize( m_position.x, m_position.y, m_size.x, m_size.y ); +} + +wxSize wxNotebookSizer::CalcMin() +{ + // This will have to be done platform by platform + // as there is no way to guess the thickness of + // the wxNotebook tabs and border. + + int borderX = 5; + int borderY = 5; + if ((m_notebook->HasFlag(wxNB_RIGHT)) || + (m_notebook->HasFlag(wxNB_LEFT))) + { + borderX += 70; // improvements later.. + } + else + { + borderY += 35; // improvements later.. + } + + if (m_notebook->GetChildren().GetCount() == 0) + return wxSize(borderX + 10, borderY + 10); + + int maxX = 0; + int maxY = 0; + + wxWindowList::Node *node = m_notebook->GetChildren().GetFirst(); + while (node) + { + wxWindow *item = node->GetData(); + wxSizer *itemsizer = item->GetSizer(); + + if (itemsizer) + { + wxSize subsize( itemsizer->CalcMin() ); + + if (subsize.x > maxX) maxX = subsize.x; + if (subsize.y > maxY) maxY = subsize.y; + } + + node = node->GetNext(); + } + + return wxSize( borderX + maxX, borderY + maxY ); +} + +