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