]>
Commit | Line | Data |
---|---|---|
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 |
31 | IMPLEMENT_APP(MyApp) |
32 | ||
477350ce | 33 | bool 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 | 51 | wxPanel *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 | 61 | wxPanel *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 | 86 | wxPanel *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 | 96 | wxPanel *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 | 110 | wxPanel *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 | 121 | int 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 | 135 | void 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 | 154 | wxPanel *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 |
179 | MyFrame::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 | |
214 | ||
215 | menuType->Check(ID_BOOK_NOTEBOOK + m_type, true); | |
9133965e WS |
216 | |
217 | wxMenu *menuOrient = new wxMenu; | |
eca15c0d VZ |
218 | menuOrient->AppendRadioItem(ID_ORIENT_DEFAULT, wxT("&Default\tCtrl-5")); |
219 | menuOrient->AppendRadioItem(ID_ORIENT_TOP, wxT("&Top\tCtrl-6")); | |
220 | menuOrient->AppendRadioItem(ID_ORIENT_BOTTOM, wxT("&Bottom\tCtrl-7")); | |
221 | menuOrient->AppendRadioItem(ID_ORIENT_LEFT, wxT("&Left\tCtrl-8")); | |
222 | menuOrient->AppendRadioItem(ID_ORIENT_RIGHT, wxT("&Right\tCtrl-9")); | |
223 | ||
224 | wxMenu *menuOperations = new wxMenu; | |
225 | menuOperations->Append(ID_ADD_PAGE, wxT("&Add page\tAlt-A")); | |
226 | menuOperations->Append(ID_INSERT_PAGE, wxT("&Insert page\tAlt-I")); | |
227 | menuOperations->Append(ID_DELETE_CUR_PAGE, wxT("&Delete current page\tAlt-D")); | |
228 | menuOperations->Append(ID_DELETE_LAST_PAGE, wxT("D&elete last page\tAlt-L")); | |
229 | menuOperations->Append(ID_NEXT_PAGE, wxT("&Next page\tAlt-N")); | |
230 | #if wxUSE_TREEBOOK | |
231 | menuOperations->AppendSeparator(); | |
232 | menuOperations->Append(ID_ADD_PAGE_BEFORE, wxT("Insert page &before\tAlt-B")); | |
233 | menuOperations->Append(ID_ADD_SUB_PAGE, wxT("Add s&ub page\tAlt-U")); | |
234 | #endif | |
9133965e WS |
235 | |
236 | wxMenu *menuFile = new wxMenu; | |
237 | menuFile->Append(wxID_ANY, wxT("&Type"), menuType, wxT("Type of control")); | |
238 | menuFile->Append(wxID_ANY, wxT("&Orientation"), menuOrient, wxT("Orientation of control")); | |
c0c99785 WS |
239 | menuFile->AppendCheckItem(ID_SHOW_IMAGES, wxT("&Show images\tAlt-S")); |
240 | menuFile->AppendCheckItem(ID_MULTI, wxT("&Multiple lines\tAlt-M")); | |
9133965e WS |
241 | menuFile->AppendSeparator(); |
242 | menuFile->Append(wxID_EXIT, wxT("E&xit"), wxT("Quits the application")); | |
243 | menuFile->Check(ID_SHOW_IMAGES, m_chkShowImages); | |
244 | menuFile->Check(ID_MULTI, m_multi); | |
245 | ||
246 | wxMenuBar *menuBar = new wxMenuBar; | |
247 | menuBar->Append(menuFile, wxT("&File")); | |
eca15c0d | 248 | menuBar->Append(menuOperations, wxT("&Operations")); |
9133965e WS |
249 | SetMenuBar(menuBar); |
250 | ||
251 | // books creation | |
eca15c0d VZ |
252 | m_panel = NULL; |
253 | m_bookCtrl = NULL; | |
76645ab3 VZ |
254 | |
255 | // create a dummy image list with a few icons | |
eca15c0d | 256 | const wxSize imageSize(32, 32); |
76645ab3 | 257 | |
eca15c0d VZ |
258 | m_imageList = new wxImageList(imageSize.GetWidth(), imageSize.GetHeight()); |
259 | m_imageList-> | |
260 | Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, imageSize)); | |
261 | m_imageList-> | |
262 | Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, imageSize)); | |
263 | m_imageList-> | |
264 | Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, imageSize)); | |
265 | m_imageList-> | |
266 | Add(wxArtProvider::GetIcon(wxART_ERROR, wxART_OTHER, imageSize)); | |
76645ab3 | 267 | |
eca15c0d | 268 | m_panel = new wxPanel(this); |
c1dfe277 | 269 | |
9133965e | 270 | #if USE_LOG |
1cfac5b8 | 271 | m_text = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString, |
eca15c0d VZ |
272 | wxDefaultPosition, wxDefaultSize, |
273 | wxTE_MULTILINE | wxTE_READONLY); | |
c1dfe277 JS |
274 | |
275 | m_logTargetOld = wxLog::SetActiveTarget( new wxLogTextCtrl(m_text) ); | |
9133965e | 276 | #endif // USE_LOG |
c1dfe277 | 277 | |
c1dfe277 JS |
278 | // Set sizers |
279 | m_sizerFrame = new wxBoxSizer(wxVERTICAL); | |
280 | ||
9133965e WS |
281 | #if USE_LOG |
282 | m_sizerFrame->Add(m_text, 1, wxEXPAND); | |
283 | #endif // USE_LOG | |
c1dfe277 | 284 | |
eca15c0d | 285 | RecreateBook(); |
c1dfe277 JS |
286 | |
287 | m_panel->SetSizer(m_sizerFrame); | |
288 | ||
c1dfe277 | 289 | m_sizerFrame->Fit(this); |
ed420f6d | 290 | m_sizerFrame->SetSizeHints(this); |
c1dfe277 JS |
291 | |
292 | Centre(wxBOTH); | |
0b4d4194 JS |
293 | } |
294 | ||
c1dfe277 | 295 | MyFrame::~MyFrame() |
0b4d4194 | 296 | { |
9133965e | 297 | #if USE_LOG |
c1dfe277 | 298 | delete wxLog::SetActiveTarget(m_logTargetOld); |
9133965e | 299 | #endif // USE_LOG |
76645ab3 | 300 | |
eca15c0d | 301 | delete m_imageList; |
0b4d4194 JS |
302 | } |
303 | ||
eca15c0d VZ |
304 | // DISPATCH_ON_TYPE() macro is an ugly way to write the "same" code for |
305 | // different wxBookCtrlBase-derived classes without duplicating code and | |
306 | // without using templates, it expands into "before <xxx> after" where "xxx" | |
307 | // part is control class-specific | |
308 | #if wxUSE_NOTEBOOK | |
309 | #define CASE_NOTEBOOK(x) case Type_Notebook: x; break; | |
5a053c22 | 310 | #else |
eca15c0d | 311 | #define CASE_NOTEBOOK(x) |
02161c7c | 312 | #endif |
eca15c0d | 313 | |
02161c7c | 314 | #if wxUSE_LISTBOOK |
eca15c0d VZ |
315 | #define CASE_LISTBOOK(x) case Type_Listbook: x; break; |
316 | #else | |
317 | #define CASE_LISTBOOK(x) | |
136bafcd VZ |
318 | #endif |
319 | ||
320 | #if wxUSE_CHOICEBOOK | |
321 | #define CASE_CHOICEBOOK(x) case Type_Choicebook: x; break; | |
136bafcd VZ |
322 | #else |
323 | #define CASE_CHOICEBOOK(x) | |
02161c7c | 324 | #endif |
eca15c0d VZ |
325 | |
326 | #if wxUSE_TREEBOOK | |
327 | #define CASE_TREEBOOK(x) case Type_Treebook: x; break; | |
328 | #else | |
329 | #define CASE_TREEBOOK(x) | |
02161c7c | 330 | #endif |
c1dfe277 | 331 | |
eca15c0d VZ |
332 | #define DISPATCH_ON_TYPE(before, nb, lb, cb, tb, after) \ |
333 | switch ( m_type ) \ | |
334 | { \ | |
335 | CASE_NOTEBOOK(before nb after) \ | |
eca15c0d | 336 | CASE_LISTBOOK(before lb after) \ |
136bafcd | 337 | CASE_CHOICEBOOK(before cb after) \ |
eca15c0d VZ |
338 | CASE_TREEBOOK(before tb after) \ |
339 | \ | |
340 | default: \ | |
341 | wxFAIL_MSG( _T("unknown book control type") ); \ | |
342 | } | |
343 | ||
344 | int MyFrame::TranslateBookFlag(int nb, int lb, int chb, int tbk) const | |
345 | { | |
346 | int flag = 0; | |
347 | ||
348 | DISPATCH_ON_TYPE(flag =, nb, lb, chb, tbk, + 0); | |
349 | ||
350 | return flag; | |
9133965e | 351 | } |
c1dfe277 | 352 | |
eca15c0d | 353 | void MyFrame::RecreateBook() |
9133965e | 354 | { |
eca15c0d VZ |
355 | int flags; |
356 | switch ( m_orient ) | |
76645ab3 | 357 | { |
eca15c0d | 358 | case ID_ORIENT_TOP: |
2ddb4d13 | 359 | flags = wxBK_TOP; |
eca15c0d VZ |
360 | break; |
361 | ||
362 | case ID_ORIENT_BOTTOM: | |
2ddb4d13 | 363 | flags = wxBK_BOTTOM; |
eca15c0d VZ |
364 | break; |
365 | ||
366 | case ID_ORIENT_LEFT: | |
2ddb4d13 | 367 | flags = wxBK_LEFT; |
eca15c0d VZ |
368 | break; |
369 | ||
370 | case ID_ORIENT_RIGHT: | |
2ddb4d13 | 371 | flags = wxBK_RIGHT; |
eca15c0d VZ |
372 | break; |
373 | ||
374 | default: | |
2ddb4d13 | 375 | flags = wxBK_DEFAULT; |
76645ab3 | 376 | } |
c1dfe277 | 377 | |
eca15c0d VZ |
378 | if ( m_multi && m_type == Type_Notebook ) |
379 | flags |= wxNB_MULTILINE; | |
380 | flags |= wxDOUBLE_BORDER; | |
381 | ||
382 | wxBookCtrlBase *oldBook = m_bookCtrl; | |
383 | ||
384 | m_bookCtrl = NULL; | |
385 | ||
386 | DISPATCH_ON_TYPE(m_bookCtrl = new, | |
387 | wxNotebook, | |
eca15c0d | 388 | wxListbook, |
136bafcd | 389 | wxChoicebook, |
eca15c0d VZ |
390 | wxTreebook, |
391 | (m_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, flags)); | |
392 | ||
393 | if ( !m_bookCtrl ) | |
394 | return; | |
395 | ||
396 | m_bookCtrl->Hide(); | |
397 | ||
398 | if ( m_chkShowImages ) | |
c1dfe277 | 399 | { |
eca15c0d | 400 | m_bookCtrl->SetImageList(m_imageList); |
c1dfe277 JS |
401 | } |
402 | ||
eca15c0d VZ |
403 | if ( oldBook ) |
404 | { | |
405 | #if wxUSE_TREEBOOK | |
406 | // we only need the old treebook if we're recreating another treebook | |
407 | wxTreebook *tbkOld = m_type == Type_Treebook | |
408 | ? wxDynamicCast(oldBook, wxTreebook) | |
409 | : NULL; | |
410 | #endif // wxUSE_TREEBOOK | |
411 | ||
412 | const int count = oldBook->GetPageCount(); | |
413 | for ( int n = 0; n < count; n++ ) | |
414 | { | |
415 | const int image = GetIconIndex(m_bookCtrl); | |
416 | const wxString str = oldBook->GetPageText(n); | |
417 | ||
418 | wxWindow *page = CreatePage(m_bookCtrl, str); | |
419 | ||
420 | // treebook complication: need to account for possible parent page | |
421 | #if wxUSE_TREEBOOK | |
422 | if ( tbkOld ) | |
423 | { | |
424 | const int parent = tbkOld->GetPageParent(n); | |
425 | if ( parent != wxNOT_FOUND ) | |
426 | { | |
427 | wxStaticCast(m_bookCtrl, wxTreebook)-> | |
428 | AddSubPage(parent, page, str, false, image); | |
429 | ||
430 | // skip adding it again below | |
431 | continue; | |
432 | } | |
433 | } | |
434 | #endif // wxUSE_TREEBOOK | |
435 | ||
436 | m_bookCtrl->AddPage(page, str, false, image); | |
437 | } | |
438 | ||
439 | const int sel = oldBook->GetSelection(); | |
440 | if ( sel != wxNOT_FOUND ) | |
441 | m_bookCtrl->SetSelection(sel); | |
442 | ||
443 | ||
444 | m_sizerFrame->Detach(oldBook); | |
445 | delete oldBook; | |
446 | } | |
447 | else // no old book | |
448 | { | |
449 | CreateInitialPages(m_bookCtrl); | |
450 | } | |
451 | ||
452 | m_sizerFrame->Insert(0, m_bookCtrl, wxSizerFlags(5).Expand().Border()); | |
453 | ||
454 | m_sizerFrame->Show(m_bookCtrl); | |
9133965e | 455 | m_sizerFrame->Layout(); |
c1dfe277 JS |
456 | } |
457 | ||
0b4d4194 | 458 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
9133965e | 459 | // File menu |
eca15c0d VZ |
460 | EVT_MENU_RANGE(ID_BOOK_NOTEBOOK, ID_BOOK_MAX, MyFrame::OnType) |
461 | EVT_MENU_RANGE(ID_ORIENT_DEFAULT, ID_ORIENT_MAX, MyFrame::OnOrient) | |
9133965e WS |
462 | EVT_MENU(ID_SHOW_IMAGES, MyFrame::OnShowImages) |
463 | EVT_MENU(ID_MULTI, MyFrame::OnMulti) | |
eca15c0d | 464 | EVT_MENU(wxID_EXIT, MyFrame::OnExit) |
9133965e WS |
465 | |
466 | // Operations menu | |
467 | EVT_MENU(ID_ADD_PAGE, MyFrame::OnAddPage) | |
468 | EVT_MENU(ID_INSERT_PAGE, MyFrame::OnInsertPage) | |
469 | EVT_MENU(ID_DELETE_CUR_PAGE, MyFrame::OnDeleteCurPage) | |
470 | EVT_MENU(ID_DELETE_LAST_PAGE, MyFrame::OnDeleteLastPage) | |
471 | EVT_MENU(ID_NEXT_PAGE, MyFrame::OnNextPage) | |
472 | ||
473 | // Book controls | |
02161c7c | 474 | #if wxUSE_NOTEBOOK |
eca15c0d VZ |
475 | EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnNotebook) |
476 | EVT_NOTEBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnNotebook) | |
02161c7c WS |
477 | #endif |
478 | #if wxUSE_LISTBOOK | |
eca15c0d VZ |
479 | EVT_LISTBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnListbook) |
480 | EVT_LISTBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnListbook) | |
02161c7c WS |
481 | #endif |
482 | #if wxUSE_CHOICEBOOK | |
eca15c0d VZ |
483 | EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnChoicebook) |
484 | EVT_CHOICEBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnChoicebook) | |
485 | #endif | |
136bafcd | 486 | #if wxUSE_TREEBOOK |
eca15c0d VZ |
487 | EVT_TREEBOOK_PAGE_CHANGED(wxID_ANY, MyFrame::OnTreebook) |
488 | EVT_TREEBOOK_PAGE_CHANGING(wxID_ANY, MyFrame::OnTreebook) | |
136bafcd VZ |
489 | |
490 | EVT_MENU(ID_ADD_SUB_PAGE, MyFrame::OnAddSubPage) | |
491 | EVT_MENU(ID_ADD_PAGE_BEFORE, MyFrame::OnAddPageBefore) | |
492 | EVT_UPDATE_UI_RANGE(ID_ADD_PAGE_BEFORE, ID_ADD_SUB_PAGE, | |
493 | MyFrame::OnUpdateTreeMenu) | |
02161c7c | 494 | #endif |
c1dfe277 | 495 | |
9133965e | 496 | // Update title in idle time |
96d37807 | 497 | EVT_IDLE(MyFrame::OnIdle) |
0b4d4194 JS |
498 | END_EVENT_TABLE() |
499 | ||
9133965e | 500 | void MyFrame::OnType(wxCommandEvent& event) |
0b4d4194 | 501 | { |
eca15c0d VZ |
502 | m_type = wx_static_cast(BookType, event.GetId() - ID_BOOK_NOTEBOOK); |
503 | ||
504 | if ( m_bookCtrl ) | |
505 | m_sizerFrame->Hide(m_bookCtrl); | |
0b4d4194 | 506 | |
eca15c0d VZ |
507 | RecreateBook(); |
508 | } | |
477350ce | 509 | |
eca15c0d | 510 | #if wxUSE_TREEBOOK |
477350ce | 511 | |
eca15c0d VZ |
512 | void MyFrame::OnUpdateTreeMenu(wxUpdateUIEvent& event) |
513 | { | |
514 | event.Enable(m_type == Type_Treebook); | |
477350ce VZ |
515 | } |
516 | ||
eca15c0d VZ |
517 | #endif // wxUSE_TREEBOOK |
518 | ||
519 | ||
9133965e | 520 | void MyFrame::OnOrient(wxCommandEvent& event) |
477350ce | 521 | { |
9133965e | 522 | m_orient = event.GetId(); |
eca15c0d | 523 | RecreateBook(); |
9133965e WS |
524 | m_sizerFrame->Layout(); |
525 | } | |
477350ce | 526 | |
9133965e WS |
527 | void MyFrame::OnShowImages(wxCommandEvent& event) |
528 | { | |
529 | m_chkShowImages = event.IsChecked(); | |
eca15c0d | 530 | RecreateBook(); |
9133965e WS |
531 | m_sizerFrame->Layout(); |
532 | } | |
c1dfe277 | 533 | |
9133965e WS |
534 | void MyFrame::OnMulti(wxCommandEvent& event) |
535 | { | |
536 | m_multi = event.IsChecked(); | |
eca15c0d | 537 | RecreateBook(); |
9133965e WS |
538 | m_sizerFrame->Layout(); |
539 | wxLogMessage(_T("Multiline setting works only in wxNotebook.")); | |
540 | } | |
477350ce | 541 | |
9133965e WS |
542 | void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) |
543 | { | |
544 | Close(); | |
326f9654 RR |
545 | } |
546 | ||
eca15c0d | 547 | wxPanel *MyFrame::CreateNewPage() const |
3957448a | 548 | { |
eca15c0d VZ |
549 | wxPanel *panel = new wxPanel(m_bookCtrl, wxID_ANY ); |
550 | (void) new wxButton(panel, wxID_ANY, wxT("First button"), wxPoint(10, 10)); | |
551 | (void) new wxButton(panel, wxID_ANY, wxT("Second button"), wxPoint(50, 100)); | |
552 | ||
553 | return panel; | |
554 | } | |
3957448a | 555 | |
eca15c0d VZ |
556 | void MyFrame::OnAddPage(wxCommandEvent& WXUNUSED(event)) |
557 | { | |
61c083e7 | 558 | wxBookCtrlBase *currBook = GetCurrentBook(); |
9133965e WS |
559 | |
560 | if ( currBook ) | |
3957448a | 561 | { |
eca15c0d VZ |
562 | static unsigned s_pageAdded = 0; |
563 | currBook->AddPage(CreateNewPage(), | |
564 | wxString::Format | |
565 | ( | |
566 | ADDED_PAGE_NAME wxT("%u"), | |
567 | ++s_pageAdded | |
568 | ), | |
569 | true, | |
570 | GetIconIndex(currBook)); | |
571 | } | |
572 | } | |
573 | ||
574 | #if wxUSE_TREEBOOK | |
575 | void MyFrame::OnAddSubPage(wxCommandEvent& WXUNUSED(event)) | |
576 | { | |
577 | wxTreebook *currBook = wxDynamicCast(GetCurrentBook(), wxTreebook); | |
578 | if ( currBook ) | |
579 | { | |
580 | const int selPos = currBook->GetSelection(); | |
581 | if ( selPos == wxNOT_FOUND ) | |
582 | { | |
583 | wxLogError(_T("Select the parent page first!")); | |
584 | return; | |
585 | } | |
586 | ||
587 | static unsigned s_subPageAdded = 0; | |
588 | currBook->AddSubPage(selPos, | |
589 | CreateNewPage(), | |
590 | wxString::Format | |
591 | ( | |
592 | ADDED_SUB_PAGE_NAME wxT("%u"), | |
593 | ++s_subPageAdded | |
594 | ), | |
595 | true, | |
596 | GetIconIndex(currBook)); | |
597 | } | |
598 | } | |
599 | ||
600 | void MyFrame::OnAddPageBefore(wxCommandEvent& WXUNUSED(event)) | |
601 | { | |
602 | wxBookCtrlBase *currBook = GetCurrentBook(); | |
603 | if ( currBook ) | |
604 | { | |
605 | const int selPos = currBook->GetSelection(); | |
606 | if ( selPos == wxNOT_FOUND ) | |
607 | { | |
608 | wxLogError(_T("Select the parent page first!")); | |
609 | return; | |
610 | } | |
611 | ||
612 | static unsigned s_subPageAdded = 0; | |
613 | currBook->InsertPage(selPos, | |
614 | CreateNewPage(), | |
615 | wxString::Format | |
616 | ( | |
617 | ADDED_PAGE_NAME_BEFORE wxT("%u"), | |
618 | ++s_subPageAdded | |
619 | ), | |
620 | true, | |
621 | GetIconIndex(currBook)); | |
3957448a VZ |
622 | } |
623 | } | |
eca15c0d | 624 | #endif // wxUSE_TREEBOOK |
3957448a | 625 | |
9133965e | 626 | void MyFrame::OnInsertPage(wxCommandEvent& WXUNUSED(event)) |
29f538ce | 627 | { |
9133965e WS |
628 | static unsigned s_pageIns = 0; |
629 | ||
61c083e7 | 630 | wxBookCtrlBase *currBook = GetCurrentBook(); |
f6bcfd97 | 631 | |
9133965e | 632 | if ( currBook ) |
f6bcfd97 | 633 | { |
9133965e WS |
634 | wxPanel *panel = CreateUserCreatedPage( currBook ); |
635 | ||
636 | currBook->InsertPage( 0, panel, | |
637 | wxString::Format(INSERTED_PAGE_NAME wxT("%u"), ++s_pageIns), false, | |
638 | GetIconIndex(currBook) ); | |
639 | ||
640 | currBook->SetSelection(0); | |
f6bcfd97 | 641 | } |
29f538ce RR |
642 | } |
643 | ||
9133965e | 644 | void MyFrame::OnDeleteCurPage(wxCommandEvent& WXUNUSED(event)) |
33d0e17c | 645 | { |
61c083e7 | 646 | wxBookCtrlBase *currBook = GetCurrentBook(); |
33d0e17c | 647 | |
9133965e WS |
648 | if ( currBook ) |
649 | { | |
650 | int sel = currBook->GetSelection(); | |
651 | ||
652 | if (sel != wxNOT_FOUND) | |
653 | { | |
654 | currBook->DeletePage(sel); | |
655 | } | |
656 | } | |
0b4d4194 JS |
657 | } |
658 | ||
9133965e | 659 | void MyFrame::OnDeleteLastPage(wxCommandEvent& WXUNUSED(event)) |
0b4d4194 | 660 | { |
61c083e7 | 661 | wxBookCtrlBase *currBook = GetCurrentBook(); |
d22699b5 | 662 | |
9133965e | 663 | if ( currBook ) |
c1dfe277 | 664 | { |
9133965e | 665 | int page = currBook->GetPageCount(); |
c1dfe277 | 666 | |
9133965e WS |
667 | if ( page != 0 ) |
668 | { | |
669 | currBook->DeletePage(page - 1); | |
c1dfe277 | 670 | } |
c1dfe277 | 671 | } |
9133965e | 672 | } |
0b4d4194 | 673 | |
9133965e WS |
674 | void MyFrame::OnNextPage(wxCommandEvent& WXUNUSED(event)) |
675 | { | |
61c083e7 | 676 | wxBookCtrlBase *currBook = GetCurrentBook(); |
0b4d4194 | 677 | |
9133965e WS |
678 | if ( currBook ) |
679 | { | |
680 | currBook->AdvanceSelection(); | |
681 | } | |
0b4d4194 JS |
682 | } |
683 | ||
c1dfe277 | 684 | void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) ) |
96d37807 | 685 | { |
9133965e WS |
686 | static int s_nPages = wxNOT_FOUND; |
687 | static int s_nSel = wxNOT_FOUND; | |
61c083e7 | 688 | static wxBookCtrlBase *s_currBook = NULL; |
9133965e | 689 | |
61c083e7 | 690 | wxBookCtrlBase *currBook = GetCurrentBook(); |
96d37807 | 691 | |
9133965e WS |
692 | int nPages = currBook ? currBook->GetPageCount() : 0; |
693 | int nSel = currBook ? currBook->GetSelection() : wxNOT_FOUND; | |
694 | ||
695 | if ( nPages != s_nPages || nSel != s_nSel || s_currBook != currBook ) | |
96d37807 VZ |
696 | { |
697 | s_nPages = nPages; | |
698 | s_nSel = nSel; | |
9133965e WS |
699 | s_currBook = currBook; |
700 | ||
701 | wxString selection; | |
702 | if ( nSel == wxNOT_FOUND ) | |
703 | selection << wxT("not found"); | |
704 | else | |
705 | selection << nSel; | |
96d37807 VZ |
706 | |
707 | wxString title; | |
9133965e | 708 | title.Printf(wxT("Notebook and friends (%d pages, selection: %s)"), nPages, selection.c_str()); |
96d37807 VZ |
709 | |
710 | SetTitle(title); | |
711 | } | |
712 | } | |
3957448a | 713 | |
eca15c0d VZ |
714 | void MyFrame::OnBookCtrl(wxBookCtrlBaseEvent& event) |
715 | { | |
716 | static const struct EventInfo | |
717 | { | |
718 | wxEventType typeChanged, | |
719 | typeChanging; | |
720 | const wxChar *name; | |
721 | } events[] = | |
722 | { | |
02161c7c | 723 | #if wxUSE_NOTEBOOK |
eca15c0d VZ |
724 | { |
725 | wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, | |
726 | wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, | |
727 | _T("wxNotebook") | |
728 | }, | |
729 | #endif // wxUSE_NOTEBOOK | |
02161c7c | 730 | #if wxUSE_LISTBOOK |
eca15c0d VZ |
731 | { |
732 | wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED, | |
733 | wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, | |
734 | _T("wxListbook") | |
735 | }, | |
736 | #endif // wxUSE_LISTBOOK | |
136bafcd VZ |
737 | #if wxUSE_CHOICEBOOK |
738 | { | |
739 | wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED, | |
740 | wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, | |
741 | _T("wxChoicebook") | |
742 | }, | |
743 | #endif // wxUSE_CHOICEBOOK | |
eca15c0d VZ |
744 | #if wxUSE_TREEBOOK |
745 | { | |
746 | wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED, | |
747 | wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING, | |
748 | _T("wxTreebook") | |
749 | }, | |
750 | #endif // wxUSE_TREEBOOK | |
751 | }; | |
752 | ||
753 | ||
754 | wxString nameEvent, | |
755 | nameControl, | |
756 | veto; | |
757 | const wxEventType eventType = event.GetEventType(); | |
758 | for ( size_t n = 0; n < WXSIZEOF(events); n++ ) | |
759 | { | |
760 | const EventInfo& ei = events[n]; | |
761 | if ( eventType == ei.typeChanged ) | |
762 | { | |
763 | nameEvent = wxT("Changed"); | |
764 | } | |
765 | else if ( eventType == ei.typeChanging ) | |
766 | { | |
767 | const int idx = event.GetOldSelection(); | |
582ca353 VZ |
768 | |
769 | // NB: can't use wxStaticCast here as wxBookCtrlBase is not in | |
770 | // wxRTTI | |
eca15c0d | 771 | const wxBookCtrlBase * const |
582ca353 | 772 | book = wx_static_cast(wxBookCtrlBase *, event.GetEventObject()); |
eca15c0d VZ |
773 | if ( idx != wxNOT_FOUND && |
774 | book && book->GetPageText(idx) == VETO_PAGE_NAME ) | |
775 | { | |
776 | if ( wxMessageBox | |
777 | ( | |
778 | wxT("Are you sure you want to leave this page?\n") | |
779 | wxT("(This demonstrates veto-ing)"), | |
780 | wxT("Notebook sample"), | |
781 | wxICON_QUESTION | wxYES_NO, | |
782 | this | |
783 | ) != wxYES ) | |
784 | { | |
785 | event.Veto(); | |
786 | veto = _T(" (vetoed)"); | |
787 | } | |
788 | } | |
789 | ||
790 | nameEvent = wxT("Changing"); | |
791 | } | |
792 | else // skip end of the loop | |
793 | { | |
794 | continue; | |
795 | } | |
796 | ||
797 | nameControl = ei.name; | |
798 | break; | |
799 | } | |
800 | ||
801 | static int s_num = 0; | |
802 | ||
803 | wxLogMessage(wxT("Event #%d: %s: %s (%d) new sel %d, old %d%s"), | |
804 | ++s_num, | |
805 | nameControl.c_str(), | |
806 | nameEvent.c_str(), | |
807 | eventType, | |
808 | event.GetSelection(), | |
809 | event.GetOldSelection(), | |
810 | veto.c_str()); | |
811 | ||
812 | #if USE_LOG | |
813 | m_text->SetInsertionPointEnd(); | |
02161c7c | 814 | #endif |
eca15c0d | 815 | } |