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 wxString orientations
[] =
222 wxASSERT_MSG( WXSIZEOF(orientations
) == Orient_Max
,
223 _T("forgot to update something") );
225 m_chkImages
= new wxCheckBox(this, wxID_ANY
, _T("Show &images"));
226 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, _T("&Tab orientation"),
227 wxDefaultPosition
, wxDefaultSize
,
228 WXSIZEOF(orientations
), orientations
,
229 1, wxRA_SPECIFY_COLS
);
231 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
233 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
234 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
235 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
237 wxButton
*btn
= new wxButton(this, NotebookPage_Reset
, _T("&Reset"));
238 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
241 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Contents"));
242 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
245 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
246 NotebookPage_NumPagesText
,
248 text
->SetEditable(false);
249 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
251 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
252 NotebookPage_CurSelectText
,
254 text
->SetEditable(false);
255 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
257 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_SelectPage
,
259 NotebookPage_SelectText
,
261 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
263 btn
= new wxButton(this, NotebookPage_AddPage
, _T("&Add page"));
264 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
266 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_InsertPage
,
267 _T("&Insert page at"),
268 NotebookPage_InsertText
,
270 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
272 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_RemovePage
,
274 NotebookPage_RemoveText
,
276 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
278 btn
= new wxButton(this, NotebookPage_DeleteAll
, _T("&Delete All"));
279 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
282 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
283 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
);
284 sizerRight
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
285 sizerRight
->SetMinSize(150, 0);
286 m_sizerNotebook
= sizerRight
; // save it to modify it later
288 // the 3 panes panes compose the window
289 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
290 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
291 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
293 // final initializations
302 NotebookWidgetsPage::~NotebookWidgetsPage()
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 void NotebookWidgetsPage::Reset()
313 m_chkImages
->SetValue(true);
314 m_radioOrient
->SetSelection(Orient_Top
);
317 void NotebookWidgetsPage::CreateImageList()
319 if ( m_chkImages
->GetValue() )
323 // create a dummy image list with a few icons
324 m_imageList
= new wxImageList(32, 32);
326 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
327 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
328 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
329 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
332 m_notebook
->SetImageList(m_imageList
);
343 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
344 // it would be logical if this removed the image list from notebook, under
345 // MSW it crashes instead
348 void NotebookWidgetsPage::CreateNotebook()
351 switch ( m_radioOrient
->GetSelection() )
354 wxFAIL_MSG( _T("unknown notebook orientation") );
374 wxNotebook
*old_note
= m_notebook
;
376 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
,
377 wxDefaultPosition
, wxDefaultSize
,
384 const int sel
= old_note
->GetSelection();
386 const int count
= old_note
->GetPageCount();
388 // recreate the pages
389 for ( int n
= 0; n
< count
; n
++ )
391 m_notebook
->AddPage(CreateNewPage(),
392 old_note
->GetPageText(n
),
394 m_chkImages
->GetValue() ?
395 GetIconIndex() : -1);
398 m_sizerNotebook
->Detach( old_note
);
404 m_notebook
->SetSelection(sel
);
408 m_sizerNotebook
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
409 m_sizerNotebook
->Layout();
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 int NotebookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
419 if ( !text
->GetValue().ToLong(&pos
) )
425 int NotebookWidgetsPage::GetIconIndex() const
429 int nImages
= m_imageList
->GetImageCount();
432 return m_notebook
->GetPageCount() % nImages
;
439 wxWindow
*NotebookWidgetsPage::CreateNewPage()
441 return new wxTextCtrl(m_notebook
, wxID_ANY
, _T("I'm a notebook page"));
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
455 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
457 m_notebook
->DeleteAllPages();
460 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
462 int pos
= GetTextValue(m_textSelect
);
463 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
465 m_notebook
->SetSelection(pos
);
468 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
470 m_notebook
->AddPage(CreateNewPage(), _T("Added page"), false,
474 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
476 int pos
= GetTextValue(m_textInsert
);
477 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
479 m_notebook
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), false,
483 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
485 int pos
= GetTextValue(m_textRemove
);
486 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
488 m_notebook
->DeletePage(pos
);
491 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
493 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
496 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
498 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
501 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
503 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
506 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
508 event
.Enable( !m_chkImages
->GetValue() ||
509 m_radioOrient
->GetSelection() != wxBK_TOP
);
512 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
514 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetPageCount()) );
517 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
519 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetSelection()) );
522 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
527 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
529 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
530 event
.GetOldSelection(),
531 event
.GetSelection(),
532 m_notebook
->GetSelection());
537 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
539 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
540 event
.GetOldSelection(),
541 event
.GetSelection(),
542 m_notebook
->GetSelection());
547 #endif // wxUSE_NOTEBOOK