1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing book controls
5 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
7 // Copyright: (c) 2001 Vadim Zeitlin, 2006 Wlodzimierz Skiba
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
28 // for all others, include the necessary headers
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/combobox.h"
36 #include "wx/radiobox.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
40 #include "wx/dynarray.h"
44 #include "wx/bookctrl.h"
45 #include "wx/artprov.h"
46 #include "wx/imaglist.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 BookPage_Reset
= wxID_HIGHEST
,
66 BookPage_NumPagesText
,
67 BookPage_CurSelectText
,
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 class BookWidgetsPage
: public WidgetsPage
88 BookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
, const char *const icon
[]);
89 virtual ~BookWidgetsPage();
91 virtual wxControl
*GetWidget() const { return m_book
; }
92 virtual void RecreateWidget() { RecreateBook(); }
94 // lazy creation of the content
95 virtual void CreateContent();
99 void OnButtonReset(wxCommandEvent
& event
);
100 void OnButtonDeleteAll(wxCommandEvent
& event
);
101 void OnButtonSelectPage(wxCommandEvent
& event
);
102 void OnButtonAddPage(wxCommandEvent
& event
);
103 void OnButtonInsertPage(wxCommandEvent
& event
);
104 void OnButtonRemovePage(wxCommandEvent
& event
);
106 void OnCheckOrRadioBox(wxCommandEvent
& event
);
108 void OnUpdateUINumPagesText(wxUpdateUIEvent
& event
);
109 void OnUpdateUICurSelectText(wxUpdateUIEvent
& event
);
111 void OnUpdateUISelectButton(wxUpdateUIEvent
& event
);
112 void OnUpdateUIInsertButton(wxUpdateUIEvent
& event
);
113 void OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
);
115 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
117 // reset book parameters
122 virtual wxBookCtrlBase
*CreateBook(long flags
) = 0;
124 #if USE_ICONS_IN_BOOK
125 // create or destroy the image list
126 void CreateImageList();
127 #endif // USE_ICONS_IN_BOOK
130 wxWindow
*CreateNewPage();
132 // get the image index for the new page
133 int GetIconIndex() const;
135 // get the numeric value of text ctrl
136 int GetTextValue(wxTextCtrl
*text
) const;
138 // is the value in range?
139 bool IsValidValue(int val
) const
140 { return (val
>= 0) && (val
< (int) m_book
->GetPageCount()); }
145 // the check/radio boxes for styles
146 wxCheckBox
*m_chkImages
;
147 wxRadioBox
*m_radioOrient
;
149 // the text controls containing input for various commands
150 wxTextCtrl
*m_textInsert
,
154 // the book itself and the sizer it is in
155 wxBookCtrlBase
*m_book
;
156 wxSizer
*m_sizerBook
;
158 #if USE_ICONS_IN_BOOK
159 // the image list for our book
160 wxImageList
*m_imageList
;
161 #endif // USE_ICONS_IN_BOOK
164 DECLARE_EVENT_TABLE()
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 BEGIN_EVENT_TABLE(BookWidgetsPage
, WidgetsPage
)
172 EVT_BUTTON(BookPage_Reset
, BookWidgetsPage::OnButtonReset
)
173 EVT_BUTTON(BookPage_SelectPage
, BookWidgetsPage::OnButtonSelectPage
)
174 EVT_BUTTON(BookPage_AddPage
, BookWidgetsPage::OnButtonAddPage
)
175 EVT_BUTTON(BookPage_InsertPage
, BookWidgetsPage::OnButtonInsertPage
)
176 EVT_BUTTON(BookPage_RemovePage
, BookWidgetsPage::OnButtonRemovePage
)
177 EVT_BUTTON(BookPage_DeleteAll
, BookWidgetsPage::OnButtonDeleteAll
)
179 EVT_UPDATE_UI(BookPage_NumPagesText
, BookWidgetsPage::OnUpdateUINumPagesText
)
180 EVT_UPDATE_UI(BookPage_CurSelectText
, BookWidgetsPage::OnUpdateUICurSelectText
)
182 EVT_UPDATE_UI(BookPage_SelectPage
, BookWidgetsPage::OnUpdateUISelectButton
)
183 EVT_UPDATE_UI(BookPage_InsertPage
, BookWidgetsPage::OnUpdateUIInsertButton
)
184 EVT_UPDATE_UI(BookPage_RemovePage
, BookWidgetsPage::OnUpdateUIRemoveButton
)
186 EVT_CHECKBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
187 EVT_RADIOBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
190 // ============================================================================
192 // ============================================================================
194 BookWidgetsPage::BookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
, const char *const icon
[])
195 :WidgetsPage(book
, imaglist
, icon
)
199 #if USE_ICONS_IN_BOOK
201 #endif // USE_ICONS_IN_BOOK
204 m_radioOrient
= NULL
;
205 m_sizerBook
= (wxSizer
*)NULL
;
208 void BookWidgetsPage::CreateContent()
210 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
213 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
215 // must be in sync with Orient enum
216 wxArrayString orientations
;
217 orientations
.Add(wxT("&top"));
218 orientations
.Add(wxT("&bottom"));
219 orientations
.Add(wxT("&left"));
220 orientations
.Add(wxT("&right"));
222 wxASSERT_MSG( orientations
.GetCount() == Orient_Max
,
223 wxT("forgot to update something") );
225 m_chkImages
= new wxCheckBox(this, wxID_ANY
, wxT("Show &images"));
226 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, wxT("&Tab orientation"),
227 wxDefaultPosition
, wxDefaultSize
,
228 orientations
, 1, wxRA_SPECIFY_COLS
);
230 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
232 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
233 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
234 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
236 wxButton
*btn
= new wxButton(this, BookPage_Reset
, wxT("&Reset"));
237 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
240 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Contents"));
241 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
244 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(wxT("Number of pages: "),
245 BookPage_NumPagesText
,
247 text
->SetEditable(false);
248 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
250 sizerRow
= CreateSizerWithTextAndLabel(wxT("Current selection: "),
251 BookPage_CurSelectText
,
253 text
->SetEditable(false);
254 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
256 sizerRow
= CreateSizerWithTextAndButton(BookPage_SelectPage
,
260 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
262 btn
= new wxButton(this, BookPage_AddPage
, wxT("&Add page"));
263 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
265 sizerRow
= CreateSizerWithTextAndButton(BookPage_InsertPage
,
266 wxT("&Insert page at"),
269 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
271 sizerRow
= CreateSizerWithTextAndButton(BookPage_RemovePage
,
275 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
277 btn
= new wxButton(this, BookPage_DeleteAll
, wxT("&Delete All"));
278 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
281 m_sizerBook
= new wxBoxSizer(wxHORIZONTAL
);
283 // the 3 panes compose the window
284 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
285 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
286 sizerTop
->Add(m_sizerBook
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
290 // final initializations
292 #if USE_ICONS_IN_BOOK
294 #endif // USE_ICONS_IN_BOOK
299 BookWidgetsPage::~BookWidgetsPage()
301 #if USE_ICONS_IN_BOOK
303 #endif // USE_ICONS_IN_BOOK
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
310 void BookWidgetsPage::Reset()
312 m_chkImages
->SetValue(true);
313 m_radioOrient
->SetSelection(Orient_Top
);
316 #if USE_ICONS_IN_BOOK
317 void BookWidgetsPage::CreateImageList()
319 if ( m_chkImages
->GetValue() )
323 // create a dummy image list with a few icons
324 m_imageList
= new wxImageList(32, 32);
326 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
327 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
328 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
329 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
333 m_book
->SetImageList(m_imageList
);
337 wxDELETE(m_imageList
);
340 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
341 // it would be logical if this removed the image list from book, under
342 // MSW it crashes instead - FIXME
344 #endif // USE_ICONS_IN_BOOK
346 void BookWidgetsPage::RecreateBook()
348 // do not recreate anything in case page content was not prepared yet
352 int flags
= ms_defaultFlags
;
354 switch ( m_radioOrient
->GetSelection() )
357 wxFAIL_MSG( wxT("unknown orientation") );
365 flags
|= wxBK_BOTTOM
;
377 wxBookCtrlBase
*oldBook
= m_book
;
379 m_book
= CreateBook(flags
);
381 #if USE_ICONS_IN_BOOK
383 #endif // USE_ICONS_IN_BOOK
387 const int sel
= oldBook
->GetSelection();
389 const int count
= oldBook
->GetPageCount();
391 // recreate the pages
392 for ( int n
= 0; n
< count
; n
++ )
394 m_book
->AddPage(CreateNewPage(),
395 oldBook
->GetPageText(n
),
397 m_chkImages
->GetValue() ?
398 GetIconIndex() : -1);
401 m_sizerBook
->Detach( oldBook
);
407 m_book
->SetSelection(sel
);
411 m_sizerBook
->Add(m_book
, 1, wxGROW
| wxALL
, 5);
412 m_sizerBook
->SetMinSize(150, 0);
413 m_sizerBook
->Layout();
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
420 int BookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
424 if ( !text
|| !text
->GetValue().ToLong(&pos
) )
430 int BookWidgetsPage::GetIconIndex() const
432 #if USE_ICONS_IN_BOOK
435 int nImages
= m_imageList
->GetImageCount();
438 return m_book
->GetPageCount() % nImages
;
441 #endif // USE_ICONS_IN_BOOK
446 wxWindow
*BookWidgetsPage::CreateNewPage()
448 return new wxTextCtrl(m_book
, wxID_ANY
, wxT("I'm a book page"));
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 void BookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
462 void BookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
464 m_book
->DeleteAllPages();
467 void BookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
469 int pos
= GetTextValue(m_textSelect
);
470 wxCHECK_RET( IsValidValue(pos
), wxT("button should be disabled") );
472 m_book
->SetSelection(pos
);
475 void BookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
477 m_book
->AddPage(CreateNewPage(), wxT("Added page"), false,
481 void BookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
483 int pos
= GetTextValue(m_textInsert
);
484 wxCHECK_RET( IsValidValue(pos
), wxT("button should be disabled") );
486 m_book
->InsertPage(pos
, CreateNewPage(), wxT("Inserted page"), false,
490 void BookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
492 int pos
= GetTextValue(m_textRemove
);
493 wxCHECK_RET( IsValidValue(pos
), wxT("button should be disabled") );
495 m_book
->DeletePage(pos
);
498 void BookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
500 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
503 void BookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
505 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
508 void BookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
510 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
513 void BookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
515 if(m_chkImages
&& m_radioOrient
)
516 event
.Enable( !m_chkImages
->GetValue() ||
517 m_radioOrient
->GetSelection() != wxBK_TOP
);
520 void BookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
523 event
.SetText( wxString::Format(wxT("%u"), unsigned(m_book
->GetPageCount())) );
526 void BookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
529 event
.SetText( wxString::Format(wxT("%d"), m_book
->GetSelection()) );
532 void BookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
539 #include "icons/notebook.xpm"
540 #include "wx/notebook.h"
542 // ----------------------------------------------------------------------------
543 // NotebookWidgetsPage
544 // ----------------------------------------------------------------------------
546 class NotebookWidgetsPage
: public BookWidgetsPage
549 NotebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
550 : BookWidgetsPage(book
, imaglist
, notebook_xpm
)
554 virtual ~NotebookWidgetsPage() {}
559 void OnPageChanging(wxNotebookEvent
& event
);
560 void OnPageChanged(wxNotebookEvent
& event
);
563 virtual wxBookCtrlBase
*CreateBook(long flags
)
565 return new wxNotebook(this, BookPage_Book
,
566 wxDefaultPosition
, wxDefaultSize
,
571 DECLARE_EVENT_TABLE()
572 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
575 // ----------------------------------------------------------------------------
577 // ----------------------------------------------------------------------------
579 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, BookWidgetsPage
)
580 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, NotebookWidgetsPage::OnPageChanging
)
581 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, NotebookWidgetsPage::OnPageChanged
)
584 #if defined(__WXUNIVERSAL__)
585 #define FAMILY_CTRLS UNIVERSAL_CTRLS
586 #elif defined(__WXMOTIF__)
587 #define FAMILY_CTRLS GENERIC_CTRLS
589 #define FAMILY_CTRLS NATIVE_CTRLS
592 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, wxT("Notebook"),
593 FAMILY_CTRLS
| BOOK_CTRLS
596 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
598 wxLogMessage(wxT("Notebook page changing from %d to %d (currently %d)."),
599 event
.GetOldSelection(),
600 event
.GetSelection(),
601 m_book
->GetSelection());
606 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
608 wxLogMessage(wxT("Notebook page changed from %d to %d (currently %d)."),
609 event
.GetOldSelection(),
610 event
.GetSelection(),
611 m_book
->GetSelection());
616 #endif // wxUSE_NOTEBOOK
620 #include "icons/listbook.xpm"
621 #include "wx/listbook.h"
623 // ----------------------------------------------------------------------------
624 // ListbookWidgetsPage
625 // ----------------------------------------------------------------------------
627 class ListbookWidgetsPage
: public BookWidgetsPage
630 ListbookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
631 : BookWidgetsPage(book
, imaglist
, listbook_xpm
)
635 virtual ~ListbookWidgetsPage() {}
640 void OnPageChanging(wxListbookEvent
& event
);
641 void OnPageChanged(wxListbookEvent
& event
);
644 virtual wxBookCtrlBase
*CreateBook(long flags
)
646 return new wxListbook(this, BookPage_Book
,
647 wxDefaultPosition
, wxDefaultSize
,
652 DECLARE_EVENT_TABLE()
653 DECLARE_WIDGETS_PAGE(ListbookWidgetsPage
)
656 // ----------------------------------------------------------------------------
658 // ----------------------------------------------------------------------------
660 BEGIN_EVENT_TABLE(ListbookWidgetsPage
, BookWidgetsPage
)
661 EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY
, ListbookWidgetsPage::OnPageChanging
)
662 EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY
, ListbookWidgetsPage::OnPageChanged
)
665 IMPLEMENT_WIDGETS_PAGE(ListbookWidgetsPage
, wxT("Listbook"),
666 GENERIC_CTRLS
| BOOK_CTRLS
669 void ListbookWidgetsPage::OnPageChanging(wxListbookEvent
& event
)
671 wxLogMessage(wxT("Listbook page changing from %d to %d (currently %d)."),
672 event
.GetOldSelection(),
673 event
.GetSelection(),
674 m_book
->GetSelection());
679 void ListbookWidgetsPage::OnPageChanged(wxListbookEvent
& event
)
681 wxLogMessage(wxT("Listbook page changed from %d to %d (currently %d)."),
682 event
.GetOldSelection(),
683 event
.GetSelection(),
684 m_book
->GetSelection());
689 #endif // wxUSE_LISTBOOK
693 #include "icons/choicebk.xpm"
694 #include "wx/choicebk.h"
696 // ----------------------------------------------------------------------------
697 // ChoicebookWidgetsPage
698 // ----------------------------------------------------------------------------
700 class ChoicebookWidgetsPage
: public BookWidgetsPage
703 ChoicebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
704 : BookWidgetsPage(book
, imaglist
, choicebk_xpm
)
708 virtual ~ChoicebookWidgetsPage() {}
713 void OnPageChanging(wxChoicebookEvent
& event
);
714 void OnPageChanged(wxChoicebookEvent
& event
);
717 virtual wxBookCtrlBase
*CreateBook(long flags
)
719 return new wxChoicebook(this, BookPage_Book
,
720 wxDefaultPosition
, wxDefaultSize
,
725 DECLARE_EVENT_TABLE()
726 DECLARE_WIDGETS_PAGE(ChoicebookWidgetsPage
)
729 // ----------------------------------------------------------------------------
731 // ----------------------------------------------------------------------------
733 BEGIN_EVENT_TABLE(ChoicebookWidgetsPage
, BookWidgetsPage
)
734 EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanging
)
735 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanged
)
738 IMPLEMENT_WIDGETS_PAGE(ChoicebookWidgetsPage
, wxT("Choicebook"),
739 GENERIC_CTRLS
| BOOK_CTRLS
742 void ChoicebookWidgetsPage::OnPageChanging(wxChoicebookEvent
& event
)
744 wxLogMessage(wxT("Choicebook page changing from %d to %d (currently %d)."),
745 event
.GetOldSelection(),
746 event
.GetSelection(),
747 m_book
->GetSelection());
752 void ChoicebookWidgetsPage::OnPageChanged(wxChoicebookEvent
& event
)
754 wxLogMessage(wxT("Choicebook page changed from %d to %d (currently %d)."),
755 event
.GetOldSelection(),
756 event
.GetSelection(),
757 m_book
->GetSelection());
762 #endif // wxUSE_CHOICEBOOK
764 #endif // wxUSE_BOOKCTRL