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