wxBookCtrl->wxBookCtrlBase. wxBookCtrl is now most suitable book for given platform...
[wxWidgets.git] / samples / widgets / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
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
27 #if wxUSE_NOTEBOOK
28
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"
46 #include "wx/artprov.h"
47
48 #include "widgets.h"
49 #include "icons/notebook.xpm"
50
51 // ----------------------------------------------------------------------------
52 // constants
53 // ----------------------------------------------------------------------------
54
55 // control ids
56 enum
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
73 enum Orient
74 {
75 Orient_Top,
76 Orient_Bottom,
77 Orient_Left,
78 Orient_Right,
79 Orient_Max
80 };
81
82 // ----------------------------------------------------------------------------
83 // NotebookWidgetsPage
84 // ----------------------------------------------------------------------------
85
86 class NotebookWidgetsPage : public WidgetsPage
87 {
88 public:
89 NotebookWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
90 virtual ~NotebookWidgetsPage();
91
92 virtual wxControl *GetWidget() const { return m_notebook; }
93
94 protected:
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
135 // is the value in range?
136 bool IsValidValue(int val) const
137 { return (val >= 0) && (val < (int) m_notebook->GetPageCount()); }
138
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
158 private:
159 DECLARE_EVENT_TABLE()
160 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage)
161 };
162
163 // ----------------------------------------------------------------------------
164 // event tables
165 // ----------------------------------------------------------------------------
166
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)
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
182 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY, NotebookWidgetsPage::OnPageChanging)
183 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, NotebookWidgetsPage::OnPageChanged)
184
185 EVT_CHECKBOX(wxID_ANY, NotebookWidgetsPage::OnCheckOrRadioBox)
186 EVT_RADIOBOX(wxID_ANY, NotebookWidgetsPage::OnCheckOrRadioBox)
187 END_EVENT_TABLE()
188
189 // ============================================================================
190 // implementation
191 // ============================================================================
192
193 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage, _T("Notebook"));
194
195 NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook *notebook,
196 wxImageList *imaglist)
197 : WidgetsPage(notebook)
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
211 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
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
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);
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
241 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Contents"));
242 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
243
244 wxTextCtrl *text;
245 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Number of pages: "),
246 NotebookPage_NumPagesText,
247 &text);
248 text->SetEditable(false);
249 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
250
251 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection: "),
252 NotebookPage_CurSelectText,
253 &text);
254 text->SetEditable(false);
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);
285 sizerRight->SetMinSize(150, 0);
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
297 SetSizer(sizerTop);
298
299 sizerTop->Fit(this);
300 }
301
302 NotebookWidgetsPage::~NotebookWidgetsPage()
303 {
304 delete m_imageList;
305 }
306
307 // ----------------------------------------------------------------------------
308 // operations
309 // ----------------------------------------------------------------------------
310
311 void NotebookWidgetsPage::Reset()
312 {
313 m_chkImages->SetValue(true);
314 m_radioOrient->SetSelection(Orient_Top);
315 }
316
317 void 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);
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));
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
348 void NotebookWidgetsPage::CreateNotebook()
349 {
350 int flags;
351 switch ( m_radioOrient->GetSelection() )
352 {
353 default:
354 wxFAIL_MSG( _T("unknown notebook orientation") );
355 // fall through
356
357 case Orient_Top:
358 flags = wxNB_TOP;
359 break;
360
361 case Orient_Bottom:
362 flags = wxNB_BOTTOM;
363 break;
364
365 case Orient_Left:
366 flags = wxNB_LEFT;
367 break;
368
369 case Orient_Right:
370 flags = wxNB_RIGHT;
371 break;
372 }
373
374 wxNotebook *old_note = m_notebook;
375
376 m_notebook = new wxNotebook(this, NotebookPage_Notebook,
377 wxDefaultPosition, wxDefaultSize,
378 flags);
379
380 CreateImageList();
381
382 if ( old_note )
383 {
384 const int sel = old_note->GetSelection();
385
386 const int count = old_note->GetPageCount();
387
388 // recreate the pages
389 for ( int n = 0; n < count; n++ )
390 {
391 m_notebook->AddPage(CreateNewPage(),
392 old_note->GetPageText(n),
393 false,
394 m_chkImages->GetValue() ?
395 GetIconIndex() : -1);
396 }
397
398 m_sizerNotebook->Detach( old_note );
399 delete old_note;
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
416 int 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
425 int 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
439 wxWindow *NotebookWidgetsPage::CreateNewPage()
440 {
441 return new wxTextCtrl(m_notebook, wxID_ANY, _T("I'm a notebook page"));
442 }
443
444 // ----------------------------------------------------------------------------
445 // event handlers
446 // ----------------------------------------------------------------------------
447
448 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
449 {
450 Reset();
451
452 CreateNotebook();
453 }
454
455 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent& WXUNUSED(event))
456 {
457 m_notebook->DeleteAllPages();
458 }
459
460 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent& WXUNUSED(event))
461 {
462 int pos = GetTextValue(m_textSelect);
463 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
464
465 m_notebook->SetSelection(pos);
466 }
467
468 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent& WXUNUSED(event))
469 {
470 m_notebook->AddPage(CreateNewPage(), _T("Added page"), false,
471 GetIconIndex());
472 }
473
474 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent& WXUNUSED(event))
475 {
476 int pos = GetTextValue(m_textInsert);
477 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
478
479 m_notebook->InsertPage(pos, CreateNewPage(), _T("Inserted page"), false,
480 GetIconIndex());
481 }
482
483 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent& WXUNUSED(event))
484 {
485 int pos = GetTextValue(m_textRemove);
486 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
487
488 m_notebook->DeletePage(pos);
489 }
490
491 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
492 {
493 event.Enable( IsValidValue(GetTextValue(m_textSelect)) );
494 }
495
496 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
497 {
498 event.Enable( IsValidValue(GetTextValue(m_textInsert)) );
499 }
500
501 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
502 {
503 event.Enable( IsValidValue(GetTextValue(m_textRemove)) );
504 }
505
506 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
507 {
508 event.Enable( !m_chkImages->GetValue() ||
509 m_radioOrient->GetSelection() != wxNB_TOP );
510 }
511
512 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent& event)
513 {
514 event.SetText( wxString::Format(_T("%d"), m_notebook->GetPageCount()) );
515 }
516
517 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent& event)
518 {
519 event.SetText( wxString::Format(_T("%d"), m_notebook->GetSelection()) );
520 }
521
522 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
523 {
524 CreateNotebook();
525 }
526
527 void 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
537 void 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
547 #endif // wxUSE_NOTEBOOK