]> git.saurik.com Git - wxWidgets.git/blame - samples/notebook/notebook.cpp
TrueType support for Canvas.
[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
b2f757f9 23#include "notebook.h"
0b4d4194
JS
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
8e97b17b 46 frame = new MyFrame((wxFrame*) NULL, -1, "Notebook", wxPoint(-1, -1), wxSize(465, 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 69 wxLayoutConstraints *c;
477350ce
VZ
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;
33d0e17c
RR
78 c->right.SameAs(m_addPageButton, wxLeft, 4);
79 c->bottom.SameAs(window, wxBottom, 4);
80 c->height.AsIs();
81 c->width.AsIs();
477350ce 82 m_insertPageButton->SetConstraints(c);
33d0e17c
RR
83
84 c = new wxLayoutConstraints;
477350ce 85 c->right.SameAs(m_insertPageButton, wxLeft, 4);
0b4d4194
JS
86 c->bottom.SameAs(window, wxBottom, 4);
87 c->height.AsIs();
88 c->width.AsIs();
477350ce 89 m_nextPageButton->SetConstraints(c);
0b4d4194
JS
90
91 c = new wxLayoutConstraints;
477350ce 92 c->right.SameAs(m_nextPageButton, wxLeft, 4);
0b4d4194
JS
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);
02800301 107 // panel1->SetBackgroundColour(wxColour("RED"));
0b4d4194
JS
108 (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10));
109 (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
e3e717ec 110
5dcf05ae 111 notebook->AddPage(panel1, "Cat", TRUE);
0b4d4194
JS
112
113 wxPanel *panel2 = new wxPanel(notebook, -1);
e3e717ec 114 panel2->SetAutoLayout(TRUE);
02800301 115 panel2->SetBackgroundColour(wxColour("BLUE"));
0b4d4194
JS
116
117 wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
bb69661b 118 wxRadioBox *radiobox = new wxRadioBox(panel2, -1, "Choose one",
d93c719a
VZ
119 wxDefaultPosition, wxDefaultSize, 5, animals,
120 2, wxRA_SPECIFY_ROWS);
e3e717ec
VZ
121
122 c = new wxLayoutConstraints;
bea56879
VZ
123 c->left.SameAs(panel2, wxLeft, 10);
124 c->top.SameAs(panel2, wxTop, 5);
e3e717ec 125 c->height.PercentOf(panel2, wxHeight, 50);
bea56879 126 c->right.SameAs(panel2, wxRight, 10);
bb69661b 127 radiobox->SetConstraints(c);
0b4d4194 128
bb69661b
VZ
129 wxRadioBox *radiobox2 = new wxRadioBox(panel2, -1, "Choose one",
130 wxDefaultPosition, wxDefaultSize,
131 5, animals,
132 2, wxRA_SPECIFY_ROWS);
6d693bb4 133
e3e717ec 134 c = new wxLayoutConstraints;
bb69661b
VZ
135 c->left.SameAs(radiobox, wxLeft);
136 c->height.AsIs();
bea56879 137 c->top.Below(radiobox, 5);
bb69661b
VZ
138 c->right.SameAs(radiobox, wxRight);
139 radiobox2->SetConstraints(c);
0b4d4194
JS
140
141 notebook->AddPage(panel2, "Dog");
e3e717ec 142
02800301
JS
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");
e3e717ec 150
29f538ce
RR
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");
e3e717ec
VZ
155
156 notebook->SetSelection(2);
0b4d4194
JS
157}
158
d22699b5
VZ
159#if USE_TABBED_DIALOG
160
0b4d4194
JS
161BEGIN_EVENT_TABLE(MyDialog, wxDialog)
162 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
163 EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK)
164END_EVENT_TABLE()
165
166MyDialog::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
173void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
174{
175 EndModal(wxID_OK);
176}
177
178void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
179{
180 EndModal(wxID_CANCEL);
181}
182
477350ce 183void MyDialog::Init()
0b4d4194 184{
0b4d4194
JS
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
bb69661b 200 Centre(wxBOTH);
0b4d4194
JS
201}
202
d22699b5
VZ
203#else // USE_TABBED_DIALOG
204
0b4d4194
JS
205BEGIN_EVENT_TABLE(MyFrame, wxFrame)
206 EVT_BUTTON(wxID_OK, MyFrame::OnOK)
29f538ce 207 EVT_BUTTON(ID_DELETE_PAGE, MyFrame::OnDeletePage)
326f9654 208 EVT_BUTTON(ID_ADD_PAGE, MyFrame::OnAddPage)
477350ce 209 EVT_BUTTON(ID_INSERT_PAGE, MyFrame::OnInsertPage)
33d0e17c 210 EVT_BUTTON(ID_NEXT_PAGE, MyFrame::OnNextPage)
96d37807 211 EVT_IDLE(MyFrame::OnIdle)
0b4d4194
JS
212END_EVENT_TABLE()
213
214MyFrame::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
326f9654
RR
223void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
224{
477350ce
VZ
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
233void 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);
326f9654
RR
242}
243
f6bcfd97
BP
244wxWindow *test = NULL;
245
29f538ce
RR
246void MyFrame::OnDeletePage(wxCommandEvent& WXUNUSED(event))
247{
f6bcfd97
BP
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*/
29f538ce
RR
263}
264
33d0e17c
RR
265void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event))
266{
267 m_notebook->AdvanceSelection();
268}
269
0b4d4194
JS
270void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) )
271{
96d37807 272 Destroy();
0b4d4194
JS
273}
274
275void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
276{
96d37807 277 Destroy();
0b4d4194
JS
278}
279
477350ce 280void MyFrame::Init()
0b4d4194 281{
0b4d4194
JS
282 m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxCLIP_CHILDREN);
283
d22699b5
VZ
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
0b4d4194
JS
291 m_notebook = new wxNotebook(m_panel, ID_NOTEBOOK);
292
d22699b5 293 c = new wxLayoutConstraints;
0b4d4194
JS
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);
d22699b5 304 SetAutoLayout(TRUE);
0b4d4194 305
d22699b5 306 Centre(wxBOTH);
0b4d4194
JS
307
308 Show(TRUE);
309}
310
96d37807
VZ
311void 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}
d22699b5
VZ
329
330#endif // USE_TABBED_DIALOG