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"
35 #include "wx/imaglist.h"
36 #include "wx/sysopt.h"
38 #include "wx/msw/private.h"
41 #include "wx/msw/winundef.h"
44 #include "wx/msw/uxtheme.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // check that the page index is valid
52 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
54 // you can set USE_NOTEBOOK_ANTIFLICKER to 0 for desktop Windows versions too
55 // to disable code whih results in flicker-less notebook redrawing at the
56 // expense of some extra GDI resource consumption
58 // notebooks are never resized under CE anyhow
59 #define USE_NOTEBOOK_ANTIFLICKER 0
61 #define USE_NOTEBOOK_ANTIFLICKER 1
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // This is a work-around for missing defines in gcc-2.95 headers
70 #define TCS_RIGHT 0x0002
74 #define TCS_VERTICAL 0x0080
78 #define TCS_BOTTOM TCS_RIGHT
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 #if USE_NOTEBOOK_ANTIFLICKER
87 // the pointer to standard spin button wnd proc
88 static WXFARPROC gs_wndprocNotebookSpinBtn
= (WXFARPROC
)NULL
;
90 // the pointer to standard tab control wnd proc
91 static WXFARPROC gs_wndprocNotebook
= (WXFARPROC
)NULL
;
93 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
98 #endif // USE_NOTEBOOK_ANTIFLICKER
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 #include "wx/listimpl.cpp"
106 WX_DEFINE_LIST( wxNotebookPageInfoList
)
108 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
109 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
111 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
112 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
113 EVT_SIZE(wxNotebook::OnSize
)
114 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
116 #if USE_NOTEBOOK_ANTIFLICKER
117 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
)
118 EVT_PAINT(wxNotebook::OnPaint
)
119 #endif // USE_NOTEBOOK_ANTIFLICKER
122 #if wxUSE_EXTENDED_RTTI
123 WX_DEFINE_FLAGS( wxNotebookStyle
)
125 wxBEGIN_FLAGS( wxNotebookStyle
)
126 // new style border flags, we put them first to
127 // use them for streaming out
128 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
129 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
130 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
131 wxFLAGS_MEMBER(wxBORDER_RAISED
)
132 wxFLAGS_MEMBER(wxBORDER_STATIC
)
133 wxFLAGS_MEMBER(wxBORDER_NONE
)
135 // old style border flags
136 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
137 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
138 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
139 wxFLAGS_MEMBER(wxRAISED_BORDER
)
140 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
141 wxFLAGS_MEMBER(wxBORDER
)
143 // standard window styles
144 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
145 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
146 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
147 wxFLAGS_MEMBER(wxWANTS_CHARS
)
148 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
149 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
150 wxFLAGS_MEMBER(wxVSCROLL
)
151 wxFLAGS_MEMBER(wxHSCROLL
)
153 wxFLAGS_MEMBER(wxNB_FIXEDWIDTH
)
154 wxFLAGS_MEMBER(wxBK_DEFAULT
)
155 wxFLAGS_MEMBER(wxBK_TOP
)
156 wxFLAGS_MEMBER(wxBK_LEFT
)
157 wxFLAGS_MEMBER(wxBK_RIGHT
)
158 wxFLAGS_MEMBER(wxBK_BOTTOM
)
159 wxFLAGS_MEMBER(wxNB_NOPAGETHEME
)
160 wxFLAGS_MEMBER(wxNB_FLAT
)
162 wxEND_FLAGS( wxNotebookStyle
)
164 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook
, wxControl
,"wx/notebook.h")
165 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo
, wxObject
, "wx/notebook.h" )
167 wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo
* , wxNotebookPageInfoList
) ;
169 template<> void wxCollectionToVariantArray( wxNotebookPageInfoList
const &theList
, wxxVariantArray
&value
)
171 wxListCollectionToVariantArray
<wxNotebookPageInfoList::compatibility_iterator
>( theList
, value
) ;
174 wxBEGIN_PROPERTIES_TABLE(wxNotebook
)
175 wxEVENT_PROPERTY( PageChanging
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, wxNotebookEvent
)
176 wxEVENT_PROPERTY( PageChanged
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, wxNotebookEvent
)
178 wxPROPERTY_COLLECTION( PageInfos
, wxNotebookPageInfoList
, wxNotebookPageInfo
* , AddPageInfo
, GetPageInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
179 wxPROPERTY_FLAGS( WindowStyle
, wxNotebookStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
180 wxEND_PROPERTIES_TABLE()
182 wxBEGIN_HANDLERS_TABLE(wxNotebook
)
183 wxEND_HANDLERS_TABLE()
185 wxCONSTRUCTOR_5( wxNotebook
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
188 wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo
)
189 wxREADONLY_PROPERTY( Page
, wxNotebookPage
* , GetPage
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
190 wxREADONLY_PROPERTY( Text
, wxString
, GetText
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
191 wxREADONLY_PROPERTY( Selected
, bool , GetSelected
, false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
192 wxREADONLY_PROPERTY( ImageId
, int , GetImageId
, -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
193 wxEND_PROPERTIES_TABLE()
195 wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo
)
196 wxEND_HANDLERS_TABLE()
198 wxCONSTRUCTOR_4( wxNotebookPageInfo
, wxNotebookPage
* , Page
, wxString
, Text
, bool , Selected
, int , ImageId
)
201 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
202 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo
, wxObject
)
204 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
206 // ============================================================================
208 // ============================================================================
210 // ----------------------------------------------------------------------------
211 // wxNotebook construction
212 // ----------------------------------------------------------------------------
214 const wxNotebookPageInfoList
& wxNotebook::GetPageInfos() const
216 wxNotebookPageInfoList
* list
= const_cast< wxNotebookPageInfoList
* >( &m_pageInfos
) ;
217 WX_CLEAR_LIST( wxNotebookPageInfoList
, *list
) ;
218 for( size_t i
= 0 ; i
< GetPageCount() ; ++i
)
220 wxNotebookPageInfo
*info
= new wxNotebookPageInfo() ;
221 info
->Create( const_cast<wxNotebook
*>(this)->GetPage(i
) , GetPageText(i
) , GetSelection() == int(i
) , GetPageImage(i
) ) ;
222 list
->Append( info
) ;
227 // common part of all ctors
228 void wxNotebook::Init()
234 m_hbrBackground
= NULL
;
235 #endif // wxUSE_UXTHEME
237 #if USE_NOTEBOOK_ANTIFLICKER
238 m_hasSubclassedUpdown
= false;
239 #endif // USE_NOTEBOOK_ANTIFLICKER
242 // default for dynamic class
243 wxNotebook::wxNotebook()
248 // the same arguments as for wxControl
249 wxNotebook::wxNotebook(wxWindow
*parent
,
254 const wxString
& name
)
258 Create(parent
, id
, pos
, size
, style
, name
);
262 bool wxNotebook::Create(wxWindow
*parent
,
267 const wxString
& name
)
269 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
271 #if defined(__POCKETPC__)
272 style
|= wxBK_BOTTOM
| wxNB_FLAT
;
279 // Not sure why, but without this style, there is no border
280 // around the notebook tabs.
281 if (style
& wxNB_FLAT
)
282 style
|= wxBORDER_SUNKEN
;
286 // ComCtl32 notebook tabs simply don't work unless they're on top if we have uxtheme, we can
287 // work around it later (after control creation), but if we don't have uxtheme, we have to clear
289 const int verComCtl32
= wxApp::GetComCtl32Version();
290 if ( verComCtl32
== 600 )
292 style
&= ~(wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
);
294 #endif //wxUSE_UXTHEME
296 LPCTSTR className
= WC_TABCONTROL
;
298 #if USE_NOTEBOOK_ANTIFLICKER
299 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
300 // causes horrible flicker when resizing notebook, so get rid of it by
301 // using a class without these styles (but otherwise identical to it)
302 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
304 static ClassRegistrar s_clsNotebook
;
305 if ( !s_clsNotebook
.IsInitialized() )
307 // get a copy of standard class and modify it
310 if ( ::GetClassInfo(NULL
, WC_TABCONTROL
, &wc
) )
313 wx_reinterpret_cast(WXFARPROC
, wc
.lpfnWndProc
);
314 wc
.lpszClassName
= wxT("_wx_SysTabCtl32");
315 wc
.style
&= ~(CS_HREDRAW
| CS_VREDRAW
);
316 wc
.hInstance
= wxGetInstance();
317 wc
.lpfnWndProc
= wxNotebookWndProc
;
318 s_clsNotebook
.Register(wc
);
322 wxLogLastError(_T("GetClassInfoEx(SysTabCtl32)"));
326 // use our custom class if available but fall back to the standard
327 // notebook if we failed to register it
328 if ( s_clsNotebook
.IsRegistered() )
330 // it's ok to use c_str() here as the static s_clsNotebook object
331 // has sufficiently long lifetime
332 className
= s_clsNotebook
.GetName().c_str();
335 #endif // USE_NOTEBOOK_ANTIFLICKER
337 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
338 wxDefaultValidator
, name
) )
341 if ( !MSWCreateControl(className
, wxEmptyString
, pos
, size
) )
345 if ( HasFlag(wxNB_NOPAGETHEME
) ||
346 wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) )
348 SetBackgroundColour(GetThemeBackgroundColour());
350 else // use themed background by default
352 // create backing store
356 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
357 // control is simply not rendered correctly), so we disable themes
358 // if possible, otherwise we simply clear the styles.
359 // It's probably not possible to have UXTHEME without ComCtl32 6 or better, but lets
361 const int verComCtl32
= wxApp::GetComCtl32Version();
362 if ( verComCtl32
== 600 )
364 // check if we use themes at all -- if we don't, we're still okay
365 if ( wxUxThemeEngine::GetIfActive() && (style
& (wxBK_BOTTOM
|wxBK_LEFT
|wxBK_RIGHT
)))
367 wxUxThemeEngine::GetIfActive()->SetWindowTheme((HWND
)this->GetHandle(), L
"", L
"");
368 SetBackgroundColour(GetThemeBackgroundColour()); //correct the background color for the new non-themed control
371 #endif // wxUSE_UXTHEME
373 // Undocumented hack to get flat notebook style
374 // In fact, we should probably only do this in some
375 // curcumstances, i.e. if we know we will have a border
376 // at the bottom (the tab control doesn't draw it itself)
377 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
378 if (HasFlag(wxNB_FLAT
))
380 SendMessage(GetHwnd(), CCM_SETVERSION
, COMCTL32_VERSION
, 0);
382 SetBackgroundColour(*wxWHITE
);
388 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
390 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
392 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
394 if ( style
& wxNB_MULTILINE
)
395 tabStyle
|= TCS_MULTILINE
;
396 if ( style
& wxNB_FIXEDWIDTH
)
397 tabStyle
|= TCS_FIXEDWIDTH
;
399 if ( style
& wxBK_BOTTOM
)
400 tabStyle
|= TCS_RIGHT
;
401 else if ( style
& wxBK_LEFT
)
402 tabStyle
|= TCS_VERTICAL
;
403 else if ( style
& wxBK_RIGHT
)
404 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
409 // note that we never want to have the default WS_EX_CLIENTEDGE style
410 // as it looks too ugly for the notebooks
417 wxNotebook::~wxNotebook()
420 if ( m_hbrBackground
)
421 ::DeleteObject((HBRUSH
)m_hbrBackground
);
422 #endif // wxUSE_UXTHEME
425 // ----------------------------------------------------------------------------
426 // wxNotebook accessors
427 // ----------------------------------------------------------------------------
429 size_t wxNotebook::GetPageCount() const
432 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(GetHwnd()) );
434 return m_pages
.Count();
437 int wxNotebook::GetRowCount() const
439 return TabCtrl_GetRowCount(GetHwnd());
442 int wxNotebook::SetSelection(size_t nPage
)
444 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
446 if ( int(nPage
) != m_nSelection
)
448 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
449 event
.SetSelection(nPage
);
450 event
.SetOldSelection(m_nSelection
);
451 event
.SetEventObject(this);
452 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
454 // program allows the page change
455 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
456 (void)GetEventHandler()->ProcessEvent(event
);
458 TabCtrl_SetCurSel(GetHwnd(), nPage
);
465 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
467 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
470 tcItem
.mask
= TCIF_TEXT
;
471 tcItem
.pszText
= (wxChar
*)strText
.c_str();
473 if ( !HasFlag(wxNB_MULTILINE
) )
474 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
476 // multiline - we need to set new page size if a line is added or removed
477 int rows
= GetRowCount();
478 bool ret
= TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
480 if ( ret
&& rows
!= GetRowCount() )
482 const wxRect r
= GetPageSize();
483 const size_t count
= m_pages
.Count();
484 for ( size_t page
= 0; page
< count
; page
++ )
485 m_pages
[page
]->SetSize(r
);
491 wxString
wxNotebook::GetPageText(size_t nPage
) const
493 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
497 tcItem
.mask
= TCIF_TEXT
;
498 tcItem
.pszText
= buf
;
499 tcItem
.cchTextMax
= WXSIZEOF(buf
);
502 if ( TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) )
503 str
= tcItem
.pszText
;
508 int wxNotebook::GetPageImage(size_t nPage
) const
510 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
513 tcItem
.mask
= TCIF_IMAGE
;
515 return TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) ? tcItem
.iImage
: wxNOT_FOUND
;
518 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
520 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
523 tcItem
.mask
= TCIF_IMAGE
;
524 tcItem
.iImage
= nImage
;
526 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
529 void wxNotebook::SetImageList(wxImageList
* imageList
)
531 wxNotebookBase::SetImageList(imageList
);
535 (void) TabCtrl_SetImageList(GetHwnd(), (HIMAGELIST
)imageList
->GetHIMAGELIST());
539 // ----------------------------------------------------------------------------
540 // wxNotebook size settings
541 // ----------------------------------------------------------------------------
543 wxRect
wxNotebook::GetPageSize() const
548 ::GetClientRect(GetHwnd(), &rc
);
550 // This check is to work around a bug in TabCtrl_AdjustRect which will
551 // cause a crash on win2k or on XP with themes disabled if either
552 // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle
555 // The value of 20 is chosen arbitrarily but seems to work
556 if ( rc
.right
> 20 && rc
.bottom
> 20 )
558 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
560 wxCopyRECTToRect(rc
, r
);
566 void wxNotebook::SetPageSize(const wxSize
& size
)
568 // transform the page size into the notebook size
575 TabCtrl_AdjustRect(GetHwnd(), true, &rc
);
578 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
581 void wxNotebook::SetPadding(const wxSize
& padding
)
583 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
586 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
588 void wxNotebook::SetTabSize(const wxSize
& sz
)
590 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
593 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
595 // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP
596 wxSize sizeTotal
= sizePage
;
599 if ( GetPageCount() > 0 )
602 TabCtrl_GetItemRect(GetHwnd(), 0, &rect
);
603 tabSize
.x
= rect
.right
- rect
.left
;
604 tabSize
.y
= rect
.bottom
- rect
.top
;
607 // add an extra margin in both directions
608 const int MARGIN
= 8;
611 sizeTotal
.x
+= MARGIN
;
612 sizeTotal
.y
+= tabSize
.y
+ MARGIN
;
614 else // horizontal layout
616 sizeTotal
.x
+= tabSize
.x
+ MARGIN
;
617 sizeTotal
.y
+= MARGIN
;
623 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
625 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
627 const wxRect r
= GetPageSize();
634 // ----------------------------------------------------------------------------
635 // wxNotebook operations
636 // ----------------------------------------------------------------------------
638 // remove one page from the notebook, without deleting
639 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
641 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
645 TabCtrl_DeleteItem(GetHwnd(), nPage
);
647 if ( m_pages
.IsEmpty() )
649 // no selection any more, the notebook becamse empty
652 else // notebook still not empty
654 int selNew
= TabCtrl_GetCurSel(GetHwnd());
657 // No selection change, just refresh the current selection.
658 // Because it could be that the slection index changed
659 // we need to update it.
660 // Note: this does not mean the selection it self changed.
661 m_nSelection
= selNew
;
662 m_pages
[m_nSelection
]->Refresh();
664 else if (int(nPage
) == m_nSelection
)
666 // The selection was deleted.
668 // Determine new selection.
669 if (m_nSelection
== int(GetPageCount()))
670 selNew
= m_nSelection
- 1;
672 selNew
= m_nSelection
;
674 // m_nSelection must be always valid so reset it before calling
677 SetSelection(selNew
);
681 wxFAIL
; // Windows did not behave ok.
689 bool wxNotebook::DeleteAllPages()
691 size_t nPageCount
= GetPageCount();
693 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
694 delete m_pages
[nPage
];
698 TabCtrl_DeleteAllItems(GetHwnd());
702 InvalidateBestSize();
706 // same as AddPage() but does it at given position
707 bool wxNotebook::InsertPage(size_t nPage
,
708 wxNotebookPage
*pPage
,
709 const wxString
& strText
,
713 wxCHECK_MSG( pPage
!= NULL
, false, _T("NULL page in wxNotebook::InsertPage") );
714 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false,
715 _T("invalid index in wxNotebook::InsertPage") );
717 wxASSERT_MSG( pPage
->GetParent() == this,
718 _T("notebook pages must have notebook as parent") );
720 // add a new tab to the control
721 // ----------------------------
723 // init all fields to 0
725 wxZeroMemory(tcItem
);
727 // set the image, if any
730 tcItem
.mask
|= TCIF_IMAGE
;
731 tcItem
.iImage
= imageId
;
735 if ( !strText
.empty() )
737 tcItem
.mask
|= TCIF_TEXT
;
738 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
741 // hide the page: unless it is selected, it shouldn't be shown (and if it
742 // is selected it will be shown later)
743 HWND hwnd
= GetWinHwnd(pPage
);
744 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
746 // this updates internal flag too -- otherwise it would get out of sync
747 // with the real state
751 // fit the notebook page to the tab control's display area: this should be
752 // done before adding it to the notebook or TabCtrl_InsertItem() will
753 // change the notebooks size itself!
754 AdjustPageSize(pPage
);
756 // finally do insert it
757 if ( TabCtrl_InsertItem(GetHwnd(), nPage
, &tcItem
) == -1 )
759 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
764 // need to update the bg brush when the first page is added
765 // so the first panel gets the correct themed background
766 if ( m_pages
.empty() )
771 // succeeded: save the pointer to the page
772 m_pages
.Insert(pPage
, nPage
);
774 // we may need to adjust the size again if the notebook size changed:
775 // normally this only happens for the first page we add (the tabs which
776 // hadn't been there before are now shown) but for a multiline notebook it
777 // can happen for any page at all as a new row could have been started
778 if ( m_pages
.GetCount() == 1 || HasFlag(wxNB_MULTILINE
) )
780 AdjustPageSize(pPage
);
783 // now deal with the selection
784 // ---------------------------
786 // if the inserted page is before the selected one, we must update the
787 // index of the selected page
788 if ( int(nPage
) <= m_nSelection
)
790 // one extra page added
794 // some page should be selected: either this one or the first one if there
795 // is still no selection
799 else if ( m_nSelection
== -1 )
803 SetSelection(selNew
);
805 InvalidateBestSize();
810 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
812 TC_HITTESTINFO hitTestInfo
;
813 hitTestInfo
.pt
.x
= pt
.x
;
814 hitTestInfo
.pt
.y
= pt
.y
;
815 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
821 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
822 *flags
|= wxBK_HITTEST_NOWHERE
;
823 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
824 *flags
|= wxBK_HITTEST_ONITEM
;
825 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
826 *flags
|= wxBK_HITTEST_ONICON
;
827 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
828 *flags
|= wxBK_HITTEST_ONLABEL
;
829 if ( item
== wxNOT_FOUND
&& GetPageSize().Contains(pt
) )
830 *flags
|= wxBK_HITTEST_ONPAGE
;
836 // ----------------------------------------------------------------------------
837 // flicker-less notebook redraw
838 // ----------------------------------------------------------------------------
840 #if USE_NOTEBOOK_ANTIFLICKER
842 // wnd proc for the spin button
843 LRESULT APIENTRY _EXPORT
wxNotebookSpinBtnWndProc(HWND hwnd
,
848 if ( message
== WM_ERASEBKGND
)
851 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn
,
852 hwnd
, message
, wParam
, lParam
);
855 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
860 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook
,
861 hwnd
, message
, wParam
, lParam
);
864 void wxNotebook::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
869 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
))
874 ::GetClientRect(GetHwnd(), &rc
);
875 wxBitmap
bmp(rc
.right
, rc
.bottom
);
876 memdc
.SelectObject(bmp
);
878 // if there is no special brush just use the solid background colour
880 HBRUSH hbr
= (HBRUSH
)m_hbrBackground
;
887 brush
= wxBrush(GetBackgroundColour());
888 hbr
= GetHbrushOf(brush
);
891 ::FillRect(GetHdcOf(memdc
), &rc
, hbr
);
893 MSWDefWindowProc(WM_PAINT
, (WPARAM
)memdc
.GetHDC(), 0);
895 dc
.Blit(0, 0, rc
.right
, rc
.bottom
, &memdc
, 0, 0);
898 #endif // USE_NOTEBOOK_ANTIFLICKER
900 // ----------------------------------------------------------------------------
901 // wxNotebook callbacks
902 // ----------------------------------------------------------------------------
904 void wxNotebook::OnSize(wxSizeEvent
& event
)
906 if ( GetPageCount() == 0 )
908 // Prevents droppings on resize, but does cause some flicker
909 // when there are no pages.
917 // Without this, we can sometimes get droppings at the edges
918 // of a notebook, for example a notebook in a splitter window.
919 // This needs to be reconciled with the RefreshRect calls
920 // at the end of this function, which weren't enough to prevent
923 wxSize sz
= GetClientSize();
925 // Refresh right side
926 wxRect
rect(sz
.x
-4, 0, 4, sz
.y
);
929 // Refresh bottom side
930 rect
= wxRect(0, sz
.y
-4, sz
.x
, 4);
934 rect
= wxRect(0, 0, 4, sz
.y
);
937 #endif // !__WXWINCE__
939 // fit all the notebook pages to the tab control's display area
942 rc
.left
= rc
.top
= 0;
943 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
945 // save the total size, we'll use it below
946 int widthNbook
= rc
.right
- rc
.left
,
947 heightNbook
= rc
.bottom
- rc
.top
;
949 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
950 // returns completely false values for multiline tab controls after the tabs
951 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
953 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
955 // and the only work around I could find was this ugly hack... without it
956 // simply toggling the "multiline" checkbox in the notebook sample resulted
957 // in a noticeable page displacement
958 if ( HasFlag(wxNB_MULTILINE
) )
960 // avoid an infinite recursion: we get another notification too!
961 static bool s_isInOnSize
= false;
966 SendMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
,
967 MAKELPARAM(rc
.right
, rc
.bottom
));
968 s_isInOnSize
= false;
973 // background bitmap size has changed, update the brush using it too
975 #endif // wxUSE_UXTHEME
977 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
979 int width
= rc
.right
- rc
.left
,
980 height
= rc
.bottom
- rc
.top
;
981 size_t nCount
= m_pages
.Count();
982 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
983 wxNotebookPage
*pPage
= m_pages
[nPage
];
984 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
988 // unless we had already repainted everything, we now need to refresh
989 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
991 // invalidate areas not covered by pages
992 RefreshRect(wxRect(0, 0, widthNbook
, rc
.top
), false);
993 RefreshRect(wxRect(0, rc
.top
, rc
.left
, height
), false);
994 RefreshRect(wxRect(0, rc
.bottom
, widthNbook
, heightNbook
- rc
.bottom
),
996 RefreshRect(wxRect(rc
.right
, rc
.top
, widthNbook
- rc
.right
, height
),
1000 #if USE_NOTEBOOK_ANTIFLICKER
1001 // subclass the spin control used by the notebook to scroll pages to
1002 // prevent it from flickering on resize
1003 if ( !m_hasSubclassedUpdown
)
1005 // iterate over all child windows to find spin button
1006 for ( HWND child
= ::GetWindow(GetHwnd(), GW_CHILD
);
1008 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
1010 wxWindow
*childWindow
= wxFindWinFromHandle((WXHWND
)child
);
1012 // see if it exists, if no wxWindow found then assume it's the spin
1016 // subclass the spin button to override WM_ERASEBKGND
1017 if ( !gs_wndprocNotebookSpinBtn
)
1018 gs_wndprocNotebookSpinBtn
= (WXFARPROC
)wxGetWindowProc(child
);
1020 wxSetWindowProc(child
, wxNotebookSpinBtnWndProc
);
1021 m_hasSubclassedUpdown
= true;
1026 #endif // USE_NOTEBOOK_ANTIFLICKER
1031 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
1033 // is it our tab control?
1034 if ( event
.GetEventObject() == this )
1036 int sel
= event
.GetOldSelection();
1038 m_pages
[sel
]->Show(false);
1040 sel
= event
.GetSelection();
1043 wxNotebookPage
*pPage
= m_pages
[sel
];
1047 // Changing the page should give the focus to it but, as per bug report
1048 // http://sf.net/tracker/index.php?func=detail&aid=1150659&group_id=9863&atid=109863,
1049 // we should not set the focus to it directly since it erroneously
1050 // selects radio buttons and breaks keyboard handling for a notebook's
1051 // scroll buttons. So give focus to the notebook and not the page.
1053 // but don't do this is the notebook is hidden
1054 if ( ::IsWindowVisible(GetHwnd()) )
1060 // we want to give others a chance to process this message as well
1064 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
1066 if ( event
.IsWindowChange() ) {
1068 AdvanceSelection(event
.GetDirection());
1071 // we get this event in 3 cases
1073 // a) one of our pages might have generated it because the user TABbed
1074 // out from it in which case we should propagate the event upwards and
1075 // our parent will take care of setting the focus to prev/next sibling
1079 // b) the parent panel wants to give the focus to us so that we
1080 // forward it to our selected page. We can't deal with this in
1081 // OnSetFocus() because we don't know which direction the focus came
1082 // from in this case and so can't choose between setting the focus to
1083 // first or last panel child
1087 // c) we ourselves (see MSWTranslateMessage) generated the event
1089 wxWindow
* const parent
= GetParent();
1091 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
1092 const bool isFromParent
= event
.GetEventObject() == (wxObject
*) parent
;
1093 const bool isFromSelf
= event
.GetEventObject() == (wxObject
*) this;
1095 if ( isFromParent
|| isFromSelf
)
1097 // no, it doesn't come from child, case (b) or (c): forward to a
1098 // page but only if direction is backwards (TAB) or from ourselves,
1099 if ( m_nSelection
!= -1 &&
1100 (!event
.GetDirection() || isFromSelf
) )
1102 // so that the page knows that the event comes from it's parent
1103 // and is being propagated downwards
1104 event
.SetEventObject(this);
1106 wxWindow
*page
= m_pages
[m_nSelection
];
1107 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
1111 //else: page manages focus inside it itself
1113 else // otherwise set the focus to the notebook itself
1120 // it comes from our child, case (a), pass to the parent, but only
1121 // if the direction is forwards. Otherwise set the focus to the
1122 // notebook itself. The notebook is always the 'first' control of a
1124 if ( !event
.GetDirection() )
1130 event
.SetCurrentFocus(this);
1131 parent
->GetEventHandler()->ProcessEvent(event
);
1139 bool wxNotebook::DoDrawBackground(WXHDC hDC
, wxWindow
*child
)
1141 wxUxThemeHandle
theme(child
? child
: this, L
"TAB");
1145 // get the notebook client rect (we're not interested in drawing tabs
1147 wxRect r
= GetPageSize();
1152 wxCopyRectToRECT(r
, rc
);
1154 // map rect to the coords of the window we're drawing in
1156 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1158 // we have the content area (page size), but we need to draw all of the
1159 // background for it to be aligned correctly
1160 wxUxThemeEngine::Get()->GetThemeBackgroundExtent
1169 wxUxThemeEngine::Get()->DrawThemeBackground
1182 WXHBRUSH
wxNotebook::QueryBgBitmap()
1184 wxRect r
= GetPageSize();
1188 WindowHDC
hDC(GetHwnd());
1189 MemoryHDC
hDCMem(hDC
);
1190 CompatibleBitmap
hBmp(hDC
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1192 SelectInHDC
selectBmp(hDCMem
, hBmp
);
1194 if ( !DoDrawBackground((WXHDC
)(HDC
)hDCMem
) )
1197 return (WXHBRUSH
)::CreatePatternBrush(hBmp
);
1200 void wxNotebook::UpdateBgBrush()
1202 if ( m_hbrBackground
)
1203 ::DeleteObject((HBRUSH
)m_hbrBackground
);
1205 if ( !m_hasBgCol
&& wxUxThemeEngine::GetIfActive() )
1207 m_hbrBackground
= QueryBgBitmap();
1209 else // no themes or we've got user-defined solid colour
1211 m_hbrBackground
= NULL
;
1215 WXHBRUSH
wxNotebook::MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
)
1217 if ( m_hbrBackground
)
1219 // before drawing with the background brush, we need to position it
1222 ::GetWindowRect((HWND
)hWnd
, &rc
);
1224 ::MapWindowPoints(NULL
, GetHwnd(), (POINT
*)&rc
, 1);
1226 if ( !::SetBrushOrgEx((HDC
)hDC
, -rc
.left
, -rc
.top
, NULL
) )
1228 wxLogLastError(_T("SetBrushOrgEx(notebook bg brush)"));
1231 return m_hbrBackground
;
1234 return wxNotebookBase::MSWGetBgBrushForChild(hDC
, hWnd
);
1237 bool wxNotebook::MSWPrintChild(WXHDC hDC
, wxWindow
*child
)
1239 // solid background colour overrides themed background drawing
1240 if ( !UseBgCol() && DoDrawBackground(hDC
, child
) )
1243 // If we're using a solid colour (for example if we've switched off
1244 // theming for this notebook), paint it
1247 wxRect r
= GetPageSize();
1252 wxCopyRectToRECT(r
, rc
);
1254 // map rect to the coords of the window we're drawing in
1256 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1258 wxBrush
brush(GetBackgroundColour());
1259 HBRUSH hbr
= GetHbrushOf(brush
);
1261 ::FillRect((HDC
) hDC
, &rc
, hbr
);
1266 return wxNotebookBase::MSWPrintChild(hDC
, child
);
1269 #endif // wxUSE_UXTHEME
1271 // Windows only: attempts to get colour for UX theme page background
1272 wxColour
wxNotebook::GetThemeBackgroundColour() const
1275 if (wxUxThemeEngine::Get())
1277 wxUxThemeHandle
hTheme((wxNotebook
*) this, L
"TAB");
1280 // This is total guesswork.
1281 // See PlatformSDK\Include\Tmschema.h for values
1282 COLORREF themeColor
;
1283 wxUxThemeEngine::Get()->GetThemeColor(
1287 3821 /* FILLCOLORHINT */,
1291 [DS] Workaround for WindowBlinds:
1292 Some themes return a near black theme color using FILLCOLORHINT,
1293 this makes notebook pages have an ugly black background and makes
1294 text (usually black) unreadable. Retry again with FILLCOLOR.
1296 This workaround potentially breaks appearance of some themes,
1297 but in practice it already fixes some themes.
1299 if (themeColor
== 1)
1301 wxUxThemeEngine::Get()->GetThemeColor(
1305 3802 /* FILLCOLOR */,
1309 return wxRGBToColour(themeColor
);
1312 #endif // wxUSE_UXTHEME
1314 return GetBackgroundColour();
1317 // ----------------------------------------------------------------------------
1318 // wxNotebook base class virtuals
1319 // ----------------------------------------------------------------------------
1321 #if wxUSE_CONSTRAINTS
1323 // override these 2 functions to do nothing: everything is done in OnSize
1325 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
1327 // don't set the sizes of the pages - their correct size is not yet known
1328 wxControl::SetConstraintSizes(false);
1331 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
1336 #endif // wxUSE_CONSTRAINTS
1338 // ----------------------------------------------------------------------------
1339 // wxNotebook Windows message handlers
1340 // ----------------------------------------------------------------------------
1342 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
1343 WXWORD pos
, WXHWND control
)
1345 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1350 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
1353 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
1355 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
1357 NMHDR
* hdr
= (NMHDR
*)lParam
;
1358 switch ( hdr
->code
) {
1360 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
1363 case TCN_SELCHANGING
:
1364 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
1368 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1371 event
.SetSelection(TabCtrl_GetCurSel(GetHwnd()));
1372 event
.SetOldSelection(m_nSelection
);
1373 event
.SetEventObject(this);
1374 event
.SetInt(idCtrl
);
1376 bool processed
= GetEventHandler()->ProcessEvent(event
);
1377 *result
= !event
.IsAllowed();
1381 #endif // wxUSE_NOTEBOOK