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