]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: test.cpp | |
3 | // Purpose: wxNotebook demo | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 26/10/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #endif | |
22 | ||
23 | #include "test.h" | |
24 | ||
25 | // If 1, use a dialog. Otherwise use a frame. | |
26 | #define USE_TABBED_DIALOG 0 | |
27 | #if USE_TABBED_DIALOG | |
28 | MyDialog* dialog = (MyDialog *) NULL; | |
29 | #else // !USE_TABBED_DIALOG | |
30 | MyFrame* frame = (MyFrame *) NULL; | |
31 | #endif // USE_TABBED_DIALOG | |
32 | ||
33 | IMPLEMENT_APP(MyApp) | |
34 | ||
35 | bool MyApp::OnInit(void) | |
36 | { | |
37 | // Create the main window | |
38 | #if USE_TABBED_DIALOG | |
39 | dialog = new MyDialog((wxFrame *) NULL, -1, (char *) "Notebook", wxPoint(-1, -1), wxSize(365, 390), wxDIALOG_MODAL|wxDEFAULT_DIALOG_STYLE); | |
40 | ||
41 | dialog->ShowModal(); | |
42 | ||
43 | // Quit immediately the dialog has been dismissed | |
44 | return FALSE; | |
45 | #else | |
46 | frame = new MyFrame((wxFrame*) NULL, -1, "Notebook", wxPoint(-1, -1), wxSize(365, 390) ); | |
47 | ||
48 | // Problem with generic wxNotebook implementation whereby it doesn't size properly unless | |
49 | // you set the size again | |
50 | #if defined(__WIN16__) | |
51 | int width, height; | |
52 | frame->GetSize(& width, & height); | |
53 | frame->SetSize(-1, -1, width, height); | |
54 | #endif | |
55 | ||
56 | return TRUE; | |
57 | #endif | |
58 | } | |
59 | ||
60 | void MyApp::InitTabView(wxNotebook* notebook, wxPanel* window) | |
61 | { | |
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_okButton->SetDefault(); | |
66 | ||
67 | wxLayoutConstraints* c = new wxLayoutConstraints; | |
68 | c->right.SameAs(window, wxRight, 4); | |
69 | c->bottom.SameAs(window, wxBottom, 4); | |
70 | c->height.AsIs(); | |
71 | c->width.AsIs(); | |
72 | m_addPageButton->SetConstraints(c); | |
73 | ||
74 | c = new wxLayoutConstraints; | |
75 | c->right.SameAs(m_addPageButton, wxLeft, 4); | |
76 | c->bottom.SameAs(window, wxBottom, 4); | |
77 | c->height.AsIs(); | |
78 | c->width.AsIs(); | |
79 | m_cancelButton->SetConstraints(c); | |
80 | ||
81 | c = new wxLayoutConstraints; | |
82 | c->right.SameAs(m_cancelButton, wxLeft, 4); | |
83 | c->bottom.SameAs(window, wxBottom, 4); | |
84 | c->height.AsIs(); | |
85 | c->width.AsIs(); | |
86 | m_okButton->SetConstraints(c); | |
87 | ||
88 | // Add some panels | |
89 | wxPanel *panel1 = new wxPanel(notebook, -1); | |
90 | // panel1->SetBackgroundColour(wxColour("RED")); | |
91 | (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10)); | |
92 | (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150)); | |
93 | ||
94 | notebook->AddPage(panel1, "Cat", TRUE); | |
95 | ||
96 | wxPanel *panel2 = new wxPanel(notebook, -1); | |
97 | panel2->SetAutoLayout(TRUE); | |
98 | panel2->SetBackgroundColour(wxColour("BLUE")); | |
99 | ||
100 | wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" }; | |
101 | wxRadioBox *radiobox = new wxRadioBox(panel2, -1, "Choose one", | |
102 | wxDefaultPosition, wxDefaultSize, 5, animals, | |
103 | 2, wxRA_SPECIFY_ROWS); | |
104 | ||
105 | c = new wxLayoutConstraints; | |
106 | c->left.SameAs(panel2, wxLeft, 10); | |
107 | c->top.SameAs(panel2, wxTop, 5); | |
108 | c->height.PercentOf(panel2, wxHeight, 50); | |
109 | c->right.SameAs(panel2, wxRight, 10); | |
110 | radiobox->SetConstraints(c); | |
111 | ||
112 | wxRadioBox *radiobox2 = new wxRadioBox(panel2, -1, "Choose one", | |
113 | wxDefaultPosition, wxDefaultSize, | |
114 | 5, animals, | |
115 | 2, wxRA_SPECIFY_ROWS); | |
116 | ||
117 | c = new wxLayoutConstraints; | |
118 | c->left.SameAs(radiobox, wxLeft); | |
119 | c->height.AsIs(); | |
120 | c->top.Below(radiobox, 5); | |
121 | c->right.SameAs(radiobox, wxRight); | |
122 | radiobox2->SetConstraints(c); | |
123 | ||
124 | notebook->AddPage(panel2, "Dog"); | |
125 | ||
126 | wxPanel *panel3 = new wxPanel(notebook, -1); | |
127 | panel3->SetBackgroundColour(wxColour("WHITE")); | |
128 | notebook->AddPage(panel3, "Goat"); | |
129 | ||
130 | wxPanel *panel4 = new wxPanel(notebook, -1); | |
131 | panel4->SetBackgroundColour(wxColour("YELLOW")); | |
132 | notebook->AddPage(panel4, "Sheep"); | |
133 | ||
134 | wxPanel *panel5 = new wxPanel(notebook, -1); | |
135 | panel5->SetBackgroundColour(wxColour("MAGENTA")); | |
136 | (void)new wxStaticText(panel5, -1, "This page has been inserted, not added", wxPoint(10, 10) ); | |
137 | notebook->InsertPage(0, panel5, "Sheep"); | |
138 | ||
139 | notebook->SetSelection(2); | |
140 | } | |
141 | ||
142 | #if USE_TABBED_DIALOG | |
143 | ||
144 | BEGIN_EVENT_TABLE(MyDialog, wxDialog) | |
145 | EVT_BUTTON(wxID_OK, MyDialog::OnOK) | |
146 | EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK) | |
147 | END_EVENT_TABLE() | |
148 | ||
149 | MyDialog::MyDialog(wxWindow* parent, const wxWindowID id, const wxString& title, | |
150 | const wxPoint& pos, const wxSize& size, const long windowStyle): | |
151 | wxDialog(parent, id, title, pos, size, windowStyle) | |
152 | { | |
153 | Init(); | |
154 | } | |
155 | ||
156 | void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) ) | |
157 | { | |
158 | EndModal(wxID_OK); | |
159 | } | |
160 | ||
161 | void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) ) | |
162 | { | |
163 | EndModal(wxID_CANCEL); | |
164 | } | |
165 | ||
166 | void MyDialog::Init(void) | |
167 | { | |
168 | m_notebook = new wxNotebook(this, ID_NOTEBOOK); | |
169 | ||
170 | wxLayoutConstraints* c = new wxLayoutConstraints; | |
171 | c->left.SameAs(this, wxLeft, 4); | |
172 | c->right.SameAs(this, wxRight, 4); | |
173 | c->top.SameAs(this, wxTop, 4); | |
174 | c->bottom.SameAs(this, wxBottom, 40); | |
175 | ||
176 | m_notebook->SetConstraints(c); | |
177 | ||
178 | wxGetApp().InitTabView(m_notebook, this); | |
179 | ||
180 | SetAutoLayout(TRUE); | |
181 | Layout(); | |
182 | ||
183 | Centre(wxBOTH); | |
184 | } | |
185 | ||
186 | #else // USE_TABBED_DIALOG | |
187 | ||
188 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
189 | EVT_BUTTON(wxID_OK, MyFrame::OnOK) | |
190 | EVT_BUTTON(ID_DELETE_PAGE, MyFrame::OnDeletePage) | |
191 | EVT_BUTTON(ID_ADD_PAGE, MyFrame::OnAddPage) | |
192 | EVT_IDLE(MyFrame::OnIdle) | |
193 | END_EVENT_TABLE() | |
194 | ||
195 | MyFrame::MyFrame(wxFrame* parent, const wxWindowID id, const wxString& title, | |
196 | const wxPoint& pos, const wxSize& size, const long windowStyle): | |
197 | wxFrame(parent, id, title, pos, size, windowStyle) | |
198 | { | |
199 | m_panel = (wxPanel*) NULL; | |
200 | m_notebook = (wxNotebook*) NULL; | |
201 | Init(); | |
202 | } | |
203 | ||
204 | void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event)) | |
205 | { | |
206 | wxPanel *panel = new wxPanel( m_notebook, -1 ); | |
207 | (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) ); | |
208 | m_notebook->AddPage( panel, "Added" ); | |
209 | // m_notebook->SetSelection( m_notebook->GetPageCount()-1 ); | |
210 | } | |
211 | ||
212 | void MyFrame::OnDeletePage(wxCommandEvent& WXUNUSED(event)) | |
213 | { | |
214 | m_notebook->DeletePage( m_notebook->GetPageCount()-1 ); | |
215 | } | |
216 | ||
217 | void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) ) | |
218 | { | |
219 | Destroy(); | |
220 | } | |
221 | ||
222 | void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) ) | |
223 | { | |
224 | Destroy(); | |
225 | } | |
226 | ||
227 | void MyFrame::Init(void) | |
228 | { | |
229 | m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxCLIP_CHILDREN); | |
230 | ||
231 | wxLayoutConstraints* c = new wxLayoutConstraints; | |
232 | c->left.SameAs(this, wxLeft); | |
233 | c->right.SameAs(this, wxRight); | |
234 | c->top.SameAs(this, wxTop); | |
235 | c->bottom.SameAs(this, wxBottom); | |
236 | m_panel->SetConstraints(c); | |
237 | ||
238 | // Note, omit the wxTAB_STYLE_COLOUR_INTERIOR, so we will guarantee a match | |
239 | // with the panel background, and save a bit of time. | |
240 | m_notebook = new wxNotebook(m_panel, ID_NOTEBOOK); | |
241 | ||
242 | c = new wxLayoutConstraints; | |
243 | c->left.SameAs(m_panel, wxLeft, 4); | |
244 | c->right.SameAs(m_panel, wxRight, 4); | |
245 | c->top.SameAs(m_panel, wxTop, 4); | |
246 | c->bottom.SameAs(m_panel, wxBottom, 40); | |
247 | ||
248 | m_notebook->SetConstraints(c); | |
249 | ||
250 | wxGetApp().InitTabView(m_notebook, m_panel); | |
251 | ||
252 | m_panel->SetAutoLayout(TRUE); | |
253 | SetAutoLayout(TRUE); | |
254 | ||
255 | Centre(wxBOTH); | |
256 | ||
257 | Show(TRUE); | |
258 | } | |
259 | ||
260 | void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event)) | |
261 | { | |
262 | static int s_nPages = -1; | |
263 | static int s_nSel = -1; | |
264 | ||
265 | int nPages = m_notebook->GetPageCount(); | |
266 | int nSel = m_notebook->GetSelection(); | |
267 | if ( nPages != s_nPages || nSel != s_nSel ) | |
268 | { | |
269 | s_nPages = nPages; | |
270 | s_nSel = nSel; | |
271 | ||
272 | wxString title; | |
273 | title.Printf("Notebook (%d pages, selection: %d)", nPages, nSel); | |
274 | ||
275 | SetTitle(title); | |
276 | } | |
277 | } | |
278 | ||
279 | #endif // USE_TABBED_DIALOG |