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