]>
git.saurik.com Git - wxWidgets.git/blob - samples/notebook/notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxNotebook demo
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
25 // If 1, use a dialog. Otherwise use a frame.
26 #define USE_TABBED_DIALOG 0
28 MyDialog
* dialog
= (MyDialog
*) NULL
;
29 #else // !USE_TABBED_DIALOG
30 MyFrame
* frame
= (MyFrame
*) NULL
;
31 #endif // USE_TABBED_DIALOG
37 // Create the main window
39 dialog
= new MyDialog((wxFrame
*) NULL
, -1, (char *) "Notebook", wxPoint(-1, -1), wxSize(365, 390), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
);
43 // Quit immediately the dialog has been dismissed
46 frame
= new MyFrame((wxFrame
*) NULL
, -1, "Notebook", wxPoint(-1, -1), wxSize(465, 390) );
48 // Problem with generic wxNotebook implementation whereby it doesn't size properly unless
49 // you set the size again
50 #if defined(__WIN16__) || defined(__WXMOTIF__)
52 frame
->GetSize(& width
, & height
);
53 frame
->SetSize(-1, -1, width
, height
);
60 void MyApp::InitTabView(wxNotebook
* notebook
, wxPanel
* window
)
62 m_okButton
= new wxButton(window
, wxID_OK
, "Close", wxPoint(-1, -1), wxSize(80, 25));
63 m_cancelButton
= new wxButton(window
, ID_DELETE_PAGE
, "&Delete page", wxPoint(-1, -1), wxSize(80, 25));
64 m_addPageButton
= new wxButton(window
, ID_ADD_PAGE
, "&Add page", wxPoint(-1, -1), wxSize(80, 25));
65 m_insertPageButton
= new wxButton(window
, ID_INSERT_PAGE
, "&Insert page", wxPoint(-1, -1), wxSize(80, 25));
66 m_nextPageButton
= new wxButton(window
, ID_NEXT_PAGE
, "&Next page", wxPoint(-1, -1), wxSize(80, 25));
67 m_okButton
->SetDefault();
69 wxLayoutConstraints
*c
;
70 c
= new wxLayoutConstraints
;
71 c
->right
.SameAs(window
, wxRight
, 4);
72 c
->bottom
.SameAs(window
, wxBottom
, 4);
75 m_addPageButton
->SetConstraints(c
);
77 c
= new wxLayoutConstraints
;
78 c
->right
.SameAs(m_addPageButton
, wxLeft
, 4);
79 c
->bottom
.SameAs(window
, wxBottom
, 4);
82 m_insertPageButton
->SetConstraints(c
);
84 c
= new wxLayoutConstraints
;
85 c
->right
.SameAs(m_insertPageButton
, wxLeft
, 4);
86 c
->bottom
.SameAs(window
, wxBottom
, 4);
89 m_nextPageButton
->SetConstraints(c
);
91 c
= new wxLayoutConstraints
;
92 c
->right
.SameAs(m_nextPageButton
, wxLeft
, 4);
93 c
->bottom
.SameAs(window
, wxBottom
, 4);
96 m_cancelButton
->SetConstraints(c
);
98 c
= new wxLayoutConstraints
;
99 c
->right
.SameAs(m_cancelButton
, wxLeft
, 4);
100 c
->bottom
.SameAs(window
, wxBottom
, 4);
103 m_okButton
->SetConstraints(c
);
106 wxPanel
*panel1
= new wxPanel(notebook
, -1);
107 // panel1->SetBackgroundColour(wxColour("RED"));
108 (void)new wxButton(panel1
, -1, "Press me", wxPoint(10, 10));
109 (void)new wxTextCtrl(panel1
, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
111 notebook
->AddPage(panel1
, "Cat", TRUE
);
113 wxPanel
*panel2
= new wxPanel(notebook
, -1);
114 panel2
->SetAutoLayout(TRUE
);
115 panel2
->SetBackgroundColour(wxColour("BLUE"));
117 wxString animals
[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
118 wxRadioBox
*radiobox
= new wxRadioBox(panel2
, -1, "Choose one",
119 wxDefaultPosition
, wxDefaultSize
, 5, animals
,
120 2, wxRA_SPECIFY_ROWS
);
122 c
= new wxLayoutConstraints
;
123 c
->left
.SameAs(panel2
, wxLeft
, 10);
124 c
->top
.SameAs(panel2
, wxTop
, 5);
125 c
->height
.PercentOf(panel2
, wxHeight
, 50);
126 c
->right
.SameAs(panel2
, wxRight
, 10);
127 radiobox
->SetConstraints(c
);
129 wxRadioBox
*radiobox2
= new wxRadioBox(panel2
, -1, "Choose one",
130 wxDefaultPosition
, wxDefaultSize
,
132 2, wxRA_SPECIFY_ROWS
);
134 c
= new wxLayoutConstraints
;
135 c
->left
.SameAs(radiobox
, wxLeft
);
137 c
->top
.Below(radiobox
, 5);
138 c
->right
.SameAs(radiobox
, wxRight
);
139 radiobox2
->SetConstraints(c
);
141 notebook
->AddPage(panel2
, "Dog");
143 wxPanel
*panel3
= new wxPanel(notebook
, -1);
144 panel3
->SetBackgroundColour(wxColour("WHITE"));
145 notebook
->AddPage(panel3
, "Goat");
147 wxPanel
*panel4
= new wxPanel(notebook
, -1);
148 panel4
->SetBackgroundColour(wxColour("YELLOW"));
149 notebook
->AddPage(panel4
, "Sheep");
151 wxPanel
*panel5
= new wxPanel(notebook
, -1);
152 panel5
->SetBackgroundColour(wxColour("MAGENTA"));
153 (void)new wxStaticText(panel5
, -1, "This page has been inserted, not added", wxPoint(10, 10) );
154 notebook
->InsertPage(0, panel5
, "Sheep");
156 notebook
->SetSelection(2);
159 #if USE_TABBED_DIALOG
161 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
162 EVT_BUTTON(wxID_OK
, MyDialog::OnOK
)
163 EVT_BUTTON(wxID_CANCEL
, MyDialog::OnOK
)
166 MyDialog::MyDialog(wxWindow
* parent
, const wxWindowID id
, const wxString
& title
,
167 const wxPoint
& pos
, const wxSize
& size
, const long windowStyle
):
168 wxDialog(parent
, id
, title
, pos
, size
, windowStyle
)
173 void MyDialog::OnOK(wxCommandEvent
& WXUNUSED(event
) )
178 void MyDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
) )
180 EndModal(wxID_CANCEL
);
183 void MyDialog::Init()
185 m_notebook
= new wxNotebook(this, ID_NOTEBOOK
);
187 wxLayoutConstraints
* c
= new wxLayoutConstraints
;
188 c
->left
.SameAs(this, wxLeft
, 4);
189 c
->right
.SameAs(this, wxRight
, 4);
190 c
->top
.SameAs(this, wxTop
, 4);
191 c
->bottom
.SameAs(this, wxBottom
, 40);
193 m_notebook
->SetConstraints(c
);
195 wxGetApp().InitTabView(m_notebook
, this);
203 #else // USE_TABBED_DIALOG
205 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
206 EVT_BUTTON(wxID_OK
, MyFrame::OnOK
)
207 EVT_BUTTON(ID_DELETE_PAGE
, MyFrame::OnDeletePage
)
208 EVT_BUTTON(ID_ADD_PAGE
, MyFrame::OnAddPage
)
209 EVT_BUTTON(ID_INSERT_PAGE
, MyFrame::OnInsertPage
)
210 EVT_BUTTON(ID_NEXT_PAGE
, MyFrame::OnNextPage
)
211 EVT_IDLE(MyFrame::OnIdle
)
214 MyFrame::MyFrame(wxFrame
* parent
, const wxWindowID id
, const wxString
& title
,
215 const wxPoint
& pos
, const wxSize
& size
, const long windowStyle
):
216 wxFrame(parent
, id
, title
, pos
, size
, windowStyle
)
218 m_panel
= (wxPanel
*) NULL
;
219 m_notebook
= (wxNotebook
*) NULL
;
223 void MyFrame::OnAddPage(wxCommandEvent
& WXUNUSED(event
))
225 static size_t s_pageAdded
= 0;
227 wxPanel
*panel
= new wxPanel( m_notebook
, -1 );
228 (void)new wxButton( panel
, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
230 m_notebook
->AddPage( panel
, wxString::Format("Added %u", ++s_pageAdded
) );
233 void MyFrame::OnInsertPage(wxCommandEvent
& WXUNUSED(event
))
235 static size_t s_pageIns
= 0;
237 wxPanel
*panel
= new wxPanel( m_notebook
, -1 );
238 (void)new wxButton( panel
, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
240 m_notebook
->InsertPage(0, panel
, wxString::Format("Inserted %u", ++s_pageIns
) );
241 m_notebook
->SetSelection(0);
244 wxWindow
*test
= NULL
;
246 void MyFrame::OnDeletePage(wxCommandEvent
& WXUNUSED(event
))
248 if (m_notebook
->GetPageCount() > 0)
249 m_notebook
->DeletePage( m_notebook
->GetPageCount()-1 );
254 m_notebook->AddPage( test, "Readded" );
259 test = m_notebook->GetPage( m_notebook->GetPageCount()-1 );
260 m_notebook->RemovePage( m_notebook->GetPageCount()-1 );
265 void MyFrame::OnNextPage(wxCommandEvent
& WXUNUSED(event
))
267 m_notebook
->AdvanceSelection();
270 void MyFrame::OnOK(wxCommandEvent
& WXUNUSED(event
) )
275 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
) )
282 m_panel
= new wxPanel(this, -1, wxDefaultPosition
, wxDefaultSize
, wxTAB_TRAVERSAL
|wxCLIP_CHILDREN
);
284 wxLayoutConstraints
* c
= new wxLayoutConstraints
;
285 c
->left
.SameAs(this, wxLeft
);
286 c
->right
.SameAs(this, wxRight
);
287 c
->top
.SameAs(this, wxTop
);
288 c
->bottom
.SameAs(this, wxBottom
);
289 m_panel
->SetConstraints(c
);
291 m_notebook
= new wxNotebook(m_panel
, ID_NOTEBOOK
);
293 c
= new wxLayoutConstraints
;
294 c
->left
.SameAs(m_panel
, wxLeft
, 4);
295 c
->right
.SameAs(m_panel
, wxRight
, 4);
296 c
->top
.SameAs(m_panel
, wxTop
, 4);
297 c
->bottom
.SameAs(m_panel
, wxBottom
, 40);
299 m_notebook
->SetConstraints(c
);
301 wxGetApp().InitTabView(m_notebook
, m_panel
);
303 m_panel
->SetAutoLayout(TRUE
);
311 void MyFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
313 static int s_nPages
= -1;
314 static int s_nSel
= -1;
316 int nPages
= m_notebook
->GetPageCount();
317 int nSel
= m_notebook
->GetSelection();
318 if ( nPages
!= s_nPages
|| nSel
!= s_nSel
)
324 title
.Printf("Notebook (%d pages, selection: %d)", nPages
, nSel
);
330 #endif // USE_TABBED_DIALOG