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
; }
93 virtual void RecreateWidget() { CreateNotebook(); }
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;
136 // is the value in range?
137 bool IsValidValue(int val
) const
138 { return (val
>= 0) && (val
< (int) m_notebook
->GetPageCount()); }
143 // the check/radio boxes for styles
144 wxCheckBox
*m_chkImages
;
145 wxRadioBox
*m_radioOrient
;
147 // the text controls containing input for various commands
148 wxTextCtrl
*m_textInsert
,
152 // the notebook itself and the sizer it is in
153 wxNotebook
*m_notebook
;
154 wxSizer
*m_sizerNotebook
;
156 // thei mage list for our notebook
157 wxImageList
*m_imageList
;
160 DECLARE_EVENT_TABLE()
161 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, WidgetsPage
)
169 EVT_BUTTON(NotebookPage_Reset
, NotebookWidgetsPage::OnButtonReset
)
170 EVT_BUTTON(NotebookPage_SelectPage
, NotebookWidgetsPage::OnButtonSelectPage
)
171 EVT_BUTTON(NotebookPage_AddPage
, NotebookWidgetsPage::OnButtonAddPage
)
172 EVT_BUTTON(NotebookPage_InsertPage
, NotebookWidgetsPage::OnButtonInsertPage
)
173 EVT_BUTTON(NotebookPage_RemovePage
, NotebookWidgetsPage::OnButtonRemovePage
)
174 EVT_BUTTON(NotebookPage_DeleteAll
, NotebookWidgetsPage::OnButtonDeleteAll
)
176 EVT_UPDATE_UI(NotebookPage_NumPagesText
, NotebookWidgetsPage::OnUpdateUINumPagesText
)
177 EVT_UPDATE_UI(NotebookPage_CurSelectText
, NotebookWidgetsPage::OnUpdateUICurSelectText
)
179 EVT_UPDATE_UI(NotebookPage_SelectPage
, NotebookWidgetsPage::OnUpdateUISelectButton
)
180 EVT_UPDATE_UI(NotebookPage_InsertPage
, NotebookWidgetsPage::OnUpdateUIInsertButton
)
181 EVT_UPDATE_UI(NotebookPage_RemovePage
, NotebookWidgetsPage::OnUpdateUIRemoveButton
)
183 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, NotebookWidgetsPage::OnPageChanging
)
184 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, NotebookWidgetsPage::OnPageChanged
)
186 EVT_CHECKBOX(wxID_ANY
, NotebookWidgetsPage::OnCheckOrRadioBox
)
187 EVT_RADIOBOX(wxID_ANY
, NotebookWidgetsPage::OnCheckOrRadioBox
)
190 // ============================================================================
192 // ============================================================================
194 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook"));
196 NotebookWidgetsPage::NotebookWidgetsPage(wxBookCtrlBase
*book
,
197 wxImageList
*imaglist
)
200 imaglist
->Add(wxBitmap(notebook_xpm
));
206 m_notebook
= (wxNotebook
*)NULL
;
207 m_sizerNotebook
= (wxSizer
*)NULL
;
209 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
212 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
214 // must be in sync with Orient enum
215 wxArrayString orientations
;
216 orientations
.Add(_T("&top"));
217 orientations
.Add(_T("&bottom"));
218 orientations
.Add(_T("&left"));
219 orientations
.Add(_T("&right"));
221 wxASSERT_MSG( orientations
.GetCount() == Orient_Max
,
222 _T("forgot to update something") );
224 m_chkImages
= new wxCheckBox(this, wxID_ANY
, _T("Show &images"));
225 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, _T("&Tab orientation"),
226 wxDefaultPosition
, wxDefaultSize
,
227 orientations
, 1, wxRA_SPECIFY_COLS
);
229 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
231 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
232 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
233 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
235 wxButton
*btn
= new wxButton(this, NotebookPage_Reset
, _T("&Reset"));
236 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
239 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Contents"));
240 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
243 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
244 NotebookPage_NumPagesText
,
246 text
->SetEditable(false);
247 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
249 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
250 NotebookPage_CurSelectText
,
252 text
->SetEditable(false);
253 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
255 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_SelectPage
,
257 NotebookPage_SelectText
,
259 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
261 btn
= new wxButton(this, NotebookPage_AddPage
, _T("&Add page"));
262 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
264 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_InsertPage
,
265 _T("&Insert page at"),
266 NotebookPage_InsertText
,
268 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
270 sizerRow
= CreateSizerWithTextAndButton(NotebookPage_RemovePage
,
272 NotebookPage_RemoveText
,
274 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
276 btn
= new wxButton(this, NotebookPage_DeleteAll
, _T("&Delete All"));
277 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
280 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
281 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
);
282 sizerRight
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
283 sizerRight
->SetMinSize(150, 0);
284 m_sizerNotebook
= sizerRight
; // save it to modify it later
286 // the 3 panes panes compose the window
287 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
288 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
289 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
291 // 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);
324 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
325 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
326 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
327 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
330 m_notebook
->SetImageList(m_imageList
);
341 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
342 // it would be logical if this removed the image list from notebook, under
343 // MSW it crashes instead
346 void NotebookWidgetsPage::CreateNotebook()
348 int flags
= ms_defaultFlags
;
349 switch ( m_radioOrient
->GetSelection() )
352 wxFAIL_MSG( _T("unknown notebook orientation") );
360 flags
|= wxBK_BOTTOM
;
372 wxNotebook
*old_note
= m_notebook
;
374 m_notebook
= new wxNotebook(this, NotebookPage_Notebook
,
375 wxDefaultPosition
, wxDefaultSize
,
382 const int sel
= old_note
->GetSelection();
384 const int count
= old_note
->GetPageCount();
386 // recreate the pages
387 for ( int n
= 0; n
< count
; n
++ )
389 m_notebook
->AddPage(CreateNewPage(),
390 old_note
->GetPageText(n
),
392 m_chkImages
->GetValue() ?
393 GetIconIndex() : -1);
396 m_sizerNotebook
->Detach( old_note
);
402 m_notebook
->SetSelection(sel
);
406 m_sizerNotebook
->Add(m_notebook
, 1, wxGROW
| wxALL
, 5);
407 m_sizerNotebook
->Layout();
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 int NotebookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
417 if ( !text
->GetValue().ToLong(&pos
) )
423 int NotebookWidgetsPage::GetIconIndex() const
427 int nImages
= m_imageList
->GetImageCount();
430 return m_notebook
->GetPageCount() % nImages
;
437 wxWindow
*NotebookWidgetsPage::CreateNewPage()
439 return new wxTextCtrl(m_notebook
, wxID_ANY
, _T("I'm a notebook page"));
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
453 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
455 m_notebook
->DeleteAllPages();
458 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
460 int pos
= GetTextValue(m_textSelect
);
461 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
463 m_notebook
->SetSelection(pos
);
466 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
468 m_notebook
->AddPage(CreateNewPage(), _T("Added page"), false,
472 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
474 int pos
= GetTextValue(m_textInsert
);
475 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
477 m_notebook
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), false,
481 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
483 int pos
= GetTextValue(m_textRemove
);
484 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
486 m_notebook
->DeletePage(pos
);
489 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
491 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
494 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
496 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
499 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
501 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
504 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
506 event
.Enable( !m_chkImages
->GetValue() ||
507 m_radioOrient
->GetSelection() != wxBK_TOP
);
510 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
512 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetPageCount()) );
515 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
517 event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetSelection()) );
520 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
525 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
527 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
528 event
.GetOldSelection(),
529 event
.GetSelection(),
530 m_notebook
->GetSelection());
535 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
537 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
538 event
.GetOldSelection(),
539 event
.GetSelection(),
540 m_notebook
->GetSelection());
545 #endif // wxUSE_NOTEBOOK