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