]> git.saurik.com Git - wxWidgets.git/blob - samples/notebook/test.cpp
SetSelection() in wxNotebook now sets the focus,
[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 #if USE_TABBED_DIALOG
28 MyDialog* dialog = (MyDialog *) NULL;
29 #else // !USE_TABBED_DIALOG
30 MyFrame* frame = (MyFrame *) NULL;
31 #endif // USE_TABBED_DIALOG
32
33 IMPLEMENT_APP(MyApp)
34
35 bool MyApp::OnInit(void)
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
46 frame = new MyFrame((wxFrame*) NULL, -1, "Notebook", wxPoint(-1, -1), wxSize(365, 390) );
47
48 // Problem with generic wxNotebook implementation whereby it doesn't size properly unless
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);
54 #endif
55
56 return TRUE;
57 #endif
58 }
59
60 void MyApp::InitTabView(wxNotebook* notebook, wxPanel* window)
61 {
62 m_okButton = new wxButton(window, wxID_OK, "Close", wxPoint(-1, -1), wxSize(80, 25));
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_nextPageButton = new wxButton(window, ID_NEXT_PAGE, "Next page", wxPoint(-1, -1), wxSize(80, 25));
66 m_okButton->SetDefault();
67
68 wxLayoutConstraints* c = new wxLayoutConstraints;
69 c->right.SameAs(m_addPageButton, wxLeft, 4);
70 c->bottom.SameAs(window, wxBottom, 4);
71 c->height.AsIs();
72 c->width.AsIs();
73 m_nextPageButton->SetConstraints(c);
74
75 c = new wxLayoutConstraints;
76 c->right.SameAs(window, wxRight, 4);
77 c->bottom.SameAs(window, wxBottom, 4);
78 c->height.AsIs();
79 c->width.AsIs();
80 m_addPageButton->SetConstraints(c);
81
82 c = new wxLayoutConstraints;
83 c->right.SameAs(m_addPageButton, wxLeft, 4);
84 c->bottom.SameAs(window, wxBottom, 4);
85 c->height.AsIs();
86 c->width.AsIs();
87 m_cancelButton->SetConstraints(c);
88
89 c = new wxLayoutConstraints;
90 c->right.SameAs(m_cancelButton, wxLeft, 4);
91 c->bottom.SameAs(window, wxBottom, 4);
92 c->height.AsIs();
93 c->width.AsIs();
94 m_okButton->SetConstraints(c);
95
96 // Add some panels
97 wxPanel *panel1 = new wxPanel(notebook, -1);
98 // panel1->SetBackgroundColour(wxColour("RED"));
99 (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10));
100 (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
101
102 notebook->AddPage(panel1, "Cat", TRUE);
103
104 wxPanel *panel2 = new wxPanel(notebook, -1);
105 panel2->SetAutoLayout(TRUE);
106 panel2->SetBackgroundColour(wxColour("BLUE"));
107
108 wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
109 wxRadioBox *radiobox = new wxRadioBox(panel2, -1, "Choose one",
110 wxDefaultPosition, wxDefaultSize, 5, animals,
111 2, wxRA_SPECIFY_ROWS);
112
113 c = new wxLayoutConstraints;
114 c->left.SameAs(panel2, wxLeft, 10);
115 c->top.SameAs(panel2, wxTop, 5);
116 c->height.PercentOf(panel2, wxHeight, 50);
117 c->right.SameAs(panel2, wxRight, 10);
118 radiobox->SetConstraints(c);
119
120 wxRadioBox *radiobox2 = new wxRadioBox(panel2, -1, "Choose one",
121 wxDefaultPosition, wxDefaultSize,
122 5, animals,
123 2, wxRA_SPECIFY_ROWS);
124
125 c = new wxLayoutConstraints;
126 c->left.SameAs(radiobox, wxLeft);
127 c->height.AsIs();
128 c->top.Below(radiobox, 5);
129 c->right.SameAs(radiobox, wxRight);
130 radiobox2->SetConstraints(c);
131
132 notebook->AddPage(panel2, "Dog");
133
134 wxPanel *panel3 = new wxPanel(notebook, -1);
135 panel3->SetBackgroundColour(wxColour("WHITE"));
136 notebook->AddPage(panel3, "Goat");
137
138 wxPanel *panel4 = new wxPanel(notebook, -1);
139 panel4->SetBackgroundColour(wxColour("YELLOW"));
140 notebook->AddPage(panel4, "Sheep");
141
142 wxPanel *panel5 = new wxPanel(notebook, -1);
143 panel5->SetBackgroundColour(wxColour("MAGENTA"));
144 (void)new wxStaticText(panel5, -1, "This page has been inserted, not added", wxPoint(10, 10) );
145 notebook->InsertPage(0, panel5, "Sheep");
146
147 notebook->SetSelection(2);
148 }
149
150 #if USE_TABBED_DIALOG
151
152 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
153 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
154 EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK)
155 END_EVENT_TABLE()
156
157 MyDialog::MyDialog(wxWindow* parent, const wxWindowID id, const wxString& title,
158 const wxPoint& pos, const wxSize& size, const long windowStyle):
159 wxDialog(parent, id, title, pos, size, windowStyle)
160 {
161 Init();
162 }
163
164 void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
165 {
166 EndModal(wxID_OK);
167 }
168
169 void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
170 {
171 EndModal(wxID_CANCEL);
172 }
173
174 void MyDialog::Init(void)
175 {
176 m_notebook = new wxNotebook(this, ID_NOTEBOOK);
177
178 wxLayoutConstraints* c = new wxLayoutConstraints;
179 c->left.SameAs(this, wxLeft, 4);
180 c->right.SameAs(this, wxRight, 4);
181 c->top.SameAs(this, wxTop, 4);
182 c->bottom.SameAs(this, wxBottom, 40);
183
184 m_notebook->SetConstraints(c);
185
186 wxGetApp().InitTabView(m_notebook, this);
187
188 SetAutoLayout(TRUE);
189 Layout();
190
191 Centre(wxBOTH);
192 }
193
194 #else // USE_TABBED_DIALOG
195
196 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
197 EVT_BUTTON(wxID_OK, MyFrame::OnOK)
198 EVT_BUTTON(ID_DELETE_PAGE, MyFrame::OnDeletePage)
199 EVT_BUTTON(ID_ADD_PAGE, MyFrame::OnAddPage)
200 EVT_BUTTON(ID_NEXT_PAGE, MyFrame::OnNextPage)
201 EVT_IDLE(MyFrame::OnIdle)
202 END_EVENT_TABLE()
203
204 MyFrame::MyFrame(wxFrame* parent, const wxWindowID id, const wxString& title,
205 const wxPoint& pos, const wxSize& size, const long windowStyle):
206 wxFrame(parent, id, title, pos, size, windowStyle)
207 {
208 m_panel = (wxPanel*) NULL;
209 m_notebook = (wxNotebook*) NULL;
210 Init();
211 }
212
213 void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
214 {
215 wxPanel *panel = new wxPanel( m_notebook, -1 );
216 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
217 m_notebook->AddPage( panel, "Added" );
218 // m_notebook->SetSelection( m_notebook->GetPageCount()-1 );
219 }
220
221 void MyFrame::OnDeletePage(wxCommandEvent& WXUNUSED(event))
222 {
223 m_notebook->DeletePage( m_notebook->GetPageCount()-1 );
224 }
225
226 void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event))
227 {
228 m_notebook->AdvanceSelection();
229 }
230
231 void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) )
232 {
233 Destroy();
234 }
235
236 void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
237 {
238 Destroy();
239 }
240
241 void MyFrame::Init(void)
242 {
243 m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxCLIP_CHILDREN);
244
245 wxLayoutConstraints* c = new wxLayoutConstraints;
246 c->left.SameAs(this, wxLeft);
247 c->right.SameAs(this, wxRight);
248 c->top.SameAs(this, wxTop);
249 c->bottom.SameAs(this, wxBottom);
250 m_panel->SetConstraints(c);
251
252 // Note, omit the wxTAB_STYLE_COLOUR_INTERIOR, so we will guarantee a match
253 // with the panel background, and save a bit of time.
254 m_notebook = new wxNotebook(m_panel, ID_NOTEBOOK);
255
256 c = new wxLayoutConstraints;
257 c->left.SameAs(m_panel, wxLeft, 4);
258 c->right.SameAs(m_panel, wxRight, 4);
259 c->top.SameAs(m_panel, wxTop, 4);
260 c->bottom.SameAs(m_panel, wxBottom, 40);
261
262 m_notebook->SetConstraints(c);
263
264 wxGetApp().InitTabView(m_notebook, m_panel);
265
266 m_panel->SetAutoLayout(TRUE);
267 SetAutoLayout(TRUE);
268
269 Centre(wxBOTH);
270
271 Show(TRUE);
272 }
273
274 void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
275 {
276 static int s_nPages = -1;
277 static int s_nSel = -1;
278
279 int nPages = m_notebook->GetPageCount();
280 int nSel = m_notebook->GetSelection();
281 if ( nPages != s_nPages || nSel != s_nSel )
282 {
283 s_nPages = nPages;
284 s_nSel = nSel;
285
286 wxString title;
287 title.Printf("Notebook (%d pages, selection: %d)", nPages, nSel);
288
289 SetTitle(title);
290 }
291 }
292
293 #endif // USE_TABBED_DIALOG