]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/notebook.cpp
wxID_ANY usage.
[wxWidgets.git] / samples / widgets / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: notebook.cpp
4 // Purpose: Part of the widgets sample showing book controls
5 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
6 // Created: 06.04.01
7 // Id: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin, 2006 Wlodzimierz Skiba
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_BOOKCTRL
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/bookctrl.h"
46 #include "wx/artprov.h"
47
48 #include "widgets.h"
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 // control ids
55 enum
56 {
57 BookPage_Reset = wxID_HIGHEST,
58 BookPage_SelectPage,
59 BookPage_AddPage,
60 BookPage_InsertPage,
61 BookPage_RemovePage,
62 BookPage_DeleteAll,
63 BookPage_InsertText,
64 BookPage_RemoveText,
65 BookPage_SelectText,
66 BookPage_NumPagesText,
67 BookPage_CurSelectText,
68 BookPage_Book
69 };
70
71 // book orientations
72 enum Orient
73 {
74 Orient_Top,
75 Orient_Bottom,
76 Orient_Left,
77 Orient_Right,
78 Orient_Max
79 };
80
81 // ----------------------------------------------------------------------------
82 // BookWidgetsPage
83 // ----------------------------------------------------------------------------
84
85 class BookWidgetsPage : public WidgetsPage
86 {
87 public:
88 BookWidgetsPage(WidgetsBookCtrl *book);
89 virtual ~BookWidgetsPage();
90
91 virtual wxControl *GetWidget() const { return m_book; }
92 virtual void RecreateWidget() { RecreateBook(); }
93
94 protected:
95 // event handlers
96 #ifdef ABX
97 void OnPageChanging(wxNotebookEvent& event);
98 void OnPageChanged(wxNotebookEvent& event);
99 #endif
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 book parameters
120 void Reset();
121
122 // (re)create book
123 void RecreateBook();
124 virtual wxBookCtrlBase *CreateBook(long flags) = 0;
125
126 // create or destroy the image list
127 void CreateImageList();
128
129 // create a new page
130 wxWindow *CreateNewPage();
131
132 // get the image index for the new page
133 int GetIconIndex() const;
134
135 // get the numeric value of text ctrl
136 int GetTextValue(wxTextCtrl *text) const;
137
138 // is the value in range?
139 bool IsValidValue(int val) const
140 { return (val >= 0) && (val < (int) m_book->GetPageCount()); }
141
142 // the controls
143 // ------------
144
145 // the check/radio boxes for styles
146 wxCheckBox *m_chkImages;
147 wxRadioBox *m_radioOrient;
148
149 // the text controls containing input for various commands
150 wxTextCtrl *m_textInsert,
151 *m_textRemove,
152 *m_textSelect;
153
154 // the book itself and the sizer it is in
155 wxBookCtrlBase *m_book;
156 wxSizer *m_sizerBook;
157
158 // thei mage list for our book
159 wxImageList *m_imageList;
160
161 private:
162 DECLARE_EVENT_TABLE()
163 };
164
165 // ----------------------------------------------------------------------------
166 // event tables
167 // ----------------------------------------------------------------------------
168
169 BEGIN_EVENT_TABLE(BookWidgetsPage, WidgetsPage)
170 EVT_BUTTON(BookPage_Reset, BookWidgetsPage::OnButtonReset)
171 EVT_BUTTON(BookPage_SelectPage, BookWidgetsPage::OnButtonSelectPage)
172 EVT_BUTTON(BookPage_AddPage, BookWidgetsPage::OnButtonAddPage)
173 EVT_BUTTON(BookPage_InsertPage, BookWidgetsPage::OnButtonInsertPage)
174 EVT_BUTTON(BookPage_RemovePage, BookWidgetsPage::OnButtonRemovePage)
175 EVT_BUTTON(BookPage_DeleteAll, BookWidgetsPage::OnButtonDeleteAll)
176
177 EVT_UPDATE_UI(BookPage_NumPagesText, BookWidgetsPage::OnUpdateUINumPagesText)
178 EVT_UPDATE_UI(BookPage_CurSelectText, BookWidgetsPage::OnUpdateUICurSelectText)
179
180 EVT_UPDATE_UI(BookPage_SelectPage, BookWidgetsPage::OnUpdateUISelectButton)
181 EVT_UPDATE_UI(BookPage_InsertPage, BookWidgetsPage::OnUpdateUIInsertButton)
182 EVT_UPDATE_UI(BookPage_RemovePage, BookWidgetsPage::OnUpdateUIRemoveButton)
183
184 #ifdef ABX
185 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY, BookWidgetsPage::OnPageChanging)
186 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, NotebookWidgetsPage::OnPageChanged)
187 #endif
188
189 EVT_CHECKBOX(wxID_ANY, BookWidgetsPage::OnCheckOrRadioBox)
190 EVT_RADIOBOX(wxID_ANY, BookWidgetsPage::OnCheckOrRadioBox)
191 END_EVENT_TABLE()
192
193 // ============================================================================
194 // implementation
195 // ============================================================================
196
197 BookWidgetsPage::BookWidgetsPage(WidgetsBookCtrl *book)
198 :WidgetsPage(book)
199 {
200 // init everything
201 m_chkImages = NULL;
202 m_imageList = NULL;
203
204 m_book = NULL;
205 m_sizerBook = (wxSizer *)NULL;
206
207 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
208
209 // left pane
210 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
211
212 // must be in sync with Orient enum
213 wxArrayString orientations;
214 orientations.Add(_T("&top"));
215 orientations.Add(_T("&bottom"));
216 orientations.Add(_T("&left"));
217 orientations.Add(_T("&right"));
218
219 wxASSERT_MSG( orientations.GetCount() == Orient_Max,
220 _T("forgot to update something") );
221
222 m_chkImages = new wxCheckBox(this, wxID_ANY, _T("Show &images"));
223 m_radioOrient = new wxRadioBox(this, wxID_ANY, _T("&Tab orientation"),
224 wxDefaultPosition, wxDefaultSize,
225 orientations, 1, wxRA_SPECIFY_COLS);
226
227 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
228
229 sizerLeft->Add(m_chkImages, 0, wxALL, 5);
230 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
231 sizerLeft->Add(m_radioOrient, 0, wxALL, 5);
232
233 wxButton *btn = new wxButton(this, BookPage_Reset, _T("&Reset"));
234 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
235
236 // middle pane
237 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Contents"));
238 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
239
240 wxTextCtrl *text;
241 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Number of pages: "),
242 BookPage_NumPagesText,
243 &text);
244 text->SetEditable(false);
245 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
246
247 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection: "),
248 BookPage_CurSelectText,
249 &text);
250 text->SetEditable(false);
251 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
252
253 sizerRow = CreateSizerWithTextAndButton(BookPage_SelectPage,
254 _T("&Select page"),
255 BookPage_SelectText,
256 &m_textSelect);
257 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
258
259 btn = new wxButton(this, BookPage_AddPage, _T("&Add page"));
260 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
261
262 sizerRow = CreateSizerWithTextAndButton(BookPage_InsertPage,
263 _T("&Insert page at"),
264 BookPage_InsertText,
265 &m_textInsert);
266 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
267
268 sizerRow = CreateSizerWithTextAndButton(BookPage_RemovePage,
269 _T("&Remove page"),
270 BookPage_RemoveText,
271 &m_textRemove);
272 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
273
274 btn = new wxButton(this, BookPage_DeleteAll, _T("&Delete All"));
275 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
276
277 // right pane
278 m_sizerBook = new wxBoxSizer(wxHORIZONTAL);
279
280 // the 3 panes compose the window
281 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
282 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
283 sizerTop->Add(m_sizerBook, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
284
285 // final initializations
286 Reset();
287 CreateImageList();
288
289 SetSizer(sizerTop);
290
291 sizerTop->Fit(this);
292 }
293
294 BookWidgetsPage::~BookWidgetsPage()
295 {
296 delete m_imageList;
297 }
298
299 // ----------------------------------------------------------------------------
300 // operations
301 // ----------------------------------------------------------------------------
302
303 void BookWidgetsPage::Reset()
304 {
305 m_chkImages->SetValue(true);
306 m_radioOrient->SetSelection(Orient_Top);
307 }
308
309 void BookWidgetsPage::CreateImageList()
310 {
311 if ( m_chkImages->GetValue() )
312 {
313 if ( !m_imageList )
314 {
315 // create a dummy image list with a few icons
316 m_imageList = new wxImageList(32, 32);
317 wxSize size(32, 32);
318 m_imageList->Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, size));
319 m_imageList->Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, size));
320 m_imageList->Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, size));
321 m_imageList->Add(wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, size));
322 }
323
324 if ( m_book )
325 m_book->SetImageList(m_imageList);
326 }
327 else // no images
328 {
329 if ( m_imageList )
330 {
331 delete m_imageList;
332 m_imageList = NULL;
333 }
334 }
335
336 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
337 // it would be logical if this removed the image list from book, under
338 // MSW it crashes instead - FIXME
339 }
340
341 void BookWidgetsPage::RecreateBook()
342 {
343 int flags = ms_defaultFlags;
344 switch ( m_radioOrient->GetSelection() )
345 {
346 default:
347 wxFAIL_MSG( _T("unknown orientation") );
348 // fall through
349
350 case Orient_Top:
351 flags |= wxBK_TOP;
352 break;
353
354 case Orient_Bottom:
355 flags |= wxBK_BOTTOM;
356 break;
357
358 case Orient_Left:
359 flags |= wxBK_LEFT;
360 break;
361
362 case Orient_Right:
363 flags |= wxBK_RIGHT;
364 break;
365 }
366
367 wxBookCtrlBase *oldBook = m_book;
368
369 m_book = CreateBook(flags);
370
371 CreateImageList();
372
373 if ( oldBook )
374 {
375 const int sel = oldBook->GetSelection();
376
377 const int count = oldBook->GetPageCount();
378
379 // recreate the pages
380 for ( int n = 0; n < count; n++ )
381 {
382 m_book->AddPage(CreateNewPage(),
383 oldBook->GetPageText(n),
384 false,
385 m_chkImages->GetValue() ?
386 GetIconIndex() : -1);
387 }
388
389 m_sizerBook->Detach( oldBook );
390 delete oldBook;
391
392 // restore selection
393 if ( sel != -1 )
394 {
395 m_book->SetSelection(sel);
396 }
397 }
398
399 m_sizerBook->Add(m_book, 1, wxGROW | wxALL, 5);
400 m_sizerBook->SetMinSize(150, 0);
401 m_sizerBook->Layout();
402 }
403
404 // ----------------------------------------------------------------------------
405 // helpers
406 // ----------------------------------------------------------------------------
407
408 int BookWidgetsPage::GetTextValue(wxTextCtrl *text) const
409 {
410 long pos;
411 if ( !text->GetValue().ToLong(&pos) )
412 pos = -1;
413
414 return (int)pos;
415 }
416
417 int BookWidgetsPage::GetIconIndex() const
418 {
419 if ( m_imageList )
420 {
421 int nImages = m_imageList->GetImageCount();
422 if ( nImages > 0 )
423 {
424 return m_book->GetPageCount() % nImages;
425 }
426 }
427
428 return -1;
429 }
430
431 wxWindow *BookWidgetsPage::CreateNewPage()
432 {
433 return new wxTextCtrl(m_book, wxID_ANY, _T("I'm a book page"));
434 }
435
436 // ----------------------------------------------------------------------------
437 // event handlers
438 // ----------------------------------------------------------------------------
439
440 void BookWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
441 {
442 Reset();
443
444 RecreateBook();
445 }
446
447 void BookWidgetsPage::OnButtonDeleteAll(wxCommandEvent& WXUNUSED(event))
448 {
449 m_book->DeleteAllPages();
450 }
451
452 void BookWidgetsPage::OnButtonSelectPage(wxCommandEvent& WXUNUSED(event))
453 {
454 int pos = GetTextValue(m_textSelect);
455 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
456
457 m_book->SetSelection(pos);
458 }
459
460 void BookWidgetsPage::OnButtonAddPage(wxCommandEvent& WXUNUSED(event))
461 {
462 m_book->AddPage(CreateNewPage(), _T("Added page"), false,
463 GetIconIndex());
464 }
465
466 void BookWidgetsPage::OnButtonInsertPage(wxCommandEvent& WXUNUSED(event))
467 {
468 int pos = GetTextValue(m_textInsert);
469 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
470
471 m_book->InsertPage(pos, CreateNewPage(), _T("Inserted page"), false,
472 GetIconIndex());
473 }
474
475 void BookWidgetsPage::OnButtonRemovePage(wxCommandEvent& WXUNUSED(event))
476 {
477 int pos = GetTextValue(m_textRemove);
478 wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
479
480 m_book->DeletePage(pos);
481 }
482
483 void BookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
484 {
485 event.Enable( IsValidValue(GetTextValue(m_textSelect)) );
486 }
487
488 void BookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
489 {
490 event.Enable( IsValidValue(GetTextValue(m_textInsert)) );
491 }
492
493 void BookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
494 {
495 event.Enable( IsValidValue(GetTextValue(m_textRemove)) );
496 }
497
498 void BookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
499 {
500 event.Enable( !m_chkImages->GetValue() ||
501 m_radioOrient->GetSelection() != wxBK_TOP );
502 }
503
504 void BookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent& event)
505 {
506 event.SetText( wxString::Format(_T("%d"), m_book->GetPageCount()) );
507 }
508
509 void BookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent& event)
510 {
511 event.SetText( wxString::Format(_T("%d"), m_book->GetSelection()) );
512 }
513
514 void BookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
515 {
516 RecreateBook();
517 }
518
519 #ifdef ABX
520 void BookWidgetsPage::OnPageChanging(wxBookEvent& event)
521 {
522 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
523 event.GetOldSelection(),
524 event.GetSelection(),
525 m_notebook->GetSelection());
526
527 event.Skip();
528 }
529
530 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent& event)
531 {
532 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
533 event.GetOldSelection(),
534 event.GetSelection(),
535 m_notebook->GetSelection());
536
537 event.Skip();
538 }
539
540 #endif
541
542 #if wxUSE_NOTEBOOK
543
544 #include "icons/notebook.xpm"
545 #include "wx/notebook.h"
546
547 // ----------------------------------------------------------------------------
548 // NotebookWidgetsPage
549 // ----------------------------------------------------------------------------
550
551 class NotebookWidgetsPage : public BookWidgetsPage
552 {
553 public:
554 NotebookWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist)
555 : BookWidgetsPage(book)
556 {
557 imaglist->Add(wxBitmap(notebook_xpm));
558 RecreateBook();
559 }
560 virtual ~NotebookWidgetsPage() {}
561
562 protected:
563
564 // (re)create book
565 virtual wxBookCtrlBase *CreateBook(long flags)
566 {
567 return new wxNotebook(this, BookPage_Book,
568 wxDefaultPosition, wxDefaultSize,
569 flags);
570
571 }
572
573 private:
574 DECLARE_EVENT_TABLE()
575 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage)
576 };
577
578 // ----------------------------------------------------------------------------
579 // event table
580 // ----------------------------------------------------------------------------
581
582 BEGIN_EVENT_TABLE(NotebookWidgetsPage, BookWidgetsPage)
583 END_EVENT_TABLE()
584
585 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage, _T("Notebook"),
586 (int)wxPlatform(GENERIC_CTRLS).If(wxMSW,NATIVE_CTRLS)
587 .ElseIf(wxGTK,NATIVE_CTRLS)
588 | BOOK_CTRLS
589 );
590
591 #endif // wxUSE_NOTEBOOK
592
593 #if wxUSE_LISTBOOK
594
595 #include "icons/listbook.xpm"
596 #include "wx/listbook.h"
597
598 // ----------------------------------------------------------------------------
599 // ListbookWidgetsPage
600 // ----------------------------------------------------------------------------
601
602 class ListbookWidgetsPage : public BookWidgetsPage
603 {
604 public:
605 ListbookWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist)
606 : BookWidgetsPage(book)
607 {
608 imaglist->Add(wxBitmap(listbook_xpm));
609 RecreateBook();
610 }
611 virtual ~ListbookWidgetsPage() {}
612
613 protected:
614
615 // (re)create book
616 virtual wxBookCtrlBase *CreateBook(long flags)
617 {
618 return new wxListbook(this, BookPage_Book,
619 wxDefaultPosition, wxDefaultSize,
620 flags);
621
622 }
623
624 private:
625 DECLARE_EVENT_TABLE()
626 DECLARE_WIDGETS_PAGE(ListbookWidgetsPage)
627 };
628
629 // ----------------------------------------------------------------------------
630 // event table
631 // ----------------------------------------------------------------------------
632
633 BEGIN_EVENT_TABLE(ListbookWidgetsPage, BookWidgetsPage)
634 END_EVENT_TABLE()
635
636 IMPLEMENT_WIDGETS_PAGE(ListbookWidgetsPage, _T("Listbook"),
637 GENERIC_CTRLS | BOOK_CTRLS
638 );
639
640 #endif // wxUSE_LISTBOOK
641
642 #if wxUSE_CHOICEBOOK
643
644 #include "icons/choicebk.xpm"
645 #include "wx/choicebk.h"
646
647 // ----------------------------------------------------------------------------
648 // ChoicebookWidgetsPage
649 // ----------------------------------------------------------------------------
650
651 class ChoicebookWidgetsPage : public BookWidgetsPage
652 {
653 public:
654 ChoicebookWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist)
655 : BookWidgetsPage(book)
656 {
657 imaglist->Add(wxBitmap(choicebk_xpm));
658 RecreateBook();
659 }
660 virtual ~ChoicebookWidgetsPage() {}
661
662 protected:
663
664 // (re)create book
665 virtual wxBookCtrlBase *CreateBook(long flags)
666 {
667 return new wxChoicebook(this, BookPage_Book,
668 wxDefaultPosition, wxDefaultSize,
669 flags);
670
671 }
672
673 private:
674 DECLARE_EVENT_TABLE()
675 DECLARE_WIDGETS_PAGE(ChoicebookWidgetsPage)
676 };
677
678 // ----------------------------------------------------------------------------
679 // event table
680 // ----------------------------------------------------------------------------
681
682 BEGIN_EVENT_TABLE(ChoicebookWidgetsPage, BookWidgetsPage)
683 END_EVENT_TABLE()
684
685 IMPLEMENT_WIDGETS_PAGE(ChoicebookWidgetsPage, _T("Choicebook"),
686 GENERIC_CTRLS | BOOK_CTRLS
687 );
688
689 #endif // wxUSE_CHOICEBOOK
690
691 #endif // wxUSE_BOOKCTRL