1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/notebook.h"
24 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
25 #include "wx/string.h"
30 #include "wx/dcclient.h"
31 #include "wx/dcmemory.h"
32 #include "wx/control.h"
36 #include "wx/imaglist.h"
37 #include "wx/sysopt.h"
39 #include "wx/msw/private.h"
40 #include "wx/msw/dc.h"
43 #include "wx/msw/winundef.h"
46 #include "wx/msw/uxtheme.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // check that the page index is valid
54 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
56 // you can set USE_NOTEBOOK_ANTIFLICKER to 0 for desktop Windows versions too
57 // to disable code whih results in flicker-less notebook redrawing at the
58 // expense of some extra GDI resource consumption
60 // notebooks are never resized under CE anyhow
61 #define USE_NOTEBOOK_ANTIFLICKER 0
63 #define USE_NOTEBOOK_ANTIFLICKER 1
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // This is a work-around for missing defines in gcc-2.95 headers
72 #define TCS_RIGHT 0x0002
76 #define TCS_VERTICAL 0x0080
80 #define TCS_BOTTOM TCS_RIGHT
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 #if USE_NOTEBOOK_ANTIFLICKER
89 // the pointer to standard spin button wnd proc
90 static WXFARPROC gs_wndprocNotebookSpinBtn
= (WXFARPROC
)NULL
;
92 // the pointer to standard tab control wnd proc
93 static WXFARPROC gs_wndprocNotebook
= (WXFARPROC
)NULL
;
95 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
100 #endif // USE_NOTEBOOK_ANTIFLICKER
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 static bool HasTroubleWithNonTopTabs()
108 const int verComCtl32
= wxApp::GetComCtl32Version();
110 // 600 is XP, 616 is Vista -- and both have a problem with tabs not on top
111 // (but don't just test for >= 600 as Microsoft might decide to fix it in
112 // later versions, who knows...)
113 return verComCtl32
>= 600 && verComCtl32
<= 616;
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
121 EVT_SIZE(wxNotebook::OnSize
)
122 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
124 #if USE_NOTEBOOK_ANTIFLICKER
125 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
)
126 EVT_PAINT(wxNotebook::OnPaint
)
127 #endif // USE_NOTEBOOK_ANTIFLICKER
130 // ============================================================================
132 // ============================================================================
134 // ----------------------------------------------------------------------------
135 // wxNotebook construction
136 // ----------------------------------------------------------------------------
138 // common part of all ctors
139 void wxNotebook::Init()
142 m_hbrBackground
= NULL
;
143 #endif // wxUSE_UXTHEME
145 #if USE_NOTEBOOK_ANTIFLICKER
146 m_hasSubclassedUpdown
= false;
147 m_doneUpdateHack
= false;
148 #endif // USE_NOTEBOOK_ANTIFLICKER
151 // default for dynamic class
152 wxNotebook::wxNotebook()
157 // the same arguments as for wxControl
158 wxNotebook::wxNotebook(wxWindow
*parent
,
163 const wxString
& name
)
167 Create(parent
, id
, pos
, size
, style
, name
);
171 bool wxNotebook::Create(wxWindow
*parent
,
176 const wxString
& name
)
178 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
180 #if defined(__POCKETPC__)
181 style
|= wxBK_BOTTOM
| wxNB_FLAT
;
188 // Not sure why, but without this style, there is no border
189 // around the notebook tabs.
190 if (style
& wxNB_FLAT
)
191 style
|= wxBORDER_SUNKEN
;
195 // ComCtl32 notebook tabs simply don't work unless they're on top if we
196 // have uxtheme, we can work around it later (after control creation), but
197 // if we have been compiled without uxtheme support, we have to clear those
199 if ( HasTroubleWithNonTopTabs() )
201 style
&= ~(wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
);
203 #endif //wxUSE_UXTHEME
205 #if defined(__WINE__) && wxUSE_UNICODE
206 LPCTSTR className
= L
"SysTabControl32";
208 LPCTSTR className
= WC_TABCONTROL
;
211 #if USE_NOTEBOOK_ANTIFLICKER
212 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
213 // causes horrible flicker when resizing notebook, so get rid of it by
214 // using a class without these styles (but otherwise identical to it)
215 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
217 static ClassRegistrar s_clsNotebook
;
218 if ( !s_clsNotebook
.IsInitialized() )
220 // get a copy of standard class and modify it
223 if ( ::GetClassInfo(NULL
, WC_TABCONTROL
, &wc
) )
226 reinterpret_cast<WXFARPROC
>(wc
.lpfnWndProc
);
227 wc
.lpszClassName
= wxT("_wx_SysTabCtl32");
228 wc
.style
&= ~(CS_HREDRAW
| CS_VREDRAW
);
229 wc
.hInstance
= wxGetInstance();
230 wc
.lpfnWndProc
= wxNotebookWndProc
;
231 s_clsNotebook
.Register(wc
);
235 wxLogLastError(wxT("GetClassInfoEx(SysTabCtl32)"));
239 // use our custom class if available but fall back to the standard
240 // notebook if we failed to register it
241 if ( s_clsNotebook
.IsRegistered() )
243 // it's ok to use c_str() here as the static s_clsNotebook object
244 // has sufficiently long lifetime
245 className
= s_clsNotebook
.GetName().c_str();
248 #endif // USE_NOTEBOOK_ANTIFLICKER
250 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
251 wxDefaultValidator
, name
) )
254 if ( !MSWCreateControl(className
, wxEmptyString
, pos
, size
) )
258 if ( HasFlag(wxNB_NOPAGETHEME
) ||
259 wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) )
261 SetBackgroundColour(GetThemeBackgroundColour());
263 else // use themed background by default
265 // create backing store
269 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
270 // control is simply not rendered correctly), so we disable themes
271 // if possible, otherwise we simply clear the styles.
272 if ( HasTroubleWithNonTopTabs() &&
273 (style
& (wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
)) )
275 // check if we use themes at all -- if we don't, we're still okay
276 if ( wxUxThemeEngine::GetIfActive() )
278 wxUxThemeEngine::GetIfActive()->SetWindowTheme(GetHwnd(), L
"", L
"");
280 // correct the background color for the new non-themed control
281 SetBackgroundColour(GetThemeBackgroundColour());
284 #endif // wxUSE_UXTHEME
286 // Undocumented hack to get flat notebook style
287 // In fact, we should probably only do this in some
288 // curcumstances, i.e. if we know we will have a border
289 // at the bottom (the tab control doesn't draw it itself)
290 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
291 if (HasFlag(wxNB_FLAT
))
293 SendMessage(GetHwnd(), CCM_SETVERSION
, COMCTL32_VERSION
, 0);
295 SetBackgroundColour(*wxWHITE
);
301 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
303 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
305 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
307 if ( style
& wxNB_MULTILINE
)
308 tabStyle
|= TCS_MULTILINE
;
309 if ( style
& wxNB_FIXEDWIDTH
)
310 tabStyle
|= TCS_FIXEDWIDTH
;
312 if ( style
& wxBK_BOTTOM
)
313 tabStyle
|= TCS_RIGHT
;
314 else if ( style
& wxBK_LEFT
)
315 tabStyle
|= TCS_VERTICAL
;
316 else if ( style
& wxBK_RIGHT
)
317 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
322 wxNotebook::~wxNotebook()
325 if ( m_hbrBackground
)
326 ::DeleteObject((HBRUSH
)m_hbrBackground
);
327 #endif // wxUSE_UXTHEME
330 // ----------------------------------------------------------------------------
331 // wxNotebook accessors
332 // ----------------------------------------------------------------------------
334 size_t wxNotebook::GetPageCount() const
337 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(GetHwnd()) );
339 return m_pages
.Count();
342 int wxNotebook::GetRowCount() const
344 return TabCtrl_GetRowCount(GetHwnd());
347 int wxNotebook::SetSelection(size_t nPage
)
349 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
351 if ( m_selection
== wxNOT_FOUND
|| nPage
!= (size_t)m_selection
)
353 if ( SendPageChangingEvent(nPage
) )
355 // program allows the page change
356 const int selectionOld
= m_selection
;
358 UpdateSelection(nPage
);
360 TabCtrl_SetCurSel(GetHwnd(), nPage
);
362 SendPageChangedEvent(selectionOld
, nPage
);
369 void wxNotebook::UpdateSelection(int selNew
)
371 if ( m_selection
!= wxNOT_FOUND
)
372 m_pages
[m_selection
]->Show(false);
374 if ( selNew
!= wxNOT_FOUND
)
376 wxNotebookPage
*pPage
= m_pages
[selNew
];
379 // In addition to showing the page, we also want to give focus to it to
380 // make it possible to work with it from keyboard easily. However there
381 // are two exceptions: first, don't touch the focus at all if the
382 // notebook itself is not currently shown.
383 if ( ::IsWindowVisible(GetHwnd()) )
385 // And second, don't give focus away if the tab control itself has
386 // it, as this is how the native property sheets behave: if you
387 // explicitly click on the tab label giving it focus, it will
388 // remain after switching to another page. But if the focus was
389 // inside the notebook page, it switches to the new page.
395 m_selection
= selNew
;
398 int wxNotebook::ChangeSelection(size_t nPage
)
400 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
402 const int selOld
= m_selection
;
404 if ( m_selection
== wxNOT_FOUND
|| nPage
!= (size_t)m_selection
)
406 TabCtrl_SetCurSel(GetHwnd(), nPage
);
408 UpdateSelection(nPage
);
414 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
416 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
419 tcItem
.mask
= TCIF_TEXT
;
420 tcItem
.pszText
= wxMSW_CONV_LPTSTR(strText
);
422 if ( !HasFlag(wxNB_MULTILINE
) )
423 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
425 // multiline - we need to set new page size if a line is added or removed
426 int rows
= GetRowCount();
427 bool ret
= TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
429 if ( ret
&& rows
!= GetRowCount() )
431 const wxRect r
= GetPageSize();
432 const size_t count
= m_pages
.Count();
433 for ( size_t page
= 0; page
< count
; page
++ )
434 m_pages
[page
]->SetSize(r
);
440 wxString
wxNotebook::GetPageText(size_t nPage
) const
442 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
446 tcItem
.mask
= TCIF_TEXT
;
447 tcItem
.pszText
= buf
;
448 tcItem
.cchTextMax
= WXSIZEOF(buf
);
451 if ( TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) )
452 str
= tcItem
.pszText
;
457 int wxNotebook::GetPageImage(size_t nPage
) const
459 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
462 tcItem
.mask
= TCIF_IMAGE
;
464 return TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) ? tcItem
.iImage
468 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
470 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
473 tcItem
.mask
= TCIF_IMAGE
;
474 tcItem
.iImage
= nImage
;
476 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
479 void wxNotebook::SetImageList(wxImageList
* imageList
)
481 wxNotebookBase::SetImageList(imageList
);
485 (void) TabCtrl_SetImageList(GetHwnd(), GetHimagelistOf(imageList
));
489 // ----------------------------------------------------------------------------
490 // wxNotebook size settings
491 // ----------------------------------------------------------------------------
493 wxRect
wxNotebook::GetPageSize() const
498 ::GetClientRect(GetHwnd(), &rc
);
500 // This check is to work around a bug in TabCtrl_AdjustRect which will
501 // cause a crash on win2k or on XP with themes disabled if either
502 // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle
505 // The value of 20 is chosen arbitrarily but seems to work
506 if ( rc
.right
> 20 && rc
.bottom
> 20 )
508 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
510 wxCopyRECTToRect(rc
, r
);
516 void wxNotebook::SetPageSize(const wxSize
& size
)
518 // transform the page size into the notebook size
525 TabCtrl_AdjustRect(GetHwnd(), true, &rc
);
528 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
531 void wxNotebook::SetPadding(const wxSize
& padding
)
533 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
536 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
538 void wxNotebook::SetTabSize(const wxSize
& sz
)
540 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
543 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
545 // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP
546 wxSize sizeTotal
= sizePage
;
549 if ( GetPageCount() > 0 )
552 TabCtrl_GetItemRect(GetHwnd(), 0, &rect
);
553 tabSize
.x
= rect
.right
- rect
.left
;
554 tabSize
.y
= rect
.bottom
- rect
.top
;
557 const int rows
= GetRowCount();
559 // add an extra margin in both directions
560 const int MARGIN
= 8;
563 sizeTotal
.x
+= MARGIN
;
564 sizeTotal
.y
+= tabSize
.y
* rows
+ MARGIN
;
566 else // horizontal layout
568 sizeTotal
.x
+= tabSize
.x
* rows
+ MARGIN
;
569 sizeTotal
.y
+= MARGIN
;
575 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
577 wxCHECK_RET( page
, wxT("NULL page in wxNotebook::AdjustPageSize") );
579 const wxRect r
= GetPageSize();
586 // ----------------------------------------------------------------------------
587 // wxNotebook operations
588 // ----------------------------------------------------------------------------
590 // remove one page from the notebook, without deleting
591 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
593 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
597 // hide the removed page to maintain the invariant that only the
598 // selected page is visible and others are hidden:
599 pageRemoved
->Show(false);
601 TabCtrl_DeleteItem(GetHwnd(), nPage
);
603 if ( m_pages
.IsEmpty() )
605 // no selection any more, the notebook becamse empty
606 m_selection
= wxNOT_FOUND
;
608 else // notebook still not empty
610 int selNew
= TabCtrl_GetCurSel(GetHwnd());
611 if ( selNew
!= wxNOT_FOUND
)
613 // No selection change, just refresh the current selection.
614 // Because it could be that the slection index changed
615 // we need to update it.
616 // Note: this does not mean the selection it self changed.
617 m_selection
= selNew
;
618 m_pages
[m_selection
]->Refresh();
620 else if (int(nPage
) == m_selection
)
622 // The selection was deleted.
624 // Determine new selection.
625 if (m_selection
== int(GetPageCount()))
626 selNew
= m_selection
- 1;
628 selNew
= m_selection
;
630 // m_selection must be always valid so reset it before calling
632 m_selection
= wxNOT_FOUND
;
633 SetSelection(selNew
);
637 wxFAIL
; // Windows did not behave ok.
645 bool wxNotebook::DeleteAllPages()
647 size_t nPageCount
= GetPageCount();
649 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
650 delete m_pages
[nPage
];
654 TabCtrl_DeleteAllItems(GetHwnd());
656 m_selection
= wxNOT_FOUND
;
658 InvalidateBestSize();
662 // same as AddPage() but does it at given position
663 bool wxNotebook::InsertPage(size_t nPage
,
664 wxNotebookPage
*pPage
,
665 const wxString
& strText
,
669 wxCHECK_MSG( pPage
!= NULL
, false, wxT("NULL page in wxNotebook::InsertPage") );
670 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false,
671 wxT("invalid index in wxNotebook::InsertPage") );
673 wxASSERT_MSG( pPage
->GetParent() == this,
674 wxT("notebook pages must have notebook as parent") );
676 // add a new tab to the control
677 // ----------------------------
679 // init all fields to 0
681 wxZeroMemory(tcItem
);
683 // set the image, if any
686 tcItem
.mask
|= TCIF_IMAGE
;
687 tcItem
.iImage
= imageId
;
691 if ( !strText
.empty() )
693 tcItem
.mask
|= TCIF_TEXT
;
694 tcItem
.pszText
= wxMSW_CONV_LPTSTR(strText
);
697 // hide the page: unless it is selected, it shouldn't be shown (and if it
698 // is selected it will be shown later)
699 HWND hwnd
= GetWinHwnd(pPage
);
700 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
702 // this updates internal flag too -- otherwise it would get out of sync
703 // with the real state
707 // fit the notebook page to the tab control's display area: this should be
708 // done before adding it to the notebook or TabCtrl_InsertItem() will
709 // change the notebooks size itself!
710 AdjustPageSize(pPage
);
712 // finally do insert it
713 if ( TabCtrl_InsertItem(GetHwnd(), nPage
, &tcItem
) == -1 )
715 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
720 // need to update the bg brush when the first page is added
721 // so the first panel gets the correct themed background
722 if ( m_pages
.empty() )
726 #endif // wxUSE_UXTHEME
729 // succeeded: save the pointer to the page
730 m_pages
.Insert(pPage
, nPage
);
732 // we may need to adjust the size again if the notebook size changed:
733 // normally this only happens for the first page we add (the tabs which
734 // hadn't been there before are now shown) but for a multiline notebook it
735 // can happen for any page at all as a new row could have been started
736 if ( m_pages
.GetCount() == 1 || HasFlag(wxNB_MULTILINE
) )
738 AdjustPageSize(pPage
);
740 // Additionally, force the layout of the notebook itself by posting a
741 // size event to it. If we don't do it, notebooks with pages on the
742 // left or the right side may fail to account for the fact that they
743 // are now big enough to fit all all of their pages on one row and
744 // still reserve space for the second row of tabs, see #1792.
745 const wxSize s
= GetSize();
746 ::PostMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
, MAKELPARAM(s
.x
, s
.y
));
749 // now deal with the selection
750 // ---------------------------
752 // if the inserted page is before the selected one, we must update the
753 // index of the selected page
754 if ( int(nPage
) <= m_selection
)
756 // one extra page added
760 DoSetSelectionAfterInsertion(nPage
, bSelect
);
762 InvalidateBestSize();
767 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
769 TC_HITTESTINFO hitTestInfo
;
770 hitTestInfo
.pt
.x
= pt
.x
;
771 hitTestInfo
.pt
.y
= pt
.y
;
772 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
778 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
779 *flags
|= wxBK_HITTEST_NOWHERE
;
780 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
781 *flags
|= wxBK_HITTEST_ONITEM
;
782 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
783 *flags
|= wxBK_HITTEST_ONICON
;
784 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
785 *flags
|= wxBK_HITTEST_ONLABEL
;
786 if ( item
== wxNOT_FOUND
&& GetPageSize().Contains(pt
) )
787 *flags
|= wxBK_HITTEST_ONPAGE
;
793 // ----------------------------------------------------------------------------
794 // flicker-less notebook redraw
795 // ----------------------------------------------------------------------------
797 #if USE_NOTEBOOK_ANTIFLICKER
799 // wnd proc for the spin button
800 LRESULT APIENTRY _EXPORT
wxNotebookSpinBtnWndProc(HWND hwnd
,
805 if ( message
== WM_ERASEBKGND
)
808 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn
,
809 hwnd
, message
, wParam
, lParam
);
812 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
817 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook
,
818 hwnd
, message
, wParam
, lParam
);
821 void wxNotebook::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
826 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
))
831 ::GetClientRect(GetHwnd(), &rc
);
832 wxBitmap
bmp(rc
.right
, rc
.bottom
);
833 memdc
.SelectObject(bmp
);
835 const wxLayoutDirection dir
= dc
.GetLayoutDirection();
836 memdc
.SetLayoutDirection(dir
);
838 // if there is no special brush just use the solid background colour
840 HBRUSH hbr
= (HBRUSH
)m_hbrBackground
;
847 brush
= wxBrush(GetBackgroundColour());
848 hbr
= GetHbrushOf(brush
);
851 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) memdc
.GetImpl();
853 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
855 MSWDefWindowProc(WM_PAINT
, (WPARAM
)(impl
->GetHDC()), 0);
857 // For some reason in RTL mode, source offset has to be -1, otherwise the
858 // right border (physical) remains unpainted.
859 const wxCoord ofs
= dir
== wxLayout_RightToLeft
? -1 : 0;
860 dc
.Blit(ofs
, 0, rc
.right
, rc
.bottom
, &memdc
, ofs
, 0);
863 #endif // USE_NOTEBOOK_ANTIFLICKER
865 // ----------------------------------------------------------------------------
866 // wxNotebook callbacks
867 // ----------------------------------------------------------------------------
869 void wxNotebook::OnSize(wxSizeEvent
& event
)
871 if ( GetPageCount() == 0 )
873 // Prevents droppings on resize, but does cause some flicker
874 // when there are no pages.
882 // Without this, we can sometimes get droppings at the edges
883 // of a notebook, for example a notebook in a splitter window.
884 // This needs to be reconciled with the RefreshRect calls
885 // at the end of this function, which weren't enough to prevent
888 wxSize sz
= GetClientSize();
890 // Refresh right side
891 wxRect
rect(sz
.x
-4, 0, 4, sz
.y
);
894 // Refresh bottom side
895 rect
= wxRect(0, sz
.y
-4, sz
.x
, 4);
899 rect
= wxRect(0, 0, 4, sz
.y
);
902 #endif // !__WXWINCE__
904 // fit all the notebook pages to the tab control's display area
907 rc
.left
= rc
.top
= 0;
908 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
910 // save the total size, we'll use it below
911 int widthNbook
= rc
.right
- rc
.left
,
912 heightNbook
= rc
.bottom
- rc
.top
;
914 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
915 // returns completely false values for multiline tab controls after the tabs
916 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
918 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
920 // and the only work around I could find was this ugly hack... without it
921 // simply toggling the "multiline" checkbox in the notebook sample resulted
922 // in a noticeable page displacement
923 if ( HasFlag(wxNB_MULTILINE
) )
925 // avoid an infinite recursion: we get another notification too!
926 static bool s_isInOnSize
= false;
931 SendMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
,
932 MAKELPARAM(rc
.right
, rc
.bottom
));
933 s_isInOnSize
= false;
936 // The best size depends on the number of rows of tabs, which can
937 // change when the notepad is resized.
938 InvalidateBestSize();
942 // background bitmap size has changed, update the brush using it too
944 #endif // wxUSE_UXTHEME
946 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
948 int width
= rc
.right
- rc
.left
,
949 height
= rc
.bottom
- rc
.top
;
950 size_t nCount
= m_pages
.Count();
951 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
952 wxNotebookPage
*pPage
= m_pages
[nPage
];
953 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
957 // unless we had already repainted everything, we now need to refresh
958 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
960 // invalidate areas not covered by pages
961 RefreshRect(wxRect(0, 0, widthNbook
, rc
.top
), false);
962 RefreshRect(wxRect(0, rc
.top
, rc
.left
, height
), false);
963 RefreshRect(wxRect(0, rc
.bottom
, widthNbook
, heightNbook
- rc
.bottom
),
965 RefreshRect(wxRect(rc
.right
, rc
.top
, widthNbook
- rc
.right
, height
),
969 #if USE_NOTEBOOK_ANTIFLICKER
970 // subclass the spin control used by the notebook to scroll pages to
971 // prevent it from flickering on resize
972 if ( !m_hasSubclassedUpdown
)
974 // iterate over all child windows to find spin button
975 for ( HWND child
= ::GetWindow(GetHwnd(), GW_CHILD
);
977 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
979 wxWindow
*childWindow
= wxFindWinFromHandle((WXHWND
)child
);
981 // see if it exists, if no wxWindow found then assume it's the spin
985 // subclass the spin button to override WM_ERASEBKGND
986 if ( !gs_wndprocNotebookSpinBtn
)
987 gs_wndprocNotebookSpinBtn
= (WXFARPROC
)wxGetWindowProc(child
);
989 wxSetWindowProc(child
, wxNotebookSpinBtnWndProc
);
990 m_hasSubclassedUpdown
= true;
996 // Probably because of the games we play above to avoid flicker sometimes
997 // the text controls inside notebook pages are not shown correctly (they
998 // don't have their borders) when the notebook is shown for the first time.
999 // It's not really clear why does this happen and maybe the bug is in
1000 // wxTextCtrl itself and not here but updating the page when it's about to
1001 // be shown doesn't cost much and works around the problem so do it here
1003 if ( !m_doneUpdateHack
&& IsShownOnScreen() )
1005 m_doneUpdateHack
= true;
1006 wxWindow
* const page
= GetCurrentPage();
1010 #endif // USE_NOTEBOOK_ANTIFLICKER
1015 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
1017 if ( event
.IsWindowChange() ) {
1019 AdvanceSelection(event
.GetDirection());
1022 // we get this event in 3 cases
1024 // a) one of our pages might have generated it because the user TABbed
1025 // out from it in which case we should propagate the event upwards and
1026 // our parent will take care of setting the focus to prev/next sibling
1030 // b) the parent panel wants to give the focus to us so that we
1031 // forward it to our selected page. We can't deal with this in
1032 // OnSetFocus() because we don't know which direction the focus came
1033 // from in this case and so can't choose between setting the focus to
1034 // first or last panel child
1038 // c) we ourselves (see MSWTranslateMessage) generated the event
1040 wxWindow
* const parent
= GetParent();
1042 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
1043 const bool isFromParent
= event
.GetEventObject() == (wxObject
*) parent
;
1044 const bool isFromSelf
= event
.GetEventObject() == (wxObject
*) this;
1045 const bool isForward
= event
.GetDirection();
1047 if ( isFromSelf
&& !isForward
)
1049 // focus is currently on notebook tab and should leave
1050 // it backwards (Shift-TAB)
1051 event
.SetCurrentFocus(this);
1052 parent
->HandleWindowEvent(event
);
1054 else if ( isFromParent
|| isFromSelf
)
1056 // no, it doesn't come from child, case (b) or (c): forward to a
1057 // page but only if entering notebook page (i.e. direction is
1058 // backwards (Shift-TAB) comething from out-of-notebook, or
1059 // direction is forward (TAB) from ourselves),
1060 if ( m_selection
!= wxNOT_FOUND
&&
1061 (!event
.GetDirection() || isFromSelf
) )
1063 // so that the page knows that the event comes from it's parent
1064 // and is being propagated downwards
1065 event
.SetEventObject(this);
1067 wxWindow
*page
= m_pages
[m_selection
];
1068 if ( !page
->HandleWindowEvent(event
) )
1072 //else: page manages focus inside it itself
1074 else // otherwise set the focus to the notebook itself
1081 // it comes from our child, case (a), pass to the parent, but only
1082 // if the direction is forwards. Otherwise set the focus to the
1083 // notebook itself. The notebook is always the 'first' control of a
1091 event
.SetCurrentFocus(this);
1092 parent
->HandleWindowEvent(event
);
1100 bool wxNotebook::DoDrawBackground(WXHDC hDC
, wxWindow
*child
)
1102 wxUxThemeHandle
theme(child
? child
: this, L
"TAB");
1106 // get the notebook client rect (we're not interested in drawing tabs
1108 wxRect r
= GetPageSize();
1113 wxCopyRectToRECT(r
, rc
);
1115 // map rect to the coords of the window we're drawing in
1117 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1119 // we have the content area (page size), but we need to draw all of the
1120 // background for it to be aligned correctly
1121 wxUxThemeEngine::Get()->GetThemeBackgroundExtent
1130 wxUxThemeEngine::Get()->DrawThemeBackground
1143 WXHBRUSH
wxNotebook::QueryBgBitmap()
1145 wxRect r
= GetPageSize();
1149 WindowHDC
hDC(GetHwnd());
1150 MemoryHDC
hDCMem(hDC
);
1151 CompatibleBitmap
hBmp(hDC
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1153 SelectInHDC
selectBmp(hDCMem
, hBmp
);
1155 if ( !DoDrawBackground((WXHDC
)(HDC
)hDCMem
) )
1158 return (WXHBRUSH
)::CreatePatternBrush(hBmp
);
1161 void wxNotebook::UpdateBgBrush()
1163 if ( m_hbrBackground
)
1164 ::DeleteObject((HBRUSH
)m_hbrBackground
);
1166 if ( !m_hasBgCol
&& wxUxThemeEngine::GetIfActive() )
1168 m_hbrBackground
= QueryBgBitmap();
1170 else // no themes or we've got user-defined solid colour
1172 m_hbrBackground
= NULL
;
1176 bool wxNotebook::MSWPrintChild(WXHDC hDC
, wxWindow
*child
)
1178 // solid background colour overrides themed background drawing
1179 if ( !UseBgCol() && DoDrawBackground(hDC
, child
) )
1182 // If we're using a solid colour (for example if we've switched off
1183 // theming for this notebook), paint it
1186 wxRect r
= GetPageSize();
1191 wxCopyRectToRECT(r
, rc
);
1193 // map rect to the coords of the window we're drawing in
1195 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1197 wxBrush
brush(GetBackgroundColour());
1198 HBRUSH hbr
= GetHbrushOf(brush
);
1200 ::FillRect((HDC
) hDC
, &rc
, hbr
);
1205 return wxNotebookBase::MSWPrintChild(hDC
, child
);
1208 #endif // wxUSE_UXTHEME
1210 // Windows only: attempts to get colour for UX theme page background
1211 wxColour
wxNotebook::GetThemeBackgroundColour() const
1214 if (wxUxThemeEngine::Get())
1216 wxUxThemeHandle
hTheme((wxNotebook
*) this, L
"TAB");
1219 // This is total guesswork.
1220 // See PlatformSDK\Include\Tmschema.h for values.
1221 // JACS: can also use 9 (TABP_PANE)
1222 COLORREF themeColor
;
1223 bool success
= (S_OK
== wxUxThemeEngine::Get()->GetThemeColor(
1227 3821 /* FILLCOLORHINT */,
1230 return GetBackgroundColour();
1233 [DS] Workaround for WindowBlinds:
1234 Some themes return a near black theme color using FILLCOLORHINT,
1235 this makes notebook pages have an ugly black background and makes
1236 text (usually black) unreadable. Retry again with FILLCOLOR.
1238 This workaround potentially breaks appearance of some themes,
1239 but in practice it already fixes some themes.
1241 if (themeColor
== 1)
1243 wxUxThemeEngine::Get()->GetThemeColor(
1247 3802 /* FILLCOLOR */,
1251 wxColour colour
= wxRGBToColour(themeColor
);
1253 // Under Vista, the tab background colour is reported incorrectly.
1254 // So for the default theme at least, hard-code the colour to something
1255 // that will blend in.
1257 static int s_AeroStatus
= -1;
1258 if (s_AeroStatus
== -1)
1260 WCHAR szwThemeFile
[1024];
1261 WCHAR szwThemeColor
[256];
1262 if (S_OK
== wxUxThemeEngine::Get()->GetCurrentThemeName(szwThemeFile
, 1024, szwThemeColor
, 256, NULL
, 0))
1264 wxString
themeFile(szwThemeFile
), themeColor(szwThemeColor
);
1265 if (themeFile
.Find(wxT("Aero")) != -1 && themeColor
== wxT("NormalColor"))
1274 if (s_AeroStatus
== 1)
1275 colour
= wxColour(255, 255, 255);
1280 #endif // wxUSE_UXTHEME
1282 return GetBackgroundColour();
1285 // ----------------------------------------------------------------------------
1286 // wxNotebook base class virtuals
1287 // ----------------------------------------------------------------------------
1289 #if wxUSE_CONSTRAINTS
1291 // override these 2 functions to do nothing: everything is done in OnSize
1293 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
1295 // don't set the sizes of the pages - their correct size is not yet known
1296 wxControl::SetConstraintSizes(false);
1299 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
1304 #endif // wxUSE_CONSTRAINTS
1306 // ----------------------------------------------------------------------------
1307 // wxNotebook Windows message handlers
1308 // ----------------------------------------------------------------------------
1310 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
1311 WXWORD pos
, WXHWND control
)
1313 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1318 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
1321 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
1323 wxBookCtrlEvent
event(wxEVT_NULL
, m_windowId
);
1325 NMHDR
* hdr
= (NMHDR
*)lParam
;
1326 switch ( hdr
->code
) {
1328 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
1331 case TCN_SELCHANGING
:
1332 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
1336 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1339 event
.SetSelection(TabCtrl_GetCurSel(GetHwnd()));
1340 event
.SetOldSelection(m_selection
);
1341 event
.SetEventObject(this);
1342 event
.SetInt(idCtrl
);
1344 // Change the selection before generating the event as its handler should
1345 // already see the new page selected.
1346 if ( hdr
->code
== TCN_SELCHANGE
)
1347 UpdateSelection(event
.GetSelection());
1349 bool processed
= HandleWindowEvent(event
);
1350 *result
= !event
.IsAllowed();
1354 #endif // wxUSE_NOTEBOOK