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 wxWindows 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(__WIN16__) || defined(__WXMOTIF__)
38 frame
->GetSize(& width
, & height
);
39 frame
->SetSize(-1, -1, 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
, -1, wxT("Button"),
96 wxPoint(10, 10), wxSize(-1, -1) );
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
, -1, 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
, -1,
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
, -1,
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
, -1, wxT("Maximized button"),
141 wxPoint(0, 0), wxSize(480, 360) );
143 wxBoxSizer
*sizerPanel
= new wxBoxSizer(wxVERTICAL
);
144 sizerPanel
->Add(buttonBig
, 1, wxEXPAND
);
145 panel
->SetSizer(sizerPanel
);
151 wxPanel
*MyNotebook::CreateInsertPage()
153 wxPanel
*panel
= new wxPanel(this);
155 panel
->SetBackgroundColour( wxColour( wxT("MAROON") ) );
156 (void) new wxStaticText( panel
, -1,
157 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
162 void MyNotebook::CreateInitialPages()
164 wxPanel
*panel
= (wxPanel
*) NULL
;
166 // Create and add some panels to the notebook
168 panel
= CreateRadioButtonsPage();
169 AddPage( panel
, RADIOBUTTONS_PAGE_NAME
, FALSE
, GetIconIndex() );
171 panel
= CreateVetoPage();
172 AddPage( panel
, VETO_PAGE_NAME
, FALSE
, GetIconIndex() );
174 panel
= CreateBigButtonPage();
175 AddPage( panel
, MAXIMIZED_BUTTON_PAGE_NAME
, FALSE
, GetIconIndex() );
177 panel
= CreateInsertPage();
178 InsertPage( 0, panel
, I_WAS_INSERTED_PAGE_NAME
, FALSE
, GetIconIndex() );
184 int MyNotebook::GetIconIndex() const
188 int nImages
= m_imageList
->GetImageCount();
191 return GetPageCount() % nImages
;
198 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
200 : wxFrame((wxWindow
*) NULL
, -1, title
, pos
, size
, style
)
202 m_panel
= (wxPanel
*) NULL
;
203 m_notebook
= (MyNotebook
*) NULL
;
205 // create a dummy image list with a few icons
206 wxSize
imageSize(32, 32);
209 = new wxImageList( imageSize
.GetWidth(), imageSize
.GetHeight() );
213 wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, imageSize
)
218 wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, imageSize
)
223 wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, imageSize
)
228 wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, imageSize
)
231 m_panel
= new wxPanel(this, -1, wxDefaultPosition
, wxDefaultSize
,
232 wxTAB_TRAVERSAL
| wxCLIP_CHILDREN
| wxNO_BORDER
);
234 // Create remaining controls
236 // must be in sync with Orient enum
237 wxString strOrientations
[] =
245 wxASSERT_MSG( WXSIZEOF(strOrientations
) == ORIENT_MAX
,
246 wxT("Forgot to update something") );
248 m_radioOrient
= new wxRadioBox
250 m_panel
, ID_RADIO_ORIENT
,
251 wxT("&Tab orientation"),
252 wxDefaultPosition
, wxDefaultSize
,
253 WXSIZEOF(strOrientations
), strOrientations
,
257 m_chkShowImages
= new wxCheckBox( m_panel
, ID_CHK_SHOWIMAGES
,
258 wxT("&Show images") );
260 m_btnAddPage
= new wxButton( m_panel
, ID_BTN_ADD_PAGE
, wxT("&Add page") );
262 m_btnInsertPage
= new wxButton( m_panel
, ID_BTN_INSERT_PAGE
,
263 wxT("&Insert page") );
265 m_btnDeleteCurPage
= new wxButton( m_panel
, ID_BTN_DELETE_CUR_PAGE
,
266 wxT("&Delete current page") );
268 m_btnDeleteLastPage
= new wxButton( m_panel
, ID_BTN_DELETE_LAST_PAGE
,
269 wxT("Delete las&t page") );
271 m_btnNextPage
= new wxButton( m_panel
, ID_BTN_NEXT_PAGE
,
274 m_btnExit
= new wxButton( m_panel
, wxID_OK
, wxT("&Exit") );
275 m_btnExit
->SetDefault();
277 m_notebook
= new MyNotebook(m_panel
, ID_NOTEBOOK
);
279 m_text
= new wxTextCtrl(m_panel
, -1, wxEmptyString
,
280 wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
| wxTE_READONLY
);
282 m_logTargetOld
= wxLog::SetActiveTarget( new wxLogTextCtrl(m_text
) );
284 // Create the notebook's panels
285 m_notebook
->CreateInitialPages();
288 m_sizerFrame
= new wxBoxSizer(wxVERTICAL
);
290 m_sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
292 wxBoxSizer
*sizerLeft
= new wxBoxSizer(wxVERTICAL
);
294 sizerLeft
->Add(m_radioOrient
, 0, wxEXPAND
);
295 sizerLeft
->Add(m_chkShowImages
, 0, wxEXPAND
| wxTOP
, 4);
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
);
310 m_sizerTop
->Add(sizerLeft
, 0, wxEXPAND
| wxALL
, 4);
313 m_sizerFrame
->Add(m_sizerTop
, 1, wxEXPAND
);
314 m_sizerFrame
->Add(m_text
, 0, wxEXPAND
);
318 m_panel
->SetSizer(m_sizerFrame
);
320 m_panel
->SetAutoLayout(TRUE
);
322 m_sizerFrame
->Fit(this);
330 delete wxLog::SetActiveTarget(m_logTargetOld
);
335 m_imageList
= (wxImageList
*) NULL
;
340 void MyFrame::ReInitNotebook()
344 switch ( m_radioOrient
->GetSelection() )
347 wxFAIL_MSG( wxT("unknown notebook orientation") );
367 MyNotebook
*notebook
= m_notebook
;
369 m_notebook
= new MyNotebook(m_panel
, ID_NOTEBOOK
,
370 wxDefaultPosition
, wxDefaultSize
,
373 if ( m_chkShowImages
->IsChecked() )
375 m_notebook
->SetImageList(m_imageList
);
380 int sel
= notebook
->GetSelection();
382 int count
= notebook
->GetPageCount();
383 for (int n
= 0; n
< count
; n
++)
385 wxString str
= notebook
->GetPageText(n
);
387 wxNotebookPage
*page
= m_notebook
->CreatePage(str
);
388 m_notebook
->AddPage(page
, str
, FALSE
, m_notebook
->GetIconIndex() );
393 m_sizerTop
->Remove(m_sizerNotebook
);
401 m_notebook
->SetSelection(sel
);
407 m_sizerNotebook
= new wxNotebookSizer(m_notebook
);
408 m_sizerTop
->Add(m_sizerNotebook
, 1, wxEXPAND
| wxALL
, 4);
409 m_sizerTop
->Layout();
412 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
413 EVT_RADIOBOX(ID_RADIO_ORIENT
, MyFrame::OnCheckOrRadioBox
)
414 EVT_CHECKBOX(ID_CHK_SHOWIMAGES
, MyFrame::OnCheckOrRadioBox
)
416 EVT_BUTTON(ID_BTN_ADD_PAGE
, MyFrame::OnButtonAddPage
)
417 EVT_BUTTON(ID_BTN_INSERT_PAGE
, MyFrame::OnButtonInsertPage
)
418 EVT_BUTTON(ID_BTN_DELETE_CUR_PAGE
, MyFrame::OnButtonDeleteCurPage
)
419 EVT_BUTTON(ID_BTN_DELETE_LAST_PAGE
, MyFrame::OnButtonDeleteLastPage
)
420 EVT_BUTTON(ID_BTN_NEXT_PAGE
, MyFrame::OnButtonNextPage
)
421 EVT_BUTTON(wxID_OK
, MyFrame::OnButtonExit
)
423 EVT_UPDATE_UI(ID_BTN_DELETE_CUR_PAGE
, MyFrame::OnUpdateUIBtnDeleteCurPage
)
424 EVT_UPDATE_UI(ID_BTN_DELETE_LAST_PAGE
, MyFrame::OnUpdateUIBtnDeleteLastPage
)
426 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyFrame::OnNotebook
)
427 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK
, MyFrame::OnNotebook
)
429 EVT_IDLE(MyFrame::OnIdle
)
432 void MyFrame::OnCheckOrRadioBox(wxCommandEvent
& event
)
437 void MyFrame::OnButtonAddPage( wxCommandEvent
& WXUNUSED(event
) )
439 static size_t s_pageAdded
= 0;
441 wxPanel
*panel
= new wxPanel( m_notebook
, -1 );
442 (void) new wxButton( panel
, -1, wxT("First button"),
443 wxPoint(10, 10), wxSize(-1, -1) );
444 (void) new wxButton( panel
, -1, wxT("Second button"),
445 wxPoint(50, 100), wxSize(-1, -1) );
447 m_notebook
->AddPage(panel
, wxString::Format(ADDED_PAGE_NAME
wxT("%u"),
448 ++s_pageAdded
), TRUE
, m_notebook
->GetIconIndex() );
451 void MyFrame::OnButtonInsertPage( wxCommandEvent
& WXUNUSED(event
) )
453 static size_t s_pageIns
= 0;
455 wxPanel
*panel
= m_notebook
->CreateUserCreatedPage();
457 m_notebook
->InsertPage( 0, panel
,
458 wxString::Format(INSERTED_PAGE_NAME
wxT("%u"), ++s_pageIns
), FALSE
,
459 m_notebook
->GetIconIndex() );
461 m_notebook
->SetSelection(0);
464 void MyFrame::OnButtonDeleteLastPage( wxCommandEvent
& WXUNUSED(event
) )
466 int page
= m_notebook
->GetPageCount();
470 m_notebook
->DeletePage(page
- 1);
474 void MyFrame::OnButtonDeleteCurPage( wxCommandEvent
& WXUNUSED(event
) )
476 int sel
= m_notebook
->GetSelection();
480 m_notebook
->DeletePage(sel
);
484 void MyFrame::OnButtonNextPage( wxCommandEvent
& WXUNUSED(event
) )
486 m_notebook
->AdvanceSelection();
489 void MyFrame::OnButtonExit( wxCommandEvent
& WXUNUSED(event
) )
494 void MyFrame::OnNotebook(wxNotebookEvent
& event
)
496 wxString str
= wxT("Unknown notebook event");
498 wxEventType eventType
= event
.GetEventType();
500 if (eventType
== wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
502 str
= wxT("Notebook changed");
504 else if (eventType
== wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
506 int idx
= event
.GetOldSelection();
507 if ( idx
!= -1 && m_notebook
->GetPageText(idx
) == VETO_PAGE_NAME
)
512 wxT("Are you sure you want to leave this notebook page?\n")
513 wxT("(This demonstrates veto-ing)"),
514 wxT("Notebook sample"),
515 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
524 str
= wxT("Notebook changing");
527 static int s_numNotebookEvents
= 0;
529 wxLogMessage(wxT("Notebook event #%d: %s (%d)"),
530 s_numNotebookEvents
++, str
.c_str(), eventType
);
532 m_text
->SetInsertionPointEnd();
537 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
539 static int s_nPages
= -1;
540 static int s_nSel
= -1;
542 int nPages
= m_notebook
->GetPageCount();
543 int nSel
= m_notebook
->GetSelection();
544 if ( nPages
!= s_nPages
|| nSel
!= s_nSel
)
550 title
.Printf(wxT("Notebook (%d pages, selection: %d)"), nPages
, nSel
);
556 void MyFrame::OnUpdateUIBtnDeleteCurPage(wxUpdateUIEvent
& event
)
558 event
.Enable( m_notebook
->GetSelection() != -1 );
561 void MyFrame::OnUpdateUIBtnDeleteLastPage(wxUpdateUIEvent
& event
)
563 event
.Enable( m_notebook
->GetPageCount() != 0 );