1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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 wxWindows don't define this style
86 // ----------------------------------------------------------------------------
87 // NotebookWidgetsPage
88 // ----------------------------------------------------------------------------
90 class NotebookWidgetsPage
: public WidgetsPage
93 NotebookWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
94 virtual ~NotebookWidgetsPage();
98 void OnPageChanging(wxNotebookEvent
& event
);
99 void OnPageChanged(wxNotebookEvent
& event
);
101 void OnButtonReset(wxCommandEvent
& event
);
102 void OnButtonDeleteAll(wxCommandEvent
& event
);
103 void OnButtonSelectPage(wxCommandEvent
& event
);
104 void OnButtonAddPage(wxCommandEvent
& event
);
105 void OnButtonInsertPage(wxCommandEvent
& event
);
106 void OnButtonRemovePage(wxCommandEvent
& event
);
108 void OnCheckOrRadioBox(wxCommandEvent
& event
);
110 void OnUpdateUINumPagesText(wxUpdateUIEvent
& event
);
111 void OnUpdateUICurSelectText(wxUpdateUIEvent
& event
);
113 void OnUpdateUISelectButton(wxUpdateUIEvent
& event
);
114 void OnUpdateUIInsertButton(wxUpdateUIEvent
& event
);
115 void OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
);
117 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
119 // reset the wxNotebook parameters
122 // (re)create the wxNotebook
123 void CreateNotebook();
125 // create or destroy the image list
126 void CreateImageList();
129 wxWindow
*CreateNewPage();
131 // get the image index for the new page
132 int GetIconIndex() const;
134 // get the numeric value of text ctrl
135 int GetTextValue(wxTextCtrl
*text
) const;
137 // is the value in range?
138 bool IsValidValue(int val
) const
139 { return (val
>= 0) && (val
< (int) m_notebook
->GetPageCount()); }
144 // the check/radio boxes for styles
145 wxCheckBox
*m_chkImages
;
146 wxRadioBox
*m_radioOrient
;
148 // the text controls containing input for various commands
149 wxTextCtrl
*m_textInsert
,
153 // the notebook itself and the sizer it is in
154 wxNotebook
*m_notebook
;
155 wxSizer
*m_sizerNotebook
;
157 // thei mage list for our notebook
158 wxImageList
*m_imageList
;
161 DECLARE_EVENT_TABLE()
162 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, WidgetsPage
)
170 EVT_BUTTON(NotebookPage_Reset
, NotebookWidgetsPage::OnButtonReset
)
171 EVT_BUTTON(NotebookPage_SelectPage
, NotebookWidgetsPage::OnButtonSelectPage
)
172 EVT_BUTTON(NotebookPage_AddPage
, NotebookWidgetsPage::OnButtonAddPage
)
173 EVT_BUTTON(NotebookPage_InsertPage
, NotebookWidgetsPage::OnButtonInsertPage
)
174 EVT_BUTTON(NotebookPage_RemovePage
, NotebookWidgetsPage::OnButtonRemovePage
)
175 EVT_BUTTON(NotebookPage_DeleteAll
, NotebookWidgetsPage::OnButtonDeleteAll
)
177 EVT_UPDATE_UI(NotebookPage_NumPagesText
, NotebookWidgetsPage::OnUpdateUINumPagesText
)
178 EVT_UPDATE_UI(NotebookPage_CurSelectText
, NotebookWidgetsPage::OnUpdateUICurSelectText
)
180 EVT_UPDATE_UI(NotebookPage_SelectPage
, NotebookWidgetsPage::OnUpdateUISelectButton
)
181 EVT_UPDATE_UI(NotebookPage_InsertPage
, NotebookWidgetsPage::OnUpdateUIInsertButton
)
182 EVT_UPDATE_UI(NotebookPage_RemovePage
, NotebookWidgetsPage::OnUpdateUIRemoveButton
)
184 EVT_NOTEBOOK_PAGE_CHANGING(-1, NotebookWidgetsPage::OnPageChanging
)
185 EVT_NOTEBOOK_PAGE_CHANGED(-1, NotebookWidgetsPage::OnPageChanged
)
187 EVT_CHECKBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox
)
188 EVT_RADIOBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox
)
191 // ============================================================================
193 // ============================================================================
195 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook"));
197 NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook
*notebook
,
198 wxImageList
*imaglist
)
199 : WidgetsPage(notebook
)
201 imaglist
->Add(wxBitmap(notebook_xpm
));
207 m_notebook
= (wxNotebook
*)NULL
;
208 m_sizerNotebook
= (wxSizer
*)NULL
;
210 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
213 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
215 // must be in sync with Orient enum
216 wxString orientations
[] =
224 wxASSERT_MSG( WXSIZEOF(orientations
) == Orient_Max
,
225 _T("forgot to update something") );
227 m_chkImages
= new wxCheckBox(this, -1, _T("Show &images"));
228 m_radioOrient
= new wxRadioBox(this, -1, _T("&Tab orientation"),
229 wxDefaultPosition
, wxDefaultSize
,
230 WXSIZEOF(orientations
), orientations
,
231 1, wxRA_SPECIFY_COLS
);
233 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
235 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
236 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
237 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
239 wxButton
*btn
= new wxButton(this, NotebookPage_Reset
, _T("&Reset"));
240 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
243 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Contents"));
244 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
247 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
248 NotebookPage_NumPagesText
,
250 text
->SetEditable(FALSE
);
251 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
253 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
254 NotebookPage_CurSelectText
,
256 text
->SetEditable(FALSE
);
257 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
259 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_SelectPage
,
261 NotebookPage_SelectText
,
263 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
265 btn
= new wxButton(this, NotebookPage_AddPage
, _T("&Add page"));
266 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
268 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_InsertPage
,
269 _T("&Insert page at"),
270 NotebookPage_InsertText
,
272 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
274 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_RemovePage
,
276 NotebookPage_RemoveText
,
278 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
280 btn
= new wxButton(this, NotebookPage_DeleteAll
, _T("&Delete All"));
281 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
284 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
285 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
);
286 sizerRight
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
287 sizerRight
->SetMinSize(150, 0);
288 m_sizerNotebook
= sizerRight
; // save it to modify it later
290 // the 3 panes panes compose the window
291 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
292 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
293 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
295 // final initializations
305 NotebookWidgetsPage::~NotebookWidgetsPage()
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 void NotebookWidgetsPage::Reset()
316 m_chkImages
->SetValue(TRUE
);
317 m_radioOrient
->SetSelection(Orient_Top
);
320 void NotebookWidgetsPage::CreateImageList()
322 if ( m_chkImages
->GetValue() )
326 // create a dummy image list with a few icons
327 m_imageList
= new wxImageList(32, 32);
329 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
330 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
331 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
332 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
335 m_notebook
->SetImageList(m_imageList
);
346 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
347 // it would be logical if this removed the image list from notebook, under
348 // MSW it crashes instead
351 void NotebookWidgetsPage::CreateNotebook()
354 switch ( m_radioOrient
->GetSelection() )
357 wxFAIL_MSG( _T("unknown notebok orientation") );
377 wxNotebook
*notebook
= m_notebook
;
379 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
,
380 wxDefaultPosition
, wxDefaultSize
,
387 const int sel
= notebook
->GetSelection();
389 const int count
= notebook
->GetPageCount();
391 // recreate the pages
392 for ( int n
= 0; n
< count
; n
++ )
394 m_notebook
->AddPage(CreateNewPage(),
395 notebook
->GetPageText(n
),
397 notebook
->GetPageImage(n
));
400 m_sizerNotebook
->Detach( notebook
);
406 m_notebook
->SetSelection(sel
);
410 m_sizerNotebook
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
411 m_sizerNotebook
->Layout();
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 int NotebookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
421 if ( !text
->GetValue().ToLong(&pos
) )
427 int NotebookWidgetsPage::GetIconIndex() const
431 int nImages
= m_imageList
->GetImageCount();
434 return m_notebook
->GetPageCount() % nImages
;
441 wxWindow
*NotebookWidgetsPage::CreateNewPage()
443 return new wxTextCtrl(m_notebook
, -1, _T("I'm a notebook page"));
446 // ----------------------------------------------------------------------------
448 // ----------------------------------------------------------------------------
450 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
457 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
459 m_notebook
->DeleteAllPages();
462 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
464 int pos
= GetTextValue(m_textSelect
);
465 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
467 m_notebook
->SetSelection(pos
);
470 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
472 m_notebook
->AddPage(CreateNewPage(), _T("Added page"), FALSE
,
476 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
478 int pos
= GetTextValue(m_textInsert
);
479 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
481 m_notebook
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), FALSE
,
485 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
487 int pos
= GetTextValue(m_textRemove
);
488 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
490 m_notebook
->DeletePage(pos
);
493 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
495 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
498 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
500 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
503 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
505 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
508 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
510 event
.Enable( !m_chkImages
->GetValue() ||
511 m_radioOrient
->GetSelection() != wxNB_TOP
);
514 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
516 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetPageCount()) );
519 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
521 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetSelection()) );
524 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
529 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
531 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
532 event
.GetOldSelection(),
533 event
.GetSelection(),
534 m_notebook
->GetSelection());
539 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
541 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
542 event
.GetOldSelection(),
543 event
.GetSelection(),
544 m_notebook
->GetSelection());