]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/notebook.cpp
even more fixes to last-minute wxFileSystem mods
[wxWidgets.git] / samples / widgets / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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 // 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"
44 #include "wx/artprov.h"
45
46 #include "widgets.h"
47 #if 1
48 #include "icons/notebook.xpm"
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 // control ids
55 enum
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
72 enum Orient
73 {
74 Orient_Top,
75 Orient_Bottom,
76 Orient_Left,
77 Orient_Right,
78 Orient_Max
79 };
80
81 // old versions of wxWindows don't define this style
82 #ifndef wxNB_TOP
83 #define wxNB_TOP (0)
84 #endif
85
86 // ----------------------------------------------------------------------------
87 // NotebookWidgetsPage
88 // ----------------------------------------------------------------------------
89
90 class NotebookWidgetsPage : public WidgetsPage
91 {
92 public:
93 NotebookWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
94 virtual ~NotebookWidgetsPage();
95
96 protected:
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
137 // the controls
138 // ------------
139
140 // the check/radio boxes for styles
141 wxCheckBox *m_chkImages;
142 wxRadioBox *m_radioOrient;
143
144 // the text controls containing input for various commands
145 wxTextCtrl *m_textInsert,
146 *m_textRemove,
147 *m_textSelect;
148
149 // the notebook itself and the sizer it is in
150 wxNotebook *m_notebook;
151 wxSizer *m_sizerNotebook;
152
153 // thei mage list for our notebook
154 wxImageList *m_imageList;
155
156 private:
157 DECLARE_EVENT_TABLE()
158 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage)
159 };
160
161 // ----------------------------------------------------------------------------
162 // event tables
163 // ----------------------------------------------------------------------------
164
165 BEGIN_EVENT_TABLE(NotebookWidgetsPage, WidgetsPage)
166 EVT_BUTTON(NotebookPage_Reset, NotebookWidgetsPage::OnButtonReset)
167 EVT_BUTTON(NotebookPage_SelectPage, NotebookWidgetsPage::OnButtonSelectPage)
168 EVT_BUTTON(NotebookPage_AddPage, NotebookWidgetsPage::OnButtonAddPage)
169 EVT_BUTTON(NotebookPage_InsertPage, NotebookWidgetsPage::OnButtonInsertPage)
170 EVT_BUTTON(NotebookPage_RemovePage, NotebookWidgetsPage::OnButtonRemovePage)
171 EVT_BUTTON(NotebookPage_DeleteAll, NotebookWidgetsPage::OnButtonDeleteAll)
172
173 EVT_UPDATE_UI(NotebookPage_NumPagesText, NotebookWidgetsPage::OnUpdateUINumPagesText)
174 EVT_UPDATE_UI(NotebookPage_CurSelectText, NotebookWidgetsPage::OnUpdateUICurSelectText)
175
176 EVT_UPDATE_UI(NotebookPage_SelectPage, NotebookWidgetsPage::OnUpdateUISelectButton)
177 EVT_UPDATE_UI(NotebookPage_InsertPage, NotebookWidgetsPage::OnUpdateUIInsertButton)
178 EVT_UPDATE_UI(NotebookPage_RemovePage, NotebookWidgetsPage::OnUpdateUIRemoveButton)
179
180 EVT_NOTEBOOK_PAGE_CHANGING(-1, NotebookWidgetsPage::OnPageChanging)
181 EVT_NOTEBOOK_PAGE_CHANGED(-1, NotebookWidgetsPage::OnPageChanged)
182
183 EVT_CHECKBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox)
184 EVT_RADIOBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox)
185 END_EVENT_TABLE()
186
187 // ============================================================================
188 // implementation
189 // ============================================================================
190
191 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage, _T("Notebook"));
192
193 NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook *notebook,
194 wxImageList *imaglist)
195 : WidgetsPage(notebook)
196 {
197 imaglist->Add(wxBitmap(notebook_xpm));
198
199 // init everything
200 m_chkImages = NULL;
201 m_imageList = NULL;
202
203 m_notebook = (wxNotebook *)NULL;
204 m_sizerNotebook = (wxSizer *)NULL;
205
206 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
207
208 // left pane
209 wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
210
211 // must be in sync with Orient enum
212 wxString orientations[] =
213 {
214 _T("&top"),
215 _T("&bottom"),
216 _T("&left"),
217 _T("&right"),
218 };
219
220 wxASSERT_MSG( WXSIZEOF(orientations) == Orient_Max,
221 _T("forgot to update something") );
222
223 m_chkImages = new wxCheckBox(this, -1, _T("Show &images"));
224 m_radioOrient = new wxRadioBox(this, -1, _T("&Tab orientation"),
225 wxDefaultPosition, wxDefaultSize,
226 WXSIZEOF(orientations), orientations,
227 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, -1, _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 SetAutoLayout(TRUE);
296 SetSizer(sizerTop);
297
298 sizerTop->Fit(this);
299 }
300
301 NotebookWidgetsPage::~NotebookWidgetsPage()
302 {
303 delete m_imageList;
304 }
305
306 // ----------------------------------------------------------------------------
307 // operations
308 // ----------------------------------------------------------------------------
309
310 void NotebookWidgetsPage::Reset()
311 {
312 m_chkImages->SetValue(TRUE);
313 m_radioOrient->SetSelection(Orient_Top);
314 }
315
316 void NotebookWidgetsPage::CreateImageList()
317 {
318 if ( m_chkImages->GetValue() )
319 {
320 if ( !m_imageList )
321 {
322 // create a dummy image list with a few icons
323 m_imageList = new wxImageList(32, 32);
324 wxSize size(32, 32);
325 m_imageList->Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, size));
326 m_imageList->Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, size));
327 m_imageList->Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, size));
328 m_imageList->Add(wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, size));
329 }
330
331 m_notebook->SetImageList(m_imageList);
332 }
333 else // no images
334 {
335 if ( m_imageList )
336 {
337 delete m_imageList;
338 m_imageList = NULL;
339 }
340 }
341
342 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
343 // it would be logical if this removed the image list from notebook, under
344 // MSW it crashes instead
345 }
346
347 void NotebookWidgetsPage::CreateNotebook()
348 {
349 int flags;
350 switch ( m_radioOrient->GetSelection() )
351 {
352 default:
353 wxFAIL_MSG( _T("unknown notebok orientation") );
354 // fall through
355
356 case Orient_Top:
357 flags = wxNB_TOP;
358 break;
359
360 case Orient_Bottom:
361 flags = wxNB_BOTTOM;
362 break;
363
364 case Orient_Left:
365 flags = wxNB_LEFT;
366 break;
367
368 case Orient_Right:
369 flags = wxNB_RIGHT;
370 break;
371 }
372
373 wxNotebook *notebook = m_notebook;
374
375 m_notebook = new wxNotebook(this, NotebookPage_Notebook,
376 wxDefaultPosition, wxDefaultSize,
377 flags);
378
379 CreateImageList();
380
381 if ( notebook )
382 {
383 const int sel = notebook->GetSelection();
384
385 const int count = notebook->GetPageCount();
386
387 // recreate the pages
388 for ( int n = 0; n < count; n++ )
389 {
390 m_notebook->AddPage(CreateNewPage(),
391 notebook->GetPageText(n),
392 FALSE,
393 notebook->GetPageImage(n));
394 }
395
396 m_sizerNotebook->Remove(notebook);
397 delete notebook;
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, -1, _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& event)
459 {
460 int pos = GetTextValue(m_textSelect);
461 wxCHECK_RET( pos >= 0, _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( pos >= 0, _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( pos >= 0, _T("button should be disabled") );
485
486 m_notebook->DeletePage(pos);
487 }
488
489 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
490 {
491 event.Enable( GetTextValue(m_textSelect) >= 0 );
492 }
493
494 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
495 {
496 event.Enable( GetTextValue(m_textInsert) >= 0 );
497 }
498
499 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
500 {
501 event.Enable( GetTextValue(m_textRemove) >= 0 );
502 }
503
504 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
505 {
506 event.Enable( !m_chkImages->GetValue() ||
507 m_radioOrient->GetSelection() != wxNB_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& 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