removed SetAutoLayout(true) calls when a corresponding SetSizer() was also called...
[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 wxWidgets 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 IMPLEMENT_APP(MyApp)
28
29 bool MyApp::OnInit()
30 {
31 // Create the main window
32 MyFrame *frame = new MyFrame( wxT("Notebook sample") );
33
34 // Problem with generic wxNotebook implementation whereby it doesn't size
35 // properly unless you set the size again
36 #if defined(__WXMOTIF__)
37 int width, height;
38 frame->GetSize(& width, & height);
39 frame->SetSize(wxDefaultPosition.x, wxDefaultPosition.y, width, height);
40 #endif
41
42 frame->Show();
43
44 return true;
45 }
46
47 MyNotebook::MyNotebook(wxWindow *parent, wxWindowID id,
48 const wxPoint& pos, const wxSize& size, long style)
49 : wxNotebook(parent, id, pos, size, style)
50 {
51 // Empty
52 }
53
54 wxPanel *MyNotebook::CreatePage(const wxString&pageName)
55 {
56 if
57 (
58 pageName.Contains(INSERTED_PAGE_NAME)
59 || pageName.Contains(ADDED_PAGE_NAME)
60 )
61 {
62 return CreateUserCreatedPage();
63 }
64
65 if (pageName == I_WAS_INSERTED_PAGE_NAME)
66 {
67 return CreateInsertPage();
68 }
69
70 if (pageName == VETO_PAGE_NAME)
71 {
72 return CreateVetoPage();
73 }
74
75 if (pageName == RADIOBUTTONS_PAGE_NAME)
76 {
77 return CreateRadioButtonsPage();
78 }
79
80
81 if (pageName == MAXIMIZED_BUTTON_PAGE_NAME)
82 {
83 return CreateBigButtonPage();
84 }
85
86 wxFAIL;
87
88 return (wxPanel *) NULL;
89 }
90
91 wxPanel *MyNotebook::CreateUserCreatedPage()
92 {
93 wxPanel *panel = new wxPanel(this);
94
95 (void) new wxButton( panel, wxID_ANY, wxT("Button"),
96 wxPoint(10, 10), wxDefaultSize );
97
98 return panel;
99 }
100
101 wxPanel *MyNotebook::CreateRadioButtonsPage()
102 {
103 wxPanel *panel = new wxPanel(this);
104
105 wxString animals[] = { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
106 wxT("Sabre-toothed tiger"), wxT("T Rex") };
107
108 wxRadioBox *radiobox1 = new wxRadioBox(panel, wxID_ANY, wxT("Choose one"),
109 wxDefaultPosition, wxDefaultSize, 5, animals, 2, wxRA_SPECIFY_ROWS);
110
111 wxString computers[] = { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
112 wxT("Another") };
113
114 wxRadioBox *radiobox2 = new wxRadioBox(panel, wxID_ANY,
115 wxT("Choose your favourite"), wxDefaultPosition, wxDefaultSize,
116 4, computers, 0, wxRA_SPECIFY_COLS);
117
118 wxBoxSizer *sizerPanel = new wxBoxSizer(wxVERTICAL);
119 sizerPanel->Add(radiobox1, 2, wxEXPAND);
120 sizerPanel->Add(radiobox2, 1, wxEXPAND);
121 panel->SetSizer(sizerPanel);
122
123 return panel;
124 }
125
126 wxPanel *MyNotebook::CreateVetoPage()
127 {
128 wxPanel *panel = new wxPanel(this);
129
130 (void) new wxStaticText( panel, wxID_ANY,
131 wxT("This page intentionally left blank"), wxPoint(10, 10) );
132
133 return panel;
134 }
135
136 wxPanel *MyNotebook::CreateBigButtonPage()
137 {
138 wxPanel *panel = new wxPanel(this);
139
140 wxButton *buttonBig = new wxButton(panel, wxID_ANY, wxT("Maximized button"));
141
142 wxBoxSizer *sizerPanel = new wxBoxSizer(wxVERTICAL);
143 sizerPanel->Add(buttonBig, 1, wxEXPAND);
144 panel->SetSizer(sizerPanel);
145
146 return panel;
147 }
148
149
150 wxPanel *MyNotebook::CreateInsertPage()
151 {
152 wxPanel *panel = new wxPanel(this);
153
154 panel->SetBackgroundColour( wxColour( wxT("MAROON") ) );
155 (void) new wxStaticText( panel, wxID_ANY,
156 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
157
158 return panel;
159 }
160
161 void MyNotebook::CreateInitialPages()
162 {
163 // Create and add some panels to the notebook
164
165 wxPanel *panel = CreateRadioButtonsPage();
166 AddPage( panel, RADIOBUTTONS_PAGE_NAME, false, GetIconIndex() );
167
168 panel = CreateVetoPage();
169 AddPage( panel, VETO_PAGE_NAME, false, GetIconIndex() );
170
171 panel = CreateBigButtonPage();
172 AddPage( panel, MAXIMIZED_BUTTON_PAGE_NAME, false, GetIconIndex() );
173
174 panel = CreateInsertPage();
175 InsertPage( 0, panel, I_WAS_INSERTED_PAGE_NAME, false, GetIconIndex() );
176
177
178 SetSelection(1);
179 }
180
181 int MyNotebook::GetIconIndex() const
182 {
183 if (m_imageList)
184 {
185 int nImages = m_imageList->GetImageCount();
186 if (nImages > 0)
187 {
188 return GetPageCount() % nImages;
189 }
190 }
191
192 return -1;
193 }
194
195 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
196 long style)
197 : wxFrame((wxWindow *) NULL, wxID_ANY, title, pos, size, style)
198 {
199 m_panel = (wxPanel *) NULL;
200 m_notebook = (MyNotebook *) NULL;
201
202 // create a dummy image list with a few icons
203 wxSize imageSize(32, 32);
204
205 m_imageList
206 = new wxImageList( imageSize.GetWidth(), imageSize.GetHeight() );
207
208 m_imageList->Add
209 (
210 wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, imageSize)
211 );
212
213 m_imageList->Add
214 (
215 wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, imageSize)
216 );
217
218 m_imageList->Add
219 (
220 wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, imageSize)
221 );
222
223 m_imageList->Add
224 (
225 wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, imageSize)
226 );
227
228 m_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
229 wxTAB_TRAVERSAL | wxCLIP_CHILDREN | wxNO_BORDER | wxNO_FULL_REPAINT_ON_RESIZE);
230
231 // Create remaining controls
232
233 // must be in sync with Orient enum
234 wxString strOrientations[] =
235 {
236 wxT("&Top"),
237 wxT("&Bottom"),
238 wxT("&Left"),
239 wxT("&Right"),
240 };
241
242 wxASSERT_MSG( WXSIZEOF(strOrientations) == ORIENT_MAX,
243 wxT("Forgot to update something") );
244
245 m_radioOrient = new wxRadioBox
246 (
247 m_panel, ID_RADIO_ORIENT,
248 wxT("&Tab orientation"),
249 wxDefaultPosition, wxDefaultSize,
250 WXSIZEOF(strOrientations), strOrientations,
251 1, wxRA_SPECIFY_COLS
252 );
253
254 m_chkShowImages = new wxCheckBox( m_panel, ID_CHK_SHOWIMAGES,
255 wxT("&Show images") );
256 #ifndef TEST_LISTBOOK
257 m_chkMultiLine = new wxCheckBox( m_panel, ID_CHK_MULTILINE,
258 wxT("&Multiple lines") );
259 #endif // !TEST_LISTBOOK
260
261 m_btnAddPage = new wxButton( m_panel, ID_BTN_ADD_PAGE, wxT("&Add page") );
262
263 m_btnInsertPage = new wxButton( m_panel, ID_BTN_INSERT_PAGE,
264 wxT("&Insert page") );
265
266 m_btnDeleteCurPage = new wxButton( m_panel, ID_BTN_DELETE_CUR_PAGE,
267 wxT("&Delete current page") );
268
269 m_btnDeleteLastPage = new wxButton( m_panel, ID_BTN_DELETE_LAST_PAGE,
270 wxT("Delete las&t page") );
271
272 m_btnNextPage = new wxButton( m_panel, ID_BTN_NEXT_PAGE,
273 wxT("&Next page") );
274
275 m_btnExit = new wxButton( m_panel, wxID_OK, wxT("&Exit") );
276 m_btnExit->SetDefault();
277
278 m_text = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
279 wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY);
280
281 m_logTargetOld = wxLog::SetActiveTarget( new wxLogTextCtrl(m_text) );
282
283 // Set sizers
284 m_sizerFrame = new wxBoxSizer(wxVERTICAL);
285
286 m_sizerTop = new wxBoxSizer(wxHORIZONTAL);
287
288 wxBoxSizer *sizerLeft = new wxBoxSizer(wxVERTICAL);
289 sizerLeft->Add(m_radioOrient, 0, wxEXPAND);
290 sizerLeft->Add(m_chkShowImages, 0, wxEXPAND | wxTOP, 4);
291 #ifndef TEST_LISTBOOK
292 sizerLeft->Add(m_chkMultiLine, 0, wxEXPAND | wxTOP, 4);
293 #endif // !TEST_LISTBOOK
294
295 sizerLeft->Add(0, 0, 1); // Spacer
296
297 sizerLeft->Add(m_btnAddPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
298 sizerLeft->Add(m_btnInsertPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
299 sizerLeft->Add(m_btnDeleteCurPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
300 sizerLeft->Add(m_btnDeleteLastPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
301 sizerLeft->Add(m_btnNextPage, 0, wxEXPAND | (wxTOP | wxBOTTOM), 4);
302
303 sizerLeft->Add(0, 0, 1); // Spacer
304
305 sizerLeft->Add(m_btnExit, 0, wxEXPAND);
306
307 m_sizerTop->Add(sizerLeft, 0, wxEXPAND | wxALL, 4);
308
309
310 m_sizerFrame->Add(m_sizerTop, 1, wxEXPAND);
311 m_sizerFrame->Add(m_text, 0, wxEXPAND);
312
313 ReInitNotebook();
314 m_notebook->CreateInitialPages();
315
316 m_panel->SetSizer(m_sizerFrame);
317
318 m_sizerFrame->Fit(this);
319 m_sizerFrame->SetSizeHints(this);
320
321 Centre(wxBOTH);
322
323 }
324
325 MyFrame::~MyFrame()
326 {
327 delete wxLog::SetActiveTarget(m_logTargetOld);
328
329 if (m_imageList)
330 {
331 delete m_imageList;
332 m_imageList = (wxImageList *) NULL;
333 }
334
335 }
336
337 void MyFrame::ReInitNotebook()
338 {
339 int flags;
340
341 switch ( m_radioOrient->GetSelection() )
342 {
343 default:
344 wxFAIL_MSG( wxT("unknown notebook orientation") );
345 // fall through
346
347 case ORIENT_TOP:
348 flags = wxNB_TOP;
349 break;
350
351 case ORIENT_BOTTOM:
352 flags = wxNB_BOTTOM;
353 break;
354
355 case ORIENT_LEFT:
356 flags = wxNB_LEFT;
357 break;
358
359 case ORIENT_RIGHT:
360 flags = wxNB_RIGHT;
361 break;
362 }
363
364 #ifndef TEST_LISTBOOK
365 if ( m_chkMultiLine->IsChecked() )
366 flags |= wxNB_MULTILINE;
367 #endif // !TEST_LISTBOOK
368
369 MyNotebook *notebook = m_notebook;
370
371 m_notebook = new MyNotebook(m_panel, ID_NOTEBOOK,
372 wxDefaultPosition, wxDefaultSize,
373 flags);
374
375 if ( m_chkShowImages->IsChecked() )
376 {
377 m_notebook->SetImageList(m_imageList);
378 }
379
380 if (notebook)
381 {
382 int sel = notebook->GetSelection();
383
384 int count = notebook->GetPageCount();
385 for (int n = 0; n < count; n++)
386 {
387 wxString str = notebook->GetPageText(n);
388
389 wxWindow *page = m_notebook->CreatePage(str);
390 m_notebook->AddPage(page, str, false, m_notebook->GetIconIndex() );
391 }
392
393 if (m_sizerNotebook)
394 {
395 m_sizerTop->Remove(m_sizerNotebook);
396 }
397
398 delete notebook;
399
400 // restore selection
401 if (sel != -1)
402 {
403 m_notebook->SetSelection(sel);
404 }
405
406 }
407
408
409 m_sizerNotebook = new wxBookCtrlSizer(m_notebook);
410 m_sizerTop->Add(m_sizerNotebook, 1, wxEXPAND | wxALL, 4);
411 m_sizerTop->Layout();
412 }
413
414 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
415 EVT_RADIOBOX(ID_RADIO_ORIENT, MyFrame::OnCheckOrRadioBox)
416 EVT_CHECKBOX(ID_CHK_SHOWIMAGES, MyFrame::OnCheckOrRadioBox)
417 #ifndef TEST_LISTBOOK
418 EVT_CHECKBOX(ID_CHK_MULTILINE, MyFrame::OnCheckOrRadioBox)
419 #endif // !TEST_LISTBOOK
420
421 EVT_BUTTON(ID_BTN_ADD_PAGE, MyFrame::OnButtonAddPage)
422 EVT_BUTTON(ID_BTN_INSERT_PAGE, MyFrame::OnButtonInsertPage)
423 EVT_BUTTON(ID_BTN_DELETE_CUR_PAGE, MyFrame::OnButtonDeleteCurPage)
424 EVT_BUTTON(ID_BTN_DELETE_LAST_PAGE, MyFrame::OnButtonDeleteLastPage)
425 EVT_BUTTON(ID_BTN_NEXT_PAGE, MyFrame::OnButtonNextPage)
426 EVT_BUTTON(wxID_OK, MyFrame::OnButtonExit)
427
428 EVT_UPDATE_UI(ID_BTN_DELETE_CUR_PAGE, MyFrame::OnUpdateUIBtnDeleteCurPage)
429 EVT_UPDATE_UI(ID_BTN_DELETE_LAST_PAGE, MyFrame::OnUpdateUIBtnDeleteLastPage)
430
431 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK, MyFrame::OnNotebook)
432 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK, MyFrame::OnNotebook)
433
434 EVT_IDLE(MyFrame::OnIdle)
435 END_EVENT_TABLE()
436
437 void MyFrame::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
438 {
439 ReInitNotebook();
440 }
441
442 void MyFrame::OnButtonAddPage( wxCommandEvent& WXUNUSED(event) )
443 {
444 static unsigned s_pageAdded = 0;
445
446 wxPanel *panel = new wxPanel( m_notebook, wxID_ANY );
447 (void) new wxButton( panel, wxID_ANY, wxT("First button"),
448 wxPoint(10, 10), wxDefaultSize );
449 (void) new wxButton( panel, wxID_ANY, wxT("Second button"),
450 wxPoint(50, 100), wxDefaultSize );
451
452 m_notebook->AddPage(panel, wxString::Format(ADDED_PAGE_NAME wxT("%u"),
453 ++s_pageAdded), true, m_notebook->GetIconIndex() );
454 }
455
456 void MyFrame::OnButtonInsertPage( wxCommandEvent& WXUNUSED(event) )
457 {
458 static unsigned s_pageIns = 0;
459
460 wxPanel *panel = m_notebook->CreateUserCreatedPage();
461
462 m_notebook->InsertPage( 0, panel,
463 wxString::Format(INSERTED_PAGE_NAME wxT("%u"), ++s_pageIns), false,
464 m_notebook->GetIconIndex() );
465
466 m_notebook->SetSelection(0);
467 }
468
469 void MyFrame::OnButtonDeleteLastPage( wxCommandEvent& WXUNUSED(event) )
470 {
471 int page = m_notebook->GetPageCount();
472
473 if ( page != 0 )
474 {
475 m_notebook->DeletePage(page - 1);
476 }
477 }
478
479 void MyFrame::OnButtonDeleteCurPage( wxCommandEvent& WXUNUSED(event) )
480 {
481 int sel = m_notebook->GetSelection();
482
483 if (sel != -1)
484 {
485 m_notebook->DeletePage(sel);
486 }
487 }
488
489 void MyFrame::OnButtonNextPage( wxCommandEvent& WXUNUSED(event) )
490 {
491 m_notebook->AdvanceSelection();
492 }
493
494 void MyFrame::OnButtonExit( wxCommandEvent& WXUNUSED(event) )
495 {
496 Close();
497 }
498
499 void MyFrame::OnNotebook(wxNotebookEvent& event)
500 {
501 wxString str = wxT("Unknown notebook event");
502
503 wxEventType eventType = event.GetEventType();
504
505 if (eventType == wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
506 {
507 str = wxT("Notebook changed");
508 }
509 else if (eventType == wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
510 {
511 int idx = event.GetOldSelection();
512 if ( idx != -1 && m_notebook->GetPageText(idx) == VETO_PAGE_NAME )
513 {
514 if
515 (
516 wxMessageBox(
517 wxT("Are you sure you want to leave this notebook page?\n")
518 wxT("(This demonstrates veto-ing)"),
519 wxT("Notebook sample"),
520 wxICON_QUESTION | wxYES_NO, this) != wxYES )
521 {
522 event.Veto();
523
524 return;
525 }
526
527 }
528
529 str = wxT("Notebook changing");
530 }
531
532 static int s_numNotebookEvents = 0;
533
534 wxLogMessage(wxT("Notebook event #%d: %s (%d)"),
535 s_numNotebookEvents++, str.c_str(), eventType);
536
537 m_text->SetInsertionPointEnd();
538
539 event.Skip();
540 }
541
542 void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
543 {
544 static int s_nPages = -1;
545 static int s_nSel = -1;
546
547 int nPages = m_notebook->GetPageCount();
548 int nSel = m_notebook->GetSelection();
549 if ( nPages != s_nPages || nSel != s_nSel )
550 {
551 s_nPages = nPages;
552 s_nSel = nSel;
553
554 wxString title;
555 title.Printf(wxT("Notebook (%d pages, selection: %d)"), nPages, nSel);
556
557 SetTitle(title);
558 }
559 }
560
561 void MyFrame::OnUpdateUIBtnDeleteCurPage(wxUpdateUIEvent& event)
562 {
563 event.Enable( m_notebook->GetSelection() != -1 );
564 }
565
566 void MyFrame::OnUpdateUIBtnDeleteLastPage(wxUpdateUIEvent& event)
567 {
568 event.Enable( m_notebook->GetPageCount() != 0 );
569 }