Applied patch [ 581280 ] Revamped notebook sample
[wxWidgets.git] / samples / notebook / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/notebook/notebook.cpp
3 // Purpose: a sample demonstrating notebook usage
4 // Author: Julian Smart
5 // Modified by: Dimitri Schoolwerth
6 // Created: 26/10/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998-2002 wxWindows team
9 // License: wxWindows license
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 "wx/imaglist.h"
24 #include "wx/artprov.h"
25 #include "notebook.h"
26
27 // Name of the Veto page
28 #define VETO_PAGE_NAME wxT("Veto")
29
30 IMPLEMENT_APP(MyApp)
31
32 bool MyApp::OnInit()
33 {
34 // Create the main window
35 MyFrame *frame = new MyFrame( wxT("Notebook sample") );
36
37 // Problem with generic wxNotebook implementation whereby it doesn't size
38 // properly unless you set the size again
39 #if defined(__WIN16__) || defined(__WXMOTIF__)
40 int width, height;
41 frame->GetSize(& width, & height);
42 frame->SetSize(-1, -1, width, height);
43 #endif
44
45 frame->Show();
46
47 return TRUE;
48 }
49
50 MyNotebook::MyNotebook(wxWindow *parent, wxWindowID id,
51 const wxPoint& pos, const wxSize& size, long style)
52 : wxNotebook(parent, id, pos, size, style)
53 {
54 // Empty
55 }
56
57 void MyNotebook::CreateInitialPages()
58 {
59 wxPanel *panel = (wxPanel *) NULL;
60 wxBoxSizer *sizerPanel = (wxBoxSizer *) NULL;
61
62 // Create and add some panels to the notebook
63
64
65 // Panel 1
66 panel = new wxPanel(this);
67
68 sizerPanel = new wxBoxSizer(wxVERTICAL);
69 sizerPanel->Add( new wxButton( panel, -1, wxT("Button") ) );
70 sizerPanel->Add( new wxTextCtrl(panel, -1, wxT("1234"),
71 wxDefaultPosition, wxSize(120, 150) ) );
72 panel->SetSizer(sizerPanel);
73
74 AddPage( panel, wxT("Controls without sizer"), TRUE, GetIconIndex() );
75
76
77 // Panel 2
78 panel = new wxPanel(this);
79
80 wxString animals[] = { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
81 wxT("Sabre-toothed tiger"), wxT("T Rex") };
82 wxRadioBox *radiobox1 = new wxRadioBox(panel, -1, wxT("Choose one"),
83 wxDefaultPosition, wxDefaultSize, 5, animals, 2, wxRA_SPECIFY_ROWS);
84
85 wxString computers[] = { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
86 wxT("Another") };
87 wxRadioBox *radiobox2 = new wxRadioBox(panel, -1,
88 wxT("Choose your favourite"), wxDefaultPosition, wxDefaultSize,
89 4, computers, 0, wxRA_SPECIFY_COLS);
90
91 sizerPanel = new wxBoxSizer(wxVERTICAL);
92 sizerPanel->Add(radiobox1, 2, wxEXPAND);
93 sizerPanel->Add(radiobox2, 1, wxEXPAND);
94 panel->SetSizer(sizerPanel);
95
96 AddPage( panel, wxT("Radiobuttons"), FALSE, GetIconIndex() );
97
98
99 // Panel 3
100 panel = new wxPanel(this);
101 (void) new wxStaticText( panel, -1,
102 wxT("This page intentionally left blank"), wxPoint(10, 10) );
103 AddPage( panel, VETO_PAGE_NAME, FALSE, GetIconIndex() );
104
105
106 // Panel 4
107 panel = new wxPanel(this);
108 wxButton *buttonBig = new wxButton( panel, -1, wxT("Big button"),
109 wxPoint(0, 0), wxSize(480, 360) );
110
111 sizerPanel = new wxBoxSizer(wxVERTICAL);
112 sizerPanel->Add(buttonBig, 1, wxEXPAND);
113 panel->SetSizer(sizerPanel);
114
115 AddPage( panel, wxT("Big button"), FALSE, GetIconIndex() );
116
117
118 // Panel 5
119 panel = new wxPanel(this);
120 panel->SetBackgroundColour( wxColour( wxT("MAROON") ) );
121 (void) new wxStaticText( panel, -1,
122 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
123 InsertPage( 0, panel, wxT("Inserted"), FALSE, GetIconIndex() );
124
125
126 SetSelection(2);
127
128 }
129
130 int MyNotebook::GetIconIndex() const
131 {
132 if (m_imageList)
133 {
134 int nImages = m_imageList->GetImageCount();
135 if (nImages > 0)
136 {
137 return GetPageCount() % nImages;
138 }
139 }
140
141 return -1;
142 }
143
144 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
145 long style)
146 : wxFrame((wxWindow *) NULL, -1, title, pos, size, style)
147 {
148 m_panel = (wxPanel *) NULL;
149 m_notebook = (MyNotebook *) NULL;
150 m_imageList = (wxImageList *) NULL;
151
152 m_panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize,
153 wxTAB_TRAVERSAL|wxCLIP_CHILDREN|wxNO_BORDER);
154
155 // Create remaining controls
156
157 // must be in sync with Orient enum
158 wxString strOrientations[] =
159 {
160 wxT("&Top"),
161 wxT("&Bottom"),
162 wxT("&Left"),
163 wxT("&Right"),
164 };
165
166 wxASSERT_MSG( WXSIZEOF(strOrientations) == ORIENT_MAX,
167 wxT("forgot to update something") );
168
169 m_radioOrient = new wxRadioBox
170 (
171 m_panel, ID_RADIO_ORIENT,
172 wxT("&Tab orientation"),
173 wxDefaultPosition, wxDefaultSize,
174 WXSIZEOF(strOrientations), strOrientations,
175 1, wxRA_SPECIFY_COLS
176 );
177
178 m_chkShowImages = new wxCheckBox( m_panel, ID_CHK_SHOWIMAGES,
179 wxT("&Show images") );
180
181 m_btnAddPage = new wxButton( m_panel, ID_BTN_ADD_PAGE, wxT("&Add page") );
182
183 m_btnInsertPage = new wxButton( m_panel, ID_BTN_INSERT_PAGE,
184 wxT("&Insert page") );
185
186 m_btnDeletePage = new wxButton( m_panel, ID_BTN_DELETE_PAGE,
187 wxT("&Delete page") );
188
189 m_btnNextPage = new wxButton( m_panel, ID_BTN_NEXT_PAGE,
190 wxT("&Next page") );
191
192 m_btnExit = new wxButton( m_panel, wxID_OK, wxT("&Exit") );
193 m_btnExit->SetDefault();
194
195 m_notebook = new MyNotebook(m_panel, ID_NOTEBOOK);
196
197 m_text = new wxTextCtrl(m_panel, -1, wxEmptyString,
198 wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY);
199
200 m_logTargetOld = wxLog::SetActiveTarget( new wxLogTextCtrl(m_text) );
201
202 // Create the notebook's panels
203 m_notebook->CreateInitialPages();
204
205 // Set sizers
206 m_sizerFrame = new wxBoxSizer(wxVERTICAL);
207
208 m_sizerTop = new wxBoxSizer(wxHORIZONTAL);
209
210 wxBoxSizer *sizerLeft = new wxBoxSizer(wxVERTICAL);
211 {
212 sizerLeft->Add(m_radioOrient, 0, wxEXPAND);
213 sizerLeft->Add(m_chkShowImages, 0, wxEXPAND | wxTOP, 4);
214
215 sizerLeft->Add(0, 0, 1); // Spacer
216
217 sizerLeft->Add(m_btnAddPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
218 sizerLeft->Add(m_btnInsertPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
219 sizerLeft->Add(m_btnDeletePage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
220 sizerLeft->Add(m_btnNextPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
221
222 sizerLeft->Add(0, 0, 1); // Spacer
223
224 sizerLeft->Add(m_btnExit, 0, wxEXPAND);
225 }
226
227 m_sizerTop->Add(sizerLeft, 0, wxEXPAND | wxALL, 4);
228
229
230 m_sizerFrame->Add(m_sizerTop, 1, wxEXPAND);
231 m_sizerFrame->Add(m_text, 0, wxEXPAND);
232
233 ReInitNotebook();
234
235 m_panel->SetSizer(m_sizerFrame);
236
237 m_panel->SetAutoLayout(TRUE);
238
239 m_sizerFrame->Fit(this);
240
241 Centre(wxBOTH);
242
243 }
244
245 MyFrame::~MyFrame()
246 {
247 delete wxLog::SetActiveTarget(m_logTargetOld);
248 }
249
250 void MyFrame::ReInitNotebook()
251 {
252 int flags;
253
254 switch ( m_radioOrient->GetSelection() )
255 {
256 default:
257 wxFAIL_MSG( wxT("unknown notebook orientation") );
258 // fall through
259
260 case ORIENT_TOP:
261 flags = wxNB_TOP;
262 break;
263
264 case ORIENT_BOTTOM:
265 flags = wxNB_BOTTOM;
266 break;
267
268 case ORIENT_LEFT:
269 flags = wxNB_LEFT;
270 break;
271
272 case ORIENT_RIGHT:
273 flags = wxNB_RIGHT;
274 break;
275 }
276
277 MyNotebook *notebook = m_notebook;
278
279
280 m_notebook = new MyNotebook(m_panel, ID_NOTEBOOK,
281 wxDefaultPosition, wxDefaultSize,
282 flags);
283
284 CreateImageList();
285
286 if ( notebook )
287 {
288 int sel = notebook->GetSelection();
289
290 int count = notebook->GetPageCount();
291 for ( int n = 0; n < count; n++ )
292 {
293 wxNotebookPage *page = notebook->GetPage(0);
294 page->Reparent(m_notebook);
295
296 m_notebook->AddPage( page, notebook->GetPageText(0), FALSE,
297 m_notebook->GetIconIndex() );
298
299 notebook->RemovePage(0);
300 }
301
302 if (m_sizerNotebook)
303 {
304 m_sizerTop->Remove(m_sizerNotebook);
305 }
306
307 delete notebook;
308
309 // restore selection
310 if ( sel != -1 )
311 {
312 m_notebook->SetSelection(sel);
313 }
314 }
315
316 m_sizerNotebook = new wxNotebookSizer(m_notebook);
317 m_sizerTop->Add(m_sizerNotebook, 1, wxEXPAND | wxALL, 4);
318 m_sizerTop->Layout();
319 }
320
321 void MyFrame::CreateImageList()
322 {
323 if (m_imageList)
324 {
325 delete m_imageList;
326 m_imageList = (wxImageList *) NULL;
327 }
328
329 if ( m_chkShowImages->IsChecked() )
330 {
331 // create a dummy image list with a few icons
332 wxSize size(32, 32);
333
334 m_imageList = new wxImageList( size.GetWidth(), size.GetHeight() );
335
336 m_imageList->Add
337 (
338 wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, size)
339 );
340
341 m_imageList->Add
342 (
343 wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, size)
344 );
345
346 m_imageList->Add
347 (
348 wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, size)
349 );
350
351 m_imageList->Add
352 (
353 wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, size)
354 );
355
356 m_notebook->SetImageList(m_imageList);
357 }
358 }
359
360
361 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
362 EVT_RADIOBOX(ID_RADIO_ORIENT, MyFrame::OnCheckOrRadioBox)
363 EVT_CHECKBOX(ID_CHK_SHOWIMAGES, MyFrame::OnCheckOrRadioBox)
364
365 EVT_BUTTON(ID_BTN_ADD_PAGE, MyFrame::OnButtonAddPage)
366 EVT_BUTTON(ID_BTN_INSERT_PAGE, MyFrame::OnButtonInsertPage)
367 EVT_BUTTON(ID_BTN_DELETE_PAGE, MyFrame::OnButtonDeletePage)
368 EVT_BUTTON(ID_BTN_NEXT_PAGE, MyFrame::OnButtonNextPage)
369 EVT_BUTTON(wxID_OK, MyFrame::OnButtonExit)
370
371 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK, MyFrame::OnNotebook)
372 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK, MyFrame::OnNotebook)
373
374 EVT_IDLE(MyFrame::OnIdle)
375 END_EVENT_TABLE()
376
377 void MyFrame::OnCheckOrRadioBox(wxCommandEvent& event)
378 {
379 ReInitNotebook();
380 }
381
382 void MyFrame::OnButtonAddPage( wxCommandEvent& WXUNUSED(event) )
383 {
384 static size_t s_pageAdded = 0;
385
386 wxPanel *panel = new wxPanel( m_notebook, -1 );
387 (void) new wxButton( panel, -1, wxT("Button"),
388 wxPoint(10, 10), wxSize(-1, -1) );
389
390 m_notebook->AddPage(panel, wxString::Format(wxT("Added %u"),
391 ++s_pageAdded), FALSE, m_notebook->GetIconIndex() );
392 }
393
394 void MyFrame::OnButtonInsertPage( wxCommandEvent& WXUNUSED(event) )
395 {
396 static size_t s_pageIns = 0;
397
398 wxPanel *panel = new wxPanel( m_notebook, -1 );
399 (void) new wxButton( panel, -1, wxT("Button"),
400 wxPoint(10, 10), wxSize(-1, -1) );
401
402 m_notebook->InsertPage( 0, panel,
403 wxString::Format(wxT("Inserted %u"), ++s_pageIns), FALSE,
404 m_notebook->GetIconIndex() );
405
406 m_notebook->SetSelection(0);
407 }
408
409 void MyFrame::OnButtonDeletePage( wxCommandEvent& WXUNUSED(event) )
410 {
411 int sel = m_notebook->GetSelection();
412
413 if (sel != -1)
414 {
415 m_notebook->DeletePage(sel);
416 }
417 }
418
419 void MyFrame::OnButtonNextPage( wxCommandEvent& WXUNUSED(event) )
420 {
421 m_notebook->AdvanceSelection();
422 }
423
424 void MyFrame::OnButtonExit( wxCommandEvent& WXUNUSED(event) )
425 {
426 Close();
427 }
428
429 void MyFrame::OnNotebook(wxNotebookEvent& event)
430 {
431 wxString str = wxT("Unknown notebook event");
432
433 wxEventType eventType = event.GetEventType();
434
435 if (eventType == wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
436 {
437 str = wxT("Notebook changed");
438 }
439 else if (eventType == wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
440 {
441 int idx = event.GetOldSelection();
442 if ( idx != -1 && m_notebook->GetPageText(idx) == VETO_PAGE_NAME )
443 {
444 if
445 (
446 wxMessageBox(
447 wxT("Are you sure you want to leave this notebook page?\n")
448 wxT("(This demonstrates veto-ing)"),
449 wxT("Notebook sample"),
450 wxICON_QUESTION | wxYES_NO, this) != wxYES )
451 {
452 event.Veto();
453
454 return;
455 }
456
457 }
458
459 str = wxT("Notebook changing");
460 }
461
462 static int s_numNotebookEvents = 0;
463
464 wxLogMessage(wxT("Notebook event #%d: %s (%d)"),
465 s_numNotebookEvents++, str, eventType);
466
467 m_text->SetInsertionPointEnd();
468
469 event.Skip();
470 }
471
472 void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
473 {
474 static int s_nPages = -1;
475 static int s_nSel = -1;
476
477 int nPages = m_notebook->GetPageCount();
478 int nSel = m_notebook->GetSelection();
479 if ( nPages != s_nPages || nSel != s_nSel )
480 {
481 s_nPages = nPages;
482 s_nSel = nSel;
483
484 wxString title;
485 title.Printf(wxT("Notebook (%d pages, selection: %d)"), nPages, nSel);
486
487 SetTitle(title);
488 }
489 }