]>
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__)
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
;
71 c
= new wxLayoutConstraints
;
72 c
->right
.SameAs(window
, wxRight
, 4);
73 c
->bottom
.SameAs(window
, wxBottom
, 4);
76 m_addPageButton
->SetConstraints(c
);
78 c
= new wxLayoutConstraints
;
79 c
->right
.SameAs(m_addPageButton
, wxLeft
, 4);
80 c
->bottom
.SameAs(window
, wxBottom
, 4);
83 m_insertPageButton
->SetConstraints(c
);
85 c
= new wxLayoutConstraints
;
86 c
->right
.SameAs(m_insertPageButton
, wxLeft
, 4);
87 c
->bottom
.SameAs(window
, wxBottom
, 4);
90 m_nextPageButton
->SetConstraints(c
);
92 c
= new wxLayoutConstraints
;
93 c
->right
.SameAs(m_nextPageButton
, wxLeft
, 4);
94 c
->bottom
.SameAs(window
, wxBottom
, 4);
97 m_cancelButton
->SetConstraints(c
);
99 c
= new wxLayoutConstraints
;
100 c
->right
.SameAs(m_cancelButton
, wxLeft
, 4);
101 c
->bottom
.SameAs(window
, wxBottom
, 4);
104 m_okButton
->SetConstraints(c
);
107 wxPanel
*panel1
= new wxPanel(notebook
, -1);
108 // panel1->SetBackgroundColour(wxColour("RED"));
109 (void)new wxButton(panel1
, -1, "Press me", wxPoint(10, 10));
110 (void)new wxTextCtrl(panel1
, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
112 notebook
->AddPage(panel1
, "Cat", TRUE
);
114 wxPanel
*panel2
= new wxPanel(notebook
, -1);
115 panel2
->SetAutoLayout(TRUE
);
116 panel2
->SetBackgroundColour(wxColour("BLUE"));
118 wxString animals
[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
119 wxRadioBox
*radiobox
= new wxRadioBox(panel2
, -1, "Choose one",
120 wxDefaultPosition
, wxDefaultSize
, 5, animals
,
121 2, wxRA_SPECIFY_ROWS
);
123 c
= new wxLayoutConstraints
;
124 c
->left
.SameAs(panel2
, wxLeft
, 10);
125 c
->top
.SameAs(panel2
, wxTop
, 5);
126 c
->height
.PercentOf(panel2
, wxHeight
, 50);
127 c
->right
.SameAs(panel2
, wxRight
, 10);
128 radiobox
->SetConstraints(c
);
130 wxRadioBox
*radiobox2
= new wxRadioBox(panel2
, -1, "Choose one",
131 wxDefaultPosition
, wxDefaultSize
,
133 2, wxRA_SPECIFY_ROWS
);
135 c
= new wxLayoutConstraints
;
136 c
->left
.SameAs(radiobox
, wxLeft
);
138 c
->top
.Below(radiobox
, 5);
139 c
->right
.SameAs(radiobox
, wxRight
);
140 radiobox2
->SetConstraints(c
);
142 notebook
->AddPage(panel2
, "Dog");
144 wxPanel
*panel3
= new wxPanel(notebook
, -1);
145 panel3
->SetBackgroundColour(wxColour("WHITE"));
146 notebook
->AddPage(panel3
, "Goat");
148 wxPanel
*panel4
= new wxPanel(notebook
, -1);
149 panel4
->SetBackgroundColour(wxColour("YELLOW"));
150 notebook
->AddPage(panel4
, "Sheep");
152 wxPanel
*panel5
= new wxPanel(notebook
, -1);
153 panel5
->SetBackgroundColour(wxColour("MAGENTA"));
154 (void)new wxStaticText(panel5
, -1, "This page has been inserted, not added", wxPoint(10, 10) );
155 notebook
->InsertPage(0, panel5
, "Sheep");
157 notebook
->SetSelection(2);
160 #if USE_TABBED_DIALOG
162 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
163 EVT_BUTTON(wxID_OK
, MyDialog::OnOK
)
164 EVT_BUTTON(wxID_CANCEL
, MyDialog::OnOK
)
167 MyDialog::MyDialog(wxWindow
* parent
, const wxWindowID id
, const wxString
& title
,
168 const wxPoint
& pos
, const wxSize
& size
, const long windowStyle
):
169 wxDialog(parent
, id
, title
, pos
, size
, windowStyle
)
174 void MyDialog::OnOK(wxCommandEvent
& WXUNUSED(event
) )
179 void MyDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
) )
181 EndModal(wxID_CANCEL
);
184 void MyDialog::Init()
186 m_notebook
= new wxNotebook(this, ID_NOTEBOOK
);
188 wxLayoutConstraints
* c
= new wxLayoutConstraints
;
189 c
->left
.SameAs(this, wxLeft
, 4);
190 c
->right
.SameAs(this, wxRight
, 4);
191 c
->top
.SameAs(this, wxTop
, 4);
192 c
->bottom
.SameAs(this, wxBottom
, 40);
194 m_notebook
->SetConstraints(c
);
196 wxGetApp().InitTabView(m_notebook
, this);
204 #else // USE_TABBED_DIALOG
206 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
207 EVT_BUTTON(wxID_OK
, MyFrame::OnOK
)
208 EVT_BUTTON(ID_DELETE_PAGE
, MyFrame::OnDeletePage
)
209 EVT_BUTTON(ID_ADD_PAGE
, MyFrame::OnAddPage
)
210 EVT_BUTTON(ID_INSERT_PAGE
, MyFrame::OnInsertPage
)
211 EVT_BUTTON(ID_NEXT_PAGE
, MyFrame::OnNextPage
)
212 EVT_IDLE(MyFrame::OnIdle
)
215 MyFrame::MyFrame(wxFrame
* parent
, const wxWindowID id
, const wxString
& title
,
216 const wxPoint
& pos
, const wxSize
& size
, const long windowStyle
):
217 wxFrame(parent
, id
, title
, pos
, size
, windowStyle
)
219 m_panel
= (wxPanel
*) NULL
;
220 m_notebook
= (wxNotebook
*) NULL
;
224 void MyFrame::OnAddPage(wxCommandEvent
& WXUNUSED(event
))
226 static size_t s_pageAdded
= 0;
228 wxPanel
*panel
= new wxPanel( m_notebook
, -1 );
229 (void)new wxButton( panel
, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
231 m_notebook
->AddPage( panel
, wxString::Format("Added %u", ++s_pageAdded
) );
234 void MyFrame::OnInsertPage(wxCommandEvent
& WXUNUSED(event
))
236 static size_t s_pageIns
= 0;
238 wxPanel
*panel
= new wxPanel( m_notebook
, -1 );
239 (void)new wxButton( panel
, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
241 m_notebook
->InsertPage(0, panel
, wxString::Format("Inserted %u", ++s_pageIns
) );
242 m_notebook
->SetSelection(0);
245 void MyFrame::OnDeletePage(wxCommandEvent
& WXUNUSED(event
))
247 m_notebook
->DeletePage( m_notebook
->GetPageCount()-1 );
250 void MyFrame::OnNextPage(wxCommandEvent
& WXUNUSED(event
))
252 m_notebook
->AdvanceSelection();
255 void MyFrame::OnOK(wxCommandEvent
& WXUNUSED(event
) )
260 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
) )
267 m_panel
= new wxPanel(this, -1, wxDefaultPosition
, wxDefaultSize
, wxTAB_TRAVERSAL
|wxCLIP_CHILDREN
);
269 wxLayoutConstraints
* c
= new wxLayoutConstraints
;
270 c
->left
.SameAs(this, wxLeft
);
271 c
->right
.SameAs(this, wxRight
);
272 c
->top
.SameAs(this, wxTop
);
273 c
->bottom
.SameAs(this, wxBottom
);
274 m_panel
->SetConstraints(c
);
276 m_notebook
= new wxNotebook(m_panel
, ID_NOTEBOOK
);
278 c
= new wxLayoutConstraints
;
279 c
->left
.SameAs(m_panel
, wxLeft
, 4);
280 c
->right
.SameAs(m_panel
, wxRight
, 4);
281 c
->top
.SameAs(m_panel
, wxTop
, 4);
282 c
->bottom
.SameAs(m_panel
, wxBottom
, 40);
284 m_notebook
->SetConstraints(c
);
286 wxGetApp().InitTabView(m_notebook
, m_panel
);
288 m_panel
->SetAutoLayout(TRUE
);
296 void MyFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
298 static int s_nPages
= -1;
299 static int s_nSel
= -1;
301 int nPages
= m_notebook
->GetPageCount();
302 int nSel
= m_notebook
->GetSelection();
303 if ( nPages
!= s_nPages
|| nSel
!= s_nSel
)
309 title
.Printf("Notebook (%d pages, selection: %d)", nPages
, nSel
);
315 #endif // USE_TABBED_DIALOG