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