1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/notebook/notebook.cpp
3 // Purpose: a sample demonstrating notebook usage
4 // Author: Julian Smart
5 // Modified by: Dimitri Schoolwerth
8 // Copyright: (c) 1998-2002 wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/imaglist.h"
24 #include "wx/artprov.h"
25 #include "wx/cshelp.h"
29 #if !defined(__WXMSW__) && !defined(__WXPM__)
30 #include "../sample.xpm"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
41 if ( !wxApp::OnInit() )
45 wxHelpProvider::Set( new wxSimpleHelpProvider
);
48 // Create the main window
49 MyFrame
*frame
= new MyFrame();
52 // Problem with generic wxNotebook implementation whereby it doesn't size
53 // properly unless you set the size again
54 #if defined(__WXMOTIF__)
56 frame
->GetSize(& width
, & height
);
57 frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 wxPanel
*CreateUserCreatedPage(wxBookCtrlBase
*parent
)
72 wxPanel
*panel
= new wxPanel(parent
);
75 panel
->SetHelpText( wxT( "Panel with a Button" ) );
78 (void) new wxButton( panel
, wxID_ANY
, wxT("Button"),
79 wxPoint(10, 10), wxDefaultSize
);
84 wxPanel
*CreateRadioButtonsPage(wxBookCtrlBase
*parent
)
86 wxPanel
*panel
= new wxPanel(parent
);
89 panel
->SetHelpText( wxT( "Panel with some Radio Buttons" ) );
93 { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
94 wxT("Sabre-toothed tiger"), wxT("T Rex") };
96 wxRadioBox
*radiobox1
= new wxRadioBox(panel
, wxID_ANY
, wxT("Choose one"),
97 wxDefaultPosition
, wxDefaultSize
, 5, animals
, 2, wxRA_SPECIFY_ROWS
);
99 wxString computers
[] =
100 { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
103 wxRadioBox
*radiobox2
= new wxRadioBox(panel
, wxID_ANY
,
104 wxT("Choose your favourite"), wxDefaultPosition
, wxDefaultSize
,
105 4, computers
, 0, wxRA_SPECIFY_COLS
);
107 wxBoxSizer
*sizerPanel
= new wxBoxSizer(wxVERTICAL
);
108 sizerPanel
->Add(radiobox1
, 2, wxEXPAND
);
109 sizerPanel
->Add(radiobox2
, 1, wxEXPAND
);
110 panel
->SetSizer(sizerPanel
);
115 wxPanel
*CreateVetoPage(wxBookCtrlBase
*parent
)
117 wxPanel
*panel
= new wxPanel(parent
);
120 panel
->SetHelpText( wxT( "An empty panel" ) );
123 (void) new wxStaticText( panel
, wxID_ANY
,
124 wxT("This page intentionally left blank"),
130 wxPanel
*CreateBigButtonPage(wxBookCtrlBase
*parent
)
132 wxPanel
*panel
= new wxPanel(parent
);
135 panel
->SetHelpText( wxT( "Panel with a maximized button" ) );
138 wxButton
*buttonBig
= new wxButton(panel
, wxID_ANY
, wxT("Maximized button"));
140 wxBoxSizer
*sizerPanel
= new wxBoxSizer(wxVERTICAL
);
141 sizerPanel
->Add(buttonBig
, 1, wxEXPAND
);
142 panel
->SetSizer(sizerPanel
);
147 wxPanel
*CreateInsertPage(wxBookCtrlBase
*parent
)
149 wxPanel
*panel
= new wxPanel(parent
);
152 panel
->SetHelpText( wxT( "Maroon panel" ) );
155 panel
->SetBackgroundColour( wxColour( wxT("MAROON") ) );
156 (void) new wxStaticText( panel
, wxID_ANY
,
157 wxT("This page has been inserted, not added."),
163 int GetIconIndex(wxBookCtrlBase
* bookCtrl
)
165 if (bookCtrl
&& bookCtrl
->GetImageList())
167 int nImages
= bookCtrl
->GetImageList()->GetImageCount();
170 return bookCtrl
->GetPageCount() % nImages
;
177 void CreateInitialPages(wxBookCtrlBase
*parent
)
179 // Create and add some panels to the notebook
181 wxPanel
*panel
= CreateRadioButtonsPage(parent
);
182 parent
->AddPage( panel
, RADIOBUTTONS_PAGE_NAME
, false, GetIconIndex(parent
) );
184 panel
= CreateVetoPage(parent
);
185 parent
->AddPage( panel
, VETO_PAGE_NAME
, false, GetIconIndex(parent
) );
187 panel
= CreateBigButtonPage(parent
);
188 parent
->AddPage( panel
, MAXIMIZED_BUTTON_PAGE_NAME
, false, GetIconIndex(parent
) );
190 panel
= CreateInsertPage(parent
);
191 parent
->InsertPage( 0, panel
, I_WAS_INSERTED_PAGE_NAME
, false, GetIconIndex(parent
) );
193 parent
->SetSelection(1);
196 wxPanel
*CreatePage(wxBookCtrlBase
*parent
, const wxString
&pageName
)
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
);
204 if ( pageName
== I_WAS_INSERTED_PAGE_NAME
)
205 return CreateInsertPage(parent
);
207 if ( pageName
== VETO_PAGE_NAME
)
208 return CreateVetoPage(parent
);
210 if ( pageName
== RADIOBUTTONS_PAGE_NAME
)
211 return CreateRadioButtonsPage(parent
);
213 if ( pageName
== MAXIMIZED_BUTTON_PAGE_NAME
)
214 return CreateBigButtonPage(parent
);
216 wxFAIL_MSG( wxT("unknown page name") );
222 //-----------------------------------------------------------------------------
224 //-----------------------------------------------------------------------------
226 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
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
)
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_CHANGE_SELECTION
, MyFrame::OnChangeSelection
)
242 EVT_MENU(ID_SET_SELECTION
, MyFrame::OnSetSelection
)
245 EVT_MENU(ID_CONTEXT_HELP
, MyFrame::OnContextHelp
)
247 EVT_MENU(ID_HITTEST
, MyFrame::OnHitTest
)
251 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, MyFrame::OnNotebook
)
252 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, MyFrame::OnNotebook
)
255 EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY
, MyFrame::OnListbook
)
256 EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY
, MyFrame::OnListbook
)
259 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY
, MyFrame::OnChoicebook
)
260 EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY
, MyFrame::OnChoicebook
)
263 EVT_TREEBOOK_PAGE_CHANGED(wxID_ANY
, MyFrame::OnTreebook
)
264 EVT_TREEBOOK_PAGE_CHANGING(wxID_ANY
, MyFrame::OnTreebook
)
266 EVT_MENU(ID_ADD_SUB_PAGE
, MyFrame::OnAddSubPage
)
267 EVT_MENU(ID_ADD_PAGE_BEFORE
, MyFrame::OnAddPageBefore
)
268 EVT_UPDATE_UI_RANGE(ID_ADD_PAGE_BEFORE
, ID_ADD_SUB_PAGE
,
269 MyFrame::OnUpdateTreeMenu
)
272 EVT_TOOLBOOK_PAGE_CHANGED(wxID_ANY
, MyFrame::OnToolbook
)
273 EVT_TOOLBOOK_PAGE_CHANGING(wxID_ANY
, MyFrame::OnToolbook
)
276 // Update title in idle time
277 EVT_IDLE(MyFrame::OnIdle
)
281 : wxFrame(NULL
, wxID_ANY
, wxString(wxT("wxWidgets book controls sample")))
284 SetExtraStyle(wxFRAME_EX_CONTEXTHELP
);
288 m_type
= Type_Notebook
;
289 #elif wxUSE_CHOICEBOOK
290 m_type
= Type_Choicebook
;
292 m_type
= Type_Listbook
;
294 m_type
= Type_Treebook
;
296 m_type
= Type_Toolbook
;
298 #error "Don't use Notebook sample without any book enabled in wxWidgets build!"
301 m_orient
= ID_ORIENT_DEFAULT
;
302 m_chkShowImages
= true;
305 SetIcon(wxICON(sample
));
307 // menu of the sample
308 wxMenu
*menuType
= new wxMenu
;
310 menuType
->AppendRadioItem(ID_BOOK_NOTEBOOK
, wxT("&Notebook\tCtrl-1"));
313 menuType
->AppendRadioItem(ID_BOOK_LISTBOOK
, wxT("&Listbook\tCtrl-2"));
316 menuType
->AppendRadioItem(ID_BOOK_CHOICEBOOK
, wxT("&Choicebook\tCtrl-3"));
319 menuType
->AppendRadioItem(ID_BOOK_TREEBOOK
, wxT("&Treebook\tCtrl-4"));
322 menuType
->AppendRadioItem(ID_BOOK_TOOLBOOK
, wxT("T&oolbook\tCtrl-5"));
325 menuType
->Check(ID_BOOK_NOTEBOOK
+ m_type
, true);
327 wxMenu
*menuOrient
= new wxMenu
;
328 menuOrient
->AppendRadioItem(ID_ORIENT_DEFAULT
, wxT("&Default\tAlt-0"));
329 menuOrient
->AppendRadioItem(ID_ORIENT_TOP
, wxT("&Top\tAlt-1"));
330 menuOrient
->AppendRadioItem(ID_ORIENT_BOTTOM
, wxT("&Bottom\tAlt-2"));
331 menuOrient
->AppendRadioItem(ID_ORIENT_LEFT
, wxT("&Left\tAlt-3"));
332 menuOrient
->AppendRadioItem(ID_ORIENT_RIGHT
, wxT("&Right\tAlt-4"));
334 wxMenu
*menuPageOperations
= new wxMenu
;
335 menuPageOperations
->Append(ID_ADD_PAGE
, wxT("&Add page\tAlt-A"));
336 menuPageOperations
->Append(ID_ADD_PAGE_NO_SELECT
, wxT("&Add page (don't select)\tAlt-B"));
337 menuPageOperations
->Append(ID_INSERT_PAGE
, wxT("&Insert page\tAlt-I"));
338 menuPageOperations
->Append(ID_DELETE_CUR_PAGE
, wxT("&Delete current page\tAlt-D"));
339 menuPageOperations
->Append(ID_DELETE_LAST_PAGE
, wxT("D&elete last page\tAlt-L"));
340 menuPageOperations
->Append(ID_NEXT_PAGE
, wxT("&Next page\tAlt-N"));
342 menuPageOperations
->AppendSeparator();
343 menuPageOperations
->Append(ID_ADD_PAGE_BEFORE
, wxT("Insert page &before\tAlt-B"));
344 menuPageOperations
->Append(ID_ADD_SUB_PAGE
, wxT("Add s&ub page\tAlt-U"));
346 menuPageOperations
->AppendSeparator();
347 menuPageOperations
->Append(ID_CHANGE_SELECTION
, wxT("&Change selection to 0\tCtrl-0"));
348 menuPageOperations
->Append(ID_SET_SELECTION
, wxT("&Set selection to 0\tShift-Ctrl-0"));
350 wxMenu
*menuOperations
= new wxMenu
;
352 menuOperations
->Append(ID_CONTEXT_HELP
, wxT("&Context help\tCtrl-F1"));
354 menuOperations
->Append(ID_HITTEST
, wxT("&Hit test\tCtrl-H"));
356 wxMenu
*menuFile
= new wxMenu
;
357 menuFile
->Append(wxID_ANY
, wxT("&Type"), menuType
, wxT("Type of control"));
358 menuFile
->Append(wxID_ANY
, wxT("&Orientation"), menuOrient
, wxT("Orientation of control"));
359 menuFile
->AppendCheckItem(ID_SHOW_IMAGES
, wxT("&Show images\tAlt-S"));
360 menuFile
->AppendCheckItem(ID_MULTI
, wxT("&Multiple lines\tAlt-M"));
361 menuFile
->AppendSeparator();
362 menuFile
->Append(wxID_EXIT
, wxT("E&xit"), wxT("Quits the application"));
363 menuFile
->Check(ID_SHOW_IMAGES
, m_chkShowImages
);
364 menuFile
->Check(ID_MULTI
, m_multi
);
366 wxMenuBar
*menuBar
= new wxMenuBar
;
367 menuBar
->Append(menuFile
, wxT("&File"));
368 menuBar
->Append(menuPageOperations
, wxT("&Pages"));
369 menuBar
->Append(menuOperations
, wxT("&Operations"));
376 // create a dummy image list with a few icons
377 const wxSize
imageSize(32, 32);
379 m_imageList
= new wxImageList(imageSize
.GetWidth(), imageSize
.GetHeight());
381 Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, imageSize
));
383 Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, imageSize
));
385 Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, imageSize
));
387 Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, imageSize
));
389 m_panel
= new wxPanel(this);
392 m_text
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
393 wxDefaultPosition
, wxDefaultSize
,
394 wxTE_MULTILINE
| wxTE_READONLY
);
396 m_logTargetOld
= wxLog::SetActiveTarget( new wxLogTextCtrl(m_text
) );
400 m_sizerFrame
= new wxBoxSizer(wxVERTICAL
);
403 m_sizerFrame
->Add(m_text
, 1, wxEXPAND
);
408 m_panel
->SetSizer(m_sizerFrame
);
411 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
412 sizer
->Add(m_panel
, wxSizerFlags(1).Expand());
413 SetSizerAndFit(sizer
);
419 delete wxLog::SetActiveTarget(m_logTargetOld
);
425 // DISPATCH_ON_TYPE() macro is an ugly way to write the "same" code for
426 // different wxBookCtrlBase-derived classes without duplicating code and
427 // without using templates, it expands into "before <xxx> after" where "xxx"
428 // part is control class-specific
430 #define CASE_NOTEBOOK(x) case Type_Notebook: x; break;
432 #define CASE_NOTEBOOK(x)
436 #define CASE_LISTBOOK(x) case Type_Listbook: x; break;
438 #define CASE_LISTBOOK(x)
442 #define CASE_CHOICEBOOK(x) case Type_Choicebook: x; break;
444 #define CASE_CHOICEBOOK(x)
448 #define CASE_TREEBOOK(x) case Type_Treebook: x; break;
450 #define CASE_TREEBOOK(x)
454 #define CASE_TOOLBOOK(x) case Type_Toolbook: x; break;
456 #define CASE_TOOLBOOK(x)
459 #define DISPATCH_ON_TYPE(before, nb, lb, cb, tb, toolb, after) \
462 CASE_NOTEBOOK(before nb after) \
463 CASE_LISTBOOK(before lb after) \
464 CASE_CHOICEBOOK(before cb after) \
465 CASE_TREEBOOK(before tb after) \
466 CASE_TOOLBOOK(before toolb after) \
469 wxFAIL_MSG( wxT("unknown book control type") ); \
472 int MyFrame::TranslateBookFlag(int nb
, int lb
, int chb
, int tbk
, int toolbk
) const
476 DISPATCH_ON_TYPE(flag
=, nb
, lb
, chb
, tbk
, toolbk
, + 0);
481 void MyFrame::RecreateBook()
490 case ID_ORIENT_BOTTOM
:
498 case ID_ORIENT_RIGHT
:
503 flags
= wxBK_DEFAULT
;
506 if ( m_multi
&& m_type
== Type_Notebook
)
507 flags
|= wxNB_MULTILINE
;
509 wxBookCtrlBase
*oldBook
= m_bookCtrl
;
513 DISPATCH_ON_TYPE(m_bookCtrl
= new,
519 (m_panel
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, flags
));
526 if ( m_chkShowImages
)
528 m_bookCtrl
->SetImageList(m_imageList
);
534 // we only need the old treebook if we're recreating another treebook
535 wxTreebook
*tbkOld
= m_type
== Type_Treebook
536 ? wxDynamicCast(oldBook
, wxTreebook
)
538 #endif // wxUSE_TREEBOOK
540 const int count
= oldBook
->GetPageCount();
541 for ( int n
= 0; n
< count
; n
++ )
543 const int image
= GetIconIndex(m_bookCtrl
);
544 const wxString str
= oldBook
->GetPageText(n
);
546 wxWindow
*page
= CreatePage(m_bookCtrl
, str
);
548 // treebook complication: need to account for possible parent page
552 const int parent
= tbkOld
->GetPageParent(n
);
553 if ( parent
!= wxNOT_FOUND
)
555 wxStaticCast(m_bookCtrl
, wxTreebook
)->
556 InsertSubPage(parent
, page
, str
, false, image
);
558 // skip adding it again below
562 #endif // wxUSE_TREEBOOK
564 m_bookCtrl
->AddPage(page
, str
, false, image
);
567 const int sel
= oldBook
->GetSelection();
568 if ( sel
!= wxNOT_FOUND
)
569 m_bookCtrl
->SetSelection(sel
);
572 m_sizerFrame
->Detach(oldBook
);
577 CreateInitialPages(m_bookCtrl
);
580 m_sizerFrame
->Insert(0, m_bookCtrl
, wxSizerFlags(5).Expand().Border());
582 m_sizerFrame
->Show(m_bookCtrl
);
583 m_sizerFrame
->Layout();
586 void MyFrame::AddFlagStrIfFlagPresent(wxString
& flagStr
, long flags
, long flag
,
587 const wxChar
* flagName
) const
589 if( (flags
& flag
) == flag
)
591 if( !flagStr
.empty() )
592 flagStr
+= wxT(" | ");
597 wxPanel
*MyFrame::CreateNewPage() const
599 wxPanel
*panel
= new wxPanel(m_bookCtrl
, wxID_ANY
);
602 panel
->SetHelpText( wxT( "Panel with \"First\" and \"Second\" buttons" ) );
605 (void) new wxButton(panel
, wxID_ANY
, wxT("First button"), wxPoint(10, 30));
606 (void) new wxButton(panel
, wxID_ANY
, wxT("Second button"), wxPoint(150, 30));
612 //-----------------------------------------------------------------------------
613 // MyFrame - event handlers
614 //-----------------------------------------------------------------------------
618 void MyFrame::OnContextHelp(wxCommandEvent
& WXUNUSED(event
))
620 // launches local event loop
621 wxContextHelp
ch( this );
626 void MyFrame::OnHitTest(wxCommandEvent
& WXUNUSED(event
))
628 wxBookCtrlBase
* book
= GetCurrentBook();
629 const wxPoint pt
= ::wxGetMousePosition();
632 int pagePos
= book
->HitTest( book
->ScreenToClient(pt
), &flags
);
636 AddFlagStrIfFlagPresent( flagsStr
, flags
, wxBK_HITTEST_NOWHERE
, wxT("wxBK_HITTEST_NOWHERE") );
637 AddFlagStrIfFlagPresent( flagsStr
, flags
, wxBK_HITTEST_ONICON
, wxT("wxBK_HITTEST_ONICON") );
638 AddFlagStrIfFlagPresent( flagsStr
, flags
, wxBK_HITTEST_ONLABEL
, wxT("wxBK_HITTEST_ONLABEL") );
639 AddFlagStrIfFlagPresent( flagsStr
, flags
, wxBK_HITTEST_ONPAGE
, wxT("wxBK_HITTEST_ONPAGE") );
641 wxLogMessage(wxT("HitTest at (%d,%d): %d: %s"),
648 void MyFrame::OnType(wxCommandEvent
& event
)
650 m_type
= static_cast<BookType
>(event
.GetId() - ID_BOOK_NOTEBOOK
);
653 m_sizerFrame
->Hide(m_bookCtrl
);
660 void MyFrame::OnUpdateTreeMenu(wxUpdateUIEvent
& event
)
662 event
.Enable(m_type
== Type_Treebook
);
665 #endif // wxUSE_TREEBOOK
667 void MyFrame::OnOrient(wxCommandEvent
& event
)
669 m_orient
= event
.GetId();
671 m_sizerFrame
->Layout();
674 void MyFrame::OnShowImages(wxCommandEvent
& event
)
676 m_chkShowImages
= event
.IsChecked();
678 m_sizerFrame
->Layout();
681 void MyFrame::OnMulti(wxCommandEvent
& event
)
683 m_multi
= event
.IsChecked();
685 m_sizerFrame
->Layout();
686 wxLogMessage(wxT("Multiline setting works only in wxNotebook."));
689 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
694 void MyFrame::OnAddPage(wxCommandEvent
& WXUNUSED(event
))
696 wxBookCtrlBase
*currBook
= GetCurrentBook();
700 static unsigned s_pageAdded
= 0;
701 currBook
->AddPage(CreateNewPage(),
704 ADDED_PAGE_NAME
wxT("%u"),
708 GetIconIndex(currBook
));
712 void MyFrame::OnAddPageNoSelect(wxCommandEvent
& WXUNUSED(event
))
714 wxBookCtrlBase
*currBook
= GetCurrentBook();
718 static unsigned s_pageAdded
= 0;
719 currBook
->AddPage(CreateNewPage(),
722 ADDED_PAGE_NAME
wxT("%u"),
726 GetIconIndex(currBook
));
731 void MyFrame::OnAddSubPage(wxCommandEvent
& WXUNUSED(event
))
733 wxTreebook
*currBook
= wxDynamicCast(GetCurrentBook(), wxTreebook
);
736 const int selPos
= currBook
->GetSelection();
737 if ( selPos
== wxNOT_FOUND
)
739 wxLogError(wxT("Select the parent page first!"));
743 static unsigned s_subPageAdded
= 0;
744 currBook
->InsertSubPage
750 ADDED_SUB_PAGE_NAME
wxT("%u"),
754 GetIconIndex(currBook
)
759 void MyFrame::OnAddPageBefore(wxCommandEvent
& WXUNUSED(event
))
761 wxBookCtrlBase
*currBook
= GetCurrentBook();
764 const int selPos
= currBook
->GetSelection();
765 if ( selPos
== wxNOT_FOUND
)
767 wxLogError(wxT("Select the parent page first!"));
771 static unsigned s_subPageAdded
= 0;
772 currBook
->InsertPage(selPos
,
776 ADDED_PAGE_NAME_BEFORE
wxT("%u"),
780 GetIconIndex(currBook
));
783 #endif // wxUSE_TREEBOOK
785 void MyFrame::OnInsertPage(wxCommandEvent
& WXUNUSED(event
))
787 static unsigned s_pageIns
= 0;
789 wxBookCtrlBase
*currBook
= GetCurrentBook();
793 wxPanel
*panel
= CreateUserCreatedPage( currBook
);
795 currBook
->InsertPage( 0, panel
,
796 wxString::Format(INSERTED_PAGE_NAME
wxT("%u"), ++s_pageIns
), false,
797 GetIconIndex(currBook
) );
799 currBook
->SetSelection(0);
803 void MyFrame::OnDeleteCurPage(wxCommandEvent
& WXUNUSED(event
))
805 wxBookCtrlBase
*currBook
= GetCurrentBook();
809 int sel
= currBook
->GetSelection();
811 if (sel
!= wxNOT_FOUND
)
813 currBook
->DeletePage(sel
);
818 void MyFrame::OnDeleteLastPage(wxCommandEvent
& WXUNUSED(event
))
820 wxBookCtrlBase
*currBook
= GetCurrentBook();
824 int page
= currBook
->GetPageCount();
828 currBook
->DeletePage(page
- 1);
833 void MyFrame::OnNextPage(wxCommandEvent
& WXUNUSED(event
))
835 wxBookCtrlBase
*currBook
= GetCurrentBook();
839 currBook
->AdvanceSelection();
843 void MyFrame::OnChangeSelection(wxCommandEvent
& WXUNUSED(event
))
845 wxBookCtrlBase
*currBook
= GetCurrentBook();
848 currBook
->ChangeSelection(0);
851 void MyFrame::OnSetSelection(wxCommandEvent
& WXUNUSED(event
))
853 wxBookCtrlBase
*currBook
= GetCurrentBook();
856 currBook
->SetSelection(0);
859 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
861 static int s_nPages
= wxNOT_FOUND
;
862 static int s_nSel
= wxNOT_FOUND
;
863 static wxBookCtrlBase
*s_currBook
= NULL
;
865 wxBookCtrlBase
*currBook
= GetCurrentBook();
867 int nPages
= currBook
? currBook
->GetPageCount() : 0;
868 int nSel
= currBook
? currBook
->GetSelection() : wxNOT_FOUND
;
870 if ( nPages
!= s_nPages
|| nSel
!= s_nSel
|| s_currBook
!= currBook
)
874 s_currBook
= currBook
;
877 if ( nSel
== wxNOT_FOUND
)
878 selection
<< wxT("not found");
883 title
.Printf(wxT("Notebook and friends (%d pages, selection: %s)"), nPages
, selection
.c_str());
889 void MyFrame::OnBookCtrl(wxBookCtrlBaseEvent
& event
)
891 static const struct EventInfo
893 wxEventType typeChanged
,
900 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
901 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
904 #endif // wxUSE_NOTEBOOK
907 wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
,
908 wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
,
911 #endif // wxUSE_LISTBOOK
914 wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
,
915 wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
,
918 #endif // wxUSE_CHOICEBOOK
921 wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
,
922 wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
,
925 #endif // wxUSE_TREEBOOK
928 wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED
,
929 wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING
,
932 #endif // wxUSE_TOOLBOOK
939 const wxEventType eventType
= event
.GetEventType();
940 for ( size_t n
= 0; n
< WXSIZEOF(events
); n
++ )
942 const EventInfo
& ei
= events
[n
];
943 if ( eventType
== ei
.typeChanged
)
945 nameEvent
= wxT("Changed");
947 else if ( eventType
== ei
.typeChanging
)
949 const int idx
= event
.GetOldSelection();
951 // NB: can't use wxStaticCast here as wxBookCtrlBase is not in
953 const wxBookCtrlBase
* const
954 book
= static_cast<wxBookCtrlBase
*>(event
.GetEventObject());
955 if ( idx
!= wxNOT_FOUND
&&
956 book
&& book
->GetPageText(idx
) == VETO_PAGE_NAME
)
960 wxT("Are you sure you want to leave this page?\n")
961 wxT("(This demonstrates veto-ing)"),
962 wxT("Notebook sample"),
963 wxICON_QUESTION
| wxYES_NO
,
968 veto
= wxT(" (vetoed)");
972 nameEvent
= wxT("Changing");
974 else // skip end of the loop
979 nameControl
= ei
.name
;
983 static int s_num
= 0;
985 wxLogMessage(wxT("Event #%d: %s: %s (%d) new sel %d, old %d%s"),
990 event
.GetSelection(),
991 event
.GetOldSelection(),
995 m_text
->SetInsertionPointEnd();