]> git.saurik.com Git - wxWidgets.git/blob - samples/notebook/notebook.cpp
some more src code reformatting
[wxWidgets.git] / samples / notebook / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: notebook.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 "notebook.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()
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(465, 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__) || defined(__WXMOTIF__)
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_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));
67 m_okButton->SetDefault();
68
69 wxLayoutConstraints *c;
70 c = new wxLayoutConstraints;
71 c->right.SameAs(window, wxRight, 4);
72 c->bottom.SameAs(window, wxBottom, 4);
73 c->height.AsIs();
74 c->width.AsIs();
75 m_addPageButton->SetConstraints(c);
76
77 c = new wxLayoutConstraints;
78 c->right.SameAs(m_addPageButton, wxLeft, 4);
79 c->bottom.SameAs(window, wxBottom, 4);
80 c->height.AsIs();
81 c->width.AsIs();
82 m_insertPageButton->SetConstraints(c);
83
84 c = new wxLayoutConstraints;
85 c->right.SameAs(m_insertPageButton, wxLeft, 4);
86 c->bottom.SameAs(window, wxBottom, 4);
87 c->height.AsIs();
88 c->width.AsIs();
89 m_nextPageButton->SetConstraints(c);
90
91 c = new wxLayoutConstraints;
92 c->right.SameAs(m_nextPageButton, wxLeft, 4);
93 c->bottom.SameAs(window, wxBottom, 4);
94 c->height.AsIs();
95 c->width.AsIs();
96 m_cancelButton->SetConstraints(c);
97
98 c = new wxLayoutConstraints;
99 c->right.SameAs(m_cancelButton, wxLeft, 4);
100 c->bottom.SameAs(window, wxBottom, 4);
101 c->height.AsIs();
102 c->width.AsIs();
103 m_okButton->SetConstraints(c);
104
105 // Add some panels
106 wxPanel *panel1 = new wxPanel(notebook, -1);
107 // panel1->SetBackgroundColour(wxColour("RED"));
108 (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10));
109 (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
110
111 notebook->AddPage(panel1, "Cat", TRUE);
112
113 wxPanel *panel2 = new wxPanel(notebook, -1);
114 panel2->SetAutoLayout(TRUE);
115 panel2->SetBackgroundColour(wxColour("BLUE"));
116
117 wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
118 wxRadioBox *radiobox = new wxRadioBox(panel2, -1, "Choose one",
119 wxDefaultPosition, wxDefaultSize, 5, animals,
120 2, wxRA_SPECIFY_ROWS);
121
122 c = new wxLayoutConstraints;
123 c->left.SameAs(panel2, wxLeft, 10);
124 c->top.SameAs(panel2, wxTop, 5);
125 c->height.PercentOf(panel2, wxHeight, 50);
126 c->right.SameAs(panel2, wxRight, 10);
127 radiobox->SetConstraints(c);
128
129 wxRadioBox *radiobox2 = new wxRadioBox(panel2, -1, "Choose one",
130 wxDefaultPosition, wxDefaultSize,
131 5, animals,
132 2, wxRA_SPECIFY_ROWS);
133
134 c = new wxLayoutConstraints;
135 c->left.SameAs(radiobox, wxLeft);
136 c->height.AsIs();
137 c->top.Below(radiobox, 5);
138 c->right.SameAs(radiobox, wxRight);
139 radiobox2->SetConstraints(c);
140
141 notebook->AddPage(panel2, "Dog");
142
143 wxPanel *panel3 = new wxPanel(notebook, -1);
144 panel3->SetBackgroundColour(wxColour("WHITE"));
145 notebook->AddPage(panel3, "Goat");
146
147 wxPanel *panel4 = new wxPanel(notebook, -1);
148 panel4->SetBackgroundColour(wxColour("YELLOW"));
149 notebook->AddPage(panel4, "Sheep");
150
151 wxPanel *panel5 = new wxPanel(notebook, -1);
152 panel5->SetBackgroundColour(wxColour("MAGENTA"));
153 (void)new wxStaticText(panel5, -1, "This page has been inserted, not added", wxPoint(10, 10) );
154 notebook->InsertPage(0, panel5, "Sheep");
155
156 notebook->SetSelection(2);
157 }
158
159 #if USE_TABBED_DIALOG
160
161 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
162 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
163 EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK)
164 END_EVENT_TABLE()
165
166 MyDialog::MyDialog(wxWindow* parent, const wxWindowID id, const wxString& title,
167 const wxPoint& pos, const wxSize& size, const long windowStyle):
168 wxDialog(parent, id, title, pos, size, windowStyle)
169 {
170 Init();
171 }
172
173 void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
174 {
175 EndModal(wxID_OK);
176 }
177
178 void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
179 {
180 EndModal(wxID_CANCEL);
181 }
182
183 void MyDialog::Init()
184 {
185 m_notebook = new wxNotebook(this, ID_NOTEBOOK);
186
187 wxLayoutConstraints* c = new wxLayoutConstraints;
188 c->left.SameAs(this, wxLeft, 4);
189 c->right.SameAs(this, wxRight, 4);
190 c->top.SameAs(this, wxTop, 4);
191 c->bottom.SameAs(this, wxBottom, 40);
192
193 m_notebook->SetConstraints(c);
194
195 wxGetApp().InitTabView(m_notebook, this);
196
197 SetAutoLayout(TRUE);
198 Layout();
199
200 Centre(wxBOTH);
201 }
202
203 #else // USE_TABBED_DIALOG
204
205 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
206 EVT_BUTTON(wxID_OK, MyFrame::OnOK)
207 EVT_BUTTON(ID_DELETE_PAGE, MyFrame::OnDeletePage)
208 EVT_BUTTON(ID_ADD_PAGE, MyFrame::OnAddPage)
209 EVT_BUTTON(ID_INSERT_PAGE, MyFrame::OnInsertPage)
210 EVT_BUTTON(ID_NEXT_PAGE, MyFrame::OnNextPage)
211 EVT_IDLE(MyFrame::OnIdle)
212 END_EVENT_TABLE()
213
214 MyFrame::MyFrame(wxFrame* parent, const wxWindowID id, const wxString& title,
215 const wxPoint& pos, const wxSize& size, const long windowStyle):
216 wxFrame(parent, id, title, pos, size, windowStyle)
217 {
218 m_panel = (wxPanel*) NULL;
219 m_notebook = (wxNotebook*) NULL;
220 Init();
221 }
222
223 void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
224 {
225 static size_t s_pageAdded = 0;
226
227 wxPanel *panel = new wxPanel( m_notebook, -1 );
228 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
229
230 m_notebook->AddPage( panel, wxString::Format("Added %u", ++s_pageAdded) );
231 }
232
233 void MyFrame::OnInsertPage(wxCommandEvent& WXUNUSED(event))
234 {
235 static size_t s_pageIns = 0;
236
237 wxPanel *panel = new wxPanel( m_notebook, -1 );
238 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
239
240 m_notebook->InsertPage(0, panel, wxString::Format("Inserted %u", ++s_pageIns) );
241 m_notebook->SetSelection(0);
242 }
243
244 wxWindow *test = NULL;
245
246 void MyFrame::OnDeletePage(wxCommandEvent& WXUNUSED(event))
247 {
248 if (m_notebook->GetPageCount() > 0)
249 m_notebook->DeletePage( m_notebook->GetPageCount()-1 );
250
251 /*
252 if (test)
253 {
254 m_notebook->AddPage( test, "Readded" );
255 test = NULL;
256 }
257 else
258 {
259 test = m_notebook->GetPage( m_notebook->GetPageCount()-1 );
260 m_notebook->RemovePage( m_notebook->GetPageCount()-1 );
261 }
262 */
263 }
264
265 void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event))
266 {
267 m_notebook->AdvanceSelection();
268 }
269
270 void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) )
271 {
272 Destroy();
273 }
274
275 void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
276 {
277 Destroy();
278 }
279
280 void MyFrame::Init()
281 {
282 m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxCLIP_CHILDREN);
283
284 wxLayoutConstraints* c = new wxLayoutConstraints;
285 c->left.SameAs(this, wxLeft);
286 c->right.SameAs(this, wxRight);
287 c->top.SameAs(this, wxTop);
288 c->bottom.SameAs(this, wxBottom);
289 m_panel->SetConstraints(c);
290
291 m_notebook = new wxNotebook(m_panel, ID_NOTEBOOK);
292
293 c = new wxLayoutConstraints;
294 c->left.SameAs(m_panel, wxLeft, 4);
295 c->right.SameAs(m_panel, wxRight, 4);
296 c->top.SameAs(m_panel, wxTop, 4);
297 c->bottom.SameAs(m_panel, wxBottom, 40);
298
299 m_notebook->SetConstraints(c);
300
301 wxGetApp().InitTabView(m_notebook, m_panel);
302
303 m_panel->SetAutoLayout(TRUE);
304 SetAutoLayout(TRUE);
305
306 Centre(wxBOTH);
307
308 Show(TRUE);
309 }
310
311 void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
312 {
313 static int s_nPages = -1;
314 static int s_nSel = -1;
315
316 int nPages = m_notebook->GetPageCount();
317 int nSel = m_notebook->GetSelection();
318 if ( nPages != s_nPages || nSel != s_nSel )
319 {
320 s_nPages = nPages;
321 s_nSel = nSel;
322
323 wxString title;
324 title.Printf("Notebook (%d pages, selection: %d)", nPages, nSel);
325
326 SetTitle(title);
327 }
328 }
329
330 #endif // USE_TABBED_DIALOG