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 ///////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "notebook.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/string.h"
32 #include "wx/imaglist.h"
34 #include "wx/control.h"
35 #include "wx/notebook.h"
37 #include "wx/sysopt.h"
39 #include "wx/msw/private.h"
43 #ifdef __GNUWIN32_OLD__
44 #include "wx/msw/gnuwin32/extra.h"
47 #if !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
51 #include "wx/msw/winundef.h"
54 #include "wx/msw/uxtheme.h"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // check that the page index is valid
62 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
65 #define m_hwnd (HWND)GetHWND()
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // This is a work-around for missing defines in gcc-2.95 headers
73 #define TCS_RIGHT 0x0002
77 #define TCS_VERTICAL 0x0080
81 #define TCS_BOTTOM TCS_RIGHT
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 #include <wx/listimpl.cpp>
90 WX_DEFINE_LIST( wxNotebookPageInfoList
) ;
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
95 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
96 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
97 EVT_SIZE(wxNotebook::OnSize
)
98 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
101 #if wxUSE_EXTENDED_RTTI
102 WX_DEFINE_FLAGS( wxNotebookStyle
)
104 wxBEGIN_FLAGS( wxNotebookStyle
)
105 // new style border flags, we put them first to
106 // use them for streaming out
107 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
108 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
109 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
110 wxFLAGS_MEMBER(wxBORDER_RAISED
)
111 wxFLAGS_MEMBER(wxBORDER_STATIC
)
112 wxFLAGS_MEMBER(wxBORDER_NONE
)
114 // old style border flags
115 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
116 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
117 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
118 wxFLAGS_MEMBER(wxRAISED_BORDER
)
119 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
120 wxFLAGS_MEMBER(wxBORDER
)
122 // standard window styles
123 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
124 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
125 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
126 wxFLAGS_MEMBER(wxWANTS_CHARS
)
127 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
128 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
129 wxFLAGS_MEMBER(wxVSCROLL
)
130 wxFLAGS_MEMBER(wxHSCROLL
)
132 wxFLAGS_MEMBER(wxNB_FIXEDWIDTH
)
133 wxFLAGS_MEMBER(wxNB_LEFT
)
134 wxFLAGS_MEMBER(wxNB_RIGHT
)
135 wxFLAGS_MEMBER(wxNB_BOTTOM
)
136 wxFLAGS_MEMBER(wxNB_NOPAGETHEME
)
137 wxFLAGS_MEMBER(wxNB_FLAT
)
139 wxEND_FLAGS( wxNotebookStyle
)
141 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook
, wxControl
,"wx/notebook.h")
142 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo
, wxObject
, "wx/notebook.h" )
144 wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo
* , wxNotebookPageInfoList
) ;
146 template<> void wxCollectionToVariantArray( wxNotebookPageInfoList
const &theList
, wxxVariantArray
&value
)
148 wxListCollectionToVariantArray
<wxNotebookPageInfoList::compatibility_iterator
>( theList
, value
) ;
151 wxBEGIN_PROPERTIES_TABLE(wxNotebook
)
152 wxEVENT_PROPERTY( PageChanging
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, wxNotebookEvent
)
153 wxEVENT_PROPERTY( PageChanged
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, wxNotebookEvent
)
155 wxPROPERTY_COLLECTION( PageInfos
, wxNotebookPageInfoList
, wxNotebookPageInfo
* , AddPageInfo
, GetPageInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
156 wxPROPERTY_FLAGS( WindowStyle
, wxNotebookStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
157 wxEND_PROPERTIES_TABLE()
159 wxBEGIN_HANDLERS_TABLE(wxNotebook
)
160 wxEND_HANDLERS_TABLE()
162 wxCONSTRUCTOR_5( wxNotebook
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
165 wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo
)
166 wxREADONLY_PROPERTY( Page
, wxNotebookPage
* , GetPage
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
167 wxREADONLY_PROPERTY( Text
, wxString
, GetText
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
168 wxREADONLY_PROPERTY( Selected
, bool , GetSelected
, false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
169 wxREADONLY_PROPERTY( ImageId
, int , GetImageId
, -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
170 wxEND_PROPERTIES_TABLE()
172 wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo
)
173 wxEND_HANDLERS_TABLE()
175 wxCONSTRUCTOR_4( wxNotebookPageInfo
, wxNotebookPage
* , Page
, wxString
, Text
, bool , Selected
, int , ImageId
)
178 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
179 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo
, wxObject
)
181 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
188 // apparently DrawThemeBackground() modifies the rect passed to it and if we
189 // don't call this function there are some drawing artifacts which are only
190 // visible with some non default themes; so modify the rect here so that it
191 // still paints the correct area
192 static void AdjustRectForThemeBg(RECT
& rc
)
194 // magic numbers needed to compensate for DrawThemeBackground()
202 // ============================================================================
204 // ============================================================================
206 // ----------------------------------------------------------------------------
207 // wxNotebook construction
208 // ----------------------------------------------------------------------------
210 const wxNotebookPageInfoList
& wxNotebook::GetPageInfos() const
212 wxNotebookPageInfoList
* list
= const_cast< wxNotebookPageInfoList
* >( &m_pageInfos
) ;
213 WX_CLEAR_LIST( wxNotebookPageInfoList
, *list
) ;
214 for( size_t i
= 0 ; i
< GetPageCount() ; ++i
)
216 wxNotebookPageInfo
*info
= new wxNotebookPageInfo() ;
217 info
->Create( const_cast<wxNotebook
*>(this)->GetPage(i
) , GetPageText(i
) , GetSelection() == int(i
) , GetPageImage(i
) ) ;
218 list
->Append( info
) ;
223 // common part of all ctors
224 void wxNotebook::Init()
230 m_hbrBackground
= NULL
;
231 #endif // wxUSE_UXTHEME
234 // default for dynamic class
235 wxNotebook::wxNotebook()
240 // the same arguments as for wxControl
241 wxNotebook::wxNotebook(wxWindow
*parent
,
246 const wxString
& name
)
250 Create(parent
, id
, pos
, size
, style
, name
);
254 bool wxNotebook::Create(wxWindow
*parent
,
259 const wxString
& name
)
261 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
262 // control is simply not rendered correctly), so disable them in this case
263 const int verComCtl32
= wxApp::GetComCtl32Version();
264 if ( verComCtl32
== 600 )
266 // check if we use themes at all -- if we don't, we're still ok
268 if ( wxUxThemeEngine::GetIfActive() )
271 style
&= ~(wxNB_BOTTOM
| wxNB_LEFT
| wxNB_RIGHT
);
275 LPCTSTR className
= WC_TABCONTROL
;
277 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
278 // causes horrible flicker when resizing notebook, so get rid of it by
279 // using a class without these styles (but otherwise identical to it)
280 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
282 static ClassRegistrar s_clsNotebook
;
283 if ( !s_clsNotebook
.IsInitialized() )
285 // get a copy of standard class and modify it
288 if ( ::GetClassInfo(::GetModuleHandle(NULL
), WC_TABCONTROL
, &wc
) )
290 wc
.lpszClassName
= wxT("_wx_SysTabCtl32");
291 wc
.style
&= ~(CS_HREDRAW
| CS_VREDRAW
);
293 s_clsNotebook
.Register(wc
);
297 wxLogLastError(_T("GetClassInfoEx(SysTabCtl32)"));
301 // use our custom class if available but fall back to the standard
302 // notebook if we failed to register it
303 if ( s_clsNotebook
.IsRegistered() )
305 // it's ok to use c_str() here as the static s_clsNotebook object
306 // has sufficiently long lifetime
307 className
= s_clsNotebook
.GetName().c_str();
311 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
312 wxDefaultValidator
, name
) )
315 if ( !MSWCreateControl(className
, wxEmptyString
, pos
, size
) )
319 if ( HasFlag(wxNB_NOPAGETHEME
) ||
320 wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) )
322 SetBackgroundColour(GetThemeBackgroundColour());
324 #endif // wxUSE_UXTHEME
326 // Undocumented hack to get flat notebook style
327 // In fact, we should probably only do this in some
328 // curcumstances, i.e. if we know we will have a border
329 // at the bottom (the tab control doesn't draw it itself)
330 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
331 if (HasFlag(wxNB_FLAT
))
333 SendMessage(m_hwnd
, CCM_SETVERSION
, COMCTL32_VERSION
, 0);
335 SetBackgroundColour(*wxWHITE
);
341 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
343 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
345 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
347 if ( style
& wxNB_MULTILINE
)
348 tabStyle
|= TCS_MULTILINE
;
349 if ( style
& wxNB_FIXEDWIDTH
)
350 tabStyle
|= TCS_FIXEDWIDTH
;
352 if ( style
& wxNB_BOTTOM
)
353 tabStyle
|= TCS_RIGHT
;
354 else if ( style
& wxNB_LEFT
)
355 tabStyle
|= TCS_VERTICAL
;
356 else if ( style
& wxNB_RIGHT
)
357 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
362 // note that we never want to have the default WS_EX_CLIENTEDGE style
363 // as it looks too ugly for the notebooks
370 wxNotebook::~wxNotebook()
373 if ( m_hbrBackground
)
374 ::DeleteObject((HBRUSH
)m_hbrBackground
);
375 #endif // wxUSE_UXTHEME
378 // ----------------------------------------------------------------------------
379 // wxNotebook accessors
380 // ----------------------------------------------------------------------------
382 size_t wxNotebook::GetPageCount() const
385 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
387 return m_pages
.Count();
390 int wxNotebook::GetRowCount() const
392 return TabCtrl_GetRowCount(m_hwnd
);
395 int wxNotebook::SetSelection(size_t nPage
)
397 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
399 if ( int(nPage
) != m_nSelection
)
401 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
402 event
.SetSelection(nPage
);
403 event
.SetOldSelection(m_nSelection
);
404 event
.SetEventObject(this);
405 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
407 // program allows the page change
408 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
409 (void)GetEventHandler()->ProcessEvent(event
);
411 TabCtrl_SetCurSel(m_hwnd
, nPage
);
418 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
420 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
423 tcItem
.mask
= TCIF_TEXT
;
424 tcItem
.pszText
= (wxChar
*)strText
.c_str();
426 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
429 wxString
wxNotebook::GetPageText(size_t nPage
) const
431 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
435 tcItem
.mask
= TCIF_TEXT
;
436 tcItem
.pszText
= buf
;
437 tcItem
.cchTextMax
= WXSIZEOF(buf
);
440 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
441 str
= tcItem
.pszText
;
446 int wxNotebook::GetPageImage(size_t nPage
) const
448 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
451 tcItem
.mask
= TCIF_IMAGE
;
453 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
456 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
458 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
461 tcItem
.mask
= TCIF_IMAGE
;
462 tcItem
.iImage
= nImage
;
464 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
467 void wxNotebook::SetImageList(wxImageList
* imageList
)
469 wxNotebookBase::SetImageList(imageList
);
473 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
477 // ----------------------------------------------------------------------------
478 // wxNotebook size settings
479 // ----------------------------------------------------------------------------
481 void wxNotebook::SetPageSize(const wxSize
& size
)
483 // transform the page size into the notebook size
490 TabCtrl_AdjustRect(GetHwnd(), true, &rc
);
493 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
496 void wxNotebook::SetPadding(const wxSize
& padding
)
498 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
501 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
503 void wxNotebook::SetTabSize(const wxSize
& sz
)
505 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
508 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
510 wxSize sizeTotal
= sizePage
;
512 // We need to make getting tab size part of the wxWidgets API.
514 if (GetPageCount() > 0)
517 TabCtrl_GetItemRect((HWND
) GetHWND(), 0, & rect
);
518 tabSize
.x
= rect
.right
- rect
.left
;
519 tabSize
.y
= rect
.bottom
- rect
.top
;
521 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
523 sizeTotal
.x
+= tabSize
.x
+ 7;
529 sizeTotal
.y
+= tabSize
.y
+ 7;
535 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
537 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
543 // get the page size from the notebook size
544 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
546 // This check is to work around a bug in TabCtrl_AdjustRect which will
547 // cause a crash on win2k, or on XP with themes disabled, if the
548 // wxNB_MULTILINE style is used and the rectangle is very small, (such as
549 // when the notebook is first created.) The value of 20 is just
550 // arbitrarily chosen, if there is a better way to determine this value
551 // then please do so. --RD
552 if (rc
.right
> 20 && rc
.bottom
> 20)
554 TabCtrl_AdjustRect(m_hwnd
, false, &rc
);
555 page
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
559 // ----------------------------------------------------------------------------
560 // wxNotebook operations
561 // ----------------------------------------------------------------------------
563 // remove one page from the notebook, without deleting
564 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
566 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
570 TabCtrl_DeleteItem(m_hwnd
, nPage
);
572 if ( m_pages
.IsEmpty() )
574 // no selection any more, the notebook becamse empty
577 else // notebook still not empty
579 int selNew
= TabCtrl_GetCurSel(m_hwnd
);
582 // No selection change, just refresh the current selection.
583 // Because it could be that the slection index changed
584 // we need to update it.
585 // Note: this does not mean the selection it self changed.
586 m_nSelection
= selNew
;
587 m_pages
[m_nSelection
]->Refresh();
589 else if (int(nPage
) == m_nSelection
)
591 // The selection was deleted.
593 // Determine new selection.
594 if (m_nSelection
== int(GetPageCount()))
595 selNew
= m_nSelection
- 1;
597 selNew
= m_nSelection
;
599 // m_nSelection must be always valid so reset it before calling
602 SetSelection(selNew
);
606 wxFAIL
; // Windows did not behave ok.
614 bool wxNotebook::DeleteAllPages()
616 size_t nPageCount
= GetPageCount();
618 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
619 delete m_pages
[nPage
];
623 TabCtrl_DeleteAllItems(m_hwnd
);
627 InvalidateBestSize();
631 // same as AddPage() but does it at given position
632 bool wxNotebook::InsertPage(size_t nPage
,
633 wxNotebookPage
*pPage
,
634 const wxString
& strText
,
638 wxCHECK_MSG( pPage
!= NULL
, false, _T("NULL page in wxNotebook::InsertPage") );
639 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false,
640 _T("invalid index in wxNotebook::InsertPage") );
642 wxASSERT_MSG( pPage
->GetParent() == this,
643 _T("notebook pages must have notebook as parent") );
645 // add a new tab to the control
646 // ----------------------------
648 // init all fields to 0
650 wxZeroMemory(tcItem
);
652 // set the image, if any
655 tcItem
.mask
|= TCIF_IMAGE
;
656 tcItem
.iImage
= imageId
;
660 if ( !strText
.empty() )
662 tcItem
.mask
|= TCIF_TEXT
;
663 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
666 // hide the page: unless it is selected, it shouldn't be shown (and if it
667 // is selected it will be shown later)
668 HWND hwnd
= GetWinHwnd(pPage
);
669 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
671 // this updates internal flag too -- otherwise it would get out of sync
672 // with the real state
676 // fit the notebook page to the tab control's display area: this should be
677 // done before adding it to the notebook or TabCtrl_InsertItem() will
678 // change the notebooks size itself!
679 AdjustPageSize(pPage
);
681 // finally do insert it
682 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 )
684 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
689 // succeeded: save the pointer to the page
690 m_pages
.Insert(pPage
, nPage
);
692 // we may need to adjust the size again if the notebook size changed:
693 // normally this only happens for the first page we add (the tabs which
694 // hadn't been there before are now shown) but for a multiline notebook it
695 // can happen for any page at all as a new row could have been started
696 if ( m_pages
.GetCount() == 1 || HasFlag(wxNB_MULTILINE
) )
698 AdjustPageSize(pPage
);
701 // now deal with the selection
702 // ---------------------------
704 // if the inserted page is before the selected one, we must update the
705 // index of the selected page
706 if ( int(nPage
) <= m_nSelection
)
708 // one extra page added
712 // some page should be selected: either this one or the first one if there
713 // is still no selection
717 else if ( m_nSelection
== -1 )
721 SetSelection(selNew
);
723 InvalidateBestSize();
728 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
730 TC_HITTESTINFO hitTestInfo
;
731 hitTestInfo
.pt
.x
= pt
.x
;
732 hitTestInfo
.pt
.y
= pt
.y
;
733 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
739 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
740 *flags
|= wxNB_HITTEST_NOWHERE
;
741 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
742 *flags
|= wxNB_HITTEST_ONITEM
;
743 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
744 *flags
|= wxNB_HITTEST_ONICON
;
745 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
746 *flags
|= wxNB_HITTEST_ONLABEL
;
753 // ----------------------------------------------------------------------------
754 // wxNotebook callbacks
755 // ----------------------------------------------------------------------------
757 void wxNotebook::OnSize(wxSizeEvent
& event
)
760 // background bitmap size has changed, update the brush using it too
762 #endif // wxUSE_UXTHEME
764 if ( GetPageCount() == 0 )
766 // Prevents droppings on resize, but does cause some flicker
767 // when there are no pages.
773 // fit all the notebook pages to the tab control's display area
776 rc
.left
= rc
.top
= 0;
777 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
779 // save the total size, we'll use it below
780 int widthNbook
= rc
.right
- rc
.left
,
781 heightNbook
= rc
.bottom
- rc
.top
;
783 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
784 // returns completely false values for multiline tab controls after the tabs
785 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
787 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
789 // and the only work around I could find was this ugly hack... without it
790 // simply toggling the "multiline" checkbox in the notebook sample resulted
791 // in a noticeable page displacement
792 if ( HasFlag(wxNB_MULTILINE
) )
794 // avoid an infinite recursion: we get another notification too!
795 static bool s_isInOnSize
= false;
800 SendMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
,
801 MAKELPARAM(rc
.right
, rc
.bottom
));
802 s_isInOnSize
= false;
806 TabCtrl_AdjustRect(m_hwnd
, false, &rc
);
808 int width
= rc
.right
- rc
.left
,
809 height
= rc
.bottom
- rc
.top
;
810 size_t nCount
= m_pages
.Count();
811 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
812 wxNotebookPage
*pPage
= m_pages
[nPage
];
813 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
817 // unless we had already repainted everything, we now need to refresh
818 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
820 // invalidate areas not covered by pages
821 RefreshRect(wxRect(0, 0, widthNbook
, rc
.top
), false);
822 RefreshRect(wxRect(0, rc
.top
, rc
.left
, height
), false);
823 RefreshRect(wxRect(0, rc
.bottom
, widthNbook
, heightNbook
- rc
.bottom
),
825 RefreshRect(wxRect(rc
.right
, rc
.top
, widthNbook
- rc
.bottom
, height
),
832 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
834 // is it our tab control?
835 if ( event
.GetEventObject() == this )
837 int sel
= event
.GetOldSelection();
839 m_pages
[sel
]->Show(false);
841 sel
= event
.GetSelection();
844 wxNotebookPage
*pPage
= m_pages
[sel
];
848 // If the newly focused window is not a child of the new page,
849 // SetFocus was not successful and the notebook itself should be
851 wxWindow
*currentFocus
= FindFocus();
852 wxWindow
*startFocus
= currentFocus
;
853 while ( currentFocus
&& currentFocus
!= pPage
&& currentFocus
!= this )
854 currentFocus
= currentFocus
->GetParent();
856 if ( startFocus
== pPage
|| currentFocus
!= pPage
)
860 else // no pages in the notebook, give the focus to itself
868 // we want to give others a chance to process this message as well
872 bool wxNotebook::MSWTranslateMessage(WXMSG
*wxmsg
)
874 const MSG
* const msg
= (MSG
*)wxmsg
;
876 // intercept TAB, CTRL+TAB and CTRL+SHIFT+TAB for processing by wxNotebook.
877 // TAB will be passed to the currently selected page, CTRL+TAB and
878 // CTRL+SHIFT+TAB will be processed by the notebook itself. do not
879 // intercept SHIFT+TAB. This goes to the parent of the notebook which will
881 if ( msg
->message
== WM_KEYDOWN
&& msg
->wParam
== VK_TAB
&&
882 msg
->hwnd
== m_hwnd
&&
883 (wxIsCtrlDown() || !wxIsShiftDown()) )
885 return MSWProcessMessage(wxmsg
);
891 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
893 if ( event
.IsWindowChange() ) {
895 AdvanceSelection(event
.GetDirection());
898 // we get this event in 3 cases
900 // a) one of our pages might have generated it because the user TABbed
901 // out from it in which case we should propagate the event upwards and
902 // our parent will take care of setting the focus to prev/next sibling
906 // b) the parent panel wants to give the focus to us so that we
907 // forward it to our selected page. We can't deal with this in
908 // OnSetFocus() because we don't know which direction the focus came
909 // from in this case and so can't choose between setting the focus to
910 // first or last panel child
914 // c) we ourselves (see MSWTranslateMessage) generated the event
916 wxWindow
* const parent
= GetParent();
918 const bool isFromParent
= event
.GetEventObject() == parent
;
919 const bool isFromSelf
= event
.GetEventObject() == this;
921 if ( isFromParent
|| isFromSelf
)
923 // no, it doesn't come from child, case (b) or (c): forward to a
924 // page but only if direction is backwards (TAB) or from ourselves,
925 if ( m_nSelection
!= -1 &&
926 (!event
.GetDirection() || isFromSelf
) )
928 // so that the page knows that the event comes from it's parent
929 // and is being propagated downwards
930 event
.SetEventObject(this);
932 wxWindow
*page
= m_pages
[m_nSelection
];
933 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
937 //else: page manages focus inside it itself
939 else // otherwise set the focus to the notebook itself
946 // it comes from our child, case (a), pass to the parent, but only
947 // if the direction is forwards. Otherwise set the focus to the
948 // notebook itself. The notebook is always the 'first' control of a
950 if ( !event
.GetDirection() )
956 event
.SetCurrentFocus(this);
957 parent
->GetEventHandler()->ProcessEvent(event
);
965 WXHBRUSH
wxNotebook::QueryBgBitmap()
968 ::GetClientRect(GetHwnd(), &rc
);
971 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
973 WindowHDC
hDC(GetHwnd());
974 MemoryHDC
hDCMem(hDC
);
975 CompatibleBitmap
hBmp(hDC
, rc
.right
, rc
.bottom
);
977 SelectInHDC
selectBmp(hDCMem
, hBmp
);
979 wxUxThemeHandle
theme(this, L
"TAB");
982 AdjustRectForThemeBg(rc
);
984 wxUxThemeEngine::Get()->DrawThemeBackground
995 return (WXHBRUSH
)::CreatePatternBrush(hBmp
);
998 void wxNotebook::UpdateBgBrush()
1000 if ( m_hbrBackground
)
1001 ::DeleteObject((HBRUSH
)m_hbrBackground
);
1003 if ( !m_hasBgCol
&& wxUxThemeEngine::GetIfActive() )
1005 m_hbrBackground
= QueryBgBitmap();
1009 m_hbrBackground
= NULL
;
1013 WXHBRUSH
wxNotebook::MSWGetBgBrushForChild(WXHDC hDC
, wxWindow
*win
)
1015 if ( m_hbrBackground
)
1017 // before drawing with the background brush, we need to position it
1020 ::GetWindowRect(GetHwndOf(win
), &rc
);
1022 ::MapWindowPoints(NULL
, GetHwnd(), (POINT
*)&rc
, 1);
1024 if ( !::SetBrushOrgEx((HDC
)hDC
, -rc
.left
, -rc
.top
, NULL
) )
1026 wxLogLastError(_T("SetBrushOrgEx(notebook bg brush)"));
1029 return m_hbrBackground
;
1032 return wxNotebookBase::MSWGetBgBrushForChild(hDC
, win
);
1035 wxColour
wxNotebook::MSWGetBgColourForChild(wxWindow
*WXUNUSED(win
))
1038 return GetBackgroundColour();
1040 // Experimental: don't do this since we're doing it in wxPanel
1041 #if 0 // defined(__POCKETPC__) || defined(__SMARTPHONE__)
1042 // For some reason, the pages will be grey by default.
1043 // Normally they should be white on these platforms.
1044 // (However the static control backgrounds are painted
1045 // in the correct colour, just not the rest of it.)
1046 // So let's give WinCE a hint.
1047 else if (!win
->m_hasBgCol
)
1051 if ( !wxUxThemeEngine::GetIfActive() )
1052 return wxNullColour
;
1054 return GetThemeBackgroundColour();
1058 wxNotebook::MSWPrintChild(wxWindow
*win
,
1060 WXLPARAM
WXUNUSED(lParam
))
1062 // Don't paint the theme for the child if we have a solid background
1068 ::GetClientRect(GetHwnd(), &rc
);
1071 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
1073 wxUxThemeHandle
theme(win
, L
"TAB");
1076 // map from this client to win client coords
1077 ::MapWindowPoints(GetHwnd(), GetHwndOf(win
), (POINT
*)&rc
, 2);
1079 AdjustRectForThemeBg(rc
);
1080 wxUxThemeEngine::Get()->DrawThemeBackground
1094 #endif // wxUSE_UXTHEME
1096 // Windows only: attempts to get colour for UX theme page background
1097 wxColour
wxNotebook::GetThemeBackgroundColour() const
1100 if (wxUxThemeEngine::Get())
1102 wxUxThemeHandle
hTheme((wxNotebook
*) this, L
"TAB");
1105 // This is total guesswork.
1106 // See PlatformSDK\Include\Tmschema.h for values
1107 COLORREF themeColor
;
1108 wxUxThemeEngine::Get()->GetThemeColor(
1112 3821 /* FILLCOLORHINT */,
1116 [DS] Workaround for WindowBlinds:
1117 Some themes return a near black theme color using FILLCOLORHINT,
1118 this makes notebook pages have an ugly black background and makes
1119 text (usually black) unreadable. Retry again with FILLCOLOR.
1121 This workaround potentially breaks appearance of some themes,
1122 but in practice it already fixes some themes.
1124 if (themeColor
== 1)
1126 wxUxThemeEngine::Get()->GetThemeColor(
1130 3802 /* FILLCOLOR */,
1134 return wxRGBToColour(themeColor
);
1137 #endif // wxUSE_UXTHEME
1139 return GetBackgroundColour();
1142 // ----------------------------------------------------------------------------
1143 // wxNotebook base class virtuals
1144 // ----------------------------------------------------------------------------
1146 #if wxUSE_CONSTRAINTS
1148 // override these 2 functions to do nothing: everything is done in OnSize
1150 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
1152 // don't set the sizes of the pages - their correct size is not yet known
1153 wxControl::SetConstraintSizes(false);
1156 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
1161 #endif // wxUSE_CONSTRAINTS
1163 // ----------------------------------------------------------------------------
1164 // wxNotebook Windows message handlers
1165 // ----------------------------------------------------------------------------
1167 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
1168 WXWORD pos
, WXHWND control
)
1170 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1175 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
1178 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
1180 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
1182 NMHDR
* hdr
= (NMHDR
*)lParam
;
1183 switch ( hdr
->code
) {
1185 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
1188 case TCN_SELCHANGING
:
1189 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
1193 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1196 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
1197 event
.SetOldSelection(m_nSelection
);
1198 event
.SetEventObject(this);
1199 event
.SetInt(idCtrl
);
1201 bool processed
= GetEventHandler()->ProcessEvent(event
);
1202 *result
= !event
.IsAllowed();
1206 #endif // wxUSE_NOTEBOOK