1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Program:     wxWindows Widgets Sample 
   4 // Purpose:     Part of the widgets sample showing wxNotebook 
   5 // Author:      Vadim Zeitlin 
   8 // Copyright:   (c) 2001 Vadim Zeitlin 
   9 // License:     wxWindows license 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // for compilers that support precompilation, includes "wx/wx.h". 
  21 #include "wx/wxprec.h" 
  27 // for all others, include the necessary headers 
  32     #include "wx/button.h" 
  33     #include "wx/checkbox.h" 
  34     #include "wx/combobox.h" 
  35     #include "wx/radiobox.h" 
  36     #include "wx/statbox.h" 
  37     #include "wx/textctrl.h" 
  39     #include "wx/dynarray.h" 
  43 #include "wx/notebook.h" 
  44 #include "wx/artprov.h" 
  48 #include "icons/notebook.xpm" 
  50 // ---------------------------------------------------------------------------- 
  52 // ---------------------------------------------------------------------------- 
  57     NotebookPage_Reset 
= 100, 
  58     NotebookPage_SelectPage
, 
  60     NotebookPage_InsertPage
, 
  61     NotebookPage_RemovePage
, 
  62     NotebookPage_DeleteAll
, 
  63     NotebookPage_InsertText
, 
  64     NotebookPage_RemoveText
, 
  65     NotebookPage_SelectText
, 
  66     NotebookPage_NumPagesText
, 
  67     NotebookPage_CurSelectText
, 
  71 // notebook orientations 
  81 // old versions of wxWindows don't define this style 
  86 // ---------------------------------------------------------------------------- 
  87 // NotebookWidgetsPage 
  88 // ---------------------------------------------------------------------------- 
  90 class NotebookWidgetsPage 
: public WidgetsPage
 
  93     NotebookWidgetsPage(wxNotebook 
*notebook
, wxImageList 
*imaglist
); 
  94     virtual ~NotebookWidgetsPage(); 
  98     void OnPageChanging(wxNotebookEvent
& event
); 
  99     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 the wxNotebook parameters 
 122     // (re)create the wxNotebook 
 123     void CreateNotebook(); 
 125     // create or destroy the image list 
 126     void CreateImageList(); 
 129     wxWindow 
*CreateNewPage(); 
 131     // get the image index for the new page 
 132     int GetIconIndex() const; 
 134     // get the numeric value of text ctrl 
 135     int GetTextValue(wxTextCtrl 
*text
) const; 
 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 notebook itself and the sizer it is in 
 150     wxNotebook 
*m_notebook
; 
 151     wxSizer 
*m_sizerNotebook
; 
 153     // thei mage list for our notebook 
 154     wxImageList 
*m_imageList
; 
 157     DECLARE_EVENT_TABLE() 
 158     DECLARE_WIDGETS_PAGE(NotebookWidgetsPage
) 
 161 // ---------------------------------------------------------------------------- 
 163 // ---------------------------------------------------------------------------- 
 165 BEGIN_EVENT_TABLE(NotebookWidgetsPage
, WidgetsPage
) 
 166     EVT_BUTTON(NotebookPage_Reset
, NotebookWidgetsPage::OnButtonReset
) 
 167     EVT_BUTTON(NotebookPage_SelectPage
, NotebookWidgetsPage::OnButtonSelectPage
) 
 168     EVT_BUTTON(NotebookPage_AddPage
, NotebookWidgetsPage::OnButtonAddPage
) 
 169     EVT_BUTTON(NotebookPage_InsertPage
, NotebookWidgetsPage::OnButtonInsertPage
) 
 170     EVT_BUTTON(NotebookPage_RemovePage
, NotebookWidgetsPage::OnButtonRemovePage
) 
 171     EVT_BUTTON(NotebookPage_DeleteAll
, NotebookWidgetsPage::OnButtonDeleteAll
) 
 173     EVT_UPDATE_UI(NotebookPage_NumPagesText
, NotebookWidgetsPage::OnUpdateUINumPagesText
) 
 174     EVT_UPDATE_UI(NotebookPage_CurSelectText
, NotebookWidgetsPage::OnUpdateUICurSelectText
) 
 176     EVT_UPDATE_UI(NotebookPage_SelectPage
, NotebookWidgetsPage::OnUpdateUISelectButton
) 
 177     EVT_UPDATE_UI(NotebookPage_InsertPage
, NotebookWidgetsPage::OnUpdateUIInsertButton
) 
 178     EVT_UPDATE_UI(NotebookPage_RemovePage
, NotebookWidgetsPage::OnUpdateUIRemoveButton
) 
 180     EVT_NOTEBOOK_PAGE_CHANGING(-1, NotebookWidgetsPage::OnPageChanging
) 
 181     EVT_NOTEBOOK_PAGE_CHANGED(-1, NotebookWidgetsPage::OnPageChanged
) 
 183     EVT_CHECKBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox
) 
 184     EVT_RADIOBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox
) 
 187 // ============================================================================ 
 189 // ============================================================================ 
 191 IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage
, _T("Notebook")); 
 193 NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook 
