1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxNotebook
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/combobox.h"
35 #include "wx/radiobox.h"
36 #include "wx/statbox.h"
37 #include "wx/textctrl.h"
39 #include "wx/dynarray.h"
43 #include "wx/notebook.h"
44 #include "wx/artprov.h"
48 #include "icons/notebook.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 NotebookPage_Reset
= 100,
58 NotebookPage_SelectPage
,
60 NotebookPage_InsertPage
,
61 NotebookPage_RemovePage
,
62 NotebookPage_DeleteAll
,
63 NotebookPage_InsertText
,
64 NotebookPage_RemoveText
,
65 NotebookPage_SelectText
,
66 NotebookPage_NumPagesText
,
67 NotebookPage_CurSelectText
,
71 // notebook orientations
81 // old versions of wxWidgets don't define this style
86 // ----------------------------------------------------------------------------
87 // NotebookWidgetsPage
88 // ----------------------------------------------------------------------------
90 class NotebookWidgetsPage
: public WidgetsPage
93 NotebookWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
94 virtual ~NotebookWidgetsPage();
96 virtual wxControl
*GetWidget() const { return m_notebook
; }
100 void OnPageChanging(wxNotebookEvent
& event
);
101 void OnPageChanged(wxNotebookEvent
& event
);
103 void OnButtonReset(wxCommandEvent
& event
);
104 void OnButtonDeleteAll(wxCommandEvent
& event
);
105 void OnButtonSelectPage(wxCommandEvent
& event
);
106 void OnButtonAddPage(wxCommandEvent
& event
);
107 void OnButtonInsertPage(wxCommandEvent
& event
);
108 void OnButtonRemovePage(wxCommandEvent
& event
);
110 void OnCheckOrRadioBox(wxCommandEvent
& event
);
112 void OnUpdateUINumPagesText(wxUpdateUIEvent
& event
);
113 void OnUpdateUICurSelectText(wxUpdateUIEvent
& event
);
115 void OnUpdateUISelectButton(wxUpdateUIEvent
& event
);
116 void OnUpdateUIInsertButton(wxUpdateUIEvent
& event
);
117 void OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
);
119 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
121 // reset the wxNotebook parameters
124 // (re)create the wxNotebook
125 void CreateNotebook();
127 // create or destroy the image list
128 void CreateImageList();
131 wxWindow
*CreateNewPage();
133 // get the image index for the new page
134 int GetIconIndex() const;
136 // get the numeric value of text ctrl
137 int GetTextValue(wxTextCtrl
*text
) const;
139 // is the value in range?
140 bool IsValidValue(int val
) const
141 { return (val
>= 0) && (val
< (int) m_notebook
->GetPageCount()); }
146 // the check/radio boxes for styles
147 wxCheckBox
*m_chkImages
;
148 wxRadioBox
*m_radioOrient
;
150 // the text controls containing input for various commands
151 wxTextCtrl
*m_textInsert
,
155 // the notebook itself and the sizer it is in
156 wxNotebook
*m_notebook
;
157 wxSizer
*m_sizerNotebook
;
159 // thei mage list for our notebook
160 wxImageList
*m_imageList
;
163 DECLARE_EVENT_TABLE()
164 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, WidgetsPage
)
172 EVT_BUTTON(NotebookPage_Reset
, NotebookWidgetsPage::OnButtonReset
)
173 EVT_BUTTON(NotebookPage_SelectPage
, NotebookWidgetsPage::OnButtonSelectPage
)
174 EVT_BUTTON(NotebookPage_AddPage
, NotebookWidgetsPage::OnButtonAddPage
)
175 EVT_BUTTON(NotebookPage_InsertPage
, NotebookWidgetsPage::OnButtonInsertPage
)
176 EVT_BUTTON(NotebookPage_RemovePage
, NotebookWidgetsPage::OnButtonRemovePage
)
177 EVT_BUTTON(NotebookPage_DeleteAll
, NotebookWidgetsPage::OnButtonDeleteAll
)
179 EVT_UPDATE_UI(NotebookPage_NumPagesText
, NotebookWidgetsPage::OnUpdateUINumPagesText
)
180 EVT_UPDATE_UI(NotebookPage_CurSelectText
, NotebookWidgetsPage::OnUpdateUICurSelectText
)
182 EVT_UPDATE_UI(NotebookPage_SelectPage
, NotebookWidgetsPage::OnUpdateUISelectButton
)
183 EVT_UPDATE_UI(NotebookPage_InsertPage
, NotebookWidgetsPage::OnUpdateUIInsertButton
)
184 EVT_UPDATE_UI(NotebookPage_RemovePage
, NotebookWidgetsPage::OnUpdateUIRemoveButton
)
186 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, NotebookWidgetsPage::OnPageChanging
)
187 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, NotebookWidgetsPage::OnPageChanged
)
189 EVT_CHECKBOX(wxID_ANY
, NotebookWidgetsPage::OnCheckOrRadioBox
)
190 EVT_RADIOBOX(wxID_ANY
, NotebookWidgetsPage::OnCheckOrRadioBox
)
193 // ============================================================================
195 // ============================================================================
197 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook"));
199 NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook
*notebook
,
200 wxImageList
*imaglist
)
201 : WidgetsPage(notebook
)
203 imaglist
->Add(wxBitmap(notebook_xpm
));
209 m_notebook
= (wxNotebook
*)NULL
;
210 m_sizerNotebook
= (wxSizer
*)NULL
;
212 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
215 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
217 // must be in sync with Orient enum
218 wxString orientations
[] =
226 wxASSERT_MSG( WXSIZEOF(orientations
) == Orient_Max
,
227 _T("forgot to update something") );
229 m_chkImages
= new wxCheckBox(this, wxID_ANY
, _T("Show &images"));
230 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, _T("&Tab orientation"),
231 wxDefaultPosition
, wxDefaultSize
,
232 WXSIZEOF(orientations
), orientations
,
233 1, wxRA_SPECIFY_COLS
);
235 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
237 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
238 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
239 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
241 wxButton
*btn
= new wxButton(this, NotebookPage_Reset
, _T("&Reset"));
242 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
245 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Contents"));
246 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
249 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
250 NotebookPage_NumPagesText
,
252 text
->SetEditable(false);
253 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
255 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
256 NotebookPage_CurSelectText
,
258 text
->SetEditable(false);
259 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
261 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_SelectPage
,
263 NotebookPage_SelectText
,
265 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
267 btn
= new wxButton(this, NotebookPage_AddPage
, _T("&Add page"));
268 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
270 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_InsertPage
,
271 _T("&Insert page at"),
272 NotebookPage_InsertText
,
274 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
276 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_RemovePage
,
278 NotebookPage_RemoveText
,
280 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
282 btn
= new wxButton(this, NotebookPage_DeleteAll
, _T("&Delete All"));
283 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
286 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
287 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
);
288 sizerRight
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
289 sizerRight
->SetMinSize(150, 0);
290 m_sizerNotebook
= sizerRight
; // save it to modify it later
292 // the 3 panes panes compose the window
293 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
294 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
295 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
297 // final initializations
306 NotebookWidgetsPage::~NotebookWidgetsPage()
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 void NotebookWidgetsPage::Reset()
317 m_chkImages
->SetValue(true);
318 m_radioOrient
->SetSelection(Orient_Top
);
321 void NotebookWidgetsPage::CreateImageList()
323 if ( m_chkImages
->GetValue() )
327 // create a dummy image list with a few icons
328 m_imageList
= new wxImageList(32, 32);
330 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
331 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
332 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
333 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
336 m_notebook
->SetImageList(m_imageList
);
347 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
348 // it would be logical if this removed the image list from notebook, under
349 // MSW it crashes instead
352 void NotebookWidgetsPage::CreateNotebook()
355 switch ( m_radioOrient
->GetSelection() )
358 wxFAIL_MSG( _T("unknown notebook orientation") );
378 wxNotebook
*old_note
= m_notebook
;
380 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
,
381 wxDefaultPosition
, wxDefaultSize
,
388 const int sel
= old_note
->GetSelection();
390 const int count
= old_note
->GetPageCount();
392 // recreate the pages
393 for ( int n
= 0; n
< count
; n
++ )
395 m_notebook
->AddPage(CreateNewPage(),
396 old_note
->GetPageText(n
),
398 m_chkImages
->GetValue() ?
399 GetIconIndex() : -1);
402 m_sizerNotebook
->Detach( old_note
);
408 m_notebook
->SetSelection(sel
);
412 m_sizerNotebook
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
413 m_sizerNotebook
->Layout();
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
420 int NotebookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
423 if ( !text
->GetValue().ToLong(&pos
) )
429 int NotebookWidgetsPage::GetIconIndex() const
433 int nImages
= m_imageList
->GetImageCount();
436 return m_notebook
->GetPageCount() % nImages
;
443 wxWindow
*NotebookWidgetsPage::CreateNewPage()
445 return new wxTextCtrl(m_notebook
, wxID_ANY
, _T("I'm a notebook page"));
448 // ----------------------------------------------------------------------------
450 // ----------------------------------------------------------------------------
452 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
459 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
461 m_notebook
->DeleteAllPages();
464 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
466 int pos
= GetTextValue(m_textSelect
);
467 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
469 m_notebook
->SetSelection(pos
);
472 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
474 m_notebook
->AddPage(CreateNewPage(), _T("Added page"), false,
478 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
480 int pos
= GetTextValue(m_textInsert
);
481 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
483 m_notebook
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), false,
487 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
489 int pos
= GetTextValue(m_textRemove
);
490 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
492 m_notebook
->DeletePage(pos
);
495 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
497 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
500 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
502 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
505 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
507 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
510 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
512 event
.Enable( !m_chkImages
->GetValue() ||
513 m_radioOrient
->GetSelection() != wxNB_TOP
);
516 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
518 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetPageCount()) );
521 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
523 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetSelection()) );
526 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
531 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
533 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
534 event
.GetOldSelection(),
535 event
.GetSelection(),
536 m_notebook
->GetSelection());
541 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
543 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
544 event
.GetOldSelection(),
545 event
.GetSelection(),
546 m_notebook
->GetSelection());