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"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 BookPage_Reset
= wxID_HIGHEST
,
66 BookPage_NumPagesText
,
67 BookPage_CurSelectText
,
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 class BookWidgetsPage
: public WidgetsPage
88 BookWidgetsPage(WidgetsBookCtrl
*book
);
89 virtual ~BookWidgetsPage();
91 virtual wxControl
*GetWidget() const { return m_book
; }
92 virtual void RecreateWidget() { RecreateBook(); }
96 void OnButtonReset(wxCommandEvent
& event
);
97 void OnButtonDeleteAll(wxCommandEvent
& event
);
98 void OnButtonSelectPage(wxCommandEvent
& event
);
99 void OnButtonAddPage(wxCommandEvent
& event
);
100 void OnButtonInsertPage(wxCommandEvent
& event
);
101 void OnButtonRemovePage(wxCommandEvent
& event
);
103 void OnCheckOrRadioBox(wxCommandEvent
& event
);
105 void OnUpdateUINumPagesText(wxUpdateUIEvent
& event
);
106 void OnUpdateUICurSelectText(wxUpdateUIEvent
& event
);
108 void OnUpdateUISelectButton(wxUpdateUIEvent
& event
);
109 void OnUpdateUIInsertButton(wxUpdateUIEvent
& event
);
110 void OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
);
112 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
114 // reset book parameters
119 virtual wxBookCtrlBase
*CreateBook(long flags
) = 0;
121 // create or destroy the image list
122 void CreateImageList();
125 wxWindow
*CreateNewPage();
127 // get the image index for the new page
128 int GetIconIndex() const;
130 // get the numeric value of text ctrl
131 int GetTextValue(wxTextCtrl
*text
) const;
133 // is the value in range?
134 bool IsValidValue(int val
) const
135 { return (val
>= 0) && (val
< (int) m_book
->GetPageCount()); }
140 // the check/radio boxes for styles
141 wxCheckBox
*m_chkImages
;
142 wxRadioBox
*m_radioOrient
;
144 // the text controls containing input for various commands
145 wxTextCtrl
*m_textInsert
,
149 // the book itself and the sizer it is in
150 wxBookCtrlBase
*m_book
;
151 wxSizer
*m_sizerBook
;
153 // thei mage list for our book
154 wxImageList
*m_imageList
;
157 DECLARE_EVENT_TABLE()
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 BEGIN_EVENT_TABLE(BookWidgetsPage
, WidgetsPage
)
165 EVT_BUTTON(BookPage_Reset
, BookWidgetsPage
::OnButtonReset
)
166 EVT_BUTTON(BookPage_SelectPage
, BookWidgetsPage
::OnButtonSelectPage
)
167 EVT_BUTTON(BookPage_AddPage
, BookWidgetsPage
::OnButtonAddPage
)
168 EVT_BUTTON(BookPage_InsertPage
, BookWidgetsPage
::OnButtonInsertPage
)
169 EVT_BUTTON(BookPage_RemovePage
, BookWidgetsPage
::OnButtonRemovePage
)
170 EVT_BUTTON(BookPage_DeleteAll
, BookWidgetsPage
::OnButtonDeleteAll
)
172 EVT_UPDATE_UI(BookPage_NumPagesText
, BookWidgetsPage
::OnUpdateUINumPagesText
)
173 EVT_UPDATE_UI(BookPage_CurSelectText
, BookWidgetsPage
::OnUpdateUICurSelectText
)
175 EVT_UPDATE_UI(BookPage_SelectPage
, BookWidgetsPage
::OnUpdateUISelectButton
)
176 EVT_UPDATE_UI(BookPage_InsertPage
, BookWidgetsPage
::OnUpdateUIInsertButton
)
177 EVT_UPDATE_UI(BookPage_RemovePage
, BookWidgetsPage
::OnUpdateUIRemoveButton
)
179 EVT_CHECKBOX(wxID_ANY
, BookWidgetsPage
::OnCheckOrRadioBox
)
180 EVT_RADIOBOX(wxID_ANY
, BookWidgetsPage
::OnCheckOrRadioBox
)
183 // ============================================================================
185 // ============================================================================
187 BookWidgetsPage
::BookWidgetsPage(WidgetsBookCtrl
*book
)
195 m_sizerBook
= (wxSizer
*)NULL
;
197 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
200 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
202 // must be in sync with Orient enum
203 wxArrayString orientations
;
204 orientations
.Add(_T("&top"));
205 orientations
.Add(_T("&bottom"));
206 orientations
.Add(_T("&left"));
207 orientations
.Add(_T("&right"));
209 wxASSERT_MSG( orientations
.GetCount() == Orient_Max
,
210 _T("forgot to update something") );
212 m_chkImages
= new wxCheckBox(this, wxID_ANY
, _T("Show &images"));
213 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, _T("&Tab orientation"),
214 wxDefaultPosition
, wxDefaultSize
,
215 orientations
, 1, wxRA_SPECIFY_COLS
);
217 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
219 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
220 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
221 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
223 wxButton
*btn
= new wxButton(this, BookPage_Reset
, _T("&Reset"));
224 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
227 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Contents"));
228 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
231 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
232 BookPage_NumPagesText
,
234 text
->SetEditable(false);
235 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
237 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
238 BookPage_CurSelectText
,
240 text
->SetEditable(false);
241 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
243 sizerRow
= CreateSizerWithTextAndButton(BookPage_SelectPage
,
247 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
249 btn
= new wxButton(this, BookPage_AddPage
, _T("&Add page"));
250 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
252 sizerRow
= CreateSizerWithTextAndButton(BookPage_InsertPage
,
253 _T("&Insert page at"),
256 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
258 sizerRow
= CreateSizerWithTextAndButton(BookPage_RemovePage
,
262 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
264 btn
= new wxButton(this, BookPage_DeleteAll
, _T("&Delete All"));
265 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
268 m_sizerBook
= new wxBoxSizer(wxHORIZONTAL
);
270 // the 3 panes compose the window
271 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
272 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
273 sizerTop
->Add(m_sizerBook
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
275 // final initializations
284 BookWidgetsPage
::~BookWidgetsPage()
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 void BookWidgetsPage
::Reset()
295 m_chkImages
->SetValue(true);
296 m_radioOrient
->SetSelection(Orient_Top
);
299 void BookWidgetsPage
::CreateImageList()
301 if ( m_chkImages
->GetValue() )
305 // create a dummy image list with a few icons
306 m_imageList
= new wxImageList(32, 32);
308 m_imageList
->Add(wxArtProvider
::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
309 m_imageList
->Add(wxArtProvider
::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
310 m_imageList
->Add(wxArtProvider
::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
311 m_imageList
->Add(wxArtProvider
::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
315 m_book
->SetImageList(m_imageList
);
326 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
327 // it would be logical if this removed the image list from book, under
328 // MSW it crashes instead - FIXME
331 void BookWidgetsPage
::RecreateBook()
333 int flags
= ms_defaultFlags
;
334 switch ( m_radioOrient
->GetSelection() )
337 wxFAIL_MSG( _T("unknown orientation") );
345 flags
|= wxBK_BOTTOM
;
357 wxBookCtrlBase
*oldBook
= m_book
;
359 m_book
= CreateBook(flags
);
365 const int sel
= oldBook
->GetSelection();
367 const int count
= oldBook
->GetPageCount();
369 // recreate the pages
370 for ( int n
= 0; n
< count
; n
++ )
372 m_book
->AddPage(CreateNewPage(),
373 oldBook
->GetPageText(n
),
375 m_chkImages
->GetValue() ?
376 GetIconIndex() : -1);
379 m_sizerBook
->Detach( oldBook
);
385 m_book
->SetSelection(sel
);
389 m_sizerBook
->Add(m_book
, 1, wxGROW
| wxALL
, 5);
390 m_sizerBook
->SetMinSize(150, 0);
391 m_sizerBook
->Layout();
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
398 int BookWidgetsPage
::GetTextValue(wxTextCtrl
*text
) const
401 if ( !text
->GetValue().ToLong(&pos
) )
407 int BookWidgetsPage
::GetIconIndex() const
411 int nImages
= m_imageList
->GetImageCount();
414 return m_book
->GetPageCount() % nImages
;
421 wxWindow
*BookWidgetsPage
::CreateNewPage()
423 return new wxTextCtrl(m_book
, wxID_ANY
, _T("I'm a book page"));
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 void BookWidgetsPage
::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
437 void BookWidgetsPage
::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
439 m_book
->DeleteAllPages();
442 void BookWidgetsPage
::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
444 int pos
= GetTextValue(m_textSelect
);
445 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
447 m_book
->SetSelection(pos
);
450 void BookWidgetsPage
::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
452 m_book
->AddPage(CreateNewPage(), _T("Added page"), false,
456 void BookWidgetsPage
::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
458 int pos
= GetTextValue(m_textInsert
);
459 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
461 m_book
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), false,
465 void BookWidgetsPage
::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
467 int pos
= GetTextValue(m_textRemove
);
468 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
470 m_book
->DeletePage(pos
);
473 void BookWidgetsPage
::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
475 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
478 void BookWidgetsPage
::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
480 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
483 void BookWidgetsPage
::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
485 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
488 void BookWidgetsPage
::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
490 event
.Enable( !m_chkImages
->GetValue() ||
491 m_radioOrient
->GetSelection() != wxBK_TOP
);
494 void BookWidgetsPage
::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
496 event
.SetText( wxString
::Format(_T("%d"), m_book
->GetPageCount()) );
499 void BookWidgetsPage
::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
501 event
.SetText( wxString
::Format(_T("%d"), m_book
->GetSelection()) );
504 void BookWidgetsPage
::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
511 #include "icons/notebook.xpm"
512 #include "wx/notebook.h"
514 // ----------------------------------------------------------------------------
515 // NotebookWidgetsPage
516 // ----------------------------------------------------------------------------
518 class NotebookWidgetsPage
: public BookWidgetsPage
521 NotebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
522 : BookWidgetsPage(book
)
524 imaglist
->Add(wxBitmap(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
)
607 imaglist
->Add(wxBitmap(listbook_xpm
));
610 virtual ~ListbookWidgetsPage() {}
615 void OnPageChanging(wxListbookEvent
& event
);
616 void OnPageChanged(wxListbookEvent
& event
);
619 virtual wxBookCtrlBase
*CreateBook(long flags
)
621 return new wxListbook(this, BookPage_Book
,
622 wxDefaultPosition
, wxDefaultSize
,
628 DECLARE_EVENT_TABLE()
629 DECLARE_WIDGETS_PAGE(ListbookWidgetsPage
)
632 // ----------------------------------------------------------------------------
634 // ----------------------------------------------------------------------------
636 BEGIN_EVENT_TABLE(ListbookWidgetsPage
, BookWidgetsPage
)
637 EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY
, ListbookWidgetsPage
::OnPageChanging
)
638 EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY
, ListbookWidgetsPage
::OnPageChanged
)
641 IMPLEMENT_WIDGETS_PAGE(ListbookWidgetsPage
, _T("Listbook"),
642 GENERIC_CTRLS
| BOOK_CTRLS
645 void ListbookWidgetsPage
::OnPageChanging(wxListbookEvent
& event
)
647 wxLogMessage(_T("Listbook page changing from %d to %d (currently %d)."),
648 event
.GetOldSelection(),
649 event
.GetSelection(),
650 m_book
->GetSelection());
655 void ListbookWidgetsPage
::OnPageChanged(wxListbookEvent
& event
)
657 wxLogMessage(_T("Listbook page changed from %d to %d (currently %d)."),
658 event
.GetOldSelection(),
659 event
.GetSelection(),
660 m_book
->GetSelection());
665 #endif // wxUSE_LISTBOOK
669 #include "icons/choicebk.xpm"
670 #include "wx/choicebk.h"
672 // ----------------------------------------------------------------------------
673 // ChoicebookWidgetsPage
674 // ----------------------------------------------------------------------------
676 class ChoicebookWidgetsPage
: public BookWidgetsPage
679 ChoicebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
680 : BookWidgetsPage(book
)
682 imaglist
->Add(wxBitmap(choicebk_xpm
));
685 virtual ~ChoicebookWidgetsPage() {}
690 void OnPageChanging(wxChoicebookEvent
& event
);
691 void OnPageChanged(wxChoicebookEvent
& event
);
694 virtual wxBookCtrlBase
*CreateBook(long flags
)
696 return new wxChoicebook(this, BookPage_Book
,
697 wxDefaultPosition
, wxDefaultSize
,
703 DECLARE_EVENT_TABLE()
704 DECLARE_WIDGETS_PAGE(ChoicebookWidgetsPage
)
707 // ----------------------------------------------------------------------------
709 // ----------------------------------------------------------------------------
711 BEGIN_EVENT_TABLE(ChoicebookWidgetsPage
, BookWidgetsPage
)
712 EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY
, ChoicebookWidgetsPage
::OnPageChanging
)
713 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY
, ChoicebookWidgetsPage
::OnPageChanged
)
716 IMPLEMENT_WIDGETS_PAGE(ChoicebookWidgetsPage
, _T("Choicebook"),
717 GENERIC_CTRLS
| BOOK_CTRLS
720 void ChoicebookWidgetsPage
::OnPageChanging(wxChoicebookEvent
& event
)
722 wxLogMessage(_T("Choicebook page changing from %d to %d (currently %d)."),
723 event
.GetOldSelection(),
724 event
.GetSelection(),
725 m_book
->GetSelection());
730 void ChoicebookWidgetsPage
::OnPageChanged(wxChoicebookEvent
& event
)
732 wxLogMessage(_T("Choicebook page changed from %d to %d (currently %d)."),
733 event
.GetOldSelection(),
734 event
.GetSelection(),
735 m_book
->GetSelection());
740 #endif // wxUSE_CHOICEBOOK
742 #endif // wxUSE_BOOKCTRL