]> git.saurik.com Git - wxWidgets.git/blame - samples/notebook/notebook.cpp
Fix use of deprecated wxList/Node methods.
[wxWidgets.git] / samples / notebook / notebook.cpp
CommitLineData
0b4d4194 1/////////////////////////////////////////////////////////////////////////////
c1dfe277
JS
2// Name: samples/notebook/notebook.cpp
3// Purpose: a sample demonstrating notebook usage
0b4d4194 4// Author: Julian Smart
c1dfe277 5// Modified by: Dimitri Schoolwerth
0b4d4194
JS
6// Created: 26/10/98
7// RCS-ID: $Id$
c1dfe277
JS
8// Copyright: (c) 1998-2002 wxWindows team
9// License: wxWindows license
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
c1dfe277
JS
23#include "wx/imaglist.h"
24#include "wx/artprov.h"
b2f757f9 25#include "notebook.h"
0b4d4194 26
0b4d4194
JS
27IMPLEMENT_APP(MyApp)
28
477350ce 29bool MyApp::OnInit()
0b4d4194 30{
c1dfe277
JS
31 // Create the main window
32 MyFrame *frame = new MyFrame( wxT("Notebook sample") );
33
34 // Problem with generic wxNotebook implementation whereby it doesn't size
35 // properly unless you set the size again
37d403aa 36#if defined(__WIN16__) || defined(__WXMOTIF__)
c1dfe277
JS
37 int width, height;
38 frame->GetSize(& width, & height);
39 frame->SetSize(-1, -1, width, height);
5dcf05ae
JS
40#endif
41
c1dfe277
JS
42 frame->Show();
43
44 return TRUE;
0b4d4194
JS
45}
46
c1dfe277
JS
47MyNotebook::MyNotebook(wxWindow *parent, wxWindowID id,
48 const wxPoint& pos, const wxSize& size, long style)
49 : wxNotebook(parent, id, pos, size, style)
0b4d4194 50{
c1dfe277 51 // Empty
0b4d4194
JS
52}
53
76645ab3 54wxPanel *MyNotebook::CreatePage(const wxString&pageName)
c1dfe277 55{
76645ab3
VZ
56 if
57 (
58 pageName.Contains(INSERTED_PAGE_NAME)
59 || pageName.Contains(ADDED_PAGE_NAME)
60 )
61 {
62 return CreateUserCreatedPage();
63 }
d22699b5 64
76645ab3
VZ
65 if (pageName == I_WAS_INSERTED_PAGE_NAME)
66 {
67 return CreateInsertPage();
68 }
c1dfe277 69
76645ab3
VZ
70 if (pageName == VETO_PAGE_NAME)
71 {
72 return CreateVetoPage();
73 }
c1dfe277 74
76645ab3
VZ
75 if (pageName == RADIOBUTTONS_PAGE_NAME)
76 {
77 return CreateRadioButtonsPage();
78 }
c1dfe277 79
c1dfe277 80
76645ab3
VZ
81 if (pageName == MAXIMIZED_BUTTON_PAGE_NAME)
82 {
83 return CreateBigButtonPage();
84 }
c1dfe277 85
76645ab3 86 wxFAIL;
c1dfe277 87
76645ab3
VZ
88 return (wxPanel *) NULL;
89}
90
91wxPanel *MyNotebook::CreateUserCreatedPage()
92{
93 wxPanel *panel = new wxPanel(this);
94
95 (void) new wxButton( panel, -1, wxT("Button"),
96 wxPoint(10, 10), wxSize(-1, -1) );
97
98 return panel;
99}
100
101wxPanel *MyNotebook::CreateRadioButtonsPage()
102{
103 wxPanel *panel = new wxPanel(this);
c1dfe277
JS
104
105 wxString animals[] = { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
106 wxT("Sabre-toothed tiger"), wxT("T Rex") };
76645ab3 107
c1dfe277
JS
108 wxRadioBox *radiobox1 = new wxRadioBox(panel, -1, wxT("Choose one"),
109 wxDefaultPosition, wxDefaultSize, 5, animals, 2, wxRA_SPECIFY_ROWS);
110
111 wxString computers[] = { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
112 wxT("Another") };
76645ab3 113
c1dfe277
JS
114 wxRadioBox *radiobox2 = new wxRadioBox(panel, -1,
115 wxT("Choose your favourite"), wxDefaultPosition, wxDefaultSize,
116 4, computers, 0, wxRA_SPECIFY_COLS);
0b4d4194 117
76645ab3 118 wxBoxSizer *sizerPanel = new wxBoxSizer(wxVERTICAL);
c1dfe277
JS
119 sizerPanel->Add(radiobox1, 2, wxEXPAND);
120 sizerPanel->Add(radiobox2, 1, wxEXPAND);
121 panel->SetSizer(sizerPanel);
122
76645ab3
VZ
123 return panel;
124}
c1dfe277 125
76645ab3
VZ
126wxPanel *MyNotebook::CreateVetoPage()
127{
128 wxPanel *panel = new wxPanel(this);
c1dfe277 129
c1dfe277
JS
130 (void) new wxStaticText( panel, -1,
131 wxT("This page intentionally left blank"), wxPoint(10, 10) );
c1dfe277 132
76645ab3
VZ
133 return panel;
134}
135
136wxPanel *MyNotebook::CreateBigButtonPage()
137{
138 wxPanel *panel = new wxPanel(this);
c1dfe277 139
76645ab3 140 wxButton *buttonBig = new wxButton( panel, -1, wxT("Maximized button"),
c1dfe277
JS
141 wxPoint(0, 0), wxSize(480, 360) );
142
76645ab3 143 wxBoxSizer *sizerPanel = new wxBoxSizer(wxVERTICAL);
c1dfe277
JS
144 sizerPanel->Add(buttonBig, 1, wxEXPAND);
145 panel->SetSizer(sizerPanel);
146
76645ab3
VZ
147 return panel;
148}
149
c1dfe277 150
76645ab3
VZ
151wxPanel *MyNotebook::CreateInsertPage()
152{
153 wxPanel *panel = new wxPanel(this);
c1dfe277 154
c1dfe277
JS
155 panel->SetBackgroundColour( wxColour( wxT("MAROON") ) );
156 (void) new wxStaticText( panel, -1,
157 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
c1dfe277 158
76645ab3
VZ
159 return panel;
160}
161
162void MyNotebook::CreateInitialPages()
163{
164 wxPanel *panel = (wxPanel *) NULL;
165
166 // Create and add some panels to the notebook
167
168 panel = CreateRadioButtonsPage();
169 AddPage( panel, RADIOBUTTONS_PAGE_NAME, FALSE, GetIconIndex() );
170
171 panel = CreateVetoPage();
172 AddPage( panel, VETO_PAGE_NAME, FALSE, GetIconIndex() );
c1dfe277 173
76645ab3
VZ
174 panel = CreateBigButtonPage();
175 AddPage( panel, MAXIMIZED_BUTTON_PAGE_NAME, FALSE, GetIconIndex() );
c1dfe277 176
76645ab3
VZ
177 panel = CreateInsertPage();
178 InsertPage( 0, panel, I_WAS_INSERTED_PAGE_NAME, FALSE, GetIconIndex() );
179
180
181 SetSelection(1);
c1dfe277
JS
182}
183
184int MyNotebook::GetIconIndex() const
0b4d4194 185{
c1dfe277
JS
186 if (m_imageList)
187 {
188 int nImages = m_imageList->GetImageCount();
189 if (nImages > 0)
190 {
191 return GetPageCount() % nImages;
192 }
193 }
194
195 return -1;
0b4d4194
JS
196}
197
c1dfe277
JS
198MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
199 long style)
200 : wxFrame((wxWindow *) NULL, -1, title, pos, size, style)
0b4d4194 201{
c1dfe277
JS
202 m_panel = (wxPanel *) NULL;
203 m_notebook = (MyNotebook *) NULL;
76645ab3
VZ
204
205 // create a dummy image list with a few icons
206 wxSize imageSize(32, 32);
207
208 m_imageList
209 = new wxImageList( imageSize.GetWidth(), imageSize.GetHeight() );
210
211 m_imageList->Add
212 (
213 wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, imageSize)
214 );
215
216 m_imageList->Add
217 (
218 wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, imageSize)
219 );
220
221 m_imageList->Add
222 (
223 wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, imageSize)
224 );
225
226 m_imageList->Add
227 (
228 wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, imageSize)
229 );
c1dfe277
JS
230
231 m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize,
76645ab3 232 wxTAB_TRAVERSAL | wxCLIP_CHILDREN | wxNO_BORDER);
c1dfe277
JS
233
234 // Create remaining controls
235
236 // must be in sync with Orient enum
237 wxString strOrientations[] =
238 {
239 wxT("&Top"),
240 wxT("&Bottom"),
241 wxT("&Left"),
242 wxT("&Right"),
243 };
244
245 wxASSERT_MSG( WXSIZEOF(strOrientations) == ORIENT_MAX,
76645ab3 246 wxT("Forgot to update something") );
c1dfe277
JS
247
248 m_radioOrient = new wxRadioBox
249 (
250 m_panel, ID_RADIO_ORIENT,
251 wxT("&Tab orientation"),
252 wxDefaultPosition, wxDefaultSize,
253 WXSIZEOF(strOrientations), strOrientations,
254 1, wxRA_SPECIFY_COLS
255 );
256
257 m_chkShowImages = new wxCheckBox( m_panel, ID_CHK_SHOWIMAGES,
258 wxT("&Show images") );
259
260 m_btnAddPage = new wxButton( m_panel, ID_BTN_ADD_PAGE, wxT("&Add page") );
261
262 m_btnInsertPage = new wxButton( m_panel, ID_BTN_INSERT_PAGE,
263 wxT("&Insert page") );
264
3957448a
VZ
265 m_btnDeleteCurPage = new wxButton( m_panel, ID_BTN_DELETE_CUR_PAGE,
266 wxT("&Delete current page") );
267
268 m_btnDeleteLastPage = new wxButton( m_panel, ID_BTN_DELETE_LAST_PAGE,
269 wxT("Delete las&t page") );
c1dfe277
JS
270
271 m_btnNextPage = new wxButton( m_panel, ID_BTN_NEXT_PAGE,
272 wxT("&Next page") );
273
274 m_btnExit = new wxButton( m_panel, wxID_OK, wxT("&Exit") );
275 m_btnExit->SetDefault();
276
277 m_notebook = new MyNotebook(m_panel, ID_NOTEBOOK);
278
279 m_text = new wxTextCtrl(m_panel, -1, wxEmptyString,
280 wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY);
281
282 m_logTargetOld = wxLog::SetActiveTarget( new wxLogTextCtrl(m_text) );
283
284 // Create the notebook's panels
285 m_notebook->CreateInitialPages();
286
287 // Set sizers
288 m_sizerFrame = new wxBoxSizer(wxVERTICAL);
289
290 m_sizerTop = new wxBoxSizer(wxHORIZONTAL);
291
292 wxBoxSizer *sizerLeft = new wxBoxSizer(wxVERTICAL);
293 {
294 sizerLeft->Add(m_radioOrient, 0, wxEXPAND);
295 sizerLeft->Add(m_chkShowImages, 0, wxEXPAND | wxTOP, 4);
296
297 sizerLeft->Add(0, 0, 1); // Spacer
298
299 sizerLeft->Add(m_btnAddPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
300 sizerLeft->Add(m_btnInsertPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
3957448a
VZ
301 sizerLeft->Add(m_btnDeleteCurPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
302 sizerLeft->Add(m_btnDeleteLastPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
c1dfe277
JS
303 sizerLeft->Add(m_btnNextPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
304
305 sizerLeft->Add(0, 0, 1); // Spacer
306
307 sizerLeft->Add(m_btnExit, 0, wxEXPAND);
308 }
309
310 m_sizerTop->Add(sizerLeft, 0, wxEXPAND | wxALL, 4);
311
312
313 m_sizerFrame->Add(m_sizerTop, 1, wxEXPAND);
314 m_sizerFrame->Add(m_text, 0, wxEXPAND);
315
316 ReInitNotebook();
317
318 m_panel->SetSizer(m_sizerFrame);
319
320 m_panel->SetAutoLayout(TRUE);
321
322 m_sizerFrame->Fit(this);
323
324 Centre(wxBOTH);
325
0b4d4194
JS
326}
327
c1dfe277 328MyFrame::~MyFrame()
0b4d4194 329{
c1dfe277 330 delete wxLog::SetActiveTarget(m_logTargetOld);
76645ab3
VZ
331
332 if (m_imageList)
333 {
334 delete m_imageList;
335 m_imageList = (wxImageList *) NULL;
336 }
337
0b4d4194
JS
338}
339
c1dfe277 340void MyFrame::ReInitNotebook()
0b4d4194 341{
c1dfe277 342 int flags;
0b4d4194 343
c1dfe277
JS
344 switch ( m_radioOrient->GetSelection() )
345 {
346 default:
347 wxFAIL_MSG( wxT("unknown notebook orientation") );
348 // fall through
0b4d4194 349
c1dfe277
JS
350 case ORIENT_TOP:
351 flags = wxNB_TOP;
352 break;
0b4d4194 353
c1dfe277
JS
354 case ORIENT_BOTTOM:
355 flags = wxNB_BOTTOM;
356 break;
0b4d4194 357
c1dfe277
JS
358 case ORIENT_LEFT:
359 flags = wxNB_LEFT;
360 break;
0b4d4194 361
c1dfe277
JS
362 case ORIENT_RIGHT:
363 flags = wxNB_RIGHT;
364 break;
365 }
366
367 MyNotebook *notebook = m_notebook;
368
c1dfe277
JS
369 m_notebook = new MyNotebook(m_panel, ID_NOTEBOOK,
370 wxDefaultPosition, wxDefaultSize,
371 flags);
372
76645ab3
VZ
373 if ( m_chkShowImages->IsChecked() )
374 {
375 m_notebook->SetImageList(m_imageList);
376 }
c1dfe277 377
76645ab3 378 if (notebook)
c1dfe277
JS
379 {
380 int sel = notebook->GetSelection();
381
382 int count = notebook->GetPageCount();
76645ab3 383 for (int n = 0; n < count; n++)
c1dfe277 384 {
76645ab3 385 wxString str = notebook->GetPageText(n);
c1dfe277 386
76645ab3
VZ
387 wxNotebookPage *page = m_notebook->CreatePage(str);
388 m_notebook->AddPage(page, str, FALSE, m_notebook->GetIconIndex() );
c1dfe277
JS
389 }
390
391 if (m_sizerNotebook)
392 {
393 m_sizerTop->Remove(m_sizerNotebook);
394 }
395
396 delete notebook;
397
398 // restore selection
76645ab3 399 if (sel != -1)
c1dfe277
JS
400 {
401 m_notebook->SetSelection(sel);
402 }
76645ab3 403
c1dfe277
JS
404 }
405
76645ab3 406
c1dfe277
JS
407 m_sizerNotebook = new wxNotebookSizer(m_notebook);
408 m_sizerTop->Add(m_sizerNotebook, 1, wxEXPAND | wxALL, 4);
409 m_sizerTop->Layout();
410}
411
0b4d4194 412BEGIN_EVENT_TABLE(MyFrame, wxFrame)
c1dfe277
JS
413 EVT_RADIOBOX(ID_RADIO_ORIENT, MyFrame::OnCheckOrRadioBox)
414 EVT_CHECKBOX(ID_CHK_SHOWIMAGES, MyFrame::OnCheckOrRadioBox)
415
416 EVT_BUTTON(ID_BTN_ADD_PAGE, MyFrame::OnButtonAddPage)
417 EVT_BUTTON(ID_BTN_INSERT_PAGE, MyFrame::OnButtonInsertPage)
3957448a
VZ
418 EVT_BUTTON(ID_BTN_DELETE_CUR_PAGE, MyFrame::OnButtonDeleteCurPage)
419 EVT_BUTTON(ID_BTN_DELETE_LAST_PAGE, MyFrame::OnButtonDeleteLastPage)
c1dfe277
JS
420 EVT_BUTTON(ID_BTN_NEXT_PAGE, MyFrame::OnButtonNextPage)
421 EVT_BUTTON(wxID_OK, MyFrame::OnButtonExit)
422
3957448a
VZ
423 EVT_UPDATE_UI(ID_BTN_DELETE_CUR_PAGE, MyFrame::OnUpdateUIBtnDeleteCurPage)
424 EVT_UPDATE_UI(ID_BTN_DELETE_LAST_PAGE, MyFrame::OnUpdateUIBtnDeleteLastPage)
425
c1dfe277
JS
426 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK, MyFrame::OnNotebook)
427 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK, MyFrame::OnNotebook)
428
96d37807 429 EVT_IDLE(MyFrame::OnIdle)
0b4d4194
JS
430END_EVENT_TABLE()
431
c1dfe277 432void MyFrame::OnCheckOrRadioBox(wxCommandEvent& event)
0b4d4194 433{
c1dfe277 434 ReInitNotebook();
0b4d4194
JS
435}
436
c1dfe277 437void MyFrame::OnButtonAddPage( wxCommandEvent& WXUNUSED(event) )
326f9654 438{
477350ce
VZ
439 static size_t s_pageAdded = 0;
440
441 wxPanel *panel = new wxPanel( m_notebook, -1 );
85b43fbf 442 (void) new wxButton( panel, -1, wxT("First button"),
c1dfe277 443 wxPoint(10, 10), wxSize(-1, -1) );
2b5f62a0
VZ
444 (void) new wxButton( panel, -1, wxT("Second button"),
445 wxPoint(50, 100), wxSize(-1, -1) );
477350ce 446
76645ab3 447 m_notebook->AddPage(panel, wxString::Format(ADDED_PAGE_NAME wxT("%u"),
2b5f62a0 448 ++s_pageAdded), TRUE, m_notebook->GetIconIndex() );
477350ce
VZ
449}
450
c1dfe277 451void MyFrame::OnButtonInsertPage( wxCommandEvent& WXUNUSED(event) )
477350ce
VZ
452{
453 static size_t s_pageIns = 0;
454
76645ab3 455 wxPanel *panel = m_notebook->CreateUserCreatedPage();
c1dfe277
JS
456
457 m_notebook->InsertPage( 0, panel,
76645ab3 458 wxString::Format(INSERTED_PAGE_NAME wxT("%u"), ++s_pageIns), FALSE,
c1dfe277 459 m_notebook->GetIconIndex() );
477350ce 460
477350ce 461 m_notebook->SetSelection(0);
326f9654
RR
462}
463
3957448a
VZ
464void MyFrame::OnButtonDeleteLastPage( wxCommandEvent& WXUNUSED(event) )
465{
466 int page = m_notebook->GetPageCount();
467
468 if ( page != 0 )
469 {
470 m_notebook->DeletePage(page - 1);
471 }
472}
473
474void MyFrame::OnButtonDeleteCurPage( wxCommandEvent& WXUNUSED(event) )
29f538ce 475{
c1dfe277 476 int sel = m_notebook->GetSelection();
f6bcfd97 477
c1dfe277 478 if (sel != -1)
f6bcfd97 479 {
c1dfe277 480 m_notebook->DeletePage(sel);
f6bcfd97 481 }
29f538ce
RR
482}
483
c1dfe277 484void MyFrame::OnButtonNextPage( wxCommandEvent& WXUNUSED(event) )
33d0e17c
RR
485{
486 m_notebook->AdvanceSelection();
487}
488
c1dfe277 489void MyFrame::OnButtonExit( wxCommandEvent& WXUNUSED(event) )
0b4d4194 490{
c1dfe277 491 Close();
0b4d4194
JS
492}
493
c1dfe277 494void MyFrame::OnNotebook(wxNotebookEvent& event)
0b4d4194 495{
c1dfe277 496 wxString str = wxT("Unknown notebook event");
d22699b5 497
c1dfe277 498 wxEventType eventType = event.GetEventType();
0b4d4194 499
c1dfe277
JS
500 if (eventType == wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
501 {
502 str = wxT("Notebook changed");
503 }
504 else if (eventType == wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
505 {
506 int idx = event.GetOldSelection();
507 if ( idx != -1 && m_notebook->GetPageText(idx) == VETO_PAGE_NAME )
508 {
509 if
510 (
511 wxMessageBox(
512 wxT("Are you sure you want to leave this notebook page?\n")
513 wxT("(This demonstrates veto-ing)"),
514 wxT("Notebook sample"),
515 wxICON_QUESTION | wxYES_NO, this) != wxYES )
516 {
517 event.Veto();
518
519 return;
520 }
521
522 }
523
524 str = wxT("Notebook changing");
525 }
0b4d4194 526
c1dfe277 527 static int s_numNotebookEvents = 0;
0b4d4194 528
c1dfe277 529 wxLogMessage(wxT("Notebook event #%d: %s (%d)"),
44e1a03f 530 s_numNotebookEvents++, str.c_str(), eventType);
0b4d4194 531
c1dfe277 532 m_text->SetInsertionPointEnd();
0b4d4194 533
c1dfe277 534 event.Skip();
0b4d4194
JS
535}
536
c1dfe277 537void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
96d37807
VZ
538{
539 static int s_nPages = -1;
540 static int s_nSel = -1;
541
542 int nPages = m_notebook->GetPageCount();
543 int nSel = m_notebook->GetSelection();
544 if ( nPages != s_nPages || nSel != s_nSel )
545 {
546 s_nPages = nPages;
547 s_nSel = nSel;
548
549 wxString title;
c1dfe277 550 title.Printf(wxT("Notebook (%d pages, selection: %d)"), nPages, nSel);
96d37807
VZ
551
552 SetTitle(title);
553 }
554}
3957448a
VZ
555
556void MyFrame::OnUpdateUIBtnDeleteCurPage(wxUpdateUIEvent& event)
557{
558 event.Enable( m_notebook->GetSelection() != -1 );
559}
560
561void MyFrame::OnUpdateUIBtnDeleteLastPage(wxUpdateUIEvent& event)
562{
563 event.Enable( m_notebook->GetPageCount() != 0 );
564}