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 #if wxUSE_EXTENDED_RTTI
116 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook
, wxControl
,"wx/notebook.h")
118 WX_BEGIN_PROPERTIES_TABLE(wxNotebook
)
119 WX_END_PROPERTIES_TABLE()
121 WX_BEGIN_HANDLERS_TABLE(wxNotebook
)
122 WX_END_HANDLERS_TABLE()
124 WX_CONSTRUCTOR_4( wxNotebook
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
)
127 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
129 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
136 // wxNotebook construction
137 // ----------------------------------------------------------------------------
139 // common part of all ctors
140 void wxNotebook::Init()
146 // default for dynamic class
147 wxNotebook::wxNotebook()
152 // the same arguments as for wxControl
153 wxNotebook::wxNotebook(wxWindow
*parent
,
158 const wxString
& name
)
162 Create(parent
, id
, pos
, size
, style
, name
);
166 bool wxNotebook::Create(wxWindow
*parent
,
171 const wxString
& name
)
173 // Does ComCtl32 support non-top tabs?
174 int verComCtl32
= wxApp::GetComCtl32Version();
175 if ( verComCtl32
< 470 || verComCtl32
>= 600 )
177 if (style
& wxNB_BOTTOM
)
178 style
&= ~wxNB_BOTTOM
;
180 if (style
& wxNB_LEFT
)
183 if (style
& wxNB_RIGHT
)
184 style
&= ~wxNB_RIGHT
;
187 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
188 wxDefaultValidator
, name
) )
191 if ( !MSWCreateControl(WC_TABCONTROL
, wxEmptyString
, pos
, size
) )
194 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE
)));
199 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
201 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
203 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
205 if ( style
& wxNB_MULTILINE
)
206 tabStyle
|= TCS_MULTILINE
;
207 if ( style
& wxNB_FIXEDWIDTH
)
208 tabStyle
|= TCS_FIXEDWIDTH
;
210 if ( style
& wxNB_BOTTOM
)
211 tabStyle
|= TCS_RIGHT
;
212 else if ( style
& wxNB_LEFT
)
213 tabStyle
|= TCS_VERTICAL
;
214 else if ( style
& wxNB_RIGHT
)
215 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
220 // note that we never want to have the default WS_EX_CLIENTEDGE style
221 // as it looks too ugly for the notebooks
228 // ----------------------------------------------------------------------------
229 // wxNotebook accessors
230 // ----------------------------------------------------------------------------
232 int wxNotebook::GetPageCount() const
235 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
237 return m_pages
.Count();
240 int wxNotebook::GetRowCount() const
242 return TabCtrl_GetRowCount(m_hwnd
);
245 int wxNotebook::SetSelection(int nPage
)
247 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
249 if ( nPage
!= m_nSelection
)
251 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
252 event
.SetSelection(nPage
);
253 event
.SetOldSelection(m_nSelection
);
254 event
.SetEventObject(this);
255 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
257 // program allows the page change
258 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
259 (void)GetEventHandler()->ProcessEvent(event
);
261 TabCtrl_SetCurSel(m_hwnd
, nPage
);
268 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
270 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
273 tcItem
.mask
= TCIF_TEXT
;
274 tcItem
.pszText
= (wxChar
*)strText
.c_str();
276 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
279 wxString
wxNotebook::GetPageText(int nPage
) const
281 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
285 tcItem
.mask
= TCIF_TEXT
;
286 tcItem
.pszText
= buf
;
287 tcItem
.cchTextMax
= WXSIZEOF(buf
);
290 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
291 str
= tcItem
.pszText
;
296 int wxNotebook::GetPageImage(int nPage
) const
298 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
301 tcItem
.mask
= TCIF_IMAGE
;
303 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
306 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
308 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
311 tcItem
.mask
= TCIF_IMAGE
;
312 tcItem
.iImage
= nImage
;
314 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
317 void wxNotebook::SetImageList(wxImageList
* imageList
)
319 wxNotebookBase::SetImageList(imageList
);
323 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
327 // ----------------------------------------------------------------------------
328 // wxNotebook size settings
329 // ----------------------------------------------------------------------------
331 void wxNotebook::SetPageSize(const wxSize
& size
)
333 // transform the page size into the notebook size
340 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
343 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
346 void wxNotebook::SetPadding(const wxSize
& padding
)
348 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
351 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
353 void wxNotebook::SetTabSize(const wxSize
& sz
)
355 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
358 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
360 wxSize sizeTotal
= sizePage
;
362 // We need to make getting tab size part of the wxWindows API.
363 wxSize
tabSize(0, 0);
364 if (GetPageCount() > 0)
367 TabCtrl_GetItemRect((HWND
) GetHWND(), 0, & rect
);
368 tabSize
.x
= rect
.right
- rect
.left
;
369 tabSize
.y
= rect
.bottom
- rect
.top
;
371 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
373 sizeTotal
.x
+= tabSize
.x
+ 7;
379 sizeTotal
.y
+= tabSize
.y
+ 7;
385 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
387 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
393 // get the page size from the notebook size
394 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
395 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
397 page
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
400 // ----------------------------------------------------------------------------
401 // wxNotebook operations
402 // ----------------------------------------------------------------------------
404 // remove one page from the notebook, without deleting
405 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
407 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
411 TabCtrl_DeleteItem(m_hwnd
, nPage
);
413 if ( m_pages
.IsEmpty() )
415 // no selection any more, the notebook becamse empty
418 else // notebook still not empty
420 // change the selected page if it was deleted or became invalid
422 if ( m_nSelection
== GetPageCount() )
424 // last page deleted, make the new last page the new selection
425 selNew
= m_nSelection
- 1;
427 else if ( nPage
<= m_nSelection
)
429 // we must show another page, even if it has the same index
430 selNew
= m_nSelection
;
432 else // nothing changes for the currently selected page
436 // we still must refresh the current page: this needs to be done
437 // for some unknown reason if the tab control shows the up-down
438 // control (i.e. when there are too many pages) -- otherwise after
439 // deleting a page nothing at all is shown
440 if (m_nSelection
>= 0)
441 m_pages
[m_nSelection
]->Refresh();
446 // m_nSelection must be always valid so reset it before calling
449 SetSelection(selNew
);
457 bool wxNotebook::DeleteAllPages()
459 int nPageCount
= GetPageCount();
461 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
462 delete m_pages
[nPage
];
466 TabCtrl_DeleteAllItems(m_hwnd
);
473 // same as AddPage() but does it at given position
474 bool wxNotebook::InsertPage(int nPage
,
475 wxNotebookPage
*pPage
,
476 const wxString
& strText
,
480 wxCHECK_MSG( pPage
!= NULL
, FALSE
, _T("NULL page in wxNotebook::InsertPage") );
481 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
,
482 _T("invalid index in wxNotebook::InsertPage") );
484 wxASSERT_MSG( pPage
->GetParent() == this,
485 _T("notebook pages must have notebook as parent") );
487 #if wxUSE_UXTHEME && wxUSE_UXTHEME_AUTO
488 // Automatically apply the theme background,
489 // changing the colour of the panel to match the
490 // tab page colour. This won't work well with all
491 // themes but it's a start.
492 if (wxUxThemeEngine::Get() && pPage
->IsKindOf(CLASSINFO(wxPanel
)))
494 ApplyThemeBackground(pPage
, GetThemeBackgroundColour());
498 // add a new tab to the control
499 // ----------------------------
501 // init all fields to 0
503 wxZeroMemory(tcItem
);
505 // set the image, if any
508 tcItem
.mask
|= TCIF_IMAGE
;
509 tcItem
.iImage
= imageId
;
513 if ( !strText
.IsEmpty() )
515 tcItem
.mask
|= TCIF_TEXT
;
516 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
519 // fit the notebook page to the tab control's display area: this should be
520 // done before adding it to the notebook or TabCtrl_InsertItem() will
521 // change the notebooks size itself!
522 AdjustPageSize(pPage
);
524 // finally do insert it
525 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 )
527 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
532 // succeeded: save the pointer to the page
533 m_pages
.Insert(pPage
, nPage
);
535 // for the first page (only) we need to adjust the size again because the
536 // notebook size changed: the tabs which hadn't been there before are now
538 if ( m_pages
.GetCount() == 1 )
540 AdjustPageSize(pPage
);
543 // hide the page: unless it is selected, it shouldn't be shown (and if it
544 // is selected it will be shown later)
545 HWND hwnd
= GetWinHwnd(pPage
);
546 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
548 // this updates internal flag too -- otherwise it would get out of sync
549 // with the real state
553 // now deal with the selection
554 // ---------------------------
556 // if the inserted page is before the selected one, we must update the
557 // index of the selected page
558 if ( nPage
<= m_nSelection
)
560 // one extra page added
564 // some page should be selected: either this one or the first one if there
565 // is still no selection
569 else if ( m_nSelection
== -1 )
573 SetSelection(selNew
);
578 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
580 TC_HITTESTINFO hitTestInfo
;
581 hitTestInfo
.pt
.x
= pt
.x
;
582 hitTestInfo
.pt
.y
= pt
.y
;
583 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
589 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
590 *flags
|= wxNB_HITTEST_NOWHERE
;
591 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
592 *flags
|= wxNB_HITTEST_ONITEM
;
593 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
594 *flags
|= wxNB_HITTEST_ONICON
;
595 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
596 *flags
|= wxNB_HITTEST_ONLABEL
;
603 // ----------------------------------------------------------------------------
604 // wxNotebook callbacks
605 // ----------------------------------------------------------------------------
607 void wxNotebook::OnSize(wxSizeEvent
& event
)
609 // fit the notebook page to the tab control's display area
611 rc
.left
= rc
.top
= 0;
612 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
614 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
616 int width
= rc
.right
- rc
.left
,
617 height
= rc
.bottom
- rc
.top
;
618 size_t nCount
= m_pages
.Count();
619 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
620 wxNotebookPage
*pPage
= m_pages
[nPage
];
621 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
627 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
629 // is it our tab control?
630 if ( event
.GetEventObject() == this )
632 int sel
= event
.GetOldSelection();
634 m_pages
[sel
]->Show(FALSE
);
636 sel
= event
.GetSelection();
639 wxNotebookPage
*pPage
= m_pages
[sel
];
647 // we want to give others a chance to process this message as well
651 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
653 // this function is only called when the focus is explicitly set (i.e. from
654 // the program) to the notebook - in this case we don't need the
655 // complicated OnNavigationKey() logic because the programmer knows better
658 // set focus to the currently selected page if any
659 if ( m_nSelection
!= -1 )
660 m_pages
[m_nSelection
]->SetFocus();
665 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
667 if ( event
.IsWindowChange() ) {
669 AdvanceSelection(event
.GetDirection());
672 // we get this event in 2 cases
674 // a) one of our pages might have generated it because the user TABbed
675 // out from it in which case we should propagate the event upwards and
676 // our parent will take care of setting the focus to prev/next sibling
680 // b) the parent panel wants to give the focus to us so that we
681 // forward it to our selected page. We can't deal with this in
682 // OnSetFocus() because we don't know which direction the focus came
683 // from in this case and so can't choose between setting the focus to
684 // first or last panel child
685 wxWindow
*parent
= GetParent();
686 // the cast is here to fic a GCC ICE
687 if ( ((wxWindow
*)event
.GetEventObject()) == parent
)
689 // no, it doesn't come from child, case (b): forward to a page
690 if ( m_nSelection
!= -1 )
692 // so that the page knows that the event comes from it's parent
693 // and is being propagated downwards
694 event
.SetEventObject(this);
696 wxWindow
*page
= m_pages
[m_nSelection
];
697 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
701 //else: page manages focus inside it itself
705 // we have no pages - still have to give focus to _something_
711 // it comes from our child, case (a), pass to the parent
713 event
.SetCurrentFocus(this);
714 parent
->GetEventHandler()->ProcessEvent(event
);
720 // ----------------------------------------------------------------------------
721 // wxNotebook base class virtuals
722 // ----------------------------------------------------------------------------
724 #if wxUSE_CONSTRAINTS
726 // override these 2 functions to do nothing: everything is done in OnSize
728 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
730 // don't set the sizes of the pages - their correct size is not yet known
731 wxControl::SetConstraintSizes(FALSE
);
734 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
739 #endif // wxUSE_CONSTRAINTS
741 // ----------------------------------------------------------------------------
742 // wxNotebook Windows message handlers
743 // ----------------------------------------------------------------------------
745 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
746 WXWORD pos
, WXHWND control
)
748 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
753 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
756 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
758 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
760 NMHDR
* hdr
= (NMHDR
*)lParam
;
761 switch ( hdr
->code
) {
763 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
766 case TCN_SELCHANGING
:
767 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
771 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
774 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
775 event
.SetOldSelection(m_nSelection
);
776 event
.SetEventObject(this);
777 event
.SetInt(idCtrl
);
779 bool processed
= GetEventHandler()->ProcessEvent(event
);
780 *result
= !event
.IsAllowed();
784 // Windows only: attempts to get colour for UX theme page background
785 wxColour
wxNotebook::GetThemeBackgroundColour()
788 if (wxUxThemeEngine::Get())
790 wxUxThemeHandle
hTheme(this, L
"TAB");
793 // This is total guesswork.
794 // See PlatformSDK\Include\Tmschema.h for values
796 wxUxThemeEngine::Get()->GetThemeColor
801 3821, /* FILLCOLORHINT */
805 wxColour
colour(GetRValue(themeColor
), GetGValue(themeColor
), GetBValue(themeColor
));
809 #endif // wxUSE_UXTHEME
811 return GetBackgroundColour();
814 // Windows only: attempts to apply the UX theme page background to this page
815 void wxNotebook::ApplyThemeBackground(wxWindow
* window
, const wxColour
& colour
)
818 // Don't set the background for buttons since this will
819 // switch it into ownerdraw mode
820 if (window
->IsKindOf(CLASSINFO(wxButton
)) && !window
->IsKindOf(CLASSINFO(wxBitmapButton
)))
821 // This is essential, otherwise you'll see dark grey
822 // corners in the buttons.
823 ((wxButton
*)window
)->wxControl::SetBackgroundColour(colour
);
824 else if (window
->IsKindOf(CLASSINFO(wxStaticText
)) ||
825 window
->IsKindOf(CLASSINFO(wxStaticBox
)) ||
826 window
->IsKindOf(CLASSINFO(wxStaticLine
)) ||
827 window
->IsKindOf(CLASSINFO(wxRadioButton
)) ||
828 window
->IsKindOf(CLASSINFO(wxRadioBox
)) ||
829 window
->IsKindOf(CLASSINFO(wxCheckBox
)) ||
830 window
->IsKindOf(CLASSINFO(wxBitmapButton
)) ||
831 window
->IsKindOf(CLASSINFO(wxSlider
)) ||
832 window
->IsKindOf(CLASSINFO(wxPanel
)) ||
833 (window
->IsKindOf(CLASSINFO(wxNotebook
)) && (window
!= this)) ||
834 window
->IsKindOf(CLASSINFO(wxScrolledWindow
))
837 window
->SetBackgroundColour(colour
);
840 for ( wxWindowList::compatibility_iterator node
= window
->GetChildren().GetFirst(); node
; node
= node
->GetNext() )
842 wxWindow
*child
= node
->GetData();
843 ApplyThemeBackground(child
, colour
);
851 #endif // wxUSE_NOTEBOOK