1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/notebook.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "notebook.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/string.h"
31 #include "wx/imaglist.h"
33 #include "wx/control.h"
34 #include "wx/notebook.h"
37 #include "wx/msw/private.h"
39 // Windows standard headers
41 #error "wxNotebook is only supported Windows 95 and above"
44 #include <windowsx.h> // for SetWindowFont
46 #ifdef __GNUWIN32_OLD__
47 #include "wx/msw/gnuwin32/extra.h"
50 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
54 #include "wx/msw/winundef.h"
57 #include "wx/msw/uxtheme.h"
59 #include "wx/radiobut.h"
60 #include "wx/radiobox.h"
61 #include "wx/checkbox.h"
62 #include "wx/bmpbuttn.h"
63 #include "wx/statline.h"
64 #include "wx/statbox.h"
65 #include "wx/stattext.h"
66 #include "wx/slider.h"
67 #include "wx/scrolwin.h"
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // check that the page index is valid
76 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
79 #define m_hwnd (HWND)GetHWND()
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // This is a work-around for missing defines in gcc-2.95 headers
87 #define TCS_RIGHT 0x0002
91 #define TCS_VERTICAL 0x0080
95 #define TCS_BOTTOM TCS_RIGHT
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
105 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
106 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
108 EVT_SIZE(wxNotebook::OnSize
)
110 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
112 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
115 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
116 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
123 // wxNotebook construction
124 // ----------------------------------------------------------------------------
126 // common part of all ctors
127 void wxNotebook::Init()
133 // default for dynamic class
134 wxNotebook::wxNotebook()
139 // the same arguments as for wxControl
140 wxNotebook::wxNotebook(wxWindow
*parent
,
145 const wxString
& name
)
149 Create(parent
, id
, pos
, size
, style
, name
);
153 bool wxNotebook::Create(wxWindow
*parent
,
158 const wxString
& name
)
160 // Does ComCtl32 support non-top tabs?
161 int verComCtl32
= wxApp::GetComCtl32Version();
162 if ( verComCtl32
< 470 || verComCtl32
>= 600 )
164 if (style
& wxNB_BOTTOM
)
165 style
&= ~wxNB_BOTTOM
;
167 if (style
& wxNB_LEFT
)
170 if (style
& wxNB_RIGHT
)
171 style
&= ~wxNB_RIGHT
;
174 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
175 wxDefaultValidator
, name
) )
178 if ( !MSWCreateControl(WC_TABCONTROL
, wxEmptyString
, pos
, size
) )
181 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE
)));
186 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
188 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
190 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
192 if ( style
& wxNB_MULTILINE
)
193 tabStyle
|= TCS_MULTILINE
;
194 if ( style
& wxNB_FIXEDWIDTH
)
195 tabStyle
|= TCS_FIXEDWIDTH
;
197 if ( style
& wxNB_BOTTOM
)
198 tabStyle
|= TCS_RIGHT
;
199 else if ( style
& wxNB_LEFT
)
200 tabStyle
|= TCS_VERTICAL
;
201 else if ( style
& wxNB_RIGHT
)
202 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
207 // note that we never want to have the default WS_EX_CLIENTEDGE style
208 // as it looks too ugly for the notebooks
215 // ----------------------------------------------------------------------------
216 // wxNotebook accessors
217 // ----------------------------------------------------------------------------
219 int wxNotebook::GetPageCount() const
222 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
224 return m_pages
.Count();
227 int wxNotebook::GetRowCount() const
229 return TabCtrl_GetRowCount(m_hwnd
);
232 int wxNotebook::SetSelection(int nPage
)
234 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
236 if ( nPage
!= m_nSelection
)
238 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
239 event
.SetSelection(nPage
);
240 event
.SetOldSelection(m_nSelection
);
241 event
.SetEventObject(this);
242 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
244 // program allows the page change
245 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
246 (void)GetEventHandler()->ProcessEvent(event
);
248 TabCtrl_SetCurSel(m_hwnd
, nPage
);
255 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
257 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
260 tcItem
.mask
= TCIF_TEXT
;
261 tcItem
.pszText
= (wxChar
*)strText
.c_str();
263 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
266 wxString
wxNotebook::GetPageText(int nPage
) const
268 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
272 tcItem
.mask
= TCIF_TEXT
;
273 tcItem
.pszText
= buf
;
274 tcItem
.cchTextMax
= WXSIZEOF(buf
);
277 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
278 str
= tcItem
.pszText
;
283 int wxNotebook::GetPageImage(int nPage
) const
285 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
288 tcItem
.mask
= TCIF_IMAGE
;
290 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
293 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
295 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
298 tcItem
.mask
= TCIF_IMAGE
;
299 tcItem
.iImage
= nImage
;
301 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
304 void wxNotebook::SetImageList(wxImageList
* imageList
)
306 wxNotebookBase::SetImageList(imageList
);
310 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
314 // ----------------------------------------------------------------------------
315 // wxNotebook size settings
316 // ----------------------------------------------------------------------------
318 void wxNotebook::SetPageSize(const wxSize
& size
)
320 // transform the page size into the notebook size
327 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
330 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
333 void wxNotebook::SetPadding(const wxSize
& padding
)
335 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
338 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
340 void wxNotebook::SetTabSize(const wxSize
& sz
)
342 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
345 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
347 wxSize sizeTotal
= sizePage
;
349 // We need to make getting tab size part of the wxWindows API.
350 wxSize
tabSize(0, 0);
351 if (GetPageCount() > 0)
354 TabCtrl_GetItemRect((HWND
) GetHWND(), 0, & rect
);
355 tabSize
.x
= rect
.right
- rect
.left
;
356 tabSize
.y
= rect
.bottom
- rect
.top
;
358 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
360 sizeTotal
.x
+= tabSize
.x
+ 7;
366 sizeTotal
.y
+= tabSize
.y
+ 7;
372 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
374 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
380 // get the page size from the notebook size
381 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
382 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
384 page
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
387 // ----------------------------------------------------------------------------
388 // wxNotebook operations
389 // ----------------------------------------------------------------------------
391 // remove one page from the notebook, without deleting
392 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
394 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
398 TabCtrl_DeleteItem(m_hwnd
, nPage
);
400 if ( m_pages
.IsEmpty() )
402 // no selection any more, the notebook becamse empty
405 else // notebook still not empty
407 // change the selected page if it was deleted or became invalid
409 if ( m_nSelection
== GetPageCount() )
411 // last page deleted, make the new last page the new selection
412 selNew
= m_nSelection
- 1;
414 else if ( nPage
<= m_nSelection
)
416 // we must show another page, even if it has the same index
417 selNew
= m_nSelection
;
419 else // nothing changes for the currently selected page
423 // we still must refresh the current page: this needs to be done
424 // for some unknown reason if the tab control shows the up-down
425 // control (i.e. when there are too many pages) -- otherwise after
426 // deleting a page nothing at all is shown
427 if (m_nSelection
>= 0)
428 m_pages
[m_nSelection
]->Refresh();
433 // m_nSelection must be always valid so reset it before calling
436 SetSelection(selNew
);
444 bool wxNotebook::DeleteAllPages()
446 int nPageCount
= GetPageCount();
448 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
449 delete m_pages
[nPage
];
453 TabCtrl_DeleteAllItems(m_hwnd
);
460 // same as AddPage() but does it at given position
461 bool wxNotebook::InsertPage(int nPage
,
462 wxNotebookPage
*pPage
,
463 const wxString
& strText
,
467 wxCHECK_MSG( pPage
!= NULL
, FALSE
, _T("NULL page in wxNotebook::InsertPage") );
468 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
,
469 _T("invalid index in wxNotebook::InsertPage") );
471 wxASSERT_MSG( pPage
->GetParent() == this,
472 _T("notebook pages must have notebook as parent") );
474 #if wxUSE_UXTHEME && wxUSE_UXTHEME_AUTO
475 // Automatically apply the theme background,
476 // changing the colour of the panel to match the
477 // tab page colour. This won't work well with all
478 // themes but it's a start.
479 if (wxUxThemeEngine::Get() && pPage
->IsKindOf(CLASSINFO(wxPanel
)))
481 ApplyThemeBackground(pPage
, GetThemeBackgroundColour());
485 // add a new tab to the control
486 // ----------------------------
488 // init all fields to 0
490 wxZeroMemory(tcItem
);
492 // set the image, if any
495 tcItem
.mask
|= TCIF_IMAGE
;
496 tcItem
.iImage
= imageId
;
500 if ( !strText
.IsEmpty() )
502 tcItem
.mask
|= TCIF_TEXT
;
503 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
506 // fit the notebook page to the tab control's display area: this should be
507 // done before adding it to the notebook or TabCtrl_InsertItem() will
508 // change the notebooks size itself!
509 AdjustPageSize(pPage
);
511 // finally do insert it
512 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 )
514 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
519 // succeeded: save the pointer to the page
520 m_pages
.Insert(pPage
, nPage
);
522 // for the first page (only) we need to adjust the size again because the
523 // notebook size changed: the tabs which hadn't been there before are now
525 if ( m_pages
.GetCount() == 1 )
527 AdjustPageSize(pPage
);
530 // hide the page: unless it is selected, it shouldn't be shown (and if it
531 // is selected it will be shown later)
532 HWND hwnd
= GetWinHwnd(pPage
);
533 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
535 // this updates internal flag too -- otherwise it would get out of sync
536 // with the real state
540 // now deal with the selection
541 // ---------------------------
543 // if the inserted page is before the selected one, we must update the
544 // index of the selected page
545 if ( nPage
<= m_nSelection
)
547 // one extra page added
551 // some page should be selected: either this one or the first one if there
552 // is still no selection
556 else if ( m_nSelection
== -1 )
560 SetSelection(selNew
);
565 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
567 TC_HITTESTINFO hitTestInfo
;
568 hitTestInfo
.pt
.x
= pt
.x
;
569 hitTestInfo
.pt
.y
= pt
.y
;
570 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
576 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
577 *flags
|= wxNB_HITTEST_NOWHERE
;
578 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
579 *flags
|= wxNB_HITTEST_ONITEM
;
580 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
581 *flags
|= wxNB_HITTEST_ONICON
;
582 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
583 *flags
|= wxNB_HITTEST_ONLABEL
;
590 // ----------------------------------------------------------------------------
591 // wxNotebook callbacks
592 // ----------------------------------------------------------------------------
594 void wxNotebook::OnSize(wxSizeEvent
& event
)
596 // fit the notebook page to the tab control's display area
598 rc
.left
= rc
.top
= 0;
599 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
601 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
603 int width
= rc
.right
- rc
.left
,
604 height
= rc
.bottom
- rc
.top
;
605 size_t nCount
= m_pages
.Count();
606 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
607 wxNotebookPage
*pPage
= m_pages
[nPage
];
608 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
614 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
616 // is it our tab control?
617 if ( event
.GetEventObject() == this )
619 int sel
= event
.GetOldSelection();
621 m_pages
[sel
]->Show(FALSE
);
623 sel
= event
.GetSelection();
626 wxNotebookPage
*pPage
= m_pages
[sel
];
634 // we want to give others a chance to process this message as well
638 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
640 // this function is only called when the focus is explicitly set (i.e. from
641 // the program) to the notebook - in this case we don't need the
642 // complicated OnNavigationKey() logic because the programmer knows better
645 // set focus to the currently selected page if any
646 if ( m_nSelection
!= -1 )
647 m_pages
[m_nSelection
]->SetFocus();
652 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
654 if ( event
.IsWindowChange() ) {
656 AdvanceSelection(event
.GetDirection());
659 // we get this event in 2 cases
661 // a) one of our pages might have generated it because the user TABbed
662 // out from it in which case we should propagate the event upwards and
663 // our parent will take care of setting the focus to prev/next sibling
667 // b) the parent panel wants to give the focus to us so that we
668 // forward it to our selected page. We can't deal with this in
669 // OnSetFocus() because we don't know which direction the focus came
670 // from in this case and so can't choose between setting the focus to
671 // first or last panel child
672 wxWindow
*parent
= GetParent();
673 // the cast is here to fic a GCC ICE
674 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
676 // no, it doesn't come from child, case (b): forward to a page
677 if ( m_nSelection
!= -1 )
679 // so that the page knows that the event comes from it's parent
680 // and is being propagated downwards
681 event
.SetEventObject(this);
683 wxWindow
*page
= m_pages
[m_nSelection
];
684 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
688 //else: page manages focus inside it itself
692 // we have no pages - still have to give focus to _something_
698 // it comes from our child, case (a), pass to the parent
700 event
.SetCurrentFocus(this);
701 parent
->GetEventHandler()->ProcessEvent(event
);
707 // ----------------------------------------------------------------------------
708 // wxNotebook base class virtuals
709 // ----------------------------------------------------------------------------
711 #if wxUSE_CONSTRAINTS
713 // override these 2 functions to do nothing: everything is done in OnSize
715 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
717 // don't set the sizes of the pages - their correct size is not yet known
718 wxControl::SetConstraintSizes(FALSE
);
721 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
726 #endif // wxUSE_CONSTRAINTS
728 // ----------------------------------------------------------------------------
729 // wxNotebook Windows message handlers
730 // ----------------------------------------------------------------------------
732 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
733 WXWORD pos
, WXHWND control
)
735 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
740 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
743 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
745 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
747 NMHDR
* hdr
= (NMHDR
*)lParam
;
748 switch ( hdr
->code
) {
750 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
753 case TCN_SELCHANGING
:
754 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
758 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
761 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
762 event
.SetOldSelection(m_nSelection
);
763 event
.SetEventObject(this);
764 event
.SetInt(idCtrl
);
766 bool processed
= GetEventHandler()->ProcessEvent(event
);
767 *result
= !event
.IsAllowed();
771 // Windows only: attempts to get colour for UX theme page background
772 wxColour
wxNotebook::GetThemeBackgroundColour()
775 if (wxUxThemeEngine::Get())
777 WXHTHEME hTheme
= wxUxThemeEngine::Get()->m_pfnOpenThemeData(GetHWND(), L
"TAB");
780 // This is total guesswork.
781 // See PlatformSDK\Include\Tmschema.h for values
783 wxUxThemeEngine::Get()->
784 m_pfnGetThemeColor(hTheme
,
787 3821, /* FILLCOLORHINT */
790 wxUxThemeEngine::Get()->m_pfnCloseThemeData(hTheme
);
792 wxColour
colour(GetRValue(themeColor
), GetGValue(themeColor
), GetBValue(themeColor
));
797 return GetBackgroundColour();
800 // Windows only: attempts to apply the UX theme page background to this page
801 void wxNotebook::ApplyThemeBackground(wxWindow
* window
, const wxColour
& colour
)
804 // Don't set the background for buttons since this will
805 // switch it into ownerdraw mode
806 if (window
->IsKindOf(CLASSINFO(wxButton
)) && !window
->IsKindOf(CLASSINFO(wxBitmapButton
)))
807 // This is essential, otherwise you'll see dark grey
808 // corners in the buttons.
809 ((wxButton
*)window
)->wxControl::SetBackgroundColour(colour
);
810 else if (window
->IsKindOf(CLASSINFO(wxStaticText
)) ||
811 window
->IsKindOf(CLASSINFO(wxStaticBox
)) ||
812 window
->IsKindOf(CLASSINFO(wxStaticLine
)) ||
813 window
->IsKindOf(CLASSINFO(wxRadioButton
)) ||
814 window
->IsKindOf(CLASSINFO(wxRadioBox
)) ||
815 window
->IsKindOf(CLASSINFO(wxCheckBox
)) ||
816 window
->IsKindOf(CLASSINFO(wxBitmapButton
)) ||
817 window
->IsKindOf(CLASSINFO(wxSlider
)) ||
818 window
->IsKindOf(CLASSINFO(wxPanel
)) ||
819 (window
->IsKindOf(CLASSINFO(wxNotebook
)) && (window
!= this)) ||
820 window
->IsKindOf(CLASSINFO(wxScrolledWindow
))
823 window
->SetBackgroundColour(colour
);
826 for ( wxWindowList::compatibility_iterator node
= window
->GetChildren().GetFirst(); node
; node
= node
->GetNext() )
828 wxWindow
*child
= node
->GetData();
829 ApplyThemeBackground(child
, colour
);
837 #endif // wxUSE_NOTEBOOK