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 // License: wxWindows license
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"
27 #if !defined(__WXMSW__) && !defined(__WXPM__)
28 #include "../sample.xpm"
35 // Create the main window
36 MyFrame
*frame
= new MyFrame( wxT("Notebook sample") );
38 // Problem with generic wxNotebook implementation whereby it doesn't size
39 // properly unless you set the size again
40 #if defined(__WXMOTIF__)
42 frame
->GetSize(& width
, & height
);
43 frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
51 wxPanel
*CreateUserCreatedPage(wxBookCtrlBase
*parent
)
53 wxPanel
*panel
= new wxPanel(parent
);
55 (void) new wxButton( panel
, wxID_ANY
, wxT("Button"),
56 wxPoint(10, 10), wxDefaultSize
);
61 wxPanel
*CreateRadioButtonsPage(wxBookCtrlBase
*parent
)
63 wxPanel
*panel
= new wxPanel(parent
);
65 wxString animals
[] = { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
66 wxT("Sabre-toothed tiger"), wxT("T Rex") };
68 wxRadioBox
*radiobox1
= new wxRadioBox(panel
, wxID_ANY
, wxT("Choose one"),
69 wxDefaultPosition
, wxDefaultSize
, 5, animals
, 2, wxRA_SPECIFY_ROWS
);
71 wxString computers
[] = { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
74 wxRadioBox
*radiobox2
= new wxRadioBox(panel
, wxID_ANY
,
75 wxT("Choose your favourite"), wxDefaultPosition
, wxDefaultSize
,
76 4, computers
, 0, wxRA_SPECIFY_COLS
);
78 wxBoxSizer
*sizerPanel
= new wxBoxSizer(wxVERTICAL
);
79 sizerPanel
->Add(radiobox1
, 2, wxEXPAND
);
80 sizerPanel
->Add(radiobox2
, 1, wxEXPAND
);
81 panel
->SetSizer(sizerPanel
);
86 wxPanel
*CreateVetoPage(wxBookCtrlBase
*parent
)
88 wxPanel
*panel
= new wxPanel(parent
);
90 (void) new wxStaticText( panel
, wxID_ANY
,
91 wxT("This page intentionally left blank"), wxPoint(10, 10) );
96 wxPanel
*CreateBigButtonPage(wxBookCtrlBase
*parent
)
98 wxPanel
*panel
= new wxPanel(parent
);
100 wxButton
*buttonBig
= new wxButton(panel
, wxID_ANY
, wxT("Maximized button"));
102 wxBoxSizer
*sizerPanel
= new wxBoxSizer(wxVERTICAL
);
103 sizerPanel
->Add(buttonBig
, 1, wxEXPAND
);
104 panel
->SetSizer(sizerPanel
);
110 wxPanel
*CreateInsertPage(wxBookCtrlBase
*parent
)
112 wxPanel
*panel
= new wxPanel(parent
);
114 panel
->SetBackgroundColour( wxColour( wxT("MAROON") ) );
115 (void) new wxStaticText( panel
, wxID_ANY
,
116 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
121 int GetIconIndex(wxBookCtrlBase
* bookCtrl
)
123 if (bookCtrl
&& bookCtrl
->GetImageList())
125 int nImages
= bookCtrl
->GetImageList()->GetImageCount();
128 return bookCtrl
->GetPageCount() % nImages
;
135 void CreateInitialPages(wxBookCtrlBase
*parent
)
137 // Create and add some panels to the notebook
139 wxPanel
*panel
= CreateRadioButtonsPage(parent
);
140 parent
->AddPage( panel
, RADIOBUTTONS_PAGE_NAME
, false, GetIconIndex(parent
) );
142 panel
= CreateVetoPage(parent
);
143 parent
->AddPage( panel
, VETO_PAGE_NAME
, false, GetIconIndex(parent
) );
145 panel
= CreateBigButtonPage(parent
);
146 parent
->AddPage( panel
, MAXIMIZED_BUTTON_PAGE_NAME
, false, GetIconIndex(parent
) );
148 panel
= CreateInsertPage(parent
);
149 parent
->InsertPage( 0, panel
, I_WAS_INSERTED_PAGE_NAME
, false, GetIconIndex(parent
) );
151 parent
->SetSelection(1);
154 wxPanel
*CreatePage(wxBookCtrlBase
*parent
, const wxString
&pageName
)
158 pageName
.Contains(INSERTED_PAGE_NAME
)
159 || pageName
.Contains(ADDED_PAGE_NAME
)
162 return CreateUserCreatedPage(parent
);
165 if (pageName
== I_WAS_INSERTED_PAGE_NAME
)
167 return CreateInsertPage(parent
);
170 if (pageName
== VETO_PAGE_NAME
)
172 return CreateVetoPage(parent
);
175 if (pageName
== RADIOBUTTONS_PAGE_NAME
)
177 return CreateRadioButtonsPage(parent
);
181 if (pageName
== MAXIMIZED_BUTTON_PAGE_NAME
)
183 return CreateBigButtonPage(parent
);
188 return (wxPanel
*) NULL
;
191 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
193 : wxFrame((wxWindow
*) NULL
, wxID_ANY
, title
, pos
, size
, style
)
196 m_type
= ID_BOOK_NOTEBOOK
;
197 #elif wxUSE_CHOICEBOOK
198 m_type
= ID_BOOK_CHOICEBOOK
;
200 m_type
= ID_BOOK_LISTBOOK
;
202 #error "Don't use Notebook sample without any book enabled in wxWidgets build!"
205 m_orient
= ID_ORIENT_DEFAULT
;
206 m_chkShowImages
= true;
209 SetIcon(wxICON(sample
));
211 // menu of the sample
212 wxMenu
*menuType
= new wxMenu
;
214 menuType
->AppendRadioItem(ID_BOOK_NOTEBOOK
, wxT("&Notebook\tCtrl-1"));
217 menuType
->AppendRadioItem(ID_BOOK_LISTBOOK
, wxT("&Listbook\tCtrl-2"));
220 menuType
->AppendRadioItem(ID_BOOK_CHOICEBOOK
, wxT("&Choicebook\tCtrl-3"));
222 menuType
->Check(m_type
, true);
224 wxMenu
*menuOrient
= new wxMenu
;
225 menuOrient
->AppendRadioItem(ID_ORIENT_DEFAULT
, wxT("&Default\tCtrl-4"));
226 menuOrient
->AppendRadioItem(ID_ORIENT_TOP
, wxT("&Top\tCtrl-5"));
227 menuOrient
->AppendRadioItem(ID_ORIENT_BOTTOM
, wxT("&Bottom\tCtrl-6"));
228 menuOrient
->AppendRadioItem(ID_ORIENT_LEFT
, wxT("&Left\tCtrl-7"));
229 menuOrient
->AppendRadioItem(ID_ORIENT_RIGHT
, wxT("&Right\tCtrl-8"));
231 wxMenu
*menuDo
= new wxMenu
;
232 menuDo
->Append(ID_ADD_PAGE
, wxT("&Add page\tAlt-A"));
233 menuDo
->Append(ID_INSERT_PAGE
, wxT("&Insert page\tAlt-I"));
234 menuDo
->Append(ID_DELETE_CUR_PAGE
, wxT("&Delete current page\tAlt-D"));
235 menuDo
->Append(ID_DELETE_LAST_PAGE
, wxT("D&elete last page\tAlt-L"));
236 menuDo
->Append(ID_NEXT_PAGE
, wxT("&Next page\tAlt-N"));
238 wxMenu
*menuFile
= new wxMenu
;
239 menuFile
->Append(wxID_ANY
, wxT("&Type"), menuType
, wxT("Type of control"));
240 menuFile
->Append(wxID_ANY
, wxT("&Orientation"), menuOrient
, wxT("Orientation of control"));
241 menuFile
->AppendCheckItem(ID_SHOW_IMAGES
, wxT("&Show images\tAlt-S"));
242 menuFile
->AppendCheckItem(ID_MULTI
, wxT("&Multiple lines\tAlt-M"));
243 menuFile
->AppendSeparator();
244 menuFile
->Append(wxID_EXIT
, wxT("E&xit"), wxT("Quits the application"));
245 menuFile
->Check(ID_SHOW_IMAGES
, m_chkShowImages
);
246 menuFile
->Check(ID_MULTI
, m_multi
);
248 wxMenuBar
*menuBar
= new wxMenuBar
;
249 menuBar
->Append(menuFile
, wxT("&File"));
250 menuBar
->Append(menuDo
, wxT("&Operations"));
255 m_panel
= (wxPanel
*) NULL
;
257 m_notebook
= (wxNotebook
*) NULL
;
260 m_choicebook
= (wxChoicebook
*) NULL
;
263 m_listbook
= (wxListbook
*) NULL
;
266 // create a dummy image list with a few icons
267 wxSize
imageSize(32, 32);
270 = new wxImageList( imageSize
.GetWidth(), imageSize
.GetHeight() );
274 wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, imageSize
)
279 wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, imageSize
)
284 wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, imageSize
)
289 wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, imageSize
)
292 m_panel
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
293 wxTAB_TRAVERSAL
| wxCLIP_CHILDREN
| wxNO_BORDER
| wxNO_FULL_REPAINT_ON_RESIZE
);
296 m_text
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
297 wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
| wxTE_READONLY
);
299 m_logTargetOld
= wxLog::SetActiveTarget( new wxLogTextCtrl(m_text
) );
303 m_sizerFrame
= new wxBoxSizer(wxVERTICAL
);
306 m_sizerFrame
->Add(m_text
, 1, wxEXPAND
);
311 m_panel
->SetSizer(m_sizerFrame
);
313 m_sizerFrame
->Fit(this);
314 m_sizerFrame
->SetSizeHints(this);
322 delete wxLog::SetActiveTarget(m_logTargetOld
);
328 m_imageList
= (wxImageList
*) NULL
;
332 int MyFrame::SelectFlag(int id
, int nb
, int lb
, int chb
)
336 case ID_NOTEBOOK
: return nb
;
337 case ID_LISTBOOK
: return lb
;
338 case ID_CHOICEBOOK
: return chb
;
343 #ifdef __SMARTPHONE__
349 #define RECREATE( wxBookType , idBook, oldBook , newBook ) \
353 switch ( m_orient ) \
355 case ID_ORIENT_TOP: \
356 flags = SelectFlag(idBook, wxNB_TOP, wxLB_TOP, wxCHB_TOP); \
359 case ID_ORIENT_BOTTOM: \
360 flags = SelectFlag(idBook, wxNB_BOTTOM, wxLB_BOTTOM, wxCHB_BOTTOM); \
363 case ID_ORIENT_LEFT: \
364 flags = SelectFlag(idBook, wxNB_LEFT, wxLB_LEFT, wxCHB_LEFT); \
367 case ID_ORIENT_RIGHT: \
368 flags = SelectFlag(idBook, wxNB_RIGHT, wxLB_RIGHT, wxCHB_RIGHT); \
372 flags = SelectFlag(idBook, wxNB_DEFAULT, wxLB_DEFAULT, wxCHB_DEFAULT); \
375 if ( m_multi && ( idBook == ID_NOTEBOOK ) ) \
376 flags |= wxNB_MULTILINE; \
378 wxBookType *oldBook = newBook; \
380 newBook = new wxBookType(m_panel, idBook, \
381 wxDefaultPosition, wxDefaultSize, \
384 if ( m_chkShowImages ) \
386 newBook->SetImageList(m_imageList); \
391 int sel = oldBook->GetSelection(); \
393 int count = oldBook->GetPageCount(); \
394 for (int n = 0; n < count; n++) \
396 wxString str = oldBook->GetPageText(n); \
398 wxWindow *page = CreatePage(newBook, str); \
399 newBook->AddPage(page, str, false, GetIconIndex(newBook) ); \
402 m_sizerFrame->Detach(oldBook); \
406 if (sel != wxNOT_FOUND) \
408 newBook->SetSelection(sel); \
414 CreateInitialPages(newBook); \
417 m_sizerFrame->Insert(0, newBook, 5, wxEXPAND | wxALL, MARGIN); \
419 m_sizerFrame->Hide(newBook); \
422 void MyFrame::RecreateBooks()
425 RECREATE( wxNotebook
, ID_NOTEBOOK
, notebook
, m_notebook
);
428 RECREATE( wxListbook
, ID_LISTBOOK
, listbook
, m_listbook
);
431 RECREATE( wxChoicebook
, ID_CHOICEBOOK
, choicebook
, m_choicebook
);
437 wxBookCtrlBase
*MyFrame::GetCurrentBook()
442 case ID_BOOK_NOTEBOOK
: return m_notebook
;
445 case ID_BOOK_LISTBOOK
: return m_listbook
;
448 case ID_BOOK_CHOICEBOOK
: return m_choicebook
;
454 void MyFrame::ShowCurrentBook()
459 case ID_BOOK_NOTEBOOK
: if(m_notebook
) m_sizerFrame
->Show(m_notebook
); break;
462 case ID_BOOK_LISTBOOK
: if(m_listbook
) m_sizerFrame
->Show(m_listbook
); break;
465 case ID_BOOK_CHOICEBOOK
: if(m_choicebook
) m_sizerFrame
->Show(m_choicebook
); break;
469 m_sizerFrame
->Layout();
472 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
474 EVT_MENU_RANGE(ID_BOOK_NOTEBOOK
,ID_BOOK_MAX
,MyFrame::OnType
)
475 EVT_MENU_RANGE(ID_ORIENT_DEFAULT
,ID_ORIENT_MAX
,MyFrame::OnOrient
)
476 EVT_MENU(ID_SHOW_IMAGES
, MyFrame::OnShowImages
)
477 EVT_MENU(ID_MULTI
, MyFrame::OnMulti
)
478 EVT_MENU(wxID_EXIT
,MyFrame::OnExit
)
481 EVT_MENU(ID_ADD_PAGE
, MyFrame::OnAddPage
)
482 EVT_MENU(ID_INSERT_PAGE
, MyFrame::OnInsertPage
)
483 EVT_MENU(ID_DELETE_CUR_PAGE
, MyFrame::OnDeleteCurPage
)
484 EVT_MENU(ID_DELETE_LAST_PAGE
, MyFrame::OnDeleteLastPage
)
485 EVT_MENU(ID_NEXT_PAGE
, MyFrame::OnNextPage
)
489 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyFrame::OnNotebook
)
490 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK
, MyFrame::OnNotebook
)
493 EVT_LISTBOOK_PAGE_CHANGED(ID_LISTBOOK
, MyFrame::OnListbook
)
494 EVT_LISTBOOK_PAGE_CHANGING(ID_LISTBOOK
, MyFrame::OnListbook
)
497 EVT_CHOICEBOOK_PAGE_CHANGED(ID_CHOICEBOOK
, MyFrame::OnChoicebook
)
498 EVT_CHOICEBOOK_PAGE_CHANGING(ID_CHOICEBOOK
, MyFrame::OnChoicebook
)
501 // Update title in idle time
502 EVT_IDLE(MyFrame::OnIdle
)
505 void MyFrame::OnType(wxCommandEvent
& event
)
507 wxBookCtrlBase
*currBook
= GetCurrentBook();
509 m_type
= event
.GetId();
512 m_sizerFrame
->Hide(currBook
);
517 void MyFrame::OnOrient(wxCommandEvent
& event
)
519 m_orient
= event
.GetId();
521 m_sizerFrame
->Layout();
524 void MyFrame::OnShowImages(wxCommandEvent
& event
)
526 m_chkShowImages
= event
.IsChecked();
528 m_sizerFrame
->Layout();
531 void MyFrame::OnMulti(wxCommandEvent
& event
)
533 m_multi
= event
.IsChecked();
535 m_sizerFrame
->Layout();
536 wxLogMessage(_T("Multiline setting works only in wxNotebook."));
539 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
544 void MyFrame::OnAddPage(wxCommandEvent
& WXUNUSED(event
))
546 static unsigned s_pageAdded
= 0;
548 wxBookCtrlBase
*currBook
= GetCurrentBook();
552 wxPanel
*panel
= new wxPanel( currBook
, wxID_ANY
);
553 (void) new wxButton( panel
, wxID_ANY
, wxT("First button"),
554 wxPoint(10, 10), wxDefaultSize
);
555 (void) new wxButton( panel
, wxID_ANY
, wxT("Second button"),
556 wxPoint(50, 100), wxDefaultSize
);
558 currBook
->AddPage(panel
, wxString::Format(ADDED_PAGE_NAME
wxT("%u"),
559 ++s_pageAdded
), true, GetIconIndex(currBook
) );
563 void MyFrame::OnInsertPage(wxCommandEvent
& WXUNUSED(event
))
565 static unsigned s_pageIns
= 0;
567 wxBookCtrlBase
*currBook
= GetCurrentBook();
571 wxPanel
*panel
= CreateUserCreatedPage( currBook
);
573 currBook
->InsertPage( 0, panel
,
574 wxString::Format(INSERTED_PAGE_NAME
wxT("%u"), ++s_pageIns
), false,
575 GetIconIndex(currBook
) );
577 currBook
->SetSelection(0);
581 void MyFrame::OnDeleteCurPage(wxCommandEvent
& WXUNUSED(event
))
583 wxBookCtrlBase
*currBook
= GetCurrentBook();
587 int sel
= currBook
->GetSelection();
589 if (sel
!= wxNOT_FOUND
)
591 currBook
->DeletePage(sel
);
596 void MyFrame::OnDeleteLastPage(wxCommandEvent
& WXUNUSED(event
))
598 wxBookCtrlBase
*currBook
= GetCurrentBook();
602 int page
= currBook
->GetPageCount();
606 currBook
->DeletePage(page
- 1);
611 void MyFrame::OnNextPage(wxCommandEvent
& WXUNUSED(event
))
613 wxBookCtrlBase
*currBook
= GetCurrentBook();
617 currBook
->AdvanceSelection();
621 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
623 static int s_nPages
= wxNOT_FOUND
;
624 static int s_nSel
= wxNOT_FOUND
;
625 static wxBookCtrlBase
*s_currBook
= NULL
;
627 wxBookCtrlBase
*currBook
= GetCurrentBook();
629 int nPages
= currBook
? currBook
->GetPageCount() : 0;
630 int nSel
= currBook
? currBook
->GetSelection() : wxNOT_FOUND
;
632 if ( nPages
!= s_nPages
|| nSel
!= s_nSel
|| s_currBook
!= currBook
)
636 s_currBook
= currBook
;
639 if ( nSel
== wxNOT_FOUND
)
640 selection
<< wxT("not found");
645 title
.Printf(wxT("Notebook and friends (%d pages, selection: %s)"), nPages
, selection
.c_str());
652 #define BOOKEVENT_LOG m_text->SetInsertionPointEnd();
654 #define BOOKEVENT_LOG
657 #define BOOKEVENT(OnBook,wxBookEvent,bookStr,wxEVT_PAGE_CHANGED,wxEVT_PAGE_CHANGING,s_num) \
658 void MyFrame::OnBook(wxBookEvent& event) \
660 wxString str = wxT("Unknown "); \
661 str << wxT(bookStr); \
662 str << wxT(" event"); \
664 wxEventType eventType = event.GetEventType(); \
666 if (eventType == wxEVT_PAGE_CHANGED) \
668 str = wxT("Changed"); \
670 else if (eventType == wxEVT_PAGE_CHANGING) \
672 int idx = event.GetOldSelection(); \
673 wxBookCtrlBase *book = (wxBookCtrlBase *)event.GetEventObject(); \
674 if ( idx != wxNOT_FOUND && book && book->GetPageText(idx) == VETO_PAGE_NAME ) \
679 wxT("Are you sure you want to leave this page?\n") \
680 wxT("(This demonstrates veto-ing)"), \
681 wxT("Notebook sample"), \
682 wxICON_QUESTION | wxYES_NO, this) != wxYES ) \
689 str = wxT("Changing"); \
692 static int s_num = 0; \
695 logMsg.Printf(wxT("%s event #%d: %s (%d) Sel %d, OldSel %d"), \
696 wxT(bookStr),s_num++, str.c_str(), eventType, \
697 event.GetSelection(), event.GetOldSelection()); \
699 wxLogMessage(logMsg.c_str()); \
705 BOOKEVENT(OnNotebook
,wxNotebookEvent
,"wxNotebook",wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,s_numNotebookEvents
)
708 BOOKEVENT(OnChoicebook
,wxChoicebookEvent
,"wxChoicebook",wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
,wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
,s_numChoicebookEvents
)
711 BOOKEVENT(OnListbook
,wxListbookEvent
,"wxListbook",wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
,wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
,s_numListbookEvents
)