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"
27 // Name of the Veto page
28 #define VETO_PAGE_NAME wxT("Veto")
34 // Create the main window
35 MyFrame
*frame
= new MyFrame( wxT("Notebook sample") );
37 // Problem with generic wxNotebook implementation whereby it doesn't size
38 // properly unless you set the size again
39 #if defined(__WIN16__) || defined(__WXMOTIF__)
41 frame
->GetSize(& width
, & height
);
42 frame
->SetSize(-1, -1, width
, height
);
50 MyNotebook
::MyNotebook(wxWindow
*parent
, wxWindowID id
,
51 const wxPoint
& pos
, const wxSize
& size
, long style
)
52 : wxNotebook(parent
, id
, pos
, size
, style
)
57 void MyNotebook
::CreateInitialPages()
59 wxPanel
*panel
= (wxPanel
*) NULL
;
60 wxBoxSizer
*sizerPanel
= (wxBoxSizer
*) NULL
;
62 // Create and add some panels to the notebook
66 panel
= new wxPanel(this);
68 sizerPanel
= new wxBoxSizer(wxVERTICAL
);
69 sizerPanel
->Add( new wxButton( panel
, -1, wxT("Button") ) );
70 sizerPanel
->Add( new wxTextCtrl(panel
, -1, wxT("1234"),
71 wxDefaultPosition
, wxSize(120, 150) ) );
72 panel
->SetSizer(sizerPanel
);
74 AddPage( panel
, wxT("Controls without sizer"), TRUE
, GetIconIndex() );
78 panel
= new wxPanel(this);
80 wxString animals
[] = { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
81 wxT("Sabre-toothed tiger"), wxT("T Rex") };
82 wxRadioBox
*radiobox1
= new wxRadioBox(panel
, -1, wxT("Choose one"),
83 wxDefaultPosition
, wxDefaultSize
, 5, animals
, 2, wxRA_SPECIFY_ROWS
);
85 wxString computers
[] = { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
87 wxRadioBox
*radiobox2
= new wxRadioBox(panel
, -1,
88 wxT("Choose your favourite"), wxDefaultPosition
, wxDefaultSize
,
89 4, computers
, 0, wxRA_SPECIFY_COLS
);
91 sizerPanel
= new wxBoxSizer(wxVERTICAL
);
92 sizerPanel
->Add(radiobox1
, 2, wxEXPAND
);
93 sizerPanel
->Add(radiobox2
, 1, wxEXPAND
);
94 panel
->SetSizer(sizerPanel
);
96 AddPage( panel
, wxT("Radiobuttons"), FALSE
, GetIconIndex() );
100 panel
= new wxPanel(this);
101 (void) new wxStaticText( panel
, -1,
102 wxT("This page intentionally left blank"), wxPoint(10, 10) );
103 AddPage( panel
, VETO_PAGE_NAME
, FALSE
, GetIconIndex() );
107 panel
= new wxPanel(this);
108 wxButton
*buttonBig
= new wxButton( panel
, -1, wxT("Big button"),
109 wxPoint(0, 0), wxSize(480, 360) );
111 sizerPanel
= new wxBoxSizer(wxVERTICAL
);
112 sizerPanel
->Add(buttonBig
, 1, wxEXPAND
);
113 panel
->SetSizer(sizerPanel
);
115 AddPage( panel
, wxT("Big button"), FALSE
, GetIconIndex() );
119 panel
= new wxPanel(this);
120 panel
->SetBackgroundColour( wxColour( wxT("MAROON") ) );
121 (void) new wxStaticText( panel
, -1,
122 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
123 InsertPage( 0, panel
, wxT("Inserted"), FALSE
, GetIconIndex() );
130 int MyNotebook
::GetIconIndex() const
134 int nImages
= m_imageList
->GetImageCount();
137 return GetPageCount() % nImages
;
144 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
146 : wxFrame((wxWindow
*) NULL
, -1, title
, pos
, size
, style
)
148 m_panel
= (wxPanel
*) NULL
;
149 m_notebook
= (MyNotebook
*) NULL
;
150 m_imageList
= (wxImageList
*) NULL
;
152 m_panel
= new wxPanel(this, -1, wxDefaultPosition
, wxDefaultSize
,
153 wxTAB_TRAVERSAL
|wxCLIP_CHILDREN
|wxNO_BORDER
);
155 // Create remaining controls
157 // must be in sync with Orient enum
158 wxString strOrientations
[] =
166 wxASSERT_MSG( WXSIZEOF(strOrientations
) == ORIENT_MAX
,
167 wxT("forgot to update something") );
169 m_radioOrient
= new wxRadioBox
171 m_panel
, ID_RADIO_ORIENT
,
172 wxT("&Tab orientation"),
173 wxDefaultPosition
, wxDefaultSize
,
174 WXSIZEOF(strOrientations
), strOrientations
,
178 m_chkShowImages
= new wxCheckBox( m_panel
, ID_CHK_SHOWIMAGES
,
179 wxT("&Show images") );
181 m_btnAddPage
= new wxButton( m_panel
, ID_BTN_ADD_PAGE
, wxT("&Add page") );
183 m_btnInsertPage
= new wxButton( m_panel
, ID_BTN_INSERT_PAGE
,
184 wxT("&Insert page") );
186 m_btnDeletePage
= new wxButton( m_panel
, ID_BTN_DELETE_PAGE
,
187 wxT("&Delete page") );
189 m_btnNextPage
= new wxButton( m_panel
, ID_BTN_NEXT_PAGE
,
192 m_btnExit
= new wxButton( m_panel
, wxID_OK
, wxT("&Exit") );
193 m_btnExit
->SetDefault();
195 m_notebook
= new MyNotebook(m_panel
, ID_NOTEBOOK
);
197 m_text
= new wxTextCtrl(m_panel
, -1, wxEmptyString
,
198 wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
| wxTE_READONLY
);
200 m_logTargetOld
= wxLog
::SetActiveTarget( new wxLogTextCtrl(m_text
) );
202 // Create the notebook's panels
203 m_notebook
->CreateInitialPages();
206 m_sizerFrame
= new wxBoxSizer(wxVERTICAL
);
208 m_sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
210 wxBoxSizer
*sizerLeft
= new wxBoxSizer(wxVERTICAL
);
212 sizerLeft
->Add(m_radioOrient
, 0, wxEXPAND
);
213 sizerLeft
->Add(m_chkShowImages
, 0, wxEXPAND
| wxTOP
, 4);
215 sizerLeft
->Add(0, 0, 1); // Spacer
217 sizerLeft
->Add(m_btnAddPage
, 0, wxEXPAND
| (wxTOP
| wxBOTTOM
), 4);
218 sizerLeft
->Add(m_btnInsertPage
, 0, wxEXPAND
| (wxTOP
| wxBOTTOM
), 4);
219 sizerLeft
->Add(m_btnDeletePage
, 0, wxEXPAND
| (wxTOP
| wxBOTTOM
), 4);
220 sizerLeft
->Add(m_btnNextPage
, 0, wxEXPAND
| (wxTOP
| wxBOTTOM
), 4);
222 sizerLeft
->Add(0, 0, 1); // Spacer
224 sizerLeft
->Add(m_btnExit
, 0, wxEXPAND
);
227 m_sizerTop
->Add(sizerLeft
, 0, wxEXPAND
| wxALL
, 4);
230 m_sizerFrame
->Add(m_sizerTop
, 1, wxEXPAND
);
231 m_sizerFrame
->Add(m_text
, 0, wxEXPAND
);
235 m_panel
->SetSizer(m_sizerFrame
);
237 m_panel
->SetAutoLayout(TRUE
);
239 m_sizerFrame
->Fit(this);
247 delete wxLog
::SetActiveTarget(m_logTargetOld
);
250 void MyFrame
::ReInitNotebook()
254 switch ( m_radioOrient
->GetSelection() )
257 wxFAIL_MSG( wxT("unknown notebook orientation") );
277 MyNotebook
*notebook
= m_notebook
;
280 m_notebook
= new MyNotebook(m_panel
, ID_NOTEBOOK
,
281 wxDefaultPosition
, wxDefaultSize
,
288 int sel
= notebook
->GetSelection();
290 int count
= notebook
->GetPageCount();
291 for ( int n
= 0; n
< count
; n
++ )
293 wxNotebookPage
*page
= notebook
->GetPage(0);
294 page
->Reparent(m_notebook
);
296 m_notebook
->AddPage( page
, notebook
->GetPageText(0), FALSE
,
297 m_notebook
->GetIconIndex() );
299 notebook
->RemovePage(0);
304 m_sizerTop
->Remove(m_sizerNotebook
);
312 m_notebook
->SetSelection(sel
);
316 m_sizerNotebook
= new wxNotebookSizer(m_notebook
);
317 m_sizerTop
->Add(m_sizerNotebook
, 1, wxEXPAND
| wxALL
, 4);
318 m_sizerTop
->Layout();
321 void MyFrame
::CreateImageList()
326 m_imageList
= (wxImageList
*) NULL
;
329 if ( m_chkShowImages
->IsChecked() )
331 // create a dummy image list with a few icons
334 m_imageList
= new wxImageList( size
.GetWidth(), size
.GetHeight() );
338 wxArtProvider
::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
)
343 wxArtProvider
::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
)
348 wxArtProvider
::GetIcon(wxART_WARNING
, wxART_OTHER
, size
)
353 wxArtProvider
::GetIcon(wxART_ERROR
, wxART_OTHER
, size
)
356 m_notebook
->SetImageList(m_imageList
);
361 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
362 EVT_RADIOBOX(ID_RADIO_ORIENT
, MyFrame
::OnCheckOrRadioBox
)
363 EVT_CHECKBOX(ID_CHK_SHOWIMAGES
, MyFrame
::OnCheckOrRadioBox
)
365 EVT_BUTTON(ID_BTN_ADD_PAGE
, MyFrame
::OnButtonAddPage
)
366 EVT_BUTTON(ID_BTN_INSERT_PAGE
, MyFrame
::OnButtonInsertPage
)
367 EVT_BUTTON(ID_BTN_DELETE_PAGE
, MyFrame
::OnButtonDeletePage
)
368 EVT_BUTTON(ID_BTN_NEXT_PAGE
, MyFrame
::OnButtonNextPage
)
369 EVT_BUTTON(wxID_OK
, MyFrame
::OnButtonExit
)
371 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyFrame
::OnNotebook
)
372 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK
, MyFrame
::OnNotebook
)
374 EVT_IDLE(MyFrame
::OnIdle
)
377 void MyFrame
::OnCheckOrRadioBox(wxCommandEvent
& event
)
382 void MyFrame
::OnButtonAddPage( wxCommandEvent
& WXUNUSED(event
) )
384 static size_t s_pageAdded
= 0;
386 wxPanel
*panel
= new wxPanel( m_notebook
, -1 );
387 (void) new wxButton( panel
, -1, wxT("Button"),
388 wxPoint(10, 10), wxSize(-1, -1) );
390 m_notebook
->AddPage(panel
, wxString
::Format(wxT("Added %u"),
391 ++s_pageAdded
), FALSE
, m_notebook
->GetIconIndex() );
394 void MyFrame
::OnButtonInsertPage( wxCommandEvent
& WXUNUSED(event
) )
396 static size_t s_pageIns
= 0;
398 wxPanel
*panel
= new wxPanel( m_notebook
, -1 );
399 (void) new wxButton( panel
, -1, wxT("Button"),
400 wxPoint(10, 10), wxSize(-1, -1) );
402 m_notebook
->InsertPage( 0, panel
,
403 wxString
::Format(wxT("Inserted %u"), ++s_pageIns
), FALSE
,
404 m_notebook
->GetIconIndex() );
406 m_notebook
->SetSelection(0);
409 void MyFrame
::OnButtonDeletePage( wxCommandEvent
& WXUNUSED(event
) )
411 int sel
= m_notebook
->GetSelection();
415 m_notebook
->DeletePage(sel
);
419 void MyFrame
::OnButtonNextPage( wxCommandEvent
& WXUNUSED(event
) )
421 m_notebook
->AdvanceSelection();
424 void MyFrame
::OnButtonExit( wxCommandEvent
& WXUNUSED(event
) )
429 void MyFrame
::OnNotebook(wxNotebookEvent
& event
)
431 wxString str
= wxT("Unknown notebook event");
433 wxEventType eventType
= event
.GetEventType();
435 if (eventType
== wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
437 str
= wxT("Notebook changed");
439 else if (eventType
== wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
441 int idx
= event
.GetOldSelection();
442 if ( idx
!= -1 && m_notebook
->GetPageText(idx
) == VETO_PAGE_NAME
)
447 wxT("Are you sure you want to leave this notebook page?\n")
448 wxT("(This demonstrates veto-ing)"),
449 wxT("Notebook sample"),
450 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
459 str
= wxT("Notebook changing");
462 static int s_numNotebookEvents
= 0;
464 wxLogMessage(wxT("Notebook event #%d: %s (%d)"),
465 s_numNotebookEvents
++, str
, eventType
);
467 m_text
->SetInsertionPointEnd();
472 void MyFrame
::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
474 static int s_nPages
= -1;
475 static int s_nSel
= -1;
477 int nPages
= m_notebook
->GetPageCount();
478 int nSel
= m_notebook
->GetSelection();
479 if ( nPages
!= s_nPages
|| nSel
!= s_nSel
)
485 title
.Printf(wxT("Notebook (%d pages, selection: %d)"), nPages
, nSel
);