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