]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/notebook/notebook.cpp
fixed bug with the caret positioning after SetValue() introduced by the last commit
[wxWidgets.git] / samples / notebook / notebook.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: notebook.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// If 1, use a dialog. Otherwise use a frame.
24#define USE_TABBED_DIALOG 1
25
26#include "notebook.h"
27
28#if USE_TABBED_DIALOG
29MyDialog* dialog = (MyDialog *) NULL;
30#else // !USE_TABBED_DIALOG
31MyFrame* frame = (MyFrame *) NULL;
32#endif // USE_TABBED_DIALOG
33
34IMPLEMENT_APP(MyApp)
35
36bool MyApp::OnInit()
37{
38 // Create the main window
39#if USE_TABBED_DIALOG
40 dialog = new MyDialog((wxFrame *) NULL, -1, "Notebook", wxPoint(-1, -1), wxSize(365, 390),
41 wxRESIZE_BORDER);
42 dialog->ShowModal();
43 delete dialog;
44
45 // Quit immediately the dialog has been dismissed
46 return FALSE;
47#else
48 frame = new MyFrame((wxFrame*) NULL, -1, "Notebook", wxPoint(-1, -1), wxSize(465, 390) );
49
50 // Problem with generic wxNotebook implementation whereby it doesn't size properly unless
51 // you set the size again
52#if defined(__WIN16__) || defined(__WXMOTIF__)
53 int width, height;
54 frame->GetSize(& width, & height);
55 frame->SetSize(-1, -1, width, height);
56#endif
57
58 return TRUE;
59#endif
60}
61
62void MyApp::InitTabView(wxNotebook* notebook, wxWindow* window)
63{
64 m_okButton = new wxButton(window, wxID_OK, "Close", wxPoint(-1, -1), wxSize(80, 25));
65 m_cancelButton = new wxButton(window, ID_DELETE_PAGE, "&Delete page", wxPoint(-1, -1), wxSize(80, 25));
66 m_addPageButton = new wxButton(window, ID_ADD_PAGE, "&Add page", wxPoint(-1, -1), wxSize(80, 25));
67 m_insertPageButton = new wxButton(window, ID_INSERT_PAGE, "&Insert page", wxPoint(-1, -1), wxSize(80, 25));
68 m_nextPageButton = new wxButton(window, ID_NEXT_PAGE, "&Next page", wxPoint(-1, -1), wxSize(80, 25));
69 m_okButton->SetDefault();
70
71 wxLayoutConstraints *c;
72 c = new wxLayoutConstraints;
73 c->right.SameAs(window, wxRight, 4);
74 c->bottom.SameAs(window, wxBottom, 4);
75 c->height.AsIs();
76 c->width.AsIs();
77 m_addPageButton->SetConstraints(c);
78
79 c = new wxLayoutConstraints;
80 c->right.SameAs(m_addPageButton, wxLeft, 4);
81 c->bottom.SameAs(window, wxBottom, 4);
82 c->height.AsIs();
83 c->width.AsIs();
84 m_insertPageButton->SetConstraints(c);
85
86 c = new wxLayoutConstraints;
87 c->right.SameAs(m_insertPageButton, wxLeft, 4);
88 c->bottom.SameAs(window, wxBottom, 4);
89 c->height.AsIs();
90 c->width.AsIs();
91 m_nextPageButton->SetConstraints(c);
92
93 c = new wxLayoutConstraints;
94 c->right.SameAs(m_nextPageButton, wxLeft, 4);
95 c->bottom.SameAs(window, wxBottom, 4);
96 c->height.AsIs();
97 c->width.AsIs();
98 m_cancelButton->SetConstraints(c);
99
100 c = new wxLayoutConstraints;
101 c->right.SameAs(m_cancelButton, wxLeft, 4);
102 c->bottom.SameAs(window, wxBottom, 4);
103 c->height.AsIs();
104 c->width.AsIs();
105 m_okButton->SetConstraints(c);
106
107 // Add some panels
108 wxPanel *panel1 = new wxPanel(notebook, -1);
109 // panel1->SetBackgroundColour(wxColour("RED"));
110 (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10));
111 (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
112
113 notebook->AddPage(panel1, "Cat", TRUE);
114
115 wxPanel *panel2 = new wxPanel(notebook, -1);
116 panel2->SetAutoLayout(TRUE);
117 panel2->SetBackgroundColour(wxColour("BLUE"));
118
119 wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
120 wxRadioBox *radiobox = new wxRadioBox(panel2, -1, "Choose one",
121 wxDefaultPosition, wxDefaultSize, 5, animals,
122 2, wxRA_SPECIFY_ROWS);
123
124 c = new wxLayoutConstraints;
125 c->left.SameAs(panel2, wxLeft, 10);
126 c->top.SameAs(panel2, wxTop, 5);
127 c->height.PercentOf(panel2, wxHeight, 50);
128 c->right.SameAs(panel2, wxRight, 10);
129 radiobox->SetConstraints(c);
130
131 wxRadioBox *radiobox2 = new wxRadioBox(panel2, -1, "Choose one",
132 wxDefaultPosition, wxDefaultSize,
133 5, animals,
134 2, wxRA_SPECIFY_ROWS);
135
136 c = new wxLayoutConstraints;
137 c->left.SameAs(radiobox, wxLeft);
138 c->height.AsIs();
139 c->top.Below(radiobox, 5);
140 c->right.SameAs(radiobox, wxRight);
141 radiobox2->SetConstraints(c);
142
143 notebook->AddPage(panel2, "Dog");
144
145 wxPanel *panel3 = new wxPanel(notebook, -1);
146 panel3->SetBackgroundColour(wxColour("WHITE"));
147 notebook->AddPage(panel3, "Goat");
148
149 wxPanel *panel4 = new wxPanel(notebook, -1);
150 panel4->SetBackgroundColour(wxColour("YELLOW"));
151 notebook->AddPage(panel4, "Sheep");
152
153 wxPanel *panel5 = new wxPanel(notebook, -1);
154 panel5->SetBackgroundColour(wxColour("MAGENTA"));
155 (void)new wxStaticText(panel5, -1, "This page has been inserted, not added", wxPoint(10, 10) );
156 notebook->InsertPage(0, panel5, "Sheep");
157
158 notebook->SetSelection(2);
159}
160
161#if USE_TABBED_DIALOG
162
163BEGIN_EVENT_TABLE(MyDialog, wxDialog)
164 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
165 EVT_BUTTON(wxID_CANCEL, MyDialog::OnOK)
166END_EVENT_TABLE()
167
168MyDialog::MyDialog(wxWindow* parent, const wxWindowID id, const wxString& title,
169 const wxPoint& pos, const wxSize& size, const long windowStyle):
170 wxDialog(parent, id, title, pos, size, windowStyle)
171{
172 Init();
173}
174
175void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
176{
177 EndModal(wxID_OK);
178}
179
180void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
181{
182 EndModal(wxID_CANCEL);
183}
184
185void MyDialog::Init()
186{
187 m_notebook = new wxNotebook(this, ID_NOTEBOOK);
188
189 wxLayoutConstraints* c = new wxLayoutConstraints;
190 c->left.SameAs(this, wxLeft, 4);
191 c->right.SameAs(this, wxRight, 4);
192 c->top.SameAs(this, wxTop, 4);
193 c->bottom.SameAs(this, wxBottom, 40);
194
195 m_notebook->SetConstraints(c);
196
197 wxGetApp().InitTabView(m_notebook, this);
198
199 SetAutoLayout(TRUE);
200 Layout();
201
202 Centre(wxBOTH);
203}
204
205#else // USE_TABBED_DIALOG
206
207BEGIN_EVENT_TABLE(MyFrame, wxFrame)
208 EVT_BUTTON(wxID_OK, MyFrame::OnOK)
209 EVT_BUTTON(ID_DELETE_PAGE, MyFrame::OnDeletePage)
210 EVT_BUTTON(ID_ADD_PAGE, MyFrame::OnAddPage)
211 EVT_BUTTON(ID_INSERT_PAGE, MyFrame::OnInsertPage)
212 EVT_BUTTON(ID_NEXT_PAGE, MyFrame::OnNextPage)
213 EVT_IDLE(MyFrame::OnIdle)
214END_EVENT_TABLE()
215
216MyFrame::MyFrame(wxFrame* parent, const wxWindowID id, const wxString& title,
217 const wxPoint& pos, const wxSize& size, const long windowStyle):
218 wxFrame(parent, id, title, pos, size, windowStyle)
219{
220 m_panel = (wxPanel*) NULL;
221 m_notebook = (wxNotebook*) NULL;
222 Init();
223}
224
225void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
226{
227 static size_t s_pageAdded = 0;
228
229 wxPanel *panel = new wxPanel( m_notebook, -1 );
230 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
231
232 m_notebook->AddPage( panel, wxString::Format("Added %u", ++s_pageAdded) );
233}
234
235void MyFrame::OnInsertPage(wxCommandEvent& WXUNUSED(event))
236{
237 static size_t s_pageIns = 0;
238
239 wxPanel *panel = new wxPanel( m_notebook, -1 );
240 (void)new wxButton( panel, -1, "Button", wxPoint( 10,10 ), wxSize(-1,-1) );
241
242 m_notebook->InsertPage(0, panel, wxString::Format("Inserted %u", ++s_pageIns) );
243 m_notebook->SetSelection(0);
244}
245
246wxWindow *test = NULL;
247
248void MyFrame::OnDeletePage(wxCommandEvent& WXUNUSED(event))
249{
250 if (m_notebook->GetPageCount() > 0)
251 m_notebook->DeletePage( m_notebook->GetPageCount()-1 );
252
253/*
254 if (test)
255 {
256 m_notebook->AddPage( test, "Readded" );
257 test = NULL;
258 }
259 else
260 {
261 test = m_notebook->GetPage( m_notebook->GetPageCount()-1 );
262 m_notebook->RemovePage( m_notebook->GetPageCount()-1 );
263 }
264*/
265}
266
267void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event))
268{
269 m_notebook->AdvanceSelection();
270}
271
272void MyFrame::OnOK(wxCommandEvent& WXUNUSED(event) )
273{
274 Destroy();
275}
276
277void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
278{
279 Destroy();
280}
281
282void MyFrame::Init()
283{
284 m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxCLIP_CHILDREN);
285
286 wxLayoutConstraints* c = new wxLayoutConstraints;
287 c->left.SameAs(this, wxLeft);
288 c->right.SameAs(this, wxRight);
289 c->top.SameAs(this, wxTop);
290 c->bottom.SameAs(this, wxBottom);
291 m_panel->SetConstraints(c);
292
293 m_notebook = new wxNotebook(m_panel, ID_NOTEBOOK);
294
295 c = new wxLayoutConstraints;
296 c->left.SameAs(m_panel, wxLeft, 4);
297 c->right.SameAs(m_panel, wxRight, 4);
298 c->top.SameAs(m_panel, wxTop, 4);
299 c->bottom.SameAs(m_panel, wxBottom, 40);
300
301 m_notebook->SetConstraints(c);
302
303 wxGetApp().InitTabView(m_notebook, m_panel);
304
305 m_panel->SetAutoLayout(TRUE);
306 SetAutoLayout(TRUE);
307
308 Centre(wxBOTH);
309
310 Show(TRUE);
311}
312
313void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
314{
315 static int s_nPages = -1;
316 static int s_nSel = -1;
317
318 int nPages = m_notebook->GetPageCount();
319 int nSel = m_notebook->GetSelection();
320 if ( nPages != s_nPages || nSel != s_nSel )
321 {
322 s_nPages = nPages;
323 s_nSel = nSel;
324
325 wxString title;
326 title.Printf("Notebook (%d pages, selection: %d)", nPages, nSel);
327
328 SetTitle(title);
329 }
330}
331
332#endif // USE_TABBED_DIALOG