use a single wxBookCtrlEvent class for all wxBookCtrlBase-derived controls (#9667)
[wxWidgets.git] / src / generic / notebook.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/notebook.cpp
3 // Purpose: generic implementation of wxNotebook
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_NOTEBOOK
28
29 #include "wx/notebook.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
36 #endif
37
38 #include "wx/imaglist.h"
39 #include "wx/generic/tabg.h"
40
41 // ----------------------------------------------------------------------------
42 // macros
43 // ----------------------------------------------------------------------------
44
45 // check that the page index is valid
46 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
47
48 // ----------------------------------------------------------------------------
49 // event table
50 // ----------------------------------------------------------------------------
51
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
54
55 BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase)
56 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxNotebook::OnSelChange)
57 EVT_SIZE(wxNotebook::OnSize)
58 EVT_PAINT(wxNotebook::OnPaint)
59 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent)
60 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
61 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
62 END_EVENT_TABLE()
63
64 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxBookCtrlBase)
65
66 // ============================================================================
67 // implementation
68 // ============================================================================
69
70 // ============================================================================
71 // Private class
72 // ============================================================================
73
74 WX_DECLARE_HASH_MAP(int, wxNotebookPage*, wxIntegerHash, wxIntegerEqual,
75 wxIntToNotebookPageHashMap);
76
77 WX_DECLARE_HASH_MAP(wxNotebookPage*, int, wxPointerHash, wxPointerEqual,
78 wxNotebookPageToIntHashMap);
79
80 // This reuses wxTabView to draw the tabs.
81 class WXDLLEXPORT wxNotebookTabView: public wxTabView
82 {
83 DECLARE_DYNAMIC_CLASS(wxNotebookTabView)
84 public:
85 wxNotebookTabView(wxNotebook* notebook, long style = wxTAB_STYLE_DRAW_BOX | wxTAB_STYLE_COLOUR_INTERIOR);
86 virtual ~wxNotebookTabView(void);
87
88 // Called when a tab is activated
89 virtual void OnTabActivate(int activateId, int deactivateId);
90 // Allows vetoing
91 virtual bool OnTabPreActivate(int activateId, int deactivateId);
92
93 // map integer ids used by wxTabView to wxNotebookPage pointers
94 int GetId(wxNotebookPage *page);
95 wxNotebookPage *GetPage(int id) { return m_idToPage[id]; }
96
97 protected:
98 wxNotebook* m_notebook;
99
100 private:
101 wxIntToNotebookPageHashMap m_idToPage;
102 wxNotebookPageToIntHashMap m_pageToId;
103 int m_nextid;
104 };
105
106 static int GetPageId(wxTabView *tabview, wxNotebookPage *page)
107 {
108 return wx_static_cast(wxNotebookTabView*, tabview)->GetId(page);
109 }
110
111 // ----------------------------------------------------------------------------
112 // wxNotebook construction
113 // ----------------------------------------------------------------------------
114
115 // common part of all ctors
116 void wxNotebook::Init()
117 {
118 m_tabView = (wxNotebookTabView*) NULL;
119 m_nSelection = -1;
120 }
121
122 // default for dynamic class
123 wxNotebook::wxNotebook()
124 {
125 Init();
126 }
127
128 // the same arguments as for wxControl
129 wxNotebook::wxNotebook(wxWindow *parent,
130 wxWindowID id,
131 const wxPoint& pos,
132 const wxSize& size,
133 long style,
134 const wxString& name)
135 {
136 Init();
137
138 Create(parent, id, pos, size, style, name);
139 }
140
141 // Create() function
142 bool wxNotebook::Create(wxWindow *parent,
143 wxWindowID id,
144 const wxPoint& pos,
145 const wxSize& size,
146 long style,
147 const wxString& name)
148 {
149 // base init
150 SetName(name);
151
152 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
153 style |= wxBK_TOP;
154
155 m_windowId = id == wxID_ANY ? NewControlId() : id;
156
157 if (!wxControl::Create(parent, id, pos, size, style|wxNO_BORDER, wxDefaultValidator, name))
158 return false;
159
160 SetTabView(new wxNotebookTabView(this));
161
162 return true;
163 }
164
165 // dtor
166 wxNotebook::~wxNotebook()
167 {
168 delete m_tabView;
169 }
170
171 // ----------------------------------------------------------------------------
172 // wxNotebook accessors
173 // ----------------------------------------------------------------------------
174 int wxNotebook::GetRowCount() const
175 {
176 // TODO
177 return 0;
178 }
179
180 int wxNotebook::SetSelection(size_t nPage)
181 {
182 wxASSERT( IS_VALID_PAGE(nPage) );
183
184 wxNotebookPage* pPage = GetPage(nPage);
185
186 m_tabView->SetTabSelection(GetPageId(m_tabView, pPage));
187
188 // TODO
189 return 0;
190 }
191
192 int wxNotebook::ChangeSelection(size_t nPage)
193 {
194 // FIXME: currently it does generate events too
195 return SetSelection(nPage);
196 }
197
198 #if 0
199 void wxNotebook::AdvanceSelection(bool bForward)
200 {
201 int nSel = GetSelection();
202 int nMax = GetPageCount() - 1;
203 if ( bForward )
204 SetSelection(nSel == nMax ? 0 : nSel + 1);
205 else
206 SetSelection(nSel == 0 ? nMax : nSel - 1);
207 }
208 #endif
209
210 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
211 {
212 wxASSERT( IS_VALID_PAGE(nPage) );
213
214 wxNotebookPage* page = GetPage(nPage);
215 if (page)
216 {
217 m_tabView->SetTabText(GetPageId(m_tabView, page), strText);
218 Refresh();
219 return true;
220 }
221
222 return false;
223 }
224
225 wxString wxNotebook::GetPageText(size_t nPage) const
226 {
227 wxASSERT( IS_VALID_PAGE(nPage) );
228
229 wxNotebookPage* page = ((wxNotebook*)this)->GetPage(nPage);
230 if (page)
231 return m_tabView->GetTabText(GetPageId(m_tabView, page));
232 else
233 return wxEmptyString;
234 }
235
236 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage)) const
237 {
238 wxASSERT( IS_VALID_PAGE(nPage) );
239
240 // TODO
241 return 0;
242 }
243
244 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage),
245 int WXUNUSED(nImage))
246 {
247 wxASSERT( IS_VALID_PAGE(nPage) );
248
249 // TODO
250 return false;
251 }
252
253 // set the size (the same for all pages)
254 void wxNotebook::SetPageSize(const wxSize& WXUNUSED(size))
255 {
256 // TODO
257 }
258
259 // set the padding between tabs (in pixels)
260 void wxNotebook::SetPadding(const wxSize& WXUNUSED(padding))
261 {
262 // TODO
263 }
264
265 // set the size of the tabs for wxNB_FIXEDWIDTH controls
266 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
267 {
268 // TODO
269 }
270
271 // ----------------------------------------------------------------------------
272 // wxNotebook operations
273 // ----------------------------------------------------------------------------
274
275 // remove one page from the notebook and delete it
276 bool wxNotebook::DeletePage(size_t nPage)
277 {
278 wxCHECK( IS_VALID_PAGE(nPage), false );
279
280 if (m_nSelection != -1)
281 {
282 m_pages[m_nSelection]->Show(false);
283 m_pages[m_nSelection]->Lower();
284 }
285
286 wxNotebookPage* pPage = GetPage(nPage);
287
288 m_tabView->RemoveTab(GetPageId(m_tabView, pPage));
289
290 m_pages.Remove(pPage);
291 delete pPage;
292
293 if (m_pages.GetCount() == 0)
294 {
295 m_nSelection = -1;
296 m_tabView->SetTabSelection(-1, false);
297 }
298 else if (m_nSelection > -1)
299 {
300 m_nSelection = -1;
301
302 m_tabView->SetTabSelection(GetPageId(m_tabView, GetPage(0)), false);
303
304 if (m_nSelection != 0)
305 ChangePage(-1, 0);
306 }
307
308 RefreshLayout(false);
309
310 return true;
311 }
312
313 bool wxNotebook::DeletePage(wxNotebookPage* page)
314 {
315 int pagePos = FindPagePosition(page);
316 if (pagePos > -1)
317 return DeletePage(pagePos);
318 else
319 return false;
320 }
321
322 bool wxNotebook::RemovePage(size_t nPage)
323 {
324 return DoRemovePage(nPage) != NULL;
325 }
326
327 // remove one page from the notebook
328 wxWindow* wxNotebook::DoRemovePage(size_t nPage)
329 {
330 wxCHECK( IS_VALID_PAGE(nPage), NULL );
331
332 m_pages[nPage]->Show(false);
333 // m_pages[nPage]->Lower();
334
335 wxNotebookPage* pPage = GetPage(nPage);
336
337 m_tabView->RemoveTab(GetPageId(m_tabView, pPage));
338
339 m_pages.Remove(pPage);
340
341 if (m_pages.GetCount() == 0)
342 {
343 m_nSelection = -1;
344 m_tabView->SetTabSelection(-1, true);
345 }
346 else if (m_nSelection > -1)
347 {
348 // Only change the selection if the page we
349 // deleted was the selection.
350 if (nPage == (size_t)m_nSelection)
351 {
352 m_nSelection = -1;
353 // Select the first tab. Generates a ChangePage.
354 m_tabView->SetTabSelection(0, true);
355 }
356 else
357 {
358 // We must adjust which tab we think is selected.
359 // If greater than the page we deleted, it must be moved down
360 // a notch.
361 if (size_t(m_nSelection) > nPage)
362 m_nSelection -- ;
363 }
364 }
365
366 RefreshLayout(false);
367
368 return pPage;
369 }
370
371 bool wxNotebook::RemovePage(wxNotebookPage* page)
372 {
373 int pagePos = FindPagePosition(page);
374 if (pagePos > -1)
375 return RemovePage(pagePos);
376 else
377 return false;
378 }
379
380 // Find the position of the wxNotebookPage, -1 if not found.
381 int wxNotebook::FindPagePosition(wxNotebookPage* page) const
382 {
383 size_t nPageCount = GetPageCount();
384 size_t nPage;
385 for ( nPage = 0; nPage < nPageCount; nPage++ )
386 if (m_pages[nPage] == page)
387 return nPage;
388 return -1;
389 }
390
391 // remove all pages
392 bool wxNotebook::DeleteAllPages()
393 {
394 m_tabView->ClearTabs(true);
395
396 size_t nPageCount = GetPageCount();
397 size_t nPage;
398 for ( nPage = 0; nPage < nPageCount; nPage++ )
399 delete m_pages[nPage];
400
401 m_pages.Clear();
402
403 return true;
404 }
405
406 // same as AddPage() but does it at given position
407 bool wxNotebook::InsertPage(size_t nPage,
408 wxNotebookPage *pPage,
409 const wxString& strText,
410 bool bSelect,
411 int WXUNUSED(imageId))
412 {
413 wxASSERT( pPage != NULL );
414 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false );
415
416 m_tabView->AddTab(GetPageId(m_tabView, pPage), strText);
417
418 if (!bSelect)
419 pPage->Show(false);
420
421 // save the pointer to the page
422 m_pages.Insert(pPage, nPage);
423
424 if (bSelect)
425 {
426 // This will cause ChangePage to be called, via OnSelPage
427
428 m_tabView->SetTabSelection(GetPageId(m_tabView, pPage), true);
429 }
430
431 // some page must be selected: either this one or the first one if there is
432 // still no selection
433 if ( m_nSelection == -1 )
434 ChangePage(-1, 0);
435
436 RefreshLayout(false);
437
438 return true;
439 }
440
441 // ----------------------------------------------------------------------------
442 // wxNotebook callbacks
443 // ----------------------------------------------------------------------------
444
445 // @@@ OnSize() is used for setting the font when it's called for the first
446 // time because doing it in ::Create() doesn't work (for unknown reasons)
447 void wxNotebook::OnSize(wxSizeEvent& event)
448 {
449 static bool s_bFirstTime = true;
450 if ( s_bFirstTime ) {
451 // TODO: any first-time-size processing.
452 s_bFirstTime = false;
453 }
454
455 RefreshLayout();
456
457 // Processing continues to next OnSize
458 event.Skip();
459 }
460
461 // This was supposed to cure the non-display of the notebook
462 // until the user resizes the window.
463 // What's going on?
464 void wxNotebook::OnInternalIdle()
465 {
466 wxWindow::OnInternalIdle();
467
468 #if 0
469 static bool s_bFirstTime = true;
470 if ( s_bFirstTime ) {
471 /*
472 wxSize sz(GetSize());
473 sz.x ++;
474 SetSize(sz);
475 sz.x --;
476 SetSize(sz);
477 */
478
479 /*
480 wxSize sz(GetSize());
481 wxSizeEvent sizeEvent(sz, GetId());
482 sizeEvent.SetEventObject(this);
483 GetEventHandler()->ProcessEvent(sizeEvent);
484 Refresh();
485 */
486 s_bFirstTime = false;
487 }
488 #endif
489 }
490
491 // Implementation: calculate the layout of the view rect
492 // and resize the children if required
493 bool wxNotebook::RefreshLayout(bool force)
494 {
495 if (m_tabView)
496 {
497 wxRect oldRect = m_tabView->GetViewRect();
498
499 int cw, ch;
500 GetClientSize(& cw, & ch);
501
502 int tabHeight = m_tabView->GetTotalTabHeight();
503 wxRect rect;
504 rect.x = 4;
505 rect.y = tabHeight + 4;
506 rect.width = cw - 8;
507 rect.height = ch - 4 - rect.y ;
508
509 m_tabView->SetViewRect(rect);
510
511 m_tabView->LayoutTabs();
512
513 // Need to do it a 2nd time to get the tab height with
514 // the new view width, since changing the view width changes the
515 // tab layout.
516 tabHeight = m_tabView->GetTotalTabHeight();
517 rect.x = 4;
518 rect.y = tabHeight + 4;
519 rect.width = cw - 8;
520 rect.height = ch - 4 - rect.y ;
521
522 m_tabView->SetViewRect(rect);
523
524 m_tabView->LayoutTabs();
525
526 if (!force && (rect == oldRect))
527 return false;
528
529 // fit the notebook page to the tab control's display area
530
531 size_t nCount = m_pages.Count();
532 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
533 wxNotebookPage *pPage = m_pages[nPage];
534 wxRect clientRect = GetAvailableClientSize();
535 if (pPage->IsShown())
536 {
537 pPage->SetSize(clientRect.x, clientRect.y, clientRect.width, clientRect.height);
538 if ( pPage->GetAutoLayout() )
539 pPage->Layout();
540 }
541 }
542 Refresh();
543 }
544 return true;
545 }
546
547 void wxNotebook::OnSelChange(wxBookCtrlEvent& event)
548 {
549 // is it our tab control?
550 if ( event.GetEventObject() == this )
551 {
552 if (event.GetSelection() != m_nSelection)
553 ChangePage(event.GetOldSelection(), event.GetSelection());
554 }
555
556 // we want to give others a chance to process this message as well
557 event.Skip();
558 }
559
560 void wxNotebook::OnSetFocus(wxFocusEvent& event)
561 {
562 // set focus to the currently selected page if any
563 if ( m_nSelection != -1 )
564 m_pages[m_nSelection]->SetFocus();
565
566 event.Skip();
567 }
568
569 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
570 {
571 if ( event.IsWindowChange() ) {
572 // change pages
573 AdvanceSelection(event.GetDirection());
574 }
575 else {
576 // pass to the parent
577 if ( GetParent() ) {
578 event.SetCurrentFocus(this);
579 GetParent()->ProcessEvent(event);
580 }
581 }
582 }
583
584 // ----------------------------------------------------------------------------
585 // wxNotebook base class virtuals
586 // ----------------------------------------------------------------------------
587
588 // override these 2 functions to do nothing: everything is done in OnSize
589
590 void wxNotebook::SetConstraintSizes(bool /* recurse */)
591 {
592 // don't set the sizes of the pages - their correct size is not yet known
593 wxControl::SetConstraintSizes(false);
594 }
595
596 bool wxNotebook::DoPhase(int /* nPhase */)
597 {
598 return true;
599 }
600
601 void wxNotebook::Command(wxCommandEvent& WXUNUSED(event))
602 {
603 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
604 }
605
606 // ----------------------------------------------------------------------------
607 // wxNotebook helper functions
608 // ----------------------------------------------------------------------------
609
610 // hide the currently active panel and show the new one
611 void wxNotebook::ChangePage(int nOldSel, int nSel)
612 {
613 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
614 wxASSERT( nOldSel != nSel ); // impossible
615
616 if ( nOldSel != -1 ) {
617 m_pages[nOldSel]->Show(false);
618 m_pages[nOldSel]->Lower();
619 }
620
621 wxNotebookPage *pPage = m_pages[nSel];
622
623 wxRect clientRect = GetAvailableClientSize();
624 pPage->SetSize(clientRect.x, clientRect.y, clientRect.width, clientRect.height);
625
626 Refresh();
627
628 pPage->Show(true);
629 pPage->Raise();
630 pPage->SetFocus();
631
632 m_nSelection = nSel;
633 }
634
635 void wxNotebook::OnMouseEvent(wxMouseEvent& event)
636 {
637 if (m_tabView)
638 m_tabView->OnEvent(event);
639 }
640
641 void wxNotebook::OnPaint(wxPaintEvent& WXUNUSED(event) )
642 {
643 wxPaintDC dc(this);
644 if (m_tabView)
645 m_tabView->Draw(dc);
646 }
647
648 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
649 {
650 // MBN: since the total tab height is really a function of the
651 // width, this should really call
652 // GetTotalTabHeightPretendingWidthIs(), but the current
653 // implementation will suffice, provided the wxNotebook has been
654 // created with a sensible initial width.
655 return wxSize( sizePage.x + 12,
656 sizePage.y + m_tabView->GetTotalTabHeight() + 6 + 4 );
657 }
658
659 wxRect wxNotebook::GetAvailableClientSize()
660 {
661 int cw, ch;
662 GetClientSize(& cw, & ch);
663
664 int tabHeight = m_tabView->GetTotalTabHeight();
665
666 // TODO: these margins should be configurable.
667 wxRect rect;
668 rect.x = 6;
669 rect.y = tabHeight + 6;
670 rect.width = cw - 12;
671 rect.height = ch - 4 - rect.y ;
672
673 return rect;
674 }
675
676 /*
677 * wxNotebookTabView
678 */
679
680 IMPLEMENT_CLASS(wxNotebookTabView, wxTabView)
681
682 wxNotebookTabView::wxNotebookTabView(wxNotebook *notebook, long style)
683 : wxTabView(style), m_nextid(1)
684 {
685 m_notebook = notebook;
686
687 m_notebook->SetTabView(this);
688
689 SetWindow(m_notebook);
690 }
691
692 wxNotebookTabView::~wxNotebookTabView(void)
693 {
694 }
695
696 int wxNotebookTabView::GetId(wxNotebookPage *page)
697 {
698 int& id = m_pageToId[page];
699
700 if (!id)
701 {
702 id = m_nextid++;
703 m_idToPage[id] = page;
704 }
705
706 return id;
707 }
708
709 // Called when a tab is activated
710 void wxNotebookTabView::OnTabActivate(int activateId, int deactivateId)
711 {
712 if (!m_notebook)
713 return;
714
715 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_notebook->GetId());
716
717 // Translate from wxTabView's ids (which aren't position-dependent)
718 // to wxNotebook's (which are).
719 wxNotebookPage* pActive = GetPage(activateId);
720 wxNotebookPage* pDeactive = GetPage(deactivateId);
721
722 int activatePos = m_notebook->FindPagePosition(pActive);
723 int deactivatePos = m_notebook->FindPagePosition(pDeactive);
724
725 event.SetEventObject(m_notebook);
726 event.SetSelection(activatePos);
727 event.SetOldSelection(deactivatePos);
728 m_notebook->GetEventHandler()->ProcessEvent(event);
729 }
730
731 // Allows Vetoing
732 bool wxNotebookTabView::OnTabPreActivate(int activateId, int deactivateId)
733 {
734 bool retval = true;
735
736 if (m_notebook)
737 {
738 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_notebook->GetId());
739
740 // Translate from wxTabView's ids (which aren't position-dependent)
741 // to wxNotebook's (which are).
742 wxNotebookPage* pActive = GetPage(activateId);
743 wxNotebookPage* pDeactive = GetPage(deactivateId);
744
745 int activatePos = m_notebook->FindPagePosition(pActive);
746 int deactivatePos = m_notebook->FindPagePosition(pDeactive);
747
748 event.SetEventObject(m_notebook);
749 event.SetSelection(activatePos);
750 event.SetOldSelection(deactivatePos);
751 if (m_notebook->GetEventHandler()->ProcessEvent(event))
752 {
753 retval = event.IsAllowed();
754 }
755 }
756 return retval;
757 }
758
759 #endif // wxUSE_NOTEBOOK