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(); }
97 void OnPageChanging(wxNotebookEvent
& event
);
98 void OnPageChanged(wxNotebookEvent
& event
);
101 void OnButtonReset(wxCommandEvent
& event
);
102 void OnButtonDeleteAll(wxCommandEvent
& event
);
103 void OnButtonSelectPage(wxCommandEvent
& event
);
104 void OnButtonAddPage(wxCommandEvent
& event
);
105 void OnButtonInsertPage(wxCommandEvent
& event
);
106 void OnButtonRemovePage(wxCommandEvent
& event
);
108 void OnCheckOrRadioBox(wxCommandEvent
& event
);
110 void OnUpdateUINumPagesText(wxUpdateUIEvent
& event
);
111 void OnUpdateUICurSelectText(wxUpdateUIEvent
& event
);
113 void OnUpdateUISelectButton(wxUpdateUIEvent
& event
);
114 void OnUpdateUIInsertButton(wxUpdateUIEvent
& event
);
115 void OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
);
117 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
119 // reset book parameters
124 virtual wxBookCtrlBase
*CreateBook(long flags
) = 0;
126 // create or destroy the image list
127 void CreateImageList();
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 // thei mage list for our book
159 wxImageList
*m_imageList
;
162 DECLARE_EVENT_TABLE()
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 BEGIN_EVENT_TABLE(BookWidgetsPage
, WidgetsPage
)
170 EVT_BUTTON(BookPage_Reset
, BookWidgetsPage::OnButtonReset
)
171 EVT_BUTTON(BookPage_SelectPage
, BookWidgetsPage::OnButtonSelectPage
)
172 EVT_BUTTON(BookPage_AddPage
, BookWidgetsPage::OnButtonAddPage
)
173 EVT_BUTTON(BookPage_InsertPage
, BookWidgetsPage::OnButtonInsertPage
)
174 EVT_BUTTON(BookPage_RemovePage
, BookWidgetsPage::OnButtonRemovePage
)
175 EVT_BUTTON(BookPage_DeleteAll
, BookWidgetsPage::OnButtonDeleteAll
)
177 EVT_UPDATE_UI(BookPage_NumPagesText
, BookWidgetsPage::OnUpdateUINumPagesText
)
178 EVT_UPDATE_UI(BookPage_CurSelectText
, BookWidgetsPage::OnUpdateUICurSelectText
)
180 EVT_UPDATE_UI(BookPage_SelectPage
, BookWidgetsPage::OnUpdateUISelectButton
)
181 EVT_UPDATE_UI(BookPage_InsertPage
, BookWidgetsPage::OnUpdateUIInsertButton
)
182 EVT_UPDATE_UI(BookPage_RemovePage
, BookWidgetsPage::OnUpdateUIRemoveButton
)
185 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY
, BookWidgetsPage::OnPageChanging
)
186 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, NotebookWidgetsPage::OnPageChanged
)
189 EVT_CHECKBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
190 EVT_RADIOBOX(wxID_ANY
, BookWidgetsPage::OnCheckOrRadioBox
)
193 // ============================================================================
195 // ============================================================================
197 BookWidgetsPage::BookWidgetsPage(WidgetsBookCtrl
*book
)
205 m_sizerBook
= (wxSizer
*)NULL
;
207 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
210 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
212 // must be in sync with Orient enum
213 wxArrayString orientations
;
214 orientations
.Add(_T("&top"));
215 orientations
.Add(_T("&bottom"));
216 orientations
.Add(_T("&left"));
217 orientations
.Add(_T("&right"));
219 wxASSERT_MSG( orientations
.GetCount() == Orient_Max
,
220 _T("forgot to update something") );
222 m_chkImages
= new wxCheckBox(this, wxID_ANY
, _T("Show &images"));
223 m_radioOrient
= new wxRadioBox(this, wxID_ANY
, _T("&Tab orientation"),
224 wxDefaultPosition
, wxDefaultSize
,
225 orientations
, 1, wxRA_SPECIFY_COLS
);
227 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
229 sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5);
230 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
231 sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5);
233 wxButton
*btn
= new wxButton(this, BookPage_Reset
, _T("&Reset"));
234 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
237 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Contents"));
238 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
241 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Number of pages: "),
242 BookPage_NumPagesText
,
244 text
->SetEditable(false);
245 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
247 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection: "),
248 BookPage_CurSelectText
,
250 text
->SetEditable(false);
251 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
253 sizerRow
= CreateSizerWithTextAndButton(BookPage_SelectPage
,
257 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
259 btn
= new wxButton(this, BookPage_AddPage
, _T("&Add page"));
260 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
262 sizerRow
= CreateSizerWithTextAndButton(BookPage_InsertPage
,
263 _T("&Insert page at"),
266 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
268 sizerRow
= CreateSizerWithTextAndButton(BookPage_RemovePage
,
272 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
274 btn
= new wxButton(this, BookPage_DeleteAll
, _T("&Delete All"));
275 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
278 m_sizerBook
= new wxBoxSizer(wxHORIZONTAL
);
280 // the 3 panes compose the window
281 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
282 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
283 sizerTop
->Add(m_sizerBook
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
285 // final initializations
294 BookWidgetsPage::~BookWidgetsPage()
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 void BookWidgetsPage::Reset()
305 m_chkImages
->SetValue(true);
306 m_radioOrient
->SetSelection(Orient_Top
);
309 void BookWidgetsPage::CreateImageList()
311 if ( m_chkImages
->GetValue() )
315 // create a dummy image list with a few icons
316 m_imageList
= new wxImageList(32, 32);
318 m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
319 m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
320 m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
321 m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
));
325 m_book
->SetImageList(m_imageList
);
336 // because of the bug in wxMSW we can't use SetImageList(NULL) - although
337 // it would be logical if this removed the image list from book, under
338 // MSW it crashes instead - FIXME
341 void BookWidgetsPage::RecreateBook()
343 int flags
= ms_defaultFlags
;
344 switch ( m_radioOrient
->GetSelection() )
347 wxFAIL_MSG( _T("unknown orientation") );
355 flags
|= wxBK_BOTTOM
;
367 wxBookCtrlBase
*oldBook
= m_book
;
369 m_book
= CreateBook(flags
);
375 const int sel
= oldBook
->GetSelection();
377 const int count
= oldBook
->GetPageCount();
379 // recreate the pages
380 for ( int n
= 0; n
< count
; n
++ )
382 m_book
->AddPage(CreateNewPage(),
383 oldBook
->GetPageText(n
),
385 m_chkImages
->GetValue() ?
386 GetIconIndex() : -1);
389 m_sizerBook
->Detach( oldBook
);
395 m_book
->SetSelection(sel
);
399 m_sizerBook
->Add(m_book
, 1, wxGROW
| wxALL
, 5);
400 m_sizerBook
->SetMinSize(150, 0);
401 m_sizerBook
->Layout();
404 // ----------------------------------------------------------------------------
406 // ----------------------------------------------------------------------------
408 int BookWidgetsPage::GetTextValue(wxTextCtrl
*text
) const
411 if ( !text
->GetValue().ToLong(&pos
) )
417 int BookWidgetsPage::GetIconIndex() const
421 int nImages
= m_imageList
->GetImageCount();
424 return m_book
->GetPageCount() % nImages
;
431 wxWindow
*BookWidgetsPage::CreateNewPage()
433 return new wxTextCtrl(m_book
, wxID_ANY
, _T("I'm a book page"));
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 void BookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
447 void BookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
))
449 m_book
->DeleteAllPages();
452 void BookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& WXUNUSED(event
))
454 int pos
= GetTextValue(m_textSelect
);
455 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
457 m_book
->SetSelection(pos
);
460 void BookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
))
462 m_book
->AddPage(CreateNewPage(), _T("Added page"), false,
466 void BookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
))
468 int pos
= GetTextValue(m_textInsert
);
469 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
471 m_book
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), false,
475 void BookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
))
477 int pos
= GetTextValue(m_textRemove
);
478 wxCHECK_RET( IsValidValue(pos
), _T("button should be disabled") );
480 m_book
->DeletePage(pos
);
483 void BookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
)
485 event
.Enable( IsValidValue(GetTextValue(m_textSelect
)) );
488 void BookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
)
490 event
.Enable( IsValidValue(GetTextValue(m_textInsert
)) );
493 void BookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
)
495 event
.Enable( IsValidValue(GetTextValue(m_textRemove
)) );
498 void BookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
500 event
.Enable( !m_chkImages
->GetValue() ||
501 m_radioOrient
->GetSelection() != wxBK_TOP
);
504 void BookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
)
506 event
.SetText( wxString::Format(_T("%d"), m_book
->GetPageCount()) );
509 void BookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
)
511 event
.SetText( wxString::Format(_T("%d"), m_book
->GetSelection()) );
514 void BookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
520 void BookWidgetsPage::OnPageChanging(wxBookEvent
& event
)
522 wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
523 event
.GetOldSelection(),
524 event
.GetSelection(),
525 m_notebook
->GetSelection());
530 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
)
532 wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
533 event
.GetOldSelection(),
534 event
.GetSelection(),
535 m_notebook
->GetSelection());
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
)
557 imaglist
->Add(wxBitmap(notebook_xpm
));
560 virtual ~NotebookWidgetsPage() {}
565 virtual wxBookCtrlBase
*CreateBook(long flags
)
567 return new wxNotebook(this, BookPage_Book
,
568 wxDefaultPosition
, wxDefaultSize
,
574 DECLARE_EVENT_TABLE()
575 DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
)
578 // ----------------------------------------------------------------------------
580 // ----------------------------------------------------------------------------
582 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, BookWidgetsPage
)
585 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook"),
586 (int)wxPlatform(GENERIC_CTRLS
).If(wxMSW
,NATIVE_CTRLS
)
587 .ElseIf(wxGTK
,NATIVE_CTRLS
)
591 #endif // wxUSE_NOTEBOOK
595 #include "icons/listbook.xpm"
596 #include "wx/listbook.h"
598 // ----------------------------------------------------------------------------
599 // ListbookWidgetsPage
600 // ----------------------------------------------------------------------------
602 class ListbookWidgetsPage
: public BookWidgetsPage
605 ListbookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
606 : BookWidgetsPage(book
)
608 imaglist
->Add(wxBitmap(listbook_xpm
));
611 virtual ~ListbookWidgetsPage() {}
616 virtual wxBookCtrlBase
*CreateBook(long flags
)
618 return new wxListbook(this, BookPage_Book
,
619 wxDefaultPosition
, wxDefaultSize
,
625 DECLARE_EVENT_TABLE()
626 DECLARE_WIDGETS_PAGE(ListbookWidgetsPage
)
629 // ----------------------------------------------------------------------------
631 // ----------------------------------------------------------------------------
633 BEGIN_EVENT_TABLE(ListbookWidgetsPage
, BookWidgetsPage
)
636 IMPLEMENT_WIDGETS_PAGE(ListbookWidgetsPage
, _T("Listbook"),
637 GENERIC_CTRLS
| BOOK_CTRLS
640 #endif // wxUSE_LISTBOOK
644 #include "icons/choicebk.xpm"
645 #include "wx/choicebk.h"
647 // ----------------------------------------------------------------------------
648 // ChoicebookWidgetsPage
649 // ----------------------------------------------------------------------------
651 class ChoicebookWidgetsPage
: public BookWidgetsPage
654 ChoicebookWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
655 : BookWidgetsPage(book
)
657 imaglist
->Add(wxBitmap(choicebk_xpm
));
660 virtual ~ChoicebookWidgetsPage() {}
665 virtual wxBookCtrlBase
*CreateBook(long flags
)
667 return new wxChoicebook(this, BookPage_Book
,
668 wxDefaultPosition
, wxDefaultSize
,
674 DECLARE_EVENT_TABLE()
675 DECLARE_WIDGETS_PAGE(ChoicebookWidgetsPage
)
678 // ----------------------------------------------------------------------------
680 // ----------------------------------------------------------------------------
682 BEGIN_EVENT_TABLE(ChoicebookWidgetsPage
, BookWidgetsPage
)
685 IMPLEMENT_WIDGETS_PAGE(ChoicebookWidgetsPage
, _T("Choicebook"),
686 GENERIC_CTRLS
| BOOK_CTRLS
689 #endif // wxUSE_CHOICEBOOK
691 #endif // wxUSE_BOOKCTRL