]> git.saurik.com Git - wxWidgets.git/blame - samples/notebook/test.cpp
Committing menubar changes again after CVS trashed them.
[wxWidgets.git] / samples / notebook / test.cpp
CommitLineData
0b4d4194
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: test.cpp
3// Purpose: wxNotebook demo
4// Author: Julian Smart
5// Modified by:
6// Created: 26/10/98
7// RCS-ID: $Id$
8// Copyright: (c)
e3e717ec 9// Licence: wxWindows licence
0b4d4194
JS
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
d22699b5 16 #pragma hdrstop
0b4d4194
JS
17#endif
18
19#ifndef WX_PRECOMP
d22699b5 20 #include "wx/wx.h"
0b4d4194
JS
21#endif
22
0b4d4194
JS
23#include "test.h"
24
25// If 1, use a dialog. Otherwise use a frame.
26#define USE_TABBED_DIALOG 0
d22699b5 27#if USE_TABBED_DIALOG
0b4d4194 28MyDialog* dialog = (MyDialog *) NULL;
d22699b5 29#else // !USE_TABBED_DIALOG
0b4d4194 30MyFrame* frame = (MyFrame *) NULL;
d22699b5 31#endif // USE_TABBED_DIALOG
0b4d4194
JS
32
33IMPLEMENT_APP(MyApp)
34
477350ce 35bool MyApp::OnInit()
0b4d4194
JS
36{
37 // Create the main window
38#if USE_TABBED_DIALOG
39 dialog = new MyDialog((wxFrame *) NULL, -1, (char *) "Notebook", wxPoint(-1, -1), wxSize(365, 390), wxDIALOG_MODAL|wxDEFAULT_DIALOG_STYLE);
40
41 dialog->ShowModal();
42
43 // Quit immediately the dialog has been dismissed
44 return FALSE;
45#else
e3e717ec 46 frame = new MyFrame((wxFrame*) NULL, -1, "Notebook", wxPoint(-1, -1), wxSize(365, 390) );
0b4d4194 47
f60d0f94 48 // Problem with generic wxNotebook implementation whereby it doesn't size properly unless
ee4c6942
JS
49 // you set the size again
50#if defined(__WIN16__)
51 int width, height;
52 frame->GetSize(& width, & height);
53 frame->SetSize(-1, -1, width, height);
5dcf05ae
JS
54#endif
55
0b4d4194
JS
56 return TRUE;
57#endif
58}
59
d22699b5 60void MyApp::InitTabView(wxNotebook* notebook, wxPanel* window)
0b4d4194 61{
0b4d4194 62 m_okButton = new wxButton(window, wxID_OK, "Close", wxPoint(-1, -1), wxSize(80, 25));
477350ce
VZ
63 m_cancelButton = new wxButton(window, ID_DELETE_PAGE, "&Delete page", wxPoint(-1, -1), wxSize(80, 25));
64 m_addPageButton = new wxButton(window, ID_ADD_PAGE, "&Add page", wxPoint(-1, -1), wxSize(80, 25));
65 m_insertPageButton = new wxButton(window, ID_INSERT_PAGE, "&Insert page", wxPoint(-1, -1), wxSize(80, 25));
66 m_nextPageButton = new wxButton(window, ID_NEXT_PAGE, "&Next page", wxPoint(-1, -1), wxSize(80, 25));
0b4d4194
JS
67 m_okButton->SetDefault();
68
477350ce
VZ
69 wxLayoutConstraints *c;
70
71 c = new wxLayoutConstraints;
72 c->right.SameAs(window, wxRight, 4);
73 c->bottom.SameAs(window, wxBottom, 4);
74 c->height.AsIs();
75 c->width.AsIs();
76 m_addPageButton->SetConstraints(c);
77
78 c = new wxLayoutConstraints;
33d0e17c
RR
79 c->right.SameAs(m_addPageButton, wxLeft, 4);
80 c->bottom.SameAs(window, wxBottom, 4);
81 c->height.AsIs();
82 c->width.AsIs();
477350ce 83 m_insertPageButton->SetConstraints(c);
33d0e17c
RR
84
85 c = new wxLayoutConstraints;
477350ce 86 c->right.SameAs(m_insertPageButton, wxLeft, 4);
0b4d4194
JS
87 c->bottom.SameAs(window, wxBottom, 4);
88 c->height.AsIs();
89 c->width.AsIs();
477350ce 90 m_nextPageButton->SetConstraints(c);
0b4d4194
JS
91
92 c = new wxLayoutConstraints;
477350ce 93 c->right.SameAs(m_nextPageButton, wxLeft, 4);
0b4d4194
JS
94 c->bottom.SameAs(window, wxBottom, 4);
95 c->height.AsIs();
96 c->width.AsIs();
97 m_cancelButton->SetConstraints(c);
98
99 c = new wxLayoutConstraints;
100 c->right.SameAs(m_cancelButton, wxLeft, 4);
101 c->bottom.SameAs(window, wxBottom, 4);
102 c->height.AsIs();
103 c->width.AsIs();
104 m_okButton->SetConstraints(c);
105
106 // Add some panels
107 wxPanel *panel1 = new wxPanel(notebook, -1);
02800301 108 // panel1->SetBackgroundColour(wxColour("RED"));
0b4d4194
JS
109 (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10));
110 (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
e3e717ec 111
5dcf05ae 112 notebook->AddPage(panel1, "Cat", TRUE);
0b4d4194
JS
113
114 wxPanel *panel2 = new wxPanel(notebook, -1);
e3e717ec 115 panel2->SetAutoLayout(TRUE);
02800301 116 panel2->SetBackgroundColour(wxColour("BLUE"));
0b4d4194
JS
117
118 wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
bb69661b 119 wxRadioBox *radiobox = new wxRadioBox(panel2, -1, "Choose one",
d93c719a
VZ
120 wxDefaultPosition, wxDefaultSize, 5, animals,
121 2, wxRA_SPECIFY_ROWS);
e3e717ec
VZ
122
123 c = new wxLayoutConstraints;
bea56879
VZ
124 c->left.SameAs(panel2, wxLeft, 10);
125 c->top.SameAs(panel2, wxTop, 5);
e3e717ec 126 c->height.PercentOf(panel2, wxHeight, 50);
bea56879 127 c->right.SameAs(panel2, wxRight, 10);
bb69661b 128 radiobox->SetConstraints(c);
0b4d4194 129
bb69661b
VZ
130 wxRadioBox *radiobox2 = new wxRadioBox(panel2, -1, "Choose one",
131 wxDefaultPosition, wxDefaultSize,
132 5, animals,
133 2, wxRA_SPECIFY_ROWS);
6d693bb4 134
e3e717ec 135 c = new wxLayoutConstraints;
bb69661b
VZ
136 c->left.SameAs(radiobox, wxLeft);
137 c->height.AsIs();
bea56879 138 c->top.Below(radiobox, 5);
bb69661b
VZ
139 c->right.SameAs(radiobox, wxRight);
140 radiobox2->SetConstraints(c);
0b4d4194
JS
141
142 notebook->AddPage(panel2, "Dog");
e3e717ec 143
02800301
JS
144 wxPanel *panel3 = new wxPanel(notebook, -1);
145 panel3->SetBackgroundColour(wxColour("WHITE"));
146 notebook->AddPage(panel3, "Goat");
147
148 wxPanel *panel4 = new wxPanel(notebook, -1);
149 panel4->SetBackgroundColour(wxColour("YELLOW"));
150 notebook->AddPage(panel4, "Sheep");
e3e717ec 151
29f538ce
RR
152 wxPanel *panel5 = new wxPanel(notebook, -1);
153 panel5->SetBackgroundColour(wxColour("MAGENTA"));
154 (void)new wxStaticText(panel5, -1, "This page has been inserted, not added", wxPoint(10, 10) );
155 notebook->InsertPage(0, panel5, "Sheep");
e3e717ec
VZ
156
157 notebook->SetSelection(2);
0b4d4194
JS
158}
159
d22699b5
VZ
160#if USE_TABBED_DIALOG
161
0b4d4194
JS
162BEGIN_EVENT_TABLE(MyDialog, wxDialog)
163 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
164 EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK)
165END_EVENT_TABLE()
166
167MyDialog::MyDialog(wxWindow* parent, const wxWindowID id, const wxString& title,
168 const wxPoint& pos, const wxSize& size, const long windowStyle):
169 wxDialog(parent, id, title, pos, size, windowStyle)
170{
171 Init();
172}
173
174void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
175{
176 EndModal(wxID_OK);
177}
178
179void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
180{
181 EndModal(wxID_CANCEL);
182}
183
477350ce 184void MyDialog::Init()
0b4d4194 185{
0b4d4194
JS
186 m_notebook = new wxNotebook(this, ID_NOTEBOOK);
187
188 wxLayoutConstraints* c = new wxLayoutConstraints;
189 c->left.SameAs(this, wxLeft, 4);
190 c->right.SameAs(this, wxRight, 4);
191 c->top.SameAs(this, wxTop, 4);
192 c->bottom.SameAs(this, wxBottom, 40);
193
194 m_notebook->SetConstraints(c);
195
196 wxGetApp().InitTabView(m_notebook, this);
197
198 SetAutoLayout(TRUE);
199 Layout();
200
bb69661b 201 Centre(wxBOTH);
0b4d4194
JS
202}
203
d22699b5
VZ
204#else // USE_TABBED_DIALOG
205
0b4d4194
JS
206BEGIN_EVENT_TABLE(MyFrame, wxFrame)
207 EVT_BUTTON(wxID_OK, MyFrame::OnOK)
29f538ce 208 EVT_BUTTON(ID_DELETE_PAGE, MyFrame::OnDeletePage)
326f9654 209 EVT_BUTTON(ID_ADD_PAGE, MyFrame::OnAddPage)
477350ce 210 EVT_BUTTON(ID_INSERT_PAGE, MyFrame::OnInsertPage)
33d0e17c 211 EVT_BUTTON(ID_NEXT_PAGE, MyFrame::OnNextPage)
96d37807 212 EVT_IDLE(MyFrame::OnIdle)
0b4d4194
JS
213END_EVENT_TABLE()
214
215MyFrame::MyFrame(wxFrame* parent, const wxWindowID id, const wxString& title,
216 const wxPoint& pos, const wxSize& size, const long windowStyle):
217 wxFrame(parent, id, title, pos, size, windowStyle)
218{
219 m_panel = (wxPanel*) NULL;
220 m_notebook = (wxNotebook*) NULL;
221 Init();
222}
223
326f9654
RR
224void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
225{
477350ce
VZ
226 static size_t s_pageAdded = 0;
227
228 wxPanel *panel = new wxPanel( m_notebook, -1 );
229 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
230
231 m_notebook->AddPage( panel, wxString::Format("Added %u", ++s_pageAdded) );
232}
233
234void MyFrame::OnInsertPage(wxCommandEvent& WXUNUSED(event))
235{
236 static size_t s_pageIns = 0;
237
238 wxPanel *panel = new wxPanel( m_notebook, -1 );
239 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
240
241 m_notebook->InsertPage(0, panel, wxString::Format("Inserted %u", ++s_pageIns) );
242 m_notebook->SetSelection(0);
326f9654
RR
243}
244
29f538ce
RR
245void MyFrame::OnDeletePage(wxCommandEvent& WXUNUSED(event))
246{
96d37807 247 m_notebook->DeletePage( m_notebook->GetPageCount()-1 );
29f538ce
RR
248}
249
33d0e17c
RR
250void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event))
251{
252 m_notebook->AdvanceSelection();
253}
254
0b4d4194
JS
255void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) )
256{
96d37807 257 Destroy();
0b4d4194
JS
258}
259
260void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
261{
96d37807 262 Destroy();
0b4d4194
JS
263}
264
477350ce 265void MyFrame::Init()
0b4d4194 266{
0b4d4194
JS
267 m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxCLIP_CHILDREN);
268
d22699b5
VZ
269 wxLayoutConstraints* c = new wxLayoutConstraints;
270 c->left.SameAs(this, wxLeft);
271 c->right.SameAs(this, wxRight);
272 c->top.SameAs(this, wxTop);
273 c->bottom.SameAs(this, wxBottom);
274 m_panel->SetConstraints(c);
275
0b4d4194
JS
276 // Note, omit the wxTAB_STYLE_COLOUR_INTERIOR, so we will guarantee a match
277 // with the panel background, and save a bit of time.
278 m_notebook = new wxNotebook(m_panel, ID_NOTEBOOK);
279
d22699b5 280 c = new wxLayoutConstraints;
0b4d4194
JS
281 c->left.SameAs(m_panel, wxLeft, 4);
282 c->right.SameAs(m_panel, wxRight, 4);
283 c->top.SameAs(m_panel, wxTop, 4);
284 c->bottom.SameAs(m_panel, wxBottom, 40);
285
286 m_notebook->SetConstraints(c);
287
288 wxGetApp().InitTabView(m_notebook, m_panel);
289
290 m_panel->SetAutoLayout(TRUE);
d22699b5 291 SetAutoLayout(TRUE);
0b4d4194 292
d22699b5 293 Centre(wxBOTH);
0b4d4194
JS
294
295 Show(TRUE);
296}
297
96d37807
VZ
298void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
299{
300 static int s_nPages = -1;
301 static int s_nSel = -1;
302
303 int nPages = m_notebook->GetPageCount();
304 int nSel = m_notebook->GetSelection();
305 if ( nPages != s_nPages || nSel != s_nSel )
306 {
307 s_nPages = nPages;
308 s_nSel = nSel;
309
310 wxString title;
311 title.Printf("Notebook (%d pages, selection: %d)", nPages, nSel);
312
313 SetTitle(title);
314 }
315}
d22699b5
VZ
316
317#endif // USE_TABBED_DIALOG