]> git.saurik.com Git - wxWidgets.git/blob - samples/notebook/notebook.cpp
Don't require skipping "page changed" event in wxMSW wxNotebook.
[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 // Licence: wxWindows licence
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 #elif wxUSE_TOOLBOOK
295 m_type = Type_Toolbook;
296 #else
297 #error "Don't use Notebook sample without any book enabled in wxWidgets build!"
298 #endif
299
300 m_orient = ID_ORIENT_DEFAULT;
301 m_chkShowImages = true;
302 m_multi = false;
303
304 SetIcon(wxICON(sample));
305
306 // menu of the sample
307 wxMenu *menuType = new wxMenu;
308 #if wxUSE_NOTEBOOK
309 menuType->AppendRadioItem(ID_BOOK_NOTEBOOK, wxT("&Notebook\tCtrl-1"));
310 #endif
311 #if wxUSE_LISTBOOK
312 menuType->AppendRadioItem(ID_BOOK_LISTBOOK, wxT("&Listbook\tCtrl-2"));
313 #endif
314 #if wxUSE_CHOICEBOOK
315 menuType->AppendRadioItem(ID_BOOK_CHOICEBOOK, wxT("&Choicebook\tCtrl-3"));
316 #endif
317 #if wxUSE_TREEBOOK
318 menuType->AppendRadioItem(ID_BOOK_TREEBOOK, wxT("&Treebook\tCtrl-4"));
319 #endif
320 #if wxUSE_TOOLBOOK
321 menuType->AppendRadioItem(ID_BOOK_TOOLBOOK, wxT("T&oolbook\tCtrl-5"));
322 #endif
323
324 menuType->Check(ID_BOOK_NOTEBOOK + m_type, true);
325
326 wxMenu *menuOrient = new wxMenu;
327 menuOrient->AppendRadioItem(ID_ORIENT_DEFAULT, wxT("&Default\tAlt-0"));
328 menuOrient->AppendRadioItem(ID_ORIENT_TOP, wxT("&Top\tAlt-1"));
329 menuOrient->AppendRadioItem(ID_ORIENT_BOTTOM, wxT("&Bottom\tAlt-2"));
330 menuOrient->AppendRadioItem(ID_ORIENT_LEFT, wxT("&Left\tAlt-3"));
331 menuOrient->AppendRadioItem(ID_ORIENT_RIGHT, wxT("&Right\tAlt-4"));
332
333 wxMenu *menuPageOperations = new wxMenu;
334 menuPageOperations->Append(ID_ADD_PAGE, wxT("&Add page\tAlt-A"));
335 menuPageOperations->Append(ID_ADD_PAGE_NO_SELECT, wxT("&Add page (don't select)\tAlt-B"));
336 menuPageOperations->Append(ID_INSERT_PAGE, wxT("&Insert page\tAlt-I"));
337 menuPageOperations->Append(ID_DELETE_CUR_PAGE, wxT("&Delete current page\tAlt-D"));
338 menuPageOperations->Append(ID_DELETE_LAST_PAGE, wxT("D&elete last page\tAlt-L"));
339 menuPageOperations->Append(ID_NEXT_PAGE, wxT("&Next page\tAlt-N"));
340 #if wxUSE_TREEBOOK
341 menuPageOperations->AppendSeparator();
342 menuPageOperations->Append(ID_ADD_PAGE_BEFORE, wxT("Insert page &before\tAlt-B"));
343 menuPageOperations->Append(ID_ADD_SUB_PAGE, wxT("Add s&ub page\tAlt-U"));
344 #endif
345 menuPageOperations->AppendSeparator();
346 menuPageOperations->Append(ID_GO_HOME, wxT("Go to the first page\tCtrl-F"));
347
348 wxMenu *menuOperations = new wxMenu;
349 #if wxUSE_HELP
350 menuOperations->Append(ID_CONTEXT_HELP, wxT("&Context help\tCtrl-F1"));
351 #endif // wxUSE_HELP
352 menuOperations->Append(ID_HITTEST, wxT("&Hit test\tCtrl-H"));
353
354 wxMenu *menuFile = new wxMenu;
355 menuFile->Append(wxID_ANY, wxT("&Type"), menuType, wxT("Type of control"));
356 menuFile->Append(wxID_ANY, wxT("&Orientation"), menuOrient, wxT("Orientation of control"));
357 menuFile->AppendCheckItem(ID_SHOW_IMAGES, wxT("&Show images\tAlt-S"));
358 menuFile->AppendCheckItem(ID_MULTI, wxT("&Multiple lines\tAlt-M"));
359 menuFile->AppendSeparator();
360 menuFile->Append(wxID_EXIT, wxT("E&xit"), wxT("Quits the application"));
361 menuFile->Check(ID_SHOW_IMAGES, m_chkShowImages);
362 menuFile->Check(ID_MULTI, m_multi);
363
364 wxMenuBar *menuBar = new wxMenuBar;
365 menuBar->Append(menuFile, wxT("&File"));
366 menuBar->Append(menuPageOperations, wxT("&Pages"));
367 menuBar->Append(menuOperations, wxT("&Operations"));
368 SetMenuBar(menuBar);
369
370 // books creation
371 m_panel = NULL;
372 m_bookCtrl = NULL;
373
374 // create a dummy image list with a few icons
375 const wxSize imageSize(32, 32);
376
377 m_imageList = new wxImageList(imageSize.GetWidth(), imageSize.GetHeight());
378 m_imageList->
379 Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, imageSize));
380 m_imageList->
381 Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, imageSize));
382 m_imageList->
383 Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, imageSize));
384 m_imageList->
385 Add(wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, imageSize));
386
387 m_panel = new wxPanel(this);
388
389 #if USE_LOG
390 m_text = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
391 wxDefaultPosition, wxDefaultSize,
392 wxTE_MULTILINE | wxTE_READONLY);
393
394 m_logTargetOld = wxLog::SetActiveTarget( new wxLogTextCtrl(m_text) );
395 #endif // USE_LOG
396
397 // Set sizers
398 m_sizerFrame = new wxBoxSizer(wxVERTICAL);
399
400 #if USE_LOG
401 m_sizerFrame->Add(m_text, 1, wxEXPAND);
402 #endif // USE_LOG
403
404 RecreateBook();
405
406 m_panel->SetSizer(m_sizerFrame);
407 m_panel->Layout();
408
409 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
410 sizer->Add(m_panel, wxSizerFlags(1).Expand());
411 SetSizerAndFit(sizer);
412 }
413
414 MyFrame::~MyFrame()
415 {
416 #if USE_LOG
417 delete wxLog::SetActiveTarget(m_logTargetOld);
418 #endif // USE_LOG
419
420 delete m_imageList;
421 }
422
423 // DISPATCH_ON_TYPE() macro is an ugly way to write the "same" code for
424 // different wxBookCtrlBase-derived classes without duplicating code and
425 // without using templates, it expands into "before <xxx> after" where "xxx"
426 // part is control class-specific
427 #if wxUSE_NOTEBOOK
428 #define CASE_NOTEBOOK(x) case Type_Notebook: x; break;
429 #else
430 #define CASE_NOTEBOOK(x)
431 #endif
432
433 #if wxUSE_LISTBOOK
434 #define CASE_LISTBOOK(x) case Type_Listbook: x; break;
435 #else
436 #define CASE_LISTBOOK(x)
437 #endif
438
439 #if wxUSE_CHOICEBOOK
440 #define CASE_CHOICEBOOK(x) case Type_Choicebook: x; break;
441 #else
442 #define CASE_CHOICEBOOK(x)
443 #endif
444
445 #if wxUSE_TREEBOOK
446 #define CASE_TREEBOOK(x) case Type_Treebook: x; break;
447 #else
448 #define CASE_TREEBOOK(x)
449 #endif
450
451 #if wxUSE_TOOLBOOK
452 #define CASE_TOOLBOOK(x) case Type_Toolbook: x; break;
453 #else
454 #define CASE_TOOLBOOK(x)
455 #endif
456
457 #define DISPATCH_ON_TYPE(before, nb, lb, cb, tb, toolb, after) \
458 switch ( m_type ) \
459 { \
460 CASE_NOTEBOOK(before nb after) \
461 CASE_LISTBOOK(before lb after) \
462 CASE_CHOICEBOOK(before cb after) \
463 CASE_TREEBOOK(before tb after) \
464 CASE_TOOLBOOK(before toolb after) \
465 \
466 default: \
467 wxFAIL_MSG( wxT("unknown book control type") ); \
468 }
469
470 int MyFrame::TranslateBookFlag(int nb, int lb, int chb, int tbk, int toolbk) const
471 {
472 int flag = 0;
473
474 DISPATCH_ON_TYPE(flag =, nb, lb, chb, tbk, toolbk, + 0);
475
476 return flag;
477 }
478
479 void MyFrame::RecreateBook()
480 {
481 int flags;
482 switch ( m_orient )
483 {
484 case ID_ORIENT_TOP:
485 flags = wxBK_TOP;
486 break;
487
488 case ID_ORIENT_BOTTOM:
489 flags = wxBK_BOTTOM;
490 break;
491
492 case ID_ORIENT_LEFT:
493 flags = wxBK_LEFT;
494 break;
495
496 case ID_ORIENT_RIGHT:
497 flags = wxBK_RIGHT;
498 break;
499
500 default:
501 flags = wxBK_DEFAULT;
502 }
503
504 if ( m_multi && m_type == Type_Notebook )
505 flags |= wxNB_MULTILINE;
506
507 wxBookCtrlBase *oldBook = m_bookCtrl;
508
509 m_bookCtrl = NULL;
510
511 DISPATCH_ON_TYPE(m_bookCtrl = new,
512 wxNotebook,
513 wxListbook,
514 wxChoicebook,
515 wxTreebook,
516 wxToolbook,
517 (m_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, flags));
518
519 if ( !m_bookCtrl )
520 return;
521
522 m_bookCtrl->Hide();
523
524 if ( m_chkShowImages )
525 {
526 m_bookCtrl->SetImageList(m_imageList);
527 }
528
529 if ( oldBook )
530 {
531 #if wxUSE_TREEBOOK
532 // we only need the old treebook if we're recreating another treebook
533 wxTreebook *tbkOld = m_type == Type_Treebook
534 ? wxDynamicCast(oldBook, wxTreebook)
535 : NULL;
536 #endif // wxUSE_TREEBOOK
537
538 const int count = oldBook->GetPageCount();
539 for ( int n = 0; n < count; n++ )
540 {
541 const int image = GetIconIndex(m_bookCtrl);
542 const wxString str = oldBook->GetPageText(n);
543
544 wxWindow *page = CreatePage(m_bookCtrl, str);
545
546 // treebook complication: need to account for possible parent page
547 #if wxUSE_TREEBOOK
548 if ( tbkOld )
549 {
550 const int parent = tbkOld->GetPageParent(n);
551 if ( parent != wxNOT_FOUND )
552 {
553 wxStaticCast(m_bookCtrl, wxTreebook)->
554 InsertSubPage(parent, page, str, false, image);
555
556 // skip adding it again below
557 continue;
558 }
559 }
560 #endif // wxUSE_TREEBOOK
561
562 m_bookCtrl->AddPage(page, str, false, image);
563 }
564
565 const int sel = oldBook->GetSelection();
566 if ( sel != wxNOT_FOUND )
567 m_bookCtrl->SetSelection(sel);
568
569
570 m_sizerFrame->Detach(oldBook);
571 delete oldBook;
572 }
573 else // no old book
574 {
575 CreateInitialPages(m_bookCtrl);
576 }
577
578 m_sizerFrame->Insert(0, m_bookCtrl, wxSizerFlags(5).Expand().Border());
579
580 m_sizerFrame->Show(m_bookCtrl);
581 m_sizerFrame->Layout();
582 }
583
584 void MyFrame::AddFlagStrIfFlagPresent(wxString & flagStr, long flags, long flag,
585 const wxChar * flagName) const
586 {
587 if( (flags & flag) == flag )
588 {
589 if( !flagStr.empty() )
590 flagStr += wxT(" | ");
591 flagStr += flagName;
592 }
593 }
594
595 wxPanel *MyFrame::CreateNewPage() const
596 {
597 wxPanel *panel = new wxPanel(m_bookCtrl, wxID_ANY );
598
599 #if wxUSE_HELP
600 panel->SetHelpText( wxT( "Panel with \"First\" and \"Second\" buttons" ) );
601 #endif
602
603 (void) new wxButton(panel, wxID_ANY, wxT("First button"), wxPoint(10, 30));
604 (void) new wxButton(panel, wxID_ANY, wxT("Second button"), wxPoint(150, 30));
605
606 return panel;
607 }
608
609
610 //-----------------------------------------------------------------------------
611 // MyFrame - event handlers
612 //-----------------------------------------------------------------------------
613
614 #if wxUSE_HELP
615
616 void MyFrame::OnContextHelp(wxCommandEvent& WXUNUSED(event))
617 {
618 // launches local event loop
619 wxContextHelp ch( this );
620 }
621
622 #endif // wxUSE_HELP
623
624 void MyFrame::OnHitTest(wxCommandEvent& WXUNUSED(event))
625 {
626 wxBookCtrlBase * book = GetCurrentBook();
627 const wxPoint pt = ::wxGetMousePosition();
628
629 long flags;
630 int pagePos = book->HitTest( book->ScreenToClient(pt), &flags );
631
632 wxString flagsStr;
633
634 AddFlagStrIfFlagPresent( flagsStr, flags, wxBK_HITTEST_NOWHERE, wxT("wxBK_HITTEST_NOWHERE") );
635 AddFlagStrIfFlagPresent( flagsStr, flags, wxBK_HITTEST_ONICON, wxT("wxBK_HITTEST_ONICON") );
636 AddFlagStrIfFlagPresent( flagsStr, flags, wxBK_HITTEST_ONLABEL, wxT("wxBK_HITTEST_ONLABEL") );
637 AddFlagStrIfFlagPresent( flagsStr, flags, wxBK_HITTEST_ONPAGE, wxT("wxBK_HITTEST_ONPAGE") );
638
639 wxLogMessage(wxT("HitTest at (%d,%d): %d: %s"),
640 pt.x,
641 pt.y,
642 pagePos,
643 flagsStr.c_str());
644 }
645
646 void MyFrame::OnType(wxCommandEvent& event)
647 {
648 m_type = static_cast<BookType>(event.GetId() - ID_BOOK_NOTEBOOK);
649
650 if ( m_bookCtrl )
651 m_sizerFrame->Hide(m_bookCtrl);
652
653 RecreateBook();
654 }
655
656 #if wxUSE_TREEBOOK
657
658 void MyFrame::OnUpdateTreeMenu(wxUpdateUIEvent& event)
659 {
660 event.Enable(m_type == Type_Treebook);
661 }
662
663 #endif // wxUSE_TREEBOOK
664
665 void MyFrame::OnOrient(wxCommandEvent& event)
666 {
667 m_orient = event.GetId();
668 RecreateBook();
669 m_sizerFrame->Layout();
670 }
671
672 void MyFrame::OnShowImages(wxCommandEvent& event)
673 {
674 m_chkShowImages = event.IsChecked();
675 RecreateBook();
676 m_sizerFrame->Layout();
677 }
678
679 void MyFrame::OnMulti(wxCommandEvent& event)
680 {
681 m_multi = event.IsChecked();
682 RecreateBook();
683 m_sizerFrame->Layout();
684 wxLogMessage(wxT("Multiline setting works only in wxNotebook."));
685 }
686
687 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
688 {
689 Close();
690 }
691
692 void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
693 {
694 wxBookCtrlBase *currBook = GetCurrentBook();
695
696 if ( currBook )
697 {
698 static unsigned s_pageAdded = 0;
699 currBook->AddPage(CreateNewPage(),
700 wxString::Format
701 (
702 ADDED_PAGE_NAME wxT("%u"),
703 ++s_pageAdded
704 ),
705 true,
706 GetIconIndex(currBook));
707 }
708 }
709
710 void MyFrame::OnAddPageNoSelect(wxCommandEvent& WXUNUSED(event))
711 {
712 wxBookCtrlBase *currBook = GetCurrentBook();
713
714 if ( currBook )
715 {
716 static unsigned s_pageAdded = 0;
717 currBook->AddPage(CreateNewPage(),
718 wxString::Format
719 (
720 ADDED_PAGE_NAME wxT("%u"),
721 ++s_pageAdded
722 ),
723 false,
724 GetIconIndex(currBook));
725 }
726 }
727
728 #if wxUSE_TREEBOOK
729 void MyFrame::OnAddSubPage(wxCommandEvent& WXUNUSED(event))
730 {
731 wxTreebook *currBook = wxDynamicCast(GetCurrentBook(), wxTreebook);
732 if ( currBook )
733 {
734 const int selPos = currBook->GetSelection();
735 if ( selPos == wxNOT_FOUND )
736 {
737 wxLogError(wxT("Select the parent page first!"));
738 return;
739 }
740
741 static unsigned s_subPageAdded = 0;
742 currBook->InsertSubPage
743 (
744 selPos,
745 CreateNewPage(),
746 wxString::Format
747 (
748 ADDED_SUB_PAGE_NAME wxT("%u"),
749 ++s_subPageAdded
750 ),
751 true,
752 GetIconIndex(currBook)
753 );
754 }
755 }
756
757 void MyFrame::OnAddPageBefore(wxCommandEvent& WXUNUSED(event))
758 {
759 wxBookCtrlBase *currBook = GetCurrentBook();
760 if ( currBook )
761 {
762 const int selPos = currBook->GetSelection();
763 if ( selPos == wxNOT_FOUND )
764 {
765 wxLogError(wxT("Select the parent page first!"));
766 return;
767 }
768
769 static unsigned s_subPageAdded = 0;
770 currBook->InsertPage(selPos,
771 CreateNewPage(),
772 wxString::Format
773 (
774 ADDED_PAGE_NAME_BEFORE wxT("%u"),
775 ++s_subPageAdded
776 ),
777 true,
778 GetIconIndex(currBook));
779 }
780 }
781 #endif // wxUSE_TREEBOOK
782
783 void MyFrame::OnInsertPage(wxCommandEvent& WXUNUSED(event))
784 {
785 static unsigned s_pageIns = 0;
786
787 wxBookCtrlBase *currBook = GetCurrentBook();
788
789 if ( currBook )
790 {
791 wxPanel *panel = CreateUserCreatedPage( currBook );
792
793 currBook->InsertPage( 0, panel,
794 wxString::Format(INSERTED_PAGE_NAME wxT("%u"), ++s_pageIns), false,
795 GetIconIndex(currBook) );
796
797 currBook->SetSelection(0);
798 }
799 }
800
801 void MyFrame::OnDeleteCurPage(wxCommandEvent& WXUNUSED(event))
802 {
803 wxBookCtrlBase *currBook = GetCurrentBook();
804
805 if ( currBook )
806 {
807 int sel = currBook->GetSelection();
808
809 if (sel != wxNOT_FOUND)
810 {
811 currBook->DeletePage(sel);
812 }
813 }
814 }
815
816 void MyFrame::OnDeleteLastPage(wxCommandEvent& WXUNUSED(event))
817 {
818 wxBookCtrlBase *currBook = GetCurrentBook();
819
820 if ( currBook )
821 {
822 int page = currBook->GetPageCount();
823
824 if ( page != 0 )
825 {
826 currBook->DeletePage(page - 1);
827 }
828 }
829 }
830
831 void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event))
832 {
833 wxBookCtrlBase *currBook = GetCurrentBook();
834
835 if ( currBook )
836 {
837 currBook->AdvanceSelection();
838 }
839 }
840
841 void MyFrame::OnGoHome(wxCommandEvent& WXUNUSED(event))
842 {
843 wxBookCtrlBase *currBook = GetCurrentBook();
844
845 if ( currBook )
846 {
847 // ChangeSelection shouldn't send any events, SetSelection() should
848 currBook->ChangeSelection(0);
849 //currBook->SetSelection(0);
850 }
851 }
852
853 void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
854 {
855 static int s_nPages = wxNOT_FOUND;
856 static int s_nSel = wxNOT_FOUND;
857 static wxBookCtrlBase *s_currBook = NULL;
858
859 wxBookCtrlBase *currBook = GetCurrentBook();
860
861 int nPages = currBook ? currBook->GetPageCount() : 0;
862 int nSel = currBook ? currBook->GetSelection() : wxNOT_FOUND;
863
864 if ( nPages != s_nPages || nSel != s_nSel || s_currBook != currBook )
865 {
866 s_nPages = nPages;
867 s_nSel = nSel;
868 s_currBook = currBook;
869
870 wxString selection;
871 if ( nSel == wxNOT_FOUND )
872 selection << wxT("not found");
873 else
874 selection << nSel;
875
876 wxString title;
877 title.Printf(wxT("Notebook and friends (%d pages, selection: %s)"), nPages, selection.c_str());
878
879 SetTitle(title);
880 }
881 }
882
883 void MyFrame::OnBookCtrl(wxBookCtrlBaseEvent& event)
884 {
885 static const struct EventInfo
886 {
887 wxEventType typeChanged,
888 typeChanging;
889 const wxChar *name;
890 } events[] =
891 {
892 #if wxUSE_NOTEBOOK
893 {
894 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
895 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING,
896 wxT("wxNotebook")
897 },
898 #endif // wxUSE_NOTEBOOK
899 #if wxUSE_LISTBOOK
900 {
901 wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED,
902 wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING,
903 wxT("wxListbook")
904 },
905 #endif // wxUSE_LISTBOOK
906 #if wxUSE_CHOICEBOOK
907 {
908 wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED,
909 wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING,
910 wxT("wxChoicebook")
911 },
912 #endif // wxUSE_CHOICEBOOK
913 #if wxUSE_TREEBOOK
914 {
915 wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED,
916 wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING,
917 wxT("wxTreebook")
918 },
919 #endif // wxUSE_TREEBOOK
920 #if wxUSE_TOOLBOOK
921 {
922 wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED,
923 wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING,
924 wxT("wxToolbook")
925 },
926 #endif // wxUSE_TOOLBOOK
927 };
928
929
930 wxString nameEvent,
931 nameControl,
932 veto;
933 const wxEventType eventType = event.GetEventType();
934 for ( size_t n = 0; n < WXSIZEOF(events); n++ )
935 {
936 const EventInfo& ei = events[n];
937 if ( eventType == ei.typeChanged )
938 {
939 nameEvent = wxT("Changed");
940 }
941 else if ( eventType == ei.typeChanging )
942 {
943 const int idx = event.GetOldSelection();
944
945 // NB: can't use wxStaticCast here as wxBookCtrlBase is not in
946 // wxRTTI
947 const wxBookCtrlBase * const
948 book = static_cast<wxBookCtrlBase *>(event.GetEventObject());
949 if ( idx != wxNOT_FOUND &&
950 book && book->GetPageText(idx) == VETO_PAGE_NAME )
951 {
952 if ( wxMessageBox
953 (
954 wxT("Are you sure you want to leave this page?\n")
955 wxT("(This demonstrates veto-ing)"),
956 wxT("Notebook sample"),
957 wxICON_QUESTION | wxYES_NO,
958 this
959 ) != wxYES )
960 {
961 event.Veto();
962 veto = wxT(" (vetoed)");
963 }
964 }
965
966 nameEvent = wxT("Changing");
967 }
968 else // skip end of the loop
969 {
970 continue;
971 }
972
973 nameControl = ei.name;
974 break;
975 }
976
977 static int s_num = 0;
978
979 wxLogMessage(wxT("Event #%d: %s: %s (%d) new sel %d, old %d%s"),
980 ++s_num,
981 nameControl.c_str(),
982 nameEvent.c_str(),
983 eventType,
984 event.GetSelection(),
985 event.GetOldSelection(),
986 veto.c_str());
987
988 #if USE_LOG
989 m_text->SetInsertionPointEnd();
990 #endif
991 }