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