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