]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/notebook.cpp
don't use grey background for the control, it makes selection almost completely invis...
[wxWidgets.git] / samples / widgets / notebook.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
be5a51fb 2// Program: wxWidgets Widgets Sample
32b8ec41
VZ
3// Name: notebook.cpp
4// Purpose: Part of the widgets sample showing wxNotebook
5// Author: Vadim Zeitlin
6// Created: 06.04.01
7// Id: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin
9// License: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
61c083e7
WS
27#if wxUSE_NOTEBOOK
28
32b8ec41
VZ
29// for all others, include the necessary headers
30#ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/log.h"
33
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"
40
41 #include "wx/dynarray.h"
42#endif
43
44#include "wx/sizer.h"
45#include "wx/notebook.h"
389d906b 46#include "wx/artprov.h"
32b8ec41
VZ
47
48#include "widgets.h"
32b8ec41
VZ
49#include "icons/notebook.xpm"
50
51// ----------------------------------------------------------------------------
52// constants
53// ----------------------------------------------------------------------------
54
55// control ids
56enum
57{
58 NotebookPage_Reset = 100,
59 NotebookPage_SelectPage,
60 NotebookPage_AddPage,
61 NotebookPage_InsertPage,
62 NotebookPage_RemovePage,
63 NotebookPage_DeleteAll,
64 NotebookPage_InsertText,
65 NotebookPage_RemoveText,
66 NotebookPage_SelectText,
67 NotebookPage_NumPagesText,
68 NotebookPage_CurSelectText,
69 NotebookPage_Notebook
70};
71
72// notebook orientations
73enum Orient
74{
75 Orient_Top,
76 Orient_Bottom,
77 Orient_Left,
78 Orient_Right,
79 Orient_Max
80};
81
32b8ec41
VZ
82// ----------------------------------------------------------------------------
83// NotebookWidgetsPage
84// ----------------------------------------------------------------------------
85
86class NotebookWidgetsPage : public WidgetsPage
87{
88public:
5378558e 89 NotebookWidgetsPage(wxBookCtrlBase *book, wxImageList *imaglist);
32b8ec41
VZ
90 virtual ~NotebookWidgetsPage();
91
195df7a7
VZ
92 virtual wxControl *GetWidget() const { return m_notebook; }
93
32b8ec41
VZ
94protected:
95 // event handlers
96 void OnPageChanging(wxNotebookEvent& event);
97 void OnPageChanged(wxNotebookEvent& event);
98
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);
105
106 void OnCheckOrRadioBox(wxCommandEvent& event);
107
108 void OnUpdateUINumPagesText(wxUpdateUIEvent& event);
109 void OnUpdateUICurSelectText(wxUpdateUIEvent& event);
110
111 void OnUpdateUISelectButton(wxUpdateUIEvent& event);
112 void OnUpdateUIInsertButton(wxUpdateUIEvent& event);
113 void OnUpdateUIRemoveButton(wxUpdateUIEvent& event);
114
115 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
116
117 // reset the wxNotebook parameters
118 void Reset();
119
120 // (re)create the wxNotebook
121 void CreateNotebook();
122
123 // create or destroy the image list
124 void CreateImageList();
125
126 // create a new page
127 wxWindow *CreateNewPage();
128
129 // get the image index for the new page
130 int GetIconIndex() const;
131
132 // get the numeric value of text ctrl
133 int GetTextValue(wxTextCtrl *text) const;
134
657c8818
JS
135 // is the value in range?
136 bool IsValidValue(int val) const
c3ced9ec 137 { return (val >= 0) && (val < (int) m_notebook->GetPageCount()); }
657c8818 138
32b8ec41
VZ
139 // the controls
140 // ------------
141
142 // the check/radio boxes for styles
143 wxCheckBox *m_chkImages;
144 wxRadioBox *m_radioOrient;
145
146 // the text controls containing input for various commands
147 wxTextCtrl *m_textInsert,
148 *m_textRemove,
149 *m_textSelect;
150
151 // the notebook itself and the sizer it is in
152 wxNotebook *m_notebook;
153 wxSizer *m_sizerNotebook;
154
155 // thei mage list for our notebook
156 wxImageList *m_imageList;
157
158private:
5e173f35
GD
159 DECLARE_EVENT_TABLE()
160 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage)
32b8ec41
VZ
161};
162
163// ----------------------------------------------------------------------------
164// event tables
165// ----------------------------------------------------------------------------
166
167BEGIN_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)
174
175 EVT_UPDATE_UI(NotebookPage_NumPagesText, NotebookWidgetsPage::OnUpdateUINumPagesText)
176 EVT_UPDATE_UI(NotebookPage_CurSelectText, NotebookWidgetsPage::OnUpdateUICurSelectText)
177
178 EVT_UPDATE_UI(NotebookPage_SelectPage, NotebookWidgetsPage::OnUpdateUISelectButton)
179 EVT_UPDATE_UI(NotebookPage_InsertPage, NotebookWidgetsPage::OnUpdateUIInsertButton)
180 EVT_UPDATE_UI(NotebookPage_RemovePage, NotebookWidgetsPage::OnUpdateUIRemoveButton)
181
206d3a16
JS
182 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY, NotebookWidgetsPage::OnPageChanging)
183 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, NotebookWidgetsPage::OnPageChanged)
32b8ec41 184
206d3a16
JS
185 EVT_CHECKBOX(wxID_ANY, NotebookWidgetsPage::OnCheckOrRadioBox)
186 EVT_RADIOBOX(wxID_ANY, NotebookWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
187END_EVENT_TABLE()
188
189// ============================================================================
190// implementation
191// ============================================================================
192
193IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage, _T("Notebook"));
194
5378558e 195NotebookWidgetsPage::NotebookWidgetsPage(wxBookCtrlBase *book,
32b8ec41 196 wxImageList *imaglist)
923f4e79 197 : WidgetsPage(book)
32b8ec41
VZ
198{
199 imaglist->Add(wxBitmap(notebook_xpm));
200
201 // init everything
202 m_chkImages = NULL;
203 m_imageList = NULL;
204
205 m_notebook = (wxNotebook *)NULL;
206 m_sizerNotebook = (wxSizer *)NULL;
207
208 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
209
210 // left pane
206d3a16 211 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
212
213 // must be in sync with Orient enum
214 wxString orientations[] =
215 {
216 _T("&top"),
217 _T("&bottom"),
218 _T("&left"),
219 _T("&right"),
220 };
221
222 wxASSERT_MSG( WXSIZEOF(orientations) == Orient_Max,
223 _T("forgot to update something") );
224
206d3a16
JS
225 m_chkImages = new wxCheckBox(this, wxID_ANY, _T("Show &images"));
226 m_radioOrient = new wxRadioBox(this, wxID_ANY, _T("&Tab orientation"),
32b8ec41
VZ
227 wxDefaultPosition, wxDefaultSize,
228 WXSIZEOF(orientations), orientations,
229 1, wxRA_SPECIFY_COLS);
230
231 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
232
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);
236
237 wxButton *btn = new wxButton(this, NotebookPage_Reset, _T("&Reset"));
238 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
239
240 // middle pane
206d3a16 241 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Contents"));
32b8ec41
VZ
242 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
243
244 wxTextCtrl *text;
245 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Number of pages: "),
246 NotebookPage_NumPagesText,
247 &text);
206d3a16 248 text->SetEditable(false);
32b8ec41
VZ
249 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
250
251 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection: "),
252 NotebookPage_CurSelectText,
253 &text);
206d3a16 254 text->SetEditable(false);
32b8ec41
VZ
255 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
256
257 sizerRow = CreateSizerWithTextAndButton(NotebookPage_SelectPage,
258 _T("&Select page"),
259 NotebookPage_SelectText,
260 &m_textSelect);
261 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
262
263 btn = new wxButton(this, NotebookPage_AddPage, _T("&Add page"));
264 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
265
266 sizerRow = CreateSizerWithTextAndButton(NotebookPage_InsertPage,
267 _T("&Insert page at"),
268 NotebookPage_InsertText,
269 &m_textInsert);
270 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
271
272 sizerRow = CreateSizerWithTextAndButton(NotebookPage_RemovePage,
273 _T("&Remove page"),
274 NotebookPage_RemoveText,
275 &m_textRemove);
276 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
277
278 btn = new wxButton(this, NotebookPage_DeleteAll, _T("&Delete All"));
279 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
280
281 // right pane
282 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
283 m_notebook = new wxNotebook(this, NotebookPage_Notebook);
284 sizerRight->Add(m_notebook, 1, wxGROW | wxALL, 5);
7b127900 285 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
286 m_sizerNotebook = sizerRight; // save it to modify it later
287
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);
292
293 // final initializations
294 Reset();
295 CreateImageList();
296
32b8ec41
VZ
297 SetSizer(sizerTop);
298
299 sizerTop->Fit(this);
300}
301
302NotebookWidgetsPage::~NotebookWidgetsPage()
303{
304 delete m_imageList;
305}
306
307// ----------------------------------------------------------------------------
308// operations
309// ----------------------------------------------------------------------------
310
311void NotebookWidgetsPage::Reset()
312{
206d3a16 313 m_chkImages->SetValue(true);
32b8ec41
VZ
314 m_radioOrient->SetSelection(Orient_Top);
315}
316
317void NotebookWidgetsPage::CreateImageList()
318{
319 if ( m_chkImages->GetValue() )
320 {
321 if ( !m_imageList )
322 {
323 // create a dummy image list with a few icons
324 m_imageList = new wxImageList(32, 32);
389d906b
VS
325 wxSize size(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));
32b8ec41
VZ
330 }
331
332 m_notebook->SetImageList(m_imageList);
333 }
334 else // no images
335 {
336 if ( m_imageList )
337 {
338 delete m_imageList;
339 m_imageList = NULL;
340 }
341 }
342
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
346}
347
348void NotebookWidgetsPage::CreateNotebook()
349{
350 int flags;
351 switch ( m_radioOrient->GetSelection() )
352 {
353 default:
fd76d963 354 wxFAIL_MSG( _T("unknown notebook orientation") );
32b8ec41
VZ
355 // fall through
356
357 case Orient_Top:
2ddb4d13 358 flags = wxBK_TOP;
32b8ec41
VZ
359 break;
360
361 case Orient_Bottom:
2ddb4d13 362 flags = wxBK_BOTTOM;
32b8ec41
VZ
363 break;
364
365 case Orient_Left:
2ddb4d13 366 flags = wxBK_LEFT;
32b8ec41
VZ
367 break;
368
369 case Orient_Right:
2ddb4d13 370 flags = wxBK_RIGHT;
32b8ec41
VZ
371 break;
372 }
373
fd76d963 374 wxNotebook *old_note = m_notebook;
32b8ec41
VZ
375
376 m_notebook = new wxNotebook(this, NotebookPage_Notebook,
377 wxDefaultPosition, wxDefaultSize,
378 flags);
379
380 CreateImageList();
381
fd76d963 382 if ( old_note )
32b8ec41 383 {
fd76d963 384 const int sel = old_note->GetSelection();
32b8ec41 385
fd76d963 386 const int count = old_note->GetPageCount();
2b5f62a0
VZ
387
388 // recreate the pages
32b8ec41
VZ
389 for ( int n = 0; n < count; n++ )
390 {
2b5f62a0 391 m_notebook->AddPage(CreateNewPage(),
fd76d963 392 old_note->GetPageText(n),
206d3a16 393 false,
61c083e7 394 m_chkImages->GetValue() ?
fd76d963 395 GetIconIndex() : -1);
32b8ec41
VZ
396 }
397
fd76d963
JS
398 m_sizerNotebook->Detach( old_note );
399 delete old_note;
32b8ec41
VZ
400
401 // restore selection
402 if ( sel != -1 )
403 {
404 m_notebook->SetSelection(sel);
405 }
406 }
407
408 m_sizerNotebook->Add(m_notebook, 1, wxGROW | wxALL, 5);
409 m_sizerNotebook->Layout();
410}
411
412// ----------------------------------------------------------------------------
413// helpers
414// ----------------------------------------------------------------------------
415
416int NotebookWidgetsPage::GetTextValue(wxTextCtrl *text) const
417{
418 long pos;
419 if ( !text->GetValue().ToLong(&pos) )
420 pos = -1;
421
422 return (int)pos;
423}
424
425int NotebookWidgetsPage::GetIconIndex() const
426{
427 if ( m_imageList )
428 {
429 int nImages = m_imageList->GetImageCount();
430 if ( nImages > 0 )
431 {
432 return m_notebook->GetPageCount() % nImages;
433 }
434 }
435
436 return -1;
437}
438
439wxWindow *NotebookWidgetsPage::CreateNewPage()
440{
206d3a16 441 return new wxTextCtrl(m_notebook, wxID_ANY, _T("I'm a notebook page"));
32b8ec41
VZ
442}
443
444// ----------------------------------------------------------------------------
445// event handlers
446// ----------------------------------------------------------------------------
447
448void NotebookWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
449{
450 Reset();
451
452 CreateNotebook();
453}
454
455void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent& WXUNUSED(event))
456{
457 m_notebook->DeleteAllPages();
458}
459
c02e5a31 460void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
461{
462 int pos = GetTextValue(m_textSelect);
657c8818 463 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
32b8ec41
VZ
464
465 m_notebook->SetSelection(pos);
466}
467
468void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent& WXUNUSED(event))
469{
206d3a16 470 m_notebook->AddPage(CreateNewPage(), _T("Added page"), false,
32b8ec41
VZ
471 GetIconIndex());
472}
473
474void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent& WXUNUSED(event))
475{
476 int pos = GetTextValue(m_textInsert);
657c8818 477 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
32b8ec41 478
206d3a16 479 m_notebook->InsertPage(pos, CreateNewPage(), _T("Inserted page"), false,
32b8ec41
VZ
480 GetIconIndex());
481}
482
483void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent& WXUNUSED(event))
484{
485 int pos = GetTextValue(m_textRemove);
657c8818 486 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
32b8ec41
VZ
487
488 m_notebook->DeletePage(pos);
489}
490
491void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
492{
657c8818 493 event.Enable( IsValidValue(GetTextValue(m_textSelect)) );
32b8ec41
VZ
494}
495
496void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
497{
657c8818 498 event.Enable( IsValidValue(GetTextValue(m_textInsert)) );
32b8ec41
VZ
499}
500
501void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
502{
657c8818 503 event.Enable( IsValidValue(GetTextValue(m_textRemove)) );
32b8ec41
VZ
504}
505
506void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
507{
508 event.Enable( !m_chkImages->GetValue() ||
2ddb4d13 509 m_radioOrient->GetSelection() != wxBK_TOP );
32b8ec41
VZ
510}
511
512void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent& event)
513{
514 event.SetText( wxString::Format(_T("%d"), m_notebook->GetPageCount()) );
515}
516
517void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent& event)
518{
519 event.SetText( wxString::Format(_T("%d"), m_notebook->GetSelection()) );
520}
521
c02e5a31 522void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
523{
524 CreateNotebook();
525}
526
527void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent& event)
528{
529 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
530 event.GetOldSelection(),
531 event.GetSelection(),
532 m_notebook->GetSelection());
533
534 event.Skip();
535}
536
537void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent& event)
538{
539 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
540 event.GetOldSelection(),
541 event.GetSelection(),
542 m_notebook->GetSelection());
543
544 event.Skip();
545}
546
61c083e7 547#endif // wxUSE_NOTEBOOK