]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/notebook.cpp
bakefile-generated makefiles for the sample
[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 virtual void RecreateWidget() { CreateNotebook(); }
94
95 protected:
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
136 // is the value in range?
137 bool IsValidValue(int val) const
138 { return (val >= 0) && (val < (int) m_notebook->GetPageCount()); }
139
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
159 private:
160 DECLARE_EVENT_TABLE()
161 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage)
162 };
163
164 // ----------------------------------------------------------------------------
165 // event tables
166 // ----------------------------------------------------------------------------
167
168 BEGIN_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
183 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY, NotebookWidgetsPage::OnPageChanging)
184 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, NotebookWidgetsPage::OnPageChanged)
185
186 EVT_CHECKBOX(wxID_ANY, NotebookWidgetsPage::OnCheckOrRadioBox)
187 EVT_RADIOBOX(wxID_ANY, NotebookWidgetsPage::OnCheckOrRadioBox)
188 END_EVENT_TABLE()
189
190 // ============================================================================
191 // implementation
192 // ============================================================================
193
194 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage, _T("Notebook"));
195
196 NotebookWidgetsPage::NotebookWidgetsPage(wxBookCtrlBase *book,
197 wxImageList *imaglist)
198 : WidgetsPage(book)
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
212 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
213
214 // must be in sync with Orient enum
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,
222 _T("forgot to update something") );
223
224 m_chkImages = new wxCheckBox(this, wxID_ANY, _T("Show &images"));
225 m_radioOrient = new wxRadioBox(this, wxID_ANY, _T("&Tab orientation"),
226 wxDefaultPosition, wxDefaultSize,
227 orientations, 1, wxRA_SPECIFY_COLS);
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
239 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Contents"));
240 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
241
242 wxTextCtrl *text;
243 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Number of pages: "),
244 NotebookPage_NumPagesText,
245 &text);
246 text->SetEditable(false);
247 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
248
249 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection: "),
250 NotebookPage_CurSelectText,
251 &text);
252 text->SetEditable(false);
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);
283 sizerRight->SetMinSize(150, 0);
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
295 SetSizer(sizerTop);
296
297 sizerTop->Fit(this);
298 }
299
300 NotebookWidgetsPage::~NotebookWidgetsPage()
301 {
302 delete m_imageList;
303 }
304
305 // ----------------------------------------------------------------------------
306 // operations
307 // ----------------------------------------------------------------------------
308
309 void NotebookWidgetsPage::Reset()
310 {
311 m_chkImages->SetValue(true);
312 m_radioOrient->SetSelection(Orient_Top);
313 }
314
315 void 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);
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));
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
346 void NotebookWidgetsPage::CreateNotebook()
347 {
348 int flags = ms_defaultFlags;
349 switch ( m_radioOrient->GetSelection() )
350 {
351 default:
352 wxFAIL_MSG( _T("unknown notebook orientation") );
353 // fall through
354
355 case Orient_Top:
356 flags |= wxBK_TOP;
357 break;
358
359 case Orient_Bottom:
360 flags |= wxBK_BOTTOM;
361 break;
362
363 case Orient_Left:
364 flags |= wxBK_LEFT;
365 break;
366
367 case Orient_Right:
368 flags |= wxBK_RIGHT;
369 break;
370 }
371
372 wxNotebook *old_note = m_notebook;
373
374 m_notebook = new wxNotebook(this, NotebookPage_Notebook,
375 wxDefaultPosition, wxDefaultSize,
376 flags);
377
378 CreateImageList();
379
380 if ( old_note )
381 {
382 const int sel = old_note->GetSelection();
383
384 const int count = old_note->GetPageCount();
385
386 // recreate the pages
387 for ( int n = 0; n < count; n++ )
388 {
389 m_notebook->AddPage(CreateNewPage(),
390 old_note->GetPageText(n),
391 false,
392 m_chkImages->GetValue() ?
393 GetIconIndex() : -1);
394 }
395
396 m_sizerNotebook->Detach( old_note );
397 delete old_note;
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
414 int 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
423 int 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
437 wxWindow *NotebookWidgetsPage::CreateNewPage()
438 {
439 return new wxTextCtrl(m_notebook, wxID_ANY, _T("I'm a notebook page"));
440 }
441
442 // ----------------------------------------------------------------------------
443 // event handlers
444 // ----------------------------------------------------------------------------
445
446 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
447 {
448 Reset();
449
450 CreateNotebook();
451 }
452
453 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent& WXUNUSED(event))
454 {
455 m_notebook->DeleteAllPages();
456 }
457
458 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent& WXUNUSED(event))
459 {
460 int pos = GetTextValue(m_textSelect);
461 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
462
463 m_notebook->SetSelection(pos);
464 }
465
466 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent& WXUNUSED(event))
467 {
468 m_notebook->AddPage(CreateNewPage(), _T("Added page"), false,
469 GetIconIndex());
470 }
471
472 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent& WXUNUSED(event))
473 {
474 int pos = GetTextValue(m_textInsert);
475 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
476
477 m_notebook->InsertPage(pos, CreateNewPage(), _T("Inserted page"), false,
478 GetIconIndex());
479 }
480
481 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent& WXUNUSED(event))
482 {
483 int pos = GetTextValue(m_textRemove);
484 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
485
486 m_notebook->DeletePage(pos);
487 }
488
489 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
490 {
491 event.Enable( IsValidValue(GetTextValue(m_textSelect)) );
492 }
493
494 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
495 {
496 event.Enable( IsValidValue(GetTextValue(m_textInsert)) );
497 }
498
499 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
500 {
501 event.Enable( IsValidValue(GetTextValue(m_textRemove)) );
502 }
503
504 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
505 {
506 event.Enable( !m_chkImages->GetValue() ||
507 m_radioOrient->GetSelection() != wxBK_TOP );
508 }
509
510 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent& event)
511 {
512 event.SetText( wxString::Format(_T("%d"), m_notebook->GetPageCount()) );
513 }
514
515 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent& event)
516 {
517 event.SetText( wxString::Format(_T("%d"), m_notebook->GetSelection()) );
518 }
519
520 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
521 {
522 CreateNotebook();
523 }
524
525 void 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
535 void 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
545 #endif // wxUSE_NOTEBOOK