]> git.saurik.com Git - wxWidgets.git/blob - samples/notebook/test.cpp
Fixed a notebook crash and added more tests to sample.
[wxWidgets.git] / samples / notebook / test.cpp
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)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "test.h"
24
25 // If 1, use a dialog. Otherwise use a frame.
26 #define USE_TABBED_DIALOG 0
27
28 MyDialog* dialog = (MyDialog *) NULL;
29 MyFrame* frame = (MyFrame *) NULL;
30
31 IMPLEMENT_APP(MyApp)
32
33 bool MyApp::OnInit(void)
34 {
35 // Create the main window
36 #if USE_TABBED_DIALOG
37 dialog = new MyDialog((wxFrame *) NULL, -1, (char *) "Notebook", wxPoint(-1, -1), wxSize(365, 390), wxDIALOG_MODAL|wxDEFAULT_DIALOG_STYLE);
38
39 dialog->ShowModal();
40
41 // Quit immediately the dialog has been dismissed
42 return FALSE;
43 #else
44 frame = new MyFrame((wxFrame*) NULL, -1, (char *) "Notebook", wxPoint(-1, -1), wxSize(365, 390), wxDEFAULT_FRAME_STYLE);
45
46 // Problem with generic wxNotebook implementation whereby it doesn't size properly unless
47 // you set the size again
48 #if defined(__WIN16__)
49 int width, height;
50 frame->GetSize(& width, & height);
51 frame->SetSize(-1, -1, width, height);
52 #endif
53
54 return TRUE;
55 #endif
56 }
57
58 void MyApp::InitTabView(wxNotebook* notebook, wxWindow* window)
59 {
60 m_okButton = new wxButton(window, wxID_OK, "Close", wxPoint(-1, -1), wxSize(80, 25));
61 m_cancelButton = new wxButton(window, ID_DELETE_PAGE, "Delete page", wxPoint(-1, -1), wxSize(80, 25));
62 m_addPageButton = new wxButton(window, ID_ADD_PAGE, "Add page", wxPoint(-1, -1), wxSize(80, 25));
63 m_okButton->SetDefault();
64
65 wxLayoutConstraints* c = new wxLayoutConstraints;
66 c->right.SameAs(window, wxRight, 4);
67 c->bottom.SameAs(window, wxBottom, 4);
68 c->height.AsIs();
69 c->width.AsIs();
70 m_addPageButton->SetConstraints(c);
71
72 c = new wxLayoutConstraints;
73 c->right.SameAs(m_addPageButton, wxLeft, 4);
74 c->bottom.SameAs(window, wxBottom, 4);
75 c->height.AsIs();
76 c->width.AsIs();
77 m_cancelButton->SetConstraints(c);
78
79 c = new wxLayoutConstraints;
80 c->right.SameAs(m_cancelButton, wxLeft, 4);
81 c->bottom.SameAs(window, wxBottom, 4);
82 c->height.AsIs();
83 c->width.AsIs();
84 m_okButton->SetConstraints(c);
85
86 // Add some panels
87 wxPanel *panel1 = new wxPanel(notebook, -1);
88 // panel1->SetBackgroundColour(wxColour("RED"));
89 (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10));
90 (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
91
92 notebook->AddPage(panel1, "Cat", TRUE);
93
94 wxPanel *panel2 = new wxPanel(notebook, -1);
95 panel2->SetBackgroundColour(wxColour("BLUE"));
96
97 wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
98 (void)new wxListBox(panel2, -1, wxPoint(5, 5), wxSize(170, 80), 5, animals);
99
100 (void)new wxTextCtrl(panel2, -1, "Some notes about the animals in this house", wxPoint(5, 100), wxSize(170, 100),
101 wxTE_MULTILINE);
102
103 notebook->AddPage(panel2, "Dog");
104 wxPanel *panel3 = new wxPanel(notebook, -1);
105 panel3->SetBackgroundColour(wxColour("WHITE"));
106 notebook->AddPage(panel3, "Goat");
107
108 wxPanel *panel4 = new wxPanel(notebook, -1);
109 panel4->SetBackgroundColour(wxColour("YELLOW"));
110 notebook->AddPage(panel4, "Sheep");
111
112 wxPanel *panel5 = new wxPanel(notebook, -1);
113 panel5->SetBackgroundColour(wxColour("MAGENTA"));
114 (void)new wxStaticText(panel5, -1, "This page has been inserted, not added", wxPoint(10, 10) );
115 notebook->InsertPage(0, panel5, "Sheep");
116 }
117
118 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
119 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
120 EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK)
121 END_EVENT_TABLE()
122
123 MyDialog::MyDialog(wxWindow* parent, const wxWindowID id, const wxString& title,
124 const wxPoint& pos, const wxSize& size, const long windowStyle):
125 wxDialog(parent, id, title, pos, size, windowStyle)
126 {
127 Init();
128 }
129
130 void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
131 {
132 EndModal(wxID_OK);
133 }
134
135 void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
136 {
137 EndModal(wxID_CANCEL);
138 }
139
140 void MyDialog::Init(void)
141 {
142 m_notebook = new wxNotebook(this, ID_NOTEBOOK);
143
144 wxLayoutConstraints* c = new wxLayoutConstraints;
145 c->left.SameAs(this, wxLeft, 4);
146 c->right.SameAs(this, wxRight, 4);
147 c->top.SameAs(this, wxTop, 4);
148 c->bottom.SameAs(this, wxBottom, 40);
149
150 m_notebook->SetConstraints(c);
151
152 wxGetApp().InitTabView(m_notebook, this);
153
154 SetAutoLayout(TRUE);
155 Layout();
156
157 this->Centre(wxBOTH);
158 }
159
160 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
161 EVT_BUTTON(wxID_OK, MyFrame::OnOK)
162 EVT_BUTTON(ID_DELETE_PAGE, MyFrame::OnDeletePage)
163 EVT_BUTTON(ID_ADD_PAGE, MyFrame::OnAddPage)
164 EVT_SIZE(MyFrame::OnSize)
165 END_EVENT_TABLE()
166
167 MyFrame::MyFrame(wxFrame* parent, const wxWindowID id, const wxString& title,
168 const wxPoint& pos, const wxSize& size, const long windowStyle):
169 wxFrame(parent, id, title, pos, size, windowStyle)
170 {
171 m_panel = (wxPanel*) NULL;
172 m_notebook = (wxNotebook*) NULL;
173 Init();
174 }
175
176 void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
177 {
178 wxPanel *panel = new wxPanel( m_notebook, -1 );
179 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
180 m_notebook->AddPage( panel, "Added" );
181 m_notebook->SetSelection( m_notebook->GetPageCount()-1 );
182 }
183
184 void MyFrame::OnDeletePage(wxCommandEvent& WXUNUSED(event))
185 {
186 m_notebook->DeletePage( m_notebook->GetPageCount()-1 );
187 }
188
189 void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) )
190 {
191 this->Destroy();
192 }
193
194 void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
195 {
196 this->Destroy();
197 }
198
199 void MyFrame::Init(void)
200 {
201 m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxCLIP_CHILDREN);
202
203 // Note, omit the wxTAB_STYLE_COLOUR_INTERIOR, so we will guarantee a match
204 // with the panel background, and save a bit of time.
205 m_notebook = new wxNotebook(m_panel, ID_NOTEBOOK);
206
207 wxLayoutConstraints* c = new wxLayoutConstraints;
208 c->left.SameAs(m_panel, wxLeft, 4);
209 c->right.SameAs(m_panel, wxRight, 4);
210 c->top.SameAs(m_panel, wxTop, 4);
211 c->bottom.SameAs(m_panel, wxBottom, 40);
212
213 m_notebook->SetConstraints(c);
214
215 wxGetApp().InitTabView(m_notebook, m_panel);
216
217 m_panel->SetAutoLayout(TRUE);
218
219 m_panel->Layout();
220
221 this->Centre(wxBOTH);
222
223 Show(TRUE);
224 }
225
226 void MyFrame::OnSize(wxSizeEvent& event)
227 {
228 wxFrame::OnSize(event);
229 m_panel->Layout();
230 }
231