]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/widgets/notebook.cpp
added GetCurrentPage()
[wxWidgets.git] / samples / widgets / notebook.cpp
index 6eb8b1ae6512f4aa80d1772a27eca73094515e27..68d6647d5d91b1ca32f308d1d9dc4a867ed0e85c 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Program:     wxWindows Widgets Sample
+// Program:     wxWidgets Widgets Sample
 // Name:        notebook.cpp
 // Purpose:     Part of the widgets sample showing wxNotebook
 // Author:      Vadim Zeitlin
@@ -78,7 +78,7 @@ enum Orient
     Orient_Max
 };
 
-// old versions of wxWindows don't define this style
+// old versions of wxWidgets don't define this style
 #ifndef wxNB_TOP
     #define wxNB_TOP (0)
 #endif
@@ -134,6 +134,10 @@ protected:
     // get the numeric value of text ctrl
     int GetTextValue(wxTextCtrl *text) const;
 
+    // is the value in range?
+    bool IsValidValue(int val) const
+        { return (val >= 0) && (val < (int) m_notebook->GetPageCount()); }
+
     // the controls
     // ------------
 
@@ -177,11 +181,11 @@ BEGIN_EVENT_TABLE(NotebookWidgetsPage, WidgetsPage)
     EVT_UPDATE_UI(NotebookPage_InsertPage, NotebookWidgetsPage::OnUpdateUIInsertButton)
     EVT_UPDATE_UI(NotebookPage_RemovePage, NotebookWidgetsPage::OnUpdateUIRemoveButton)
 
-    EVT_NOTEBOOK_PAGE_CHANGING(-1, NotebookWidgetsPage::OnPageChanging)
-    EVT_NOTEBOOK_PAGE_CHANGED(-1, NotebookWidgetsPage::OnPageChanged)
+    EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY, NotebookWidgetsPage::OnPageChanging)
+    EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, NotebookWidgetsPage::OnPageChanged)
 
-    EVT_CHECKBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox)
-    EVT_RADIOBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox)
+    EVT_CHECKBOX(wxID_ANY, NotebookWidgetsPage::OnCheckOrRadioBox)
+    EVT_RADIOBOX(wxID_ANY, NotebookWidgetsPage::OnCheckOrRadioBox)
 END_EVENT_TABLE()
 
 // ============================================================================
@@ -206,7 +210,7 @@ NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook *notebook,
     wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
 
     // left pane
-    wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
+    wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
 
     // must be in sync with Orient enum
     wxString orientations[] =
@@ -220,8 +224,8 @@ NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook *notebook,
     wxASSERT_MSG( WXSIZEOF(orientations) == Orient_Max,
                   _T("forgot to update something") );
 
-    m_chkImages = new wxCheckBox(this, -1, _T("Show &images"));
-    m_radioOrient = new wxRadioBox(this, -1, _T("&Tab orientation"),
+    m_chkImages = new wxCheckBox(this, wxID_ANY, _T("Show &images"));
+    m_radioOrient = new wxRadioBox(this, wxID_ANY, _T("&Tab orientation"),
                                    wxDefaultPosition, wxDefaultSize,
                                    WXSIZEOF(orientations), orientations,
                                    1, wxRA_SPECIFY_COLS);
@@ -236,20 +240,20 @@ NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook *notebook,
     sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
 
     // middle pane
-    wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Contents"));
+    wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Contents"));
     wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
 
     wxTextCtrl *text;
     wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Number of pages: "),
                                                     NotebookPage_NumPagesText,
                                                     &text);
-    text->SetEditable(FALSE);
+    text->SetEditable(false);
     sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
 
     sizerRow = CreateSizerWithTextAndLabel(_T("Current selection: "),
                                            NotebookPage_CurSelectText,
                                            &text);
