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