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 MyNotebook::MyNotebook(wxWindow
*parent
, wxWindowID id
,
48 const wxPoint
& pos
, const wxSize
& size
, long style
)
49 : wxNotebook(parent
, id
, pos
, size
, style
)
54 wxPanel
*MyNotebook::CreatePage(const wxString
&pageName
)
58 pageName
.Contains(INSERTED_PAGE_NAME
)
59 || pageName
.Contains(ADDED_PAGE_NAME
)
62 return CreateUserCreatedPage();
65 if (pageName
== I_WAS_INSERTED_PAGE_NAME
)
67 return CreateInsertPage();
70 if (pageName
== VETO_PAGE_NAME
)
72 return CreateVetoPage();
75 if (pageName
== RADIOBUTTONS_PAGE_NAME
)
77 return CreateRadioButtonsPage();
81 if (pageName
== MAXIMIZED_BUTTON_PAGE_NAME
)
83 return CreateBigButtonPage();
88 return (wxPanel
*) NULL
;
91 wxPanel
*MyNotebook::CreateUserCreatedPage()
93 wxPanel
*panel
= new wxPanel(this);
95 (void) new wxButton( panel
, wxID_ANY
, wxT("Button"),
96 wxPoint(10, 10), wxDefaultSize
);
101 wxPanel
*MyNotebook::CreateRadioButtonsPage()
103 wxPanel
*panel
= new wxPanel(this);
105 wxString animals
[] = { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
106 wxT("Sabre-toothed tiger"), wxT("T Rex") };
108 wxRadioBox
*radiobox1
= new wxRadioBox(panel
, wxID_ANY
, wxT("Choose one"),
109 wxDefaultPosition
, wxDefaultSize
, 5, animals
, 2, wxRA_SPECIFY_ROWS
);
111 wxString computers
[] = { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
114 wxRadioBox
*radiobox2
= new wxRadioBox(panel
, wxID_ANY
,
115 wxT("Choose your favourite"), wxDefaultPosition
, wxDefaultSize
,
116 4, computers
, 0, wxRA_SPECIFY_COLS
);
118 wxBoxSizer
*sizerPanel
= new wxBoxSizer(wxVERTICAL
);
119 sizerPanel
->Add(radiobox1
, 2, wxEXPAND
);
120 sizerPanel
->Add(radiobox2
, 1, wxEXPAND
);
121 panel
->SetSizer(sizerPanel
);
126 wxPanel
*MyNotebook::CreateVetoPage()
128 wxPanel
*panel
= new wxPanel(this);
130 (void) new wxStaticText( panel
, wxID_ANY
,
131 wxT("This page intentionally left blank"), wxPoint(10, 10) );
136 wxPanel
*MyNotebook::CreateBigButtonPage()
138 wxPanel
*panel
= new wxPanel(this);
140 wxButton
*buttonBig
= new wxButton(panel
, wxID_ANY
, wxT("Maximized button"));
142 wxBoxSizer
*sizerPanel
= new wxBoxSizer(wxVERTICAL
);
143 sizerPanel
->Add(buttonBig
, 1, wxEXPAND
);
144 panel
->SetSizer(sizerPanel
);
150 wxPanel
*MyNotebook::CreateInsertPage()
152 wxPanel
*panel
= new wxPanel(this);
154 panel
->SetBackgroundColour( wxColour( wxT("MAROON") ) );
155 (void) new wxStaticText( panel
, wxID_ANY
,
156 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
161 void MyNotebook::CreateInitialPages()
163 // Create and add some panels to the notebook
165 wxPanel
*panel
= CreateRadioButtonsPage();
166 AddPage( panel
, RADIOBUTTONS_PAGE_NAME
, false, GetIconIndex() );
168 panel
= CreateVetoPage();
169 AddPage( panel
, VETO_PAGE_NAME
, false, GetIconIndex() );
171 panel
= CreateBigButtonPage();
172 AddPage( panel
, MAXIMIZED_BUTTON_PAGE_NAME
, false, GetIconIndex() );
174 panel
= CreateInsertPage();
175 InsertPage( 0, panel
, I_WAS_INSERTED_PAGE_NAME
, false, GetIconIndex() );
181 int MyNotebook::GetIconIndex() const
185 int nImages
= m_imageList
->GetImageCount();
188 return GetPageCount() % nImages
;
195 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
197 : wxFrame((wxWindow
*) NULL
, wxID_ANY
, title
, pos
, size
, style
)
199 m_panel
= (wxPanel
*) NULL
;
200 m_notebook
= (MyNotebook
*) NULL
;
202 // create a dummy image list with a few icons
203 wxSize
imageSize(32, 32);
206 = new wxImageList( imageSize
.GetWidth(), imageSize
.GetHeight() );
210 wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, imageSize
)
215 wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, imageSize
)
220 wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, imageSize
)
225 wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, imageSize
)
228 m_panel
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
229 wxTAB_TRAVERSAL
| wxCLIP_CHILDREN
| wxNO_BORDER
| wxNO_FULL_REPAINT_ON_RESIZE
);
231 // Create remaining controls
233 // must be in sync with Orient enum
234 wxString strOrientations
[] =
242 wxASSERT_MSG( WXSIZEOF(strOrientations
) == ORIENT_MAX
,
243 wxT("Forgot to update something") );
245 m_radioOrient
= new wxRadioBox
247 m_panel
, ID_RADIO_ORIENT
,
248 wxT("&Tab orientation"),
249 wxDefaultPosition
, wxDefaultSize
,
250 WXSIZEOF(strOrientations
), strOrientations
,
254 m_chkShowImages
= new wxCheckBox( m_panel
, ID_CHK_SHOWIMAGES
,
255 wxT("&Show images") );
256 #ifndef TEST_LISTBOOK
257 m_chkMultiLine
= new wxCheckBox( m_panel
, ID_CHK_MULTILINE
,
258 wxT("&Multiple lines") );
259 #endif // !TEST_LISTBOOK
261 m_btnAddPage
= new wxButton( m_panel
, ID_BTN_ADD_PAGE
, wxT("&Add page") );
263 m_btnInsertPage
= new wxButton( m_panel
, ID_BTN_INSERT_PAGE
,
264 wxT("&Insert page") );
266 m_btnDeleteCurPage
= new wxButton( m_panel
, ID_BTN_DELETE_CUR_PAGE
,
267 wxT("&Delete current page") );
269 m_btnDeleteLastPage
= new wxButton( m_panel
, ID_BTN_DELETE_LAST_PAGE
,
270 wxT("Delete las&t page") );
272 m_btnNextPage
= new wxButton( m_panel
, ID_BTN_NEXT_PAGE
,
275 m_btnExit
= new wxButton( m_panel
, wxID_OK
, wxT("&Exit") );
276 m_btnExit
->SetDefault();
279 m_text
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
280 wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
| wxTE_READONLY
);
282 m_logTargetOld
= wxLog::SetActiveTarget( new wxLogTextCtrl(m_text
) );
286 m_sizerFrame
= new wxBoxSizer(wxVERTICAL
);
288 m_sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
290 wxBoxSizer
*sizerLeft
= new wxBoxSizer(wxVERTICAL
);
291 sizerLeft
->Add(m_radioOrient
, 0, wxEXPAND
);
292 sizerLeft
->Add(m_chkShowImages
, 0, wxEXPAND
| wxTOP
, 4);
293 #ifndef TEST_LISTBOOK
294 sizerLeft
->Add(m_chkMultiLine
, 0, wxEXPAND
| wxTOP
, 4);
295 #endif // !TEST_LISTBOOK
297 sizerLeft
->Add(0, 0, 1); // Spacer
299 sizerLeft
->Add(m_btnAddPage
, 0, wxEXPAND
| (wxTOP
| wxBOTTOM
), 4);
300 sizerLeft
->Add(m_btnInsertPage
, 0, wxEXPAND
| (wxTOP
| wxBOTTOM
), 4);
301 sizerLeft
->Add(m_btnDeleteCurPage
, 0, wxEXPAND
| (wxTOP
| wxBOTTOM
), 4);
302 sizerLeft
->Add(m_btnDeleteLastPage
, 0, wxEXPAND
| (wxTOP
| wxBOTTOM
), 4);
303 sizerLeft
->Add(m_btnNextPage
, 0, wxEXPAND
| (wxTOP
| wxBOTTOM
), 4);
305 sizerLeft
->Add(0, 0, 1); // Spacer
307 sizerLeft
->Add(m_btnExit
, 0, wxEXPAND
);
309 m_sizerTop
->Add(sizerLeft
, 0, wxEXPAND
| wxALL
, 4);
312 m_sizerFrame
->Add(m_sizerTop
, 1, wxEXPAND
);
314 m_sizerFrame
->Add(m_text
, 0, wxEXPAND
);
318 m_notebook
->CreateInitialPages();
320 m_panel
->SetSizer(m_sizerFrame
);
322 m_sizerFrame
->Fit(this);
323 m_sizerFrame
->SetSizeHints(this);
332 delete wxLog::SetActiveTarget(m_logTargetOld
);
338 m_imageList
= (wxImageList
*) NULL
;
343 void MyFrame::ReInitNotebook()
347 switch ( m_radioOrient
->GetSelection() )
350 wxFAIL_MSG( wxT("unknown notebook orientation") );
370 #ifndef TEST_LISTBOOK
371 if ( m_chkMultiLine
->IsChecked() )
372 flags
|= wxNB_MULTILINE
;
373 #endif // !TEST_LISTBOOK
375 MyNotebook
*notebook
= m_notebook
;
377 m_notebook
= new MyNotebook(m_panel
, ID_NOTEBOOK
,
378 wxDefaultPosition
, wxDefaultSize
,
381 if ( m_chkShowImages
->IsChecked() )
383 m_notebook
->SetImageList(m_imageList
);
388 int sel
= notebook
->GetSelection();
390 int count
= notebook
->GetPageCount();
391 for (int n
= 0; n
< count
; n
++)
393 wxString str
= notebook
->GetPageText(n
);
395 wxWindow
*page
= m_notebook
->CreatePage(str
);
396 m_notebook
->AddPage(page
, str
, false, m_notebook
->GetIconIndex() );
399 m_sizerTop
->Detach(notebook
);
406 m_notebook
->SetSelection(sel
);
412 m_sizerTop
->Add(m_notebook
, 1, wxEXPAND
| wxALL
, 4);
413 m_sizerTop
->Layout();
416 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
417 EVT_RADIOBOX(ID_RADIO_ORIENT
, MyFrame::OnCheckOrRadioBox
)
418 EVT_CHECKBOX(ID_CHK_SHOWIMAGES
, MyFrame::OnCheckOrRadioBox
)
419 #ifndef TEST_LISTBOOK
420 EVT_CHECKBOX(ID_CHK_MULTILINE
, MyFrame::OnCheckOrRadioBox
)
421 #endif // !TEST_LISTBOOK
423 EVT_BUTTON(ID_BTN_ADD_PAGE
, MyFrame::OnButtonAddPage
)
424 EVT_BUTTON(ID_BTN_INSERT_PAGE
, MyFrame::OnButtonInsertPage
)
425 EVT_BUTTON(ID_BTN_DELETE_CUR_PAGE
, MyFrame::OnButtonDeleteCurPage
)
426 EVT_BUTTON(ID_BTN_DELETE_LAST_PAGE
, MyFrame::OnButtonDeleteLastPage
)
427 EVT_BUTTON(ID_BTN_NEXT_PAGE
, MyFrame::OnButtonNextPage
)
428 EVT_BUTTON(wxID_OK
, MyFrame::OnButtonExit
)
430 EVT_UPDATE_UI(ID_BTN_DELETE_CUR_PAGE
, MyFrame::OnUpdateUIBtnDeleteCurPage
)
431 EVT_UPDATE_UI(ID_BTN_DELETE_LAST_PAGE
, MyFrame::OnUpdateUIBtnDeleteLastPage
)
433 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyFrame::OnNotebook
)
434 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK
, MyFrame::OnNotebook
)
436 EVT_IDLE(MyFrame::OnIdle
)
439 void MyFrame::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
444 void MyFrame::OnButtonAddPage( wxCommandEvent
& WXUNUSED(event
) )
446 static unsigned s_pageAdded
= 0;
448 wxPanel
*panel
= new wxPanel( m_notebook
, wxID_ANY
);
449 (void) new wxButton( panel
, wxID_ANY
, wxT("First button"),
450 wxPoint(10, 10), wxDefaultSize
);
451 (void) new wxButton( panel
, wxID_ANY
, wxT("Second button"),
452 wxPoint(50, 100), wxDefaultSize
);
454 m_notebook
->AddPage(panel
, wxString::Format(ADDED_PAGE_NAME
wxT("%u"),
455 ++s_pageAdded
), true, m_notebook
->GetIconIndex() );
458 void MyFrame::OnButtonInsertPage( wxCommandEvent
& WXUNUSED(event
) )
460 static unsigned s_pageIns
= 0;
462 wxPanel
*panel
= m_notebook
->CreateUserCreatedPage();
464 m_notebook
->InsertPage( 0, panel
,
465 wxString::Format(INSERTED_PAGE_NAME
wxT("%u"), ++s_pageIns
), false,
466 m_notebook
->GetIconIndex() );
468 m_notebook
->SetSelection(0);
471 void MyFrame::OnButtonDeleteLastPage( wxCommandEvent
& WXUNUSED(event
) )
473 int page
= m_notebook
->GetPageCount();
477 m_notebook
->DeletePage(page
- 1);
481 void MyFrame::OnButtonDeleteCurPage( wxCommandEvent
& WXUNUSED(event
) )
483 int sel
= m_notebook
->GetSelection();
487 m_notebook
->DeletePage(sel
);
491 void MyFrame::OnButtonNextPage( wxCommandEvent
& WXUNUSED(event
) )
493 m_notebook
->AdvanceSelection();
496 void MyFrame::OnButtonExit( wxCommandEvent
& WXUNUSED(event
) )
501 void MyFrame::OnNotebook(wxNotebookEvent
& event
)
503 wxString str
= wxT("Unknown notebook event");
505 wxEventType eventType
= event
.GetEventType();
507 if (eventType
== wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
509 str
= wxT("Notebook changed");
511 else if (eventType
== wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
513 int idx
= event
.GetOldSelection();
514 if ( idx
!= -1 && m_notebook
->GetPageText(idx
) == VETO_PAGE_NAME
)
519 wxT("Are you sure you want to leave this notebook page?\n")
520 wxT("(This demonstrates veto-ing)"),
521 wxT("Notebook sample"),
522 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
531 str
= wxT("Notebook changing");
534 static int s_numNotebookEvents
= 0;
536 wxLogMessage(wxT("Notebook event #%d: %s (%d)"),
537 s_numNotebookEvents
++, str
.c_str(), eventType
);
540 m_text
->SetInsertionPointEnd();
546 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
548 static int s_nPages
= -1;
549 static int s_nSel
= -1;
551 int nPages
= m_notebook
->GetPageCount();
552 int nSel
= m_notebook
->GetSelection();
553 if ( nPages
!= s_nPages
|| nSel
!= s_nSel
)
559 title
.Printf(wxT("Notebook (%d pages, selection: %d)"), nPages
, nSel
);
565 void MyFrame::OnUpdateUIBtnDeleteCurPage(wxUpdateUIEvent
& event
)
567 event
.Enable( m_notebook
->GetSelection() != -1 );
570 void MyFrame::OnUpdateUIBtnDeleteLastPage(wxUpdateUIEvent
& event
)
572 event
.Enable( m_notebook
->GetPageCount() != 0 );