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