*notebook
, 
 194                                          wxImageList 
*imaglist
) 
 195                   : WidgetsPage(notebook
) 
 197     imaglist
->Add(wxBitmap(notebook_xpm
)); 
 203     m_notebook 
= (wxNotebook 
*)NULL
; 
 204     m_sizerNotebook 
= (wxSizer 
*)NULL
; 
 206     wxSizer 
*sizerTop 
= new wxBoxSizer(wxHORIZONTAL
); 
 209     wxStaticBox 
*box 
= new wxStaticBox(this, -1, _T("&Set style")); 
 211     // must be in sync with Orient enum 
 212     wxString orientations
[] = 
 220     wxASSERT_MSG( WXSIZEOF(orientations
) == Orient_Max
, 
 221                   _T("forgot to update something") ); 
 223     m_chkImages 
= new wxCheckBox(this, -1, _T("Show &images")); 
 224     m_radioOrient 
= new wxRadioBox(this, -1, _T("&Tab orientation"), 
 225                                    wxDefaultPosition
, wxDefaultSize
, 
 226                                    WXSIZEOF(orientations
), orientations
, 
 227                                    1, wxRA_SPECIFY_COLS
); 
 229     wxSizer 
*sizerLeft 
= new wxStaticBoxSizer(box
, wxVERTICAL
); 
 231     sizerLeft
->Add(m_chkImages
, 0, wxALL
, 5); 
 232     sizerLeft
->Add(5, 5, 0, wxGROW 
| wxALL
, 5); // spacer 
 233     sizerLeft
->Add(m_radioOrient
, 0, wxALL
, 5); 
 235     wxButton 
*btn 
= new wxButton(this, NotebookPage_Reset
, _T("&Reset")); 
 236     sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL 
| wxALL
, 15); 
 239     wxStaticBox 
*box2 
= new wxStaticBox(this, -1, _T("&Contents")); 
 240     wxSizer 
*sizerMiddle 
= new wxStaticBoxSizer(box2
, wxVERTICAL
); 
 243     wxSizer 
*sizerRow 
= CreateSizerWithTextAndLabel(_T("Number of pages: "), 
 244                                                     NotebookPage_NumPagesText
, 
 246     text
->SetEditable(FALSE
); 
 247     sizerMiddle
->Add(sizerRow
, 0, wxALL 
| wxGROW
, 5); 
 249     sizerRow 
= CreateSizerWithTextAndLabel(_T("Current selection: "), 
 250                                            NotebookPage_CurSelectText
, 
 252     text
->SetEditable(FALSE
); 
 253     sizerMiddle
->Add(sizerRow
, 0, wxALL 
| wxGROW
, 5); 
 255     sizerRow 
= CreateSizerWithTextAndButton(NotebookPage_SelectPage
, 
 257                                             NotebookPage_SelectText
, 
 259     sizerMiddle
->Add(sizerRow
, 0, wxALL 
| wxGROW
, 5); 
 261     btn 
= new wxButton(this, NotebookPage_AddPage
, _T("&Add page")); 
 262     sizerMiddle
->Add(btn
, 0, wxALL 
| wxGROW
, 5); 
 264     sizerRow 
= CreateSizerWithTextAndButton(NotebookPage_InsertPage
, 
 265                                             _T("&Insert page at"), 
 266                                             NotebookPage_InsertText
, 
 268     sizerMiddle
->Add(sizerRow
, 0, wxALL 
| wxGROW
, 5); 
 270     sizerRow 
= CreateSizerWithTextAndButton(NotebookPage_RemovePage
, 
 272                                             NotebookPage_RemoveText
, 
 274     sizerMiddle
->Add(sizerRow
, 0, wxALL 
| wxGROW
, 5); 
 276     btn 
= new wxButton(this, NotebookPage_DeleteAll
, _T("&Delete All")); 
 277     sizerMiddle
->Add(btn
, 0, wxALL 
| wxGROW
, 5); 
 280     wxSizer 
*sizerRight 
= new wxBoxSizer(wxHORIZONTAL
); 
 281     m_notebook 
= new wxNotebook(this, NotebookPage_Notebook
); 
 282     sizerRight
->Add(m_notebook
, 1, wxGROW 
| wxALL
, 5); 
 283     sizerRight
->SetMinSize(250, 0); 
 284     m_sizerNotebook 
= sizerRight
; // save it to modify it later 
 286     // the 3 panes panes compose the window 
 287     sizerTop
->Add(sizerLeft
, 0, wxGROW 
| (wxALL 
& ~wxLEFT
), 10); 
 288     sizerTop
->Add(sizerMiddle
, 0, wxGROW 
| wxALL
, 10); 
 289     sizerTop
->Add(sizerRight
, 1, wxGROW 
| (wxALL 
& ~wxRIGHT
), 10); 
 291     // final initializations 
 301 NotebookWidgetsPage::~NotebookWidgetsPage() 
 306 // ---------------------------------------------------------------------------- 
 308 // ---------------------------------------------------------------------------- 
 310 void NotebookWidgetsPage::Reset() 
 312     m_chkImages
->SetValue(TRUE
); 
 313     m_radioOrient
->SetSelection(Orient_Top
); 
 316 void NotebookWidgetsPage::CreateImageList() 
 318     if ( m_chkImages
->GetValue() ) 
 322             // create a dummy image list with a few icons 
 323             m_imageList 
= new wxImageList(32, 32); 
 325             m_imageList
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
)); 
 326             m_imageList
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
)); 
 327             m_imageList
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
)); 
 328             m_imageList
