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 // create or destroy the image list
123 void CreateImageList();
126 wxWindow
*CreateNewPage();
128 // get the image index for the new page
129 int GetIconIndex() const;
131 // get the numeric value of text ctrl
132 int GetTextValue(wxTextCtrl
*text
) const;
134 // is the value in range?
135 bool IsValidValue(int val
) const
136 { return (val
>= 0) && (val
< (int) m_book
->GetPageCount()); }
141 // the check/radio boxes for styles
142 wxCheckBox
*m_chkImages
;
143 wxRadioBox
*m_radioOrient
;
145 // the text controls containing input for various commands
146 wxTextCtrl
*m_textInsert
,
150 // the book itself and the sizer it is in
151 wxBookCtrlBase
*m_book
;
152 wxSizer
*m_sizerBook
;
154 // thei mage list for our book
155 wxImageList
*m_imageList
;
158 DECLARE_EVENT_TABLE()
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 BEGIN_EVENT_TABLE(BookWidgetsPage
, WidgetsPage
)
166 EVT_BUTTON(BookPage_Reset
, BookWidgetsPage::OnButtonReset
)
167 EVT_BUTTON(BookPage_SelectPage
, BookWidgetsPage::OnButtonSelectPage
)
168 EVT_BUTTON(BookPage_AddPage
, BookWidgetsPage::OnButtonAddPage
)
169 EVT_BUTTON(BookPage_InsertPage
, BookWidgetsPage::OnButtonInsertPage
)
170 EVT_BUTTON(BookPage_RemovePage
, BookWidgetsPage::OnButtonRemovePage
)
171 EVT_BUTTON(BookPage_DeleteAll
, BookWidgetsPage::OnButtonDeleteAll
)
173 EVT_UPDATE_UI(BookPage_NumPagesText
, BookWidgetsPage::OnUpdateUINumPagesText
)
174 EVT_UPDATE_UI(BookPage_CurSelectText
, BookWidgetsPage::OnUpdateUICurSelectText
)
176 EVT_UPDATE_UI(BookPage_SelectPage
, BookWidgetsPage::OnUpdateUISelectButton
)
177 EVT_UPDATE_UI(BookPage_InsertPage
, BookWidgetsPage::OnUpdateUIInsertButton
)
178 EVT_UPDATE_UI(BookPage_RemovePage
, BookWidgetsPage::OnUpdateUIRemoveButton
)
180 EVT_CHECKBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
181 EVT_RADIOBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
184 // ============================================================================
186 // ============================================================================
188 BookWidgetsPage::BookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
, char* icon
[])
189 :WidgetsPage(book
, imaglist
, icon
)
196 m_sizerBook
= (wxSizer
*)NULL
;
198 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
201 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
203 // must be in sync with Orient enum
204 wxArrayString orientations
;
205 orientations
.Add(_T("&top"));
206 orientations
.Add(_T("&bottom"));
207 orientations
.Add(_T("&left"));
208 orientations
.Add(_T("&right"));
210 wxASSERT_MSG( orientations
.GetCount() == Orient_Max
,
211 _T("forgot to update something") );
213 m_chkImages
= new wxCheckBox(this, wxID_ANY
, _T("Show &images"));
214 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, _T("&Tab orientation"),
215 wxDefaultPosition
, wxDefaultSize
,
216 orientations
, 1, wxRA_SPECIFY_COLS
);
218 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
220 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
221 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
222 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
224 wxButton
*btn
= new wxButton(this, BookPage_Reset
, _T("&Reset"));
225 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
228 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Contents"));
229 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
232 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
233 BookPage_NumPagesText
,
235 text
->SetEditable(false);
236 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
238 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
239 BookPage_CurSelectText
,
241 text
->SetEditable(false);
242 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
244 sizerRow
= CreateSizerWithTextAndButton(BookPage_SelectPage
,
248 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
250 btn
= new wxButton(this, BookPage_AddPage
, _T("&Add page"));
251 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
253 sizerRow
= CreateSizerWithTextAndButton(BookPage_InsertPage
,
254 _T("&Insert page at"),
257 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
259 sizerRow
= CreateSizerWithTextAndButton(BookPage_RemovePage
,
263 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
265 btn
= new wxButton(this, BookPage_DeleteAll
, _T("&Delete All"));
266 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
269 m_sizerBook
= new wxBoxSizer(wxHORIZONTAL
);
271 // the 3 panes compose the window
272 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
273 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
274 sizerTop
->Add(m_sizerBook
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
276 // final initializations
285 BookWidgetsPage::~BookWidgetsPage()
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
294 void BookWidgetsPage::Reset()
296 m_chkImages
->SetValue(true);
297 m_radioOrient
->SetSelection(Orient_Top
);
300 void BookWidgetsPage::CreateImageList()
302 if ( m_chkImages
->GetValue() )
306 // create a dummy image list with a few icons
307 m_imageList
= new wxImageList(32, 32);
309 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
310 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
311 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
312 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
316 m_book
->SetImageList(m_imageList
);
327 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
328 // it would be logical if this removed the image list from book, under
329 // MSW it crashes instead - FIXME
332 void BookWidgetsPage::RecreateBook()
334 int flags
= ms_defaultFlags
;
335 switch ( m_radioOrient
->GetSelection() )
338 wxFAIL_MSG( _T("unknown orientation") );
346 flags
|= wxBK_BOTTOM
;
358 wxBookCtrlBase
*oldBook
= m_book
;
360 m_book
= CreateBook(flags
);
366 const int sel
= oldBook
->GetSelection();
368 const int count
= oldBook
->GetPageCount();
370 // recreate the pages
371 for ( int n
= 0; n
< count
; n
++ )
373 m_book
->AddPage(CreateNewPage(),
374 oldBook
->GetPageText(n
),
376 m_chkImages
->GetValue() ?
377 GetIconIndex() : -1);
380 m_sizerBook
->Detach( oldBook
);
386 m_book
->SetSelection(sel
);
390 m_sizerBook
->Add(m_book
, 1, wxGROW
| wxALL
, 5);
391 m_sizerBook
->SetMinSize(150, 0);
392 m_sizerBook
->Layout();
395 // ----------------------------------------------------------------------------
397 // ----------------------------------------------------------------------------
399 int BookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
402 if ( !text
->GetValue().ToLong(&pos
) )
408 int BookWidgetsPage::GetIconIndex() const
412 int nImages
= m_imageList
->GetImageCount();
415 return m_book
->GetPageCount() % nImages
;
422 wxWindow
*BookWidgetsPage::CreateNewPage()
424 return new wxTextCtrl(m_book
, wxID_ANY
, _T("I'm a book page"));
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
431 void BookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
438 void BookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
440 m_book
->DeleteAllPages();
443 void BookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
445 int pos
= GetTextValue(m_textSelect
);
446 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
448 m_book
->SetSelection(pos
);
451 void BookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
453 m_book
->AddPage(CreateNewPage(), _T("Added page"), false,
457 void BookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
459 int pos
= GetTextValue(m_textInsert
);
460 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
462 m_book
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), false,
466 void BookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
468 int pos
= GetTextValue(m_textRemove
);
469 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
471 m_book
->DeletePage(pos
);
474 void BookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
476 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
479 void BookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
481 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
484 void BookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
486 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
489 void BookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
491 event
.Enable( !m_chkImages
->GetValue() ||
492 m_radioOrient
->GetSelection() != wxBK_TOP
);
495 void BookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
497 event
.SetText( wxString::Format(_T("%d"), m_book
->GetPageCount()) );
500 void BookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
502 event
.SetText( wxString::Format(_T("%d"), m_book
->GetSelection()) );
505 void BookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
512 #include "icons/notebook.xpm"
513 #include "wx/notebook.h"
515 // ----------------------------------------------------------------------------
516 // NotebookWidgetsPage
517 // ----------------------------------------------------------------------------
519 class NotebookWidgetsPage
: public BookWidgetsPage
522 NotebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
523 : BookWidgetsPage(book
, imaglist
, notebook_xpm
)
527 virtual ~NotebookWidgetsPage() {}
532 void OnPageChanging(wxNotebookEvent
& event
);
533 void OnPageChanged(wxNotebookEvent
& event
);
536 virtual wxBookCtrlBase
*CreateBook(long flags
)
538 return new wxNotebook(this, BookPage_Book
,
539 wxDefaultPosition
, wxDefaultSize
,
545 DECLARE_EVENT_TABLE()
546 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
549 // ----------------------------------------------------------------------------
551 // ----------------------------------------------------------------------------
553 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, BookWidgetsPage
)
554 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, NotebookWidgetsPage::OnPageChanging
)
555 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, NotebookWidgetsPage::OnPageChanged
)
558 #if defined(__WXUNIVERSAL__)
559 #define FAMILY_CTRLS UNIVERSAL_CTRLS
560 #elif defined(__WXMOTIF__)
561 #define FAMILY_CTRLS GENERIC_CTRLS
563 #define FAMILY_CTRLS NATIVE_CTRLS
566 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook"),
567 FAMILY_CTRLS
| BOOK_CTRLS
570 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
572 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
573 event
.GetOldSelection(),
574 event
.GetSelection(),
575 m_book
->GetSelection());
580 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
582 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
583 event
.GetOldSelection(),
584 event
.GetSelection(),
585 m_book
->GetSelection());
590 #endif // wxUSE_NOTEBOOK
594 #include "icons/listbook.xpm"
595 #include "wx/listbook.h"
597 // ----------------------------------------------------------------------------
598 // ListbookWidgetsPage
599 // ----------------------------------------------------------------------------
601 class ListbookWidgetsPage
: public BookWidgetsPage
604 ListbookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
605 : BookWidgetsPage(book
, imaglist
, listbook_xpm
)
609 virtual ~ListbookWidgetsPage() {}
614 void OnPageChanging(wxListbookEvent
& event
);
615 void OnPageChanged(wxListbookEvent
& event
);
618 virtual wxBookCtrlBase
*CreateBook(long flags
)
620 return new wxListbook(this, BookPage_Book
,
621 wxDefaultPosition
, wxDefaultSize
,
627 DECLARE_EVENT_TABLE()
628 DECLARE_WIDGETS_PAGE(ListbookWidgetsPage
)
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 BEGIN_EVENT_TABLE(ListbookWidgetsPage
, BookWidgetsPage
)
636 EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY
, ListbookWidgetsPage::OnPageChanging
)
637 EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY
, ListbookWidgetsPage::OnPageChanged
)
640 IMPLEMENT_WIDGETS_PAGE(ListbookWidgetsPage
, _T("Listbook"),
641 GENERIC_CTRLS
| BOOK_CTRLS
644 void ListbookWidgetsPage::OnPageChanging(wxListbookEvent
& event
)
646 wxLogMessage(_T("Listbook page changing from %d to %d (currently %d)."),
647 event
.GetOldSelection(),
648 event
.GetSelection(),
649 m_book
->GetSelection());
654 void ListbookWidgetsPage::OnPageChanged(wxListbookEvent
& event
)
656 wxLogMessage(_T("Listbook page changed from %d to %d (currently %d)."),
657 event
.GetOldSelection(),
658 event
.GetSelection(),
659 m_book
->GetSelection());
664 #endif // wxUSE_LISTBOOK
668 #include "icons/choicebk.xpm"
669 #include "wx/choicebk.h"
671 // ----------------------------------------------------------------------------
672 // ChoicebookWidgetsPage
673 // ----------------------------------------------------------------------------
675 class ChoicebookWidgetsPage
: public BookWidgetsPage
678 ChoicebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
679 : BookWidgetsPage(book
, imaglist
, choicebk_xpm
)
683 virtual ~ChoicebookWidgetsPage() {}
688 void OnPageChanging(wxChoicebookEvent
& event
);
689 void OnPageChanged(wxChoicebookEvent
& event
);
692 virtual wxBookCtrlBase
*CreateBook(long flags
)
694 return new wxChoicebook(this, BookPage_Book
,
695 wxDefaultPosition
, wxDefaultSize
,
701 DECLARE_EVENT_TABLE()
702 DECLARE_WIDGETS_PAGE(ChoicebookWidgetsPage
)
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 BEGIN_EVENT_TABLE(ChoicebookWidgetsPage
, BookWidgetsPage
)
710 EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanging
)
711 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanged
)
714 IMPLEMENT_WIDGETS_PAGE(ChoicebookWidgetsPage
, _T("Choicebook"),
715 GENERIC_CTRLS
| BOOK_CTRLS
718 void ChoicebookWidgetsPage::OnPageChanging(wxChoicebookEvent
& event
)
720 wxLogMessage(_T("Choicebook page changing from %d to %d (currently %d)."),
721 event
.GetOldSelection(),
722 event
.GetSelection(),
723 m_book
->GetSelection());
728 void ChoicebookWidgetsPage::OnPageChanged(wxChoicebookEvent
& event
)
730 wxLogMessage(_T("Choicebook page changed from %d to %d (currently %d)."),
731 event
.GetOldSelection(),
732 event
.GetSelection(),
733 m_book
->GetSelection());
738 #endif // wxUSE_CHOICEBOOK
740 #endif // wxUSE_BOOKCTRL