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"
36 #include "wx/msw/private.h"
38 // Windows standard headers
40 #error "wxNotebook is only supported Windows 95 and above"
43 #include <windowsx.h> // for SetWindowFont
45 #ifdef __GNUWIN32_OLD__
46 #include "wx/msw/gnuwin32/extra.h"
49 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
53 #include "wx/msw/winundef.h"
56 #include "wx/msw/uxtheme.h"
58 #include "wx/radiobut.h"
59 #include "wx/radiobox.h"
60 #include "wx/checkbox.h"
61 #include "wx/bmpbuttn.h"
62 #include "wx/statline.h"
63 #include "wx/statbox.h"
64 #include "wx/stattext.h"
65 #include "wx/slider.h"
66 #include "wx/scrolwin.h"
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // check that the page index is valid
75 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
78 #define m_hwnd (HWND)GetHWND()
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // This is a work-around for missing defines in gcc-2.95 headers
86 #define TCS_RIGHT 0x0002
90 #define TCS_VERTICAL 0x0080
94 #define TCS_BOTTOM TCS_RIGHT
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
104 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
105 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
107 EVT_SIZE(wxNotebook::OnSize
)
109 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
111 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
114 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
115 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
122 // wxNotebook construction
123 // ----------------------------------------------------------------------------
125 // common part of all ctors
126 void wxNotebook::Init()
132 // default for dynamic class
133 wxNotebook::wxNotebook()
138 // the same arguments as for wxControl
139 wxNotebook::wxNotebook(wxWindow
*parent
,
144 const wxString
& name
)
148 Create(parent
, id
, pos
, size
, style
, name
);
152 bool wxNotebook::Create(wxWindow
*parent
,
157 const wxString
& name
)
160 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
161 wxDefaultValidator
, name
) )
164 if ( !MSWCreateControl(WC_TABCONTROL
, _T(""), pos
, size
) )
167 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE
)));
172 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
174 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
176 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
178 if ( style
& wxNB_MULTILINE
)
179 tabStyle
|= TCS_MULTILINE
;
180 if ( style
& wxNB_FIXEDWIDTH
)
181 tabStyle
|= TCS_FIXEDWIDTH
;
183 if ( style
& wxNB_BOTTOM
)
184 tabStyle
|= TCS_RIGHT
;
185 else if ( style
& wxNB_LEFT
)
186 tabStyle
|= TCS_VERTICAL
;
187 else if ( style
& wxNB_RIGHT
)
188 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
193 // note that we never want to have the default WS_EX_CLIENTEDGE style
194 // as it looks too ugly for the notebooks
201 // ----------------------------------------------------------------------------
202 // wxNotebook accessors
203 // ----------------------------------------------------------------------------
205 int wxNotebook::GetPageCount() const
208 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
210 return m_pages
.Count();
213 int wxNotebook::GetRowCount() const
215 return TabCtrl_GetRowCount(m_hwnd
);
218 int wxNotebook::SetSelection(int nPage
)
220 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
222 if ( nPage
!= m_nSelection
)
224 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
225 event
.SetSelection(nPage
);
226 event
.SetOldSelection(m_nSelection
);
227 event
.SetEventObject(this);
228 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
230 // program allows the page change
231 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
232 (void)GetEventHandler()->ProcessEvent(event
);
234 TabCtrl_SetCurSel(m_hwnd
, nPage
);
241 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
243 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
246 tcItem
.mask
= TCIF_TEXT
;
247 tcItem
.pszText
= (wxChar
*)strText
.c_str();
249 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
252 wxString
wxNotebook::GetPageText(int nPage
) const
254 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
258 tcItem
.mask
= TCIF_TEXT
;
259 tcItem
.pszText
= buf
;
260 tcItem
.cchTextMax
= WXSIZEOF(buf
);
263 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
264 str
= tcItem
.pszText
;
269 int wxNotebook::GetPageImage(int nPage
) const
271 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
274 tcItem
.mask
= TCIF_IMAGE
;
276 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
279 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
281 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
284 tcItem
.mask
= TCIF_IMAGE
;
285 tcItem
.iImage
= nImage
;
287 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
290 void wxNotebook::SetImageList(wxImageList
* imageList
)
292 wxNotebookBase::SetImageList(imageList
);
296 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
300 // ----------------------------------------------------------------------------
301 // wxNotebook size settings
302 // ----------------------------------------------------------------------------
304 void wxNotebook::SetPageSize(const wxSize
& size
)
306 // transform the page size into the notebook size
313 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
316 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
319 void wxNotebook::SetPadding(const wxSize
& padding
)
321 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
324 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
326 void wxNotebook::SetTabSize(const wxSize
& sz
)
328 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
331 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
333 wxSize sizeTotal
= sizePage
;
335 // We need to make getting tab size part of the wxWindows API.
336 wxSize
tabSize(0, 0);
337 if (GetPageCount() > 0)
340 TabCtrl_GetItemRect((HWND
) GetHWND(), 0, & rect
);
341 tabSize
.x
= rect
.right
- rect
.left
;
342 tabSize
.y
= rect
.bottom
- rect
.top
;
344 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
346 sizeTotal
.x
+= tabSize
.x
+ 7;
352 sizeTotal
.y
+= tabSize
.y
+ 7;
358 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
360 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
366 // get the page size from the notebook size
367 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
368 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
370 page
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
373 // ----------------------------------------------------------------------------
374 // wxNotebook operations
375 // ----------------------------------------------------------------------------
377 // remove one page from the notebook, without deleting
378 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
380 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
384 TabCtrl_DeleteItem(m_hwnd
, nPage
);
386 if ( m_pages
.IsEmpty() )
388 // no selection any more, the notebook becamse empty
391 else // notebook still not empty
393 // change the selected page if it was deleted or became invalid
395 if ( m_nSelection
== GetPageCount() )
397 // last page deleted, make the new last page the new selection
398 selNew
= m_nSelection
- 1;
400 else if ( nPage
<= m_nSelection
)
402 // we must show another page, even if it has the same index
403 selNew
= m_nSelection
;
405 else // nothing changes for the currently selected page
409 // we still must refresh the current page: this needs to be done
410 // for some unknown reason if the tab control shows the up-down
411 // control (i.e. when there are too many pages) -- otherwise after
412 // deleting a page nothing at all is shown
413 m_pages
[m_nSelection
]->Refresh();
418 // m_nSelection must be always valid so reset it before calling
421 SetSelection(selNew
);
429 bool wxNotebook::DeleteAllPages()
431 int nPageCount
= GetPageCount();
433 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
434 delete m_pages
[nPage
];
438 TabCtrl_DeleteAllItems(m_hwnd
);
445 // same as AddPage() but does it at given position
446 bool wxNotebook::InsertPage(int nPage
,
447 wxNotebookPage
*pPage
,
448 const wxString
& strText
,
452 wxCHECK_MSG( pPage
!= NULL
, FALSE
, _T("NULL page in wxNotebook::InsertPage") );
453 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
,
454 _T("invalid index in wxNotebook::InsertPage") );
456 wxASSERT_MSG( pPage
->GetParent() == this,
457 _T("notebook pages must have notebook as parent") );
459 #if wxUSE_UXTHEME && wxUSE_UXTHEME_AUTO
460 // Automatically apply the theme background,
461 // changing the colour of the panel to match the
462 // tab page colour. This won't work well with all
463 // themes but it's a start.
464 if (wxUxThemeEngine::Get() && pPage
->IsKindOf(CLASSINFO(wxPanel
)))
466 ApplyThemeBackground(pPage
, GetThemeBackgroundColour());
470 // add a new tab to the control
471 // ----------------------------
473 // init all fields to 0
475 wxZeroMemory(tcItem
);
477 // set the image, if any
480 tcItem
.mask
|= TCIF_IMAGE
;
481 tcItem
.iImage
= imageId
;
485 if ( !strText
.IsEmpty() )
487 tcItem
.mask
|= TCIF_TEXT
;
488 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
491 // fit the notebook page to the tab control's display area: this should be
492 // done before adding it to the notebook or TabCtrl_InsertItem() will
493 // change the notebooks size itself!
494 AdjustPageSize(pPage
);
496 // finally do insert it
497 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 )
499 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
504 // succeeded: save the pointer to the page
505 m_pages
.Insert(pPage
, nPage
);
507 // for the first page (only) we need to adjust the size again because the
508 // notebook size changed: the tabs which hadn't been there before are now
510 if ( m_pages
.GetCount() == 1 )
512 AdjustPageSize(pPage
);
515 // hide the page: unless it is selected, it shouldn't be shown (and if it
516 // is selected it will be shown later)
517 HWND hwnd
= GetWinHwnd(pPage
);
518 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
520 // this updates internal flag too -- otherwise it would get out of sync
521 // with the real state
525 // now deal with the selection
526 // ---------------------------
528 // if the inserted page is before the selected one, we must update the
529 // index of the selected page
530 if ( nPage
<= m_nSelection
)
532 // one extra page added
536 // some page should be selected: either this one or the first one if there
537 // is still no selection
541 else if ( m_nSelection
== -1 )
545 SetSelection(selNew
);
551 int wxNotebook::HitTest(const wxPoint
& pt
, long& flags
)
553 TC_HITTESTINFO hitTestInfo
;
554 hitTestInfo
.pt
.x
= pt
.x
;
555 hitTestInfo
.pt
.y
= pt
.y
;
556 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
559 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
560 flags
|= wxNB_HITTEST_NOWHERE
;
561 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
562 flags
|= wxNB_HITTEST_ONICON
;
563 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
564 flags
|= wxNB_HITTEST_ONLABEL
;
569 // ----------------------------------------------------------------------------
570 // wxNotebook callbacks
571 // ----------------------------------------------------------------------------
573 void wxNotebook::OnSize(wxSizeEvent
& event
)
575 // fit the notebook page to the tab control's display area
577 rc
.left
= rc
.top
= 0;
578 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
580 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
582 int width
= rc
.right
- rc
.left
,
583 height
= rc
.bottom
- rc
.top
;
584 size_t nCount
= m_pages
.Count();
585 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
586 wxNotebookPage
*pPage
= m_pages
[nPage
];
587 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
593 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
595 // is it our tab control?
596 if ( event
.GetEventObject() == this )
598 int sel
= event
.GetOldSelection();
600 m_pages
[sel
]->Show(FALSE
);
602 sel
= event
.GetSelection();
605 wxNotebookPage
*pPage
= m_pages
[sel
];
613 // we want to give others a chance to process this message as well
617 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
619 // this function is only called when the focus is explicitly set (i.e. from
620 // the program) to the notebook - in this case we don't need the
621 // complicated OnNavigationKey() logic because the programmer knows better
624 // set focus to the currently selected page if any
625 if ( m_nSelection
!= -1 )
626 m_pages
[m_nSelection
]->SetFocus();
631 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
633 if ( event
.IsWindowChange() ) {
635 AdvanceSelection(event
.GetDirection());
638 // we get this event in 2 cases
640 // a) one of our pages might have generated it because the user TABbed
641 // out from it in which case we should propagate the event upwards and
642 // our parent will take care of setting the focus to prev/next sibling
646 // b) the parent panel wants to give the focus to us so that we
647 // forward it to our selected page. We can't deal with this in
648 // OnSetFocus() because we don't know which direction the focus came
649 // from in this case and so can't choose between setting the focus to
650 // first or last panel child
652 wxWindow
*parent
= GetParent();
653 if ( event
.GetEventObject() == parent
)
655 // no, it doesn't come from child, case (b): forward to a page
656 if ( m_nSelection
!= -1 )
658 // so that the page knows that the event comes from it's parent
659 // and is being propagated downwards
660 event
.SetEventObject(this);
662 wxWindow
*page
= m_pages
[m_nSelection
];
663 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
667 //else: page manages focus inside it itself
671 // we have no pages - still have to give focus to _something_
677 // it comes from our child, case (a), pass to the parent
679 event
.SetCurrentFocus(this);
680 parent
->GetEventHandler()->ProcessEvent(event
);
686 // ----------------------------------------------------------------------------
687 // wxNotebook base class virtuals
688 // ----------------------------------------------------------------------------
690 #if wxUSE_CONSTRAINTS
692 // override these 2 functions to do nothing: everything is done in OnSize
694 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
696 // don't set the sizes of the pages - their correct size is not yet known
697 wxControl::SetConstraintSizes(FALSE
);
700 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
705 #endif // wxUSE_CONSTRAINTS
707 // ----------------------------------------------------------------------------
708 // wxNotebook Windows message handlers
709 // ----------------------------------------------------------------------------
711 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
712 WXWORD pos
, WXHWND control
)
714 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
719 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
722 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
724 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
726 NMHDR
* hdr
= (NMHDR
*)lParam
;
727 switch ( hdr
->code
) {
729 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
732 case TCN_SELCHANGING
:
733 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
737 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
740 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
741 event
.SetOldSelection(m_nSelection
);
742 event
.SetEventObject(this);
743 event
.SetInt(idCtrl
);
745 bool processed
= GetEventHandler()->ProcessEvent(event
);
746 *result
= !event
.IsAllowed();
750 // Windows only: attempts to get colour for UX theme page background
751 wxColour
wxNotebook::GetThemeBackgroundColour()
754 if (wxUxThemeEngine::Get())
756 WXHTHEME hTheme
= wxUxThemeEngine::Get()->m_pfnOpenThemeData(GetHWND(), L
"TAB");
759 // This is total guesswork.
760 // See PlatformSDK\Include\Tmschema.h for values
762 wxUxThemeEngine::Get()->
763 m_pfnGetThemeColor(hTheme
,
766 3821, /* FILLCOLORHINT */
769 wxUxThemeEngine::Get()->m_pfnCloseThemeData(hTheme
);
771 wxColour
colour(GetRValue(themeColor
), GetGValue(themeColor
), GetBValue(themeColor
));
776 return GetBackgroundColour();
779 // Windows only: attempts to apply the UX theme page background to this page
780 void wxNotebook::ApplyThemeBackground(wxWindow
* window
, const wxColour
& colour
)
783 // Don't set the background for buttons since this will
784 // switch it into ownerdraw mode
785 if (window
->IsKindOf(CLASSINFO(wxButton
)) && !window
->IsKindOf(CLASSINFO(wxBitmapButton
)))
786 // This is essential, otherwise you'll see dark grey
787 // corners in the buttons.
788 ((wxButton
*)window
)->wxControl::SetBackgroundColour(colour
);
789 else if (window
->IsKindOf(CLASSINFO(wxStaticText
)) ||
790 window
->IsKindOf(CLASSINFO(wxStaticBox
)) ||
791 window
->IsKindOf(CLASSINFO(wxStaticLine
)) ||
792 window
->IsKindOf(CLASSINFO(wxRadioButton
)) ||
793 window
->IsKindOf(CLASSINFO(wxRadioBox
)) ||
794 window
->IsKindOf(CLASSINFO(wxCheckBox
)) ||
795 window
->IsKindOf(CLASSINFO(wxBitmapButton
)) ||
796 window
->IsKindOf(CLASSINFO(wxSlider
)) ||
797 window
->IsKindOf(CLASSINFO(wxPanel
)) ||
798 (window
->IsKindOf(CLASSINFO(wxNotebook
)) && (window
!= this)) ||
799 window
->IsKindOf(CLASSINFO(wxScrolledWindow
))
802 window
->SetBackgroundColour(colour
);
805 for ( wxWindowList::Node
*node
= window
->GetChildren().GetFirst(); node
; node
= node
->GetNext() )
807 wxWindow
*child
= node
->GetData();
808 ApplyThemeBackground(child
, colour
);
813 #endif // wxUSE_NOTEBOOK