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"
47 #include "icons/notebook.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 NotebookPage_Reset
= 100,
57 NotebookPage_SelectPage
,
59 NotebookPage_InsertPage
,
60 NotebookPage_RemovePage
,
61 NotebookPage_DeleteAll
,
62 NotebookPage_InsertText
,
63 NotebookPage_RemoveText
,
64 NotebookPage_SelectText
,
65 NotebookPage_NumPagesText
,
66 NotebookPage_CurSelectText
,
70 // notebook orientations
80 // old versions of wxWindows don't define this style
85 // ----------------------------------------------------------------------------
86 // NotebookWidgetsPage
87 // ----------------------------------------------------------------------------
89 class NotebookWidgetsPage
: public WidgetsPage
92 NotebookWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
93 virtual ~NotebookWidgetsPage();
97 void OnPageChanging(wxNotebookEvent
& event
);
98 void OnPageChanged(wxNotebookEvent
& event
);
100 void OnButtonReset(wxCommandEvent
& event
);
101 void OnButtonDeleteAll(wxCommandEvent
& event
);
102 void OnButtonSelectPage(wxCommandEvent
& event
);
103 void OnButtonAddPage(wxCommandEvent
& event
);
104 void OnButtonInsertPage(wxCommandEvent
& event
);
105 void OnButtonRemovePage(wxCommandEvent
& event
);
107 void OnCheckOrRadioBox(wxCommandEvent
& event
);
109 void OnUpdateUINumPagesText(wxUpdateUIEvent
& event
);
110 void OnUpdateUICurSelectText(wxUpdateUIEvent
& event
);
112 void OnUpdateUISelectButton(wxUpdateUIEvent
& event
);
113 void OnUpdateUIInsertButton(wxUpdateUIEvent
& event
);
114 void OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
);
116 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
118 // reset the wxNotebook parameters
121 // (re)create the wxNotebook
122 void CreateNotebook();
124 // create or destroy the image list
125 void CreateImageList();
128 wxWindow
*CreateNewPage();
130 // get the image index for the new page
131 int GetIconIndex() const;
133 // get the numeric value of text ctrl
134 int GetTextValue(wxTextCtrl
*text
) const;
139 // the check/radio boxes for styles
140 wxCheckBox
*m_chkImages
;
141 wxRadioBox
*m_radioOrient
;
143 // the text controls containing input for various commands
144 wxTextCtrl
*m_textInsert
,
148 // the notebook itself and the sizer it is in
149 wxNotebook
*m_notebook
;
150 wxSizer
*m_sizerNotebook
;
152 // thei mage list for our notebook
153 wxImageList
*m_imageList
;
156 DECLARE_EVENT_TABLE();
157 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
);
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, WidgetsPage
)
165 EVT_BUTTON(NotebookPage_Reset
, NotebookWidgetsPage::OnButtonReset
)
166 EVT_BUTTON(NotebookPage_SelectPage
, NotebookWidgetsPage::OnButtonSelectPage
)
167 EVT_BUTTON(NotebookPage_AddPage
, NotebookWidgetsPage::OnButtonAddPage
)
168 EVT_BUTTON(NotebookPage_InsertPage
, NotebookWidgetsPage::OnButtonInsertPage
)
169 EVT_BUTTON(NotebookPage_RemovePage
, NotebookWidgetsPage::OnButtonRemovePage
)
170 EVT_BUTTON(NotebookPage_DeleteAll
, NotebookWidgetsPage::OnButtonDeleteAll
)
172 EVT_UPDATE_UI(NotebookPage_NumPagesText
, NotebookWidgetsPage::OnUpdateUINumPagesText
)
173 EVT_UPDATE_UI(NotebookPage_CurSelectText
, NotebookWidgetsPage::OnUpdateUICurSelectText
)
175 EVT_UPDATE_UI(NotebookPage_SelectPage
, NotebookWidgetsPage::OnUpdateUISelectButton
)
176 EVT_UPDATE_UI(NotebookPage_InsertPage
, NotebookWidgetsPage::OnUpdateUIInsertButton
)
177 EVT_UPDATE_UI(NotebookPage_RemovePage
, NotebookWidgetsPage::OnUpdateUIRemoveButton
)
179 EVT_NOTEBOOK_PAGE_CHANGING(-1, NotebookWidgetsPage::OnPageChanging
)
180 EVT_NOTEBOOK_PAGE_CHANGED(-1, NotebookWidgetsPage::OnPageChanged
)
182 EVT_CHECKBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox
)
183 EVT_RADIOBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox
)
186 // ============================================================================
188 // ============================================================================
190 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook"));
192 NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook
*notebook
,
193 wxImageList
*imaglist
)
194 : WidgetsPage(notebook
)
196 imaglist
->Add(wxBitmap(notebook_xpm
));
202 m_notebook
= (wxNotebook
*)NULL
;
203 m_sizerNotebook
= (wxSizer
*)NULL
;
205 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
208 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
210 // must be in sync with Orient enum
211 wxString orientations
[] =
219 wxASSERT_MSG( WXSIZEOF(orientations
) == Orient_Max
,
220 _T("forgot to update something") );
222 m_chkImages
= new wxCheckBox(this, -1, _T("Show &images"));
223 m_radioOrient
= new wxRadioBox(this, -1, _T("&Tab orientation"),
224 wxDefaultPosition
, wxDefaultSize
,
225 WXSIZEOF(orientations
), orientations
,
226 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, -1, _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(250, 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
300 NotebookWidgetsPage::~NotebookWidgetsPage()
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 void NotebookWidgetsPage::Reset()
311 m_chkImages
->SetValue(TRUE
);
312 m_radioOrient
->SetSelection(Orient_Top
);
315 void NotebookWidgetsPage::CreateImageList()
317 if ( m_chkImages
->GetValue() )
321 // create a dummy image list with a few icons
322 m_imageList
= new wxImageList(32, 32);
323 m_imageList
->Add(wxTheApp
->GetStdIcon(wxICON_INFORMATION
));
324 m_imageList
->Add(wxTheApp
->GetStdIcon(wxICON_QUESTION
));
325 m_imageList
->Add(wxTheApp
->GetStdIcon(wxICON_WARNING
));
326 m_imageList
->Add(wxTheApp
->GetStdIcon(wxICON_ERROR
));
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 notebok orientation") );
371 wxNotebook
*notebook
= m_notebook
;
373 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
,
374 wxDefaultPosition
, wxDefaultSize
,
381 int sel
= notebook
->GetSelection();
383 int count
= notebook
->GetPageCount();
384 for ( int n
= 0; n
< count
; n
++ )
386 wxNotebookPage
*page
= notebook
->GetPage(0);
387 page
->Reparent(m_notebook
);
389 m_notebook
->AddPage(page
, notebook
->GetPageText(0), FALSE
,
390 notebook
->GetPageImage(0));
392 notebook
->RemovePage(0);
395 m_sizerNotebook
->Remove(notebook
);
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
, -1, _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
& event
)
459 int pos
= GetTextValue(m_textSelect
);
460 wxCHECK_RET( pos
>= 0, _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( pos
>= 0, _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( pos
>= 0, _T("button should be disabled") );
485 m_notebook
->DeletePage(pos
);
488 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
490 event
.Enable( GetTextValue(m_textSelect
) >= 0 );
493 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
495 event
.Enable( GetTextValue(m_textInsert
) >= 0 );
498 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
500 event
.Enable( GetTextValue(m_textRemove
) >= 0 );
503 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
505 event
.Enable( !m_chkImages
->GetValue() ||
506 m_radioOrient
->GetSelection() != wxNB_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
& 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());