]> git.saurik.com Git - wxWidgets.git/blame - samples/notebook/notebook.cpp
Compile fix for VC
[wxWidgets.git] / samples / notebook / notebook.cpp
CommitLineData
0b4d4194 1/////////////////////////////////////////////////////////////////////////////
c1dfe277
JS
2// Name: samples/notebook/notebook.cpp
3// Purpose: a sample demonstrating notebook usage
0b4d4194 4// Author: Julian Smart
c1dfe277 5// Modified by: Dimitri Schoolwerth
0b4d4194
JS
6// Created: 26/10/98
7// RCS-ID: $Id$
be5a51fb 8// Copyright: (c) 1998-2002 wxWidgets team
c1dfe277 9// License: wxWindows license
0b4d4194
JS
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
d22699b5 16 #pragma hdrstop
0b4d4194
JS
17#endif
18
19#ifndef WX_PRECOMP
d22699b5 20 #include "wx/wx.h"
0b4d4194
JS
21#endif
22
c1dfe277
JS
23#include "wx/imaglist.h"
24#include "wx/artprov.h"
b2f757f9 25#include "notebook.h"
0b4d4194 26
d52f6c4e
VZ
27#if !defined(__WXMSW__) && !defined(__WXPM__)
28 #include "../sample.xpm"
29#endif
30
0b4d4194
JS
31IMPLEMENT_APP(MyApp)
32
477350ce 33bool MyApp::OnInit()
0b4d4194 34{
c1dfe277 35 // Create the main window
eca15c0d 36 MyFrame *frame = new MyFrame();
c1dfe277
JS
37
38 // Problem with generic wxNotebook implementation whereby it doesn't size
39 // properly unless you set the size again
3a5bcc4d 40#if defined(__WXMOTIF__)
c1dfe277
JS
41 int width, height;
42 frame->GetSize(& width, & height);
422d0ff0 43 frame->SetSize(wxDefaultCoord, wxDefaultCoord, width, height);
5dcf05ae
JS
44#endif
45
c1dfe277
JS
46 frame->Show();
47
1cfac5b8 48 return true;
0b4d4194
JS
49}
50
61c083e7 51wxPanel *CreateUserCreatedPage(wxBookCtrlBase *parent)
0b4d4194 52{
9133965e 53 wxPanel *panel = new wxPanel(parent);
76645ab3 54
1cfac5b8
WS
55 (void) new wxButton( panel, wxID_ANY, wxT("Button"),
56 wxPoint(10, 10), wxDefaultSize );
76645ab3
VZ
57
58 return panel;
59}
60
61c083e7 61wxPanel *CreateRadioButtonsPage(wxBookCtrlBase *parent)
76645ab3 62{
9133965e 63 wxPanel *panel = new wxPanel(parent);
c1dfe277
JS
64
65 wxString animals[] = { wxT("Fox"), wxT("Hare"), wxT("Rabbit"),
66 wxT("Sabre-toothed tiger"), wxT("T Rex") };
76645ab3 67
1cfac5b8 68 wxRadioBox *radiobox1 = new wxRadioBox(panel, wxID_ANY, wxT("Choose one"),
c1dfe277
JS
69 wxDefaultPosition, wxDefaultSize, 5, animals, 2, wxRA_SPECIFY_ROWS);
70
71 wxString computers[] = { wxT("Amiga"), wxT("Commodore 64"), wxT("PET"),
72 wxT("Another") };
76645ab3 73
1cfac5b8 74 wxRadioBox *radiobox2 = new wxRadioBox(panel, wxID_ANY,
c1dfe277
JS
75 wxT("Choose your favourite"), wxDefaultPosition, wxDefaultSize,
76 4, computers, 0, wxRA_SPECIFY_COLS);
0b4d4194 77
76645ab3 78 wxBoxSizer *sizerPanel = new wxBoxSizer(wxVERTICAL);
c1dfe277
JS
79 sizerPanel->Add(radiobox1, 2, wxEXPAND);
80 sizerPanel->Add(radiobox2, 1, wxEXPAND);
81 panel->SetSizer(sizerPanel);
82
76645ab3
VZ
83 return panel;
84}
c1dfe277 85
61c083e7 86wxPanel *CreateVetoPage(wxBookCtrlBase *parent)
76645ab3 87{
9133965e 88 wxPanel *panel = new wxPanel(parent);
c1dfe277 89
1cfac5b8 90 (void) new wxStaticText( panel, wxID_ANY,
c1dfe277 91 wxT("This page intentionally left blank"), wxPoint(10, 10) );
c1dfe277 92
76645ab3
VZ
93 return panel;
94}
95
61c083e7 96wxPanel *CreateBigButtonPage(wxBookCtrlBase *parent)
76645ab3 97{
9133965e 98 wxPanel *panel = new wxPanel(parent);
c1dfe277 99
ed420f6d 100 wxButton *buttonBig = new wxButton(panel, wxID_ANY, wxT("Maximized button"));
c1dfe277 101
76645ab3 102 wxBoxSizer *sizerPanel = new wxBoxSizer(wxVERTICAL);
c1dfe277
JS
103 sizerPanel->Add(buttonBig, 1, wxEXPAND);
104 panel->SetSizer(sizerPanel);
105
76645ab3
VZ
106 return panel;
107}
108
c1dfe277 109
61c083e7 110wxPanel *CreateInsertPage(wxBookCtrlBase *parent)
76645ab3 111{
9133965e 112 wxPanel *panel = new wxPanel(parent);
c1dfe277 113
c1dfe277 114 panel->SetBackgroundColour( wxColour( wxT("MAROON") ) );
1cfac5b8 115 (void) new wxStaticText( panel, wxID_ANY,
c1dfe277 116 wxT("This page has been inserted, not added."), wxPoint(10, 10) );
c1dfe277 117
76645ab3
VZ
118 return panel;
119}
120
61c083e7 121int GetIconIndex(wxBookCtrlBase* bookCtrl)
76645ab3 122{
9133965e
WS
123 if (bookCtrl && bookCtrl->GetImageList())
124 {
125 int nImages = bookCtrl->GetImageList()->GetImageCount();
126 if (nImages > 0)
127 {
128 return bookCtrl->GetPageCount() % nImages;
129 }
130 }
76645ab3 131
9133965e
WS
132 return -1;
133}
76645ab3 134
61c083e7 135void CreateInitialPages(wxBookCtrlBase *parent)
9133965e
WS
136{
137 // Create and add some panels to the notebook
138
139 wxPanel *panel = CreateRadioButtonsPage(parent);
140 parent->AddPage( panel, RADIOBUTTONS_PAGE_NAME, false, GetIconIndex(parent) );
c1dfe277 141
9133965e
WS
142 panel = CreateVetoPage(parent);
143 parent->AddPage( panel, VETO_PAGE_NAME, false, GetIconIndex(parent) );
c1dfe277 144
9133965e
WS
145 panel = CreateBigButtonPage(parent);
146 parent->AddPage( panel, MAXIMIZED_BUTTON_PAGE_NAME, false, GetIconIndex(parent) );
76645ab3 147
9133965e
WS
148 panel = CreateInsertPage(parent);
149 parent->InsertPage( 0, panel, I_WAS_INSERTED_PAGE_NAME, false, GetIconIndex(parent) );
76645ab3 150
9133965e 151 parent->SetSelection(1);
c1dfe277
JS
152}
153
61c083e7 154wxPanel *CreatePage(wxBookCtrlBase *parent, const wxString&pageName)
0b4d4194 155{
eca15c0d
VZ
156 if ( pageName.Contains(INSERTED_PAGE_NAME) ||
157 pageName.Contains(ADDED_PAGE_NAME) ||
158 pageName.Contains(ADDED_SUB_PAGE_NAME) ||
159 pageName.Contains(ADDED_PAGE_NAME_BEFORE) )
9133965e 160 return CreateUserCreatedPage(parent);
c1dfe277 161
eca15c0d 162 if ( pageName == I_WAS_INSERTED_PAGE_NAME )
9133965e 163 return CreateInsertPage(parent);
9133965e 164
eca15c0d 165 if ( pageName == VETO_PAGE_NAME )
9133965e 166 return CreateVetoPage(parent);
9133965e 167
eca15c0d 168 if ( pageName == RADIOBUTTONS_PAGE_NAME )
9133965e 169 return CreateRadioButtonsPage(parent);
9133965e 170
eca15c0d 171 if ( pageName == MAXIMIZED_BUTTON_PAGE_NAME )
9133965e 172 return CreateBigButtonPage(parent);
9133965e 173
eca15c0d 174 wxFAIL_MSG( _T("unknown page name") );
9133965e 175
eca15c0d 176 return NULL;
0b4d4194
JS
177}
178
eca15c0d
VZ
179MyFrame::MyFrame()
180 : wxFrame(NULL, wxID_ANY, wxString(wxT("wxWidgets book controls sample")))
0b4d4194 181{
53d09d55 182#if wxUSE_NOTEBOOK
eca15c0d 183 m_type = Type_Notebook;
53d09d55 184#elif wxUSE_CHOICEBOOK
eca15c0d 185 m_type = Type_Choicebook;
53d09d55 186#elif wxUSE_LISTBOOK
eca15c0d
VZ
187 m_type = Type_Listbook;
188#elif wxUSE_TREEBOOK
189 m_type = Type_Treebook;
53d09d55
WS
190#elif
191 #error "Don't use Notebook sample without any book enabled in wxWidgets build!"
192#endif
193
9133965e
WS
194 m_orient = ID_ORIENT_DEFAULT;
195 m_chkShowImages = true;
196 m_multi = false;
197
d52f6c4e 198 SetIcon(wxICON(sample));
9133965e 199
d52f6c4e 200 // menu of the sample
9133965e
WS
201 wxMenu *menuType = new wxMenu;
202#if wxUSE_NOTEBOOK
203 menuType->AppendRadioItem(ID_BOOK_NOTEBOOK, wxT("&Notebook\tCtrl-1"));
204#endif
205#if wxUSE_LISTBOOK
206 menuType->AppendRadioItem(ID_BOOK_LISTBOOK, wxT("&Listbook\tCtrl-2"));
207#endif
208#if wxUSE_CHOICEBOOK
209 menuType->AppendRadioItem(ID_BOOK_CHOICEBOOK, wxT("&Choicebook\tCtrl-3"));
210#endif
eca15c0d
VZ
211#if wxUSE_TREEBOOK
212 menuType->AppendRadioItem(ID_BOOK_TREEBOOK, wxT("&Treebook\tCtrl-4"));
213#endif
d709457c
JS
214#if wxUSE_TOOLBOOK
215 menuType->AppendRadioItem(ID_BOOK_TOOLBOOK, wxT("T&oolbook\tCtrl-5"));
216#endif
eca15c0d
VZ
217
218 menuType->Check(ID_BOOK_NOTEBOOK + m_type, true);
9133965e
WS
219
220 wxMenu *menuOrient = new wxMenu;
eca15c0d
VZ
221 menuOrient->AppendRadioItem(ID_ORIENT_DEFAULT, wxT("&Default\tCtrl-5"));
222 menuOrient->AppendRadioItem(ID_ORIENT_TOP, wxT("&Top\tCtrl-6"));
223 menuOrient->AppendRadioItem(ID_ORIENT_BOTTOM, wxT("&Bottom\tCtrl-7"));
224 menuOrient->AppendRadioItem(ID_ORIENT_LEFT, wxT("&Left\tCtrl-8"));
225 menuOrient->AppendRadioItem(ID_ORIENT_RIGHT, wxT("&Right\tCtrl-9"));
226
227 wxMenu *menuOperations = new wxMenu;
228 menuOperations->Append(ID_ADD_PAGE, wxT("&Add page\tAlt-A"));
229 menuOperations->Append(ID_INSERT_PAGE, wxT("&Insert page\tAlt-I"));
230 menuOperations->Append(ID_DELETE_CUR_PAGE, wxT("&Delete current page\tAlt-D"));
231 menuOperations->Append(ID_DELETE_LAST_PAGE, wxT("D&elete last page\tAlt-L"));
232 menuOperations->Append(ID_NEXT_PAGE, wxT("&Next page\tAlt-N"));
233#if wxUSE_TREEBOOK
234 menuOperations->AppendSeparator();
235 menuOperations->Append(ID_ADD_PAGE_BEFORE, wxT("Insert page &before\tAlt-B"));
236 menuOperations->Append(ID_ADD_SUB_PAGE, wxT("Add s&ub page\tAlt-U"));
237#endif
9133965e
WS
238
239 wxMenu *menuFile = new wxMenu;
240 menuFile->Append(wxID_ANY, wxT("&Type"), menuType, wxT("Type of control"));
241 menuFile->Append(wxID_ANY, wxT("&Orientation"), menuOrient, wxT("Orientation of control"));
c0c99785
WS
242 menuFile->AppendCheckItem(ID_SHOW_IMAGES, wxT("&Show images\tAlt-S"));
243 menuFile->AppendCheckItem(ID_MULTI, wxT("&Multiple lines\tAlt-M"));
9133965e
WS
244 menuFile->AppendSeparator();
245 menuFile->Append(wxID_EXIT, wxT("E&xit"), wxT("Quits the application"));
246 menuFile->Check(ID_SHOW_IMAGES, m_chkShowImages);
247 menuFile->Check(ID_MULTI, m_multi);
248
249 wxMenuBar *menuBar = new wxMenuBar;
250 menuBar->Append(menuFile, wxT("&File"));
eca15c0d 251 menuBar->Append(menuOperations, wxT("&Operations"));
9133965e
WS
252 SetMenuBar(menuBar);
253
254 // books creation
eca15c0d
VZ
255 m_panel = NULL;
256 m_bookCtrl = NULL;
76645ab3
VZ
257
258 // create a dummy image list with a few icons
eca15c0d 259 const wxSize imageSize(32, 32);
76645ab3 260
eca15c0d
VZ
261 m_imageList = new wxImageList(imageSize.GetWidth(), imageSize.GetHeight());
262 m_imageList->
263 Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, imageSize));
264 m_imageList->
265 Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, imageSize));
266 m_imageList->
267 Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, imageSize));
268 m_imageList->
269 Add(wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, imageSize));
76645ab3 270
eca15c0d 271 m_panel = new wxPanel(this);
c1dfe277 272
9133965e 273#if USE_LOG
1cfac5b8 274 m_text = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
eca15c0d
VZ
275 wxDefaultPosition, wxDefaultSize,
276 wxTE_MULTILINE | wxTE_READONLY);
c1dfe277
JS
277
278 m_logTargetOld = wxLog::SetActiveTarget( new wxLogTextCtrl(m_text) );
9133965e 279#endif // USE_LOG
c1dfe277 280
c1dfe277
JS
281 // Set sizers
282 m_sizerFrame = new wxBoxSizer(wxVERTICAL);
283
9133965e
WS
284#if USE_LOG
285 m_sizerFrame->Add(m_text, 1, wxEXPAND);
286#endif // USE_LOG
c1dfe277 287
eca15c0d 288 RecreateBook();
c1dfe277
JS
289
290 m_panel->SetSizer(m_sizerFrame);
291
c1dfe277 292 m_sizerFrame->Fit(this);
ed420f6d 293 m_sizerFrame->SetSizeHints(this);
c1dfe277
JS
294
295 Centre(wxBOTH);
0b4d4194
JS
296}
297
c1dfe277 298MyFrame::~MyFrame()
0b4d4194 299{
9133965e 300#if USE_LOG
c1dfe277 301 delete wxLog::SetActiveTarget(m_logTargetOld);
9133965e 302#endif // USE_LOG
76645ab3 303
eca15c0d 304 delete m_imageList;
0b4d4194
JS
305}
306
eca15c0d
VZ
307// DISPATCH_ON_TYPE() macro is an ugly way to write the "same" code for
308// different wxBookCtrlBase-derived classes without duplicating code and
309// without using templates, it expands into "before <xxx> after" where "xxx"
310// part is control class-specific
311#if wxUSE_NOTEBOOK
312 #define CASE_NOTEBOOK(x) case Type_Notebook: x; break;
5a053c22 313#else
eca15c0d 314 #define CASE_NOTEBOOK(x)
02161c7c 315#endif
eca15c0d 316
02161c7c 317#if wxUSE_LISTBOOK
eca15c0d
VZ
318 #define CASE_LISTBOOK(x) case Type_Listbook: x; break;
319#else
320 #define CASE_LISTBOOK(x)
136bafcd
VZ
321#endif
322
323#if wxUSE_CHOICEBOOK
324 #define CASE_CHOICEBOOK(x) case Type_Choicebook: x; break;
136bafcd
VZ
325#else
326 #define CASE_CHOICEBOOK(x)
02161c7c 327#endif
eca15c0d
VZ
328
329#if wxUSE_TREEBOOK
330 #define CASE_TREEBOOK(x) case Type_Treebook: x; break;
331#else
332 #define CASE_TREEBOOK(x)
02161c7c 333#endif
c1dfe277 334
d709457c
JS
335#if wxUSE_TOOLBOOK
336 #define CASE_TOOLBOOK(x) case Type_Toolbook: x; break;
337#else
338 #define CASE_TOOLBOOK(x)
339#endif
340
341#define DISPATCH_ON_TYPE(before, nb, lb, cb, tb, toolb, after) \
eca15c0d
VZ
342 switch ( m_type ) \
343 { \
344 CASE_NOTEBOOK(before nb after) \
eca15c0d 345 CASE_LISTBOOK(before lb after) \
136bafcd 346 CASE_CHOICEBOOK(before cb after) \
eca15c0d 347 CASE_TREEBOOK(before tb after) \
d709457c 348 CASE_TOOLBOOK(before toolb after) \
eca15c0d
VZ
349 \
350 default: \
351 wxFAIL_MSG( _T("unknown book control type") ); \
352 }
353
d709457c 354int MyFrame::TranslateBookFlag(int nb, int lb, int chb, int tbk, int toolbk) const
eca15c0d
VZ
355{
356 int flag = 0;
357
d709457c 358 DISPATCH_ON_TYPE(flag =, nb, lb, chb, tbk, toolbk, + 0);
eca15c0d
VZ
359
360 return flag;
9133965e 361}
c1dfe277 362
eca15c0d 363void MyFrame::RecreateBook()
9133965e 364{
eca15c0d
VZ
365 int flags;
366 switch ( m_orient )
76645ab3 367 {
eca15c0d 368 case ID_ORIENT_TOP:
2ddb4d13 369 flags = wxBK_TOP;
eca15c0d
VZ
370 break;
371
372 case ID_ORIENT_BOTTOM:
2ddb4d13 373 flags = wxBK_BOTTOM;
eca15c0d
VZ
374 break;
375
376 case ID_ORIENT_LEFT:
2ddb4d13 377 flags = wxBK_LEFT;
eca15c0d
VZ
378 break;
379
380 case ID_ORIENT_RIGHT:
2ddb4d13 381 flags = wxBK_RIGHT;
eca15c0d
VZ
382 break;
383
384 default:
2ddb4d13 385 flags = wxBK_DEFAULT;
76645ab3 386 }
c1dfe277 387
eca15c0d
VZ
388 if ( m_multi && m_type == Type_Notebook )
389 flags |= wxNB_MULTILINE;
390 flags |= wxDOUBLE_BORDER;
391
392 wxBookCtrlBase *oldBook = m_bookCtrl;
393
394 m_bookCtrl = NULL;
395
396 DISPATCH_ON_TYPE(m_bookCtrl = new,
397 wxNotebook,
eca15c0d 398 wxListbook,
136bafcd 399 wxChoicebook,
eca15c0d 400 wxTreebook,
d709457c 401 wxToolbook,
eca15c0d
VZ
402 (m_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, flags));
403
404 if ( !m_bookCtrl )
405 return;
406
407 m_bookCtrl->Hide();
408
409 if ( m_chkShowImages )
c1dfe277 410 {
eca15c0d 411 m_bookCtrl->SetImageList(m_imageList);
c1dfe277
JS
412 }
413
eca15c0d
VZ
414 if ( oldBook )
415 {
416#if wxUSE_TREEBOOK
417 // we only need the old treebook if we're recreating another treebook
418 wxTreebook *tbkOld = m_type == Type_Treebook
419 ? wxDynamicCast(oldBook, wxTreebook)
420 : NULL;
421#endif // wxUSE_TREEBOOK
422
423 const int count = oldBook->GetPageCount();
424 for ( int n = 0; n < count; n++ )
425 {
426 const int image = GetIconIndex(m_bookCtrl);
427 const wxString str = oldBook->GetPageText(n);
428
429 wxWindow *page = CreatePage(m_bookCtrl, str);
430
431 // treebook complication: need to account for possible parent page
432#if wxUSE_TREEBOOK
433 if ( tbkOld )
434 {
435 const int parent = tbkOld->GetPageParent(n);
436 if ( parent != wxNOT_FOUND )
437 {
438 wxStaticCast(m_bookCtrl, wxTreebook)->
9d5371c6 439 InsertSubPage(parent, page, str, false, image);
eca15c0d
VZ
440
441 // skip adding it again below
442 continue;
443 }
444 }
445#endif // wxUSE_TREEBOOK
446
447 m_bookCtrl->AddPage(page, str, false, image);
448 }
449
450 const int sel = oldBook->GetSelection();
451 if ( sel != wxNOT_FOUND )
452 m_bookCtrl->SetSelection(sel);
453
454
455 m_sizerFrame->Detach(oldBook);
456 delete oldBook;
457 }
458 else // no old book
459 {
460 CreateInitialPages(m_bookCtrl);
461 }
462
463 m_sizerFrame->Insert(0, m_bookCtrl, wxSizerFlags(5).Expand().Border());
464
465 m_sizerFrame->Show(m_bookCtrl);
9133965e 466 m_sizerFrame->Layout();
c1dfe277
JS
467}
468
0b4d4194 469BEGIN_EVENT_TABLE(MyFrame, wxFrame)
9133965e 470 // File menu
eca15c0d
VZ
471 EVT_MENU_RANGE(ID_BOOK_NOTEBOOK, ID_BOOK_MAX, MyFrame::OnType)
472 EVT_MENU_RANGE(ID_ORIENT_DEFAULT, ID_ORIENT_MAX, MyFrame::OnOrient)
9133965e
WS
473 EVT_MENU(ID_SHOW_IMAGES, MyFrame::OnShowImages)
474 EVT_MENU(ID_MULTI, MyFrame::OnMulti)
eca15c0d 475 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
9133965e
WS
476
477 // Operations menu
478 EVT_MENU(ID_ADD_PAGE, MyFrame::OnAddPage)
479 EVT_MENU(ID_INSERT_PAGE, MyFrame::OnInsertPage)
480 EVT_MENU(ID_DELETE_CUR_PAGE, MyFrame::OnDeleteCurPage)
481 EVT_MENU(ID_DELETE_LAST_PAGE, MyFrame::OnDeleteLastPage)
482 EVT_MENU(ID_NEXT_PAGE, MyFrame::OnNextPage)
483
484 // Book controls
02161c7c 485#if wxUSE_NOTEBOOK
eca15c0d
VZ
486 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnNotebook)
487 EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnNotebook)
02161c7c
WS
488#endif
489#if wxUSE_LISTBOOK
eca15c0d
VZ
490 EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnListbook)
491 EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnListbook)
02161c7c
WS
492#endif
493#if wxUSE_CHOICEBOOK
eca15c0d
VZ
494 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnChoicebook)
495 EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnChoicebook)
496#endif
136bafcd 497#if wxUSE_TREEBOOK
eca15c0d
VZ
498 EVT_TREEBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnTreebook)
499 EVT_TREEBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnTreebook)
136bafcd
VZ
500
501 EVT_MENU(ID_ADD_SUB_PAGE, MyFrame::OnAddSubPage)
502 EVT_MENU(ID_ADD_PAGE_BEFORE, MyFrame::OnAddPageBefore)
503 EVT_UPDATE_UI_RANGE(ID_ADD_PAGE_BEFORE, ID_ADD_SUB_PAGE,
504 MyFrame::OnUpdateTreeMenu)
02161c7c 505#endif
d709457c
JS
506#if wxUSE_TOOLBOOK
507 EVT_TOOLBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnToolbook)
508 EVT_TOOLBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnToolbook)
509#endif
c1dfe277 510
9133965e 511 // Update title in idle time
96d37807 512 EVT_IDLE(MyFrame::OnIdle)
0b4d4194
JS
513END_EVENT_TABLE()
514
9133965e 515void MyFrame::OnType(wxCommandEvent& event)
0b4d4194 516{
eca15c0d
VZ
517 m_type = wx_static_cast(BookType, event.GetId() - ID_BOOK_NOTEBOOK);
518
519 if ( m_bookCtrl )
520 m_sizerFrame->Hide(m_bookCtrl);
0b4d4194 521
eca15c0d
VZ
522 RecreateBook();
523}
477350ce 524
eca15c0d 525#if wxUSE_TREEBOOK
477350ce 526
eca15c0d
VZ
527void MyFrame::OnUpdateTreeMenu(wxUpdateUIEvent& event)
528{
529 event.Enable(m_type == Type_Treebook);
477350ce
VZ
530}
531
eca15c0d
VZ
532#endif // wxUSE_TREEBOOK
533
534
9133965e 535void MyFrame::OnOrient(wxCommandEvent& event)
477350ce 536{
9133965e 537 m_orient = event.GetId();
eca15c0d 538 RecreateBook();
9133965e
WS
539 m_sizerFrame->Layout();
540}
477350ce 541
9133965e
WS
542void MyFrame::OnShowImages(wxCommandEvent& event)
543{
544 m_chkShowImages = event.IsChecked();
eca15c0d 545 RecreateBook();
9133965e
WS
546 m_sizerFrame->Layout();
547}
c1dfe277 548
9133965e
WS
549void MyFrame::OnMulti(wxCommandEvent& event)
550{
551 m_multi = event.IsChecked();
eca15c0d 552 RecreateBook();
9133965e
WS
553 m_sizerFrame->Layout();
554 wxLogMessage(_T("Multiline setting works only in wxNotebook."));
555}
477350ce 556
9133965e
WS
557void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
558{
559 Close();
326f9654
RR
560}
561
eca15c0d 562wxPanel *MyFrame::CreateNewPage() const
3957448a 563{
eca15c0d
VZ
564 wxPanel *panel = new wxPanel(m_bookCtrl, wxID_ANY );
565 (void) new wxButton(panel, wxID_ANY, wxT("First button"), wxPoint(10, 10));
566 (void) new wxButton(panel, wxID_ANY, wxT("Second button"), wxPoint(50, 100));
567
568 return panel;
569}
3957448a 570
eca15c0d
VZ
571void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event))
572{
61c083e7 573 wxBookCtrlBase *currBook = GetCurrentBook();
9133965e
WS
574
575 if ( currBook )
3957448a 576 {
eca15c0d
VZ
577 static unsigned s_pageAdded = 0;
578 currBook->AddPage(CreateNewPage(),
579 wxString::Format
580 (
581 ADDED_PAGE_NAME wxT("%u"),
582 ++s_pageAdded
583 ),
584 true,
585 GetIconIndex(currBook));
586 }
587}
588
589#if wxUSE_TREEBOOK
590void MyFrame::OnAddSubPage(wxCommandEvent& WXUNUSED(event))
591{
592 wxTreebook *currBook = wxDynamicCast(GetCurrentBook(), wxTreebook);
593 if ( currBook )
594 {
595 const int selPos = currBook->GetSelection();
596 if ( selPos == wxNOT_FOUND )
597 {
598 wxLogError(_T("Select the parent page first!"));
599 return;
600 }
601
602 static unsigned s_subPageAdded = 0;
9d5371c6
VZ
603 currBook->InsertSubPage
604 (
605 selPos,
606 CreateNewPage(),
607 wxString::Format
608 (
609 ADDED_SUB_PAGE_NAME wxT("%u"),
610 ++s_subPageAdded
611 ),
612 true,
613 GetIconIndex(currBook)
614 );
eca15c0d
VZ
615 }
616}
617
618void MyFrame::OnAddPageBefore(wxCommandEvent& WXUNUSED(event))
619{
620 wxBookCtrlBase *currBook = GetCurrentBook();
621 if ( currBook )
622 {
623 const int selPos = currBook->GetSelection();
624 if ( selPos == wxNOT_FOUND )
625 {
626 wxLogError(_T("Select the parent page first!"));
627 return;
628 }
629
630 static unsigned s_subPageAdded = 0;
631 currBook->InsertPage(selPos,
632 CreateNewPage(),
633 wxString::Format
634 (
635 ADDED_PAGE_NAME_BEFORE wxT("%u"),
636 ++s_subPageAdded
637 ),
638 true,
639 GetIconIndex(currBook));
3957448a
VZ
640 }
641}
eca15c0d 642#endif // wxUSE_TREEBOOK
3957448a 643
9133965e 644void MyFrame::OnInsertPage(wxCommandEvent& WXUNUSED(event))
29f538ce 645{
9133965e
WS
646 static unsigned s_pageIns = 0;
647
61c083e7 648 wxBookCtrlBase *currBook = GetCurrentBook();
f6bcfd97 649
9133965e 650 if ( currBook )
f6bcfd97 651 {
9133965e
WS
652 wxPanel *panel = CreateUserCreatedPage( currBook );
653
654 currBook->InsertPage( 0, panel,
655 wxString::Format(INSERTED_PAGE_NAME wxT("%u"), ++s_pageIns), false,
656 GetIconIndex(currBook) );
657
658 currBook->SetSelection(0);
f6bcfd97 659 }
29f538ce
RR
660}
661
9133965e 662void MyFrame::OnDeleteCurPage(wxCommandEvent& WXUNUSED(event))
33d0e17c 663{
61c083e7 664 wxBookCtrlBase *currBook = GetCurrentBook();
33d0e17c 665
9133965e
WS
666 if ( currBook )
667 {
668 int sel = currBook->GetSelection();
669
670 if (sel != wxNOT_FOUND)
671 {
672 currBook->DeletePage(sel);
673 }
674 }
0b4d4194
JS
675}
676
9133965e 677void MyFrame::OnDeleteLastPage(wxCommandEvent& WXUNUSED(event))
0b4d4194 678{
61c083e7 679 wxBookCtrlBase *currBook = GetCurrentBook();
d22699b5 680
9133965e 681 if ( currBook )
c1dfe277 682 {
9133965e 683 int page = currBook->GetPageCount();
c1dfe277 684
9133965e
WS
685 if ( page != 0 )
686 {
687 currBook->DeletePage(page - 1);
c1dfe277 688 }
c1dfe277 689 }
9133965e 690}
0b4d4194 691
9133965e
WS
692void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event))
693{
61c083e7 694 wxBookCtrlBase *currBook = GetCurrentBook();
0b4d4194 695
9133965e
WS
696 if ( currBook )
697 {
698 currBook->AdvanceSelection();
699 }
0b4d4194
JS
700}
701
c1dfe277 702void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
96d37807 703{
9133965e
WS
704 static int s_nPages = wxNOT_FOUND;
705 static int s_nSel = wxNOT_FOUND;
61c083e7 706 static wxBookCtrlBase *s_currBook = NULL;
9133965e 707
61c083e7 708 wxBookCtrlBase *currBook = GetCurrentBook();
96d37807 709
9133965e
WS
710 int nPages = currBook ? currBook->GetPageCount() : 0;
711 int nSel = currBook ? currBook->GetSelection() : wxNOT_FOUND;
712
713 if ( nPages != s_nPages || nSel != s_nSel || s_currBook != currBook )
96d37807
VZ
714 {
715 s_nPages = nPages;
716 s_nSel = nSel;
9133965e
WS
717 s_currBook = currBook;
718
719 wxString selection;
720 if ( nSel == wxNOT_FOUND )
721 selection << wxT("not found");
722 else
723 selection << nSel;
96d37807
VZ
724
725 wxString title;
9133965e 726 title.Printf(wxT("Notebook and friends (%d pages, selection: %s)"), nPages, selection.c_str());
96d37807
VZ
727
728 SetTitle(title);
729 }
730}
3957448a 731
eca15c0d
VZ
732void MyFrame::OnBookCtrl(wxBookCtrlBaseEvent& event)
733{
734 static const struct EventInfo
735 {
736 wxEventType typeChanged,
737 typeChanging;
738 const wxChar *name;
739 } events[] =
740 {
02161c7c 741#if wxUSE_NOTEBOOK
eca15c0d
VZ
742 {
743 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
744 wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING,
745 _T("wxNotebook")
746 },
747#endif // wxUSE_NOTEBOOK
02161c7c 748#if wxUSE_LISTBOOK
eca15c0d
VZ
749 {
750 wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED,
751 wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING,
752 _T("wxListbook")
753 },
754#endif // wxUSE_LISTBOOK
136bafcd
VZ
755#if wxUSE_CHOICEBOOK
756 {
757 wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED,
758 wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING,
759 _T("wxChoicebook")
760 },
761#endif // wxUSE_CHOICEBOOK
eca15c0d
VZ
762#if wxUSE_TREEBOOK
763 {
764 wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED,
765 wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING,
766 _T("wxTreebook")
767 },
768#endif // wxUSE_TREEBOOK
d709457c
JS
769#if wxUSE_TOOLBOOK
770 {
771 wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED,
772 wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING,
773 _T("wxToolbook")
774 },
775#endif // wxUSE_TOOLBOOK
eca15c0d
VZ
776 };
777
778
779 wxString nameEvent,
780 nameControl,
781 veto;
782 const wxEventType eventType = event.GetEventType();
783 for ( size_t n = 0; n < WXSIZEOF(events); n++ )
784 {
785 const EventInfo& ei = events[n];
786 if ( eventType == ei.typeChanged )
787 {
788 nameEvent = wxT("Changed");
789 }
790 else if ( eventType == ei.typeChanging )
791 {
792 const int idx = event.GetOldSelection();
582ca353
VZ
793
794 // NB: can't use wxStaticCast here as wxBookCtrlBase is not in
795 // wxRTTI
eca15c0d 796 const wxBookCtrlBase * const
582ca353 797 book = wx_static_cast(wxBookCtrlBase *, event.GetEventObject());
eca15c0d
VZ
798 if ( idx != wxNOT_FOUND &&
799 book && book->GetPageText(idx) == VETO_PAGE_NAME )
800 {
801 if ( wxMessageBox
802 (
803 wxT("Are you sure you want to leave this page?\n")
804 wxT("(This demonstrates veto-ing)"),
805 wxT("Notebook sample"),
806 wxICON_QUESTION | wxYES_NO,
807 this
808 ) != wxYES )
809 {
810 event.Veto();
811 veto = _T(" (vetoed)");
812 }
813 }
814
815 nameEvent = wxT("Changing");
816 }
817 else // skip end of the loop
818 {
819 continue;
820 }
821
822 nameControl = ei.name;
823 break;
824 }
825
826 static int s_num = 0;
827
828 wxLogMessage(wxT("Event #%d: %s: %s (%d) new sel %d, old %d%s"),
829 ++s_num,
830 nameControl.c_str(),
831 nameEvent.c_str(),
832 eventType,
833 event.GetSelection(),
834 event.GetOldSelection(),
835 veto.c_str());
836
837#if USE_LOG
838 m_text->SetInsertionPointEnd();
02161c7c 839#endif
eca15c0d 840}