]> git.saurik.com Git - wxWidgets.git/blame - samples/notebook/notebook.cpp
need wxUSE_WCHAR_T around wxConvLocal
[wxWidgets.git] / samples / notebook / notebook.cpp
CommitLineData
0b4d4194 1/////////////////////////////////////////////////////////////////////////////
b2f757f9 2// Name: notebook.cpp
0b4d4194
JS
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
ef299235
VZ
23// If 1, use a dialog. Otherwise use a frame.
24#define USE_TABBED_DIALOG 1
25
b2f757f9 26#include "notebook.h"
0b4d4194 27
d22699b5 28#if USE_TABBED_DIALOG
0b4d4194 29MyDialog* dialog = (MyDialog *) NULL;
d22699b5 30#else // !USE_TABBED_DIALOG
0b4d4194 31MyFrame* frame = (MyFrame *) NULL;
d22699b5 32#endif // USE_TABBED_DIALOG
0b4d4194
JS
33
34IMPLEMENT_APP(MyApp)
35
477350ce 36bool MyApp::OnInit()
0b4d4194
JS
37{
38 // Create the main window
39#if USE_TABBED_DIALOG
28efed0f
VZ
40 dialog = new MyDialog((wxFrame *) NULL, -1, "Notebook", wxPoint(-1, -1), wxSize(365, 390),
41 wxRESIZE_BORDER);
0b4d4194 42 dialog->ShowModal();
ef299235 43 delete dialog;
0b4d4194
JS
44
45 // Quit immediately the dialog has been dismissed
46 return FALSE;
47#else
8e97b17b 48 frame = new MyFrame((wxFrame*) NULL, -1, "Notebook", wxPoint(-1, -1), wxSize(465, 390) );
0b4d4194 49
f60d0f94 50 // Problem with generic wxNotebook implementation whereby it doesn't size properly unless
ee4c6942 51 // you set the size again
37d403aa 52#if defined(__WIN16__) || defined(__WXMOTIF__)
ee4c6942
JS
53 int width, height;
54 frame->GetSize(& width, & height);
55 frame->SetSize(-1, -1, width, height);
5dcf05ae
JS
56#endif
57
0b4d4194
JS
58 return TRUE;
59#endif
60}
61
8cf3806b 62void MyApp::InitTabView(wxNotebook* notebook, wxWindow* window)
0b4d4194 63{
0b4d4194 64 m_okButton = new wxButton(window, wxID_OK, "Close", wxPoint(-1, -1), wxSize(80, 25));
477350ce
VZ
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));
0b4d4194
JS
69 m_okButton->SetDefault();
70
477350ce 71 wxLayoutConstraints *c;
477350ce
VZ
72 c = new wxLayoutConstraints;
73 c->right.SameAs(window, wxRight, 4);
74 c->bottom.SameAs(window, wxBottom, 4);
75 c->height.AsIs();
76 c->width.AsIs();
77 m_addPageButton->SetConstraints(c);
78
79 c = new wxLayoutConstraints;
33d0e17c
RR
80 c->right.SameAs(m_addPageButton, wxLeft, 4);
81 c->bottom.SameAs(window, wxBottom, 4);
82 c->height.AsIs();
83 c->width.AsIs();
477350ce 84 m_insertPageButton->SetConstraints(c);
33d0e17c
RR
85
86 c = new wxLayoutConstraints;
477350ce 87 c->right.SameAs(m_insertPageButton, wxLeft, 4);
0b4d4194
JS
88 c->bottom.SameAs(window, wxBottom, 4);
89 c->height.AsIs();
90 c->width.AsIs();
477350ce 91 m_nextPageButton->SetConstraints(c);
0b4d4194
JS
92
93 c = new wxLayoutConstraints;
477350ce 94 c->right.SameAs(m_nextPageButton, wxLeft, 4);
0b4d4194
JS
95 c->bottom.SameAs(window, wxBottom, 4);
96 c->height.AsIs();
97 c->width.AsIs();
98 m_cancelButton->SetConstraints(c);
99
100 c = new wxLayoutConstraints;
101 c->right.SameAs(m_cancelButton, wxLeft, 4);
102 c->bottom.SameAs(window, wxBottom, 4);
103 c->height.AsIs();
104 c->width.AsIs();
105 m_okButton->SetConstraints(c);
106
107 // Add some panels
108 wxPanel *panel1 = new wxPanel(notebook, -1);
02800301 109 // panel1->SetBackgroundColour(wxColour("RED"));
0b4d4194
JS
110 (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10));
111 (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
e3e717ec 112
5dcf05ae 113 notebook->AddPage(panel1, "Cat", TRUE);
0b4d4194
JS
114
115 wxPanel *panel2 = new wxPanel(notebook, -1);
e3e717ec 116 panel2->SetAutoLayout(TRUE);
02800301 117 panel2->SetBackgroundColour(wxColour("BLUE"));
0b4d4194
JS
118
119 wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
bb69661b 120 wxRadioBox *radiobox = new wxRadioBox(panel2, -1, "Choose one",
d93c719a
VZ
121 wxDefaultPosition, wxDefaultSize, 5, animals,
122 2, wxRA_SPECIFY_ROWS);
e3e717ec
VZ
123
124 c = new wxLayoutConstraints;
bea56879
VZ
125 c->left.SameAs(panel2, wxLeft, 10);
126 c->top.SameAs(panel2, wxTop, 5);
e3e717ec 127 c->height.PercentOf(panel2, wxHeight, 50);
bea56879 128 c->right.SameAs(panel2, wxRight, 10);
bb69661b 129 radiobox->SetConstraints(c);
0b4d4194 130
bb69661b
VZ
131 wxRadioBox *radiobox2 = new wxRadioBox(panel2, -1, "Choose one",
132 wxDefaultPosition, wxDefaultSize,
133 5, animals,
134 2, wxRA_SPECIFY_ROWS);
6d693bb4 135
e3e717ec 136 c = new wxLayoutConstraints;
bb69661b
VZ
137 c->left.SameAs(radiobox, wxLeft);
138 c->height.AsIs();
bea56879 139 c->top.Below(radiobox, 5);
bb69661b
VZ
140 c->right.SameAs(radiobox, wxRight);
141 radiobox2->SetConstraints(c);
0b4d4194
JS
142
143 notebook->AddPage(panel2, "Dog");
e3e717ec 144
02800301
JS
145 wxPanel *panel3 = new wxPanel(notebook, -1);
146 panel3->SetBackgroundColour(wxColour("WHITE"));
147 notebook->AddPage(panel3, "Goat");
148
149 wxPanel *panel4 = new wxPanel(notebook, -1);
150 panel4->SetBackgroundColour(wxColour("YELLOW"));
151 notebook->AddPage(panel4, "Sheep");
e3e717ec 152
29f538ce
RR
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");
e3e717ec
VZ
157
158 notebook->SetSelection(2);
0b4d4194
JS
159}
160
d22699b5
VZ
161#if USE_TABBED_DIALOG
162
0b4d4194
JS
163BEGIN_EVENT_TABLE(MyDialog, wxDialog)
164 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
165 EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK)
166END_EVENT_TABLE()
167
168MyDialog::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)
171{
172 Init();
173}
174
175void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
176{
177 EndModal(wxID_OK);
178}
179
180void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
181{
182 EndModal(wxID_CANCEL);
183}
184
477350ce 185void MyDialog::Init()
0b4d4194 186{
0b4d4194
JS
187 m_notebook = new wxNotebook(this, ID_NOTEBOOK);
188
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);
194
195 m_notebook->SetConstraints(c);
196
197 wxGetApp().InitTabView(m_notebook, this);
198
199 SetAutoLayout(TRUE);
200 Layout();
201
bb69661b 202 Centre(wxBOTH);
0b4d4194
JS
203}
204
d22699b5
VZ
205#else // USE_TABBED_DIALOG
206
0b4d4194
JS
207BEGIN_EVENT_TABLE(MyFrame, wxFrame)
208 EVT_BUTTON(wxID_OK, MyFrame::OnOK)
29f538ce 209 EVT_BUTTON(ID_DELETE_PAGE, MyFrame::OnDeletePage)
326f9654 210 EVT_BUTTON(ID_ADD_PAGE, MyFrame::OnAddPage)
477350ce 211 EVT_BUTTON(ID_INSERT_PAGE, MyFrame::OnInsertPage)
33d0e17c 212 EVT_BUTTON(ID_NEXT_PAGE, MyFrame::OnNextPage)
96d37807 213 EVT_IDLE(MyFrame::OnIdle)
0b4d4194
JS
214END_EVENT_TABLE()
215
216MyFrame::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)
219{
220 m_panel = (wxPanel*) NULL;
221 m_notebook = (wxNotebook*) NULL;
222 Init();
223}
224
326f9654
RR
225void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
226{
477350ce
VZ
227 static size_t s_pageAdded = 0;
228
229 wxPanel *panel = new wxPanel( m_notebook, -1 );
230 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
231
232 m_notebook->AddPage( panel, wxString::Format("Added %u", ++s_pageAdded) );
233}
234
235void MyFrame::OnInsertPage(wxCommandEvent& WXUNUSED(event))
236{
237 static size_t s_pageIns = 0;
238
239 wxPanel *panel = new wxPanel( m_notebook, -1 );
240 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
241
242 m_notebook->InsertPage(0, panel, wxString::Format("Inserted %u", ++s_pageIns) );
243 m_notebook->SetSelection(0);
326f9654
RR
244}
245
f6bcfd97
BP
246wxWindow *test = NULL;
247
29f538ce
RR
248void MyFrame::OnDeletePage(wxCommandEvent& WXUNUSED(event))
249{
f6bcfd97
BP
250 if (m_notebook->GetPageCount() > 0)
251 m_notebook->DeletePage( m_notebook->GetPageCount()-1 );
252
253/*
254 if (test)
255 {
256 m_notebook->AddPage( test, "Readded" );
257 test = NULL;
258 }
259 else
260 {
261 test = m_notebook->GetPage( m_notebook->GetPageCount()-1 );
262 m_notebook->RemovePage( m_notebook->GetPageCount()-1 );
263 }
264*/
29f538ce
RR
265}
266
33d0e17c
RR
267void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event))
268{
269 m_notebook->AdvanceSelection();
270}
271
0b4d4194
JS
272void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) )
273{
96d37807 274 Destroy();
0b4d4194
JS
275}
276
277void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
278{
96d37807 279 Destroy();
0b4d4194
JS
280}
281
477350ce 282void MyFrame::Init()
0b4d4194 283{
0b4d4194
JS
284 m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxCLIP_CHILDREN);
285
d22699b5
VZ
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);
292
0b4d4194
JS
293 m_notebook = new wxNotebook(m_panel, ID_NOTEBOOK);
294
d22699b5 295 c = new wxLayoutConstraints;
0b4d4194
JS
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);
300
301 m_notebook->SetConstraints(c);
302
303 wxGetApp().InitTabView(m_notebook, m_panel);
304
305 m_panel->SetAutoLayout(TRUE);
d22699b5 306 SetAutoLayout(TRUE);
0b4d4194 307
d22699b5 308 Centre(wxBOTH);
0b4d4194
JS
309
310 Show(TRUE);
311}
312
96d37807
VZ
313void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
314{
315 static int s_nPages = -1;
316 static int s_nSel = -1;
317
318 int nPages = m_notebook->GetPageCount();
319 int nSel = m_notebook->GetSelection();
320 if ( nPages != s_nPages || nSel != s_nSel )
321 {
322 s_nPages = nPages;
323 s_nSel = nSel;
324
325 wxString title;
326 title.Printf("Notebook (%d pages, selection: %d)", nPages, nSel);
327
328 SetTitle(title);
329 }
330}
d22699b5
VZ
331
332#endif // USE_TABBED_DIALOG