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(); }
95 // lazy creation of the content
96 virtual void CreateContent();
100 void OnButtonReset(wxCommandEvent
& event
);
101 void OnButtonDeleteAll(wxCommandEvent
& event
);
102 void OnButtonSelectPage(wxCommandEvent
& event
);
103 void OnButtonAddPage(wxCommandEvent
& event
);
104 void OnButtonInsertPage(wxCommandEvent
& event
);
105 void OnButtonRemovePage(wxCommandEvent
& event
);
107 void OnCheckOrRadioBox(wxCommandEvent
& event
);
109 void OnUpdateUINumPagesText(wxUpdateUIEvent
& event
);
110 void OnUpdateUICurSelectText(wxUpdateUIEvent
& event
);
112 void OnUpdateUISelectButton(wxUpdateUIEvent
& event
);
113 void OnUpdateUIInsertButton(wxUpdateUIEvent
& event
);
114 void OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
);
116 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
118 // reset book parameters
123 virtual wxBookCtrlBase
*CreateBook(long flags
) = 0;
125 #if USE_ICONS_IN_BOOK
126 // create or destroy the image list
127 void CreateImageList();
128 #endif // USE_ICONS_IN_BOOK
131 wxWindow
*CreateNewPage();
133 // get the image index for the new page
134 int GetIconIndex() const;
136 // get the numeric value of text ctrl
137 int GetTextValue(wxTextCtrl
*text
) const;
139 // is the value in range?
140 bool IsValidValue(int val
) const
141 { return (val
>= 0) && (val
< (int) m_book
->GetPageCount()); }
146 // the check/radio boxes for styles
147 wxCheckBox
*m_chkImages
;
148 wxRadioBox
*m_radioOrient
;
150 // the text controls containing input for various commands
151 wxTextCtrl
*m_textInsert
,
155 // the book itself and the sizer it is in
156 wxBookCtrlBase
*m_book
;
157 wxSizer
*m_sizerBook
;
159 #if USE_ICONS_IN_BOOK
160 // the image list for our book
161 wxImageList
*m_imageList
;
162 #endif // USE_ICONS_IN_BOOK
165 DECLARE_EVENT_TABLE()
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 BEGIN_EVENT_TABLE(BookWidgetsPage
, WidgetsPage
)
173 EVT_BUTTON(BookPage_Reset
, BookWidgetsPage::OnButtonReset
)
174 EVT_BUTTON(BookPage_SelectPage
, BookWidgetsPage::OnButtonSelectPage
)
175 EVT_BUTTON(BookPage_AddPage
, BookWidgetsPage::OnButtonAddPage
)
176 EVT_BUTTON(BookPage_InsertPage
, BookWidgetsPage::OnButtonInsertPage
)
177 EVT_BUTTON(BookPage_RemovePage
, BookWidgetsPage::OnButtonRemovePage
)
178 EVT_BUTTON(BookPage_DeleteAll
, BookWidgetsPage::OnButtonDeleteAll
)
180 EVT_UPDATE_UI(BookPage_NumPagesText
, BookWidgetsPage::OnUpdateUINumPagesText
)
181 EVT_UPDATE_UI(BookPage_CurSelectText
, BookWidgetsPage::OnUpdateUICurSelectText
)
183 EVT_UPDATE_UI(BookPage_SelectPage
, BookWidgetsPage::OnUpdateUISelectButton
)
184 EVT_UPDATE_UI(BookPage_InsertPage
, BookWidgetsPage::OnUpdateUIInsertButton
)
185 EVT_UPDATE_UI(BookPage_RemovePage
, BookWidgetsPage::OnUpdateUIRemoveButton
)
187 EVT_CHECKBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
188 EVT_RADIOBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
191 // ============================================================================
193 // ============================================================================
195 BookWidgetsPage::BookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
, char* icon
[])
196 :WidgetsPage(book
, imaglist
, icon
)
200 #if USE_ICONS_IN_BOOK
202 #endif // USE_ICONS_IN_BOOK
205 m_radioOrient
= NULL
;
206 m_sizerBook
= (wxSizer
*)NULL
;
209 void BookWidgetsPage::CreateContent()
211 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
214 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
216 // must be in sync with Orient enum
217 wxArrayString orientations
;
218 orientations
.Add(_T("&top"));
219 orientations
.Add(_T("&bottom"));
220 orientations
.Add(_T("&left"));
221 orientations
.Add(_T("&right"));
223 wxASSERT_MSG( orientations
.GetCount() == Orient_Max
,
224 _T("forgot to update something") );
226 m_chkImages
= new wxCheckBox(this, wxID_ANY
, _T("Show &images"));
227 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, _T("&Tab orientation"),
228 wxDefaultPosition
, wxDefaultSize
,
229 orientations
, 1, wxRA_SPECIFY_COLS
);
231 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
233 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
234 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
235 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
237 wxButton
*btn
= new wxButton(this, BookPage_Reset
, _T("&Reset"));
238 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
241 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Contents"));
242 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
245 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
246 BookPage_NumPagesText
,
248 text
->SetEditable(false);
249 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
251 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
252 BookPage_CurSelectText
,
254 text
->SetEditable(false);
255 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
257 sizerRow
= CreateSizerWithTextAndButton(BookPage_SelectPage
,
261 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
263 btn
= new wxButton(this, BookPage_AddPage
, _T("&Add page"));
264 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
266 sizerRow
= CreateSizerWithTextAndButton(BookPage_InsertPage
,
267 _T("&Insert page at"),
270 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
272 sizerRow
= CreateSizerWithTextAndButton(BookPage_RemovePage
,
276 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
278 btn
= new wxButton(this, BookPage_DeleteAll
, _T("&Delete All"));
279 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
282 m_sizerBook
= new wxBoxSizer(wxHORIZONTAL
);
284 // the 3 panes compose the window
285 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
286 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
287 sizerTop
->Add(m_sizerBook
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
291 // final initializations
293 #if USE_ICONS_IN_BOOK
295 #endif // USE_ICONS_IN_BOOK
302 BookWidgetsPage::~BookWidgetsPage()
304 #if USE_ICONS_IN_BOOK
306 #endif // USE_ICONS_IN_BOOK
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 void BookWidgetsPage::Reset()
315 m_chkImages
->SetValue(true);
316 m_radioOrient
->SetSelection(Orient_Top
);
319 #if USE_ICONS_IN_BOOK
320 void BookWidgetsPage::CreateImageList()
322 if ( m_chkImages
->GetValue() )
326 // create a dummy image list with a few icons
327 m_imageList
= new wxImageList(32, 32);
329 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
330 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
331 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
332 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
336 m_book
->SetImageList(m_imageList
);
347 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
348 // it would be logical if this removed the image list from book, under
349 // MSW it crashes instead - FIXME
351 #endif // USE_ICONS_IN_BOOK
353 void BookWidgetsPage::RecreateBook()
355 // do not recreate anything in case page content was not prepared yet
359 int flags
= ms_defaultFlags
;
361 switch ( m_radioOrient
->GetSelection() )
364 wxFAIL_MSG( _T("unknown orientation") );
372 flags
|= wxBK_BOTTOM
;
384 wxBookCtrlBase
*oldBook
= m_book
;
386 m_book
= CreateBook(flags
);
388 #if USE_ICONS_IN_BOOK
390 #endif // USE_ICONS_IN_BOOK
394 const int sel
= oldBook
->GetSelection();
396 const int count
= oldBook
->GetPageCount();
398 // recreate the pages
399 for ( int n
= 0; n
< count
; n
++ )
401 m_book
->AddPage(CreateNewPage(),
402 oldBook
->GetPageText(n
),
404 m_chkImages
->GetValue() ?
405 GetIconIndex() : -1);
408 m_sizerBook
->Detach( oldBook
);
414 m_book
->SetSelection(sel
);
418 m_sizerBook
->Add(m_book
, 1, wxGROW
| wxALL
, 5);
419 m_sizerBook
->SetMinSize(150, 0);
420 m_sizerBook
->Layout();
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 int BookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
431 if ( !text
|| !text
->GetValue().ToLong(&pos
) )
437 int BookWidgetsPage::GetIconIndex() const
439 #if USE_ICONS_IN_BOOK
442 int nImages
= m_imageList
->GetImageCount();
445 return m_book
->GetPageCount() % nImages
;
448 #endif // USE_ICONS_IN_BOOK
453 wxWindow
*BookWidgetsPage::CreateNewPage()
455 return new wxTextCtrl(m_book
, wxID_ANY
, _T("I'm a book page"));
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 void BookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
469 void BookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
471 m_book
->DeleteAllPages();
474 void BookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
476 int pos
= GetTextValue(m_textSelect
);
477 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
479 m_book
->SetSelection(pos
);
482 void BookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
484 m_book
->AddPage(CreateNewPage(), _T("Added page"), false,
488 void BookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
490 int pos
= GetTextValue(m_textInsert
);
491 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
493 m_book
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), false,
497 void BookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
499 int pos
= GetTextValue(m_textRemove
);
500 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
502 m_book
->DeletePage(pos
);
505 void BookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
507 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
510 void BookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
512 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
515 void BookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
517 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
520 void BookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
522 if(m_chkImages
&& m_radioOrient
)
523 event
.Enable( !m_chkImages
->GetValue() ||
524 m_radioOrient
->GetSelection() != wxBK_TOP
);
527 void BookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
530 event
.SetText( wxString::Format(_T("%d"), m_book
->GetPageCount()) );
533 void BookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
536 event
.SetText( wxString::Format(_T("%d"), m_book
->GetSelection()) );
539 void BookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
546 #include "icons/notebook.xpm"
547 #include "wx/notebook.h"
549 // ----------------------------------------------------------------------------
550 // NotebookWidgetsPage
551 // ----------------------------------------------------------------------------
553 class NotebookWidgetsPage
: public BookWidgetsPage
556 NotebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
557 : BookWidgetsPage(book
, imaglist
, notebook_xpm
)
561 virtual ~NotebookWidgetsPage() {}
566 void OnPageChanging(wxNotebookEvent
& event
);
567 void OnPageChanged(wxNotebookEvent
& event
);
570 virtual wxBookCtrlBase
*CreateBook(long flags
)
572 return new wxNotebook(this, BookPage_Book
,
573 wxDefaultPosition
, wxDefaultSize
,
578 DECLARE_EVENT_TABLE()
579 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, BookWidgetsPage
)
587 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, NotebookWidgetsPage::OnPageChanging
)
588 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, NotebookWidgetsPage::OnPageChanged
)
591 #if defined(__WXUNIVERSAL__)
592 #define FAMILY_CTRLS UNIVERSAL_CTRLS
593 #elif defined(__WXMOTIF__)
594 #define FAMILY_CTRLS GENERIC_CTRLS
596 #define FAMILY_CTRLS NATIVE_CTRLS
599 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook"),
600 FAMILY_CTRLS
| BOOK_CTRLS
603 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
605 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
606 event
.GetOldSelection(),
607 event
.GetSelection(),
608 m_book
->GetSelection());
613 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
615 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
616 event
.GetOldSelection(),
617 event
.GetSelection(),
618 m_book
->GetSelection());
623 #endif // wxUSE_NOTEBOOK
627 #include "icons/listbook.xpm"
628 #include "wx/listbook.h"
630 // ----------------------------------------------------------------------------
631 // ListbookWidgetsPage
632 // ----------------------------------------------------------------------------
634 class ListbookWidgetsPage
: public BookWidgetsPage
637 ListbookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
638 : BookWidgetsPage(book
, imaglist
, listbook_xpm
)
642 virtual ~ListbookWidgetsPage() {}
647 void OnPageChanging(wxListbookEvent
& event
);
648 void OnPageChanged(wxListbookEvent
& event
);
651 virtual wxBookCtrlBase
*CreateBook(long flags
)
653 return new wxListbook(this, BookPage_Book
,
654 wxDefaultPosition
, wxDefaultSize
,
659 DECLARE_EVENT_TABLE()
660 DECLARE_WIDGETS_PAGE(ListbookWidgetsPage
)
663 // ----------------------------------------------------------------------------
665 // ----------------------------------------------------------------------------
667 BEGIN_EVENT_TABLE(ListbookWidgetsPage
, BookWidgetsPage
)
668 EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY
, ListbookWidgetsPage::OnPageChanging
)
669 EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY
, ListbookWidgetsPage::OnPageChanged
)
672 IMPLEMENT_WIDGETS_PAGE(ListbookWidgetsPage
, _T("Listbook"),
673 GENERIC_CTRLS
| BOOK_CTRLS
676 void ListbookWidgetsPage::OnPageChanging(wxListbookEvent
& event
)
678 wxLogMessage(_T("Listbook page changing from %d to %d (currently %d)."),
679 event
.GetOldSelection(),
680 event
.GetSelection(),
681 m_book
->GetSelection());
686 void ListbookWidgetsPage::OnPageChanged(wxListbookEvent
& event
)
688 wxLogMessage(_T("Listbook page changed from %d to %d (currently %d)."),
689 event
.GetOldSelection(),
690 event
.GetSelection(),
691 m_book
->GetSelection());
696 #endif // wxUSE_LISTBOOK
700 #include "icons/choicebk.xpm"
701 #include "wx/choicebk.h"
703 // ----------------------------------------------------------------------------
704 // ChoicebookWidgetsPage
705 // ----------------------------------------------------------------------------
707 class ChoicebookWidgetsPage
: public BookWidgetsPage
710 ChoicebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
711 : BookWidgetsPage(book
, imaglist
, choicebk_xpm
)
715 virtual ~ChoicebookWidgetsPage() {}
720 void OnPageChanging(wxChoicebookEvent
& event
);
721 void OnPageChanged(wxChoicebookEvent
& event
);
724 virtual wxBookCtrlBase
*CreateBook(long flags
)
726 return new wxChoicebook(this, BookPage_Book
,
727 wxDefaultPosition
, wxDefaultSize
,
732 DECLARE_EVENT_TABLE()
733 DECLARE_WIDGETS_PAGE(ChoicebookWidgetsPage
)
736 // ----------------------------------------------------------------------------
738 // ----------------------------------------------------------------------------
740 BEGIN_EVENT_TABLE(ChoicebookWidgetsPage
, BookWidgetsPage
)
741 EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanging
)
742 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanged
)
745 IMPLEMENT_WIDGETS_PAGE(ChoicebookWidgetsPage
, _T("Choicebook"),
746 GENERIC_CTRLS
| BOOK_CTRLS
749 void ChoicebookWidgetsPage::OnPageChanging(wxChoicebookEvent
& event
)
751 wxLogMessage(_T("Choicebook page changing from %d to %d (currently %d)."),
752 event
.GetOldSelection(),
753 event
.GetSelection(),
754 m_book
->GetSelection());
759 void ChoicebookWidgetsPage::OnPageChanged(wxChoicebookEvent
& event
)
761 wxLogMessage(_T("Choicebook page changed from %d to %d (currently %d)."),
762 event
.GetOldSelection(),
763 event
.GetSelection(),
764 m_book
->GetSelection());
769 #endif // wxUSE_CHOICEBOOK
771 #endif // wxUSE_BOOKCTRL