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"
29 // for all others, include the necessary headers
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/combobox.h"
37 #include "wx/radiobox.h"
38 #include "wx/statbox.h"
39 #include "wx/textctrl.h"
41 #include "wx/dynarray.h"
45 #include "wx/notebook.h"
46 #include "wx/artprov.h"
49 #include "icons/notebook.xpm"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 NotebookPage_Reset
= 100,
59 NotebookPage_SelectPage
,
61 NotebookPage_InsertPage
,
62 NotebookPage_RemovePage
,
63 NotebookPage_DeleteAll
,
64 NotebookPage_InsertText
,
65 NotebookPage_RemoveText
,
66 NotebookPage_SelectText
,
67 NotebookPage_NumPagesText
,
68 NotebookPage_CurSelectText
,
72 // notebook orientations
82 // ----------------------------------------------------------------------------
83 // NotebookWidgetsPage
84 // ----------------------------------------------------------------------------
86 class NotebookWidgetsPage
: public WidgetsPage
89 NotebookWidgetsPage(wxBookCtrlBase
*book
, wxImageList
*imaglist
);
90 virtual ~NotebookWidgetsPage();
92 virtual wxControl
*GetWidget() const { return m_notebook
; }
96 void OnPageChanging(wxNotebookEvent
& event
);
97 void OnPageChanged(wxNotebookEvent
& event
);
99 void OnButtonReset(wxCommandEvent
& event
);
100 void OnButtonDeleteAll(wxCommandEvent
& event
);
101 void OnButtonSelectPage(wxCommandEvent
& event
);
102 void OnButtonAddPage(wxCommandEvent
& event
);
103 void OnButtonInsertPage(wxCommandEvent
& event
);
104 void OnButtonRemovePage(wxCommandEvent
& event
);
106 void OnCheckOrRadioBox(wxCommandEvent
& event
);
108 void OnUpdateUINumPagesText(wxUpdateUIEvent
& event
);
109 void OnUpdateUICurSelectText(wxUpdateUIEvent
& event
);
111 void OnUpdateUISelectButton(wxUpdateUIEvent
& event
);
112 void OnUpdateUIInsertButton(wxUpdateUIEvent
& event
);
113 void OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
);
115 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
117 // reset the wxNotebook parameters
120 // (re)create the wxNotebook
121 void CreateNotebook();
123 // create or destroy the image list
124 void CreateImageList();
127 wxWindow
*CreateNewPage();
129 // get the image index for the new page
130 int GetIconIndex() const;
132 // get the numeric value of text ctrl
133 int GetTextValue(wxTextCtrl
*text
) const;
135 // is the value in range?
136 bool IsValidValue(int val
) const
137 { return (val
>= 0) && (val
< (int) m_notebook
->GetPageCount()); }
142 // the check/radio boxes for styles
143 wxCheckBox
*m_chkImages
;
144 wxRadioBox
*m_radioOrient
;
146 // the text controls containing input for various commands
147 wxTextCtrl
*m_textInsert
,
151 // the notebook itself and the sizer it is in
152 wxNotebook
*m_notebook
;
153 wxSizer
*m_sizerNotebook
;
155 // thei mage list for our notebook
156 wxImageList
*m_imageList
;
159 DECLARE_EVENT_TABLE()
160 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, WidgetsPage
)
168 EVT_BUTTON(NotebookPage_Reset
, NotebookWidgetsPage::OnButtonReset
)
169 EVT_BUTTON(NotebookPage_SelectPage
, NotebookWidgetsPage::OnButtonSelectPage
)
170 EVT_BUTTON(NotebookPage_AddPage
, NotebookWidgetsPage::OnButtonAddPage
)
171 EVT_BUTTON(NotebookPage_InsertPage
, NotebookWidgetsPage::OnButtonInsertPage
)
172 EVT_BUTTON(NotebookPage_RemovePage
, NotebookWidgetsPage::OnButtonRemovePage
)
173 EVT_BUTTON(NotebookPage_DeleteAll
, NotebookWidgetsPage::OnButtonDeleteAll
)
175 EVT_UPDATE_UI(NotebookPage_NumPagesText
, NotebookWidgetsPage::OnUpdateUINumPagesText
)
176 EVT_UPDATE_UI(NotebookPage_CurSelectText
, NotebookWidgetsPage::OnUpdateUICurSelectText
)
178 EVT_UPDATE_UI(NotebookPage_SelectPage
, NotebookWidgetsPage::OnUpdateUISelectButton
)
179 EVT_UPDATE_UI(NotebookPage_InsertPage
, NotebookWidgetsPage::OnUpdateUIInsertButton
)
180 EVT_UPDATE_UI(NotebookPage_RemovePage
, NotebookWidgetsPage::OnUpdateUIRemoveButton
)
182 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, NotebookWidgetsPage::OnPageChanging
)
183 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, NotebookWidgetsPage::OnPageChanged
)
185 EVT_CHECKBOX(wxID_ANY
, NotebookWidgetsPage::OnCheckOrRadioBox
)
186 EVT_RADIOBOX(wxID_ANY
, NotebookWidgetsPage::OnCheckOrRadioBox
)
189 // ============================================================================
191 // ============================================================================
193 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook"));
195 NotebookWidgetsPage::NotebookWidgetsPage(wxBookCtrlBase
*book
,
196 wxImageList
*imaglist
)
199 imaglist
->Add(wxBitmap(notebook_xpm
));
205 m_notebook
= (wxNotebook
*)NULL
;
206 m_sizerNotebook
= (wxSizer
*)NULL
;
208 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
211 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
213 // must be in sync with Orient enum
214 wxArrayString orientations
;
215 orientations
.Add(_T("&top"));
216 orientations
.Add(_T("&bottom"));
217 orientations
.Add(_T("&left"));
218 orientations
.Add(_T("&right"));
220 wxASSERT_MSG( orientations
.GetCount() == Orient_Max
,
221 _T("forgot to update something") );
223 m_chkImages
= new wxCheckBox(this, wxID_ANY
, _T("Show &images"));
224 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, _T("&Tab orientation"),
225 wxDefaultPosition
, wxDefaultSize
,
226 orientations
, 1, wxRA_SPECIFY_COLS
);
228 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
230 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
231 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
232 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
234 wxButton
*btn
= new wxButton(this, NotebookPage_Reset
, _T("&Reset"));
235 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
238 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Contents"));
239 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
242 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
243 NotebookPage_NumPagesText
,
245 text
->SetEditable(false);
246 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
248 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
249 NotebookPage_CurSelectText
,
251 text
->SetEditable(false);
252 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
254 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_SelectPage
,
256 NotebookPage_SelectText
,
258 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
260 btn
= new wxButton(this, NotebookPage_AddPage
, _T("&Add page"));
261 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
263 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_InsertPage
,
264 _T("&Insert page at"),
265 NotebookPage_InsertText
,
267 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
269 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_RemovePage
,
271 NotebookPage_RemoveText
,
273 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
275 btn
= new wxButton(this, NotebookPage_DeleteAll
, _T("&Delete All"));
276 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
279 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
280 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
);
281 sizerRight
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
282 sizerRight
->SetMinSize(150, 0);
283 m_sizerNotebook
= sizerRight
; // save it to modify it later
285 // the 3 panes panes compose the window
286 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
287 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
288 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
290 // final initializations
299 NotebookWidgetsPage::~NotebookWidgetsPage()
304 // ----------------------------------------------------------------------------
306 // ----------------------------------------------------------------------------
308 void NotebookWidgetsPage::Reset()
310 m_chkImages
->SetValue(true);
311 m_radioOrient
->SetSelection(Orient_Top
);
314 void NotebookWidgetsPage::CreateImageList()
316 if ( m_chkImages
->GetValue() )
320 // create a dummy image list with a few icons
321 m_imageList
= new wxImageList(32, 32);
323 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
324 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
325 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
326 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
329 m_notebook
->SetImageList(m_imageList
);
340 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
341 // it would be logical if this removed the image list from notebook, under
342 // MSW it crashes instead
345 void NotebookWidgetsPage::CreateNotebook()
348 switch ( m_radioOrient
->GetSelection() )
351 wxFAIL_MSG( _T("unknown notebook orientation") );
371 wxNotebook
*old_note
= m_notebook
;
373 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
,
374 wxDefaultPosition
, wxDefaultSize
,
381 const int sel
= old_note
->GetSelection();
383 const int count
= old_note
->GetPageCount();
385 // recreate the pages
386 for ( int n
= 0; n
< count
; n
++ )
388 m_notebook
->AddPage(CreateNewPage(),
389 old_note
->GetPageText(n
),
391 m_chkImages
->GetValue() ?
392 GetIconIndex() : -1);
395 m_sizerNotebook
->Detach( old_note
);
401 m_notebook
->SetSelection(sel
);
405 m_sizerNotebook
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
406 m_sizerNotebook
->Layout();
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 int NotebookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
416 if ( !text
->GetValue().ToLong(&pos
) )
422 int NotebookWidgetsPage::GetIconIndex() const
426 int nImages
= m_imageList
->GetImageCount();
429 return m_notebook
->GetPageCount() % nImages
;
436 wxWindow
*NotebookWidgetsPage::CreateNewPage()
438 return new wxTextCtrl(m_notebook
, wxID_ANY
, _T("I'm a notebook page"));
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
452 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
454 m_notebook
->DeleteAllPages();
457 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
459 int pos
= GetTextValue(m_textSelect
);
460 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
462 m_notebook
->SetSelection(pos
);
465 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
467 m_notebook
->AddPage(CreateNewPage(), _T("Added page"), false,
471 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
473 int pos
= GetTextValue(m_textInsert
);
474 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
476 m_notebook
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), false,
480 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
482 int pos
= GetTextValue(m_textRemove
);
483 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
485 m_notebook
->DeletePage(pos
);
488 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
490 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
493 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
495 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
498 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
500 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
503 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
505 event
.Enable( !m_chkImages
->GetValue() ||
506 m_radioOrient
->GetSelection() != wxBK_TOP
);
509 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
511 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetPageCount()) );
514 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
516 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetSelection()) );
519 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
524 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
526 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
527 event
.GetOldSelection(),
528 event
.GetSelection(),
529 m_notebook
->GetSelection());
534 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
536 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
537 event
.GetOldSelection(),
538 event
.GetSelection(),
539 m_notebook
->GetSelection());
544 #endif // wxUSE_NOTEBOOK