]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/notebook.cpp
Added files to Tex2RTF file list
[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 int sel = notebook->GetSelection();
384
385 int count = notebook->GetPageCount();
386 for ( int n = 0; n < count; n++ )
387 {
388 wxNotebookPage *page = notebook->GetPage(0);
389 page->Reparent(m_notebook);
390
391 m_notebook->AddPage(page, notebook->GetPageText(0), FALSE,
392 notebook->GetPageImage(0));
393
394 notebook->RemovePage(0);
395 }
396
397 m_sizerNotebook->Remove(notebook);
398 delete notebook;
399
400 // restore selection
401 if ( sel != -1 )
402 {
403 m_notebook->SetSelection(sel);
404 }
405 }
406
407 m_sizerNotebook->Add(m_notebook, 1, wxGROW | wxALL, 5);
408 m_sizerNotebook->Layout();
409 }
410
411 // ----------------------------------------------------------------------------
412 // helpers
413 // ----------------------------------------------------------------------------
414
415 int NotebookWidgetsPage::GetTextValue(wxTextCtrl *text) const
416 {
417 long pos;
418 if ( !text->GetValue().ToLong(&pos) )
419 pos = -1;
420
421 return (int)pos;
422 }
423
424 int NotebookWidgetsPage::GetIconIndex() const
425 {
426 if ( m_imageList )
427 {
428 int nImages = m_imageList->GetImageCount();
429 if ( nImages > 0 )
430 {
431 return m_notebook->GetPageCount() % nImages;
432 }
433 }
434
435 return -1;
436 }
437
438 wxWindow *NotebookWidgetsPage::CreateNewPage()
439 {
440 return new wxTextCtrl(m_notebook, -1, _T("I'm a notebook page"));
441 }
442
443 // ----------------------------------------------------------------------------
444 // event handlers
445 // ----------------------------------------------------------------------------
446
447 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
448 {
449 Reset();
450
451 CreateNotebook();
452 }
453
454 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent& WXUNUSED(event))
455 {
456 m_notebook->DeleteAllPages();
457 }
458
459 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent& event)
460 {
461 int pos = GetTextValue(m_textSelect);
462 wxCHECK_RET( pos >= 0, _T("button should be disabled") );
463
464 m_notebook->SetSelection(pos);
465 }
466
467 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent& WXUNUSED(event))
468 {
469 m_notebook->AddPage(CreateNewPage(), _T("Added page"), FALSE,
470 GetIconIndex());
471 }
472
473 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent& WXUNUSED(event))
474 {
475 int pos = GetTextValue(m_textInsert);
476 wxCHECK_RET( pos >= 0, _T("button should be disabled") );
477
478 m_notebook->InsertPage(pos, CreateNewPage(), _T("Inserted page"), FALSE,
479 GetIconIndex());
480 }
481
482 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent& WXUNUSED(event))
483 {
484 int pos = GetTextValue(m_textRemove);
485 wxCHECK_RET( pos >= 0, _T("button should be disabled") );
486
487 m_notebook->DeletePage(pos);
488 }
489
490 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
491 {
492 event.Enable( GetTextValue(m_textSelect) >= 0 );
493 }
494
495 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
496 {
497 event.Enable( GetTextValue(m_textInsert) >= 0 );
498 }
499
500 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
501 {
502 event.Enable( GetTextValue(m_textRemove) >= 0 );
503 }
504
505 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
506 {
507 event.Enable( !m_chkImages->GetValue() ||
508 m_radioOrient->GetSelection() != wxNB_TOP );
509 }
510
511 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent& event)
512 {
513 event.SetText( wxString::Format(_T("%d"), m_notebook->GetPageCount()) );
514 }
515
516 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent& event)
517 {
518 event.SetText( wxString::Format(_T("%d"), m_notebook->GetSelection()) );
519 }
520
521 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
522 {
523 CreateNotebook();
524 }
525
526 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent& event)
527 {
528 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
529 event.GetOldSelection(),
530 event.GetSelection(),
531 m_notebook->GetSelection());
532
533 event.Skip();
534 }
535
536 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent& event)
537 {
538 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
539 event.GetOldSelection(),
540 event.GetSelection(),
541 m_notebook->GetSelection());
542
543 event.Skip();
544 }
545
546 #endif