]>
git.saurik.com Git - wxWidgets.git/blob - samples/notebook/notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxNotebook demo
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 // If 1, use a dialog. Otherwise use a frame.
24 #define USE_TABBED_DIALOG 1
29 MyDialog
* dialog
= (MyDialog
*) NULL
;
30 #else // !USE_TABBED_DIALOG
31 MyFrame
* frame
= (MyFrame
*) NULL
;
32 #endif // USE_TABBED_DIALOG
38 // Create the main window
40 dialog
= new MyDialog((wxFrame
*) NULL
, -1, "Notebook", wxPoint(-1, -1), wxSize(365, 390),
45 // Quit immediately the dialog has been dismissed
48 frame
= new MyFrame((wxFrame
*) NULL
, -1, "Notebook", wxPoint(-1, -1), wxSize(465, 390) );
50 // Problem with generic wxNotebook implementation whereby it doesn't size properly unless
51 // you set the size again
52 #if defined(__WIN16__) || defined(__WXMOTIF__)
54 frame
->GetSize(& width
, & height
);
55 frame
->SetSize(-1, -1, width
, height
);
62 void MyApp::InitTabView(wxNotebook
* notebook
, wxWindow
* window
)
64 m_okButton
= new wxButton(window
, wxID_OK
, "Close", wxPoint(-1, -1), wxSize(80, 25));
65 m_cancelButton
= new wxButton(window
, ID_DELETE_PAGE
, "&Delete page", wxPoint(-1, -1), wxSize(80, 25));
66 m_addPageButton
= new wxButton(window
, ID_ADD_PAGE
, "&Add page", wxPoint(-1, -1), wxSize(80, 25));
67 m_insertPageButton
= new wxButton(window
, ID_INSERT_PAGE
, "&Insert page", wxPoint(-1, -1), wxSize(80, 25));
68 m_nextPageButton
= new wxButton(window
, ID_NEXT_PAGE
, "&Next page", wxPoint(-1, -1), wxSize(80, 25));
69 m_okButton
->SetDefault();
71 wxLayoutConstraints
*c
;
72 c
= new wxLayoutConstraints
;
73 c
->right
.SameAs(window
, wxRight
, 4);
74 c
->bottom
.SameAs(window
, wxBottom
, 4);
77 m_addPageButton
->SetConstraints(c
);
79 c
= new wxLayoutConstraints
;
80 c
->right
.SameAs(m_addPageButton
, wxLeft
, 4);
81 c
->bottom
.SameAs(window
, wxBottom
, 4);
84 m_insertPageButton
->SetConstraints(c
);
86 c
= new wxLayoutConstraints
;
87 c
->right
.SameAs(m_insertPageButton
, wxLeft
, 4);
88 c
->bottom
.SameAs(window
, wxBottom
, 4);
91 m_nextPageButton
->SetConstraints(c
);
93 c
= new wxLayoutConstraints
;
94 c
->right
.SameAs(m_nextPageButton
, wxLeft
, 4);
95 c
->bottom
.SameAs(window
, wxBottom
, 4);
98 m_cancelButton
->SetConstraints(c
);
100 c
= new wxLayoutConstraints
;
101 c
->right
.SameAs(m_cancelButton
, wxLeft
, 4);
102 c
->bottom
.SameAs(window
, wxBottom
, 4);
105 m_okButton
->SetConstraints(c
);
108 wxPanel
*panel1
= new wxPanel(notebook
, -1);
109 // panel1->SetBackgroundColour(wxColour("RED"));
110 (void)new wxButton(panel1
, -1, "Press me", wxPoint(10, 10));
111 (void)new wxTextCtrl(panel1
, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
113 notebook
->AddPage(panel1
, "Cat", TRUE
);
115 wxPanel
*panel2
= new wxPanel(notebook
, -1);
116 panel2
->SetAutoLayout(TRUE
);
117 panel2
->SetBackgroundColour(wxColour("BLUE"));
119 wxString animals
[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
120 wxRadioBox
*radiobox
= new wxRadioBox(panel2
, -1, "Choose one",
121 wxDefaultPosition
, wxDefaultSize
, 5, animals
,
122 2, wxRA_SPECIFY_ROWS
);
124 c
= new wxLayoutConstraints
;
125 c
->left
.SameAs(panel2
, wxLeft
, 10);
126 c
->top
.SameAs(panel2
, wxTop
, 5);
127 c
->height
.PercentOf(panel2
, wxHeight
, 50);
128 c
->right
.SameAs(panel2
, wxRight
, 10);
129 radiobox
->SetConstraints(c
);
131 wxRadioBox
*radiobox2
= new wxRadioBox(panel2
, -1, "Choose one",
132 wxDefaultPosition
, wxDefaultSize
,
134 2, wxRA_SPECIFY_ROWS
);
136 c
= new wxLayoutConstraints
;
137 c
->left
.SameAs(radiobox
, wxLeft
);
139 c
->top
.Below(radiobox
, 5);
140 c
->right
.SameAs(radiobox
, wxRight
);
141 radiobox2
->SetConstraints(c
);
143 notebook
->AddPage(panel2
, "Dog");
145 wxPanel
*panel3
= new wxPanel(notebook
, -1);
146 panel3
->SetBackgroundColour(wxColour("WHITE"));
147 notebook
->AddPage(panel3
, "Goat");
149 wxPanel
*panel4
= new wxPanel(notebook
, -1);
150 panel4
->SetBackgroundColour(wxColour("YELLOW"));
151 notebook
->AddPage(panel4
, "Sheep");
153 wxPanel
*panel5
= new wxPanel(notebook
, -1);
154 panel5
->SetBackgroundColour(wxColour("MAGENTA"));
155 (void)new wxStaticText(panel5
, -1, "This page has been inserted, not added", wxPoint(10, 10) );
156 notebook
->InsertPage(0, panel5
, "Sheep");
158 notebook
->SetSelection(2);
161 #if USE_TABBED_DIALOG
163 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
164 EVT_BUTTON(wxID_OK
, MyDialog::OnOK
)
165 EVT_BUTTON(wxID_CANCEL
, MyDialog::OnOK
)
168 MyDialog::MyDialog(wxWindow
* parent
, const wxWindowID id
, const wxString
& title
,
169 const wxPoint
& pos
, const wxSize
& size
, const long windowStyle
):
170 wxDialog(parent
, id
, title
, pos
, size
, windowStyle
)
175 void MyDialog::OnOK(wxCommandEvent
& WXUNUSED(event
) )
180 void MyDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
) )
182 EndModal(wxID_CANCEL
);
185 void MyDialog::Init()
187 m_notebook
= new wxNotebook(this, ID_NOTEBOOK
);
189 wxLayoutConstraints
* c
= new wxLayoutConstraints
;
190 c
->left
.SameAs(this, wxLeft
, 4);
191 c
->right
.SameAs(this, wxRight
, 4);
192 c
->top
.SameAs(this, wxTop
, 4);
193 c
->bottom
.SameAs(this, wxBottom
, 40);
195 m_notebook
->SetConstraints(c
);
197 wxGetApp().InitTabView(m_notebook
, this);
205 #else // USE_TABBED_DIALOG
207 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
208 EVT_BUTTON(wxID_OK
, MyFrame::OnOK
)
209 EVT_BUTTON(ID_DELETE_PAGE
, MyFrame::OnDeletePage
)
210 EVT_BUTTON(ID_ADD_PAGE
, MyFrame::OnAddPage
)
211 EVT_BUTTON(ID_INSERT_PAGE
, MyFrame::OnInsertPage
)
212 EVT_BUTTON(ID_NEXT_PAGE
, MyFrame::OnNextPage
)
213 EVT_IDLE(MyFrame::OnIdle
)
216 MyFrame::MyFrame(wxFrame
* parent
, const wxWindowID id
, const wxString
& title
,
217 const wxPoint
& pos
, const wxSize
& size
, const long windowStyle
):
218 wxFrame(parent
, id
, title
, pos
, size
, windowStyle
)
220 m_panel
= (wxPanel
*) NULL
;
221 m_notebook
= (wxNotebook
*) NULL
;
225 void MyFrame::OnAddPage(wxCommandEvent
& WXUNUSED(event
))
227 static size_t s_pageAdded
= 0;
229 wxPanel
*panel
= new wxPanel( m_notebook
, -1 );
230 (void)new wxButton( panel
, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
232 m_notebook
->AddPage( panel
, wxString::Format("Added %u", ++s_pageAdded
) );
235 void MyFrame::OnInsertPage(wxCommandEvent
& WXUNUSED(event
))
237 static size_t s_pageIns
= 0;
239 wxPanel
*panel
= new wxPanel( m_notebook
, -1 );
240 (void)new wxButton( panel
, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
242 m_notebook
->InsertPage(0, panel
, wxString::Format("Inserted %u", ++s_pageIns
) );
243 m_notebook
->SetSelection(0);
246 wxWindow
*test
= NULL
;
248 void MyFrame::OnDeletePage(wxCommandEvent
& WXUNUSED(event
))
250 if (m_notebook
->GetPageCount() > 0)
251 m_notebook
->DeletePage( m_notebook
->GetPageCount()-1 );
256 m_notebook->AddPage( test, "Readded" );
261 test = m_notebook->GetPage( m_notebook->GetPageCount()-1 );
262 m_notebook->RemovePage( m_notebook->GetPageCount()-1 );
267 void MyFrame::OnNextPage(wxCommandEvent
& WXUNUSED(event
))
269 m_notebook
->AdvanceSelection();
272 void MyFrame::OnOK(wxCommandEvent
& WXUNUSED(event
) )
277 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
) )
284 m_panel
= new wxPanel(this, -1, wxDefaultPosition
, wxDefaultSize
, wxTAB_TRAVERSAL
|wxCLIP_CHILDREN
);
286 wxLayoutConstraints
* c
= new wxLayoutConstraints
;
287 c
->left
.SameAs(this, wxLeft
);
288 c
->right
.SameAs(this, wxRight
);
289 c
->top
.SameAs(this, wxTop
);
290 c
->bottom
.SameAs(this, wxBottom
);
291 m_panel
->SetConstraints(c
);
293 m_notebook
= new wxNotebook(m_panel
, ID_NOTEBOOK
);
295 c
= new wxLayoutConstraints
;
296 c
->left
.SameAs(m_panel
, wxLeft
, 4);
297 c
->right
.SameAs(m_panel
, wxRight
, 4);
298 c
->top
.SameAs(m_panel
, wxTop
, 4);
299 c
->bottom
.SameAs(m_panel
, wxBottom
, 40);
301 m_notebook
->SetConstraints(c
);
303 wxGetApp().InitTabView(m_notebook
, m_panel
);
305 m_panel
->SetAutoLayout(TRUE
);
313 void MyFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
315 static int s_nPages
= -1;
316 static int s_nSel
= -1;
318 int nPages
= m_notebook
->GetPageCount();
319 int nSel
= m_notebook
->GetSelection();
320 if ( nPages
!= s_nPages
|| nSel
!= s_nSel
)
326 title
.Printf("Notebook (%d pages, selection: %d)", nPages
, nSel
);
332 #endif // USE_TABBED_DIALOG