1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing book controls
5 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
8 // Copyright: (c) 2001 Vadim Zeitlin, 2006 Wlodzimierz Skiba
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 // for all others, include the necessary headers
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"
41 #include "wx/dynarray.h"
45 #include "wx/bookctrl.h"
46 #include "wx/artprov.h"
47 #include "wx/imaglist.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 BookPage_Reset
= wxID_HIGHEST
,
67 BookPage_NumPagesText
,
68 BookPage_CurSelectText
,
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 class BookWidgetsPage
: public WidgetsPage
89 BookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
, char* icon
[]);
90 virtual ~BookWidgetsPage();
92 virtual wxControl
*GetWidget() const { return m_book
; }
93 virtual void RecreateWidget() { RecreateBook(); }
97 void OnButtonReset(wxCommandEvent
& event
);
98 void OnButtonDeleteAll(wxCommandEvent
& event
);
99 void OnButtonSelectPage(wxCommandEvent
& event
);
100 void OnButtonAddPage(wxCommandEvent
& event
);
101 void OnButtonInsertPage(wxCommandEvent
& event
);
102 void OnButtonRemovePage(wxCommandEvent
& event
);
104 void OnCheckOrRadioBox(wxCommandEvent
& event
);
106 void OnUpdateUINumPagesText(wxUpdateUIEvent
& event
);
107 void OnUpdateUICurSelectText(wxUpdateUIEvent
& event
);
109 void OnUpdateUISelectButton(wxUpdateUIEvent
& event
);
110 void OnUpdateUIInsertButton(wxUpdateUIEvent
& event
);
111 void OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
);
113 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
115 // reset book parameters
120 virtual wxBookCtrlBase
*CreateBook(long flags
) = 0;
122 #if USE_ICONS_IN_BOOK
123 // create or destroy the image list
124 void CreateImageList();
125 #endif // USE_ICONS_IN_BOOK
128 wxWindow
*CreateNewPage();
130 // get the image index for the new page
131 int GetIconIndex() const;
133 // get the numeric value of text ctrl
134 int GetTextValue(wxTextCtrl
*text
) const;
136 // is the value in range?
137 bool IsValidValue(int val
) const
138 { return (val
>= 0) && (val
< (int) m_book
->GetPageCount()); }
143 // the check/radio boxes for styles
144 wxCheckBox
*m_chkImages
;
145 wxRadioBox
*m_radioOrient
;
147 // the text controls containing input for various commands
148 wxTextCtrl
*m_textInsert
,
152 // the book itself and the sizer it is in
153 wxBookCtrlBase
*m_book
;
154 wxSizer
*m_sizerBook
;
156 #if USE_ICONS_IN_BOOK
157 // the image list for our book
158 wxImageList
*m_imageList
;
159 #endif // USE_ICONS_IN_BOOK
162 DECLARE_EVENT_TABLE()
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
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
)
177 EVT_UPDATE_UI(BookPage_NumPagesText
, BookWidgetsPage::OnUpdateUINumPagesText
)
178 EVT_UPDATE_UI(BookPage_CurSelectText
, BookWidgetsPage::OnUpdateUICurSelectText
)
180 EVT_UPDATE_UI(BookPage_SelectPage
, BookWidgetsPage::OnUpdateUISelectButton
)
181 EVT_UPDATE_UI(BookPage_InsertPage
, BookWidgetsPage::OnUpdateUIInsertButton
)
182 EVT_UPDATE_UI(BookPage_RemovePage
, BookWidgetsPage::OnUpdateUIRemoveButton
)
184 EVT_CHECKBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
185 EVT_RADIOBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
188 // ============================================================================
190 // ============================================================================
192 BookWidgetsPage::BookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
, char* icon
[])
193 :WidgetsPage(book
, imaglist
, icon
)
197 #if USE_ICONS_IN_BOOK
199 #endif // USE_ICONS_IN_BOOK
202 m_sizerBook
= (wxSizer
*)NULL
;
204 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
207 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
209 // must be in sync with Orient enum
210 wxArrayString orientations
;
211 orientations
.Add(_T("&top"));
212 orientations
.Add(_T("&bottom"));
213 orientations
.Add(_T("&left"));
214 orientations
.Add(_T("&right"));
216 wxASSERT_MSG( orientations
.GetCount() == Orient_Max
,
217 _T("forgot to update something") );
219 m_chkImages
= new wxCheckBox(this, wxID_ANY
, _T("Show &images"));
220 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, _T("&Tab orientation"),
221 wxDefaultPosition
, wxDefaultSize
,
222 orientations
, 1, wxRA_SPECIFY_COLS
);
224 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
226 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
227 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
228 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
230 wxButton
*btn
= new wxButton(this, BookPage_Reset
, _T("&Reset"));
231 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
234 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Contents"));
235 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
238 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
239 BookPage_NumPagesText
,
241 text
->SetEditable(false);
242 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
244 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
245 BookPage_CurSelectText
,
247 text
->SetEditable(false);
248 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
250 sizerRow
= CreateSizerWithTextAndButton(BookPage_SelectPage
,
254 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
256 btn
= new wxButton(this, BookPage_AddPage
, _T("&Add page"));
257 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
259 sizerRow
= CreateSizerWithTextAndButton(BookPage_InsertPage
,
260 _T("&Insert page at"),
263 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
265 sizerRow
= CreateSizerWithTextAndButton(BookPage_RemovePage
,
269 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
271 btn
= new wxButton(this, BookPage_DeleteAll
, _T("&Delete All"));
272 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
275 m_sizerBook
= new wxBoxSizer(wxHORIZONTAL
);
277 // the 3 panes compose the window
278 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
279 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
280 sizerTop
->Add(m_sizerBook
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
282 // final initializations
284 #if USE_ICONS_IN_BOOK
286 #endif // USE_ICONS_IN_BOOK
293 BookWidgetsPage::~BookWidgetsPage()
295 #if USE_ICONS_IN_BOOK
297 #endif // USE_ICONS_IN_BOOK
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 void BookWidgetsPage::Reset()
306 m_chkImages
->SetValue(true);
307 m_radioOrient
->SetSelection(Orient_Top
);
310 #if USE_ICONS_IN_BOOK
311 void BookWidgetsPage::CreateImageList()
313 if ( m_chkImages
->GetValue() )
317 // create a dummy image list with a few icons
318 m_imageList
= new wxImageList(32, 32);
320 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
321 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
322 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
323 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
327 m_book
->SetImageList(m_imageList
);
338 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
339 // it would be logical if this removed the image list from book, under
340 // MSW it crashes instead - FIXME
342 #endif // USE_ICONS_IN_BOOK
344 void BookWidgetsPage::RecreateBook()
346 int flags
= ms_defaultFlags
;
347 switch ( m_radioOrient
->GetSelection() )
350 wxFAIL_MSG( _T("unknown orientation") );
358 flags
|= wxBK_BOTTOM
;
370 wxBookCtrlBase
*oldBook
= m_book
;
372 m_book
= CreateBook(flags
);
374 #if USE_ICONS_IN_BOOK
376 #endif // USE_ICONS_IN_BOOK
380 const int sel
= oldBook
->GetSelection();
382 const int count
= oldBook
->GetPageCount();
384 // recreate the pages
385 for ( int n
= 0; n
< count
; n
++ )
387 m_book
->AddPage(CreateNewPage(),
388 oldBook
->GetPageText(n
),
390 m_chkImages
->GetValue() ?
391 GetIconIndex() : -1);
394 m_sizerBook
->Detach( oldBook
);
400 m_book
->SetSelection(sel
);
404 m_sizerBook
->Add(m_book
, 1, wxGROW
| wxALL
, 5);
405 m_sizerBook
->SetMinSize(150, 0);
406 m_sizerBook
->Layout();
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 int BookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
416 if ( !text
->GetValue().ToLong(&pos
) )
422 int BookWidgetsPage::GetIconIndex() const
424 #if USE_ICONS_IN_BOOK
427 int nImages
= m_imageList
->GetImageCount();
430 return m_book
->GetPageCount() % nImages
;
433 #endif // USE_ICONS_IN_BOOK
438 wxWindow
*BookWidgetsPage::CreateNewPage()
440 return new wxTextCtrl(m_book
, wxID_ANY
, _T("I'm a book page"));
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
447 void BookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
454 void BookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
456 m_book
->DeleteAllPages();
459 void BookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
461 int pos
= GetTextValue(m_textSelect
);
462 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
464 m_book
->SetSelection(pos
);
467 void BookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
469 m_book
->AddPage(CreateNewPage(), _T("Added page"), false,
473 void BookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
475 int pos
= GetTextValue(m_textInsert
);
476 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
478 m_book
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), false,
482 void BookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
484 int pos
= GetTextValue(m_textRemove
);
485 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
487 m_book
->DeletePage(pos
);
490 void BookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
492 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
495 void BookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
497 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
500 void BookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
502 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
505 void BookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
507 event
.Enable( !m_chkImages
->GetValue() ||
508 m_radioOrient
->GetSelection() != wxBK_TOP
);
511 void BookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
513 event
.SetText( wxString::Format(_T("%d"), m_book
->GetPageCount()) );
516 void BookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
518 event
.SetText( wxString::Format(_T("%d"), m_book
->GetSelection()) );
521 void BookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
528 #include "icons/notebook.xpm"
529 #include "wx/notebook.h"
531 // ----------------------------------------------------------------------------
532 // NotebookWidgetsPage
533 // ----------------------------------------------------------------------------
535 class NotebookWidgetsPage
: public BookWidgetsPage
538 NotebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
539 : BookWidgetsPage(book
, imaglist
, notebook_xpm
)
543 virtual ~NotebookWidgetsPage() {}
548 void OnPageChanging(wxNotebookEvent
& event
);
549 void OnPageChanged(wxNotebookEvent
& event
);
552 virtual wxBookCtrlBase
*CreateBook(long flags
)
554 return new wxNotebook(this, BookPage_Book
,
555 wxDefaultPosition
, wxDefaultSize
,
561 DECLARE_EVENT_TABLE()
562 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, BookWidgetsPage
)
570 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, NotebookWidgetsPage::OnPageChanging
)
571 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, NotebookWidgetsPage::OnPageChanged
)
574 #if defined(__WXUNIVERSAL__)
575 #define FAMILY_CTRLS UNIVERSAL_CTRLS
576 #elif defined(__WXMOTIF__)
577 #define FAMILY_CTRLS GENERIC_CTRLS
579 #define FAMILY_CTRLS NATIVE_CTRLS
582 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook"),
583 FAMILY_CTRLS
| BOOK_CTRLS
586 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
588 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
589 event
.GetOldSelection(),
590 event
.GetSelection(),
591 m_book
->GetSelection());
596 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
598 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
599 event
.GetOldSelection(),
600 event
.GetSelection(),
601 m_book
->GetSelection());
606 #endif // wxUSE_NOTEBOOK
610 #include "icons/listbook.xpm"
611 #include "wx/listbook.h"
613 // ----------------------------------------------------------------------------
614 // ListbookWidgetsPage
615 // ----------------------------------------------------------------------------
617 class ListbookWidgetsPage
: public BookWidgetsPage
620 ListbookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
621 : BookWidgetsPage(book
, imaglist
, listbook_xpm
)
625 virtual ~ListbookWidgetsPage() {}
630 void OnPageChanging(wxListbookEvent
& event
);
631 void OnPageChanged(wxListbookEvent
& event
);
634 virtual wxBookCtrlBase
*CreateBook(long flags
)
636 return new wxListbook(this, BookPage_Book
,
637 wxDefaultPosition
, wxDefaultSize
,
643 DECLARE_EVENT_TABLE()
644 DECLARE_WIDGETS_PAGE(ListbookWidgetsPage
)
647 // ----------------------------------------------------------------------------
649 // ----------------------------------------------------------------------------
651 BEGIN_EVENT_TABLE(ListbookWidgetsPage
, BookWidgetsPage
)
652 EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY
, ListbookWidgetsPage::OnPageChanging
)
653 EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY
, ListbookWidgetsPage::OnPageChanged
)
656 IMPLEMENT_WIDGETS_PAGE(ListbookWidgetsPage
, _T("Listbook"),
657 GENERIC_CTRLS
| BOOK_CTRLS
660 void ListbookWidgetsPage::OnPageChanging(wxListbookEvent
& event
)
662 wxLogMessage(_T("Listbook page changing from %d to %d (currently %d)."),
663 event
.GetOldSelection(),
664 event
.GetSelection(),
665 m_book
->GetSelection());
670 void ListbookWidgetsPage::OnPageChanged(wxListbookEvent
& event
)
672 wxLogMessage(_T("Listbook page changed from %d to %d (currently %d)."),
673 event
.GetOldSelection(),
674 event
.GetSelection(),
675 m_book
->GetSelection());
680 #endif // wxUSE_LISTBOOK
684 #include "icons/choicebk.xpm"
685 #include "wx/choicebk.h"
687 // ----------------------------------------------------------------------------
688 // ChoicebookWidgetsPage
689 // ----------------------------------------------------------------------------
691 class ChoicebookWidgetsPage
: public BookWidgetsPage
694 ChoicebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
695 : BookWidgetsPage(book
, imaglist
, choicebk_xpm
)
699 virtual ~ChoicebookWidgetsPage() {}
704 void OnPageChanging(wxChoicebookEvent
& event
);
705 void OnPageChanged(wxChoicebookEvent
& event
);
708 virtual wxBookCtrlBase
*CreateBook(long flags
)
710 return new wxChoicebook(this, BookPage_Book
,
711 wxDefaultPosition
, wxDefaultSize
,
717 DECLARE_EVENT_TABLE()
718 DECLARE_WIDGETS_PAGE(ChoicebookWidgetsPage
)
721 // ----------------------------------------------------------------------------
723 // ----------------------------------------------------------------------------
725 BEGIN_EVENT_TABLE(ChoicebookWidgetsPage
, BookWidgetsPage
)
726 EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanging
)
727 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanged
)
730 IMPLEMENT_WIDGETS_PAGE(ChoicebookWidgetsPage
, _T("Choicebook"),
731 GENERIC_CTRLS
| BOOK_CTRLS
734 void ChoicebookWidgetsPage::OnPageChanging(wxChoicebookEvent
& event
)
736 wxLogMessage(_T("Choicebook page changing from %d to %d (currently %d)."),
737 event
.GetOldSelection(),
738 event
.GetSelection(),
739 m_book
->GetSelection());
744 void ChoicebookWidgetsPage::OnPageChanged(wxChoicebookEvent
& event
)
746 wxLogMessage(_T("Choicebook page changed from %d to %d (currently %d)."),
747 event
.GetOldSelection(),
748 event
.GetSelection(),
749 m_book
->GetSelection());
754 #endif // wxUSE_CHOICEBOOK
756 #endif // wxUSE_BOOKCTRL