-    text->SetEditable(FALSE);
+    text->SetEditable(false);
     sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
 
     sizerRow = CreateSizerWithTextAndButton(NotebookPage_SelectPage,
@@ -292,7 +296,6 @@ NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook *notebook,
     Reset();
     CreateImageList();
 
-    SetAutoLayout(TRUE);
     SetSizer(sizerTop);
 
     sizerTop->Fit(this);
@@ -309,7 +312,7 @@ NotebookWidgetsPage::~NotebookWidgetsPage()
 
 void NotebookWidgetsPage::Reset()
 {
-    m_chkImages->SetValue(TRUE);
+    m_chkImages->SetValue(true);
     m_radioOrient->SetSelection(Orient_Top);
 }
 
@@ -350,7 +353,7 @@ void NotebookWidgetsPage::CreateNotebook()
     switch ( m_radioOrient->GetSelection() )
     {
         default:
-            wxFAIL_MSG( _T("unknown notebok orientation") );
+            wxFAIL_MSG( _T("unknown notebook orientation") );
             // fall through
 
         case Orient_Top:
@@ -370,7 +373,7 @@ void NotebookWidgetsPage::CreateNotebook()
             break;
     }
 
-    wxNotebook *notebook = m_notebook;
+    wxNotebook *old_note = m_notebook;
 
     m_notebook = new wxNotebook(this, NotebookPage_Notebook,
                                 wxDefaultPosition, wxDefaultSize,
@@ -378,23 +381,24 @@ void NotebookWidgetsPage::CreateNotebook()
 
     CreateImageList();
 
-    if ( notebook )
+    if ( old_note )
     {
-        const int sel = notebook->GetSelection();
+        const int sel = old_note->GetSelection();
 
-        const int count = notebook->GetPageCount();
+        const int count = old_note->GetPageCount();
 
         // recreate the pages
         for ( int n = 0; n < count; n++ )
         {
             m_notebook->AddPage(CreateNewPage(),
-                                notebook->GetPageText(n),
-                                FALSE,
-                                notebook->GetPageImage(n));
+                                old_note->GetPageText(n),
+                                false,
+                                                                                                                               m_chkImages->GetValue() ?
+                                GetIconIndex() : -1);
         }
 
-        m_sizerNotebook->Remove(notebook);
-        delete notebook;
+        m_sizerNotebook->Detach( old_note );
+        delete old_note;
 
         // restore selection
         if ( sel != -1 )
@@ -436,7 +440,7 @@ int NotebookWidgetsPage::GetIconIndex() const
 
 wxWindow *NotebookWidgetsPage::CreateNewPage()
 {
-    return new wxTextCtrl(m_notebook, -1, _T("I'm a notebook page"));
+    return new wxTextCtrl(m_notebook, wxID_ANY, _T("I'm a notebook page"));
 }
 
 // ----------------------------------------------------------------------------
@@ -455,50 +459,50 @@ void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent& WXUNUSED(event))
     m_notebook->DeleteAllPages();
 }
 
-void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent& event)
+void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent& WXUNUSED(event))
 {
     int pos = GetTextValue(m_textSelect);
-    wxCHECK_RET( pos >= 0, _T("button should be disabled") );
+    wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
 
     m_notebook->SetSelection(pos);
 }
 
 void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent& WXUNUSED(event))
 {
-    m_notebook->AddPage(CreateNewPage(), _T("Added page"), FALSE,
+    m_notebook->AddPage(CreateNewPage(), _T("Added page"), false,
                         GetIconIndex());
 }
 
 void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent& WXUNUSED(event))
 {
     int pos = GetTextValue(m_textInsert);
-    wxCHECK_RET( pos >= 0, _T("button should be disabled") );
+    wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
 
-    m_notebook->InsertPage(pos, CreateNewPage(), _T("Inserted page"), FALSE,
+    m_notebook->InsertPage(pos, CreateNewPage(), _T("Inserted page"), false,
                            GetIconIndex());
 }
 
 void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent& WXUNUSED(event))
 {
     int pos = GetTextValue(m_textRemove);
-    wxCHECK_RET( pos >= 0, _T("button should be disabled") );
+    wxCHECK_RET( IsValidValue(pos), _T("button should be disabled") );
 
     m_notebook->DeletePage(pos);
 }
 
 void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
 {
-    event.Enable( GetTextValue(m_textSelect) >= 0 );
+    event.Enable( IsValidValue(GetTextValue(m_textSelect)) );
 }
 
 void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
 {
-    event.Enable( GetTextValue(m_textInsert) >= 0 );
+    event.Enable( IsValidValue(GetTextValue(m_textInsert)) );
 }
 
 void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
 {
-    event.Enable( GetTextValue(m_textRemove) >= 0 );
+    event.Enable( IsValidValue(GetTextValue(m_textRemove)) );
 }
 
 void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
@@ -517,7 +521,7 @@ void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent& event)
     event.SetText( wxString::Format(_T("%d"), m_notebook->GetSelection()) );
 }
 
-void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
+void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
 {
     CreateNotebook();
 }