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