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
, const char *const 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
, const char *const 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
, wxT("&Set style"));
216 // must be in sync with Orient enum
217 wxArrayString orientations
;
218 orientations
.Add(wxT("&top"));
219 orientations
.Add(wxT("&bottom"));
220 orientations
.Add(wxT("&left"));
221 orientations
.Add(wxT("&right"));
223 wxASSERT_MSG( orientations
.GetCount() == Orient_Max
,
224 wxT("forgot to update something") );
226 m_chkImages
= new wxCheckBox(this, wxID_ANY
, wxT("Show &images"));
227 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, wxT("&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
, wxT("&Reset"));
238 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
241 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Contents"));
242 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
245 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(wxT("Number of pages: "),
246 BookPage_NumPagesText
,
248 text
->SetEditable(false);
249 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
251 sizerRow
= CreateSizerWithTextAndLabel(wxT("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
, wxT("&Add page"));
264 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
266 sizerRow
= CreateSizerWithTextAndButton(BookPage_InsertPage
,
267 wxT("&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
, wxT("&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
300 BookWidgetsPage::~BookWidgetsPage()
302 #if USE_ICONS_IN_BOOK
304 #endif // USE_ICONS_IN_BOOK
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 void BookWidgetsPage::Reset()
313 m_chkImages
->SetValue(true);
314 m_radioOrient
->SetSelection(Orient_Top
);
317 #if USE_ICONS_IN_BOOK
318 void BookWidgetsPage::CreateImageList()
320 if ( m_chkImages
->GetValue() )
324 // create a dummy image list with a few icons
325 m_imageList
= new wxImageList(32, 32);
327 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
328 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
329 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
330 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
334 m_book
->SetImageList(m_imageList
);
345 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
346 // it would be logical if this removed the image list from book, under
347 // MSW it crashes instead - FIXME
349 #endif // USE_ICONS_IN_BOOK
351 void BookWidgetsPage::RecreateBook()
353 // do not recreate anything in case page content was not prepared yet
357 int flags
= ms_defaultFlags
;
359 switch ( m_radioOrient
->GetSelection() )
362 wxFAIL_MSG( wxT("unknown orientation") );
370 flags
|= wxBK_BOTTOM
;
382 wxBookCtrlBase
*oldBook
= m_book
;
384 m_book
= CreateBook(flags
);
386 #if USE_ICONS_IN_BOOK
388 #endif // USE_ICONS_IN_BOOK
392 const int sel
= oldBook
->GetSelection();
394 const int count
= oldBook
->GetPageCount();
396 // recreate the pages
397 for ( int n
= 0; n
< count
; n
++ )
399 m_book
->AddPage(CreateNewPage(),
400 oldBook
->GetPageText(n
),
402 m_chkImages
->GetValue() ?
403 GetIconIndex() : -1);
406 m_sizerBook
->Detach( oldBook
);
412 m_book
->SetSelection(sel
);
416 m_sizerBook
->Add(m_book
, 1, wxGROW
| wxALL
, 5);
417 m_sizerBook
->SetMinSize(150, 0);
418 m_sizerBook
->Layout();
421 // ----------------------------------------------------------------------------
423 // ----------------------------------------------------------------------------
425 int BookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
429 if ( !text
|| !text
->GetValue().ToLong(&pos
) )
435 int BookWidgetsPage::GetIconIndex() const
437 #if USE_ICONS_IN_BOOK
440 int nImages
= m_imageList
->GetImageCount();
443 return m_book
->GetPageCount() % nImages
;
446 #endif // USE_ICONS_IN_BOOK
451 wxWindow
*BookWidgetsPage::CreateNewPage()
453 return new wxTextCtrl(m_book
, wxID_ANY
, wxT("I'm a book page"));
456 // ----------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------
460 void BookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
467 void BookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
469 m_book
->DeleteAllPages();
472 void BookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
474 int pos
= GetTextValue(m_textSelect
);
475 wxCHECK_RET( IsValidValue(pos
), wxT("button should be disabled") );
477 m_book
->SetSelection(pos
);
480 void BookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
482 m_book
->AddPage(CreateNewPage(), wxT("Added page"), false,
486 void BookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
488 int pos
= GetTextValue(m_textInsert
);
489 wxCHECK_RET( IsValidValue(pos
), wxT("button should be disabled") );
491 m_book
->InsertPage(pos
, CreateNewPage(), wxT("Inserted page"), false,
495 void BookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
497 int pos
= GetTextValue(m_textRemove
);
498 wxCHECK_RET( IsValidValue(pos
), wxT("button should be disabled") );
500 m_book
->DeletePage(pos
);
503 void BookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
505 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
508 void BookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
510 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
513 void BookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
515 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
518 void BookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
520 if(m_chkImages
&& m_radioOrient
)
521 event
.Enable( !m_chkImages
->GetValue() ||
522 m_radioOrient
->GetSelection() != wxBK_TOP
);
525 void BookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
528 event
.SetText( wxString::Format(wxT("%u"), unsigned(m_book
->GetPageCount())) );
531 void BookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
534 event
.SetText( wxString::Format(wxT("%d"), m_book
->GetSelection()) );
537 void BookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
544 #include "icons/notebook.xpm"
545 #include "wx/notebook.h"
547 // ----------------------------------------------------------------------------
548 // NotebookWidgetsPage
549 // ----------------------------------------------------------------------------
551 class NotebookWidgetsPage
: public BookWidgetsPage
554 NotebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
555 : BookWidgetsPage(book
, imaglist
, notebook_xpm
)
559 virtual ~NotebookWidgetsPage() {}
564 void OnPageChanging(wxNotebookEvent
& event
);
565 void OnPageChanged(wxNotebookEvent
& event
);
568 virtual wxBookCtrlBase
*CreateBook(long flags
)
570 return new wxNotebook(this, BookPage_Book
,
571 wxDefaultPosition
, wxDefaultSize
,
576 DECLARE_EVENT_TABLE()
577 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
580 // ----------------------------------------------------------------------------
582 // ----------------------------------------------------------------------------
584 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, BookWidgetsPage
)
585 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, NotebookWidgetsPage::OnPageChanging
)
586 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, NotebookWidgetsPage::OnPageChanged
)
589 #if defined(__WXUNIVERSAL__)
590 #define FAMILY_CTRLS UNIVERSAL_CTRLS
591 #elif defined(__WXMOTIF__)
592 #define FAMILY_CTRLS GENERIC_CTRLS
594 #define FAMILY_CTRLS NATIVE_CTRLS
597 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, wxT("Notebook"),
598 FAMILY_CTRLS
| BOOK_CTRLS
601 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
)
603 wxLogMessage(wxT("Notebook page changing from %d to %d (currently %d)."),
604 event
.GetOldSelection(),
605 event
.GetSelection(),
606 m_book
->GetSelection());
611 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
613 wxLogMessage(wxT("Notebook page changed from %d to %d (currently %d)."),
614 event
.GetOldSelection(),
615 event
.GetSelection(),
616 m_book
->GetSelection());
621 #endif // wxUSE_NOTEBOOK
625 #include "icons/listbook.xpm"
626 #include "wx/listbook.h"
628 // ----------------------------------------------------------------------------
629 // ListbookWidgetsPage
630 // ----------------------------------------------------------------------------
632 class ListbookWidgetsPage
: public BookWidgetsPage
635 ListbookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
636 : BookWidgetsPage(book
, imaglist
, listbook_xpm
)
640 virtual ~ListbookWidgetsPage() {}
645 void OnPageChanging(wxListbookEvent
& event
);
646 void OnPageChanged(wxListbookEvent
& event
);
649 virtual wxBookCtrlBase
*CreateBook(long flags
)
651 return new wxListbook(this, BookPage_Book
,
652 wxDefaultPosition
, wxDefaultSize
,
657 DECLARE_EVENT_TABLE()
658 DECLARE_WIDGETS_PAGE(ListbookWidgetsPage
)
661 // ----------------------------------------------------------------------------
663 // ----------------------------------------------------------------------------
665 BEGIN_EVENT_TABLE(ListbookWidgetsPage
, BookWidgetsPage
)
666 EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY
, ListbookWidgetsPage::OnPageChanging
)
667 EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY
, ListbookWidgetsPage::OnPageChanged
)
670 IMPLEMENT_WIDGETS_PAGE(ListbookWidgetsPage
, wxT("Listbook"),
671 GENERIC_CTRLS
| BOOK_CTRLS
674 void ListbookWidgetsPage::OnPageChanging(wxListbookEvent
& event
)
676 wxLogMessage(wxT("Listbook page changing from %d to %d (currently %d)."),
677 event
.GetOldSelection(),
678 event
.GetSelection(),
679 m_book
->GetSelection());
684 void ListbookWidgetsPage::OnPageChanged(wxListbookEvent
& event
)
686 wxLogMessage(wxT("Listbook page changed from %d to %d (currently %d)."),
687 event
.GetOldSelection(),
688 event
.GetSelection(),
689 m_book
->GetSelection());
694 #endif // wxUSE_LISTBOOK
698 #include "icons/choicebk.xpm"
699 #include "wx/choicebk.h"
701 // ----------------------------------------------------------------------------
702 // ChoicebookWidgetsPage
703 // ----------------------------------------------------------------------------
705 class ChoicebookWidgetsPage
: public BookWidgetsPage
708 ChoicebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
709 : BookWidgetsPage(book
, imaglist
, choicebk_xpm
)
713 virtual ~ChoicebookWidgetsPage() {}
718 void OnPageChanging(wxChoicebookEvent
& event
);
719 void OnPageChanged(wxChoicebookEvent
& event
);
722 virtual wxBookCtrlBase
*CreateBook(long flags
)
724 return new wxChoicebook(this, BookPage_Book
,
725 wxDefaultPosition
, wxDefaultSize
,
730 DECLARE_EVENT_TABLE()
731 DECLARE_WIDGETS_PAGE(ChoicebookWidgetsPage
)
734 // ----------------------------------------------------------------------------
736 // ----------------------------------------------------------------------------
738 BEGIN_EVENT_TABLE(ChoicebookWidgetsPage
, BookWidgetsPage
)
739 EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanging
)
740 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY
, ChoicebookWidgetsPage::OnPageChanged
)
743 IMPLEMENT_WIDGETS_PAGE(ChoicebookWidgetsPage
, wxT("Choicebook"),
744 GENERIC_CTRLS
| BOOK_CTRLS
747 void ChoicebookWidgetsPage::OnPageChanging(wxChoicebookEvent
& event
)
749 wxLogMessage(wxT("Choicebook page changing from %d to %d (currently %d)."),
750 event
.GetOldSelection(),
751 event
.GetSelection(),
752 m_book
->GetSelection());
757 void ChoicebookWidgetsPage::OnPageChanged(wxChoicebookEvent
& event
)
759 wxLogMessage(wxT("Choicebook page changed from %d to %d (currently %d)."),
760 event
.GetOldSelection(),
761 event
.GetSelection(),
762 m_book
->GetSelection());
767 #endif // wxUSE_CHOICEBOOK
769 #endif // wxUSE_BOOKCTRL