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