]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/notebook.cpp
Acquire the GIL in GetSelections
[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
045bd076
WS
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,
32b8ec41
VZ
221 _T("forgot to update something") );
222
206d3a16
JS
223 m_chkImages = new wxCheckBox(this, wxID_ANY, _T("Show &images"));
224 m_radioOrient = new wxRadioBox(this, wxID_ANY, _T("&Tab orientation"),
32b8ec41 225 wxDefaultPosition, wxDefaultSize,
045bd076 226 orientations, 1, wxRA_SPECIFY_COLS);
32b8ec41
VZ
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
206d3a16 238 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Contents"));
32b8ec41
VZ
239 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
240
241 wxTextCtrl *text;
242 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Number of pages: "),
243 NotebookPage_NumPagesText,
244 &text);
206d3a16 245 text->SetEditable(false);
32b8ec41
VZ
246 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
247
248 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection: "),
249 NotebookPage_CurSelectText,
250 &text);
206d3a16 251 text->SetEditable(false);
32b8ec41
VZ
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);
7b127900 282 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
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
32b8ec41
VZ
294 SetSizer(sizerTop);
295
296 sizerTop->Fit(this);
297}
298
299NotebookWidgetsPage::~NotebookWidgetsPage()
300{
301 delete m_imageList;
302}
303
304// ----------------------------------------------------------------------------
305// operations
306// ----------------------------------------------------------------------------
307
308void NotebookWidgetsPage::Reset()
309{
206d3a16 310 m_chkImages->SetValue(true);
32b8ec41
VZ
311 m_radioOrient->SetSelection(Orient_Top);
312}
313
314void 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);
389d906b
VS
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));
32b8ec41
VZ
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
345void NotebookWidgetsPage::CreateNotebook()
346{
347 int flags;
348 switch ( m_radioOrient->GetSelection() )
349 {
350 default:
fd76d963 351 wxFAIL_MSG( _T("unknown notebook orientation") );
32b8ec41
VZ
352 // fall through
353
354 case Orient_Top:
2ddb4d13 355 flags = wxBK_TOP;
32b8ec41
VZ
356 break;
357
358 case Orient_Bottom:
2ddb4d13 359 flags = wxBK_BOTTOM;
32b8ec41
VZ
360 break;
361
362 case Orient_Left:
2ddb4d13 363 flags = wxBK_LEFT;
32b8ec41
VZ
364 break;
365
366 case Orient_Right:
2ddb4d13 367 flags = wxBK_RIGHT;
32b8ec41
VZ
368 break;
369 }
370
fd76d963 371 wxNotebook *old_note = m_notebook;
32b8ec41
VZ
372
373 m_notebook = new wxNotebook(this, NotebookPage_Notebook,
374 wxDefaultPosition, wxDefaultSize,
375 flags);
376
377 CreateImageList();
378
fd76d963 379 if ( old_note )
32b8ec41 380 {
fd76d963 381 const int sel = old_note->GetSelection();
32b8ec41 382
fd76d963 383 const int count = old_note->GetPageCount();
2b5f62a0
VZ
384
385 // recreate the pages
32b8ec41
VZ
386 for ( int n = 0; n < count; n++ )
387 {
2b5f62a0 388 m_notebook->AddPage(CreateNewPage(),
fd76d963 389 old_note->GetPageText(n),
206d3a16 390 false,
61c083e7 391 m_chkImages->GetValue() ?
fd76d963 392 GetIconIndex() : -1);
32b8ec41
VZ
393 }
394
fd76d963
JS
395 m_sizerNotebook->Detach( old_note );
396 delete old_note;
32b8ec41
VZ
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
413int 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
422int 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
436wxWindow *NotebookWidgetsPage::CreateNewPage()
437{
206d3a16 438 return new wxTextCtrl(m_notebook, wxID_ANY, _T("I'm a notebook page"));
32b8ec41
VZ
439}
440
441// ----------------------------------------------------------------------------
442// event handlers
443// ----------------------------------------------------------------------------
444
445void NotebookWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
446{
447 Reset();
448
449 CreateNotebook();
450}
451
452void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent& WXUNUSED(event))
453{
454 m_notebook->DeleteAllPages();
455}
456
c02e5a31 457void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
458{
459 int pos = GetTextValue(m_textSelect);
657c8818 460 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
32b8ec41
VZ
461
462 m_notebook->SetSelection(pos);
463}
464
465void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent& WXUNUSED(event))
466{
206d3a16 467 m_notebook->AddPage(CreateNewPage(), _T("Added page"), false,
32b8ec41
VZ
468 GetIconIndex());
469}
470
471void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent& WXUNUSED(event))
472{
473 int pos = GetTextValue(m_textInsert);
657c8818 474 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
32b8ec41 475
206d3a16 476 m_notebook->InsertPage(pos, CreateNewPage(), _T("Inserted page"), false,
32b8ec41
VZ
477 GetIconIndex());
478}
479
480void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent& WXUNUSED(event))
481{
482 int pos = GetTextValue(m_textRemove);
657c8818 483 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
32b8ec41
VZ
484
485 m_notebook->DeletePage(pos);
486}
487
488void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
489{
657c8818 490 event.Enable( IsValidValue(GetTextValue(m_textSelect)) );
32b8ec41
VZ
491}
492
493void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
494{
657c8818 495 event.Enable( IsValidValue(GetTextValue(m_textInsert)) );
32b8ec41
VZ
496}
497
498void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
499{
657c8818 500 event.Enable( IsValidValue(GetTextValue(m_textRemove)) );
32b8ec41
VZ
501}
502
503void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
504{
505 event.Enable( !m_chkImages->GetValue() ||
2ddb4d13 506 m_radioOrient->GetSelection() != wxBK_TOP );
32b8ec41
VZ
507}
508
509void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent& event)
510{
511 event.SetText( wxString::Format(_T("%d"), m_notebook->GetPageCount()) );
512}
513
514void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent& event)
515{
516 event.SetText( wxString::Format(_T("%d"), m_notebook->GetSelection()) );
517}
518
c02e5a31 519void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
520{
521 CreateNotebook();
522}
523
524void 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
534void 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
61c083e7 544#endif // wxUSE_NOTEBOOK