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"
31 // Create the main window
32 MyFrame
*frame
= new MyFrame( wxT("Notebook sample") );
34 // Problem with generic wxNotebook implementation whereby it doesn't size
35 // properly unless you set the size again
36 #if defined(__WXMOTIF__)
38 frame
->GetSize(& width
, & height
);
39 frame
->SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
47 wxPanel
*CreateUserCreatedPage(wxBookCtrl
*parent
)
49 wxPanel
*panel
= new wxPanel(parent
);
51 (void) new wxButton( panel
, wxID_ANY
, wxT("Button"),
52 wxPoint(10, 10), wxDefaultSize
);
57 wxPanel
*CreateRadioButtonsPage(wxBookCtrl
*parent
)
59 wxPanel
*panel
= new wxPanel(parent
);
61 wxString animals
[] = { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
62 wxT("Sabre-toothed tiger"), wxT("T Rex") };
64 wxRadioBox
*radiobox1
= new wxRadioBox(panel
, wxID_ANY
, wxT("Choose one"),
65 wxDefaultPosition
, wxDefaultSize
, 5, animals
, 2, wxRA_SPECIFY_ROWS
);
67 wxString computers
[] = { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
70 wxRadioBox
*radiobox2
= new wxRadioBox(panel
, wxID_ANY
,
71 wxT("Choose your favourite"), wxDefaultPosition
, wxDefaultSize
,
72 4, computers
, 0, wxRA_SPECIFY_COLS
);
74 wxBoxSizer
*sizerPanel
= new wxBoxSizer(wxVERTICAL
);
75 sizerPanel
->Add(radiobox1
, 2, wxEXPAND
);
76 sizerPanel
->Add(radiobox2
, 1, wxEXPAND
);
77 panel
->SetSizer(sizerPanel
);
82 wxPanel
*CreateVetoPage(wxBookCtrl
*parent
)
84 wxPanel
*panel
= new wxPanel(parent
);
86 (void) new wxStaticText( panel
, wxID_ANY
,
87 wxT("This page intentionally left blank"), wxPoint(10, 10) );
92 wxPanel
*CreateBigButtonPage(wxBookCtrl
*parent
)
94 wxPanel
*panel
= new wxPanel(parent
);
96 wxButton
*buttonBig
= new wxButton(panel
, wxID_ANY
, wxT("Maximized button"));
98 wxBoxSizer
*sizerPanel
= new wxBoxSizer(wxVERTICAL
);
99 sizerPanel
->Add(buttonBig
, 1, wxEXPAND
);
100 panel
->SetSizer(sizerPanel
);
106 wxPanel
*CreateInsertPage(wxBookCtrl
*parent
)
108 wxPanel
*panel
= new wxPanel(parent
);
110 panel
->SetBackgroundColour( wxColour( wxT("MAROON") ) );
111 (void) new wxStaticText( panel
, wxID_ANY
,
112 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
117 int GetIconIndex(wxBookCtrl
* bookCtrl
)
119 if (bookCtrl
&& bookCtrl
->GetImageList())
121 int nImages
= bookCtrl
->GetImageList()->GetImageCount();
124 return bookCtrl
->GetPageCount() % nImages
;
131 void CreateInitialPages(wxBookCtrl
*parent
)
133 // Create and add some panels to the notebook
135 wxPanel
*panel
= CreateRadioButtonsPage(parent
);
136 parent
->AddPage( panel
, RADIOBUTTONS_PAGE_NAME
, false, GetIconIndex(parent
) );
138 panel
= CreateVetoPage(parent
);
139 parent
->AddPage( panel
, VETO_PAGE_NAME
, false, GetIconIndex(parent
) );
141 panel
= CreateBigButtonPage(parent
);
142 parent
->AddPage( panel
, MAXIMIZED_BUTTON_PAGE_NAME
, false, GetIconIndex(parent
) );
144 panel
= CreateInsertPage(parent
);
145 parent
->InsertPage( 0, panel
, I_WAS_INSERTED_PAGE_NAME
, false, GetIconIndex(parent
) );
147 parent
->SetSelection(1);
150 wxPanel
*CreatePage(wxBookCtrl
*parent
, const wxString
&pageName
)
154 pageName
.Contains(INSERTED_PAGE_NAME
)
155 || pageName
.Contains(ADDED_PAGE_NAME
)
158 return CreateUserCreatedPage(parent
);
161 if (pageName
== I_WAS_INSERTED_PAGE_NAME
)
163 return CreateInsertPage(parent
);
166 if (pageName
== VETO_PAGE_NAME
)
168 return CreateVetoPage(parent
);
171 if (pageName
== RADIOBUTTONS_PAGE_NAME
)
173 return CreateRadioButtonsPage(parent
);
177 if (pageName
== MAXIMIZED_BUTTON_PAGE_NAME
)
179 return CreateBigButtonPage(parent
);
184 return (wxPanel
*) NULL
;
187 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
189 : wxFrame((wxWindow
*) NULL
, wxID_ANY
, title
, pos
, size
, style
)
191 m_type
= ID_BOOK_NOTEBOOK
;
192 m_orient
= ID_ORIENT_DEFAULT
;
193 m_chkShowImages
= true;
196 // menu of the sample
198 wxMenu
*menuType
= new wxMenu
;
200 menuType
->AppendRadioItem(ID_BOOK_NOTEBOOK
, wxT("&Notebook\tCtrl-1"));
203 menuType
->AppendRadioItem(ID_BOOK_LISTBOOK
, wxT("&Listbook\tCtrl-2"));
206 menuType
->AppendRadioItem(ID_BOOK_CHOICEBOOK
, wxT("&Choicebook\tCtrl-3"));
209 wxMenu
*menuOrient
= new wxMenu
;
210 menuOrient
->AppendRadioItem(ID_ORIENT_DEFAULT
, wxT("&Default\tCtrl-4"));
211 menuOrient
->AppendRadioItem(ID_ORIENT_TOP
, wxT("&Top\tCtrl-5"));
212 menuOrient
->AppendRadioItem(ID_ORIENT_BOTTOM
, wxT("&Bottom\tCtrl-6"));
213 menuOrient
->AppendRadioItem(ID_ORIENT_LEFT
, wxT("&Left\tCtrl-7"));
214 menuOrient
->AppendRadioItem(ID_ORIENT_RIGHT
, wxT("&Right\tCtrl-8"));
216 wxMenu
*menuDo
= new wxMenu
;
217 menuDo
->Append(ID_ADD_PAGE
, wxT("&Add page"));
218 menuDo
->Append(ID_INSERT_PAGE
, wxT("&Insert page"));
219 menuDo
->Append(ID_DELETE_CUR_PAGE
, wxT("&Delete current page"));
220 menuDo
->Append(ID_DELETE_LAST_PAGE
, wxT("D&elete last page"));
221 menuDo
->Append(ID_NEXT_PAGE
, wxT("&Next page"));
223 wxMenu
*menuFile
= new wxMenu
;
224 menuFile
->Append(wxID_ANY
, wxT("&Type"), menuType
, wxT("Type of control"));
225 menuFile
->Append(wxID_ANY
, wxT("&Orientation"), menuOrient
, wxT("Orientation of control"));
226 menuFile
->AppendCheckItem(ID_SHOW_IMAGES
, wxT("&Show images"));
227 menuFile
->AppendCheckItem(ID_MULTI
, wxT("&Multiple lines"));
228 menuFile
->AppendSeparator();
229 menuFile
->Append(wxID_EXIT
, wxT("E&xit"), wxT("Quits the application"));
230 menuFile
->Check(ID_SHOW_IMAGES
, m_chkShowImages
);
231 menuFile
->Check(ID_MULTI
, m_multi
);
233 wxMenuBar
*menuBar
= new wxMenuBar
;
234 menuBar
->Append(menuFile
, wxT("&File"));
235 menuBar
->Append(menuDo
, wxT("&Operations"));
240 m_panel
= (wxPanel
*) NULL
;
241 m_notebook
= (wxNotebook
*) NULL
;
242 m_choicebook
= (wxChoicebook
*) NULL
;
243 m_listbook
= (wxListbook
*) NULL
;
245 // create a dummy image list with a few icons
246 wxSize
imageSize(32, 32);
249 = new wxImageList( imageSize
.GetWidth(), imageSize
.GetHeight() );
253 wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, imageSize
)
258 wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, imageSize
)
263 wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, imageSize
)
268 wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, imageSize
)
271 m_panel
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
272 wxTAB_TRAVERSAL
| wxCLIP_CHILDREN
| wxNO_BORDER
| wxNO_FULL_REPAINT_ON_RESIZE
);
275 m_text
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
276 wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
| wxTE_READONLY
);
278 m_logTargetOld
= wxLog::SetActiveTarget( new wxLogTextCtrl(m_text
) );
282 m_sizerFrame
= new wxBoxSizer(wxVERTICAL
);
285 m_sizerFrame
->Add(m_text
, 1, wxEXPAND
);
290 m_panel
->SetSizer(m_sizerFrame
);
292 m_sizerFrame
->Fit(this);
293 m_sizerFrame
->SetSizeHints(this);
301 delete wxLog::SetActiveTarget(m_logTargetOld
);
307 m_imageList
= (wxImageList
*) NULL
;
311 int MyFrame::SelectFlag(int id
, int nb
, int lb
, int chb
)
315 case ID_NOTEBOOK
: return nb
;
316 case ID_LISTBOOK
: return lb
;
317 case ID_CHOICEBOOK
: return chb
;
322 #define RECREATE( wxBookType , idBook, oldBook , newBook ) \
326 switch ( m_orient ) \
328 case ID_ORIENT_TOP: \
329 flags = SelectFlag(idBook, wxNB_TOP, wxLB_TOP, wxCHB_TOP); \
332 case ID_ORIENT_BOTTOM: \
333 flags = SelectFlag(idBook, wxNB_BOTTOM, wxLB_BOTTOM, wxCHB_BOTTOM); \
336 case ID_ORIENT_LEFT: \
337 flags = SelectFlag(idBook, wxNB_LEFT, wxLB_LEFT, wxCHB_LEFT); \
340 case ID_ORIENT_RIGHT: \
341 flags = SelectFlag(idBook, wxNB_RIGHT, wxLB_RIGHT, wxCHB_RIGHT); \
345 flags = SelectFlag(idBook, wxNB_DEFAULT, wxLB_DEFAULT, wxCHB_DEFAULT); \
348 if ( m_multi && ( idBook == ID_NOTEBOOK ) ) \
349 flags |= wxNB_MULTILINE; \
351 wxBookType *oldBook = newBook; \
353 newBook = new wxBookType(m_panel, idBook, \
354 wxDefaultPosition, wxDefaultSize, \
357 if ( m_chkShowImages ) \
359 newBook->SetImageList(m_imageList); \
364 int sel = oldBook->GetSelection(); \
366 int count = oldBook->GetPageCount(); \
367 for (int n = 0; n < count; n++) \
369 wxString str = oldBook->GetPageText(n); \
371 wxWindow *page = CreatePage(newBook, str); \
372 newBook->AddPage(page, str, false, GetIconIndex(newBook) ); \
375 m_sizerFrame->Detach(oldBook); \
379 if (sel != wxNOT_FOUND) \
381 newBook->SetSelection(sel); \
387 wxPanel *panel = CreateRadioButtonsPage(newBook); \
388 newBook->AddPage( panel, RADIOBUTTONS_PAGE_NAME, false, GetIconIndex(newBook) ); \
390 panel = CreateVetoPage(newBook); \
391 newBook->AddPage( panel, VETO_PAGE_NAME, false, GetIconIndex(newBook) ); \
393 panel = CreateBigButtonPage(newBook); \
394 newBook->AddPage( panel, MAXIMIZED_BUTTON_PAGE_NAME, false, GetIconIndex(newBook) ); \
396 panel = CreateInsertPage(newBook); \
397 newBook->InsertPage( 0, panel, I_WAS_INSERTED_PAGE_NAME, false, GetIconIndex(newBook) ); \
399 newBook->SetSelection(1); \
402 m_sizerFrame->Insert(0, newBook, 5, wxEXPAND | wxALL, 4); \
404 m_sizerFrame->Hide(newBook); \
407 void MyFrame::RecreateBooks()
409 RECREATE( wxNotebook
, ID_NOTEBOOK
, notebook
, m_notebook
);
410 RECREATE( wxListbook
, ID_LISTBOOK
, listbook
, m_listbook
);
411 RECREATE( wxChoicebook
, ID_CHOICEBOOK
, choicebook
, m_choicebook
);
416 wxBookCtrl
*MyFrame::GetCurrentBook()
420 case ID_BOOK_NOTEBOOK
: return m_notebook
;
421 case ID_BOOK_LISTBOOK
: return m_listbook
;
422 case ID_BOOK_CHOICEBOOK
: return m_choicebook
;
427 void MyFrame::ShowCurrentBook()
431 case ID_BOOK_NOTEBOOK
: if(m_notebook
) m_sizerFrame
->Show(m_notebook
); break;
432 case ID_BOOK_LISTBOOK
: if(m_listbook
) m_sizerFrame
->Show(m_listbook
); break;
433 case ID_BOOK_CHOICEBOOK
: if(m_choicebook
) m_sizerFrame
->Show(m_choicebook
); break;
436 m_sizerFrame
->Layout();
439 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
441 EVT_MENU_RANGE(ID_BOOK_NOTEBOOK
,ID_BOOK_MAX
,MyFrame::OnType
)
442 EVT_MENU_RANGE(ID_ORIENT_DEFAULT
,ID_ORIENT_MAX
,MyFrame::OnOrient
)
443 EVT_MENU(ID_SHOW_IMAGES
, MyFrame::OnShowImages
)
444 EVT_MENU(ID_MULTI
, MyFrame::OnMulti
)
445 EVT_MENU(wxID_EXIT
,MyFrame::OnExit
)
448 EVT_MENU(ID_ADD_PAGE
, MyFrame::OnAddPage
)
449 EVT_MENU(ID_INSERT_PAGE
, MyFrame::OnInsertPage
)
450 EVT_MENU(ID_DELETE_CUR_PAGE
, MyFrame::OnDeleteCurPage
)
451 EVT_MENU(ID_DELETE_LAST_PAGE
, MyFrame::OnDeleteLastPage
)
452 EVT_MENU(ID_NEXT_PAGE
, MyFrame::OnNextPage
)
455 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyFrame::OnNotebook
)
456 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK
, MyFrame::OnNotebook
)
457 EVT_LISTBOOK_PAGE_CHANGED(ID_LISTBOOK
, MyFrame::OnListbook
)
458 EVT_LISTBOOK_PAGE_CHANGING(ID_LISTBOOK
, MyFrame::OnListbook
)
459 EVT_CHOICEBOOK_PAGE_CHANGED(ID_CHOICEBOOK
, MyFrame::OnChoicebook
)
460 EVT_CHOICEBOOK_PAGE_CHANGING(ID_CHOICEBOOK
, MyFrame::OnChoicebook
)
462 // Update title in idle time
463 EVT_IDLE(MyFrame::OnIdle
)
466 void MyFrame::OnType(wxCommandEvent
& event
)
468 wxBookCtrl
*currBook
= GetCurrentBook();
470 m_type
= event
.GetId();
473 m_sizerFrame
->Hide(currBook
);
478 void MyFrame::OnOrient(wxCommandEvent
& event
)
480 m_orient
= event
.GetId();
482 m_sizerFrame
->Layout();
485 void MyFrame::OnShowImages(wxCommandEvent
& event
)
487 m_chkShowImages
= event
.IsChecked();
489 m_sizerFrame
->Layout();
492 void MyFrame::OnMulti(wxCommandEvent
& event
)
494 m_multi
= event
.IsChecked();
496 m_sizerFrame
->Layout();
497 wxLogMessage(_T("Multiline setting works only in wxNotebook."));
500 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
505 void MyFrame::OnAddPage(wxCommandEvent
& WXUNUSED(event
))
507 static unsigned s_pageAdded
= 0;
509 wxBookCtrl
*currBook
= GetCurrentBook();
513 wxPanel
*panel
= new wxPanel( currBook
, wxID_ANY
);
514 (void) new wxButton( panel
, wxID_ANY
, wxT("First button"),
515 wxPoint(10, 10), wxDefaultSize
);
516 (void) new wxButton( panel
, wxID_ANY
, wxT("Second button"),
517 wxPoint(50, 100), wxDefaultSize
);
519 currBook
->AddPage(panel
, wxString::Format(ADDED_PAGE_NAME
wxT("%u"),
520 ++s_pageAdded
), true, GetIconIndex(currBook
) );
524 void MyFrame::OnInsertPage(wxCommandEvent
& WXUNUSED(event
))
526 static unsigned s_pageIns
= 0;
528 wxBookCtrl
*currBook
= GetCurrentBook();
532 wxPanel
*panel
= CreateUserCreatedPage( currBook
);
534 currBook
->InsertPage( 0, panel
,
535 wxString::Format(INSERTED_PAGE_NAME
wxT("%u"), ++s_pageIns
), false,
536 GetIconIndex(currBook
) );
538 currBook
->SetSelection(0);
542 void MyFrame::OnDeleteCurPage(wxCommandEvent
& WXUNUSED(event
))
544 wxBookCtrl
*currBook
= GetCurrentBook();
548 int sel
= currBook
->GetSelection();
550 if (sel
!= wxNOT_FOUND
)
552 currBook
->DeletePage(sel
);
557 void MyFrame::OnDeleteLastPage(wxCommandEvent
& WXUNUSED(event
))
559 wxBookCtrl
*currBook
= GetCurrentBook();
563 int page
= currBook
->GetPageCount();
567 currBook
->DeletePage(page
- 1);
572 void MyFrame::OnNextPage(wxCommandEvent
& WXUNUSED(event
))
574 wxBookCtrl
*currBook
= GetCurrentBook();
578 currBook
->AdvanceSelection();
582 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
584 static int s_nPages
= wxNOT_FOUND
;
585 static int s_nSel
= wxNOT_FOUND
;
586 static wxBookCtrl
*s_currBook
= NULL
;
588 wxBookCtrl
*currBook
= GetCurrentBook();
590 int nPages
= currBook
? currBook
->GetPageCount() : 0;
591 int nSel
= currBook
? currBook
->GetSelection() : wxNOT_FOUND
;
593 if ( nPages
!= s_nPages
|| nSel
!= s_nSel
|| s_currBook
!= currBook
)
597 s_currBook
= currBook
;
600 if ( nSel
== wxNOT_FOUND
)
601 selection
<< wxT("not found");
606 title
.Printf(wxT("Notebook and friends (%d pages, selection: %s)"), nPages
, selection
.c_str());
612 #define BOOKEVENT(OnBook,wxBookEvent,bookStr,wxEVT_PAGE_CHANGED,wxEVT_PAGE_CHANGING,s_num) \
613 void MyFrame::OnBook(wxBookEvent& event) \
615 wxString str = wxT("Unknown "); \
616 str << wxT(bookStr); \
617 str << wxT(" event"); \
619 wxEventType eventType = event.GetEventType(); \
621 if (eventType == wxEVT_PAGE_CHANGED) \
623 str = wxT("Changed"); \
625 else if (eventType == wxEVT_PAGE_CHANGING) \
627 int idx = event.GetOldSelection(); \
628 wxBookCtrl *book = (wxBookCtrl *)event.GetEventObject(); \
629 if ( idx != wxNOT_FOUND && book && book->GetPageText(idx) == VETO_PAGE_NAME ) \
634 wxT("Are you sure you want to leave this page?\n") \
635 wxT("(This demonstrates veto-ing)"), \
636 wxT("Notebook sample"), \
637 wxICON_QUESTION | wxYES_NO, this) != wxYES ) \
646 str = wxT("Changing"); \
649 static int s_num = 0; \
652 logMsg.Printf(wxT("%s event #%d: %s (%d) Sel %d, OldSel %d"), \
653 wxT(bookStr),s_num++, str.c_str(), eventType, \
654 event.GetSelection(), event.GetOldSelection()); \
656 wxLogMessage(logMsg.c_str()); \
658 m_text->SetInsertionPointEnd(); \
663 BOOKEVENT(OnNotebook
,wxNotebookEvent
,"wxNotebook",wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,s_numNotebookEvents
)
664 BOOKEVENT(OnChoicebook
,wxChoicebookEvent
,"wxChoicebook",wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
,wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
,s_numChoicebookEvents
)
665 BOOKEVENT(OnListbook
,wxListbookEvent
,"wxListbook",wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
,wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
,s_numListbookEvents
)