compilation fixes for wxUSE_TREEBOOK=0
[wxWidgets.git] / samples / notebook / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/notebook/notebook.cpp
3 // Purpose: a sample demonstrating notebook usage
4 // Author: Julian Smart
5 // Modified by: Dimitri Schoolwerth
6 // Created: 26/10/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998-2002 wxWidgets team
9 // License: wxWindows license
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 "wx/imaglist.h"
24 #include "wx/artprov.h"
25 #include "notebook.h"
26
27 #if !defined(__WXMSW__) && !defined(__WXPM__)
28 #include "../sample.xpm"
29 #endif
30
31 IMPLEMENT_APP(MyApp)
32
33 bool MyApp::OnInit()
34 {
35 // Create the main window
36 MyFrame *frame = new MyFrame();
37
38 // Problem with generic wxNotebook implementation whereby it doesn't size
39 // properly unless you set the size again
40 #if defined(__WXMOTIF__)
41 int width, height;
42 frame->GetSize(& width, & height);
43 frame->SetSize(wxDefaultCoord, wxDefaultCoord, width, height);
44 #endif
45
46 frame->Show();
47
48 return true;
49 }
50
51 wxPanel *CreateUserCreatedPage(wxBookCtrlBase *parent)
52 {
53 wxPanel *panel = new wxPanel(parent);
54
55 (void) new wxButton( panel, wxID_ANY, wxT("Button"),
56 wxPoint(10, 10), wxDefaultSize );
57
58 return panel;
59 }
60
61 wxPanel *CreateRadioButtonsPage(wxBookCtrlBase *parent)
62 {
63 wxPanel *panel = new wxPanel(parent);
64
65 wxString animals[] = { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
66 wxT("Sabre-toothed tiger"), wxT("T Rex") };
67
68 wxRadioBox *radiobox1 = new wxRadioBox(panel, wxID_ANY, wxT("Choose one"),
69 wxDefaultPosition, wxDefaultSize, 5, animals, 2, wxRA_SPECIFY_ROWS);
70
71 wxString computers[] = { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
72 wxT("Another") };
73
74 wxRadioBox *radiobox2 = new wxRadioBox(panel, wxID_ANY,
75 wxT("Choose your favourite"), wxDefaultPosition, wxDefaultSize,
76 4, computers, 0, wxRA_SPECIFY_COLS);
77
78 wxBoxSizer *sizerPanel = new wxBoxSizer(wxVERTICAL);
79 sizerPanel->Add(radiobox1, 2, wxEXPAND);
80 sizerPanel->Add(radiobox2, 1, wxEXPAND);
81 panel->SetSizer(sizerPanel);
82
83 return panel;
84 }
85
86 wxPanel *CreateVetoPage(wxBookCtrlBase *parent)
87 {
88 wxPanel *panel = new wxPanel(parent);
89
90 (void) new wxStaticText( panel, wxID_ANY,
91 wxT("This page intentionally left blank"), wxPoint(10, 10) );
92
93 return panel;
94 }
95
96 wxPanel *CreateBigButtonPage(wxBookCtrlBase *parent)
97 {
98 wxPanel *panel = new wxPanel(parent);
99
100 wxButton *buttonBig = new wxButton(panel, wxID_ANY, wxT("Maximized button"));
101
102 wxBoxSizer *sizerPanel = new wxBoxSizer(wxVERTICAL);
103 sizerPanel->Add(buttonBig, 1, wxEXPAND);
104 panel->SetSizer(sizerPanel);
105
106 return panel;
107 }
108
109
110 wxPanel *CreateInsertPage(wxBookCtrlBase *parent)
111 {
112 wxPanel *panel = new wxPanel(parent);
113
114 panel->SetBackgroundColour( wxColour( wxT("MAROON") ) );
115 (void) new wxStaticText( panel, wxID_ANY,
116 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
117
118 return panel;
119 }
120
121 int GetIconIndex(wxBookCtrlBase* bookCtrl)
122 {
123 if (bookCtrl && bookCtrl->GetImageList())
124 {
125 int nImages = bookCtrl->GetImageList()->GetImageCount();
126 if (nImages > 0)
127 {
128 return bookCtrl->GetPageCount() % nImages;
129 }
130 }
131
132 return -1;
133 }
134
135 void CreateInitialPages(wxBookCtrlBase *parent)
136 {
137 // Create and add some panels to the notebook
138
139 wxPanel *panel = CreateRadioButtonsPage(parent);
140 parent->AddPage( panel, RADIOBUTTONS_PAGE_NAME, false, GetIconIndex(parent) );
141
142 panel = CreateVetoPage(parent);
143 parent->AddPage( panel, VETO_PAGE_NAME, false, GetIconIndex(parent) );
144
145 panel = CreateBigButtonPage(parent);
146 parent->AddPage( panel, MAXIMIZED_BUTTON_PAGE_NAME, false, GetIconIndex(parent) );
147
148 panel = CreateInsertPage(parent);
149 parent->InsertPage( 0, panel, I_WAS_INSERTED_PAGE_NAME, false, GetIconIndex(parent) );
150
151 parent->SetSelection(1);
152 }
153
154 wxPanel *CreatePage(wxBookCtrlBase *parent, const wxString&pageName)
155 {
156 if ( pageName.Contains(INSERTED_PAGE_NAME) ||
157 pageName.Contains(ADDED_PAGE_NAME) ||
158 pageName.Contains(ADDED_SUB_PAGE_NAME) ||
159 pageName.Contains(ADDED_PAGE_NAME_BEFORE) )
160 return CreateUserCreatedPage(parent);
161
162 if ( pageName == I_WAS_INSERTED_PAGE_NAME )
163 return CreateInsertPage(parent);
164
165 if ( pageName == VETO_PAGE_NAME )
166 return CreateVetoPage(parent);
167
168 if ( pageName == RADIOBUTTONS_PAGE_NAME )
169 return CreateRadioButtonsPage(parent);
170
171 if ( pageName == MAXIMIZED_BUTTON_PAGE_NAME )
172 return CreateBigButtonPage(parent);
173
174 wxFAIL_MSG( _T("unknown page name") );
175
176 return NULL;
177 }
178
179 MyFrame::MyFrame()
180 : wxFrame(NULL, wxID_ANY, wxString(wxT("wxWidgets book controls sample")))
181 {
182 #if wxUSE_NOTEBOOK
183 m_type = Type_Notebook;
184 #elif wxUSE_CHOICEBOOK
185 m_type = Type_Choicebook;
186 #elif wxUSE_LISTBOOK
187 m_type = Type_Listbook;
188 #elif wxUSE_TREEBOOK
189 m_type = Type_Treebook;
190 #elif
191 #error "Don't use Notebook sample without any book enabled in wxWidgets build!"
192 #endif
193
194 m_orient = ID_ORIENT_DEFAULT;
195 m_chkShowImages = true;
196 m_multi = false;
197
198 SetIcon(wxICON(sample));
199
200 // menu of the sample
201 wxMenu *menuType = new wxMenu;
202 #if wxUSE_NOTEBOOK
203 menuType->AppendRadioItem(ID_BOOK_NOTEBOOK, wxT("&Notebook\tCtrl-1"));
204 #endif
205 #if wxUSE_LISTBOOK
206 menuType->AppendRadioItem(ID_BOOK_LISTBOOK, wxT("&Listbook\tCtrl-2"));
207 #endif
208 #if wxUSE_CHOICEBOOK
209 menuType->AppendRadioItem(ID_BOOK_CHOICEBOOK, wxT("&Choicebook\tCtrl-3"));
210 #endif
211 #if wxUSE_TREEBOOK
212 menuType->AppendRadioItem(ID_BOOK_TREEBOOK, wxT("&Treebook\tCtrl-4"));
213 #endif
214
215 menuType->Check(ID_BOOK_NOTEBOOK + m_type, true);
216
217 wxMenu *menuOrient = new wxMenu;
218 menuOrient->AppendRadioItem(ID_ORIENT_DEFAULT, wxT("&Default\tCtrl-5"));
219 menuOrient->AppendRadioItem(ID_ORIENT_TOP, wxT("&Top\tCtrl-6"));
220 menuOrient->AppendRadioItem(ID_ORIENT_BOTTOM, wxT("&Bottom\tCtrl-7"));
221 menuOrient->AppendRadioItem(ID_ORIENT_LEFT, wxT("&Left\tCtrl-8"));
222 menuOrient->AppendRadioItem(ID_ORIENT_RIGHT, wxT("&Right\tCtrl-9"));
223
224 wxMenu *menuOperations = new wxMenu;
225 menuOperations->Append(ID_ADD_PAGE, wxT("&Add page\tAlt-A"));
226 menuOperations->Append(ID_INSERT_PAGE, wxT("&Insert page\tAlt-I"));
227 menuOperations->Append(ID_DELETE_CUR_PAGE, wxT("&Delete current page\tAlt-D"));
228 menuOperations->Append(ID_DELETE_LAST_PAGE, wxT("D&elete last page\tAlt-L"));
229 menuOperations->Append(ID_NEXT_PAGE, wxT("&Next page\tAlt-N"));
230 #if wxUSE_TREEBOOK
231 menuOperations->AppendSeparator();
232 menuOperations->Append(ID_ADD_PAGE_BEFORE, wxT("Insert page &before\tAlt-B"));
233 menuOperations->Append(ID_ADD_SUB_PAGE, wxT("Add s&ub page\tAlt-U"));
234 #endif
235
236 wxMenu *menuFile = new wxMenu;
237 menuFile->Append(wxID_ANY, wxT("&Type"), menuType, wxT("Type of control"));
238 menuFile->Append(wxID_ANY, wxT("&Orientation"), menuOrient, wxT("Orientation of control"));
239 menuFile->AppendCheckItem(ID_SHOW_IMAGES, wxT("&Show images\tAlt-S"));
240 menuFile->AppendCheckItem(ID_MULTI, wxT("&Multiple lines\tAlt-M"));
241 menuFile->AppendSeparator();
242 menuFile->Append(wxID_EXIT, wxT("E&xit"), wxT("Quits the application"));
243 menuFile->Check(ID_SHOW_IMAGES, m_chkShowImages);
244 menuFile->Check(ID_MULTI, m_multi);
245
246 wxMenuBar *menuBar = new wxMenuBar;
247 menuBar->Append(menuFile, wxT("&File"));
248 menuBar->Append(menuOperations, wxT("&Operations"));
249 SetMenuBar(menuBar);
250
251 // books creation
252 m_panel = NULL;
253 m_bookCtrl = NULL;
254
255 // create a dummy image list with a few icons
256 const wxSize imageSize(32, 32);
257
258 m_imageList = new wxImageList(imageSize.GetWidth(), imageSize.GetHeight());
259 m_imageList->
260 Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, imageSize));
261 m_imageList->
262 Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, imageSize));
263 m_imageList->
264 Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, imageSize));
265 m_imageList->
266 Add(wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, imageSize));
267
268 m_panel = new wxPanel(this);
269
270 #if USE_LOG
271 m_text = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
272 wxDefaultPosition, wxDefaultSize,
273 wxTE_MULTILINE | wxTE_READONLY);
274
275 m_logTargetOld = wxLog::SetActiveTarget( new wxLogTextCtrl(m_text) );
276 #endif // USE_LOG
277
278 // Set sizers
279 m_sizerFrame = new wxBoxSizer(wxVERTICAL);
280
281 #if USE_LOG
282 m_sizerFrame->Add(m_text, 1, wxEXPAND);
283 #endif // USE_LOG
284
285 RecreateBook();
286
287 m_panel->SetSizer(m_sizerFrame);
288
289 m_sizerFrame->Fit(this);
290 m_sizerFrame->SetSizeHints(this);
291
292 Centre(wxBOTH);
293 }
294
295 MyFrame::~MyFrame()
296 {
297 #if USE_LOG
298 delete wxLog::SetActiveTarget(m_logTargetOld);
299 #endif // USE_LOG
300
301 delete m_imageList;
302 }
303
304 // DISPATCH_ON_TYPE() macro is an ugly way to write the "same" code for
305 // different wxBookCtrlBase-derived classes without duplicating code and
306 // without using templates, it expands into "before <xxx> after" where "xxx"
307 // part is control class-specific
308 #if wxUSE_NOTEBOOK
309 #define CASE_NOTEBOOK(x) case Type_Notebook: x; break;
310 #define FLAG_NOTEBOOK(x) wxNB_##x
311 #else
312 #define CASE_NOTEBOOK(x)
313 #define FLAG_NOTEBOOK(x) 0
314 #endif
315
316 #if wxUSE_LISTBOOK
317 #define CASE_LISTBOOK(x) case Type_Listbook: x; break;
318 #define FLAG_LISTBOOK(x) wxLB_##x
319 #else
320 #define CASE_LISTBOOK(x)
321 #define FLAG_LISTBOOK(x) 0
322 #endif
323
324 #if wxUSE_CHOICEBOOK
325 #define CASE_CHOICEBOOK(x) case Type_Choicebook: x; break;
326 #define FLAG_CHOICEBOOK(x) wxCHB_##x
327 #else
328 #define CASE_CHOICEBOOK(x)
329 #define FLAG_CHOICEBOOK(x) 0
330 #endif
331
332 #if wxUSE_TREEBOOK
333 #define CASE_TREEBOOK(x) case Type_Treebook: x; break;
334 #define FLAG_TREEBOOK(x) wxTBK_##x
335 #else
336 #define CASE_TREEBOOK(x)
337 #define FLAG_TREEBOOK(x) 0
338 #endif
339
340 #define DISPATCH_ON_TYPE(before, nb, lb, cb, tb, after) \
341 switch ( m_type ) \
342 { \
343 CASE_NOTEBOOK(before nb after) \
344 CASE_LISTBOOK(before lb after) \
345 CASE_CHOICEBOOK(before cb after) \
346 CASE_TREEBOOK(before tb after) \
347 \
348 default: \
349 wxFAIL_MSG( _T("unknown book control type") ); \
350 }
351
352 int MyFrame::TranslateBookFlag(int nb, int lb, int chb, int tbk) const
353 {
354 int flag = 0;
355
356 DISPATCH_ON_TYPE(flag =, nb, lb, chb, tbk, + 0);
357
358 return flag;
359 }
360
361 void MyFrame::RecreateBook()
362 {
363 #define SELECT_FLAG(f) \
364 TranslateBookFlag(FLAG_NOTEBOOK(f), FLAG_LISTBOOK(f), FLAG_CHOICEBOOK(f), FLAG_TREEBOOK(f))
365
366 int flags;
367 switch ( m_orient )
368 {
369 case ID_ORIENT_TOP:
370 flags = SELECT_FLAG(TOP);
371 break;
372
373 case ID_ORIENT_BOTTOM:
374 flags = SELECT_FLAG(BOTTOM);
375 break;
376
377 case ID_ORIENT_LEFT:
378 flags = SELECT_FLAG(LEFT);
379 break;
380
381 case ID_ORIENT_RIGHT:
382 flags = SELECT_FLAG(RIGHT);
383 break;
384
385 default:
386 flags = SELECT_FLAG(DEFAULT);
387 }
388
389 #undef SELECT_FLAG
390
391 if ( m_multi && m_type == Type_Notebook )
392 flags |= wxNB_MULTILINE;
393 flags |= wxDOUBLE_BORDER;
394
395 wxBookCtrlBase *oldBook = m_bookCtrl;
396
397 m_bookCtrl = NULL;
398
399 DISPATCH_ON_TYPE(m_bookCtrl = new,
400 wxNotebook,
401 wxListbook,
402 wxChoicebook,
403 wxTreebook,
404 (m_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, flags));
405
406 if ( !m_bookCtrl )
407 return;
408
409 m_bookCtrl->Hide();
410
411 if ( m_chkShowImages )
412 {
413 m_bookCtrl->SetImageList(m_imageList);
414 }
415
416 if ( oldBook )
417 {
418 #if wxUSE_TREEBOOK
419 // we only need the old treebook if we're recreating another treebook
420 wxTreebook *tbkOld = m_type == Type_Treebook
421 ? wxDynamicCast(oldBook, wxTreebook)
422 : NULL;
423 #endif // wxUSE_TREEBOOK
424
425 const int count = oldBook->GetPageCount();
426 for ( int n = 0; n < count; n++ )
427 {
428 const int image = GetIconIndex(m_bookCtrl);
429 const wxString str = oldBook->GetPageText(n);
430
431 wxWindow *page = CreatePage(m_bookCtrl, str);
432
433 // treebook complication: need to account for possible parent page
434 #if wxUSE_TREEBOOK
435 if ( tbkOld )
436 {
437 const int parent = tbkOld->GetPageParent(n);
438 if ( parent != wxNOT_FOUND )
439 {
440 wxStaticCast(m_bookCtrl, wxTreebook)->
441 AddSubPage(parent, page, str, false, image);
442
443 // skip adding it again below
444 continue;
445 }
446 }
447 #endif // wxUSE_TREEBOOK
448
449 m_bookCtrl->AddPage(page, str, false, image);
450 }
451
452 const int sel = oldBook->GetSelection();
453 if ( sel != wxNOT_FOUND )
454 m_bookCtrl->SetSelection(sel);
455
456
457 m_sizerFrame->Detach(oldBook);
458 delete oldBook;
459 }
460 else // no old book
461 {
462 CreateInitialPages(m_bookCtrl);
463 }
464
465 m_sizerFrame->Insert(0, m_bookCtrl, wxSizerFlags(5).Expand().Border());
466
467 m_sizerFrame->Show(m_bookCtrl);
468 m_sizerFrame->Layout();
469 }
470
471 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
472 // File menu
473 EVT_MENU_RANGE(ID_BOOK_NOTEBOOK, ID_BOOK_MAX, MyFrame::OnType)
474 EVT_MENU_RANGE(ID_ORIENT_DEFAULT, ID_ORIENT_MAX, MyFrame::OnOrient)
475 EVT_MENU(ID_SHOW_IMAGES, MyFrame::OnShowImages)
476 EVT_MENU(ID_MULTI, MyFrame::OnMulti)
477 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
478
479 // Operations menu
480 EVT_MENU(ID_ADD_PAGE, MyFrame::OnAddPage)
481 EVT_MENU(ID_INSERT_PAGE, MyFrame::OnInsertPage)
482 EVT_MENU(ID_DELETE_CUR_PAGE, MyFrame::OnDeleteCurPage)
483 EVT_MENU(ID_DELETE_LAST_PAGE, MyFrame::OnDeleteLastPage)
484 EVT_MENU(ID_NEXT_PAGE, MyFrame::OnNextPage)
485
486 // Book controls
487 #if wxUSE_NOTEBOOK
488 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnNotebook)
489 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnNotebook)
490 #endif
491 #if wxUSE_LISTBOOK
492 EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnListbook)
493 EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnListbook)
494 #endif
495 #if wxUSE_CHOICEBOOK
496 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnChoicebook)
497 EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnChoicebook)
498 #endif
499 #if wxUSE_TREEBOOK
500 EVT_TREEBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnTreebook)
501 EVT_TREEBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnTreebook)
502
503 EVT_MENU(ID_ADD_SUB_PAGE, MyFrame::OnAddSubPage)
504 EVT_MENU(ID_ADD_PAGE_BEFORE, MyFrame::OnAddPageBefore)
505 EVT_UPDATE_UI_RANGE(ID_ADD_PAGE_BEFORE, ID_ADD_SUB_PAGE,
506 MyFrame::OnUpdateTreeMenu)
507 #endif
508
509 // Update title in idle time
510 EVT_IDLE(MyFrame::OnIdle)
511 END_EVENT_TABLE()
512
513 void MyFrame::OnType(wxCommandEvent& event)
514 {
515 m_type = wx_static_cast(BookType, event.GetId() - ID_BOOK_NOTEBOOK);
516
517 if ( m_bookCtrl )
518 m_sizerFrame->Hide(m_bookCtrl);
519
520 RecreateBook();
521 }
522
523 #if wxUSE_TREEBOOK
524
525 void MyFrame::OnUpdateTreeMenu(wxUpdateUIEvent& event)
526 {
527 event.Enable(m_type == Type_Treebook);
528 }
529
530 #endif // wxUSE_TREEBOOK
531
532
533 void MyFrame::OnOrient(wxCommandEvent& event)
534 {
535 m_orient = event.GetId();
536 RecreateBook();
537 m_sizerFrame->Layout();
538 }
539
540 void MyFrame::OnShowImages(wxCommandEvent& event)
541 {
542 m_chkShowImages = event.IsChecked();
543 RecreateBook();
544 m_sizerFrame->Layout();
545 }
546
547 void MyFrame::OnMulti(wxCommandEvent& event)
548 {
549 m_multi = event.IsChecked();
550 RecreateBook();
551 m_sizerFrame->Layout();
552 wxLogMessage(_T("Multiline setting works only in wxNotebook."));
553 }
554
555 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
556 {
557 Close();
558 }
559
560 wxPanel *MyFrame::CreateNewPage() const
561 {
562 wxPanel *panel = new wxPanel(m_bookCtrl, wxID_ANY );
563 (void) new wxButton(panel, wxID_ANY, wxT("First button"), wxPoint(10, 10));
564 (void) new wxButton(panel, wxID_ANY, wxT("Second button"), wxPoint(50, 100));
565
566 return panel;
567 }
568
569 void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
570 {
571 wxBookCtrlBase *currBook = GetCurrentBook();
572
573 if ( currBook )
574 {
575 static unsigned s_pageAdded = 0;
576 currBook->AddPage(CreateNewPage(),
577 wxString::Format
578 (
579 ADDED_PAGE_NAME wxT("%u"),
580 ++s_pageAdded
581 ),
582 true,
583 GetIconIndex(currBook));
584 }
585 }
586
587 #if wxUSE_TREEBOOK
588 void MyFrame::OnAddSubPage(wxCommandEvent& WXUNUSED(event))
589 {
590 wxTreebook *currBook = wxDynamicCast(GetCurrentBook(), wxTreebook);
591 if ( currBook )
592 {
593 const int selPos = currBook->GetSelection();
594 if ( selPos == wxNOT_FOUND )
595 {
596 wxLogError(_T("Select the parent page first!"));
597 return;
598 }
599
600 static unsigned s_subPageAdded = 0;
601 currBook->AddSubPage(selPos,
602 CreateNewPage(),
603 wxString::Format
604 (
605 ADDED_SUB_PAGE_NAME wxT("%u"),
606 ++s_subPageAdded
607 ),
608 true,
609 GetIconIndex(currBook));
610 }
611 }
612
613 void MyFrame::OnAddPageBefore(wxCommandEvent& WXUNUSED(event))
614 {
615 wxBookCtrlBase *currBook = GetCurrentBook();
616 if ( currBook )
617 {
618 const int selPos = currBook->GetSelection();
619 if ( selPos == wxNOT_FOUND )
620 {
621 wxLogError(_T("Select the parent page first!"));
622 return;
623 }
624
625 static unsigned s_subPageAdded = 0;
626 currBook->InsertPage(selPos,
627 CreateNewPage(),
628 wxString::Format
629 (
630 ADDED_PAGE_NAME_BEFORE wxT("%u"),
631 ++s_subPageAdded
632 ),
633 true,
634 GetIconIndex(currBook));
635 }
636 }
637 #endif // wxUSE_TREEBOOK
638
639 void MyFrame::OnInsertPage(wxCommandEvent& WXUNUSED(event))
640 {
641 static unsigned s_pageIns = 0;
642
643 wxBookCtrlBase *currBook = GetCurrentBook();
644
645 if ( currBook )
646 {
647 wxPanel *panel = CreateUserCreatedPage( currBook );
648
649 currBook->InsertPage( 0, panel,
650 wxString::Format(INSERTED_PAGE_NAME wxT("%u"), ++s_pageIns), false,
651 GetIconIndex(currBook) );
652
653 currBook->SetSelection(0);
654 }
655 }
656
657 void MyFrame::OnDeleteCurPage(wxCommandEvent& WXUNUSED(event))
658 {
659 wxBookCtrlBase *currBook = GetCurrentBook();
660
661 if ( currBook )
662 {
663 int sel = currBook->GetSelection();
664
665 if (sel != wxNOT_FOUND)
666 {
667 currBook->DeletePage(sel);
668 }
669 }
670 }
671
672 void MyFrame::OnDeleteLastPage(wxCommandEvent& WXUNUSED(event))
673 {
674 wxBookCtrlBase *currBook = GetCurrentBook();
675
676 if ( currBook )
677 {
678 int page = currBook->GetPageCount();
679
680 if ( page != 0 )
681 {
682 currBook->DeletePage(page - 1);
683 }
684 }
685 }
686
687 void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event))
688 {
689 wxBookCtrlBase *currBook = GetCurrentBook();
690
691 if ( currBook )
692 {
693 currBook->AdvanceSelection();
694 }
695 }
696
697 void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
698 {
699 static int s_nPages = wxNOT_FOUND;
700 static int s_nSel = wxNOT_FOUND;
701 static wxBookCtrlBase *s_currBook = NULL;
702
703 wxBookCtrlBase *currBook = GetCurrentBook();
704
705 int nPages = currBook ? currBook->GetPageCount() : 0;
706 int nSel = currBook ? currBook->GetSelection() : wxNOT_FOUND;
707
708 if ( nPages != s_nPages || nSel != s_nSel || s_currBook != currBook )
709 {
710 s_nPages = nPages;
711 s_nSel = nSel;
712 s_currBook = currBook;
713
714 wxString selection;
715 if ( nSel == wxNOT_FOUND )
716 selection << wxT("not found");
717 else
718 selection << nSel;
719
720 wxString title;
721 title.Printf(wxT("Notebook and friends (%d pages, selection: %s)"), nPages, selection.c_str());
722
723 SetTitle(title);
724 }
725 }
726
727 void MyFrame::OnBookCtrl(wxBookCtrlBaseEvent& event)
728 {
729 static const struct EventInfo
730 {
731 wxEventType typeChanged,
732 typeChanging;
733 const wxChar *name;
734 } events[] =
735 {
736 #if wxUSE_NOTEBOOK
737 {
738 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
739 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING,
740 _T("wxNotebook")
741 },
742 #endif // wxUSE_NOTEBOOK
743 #if wxUSE_LISTBOOK
744 {
745 wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED,
746 wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING,
747 _T("wxListbook")
748 },
749 #endif // wxUSE_LISTBOOK
750 #if wxUSE_CHOICEBOOK
751 {
752 wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED,
753 wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING,
754 _T("wxChoicebook")
755 },
756 #endif // wxUSE_CHOICEBOOK
757 #if wxUSE_TREEBOOK
758 {
759 wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED,
760 wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING,
761 _T("wxTreebook")
762 },
763 #endif // wxUSE_TREEBOOK
764 };
765
766
767 wxString nameEvent,
768 nameControl,
769 veto;
770 const wxEventType eventType = event.GetEventType();
771 for ( size_t n = 0; n < WXSIZEOF(events); n++ )
772 {
773 const EventInfo& ei = events[n];
774 if ( eventType == ei.typeChanged )
775 {
776 nameEvent = wxT("Changed");
777 }
778 else if ( eventType == ei.typeChanging )
779 {
780 const int idx = event.GetOldSelection();
781 const wxBookCtrlBase * const
782 book = wxStaticCast(event.GetEventObject(), wxBookCtrlBase);
783 if ( idx != wxNOT_FOUND &&
784 book && book->GetPageText(idx) == VETO_PAGE_NAME )
785 {
786 if ( wxMessageBox
787 (
788 wxT("Are you sure you want to leave this page?\n")
789 wxT("(This demonstrates veto-ing)"),
790 wxT("Notebook sample"),
791 wxICON_QUESTION | wxYES_NO,
792 this
793 ) != wxYES )
794 {
795 event.Veto();
796 veto = _T(" (vetoed)");
797 }
798 }
799
800 nameEvent = wxT("Changing");
801 }
802 else // skip end of the loop
803 {
804 continue;
805 }
806
807 nameControl = ei.name;
808 break;
809 }
810
811 static int s_num = 0;
812
813 wxLogMessage(wxT("Event #%d: %s: %s (%d) new sel %d, old %d%s"),
814 ++s_num,
815 nameControl.c_str(),
816 nameEvent.c_str(),
817 eventType,
818 event.GetSelection(),
819 event.GetOldSelection(),
820 veto.c_str());
821
822 #if USE_LOG
823 m_text->SetInsertionPointEnd();
824 #endif
825 }
826