->Add(wxArtProvider::GetIcon(wxART_ERROR
, wxART_OTHER
, size
)); 
 331         m_notebook
->SetImageList(m_imageList
); 
 342     // because of the bug in wxMSW we can't use SetImageList(NULL) - although 
 343     // it would be logical if this removed the image list from notebook, under 
 344     // MSW it crashes instead 
 347 void NotebookWidgetsPage::CreateNotebook() 
 350     switch ( m_radioOrient
->GetSelection() ) 
 353             wxFAIL_MSG( _T("unknown notebok orientation") ); 
 373     wxNotebook 
*notebook 
= m_notebook
; 
 375     m_notebook 
= new wxNotebook(this, NotebookPage_Notebook
, 
 376                                 wxDefaultPosition
, wxDefaultSize
, 
 383         int sel 
= notebook
->GetSelection(); 
 385         int count 
= notebook
->GetPageCount(); 
 386         for ( int n 
= 0; n 
< count
; n
++ ) 
 388             wxNotebookPage 
*page 
= notebook
->GetPage(0); 
 389             page
->Reparent(m_notebook
); 
 391             m_notebook
->AddPage(page
, notebook
->GetPageText(0), FALSE
, 
 392                                 notebook
->GetPageImage(0)); 
 394             notebook
->RemovePage(0); 
 397         m_sizerNotebook
->Remove(notebook
); 
 403             m_notebook
->SetSelection(sel
); 
 407     m_sizerNotebook
->Add(m_notebook
, 1, wxGROW 
| wxALL
, 5); 
 408     m_sizerNotebook
->Layout(); 
 411 // ---------------------------------------------------------------------------- 
 413 // ---------------------------------------------------------------------------- 
 415 int NotebookWidgetsPage::GetTextValue(wxTextCtrl 
*text
) const 
 418     if ( !text
->GetValue().ToLong(&pos
) ) 
 424 int NotebookWidgetsPage::GetIconIndex() const 
 428        int nImages 
= m_imageList
->GetImageCount(); 
 431            return m_notebook
->GetPageCount() % nImages
; 
 438 wxWindow 
*NotebookWidgetsPage::CreateNewPage() 
 440     return new wxTextCtrl(m_notebook
, -1, _T("I'm a notebook page")); 
 443 // ---------------------------------------------------------------------------- 
 445 // ---------------------------------------------------------------------------- 
 447 void NotebookWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
)) 
 454 void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent
& WXUNUSED(event
)) 
 456     m_notebook
->DeleteAllPages(); 
 459 void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent
& event
) 
 461     int pos 
= GetTextValue(m_textSelect
); 
 462     wxCHECK_RET( pos 
>= 0, _T("button should be disabled") ); 
 464     m_notebook
->SetSelection(pos
); 
 467 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent
& WXUNUSED(event
)) 
 469     m_notebook
->AddPage(CreateNewPage(), _T("Added page"), FALSE
, 
 473 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent
& WXUNUSED(event
)) 
 475     int pos 
= GetTextValue(m_textInsert
); 
 476     wxCHECK_RET( pos 
>= 0, _T("button should be disabled") ); 
 478     m_notebook
->InsertPage(pos
, CreateNewPage(), _T("Inserted page"), FALSE
, 
 482 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent
& WXUNUSED(event
)) 
 484     int pos 
= GetTextValue(m_textRemove
); 
 485     wxCHECK_RET( pos 
>= 0, _T("button should be disabled") ); 
 487     m_notebook
->DeletePage(pos
); 
 490 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent
& event
) 
 492     event
.Enable( GetTextValue(m_textSelect
) >= 0 ); 
 495 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent
& event
) 
 497     event
.Enable( GetTextValue(m_textInsert
) >= 0 ); 
 500 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent
& event
) 
 502     event
.Enable( GetTextValue(m_textRemove
) >= 0 ); 
 505 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
) 
 507     event
.Enable( !m_chkImages
->GetValue() || 
 508                   m_radioOrient
->GetSelection() != wxNB_TOP 
); 
 511 void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent
& event
) 
 513     event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetPageCount()) ); 
 516 void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent
& event
) 
 518     event
.SetText( wxString::Format(_T("%d"), m_notebook
->GetSelection()) ); 
 521 void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
) 
 526 void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent
& event
) 
 528     wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."), 
 529                  event
.GetOldSelection(), 
 530                  event
.GetSelection(), 
 531                  m_notebook
->GetSelection()); 
 536 void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent
& event
) 
 538     wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."), 
 539                  event
.GetOldSelection(), 
 540                  event
.GetSelection(), 
 541                  m_notebook
->GetSelection());