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