Test both constuctors. Warning fix.
[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(wxBookCtrlBase *book, 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(wxBookCtrlBase *book,
196 wxImageList *imaglist)
197 : WidgetsPage(book)
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 wxArrayString orientations;
215 orientations.Add(_T("&top"));
216 orientations.Add(_T("&bottom"));
217 orientations.Add(_T("&left"));
218 orientations.Add(_T("&right"));
219
220 wxASSERT_MSG( orientations.GetCount() == Orient_Max,
221 _T("forgot to update something") );
222
223 m_chkImages = new wxCheckBox(this, wxID_ANY, _T("Show &images"));
224 m_radioOrient = new wxRadioBox(this, wxID_ANY, _T("&Tab orientation"),
225 wxDefaultPosition, wxDefaultSize,
226 orientations, 1, wxRA_SPECIFY_COLS);
227
228 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
229
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);
233
234 wxButton *btn = new wxButton(this, NotebookPage_Reset, _T("&Reset"));
235 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
236
237 // middle pane
238 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Contents"));
239 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
240
241 wxTextCtrl *text;
242 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Number of pages: "),
243 NotebookPage_NumPagesText,
244 &text);
245 text->SetEditable(false);
246 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
247
248 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection: "),
249 NotebookPage_CurSelectText,
250 &text);
251 text->SetEditable(false);
252 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
253
254 sizerRow = CreateSizerWithTextAndButton(NotebookPage_SelectPage,
255 _T("&Select page"),
256 NotebookPage_SelectText,
257 &m_textSelect);
258 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
259
260 btn = new wxButton(this, NotebookPage_AddPage, _T("&Add page"));
261 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
262
263 sizerRow = CreateSizerWithTextAndButton(NotebookPage_InsertPage,
264 _T("&Insert page at"),
265 NotebookPage_InsertText,
266 &m_textInsert);
267 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
268
269 sizerRow = CreateSizerWithTextAndButton(NotebookPage_RemovePage,
270 _T("&Remove page"),
271 NotebookPage_RemoveText,
272 &m_textRemove);
273 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
274
275 btn = new wxButton(this, NotebookPage_DeleteAll, _T("&Delete All"));
276 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
277
278 // right pane
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(150, 0);
283 m_sizerNotebook = sizerRight; // save it to modify it later
284
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);
289
290 // final initializations
291 Reset();
292 CreateImageList();
293
294 SetSizer(sizerTop);
295
296 sizerTop->Fit(this);
297 }
298
299 NotebookWidgetsPage::~NotebookWidgetsPage()
300 {
301 delete m_imageList;
302 }
303
304 // ----------------------------------------------------------------------------
305 // operations
306 // ----------------------------------------------------------------------------
307
308 void NotebookWidgetsPage::Reset()
309 {
310 m_chkImages->SetValue(true);
311 m_radioOrient->SetSelection(Orient_Top);
312 }
313
314 void NotebookWidgetsPage::CreateImageList()
315 {
316 if ( m_chkImages->GetValue() )
317 {
318 if ( !m_imageList )
319 {
320 // create a dummy image list with a few icons
321 m_imageList = new wxImageList(32, 32);
322 wxSize size(32, 32);
323 m_imageList->Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, size));
324 m_imageList->Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, size));
325 m_imageList->Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, size));
326 m_imageList->Add(wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, size));
327 }
328
329 m_notebook->SetImageList(m_imageList);
330 }
331 else // no images
332 {
333 if ( m_imageList )
334 {
335 delete m_imageList;
336 m_imageList = NULL;
337 }
338 }
339
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
343 }
344
345 void NotebookWidgetsPage::CreateNotebook()
346 {
347 int flags;
348 switch ( m_radioOrient->GetSelection() )
349 {
350 default:
351 wxFAIL_MSG( _T("unknown notebook orientation") );
352 // fall through
353
354 case Orient_Top:
355 flags = wxBK_TOP;
356 break;
357
358 case Orient_Bottom:
359 flags = wxBK_BOTTOM;
360 break;
361
362 case Orient_Left:
363 flags = wxBK_LEFT;
364 break;
365
366 case Orient_Right:
367 flags = wxBK_RIGHT;
368 break;
369 }
370
371 wxNotebook *old_note = m_notebook;
372
373 m_notebook = new wxNotebook(this, NotebookPage_Notebook,
374 wxDefaultPosition, wxDefaultSize,
375 flags);
376
377 CreateImageList();
378
379 if ( old_note )
380 {
381 const int sel = old_note->GetSelection();
382
383 const int count = old_note->GetPageCount();
384
385 // recreate the pages
386 for ( int n = 0; n < count; n++ )
387 {
388 m_notebook->AddPage(CreateNewPage(),
389 old_note->GetPageText(n),
390 false,
391 m_chkImages->GetValue() ?
392 GetIconIndex() : -1);
393 }
394
395 m_sizerNotebook->Detach( old_note );
396 delete old_note;
397
398 // restore selection
399 if ( sel != -1 )
400 {
401 m_notebook->SetSelection(sel);
402 }
403 }
404
405 m_sizerNotebook->Add(m_notebook, 1, wxGROW | wxALL, 5);
406 m_sizerNotebook->Layout();
407 }
408
409 // ----------------------------------------------------------------------------
410 // helpers
411 // ----------------------------------------------------------------------------
412
413 int NotebookWidgetsPage::GetTextValue(wxTextCtrl *text) const
414 {
415 long pos;
416 if ( !text->GetValue().ToLong(&pos) )
417 pos = -1;
418
419 return (int)pos;
420 }
421
422 int NotebookWidgetsPage::GetIconIndex() const
423 {
424 if ( m_imageList )
425 {
426 int nImages = m_imageList->GetImageCount();
427 if ( nImages > 0 )
428 {
429 return m_notebook->GetPageCount() % nImages;
430 }
431 }
432
433 return -1;
434 }
435
436 wxWindow *NotebookWidgetsPage::CreateNewPage()
437 {
438 return new wxTextCtrl(m_notebook, wxID_ANY, _T("I'm a notebook page"));
439 }
440
441 // ----------------------------------------------------------------------------
442 // event handlers
443 // ----------------------------------------------------------------------------
444
445 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
446 {
447 Reset();
448
449 CreateNotebook();
450 }
451
452 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent& WXUNUSED(event))
453 {
454 m_notebook->DeleteAllPages();
455 }
456
457 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent& WXUNUSED(event))
458 {
459 int pos = GetTextValue(m_textSelect);
460 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
461
462 m_notebook->SetSelection(pos);
463 }
464
465 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent& WXUNUSED(event))
466 {
467 m_notebook->AddPage(CreateNewPage(), _T("Added page"), false,
468 GetIconIndex());
469 }
470
471 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent& WXUNUSED(event))
472 {
473 int pos = GetTextValue(m_textInsert);
474 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
475
476 m_notebook->InsertPage(pos, CreateNewPage(), _T("Inserted page"), false,
477 GetIconIndex());
478 }
479
480 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent& WXUNUSED(event))
481 {
482 int pos = GetTextValue(m_textRemove);
483 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
484
485 m_notebook->DeletePage(pos);
486 }
487
488 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
489 {
490 event.Enable( IsValidValue(GetTextValue(m_textSelect)) );
491 }
492
493 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
494 {
495 event.Enable( IsValidValue(GetTextValue(m_textInsert)) );
496 }
497
498 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
499 {
500 event.Enable( IsValidValue(GetTextValue(m_textRemove)) );
501 }
502
503 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
504 {
505 event.Enable( !m_chkImages->GetValue() ||
506 m_radioOrient->GetSelection() != wxBK_TOP );
507 }
508
509 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent& event)
510 {
511 event.SetText( wxString::Format(_T("%d"), m_notebook->GetPageCount()) );
512 }
513
514 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent& event)
515 {
516 event.SetText( wxString::Format(_T("%d"), m_notebook->GetSelection()) );
517 }
518
519 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
520 {
521 CreateNotebook();
522 }
523
524 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent& event)
525 {
526 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
527 event.GetOldSelection(),
528 event.GetSelection(),
529 m_notebook->GetSelection());
530
531 event.Skip();
532 }
533
534 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent& event)
535 {
536 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
537 event.GetOldSelection(),
538 event.GetSelection(),
539 m_notebook->GetSelection());
540
541 event.Skip();
542 }
543
544 #endif // wxUSE_NOTEBOOK