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"
39 #include "wx/msw/dc.h"
42 #include "wx/msw/winundef.h"
45 #include "wx/msw/uxtheme.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // check that the page index is valid
53 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
55 // you can set USE_NOTEBOOK_ANTIFLICKER to 0 for desktop Windows versions too
56 // to disable code whih results in flicker-less notebook redrawing at the
57 // expense of some extra GDI resource consumption
59 // notebooks are never resized under CE anyhow
60 #define USE_NOTEBOOK_ANTIFLICKER 0
62 #define USE_NOTEBOOK_ANTIFLICKER 1
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // This is a work-around for missing defines in gcc-2.95 headers
71 #define TCS_RIGHT 0x0002
75 #define TCS_VERTICAL 0x0080
79 #define TCS_BOTTOM TCS_RIGHT
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 #if USE_NOTEBOOK_ANTIFLICKER
88 // the pointer to standard spin button wnd proc
89 static WXFARPROC gs_wndprocNotebookSpinBtn
= (WXFARPROC
)NULL
;
91 // the pointer to standard tab control wnd proc
92 static WXFARPROC gs_wndprocNotebook
= (WXFARPROC
)NULL
;
94 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
99 #endif // USE_NOTEBOOK_ANTIFLICKER
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 static bool HasTroubleWithNonTopTabs()
107 const int verComCtl32
= wxApp::GetComCtl32Version();
109 // 600 is XP, 616 is Vista -- and both have a problem with tabs not on top
110 // (but don't just test for >= 600 as Microsoft might decide to fix it in
111 // later versions, who knows...)
112 return verComCtl32
>= 600 && verComCtl32
<= 616;
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 #include "wx/listimpl.cpp"
121 WX_DEFINE_LIST( wxNotebookPageInfoList
)
123 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
124 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
126 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
127 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
128 EVT_SIZE(wxNotebook::OnSize
)
129 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
131 #if USE_NOTEBOOK_ANTIFLICKER
132 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
)
133 EVT_PAINT(wxNotebook::OnPaint
)
134 #endif // USE_NOTEBOOK_ANTIFLICKER
137 #if wxUSE_EXTENDED_RTTI
138 WX_DEFINE_FLAGS( wxNotebookStyle
)
140 wxBEGIN_FLAGS( wxNotebookStyle
)
141 // new style border flags, we put them first to
142 // use them for streaming out
143 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
144 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
145 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
146 wxFLAGS_MEMBER(wxBORDER_RAISED
)
147 wxFLAGS_MEMBER(wxBORDER_STATIC
)
148 wxFLAGS_MEMBER(wxBORDER_NONE
)
150 // old style border flags
151 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
152 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
153 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
154 wxFLAGS_MEMBER(wxRAISED_BORDER
)
155 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
156 wxFLAGS_MEMBER(wxBORDER
)
158 // standard window styles
159 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
160 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
161 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
162 wxFLAGS_MEMBER(wxWANTS_CHARS
)
163 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
164 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
165 wxFLAGS_MEMBER(wxVSCROLL
)
166 wxFLAGS_MEMBER(wxHSCROLL
)
168 wxFLAGS_MEMBER(wxNB_FIXEDWIDTH
)
169 wxFLAGS_MEMBER(wxBK_DEFAULT
)
170 wxFLAGS_MEMBER(wxBK_TOP
)
171 wxFLAGS_MEMBER(wxBK_LEFT
)
172 wxFLAGS_MEMBER(wxBK_RIGHT
)
173 wxFLAGS_MEMBER(wxBK_BOTTOM
)
174 wxFLAGS_MEMBER(wxNB_NOPAGETHEME
)
175 wxFLAGS_MEMBER(wxNB_FLAT
)
177 wxEND_FLAGS( wxNotebookStyle
)
179 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook
, wxBookCtrlBase
,"wx/notebook.h")
180 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo
, wxObject
, "wx/notebook.h" )
182 wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo
* , wxNotebookPageInfoList
) ;
184 template<> void wxCollectionToVariantArray( wxNotebookPageInfoList
const &theList
, wxxVariantArray
&value
)
186 wxListCollectionToVariantArray
<wxNotebookPageInfoList::compatibility_iterator
>( theList
, value
) ;
189 wxBEGIN_PROPERTIES_TABLE(wxNotebook
)
190 wxEVENT_PROPERTY( PageChanging
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, wxBookCtrlEvent
)
191 wxEVENT_PROPERTY( PageChanged
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, wxBookCtrlEvent
)
193 wxPROPERTY_COLLECTION( PageInfos
, wxNotebookPageInfoList
, wxNotebookPageInfo
* , AddPageInfo
, GetPageInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
194 wxPROPERTY_FLAGS( WindowStyle
, wxNotebookStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
195 wxEND_PROPERTIES_TABLE()
197 wxBEGIN_HANDLERS_TABLE(wxNotebook
)
198 wxEND_HANDLERS_TABLE()
200 wxCONSTRUCTOR_5( wxNotebook
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
203 wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo
)
204 wxREADONLY_PROPERTY( Page
, wxNotebookPage
* , GetPage
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
205 wxREADONLY_PROPERTY( Text
, wxString
, GetText
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
206 wxREADONLY_PROPERTY( Selected
, bool , GetSelected
, false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
207 wxREADONLY_PROPERTY( ImageId
, int , GetImageId
, -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
208 wxEND_PROPERTIES_TABLE()
210 wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo
)
211 wxEND_HANDLERS_TABLE()
213 wxCONSTRUCTOR_4( wxNotebookPageInfo
, wxNotebookPage
* , Page
, wxString
, Text
, bool , Selected
, int , ImageId
)
216 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
217 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo
, wxObject
)
220 // ============================================================================
222 // ============================================================================
224 // ----------------------------------------------------------------------------
225 // wxNotebook construction
226 // ----------------------------------------------------------------------------
228 const wxNotebookPageInfoList
& wxNotebook::GetPageInfos() const
230 wxNotebookPageInfoList
* list
= const_cast< wxNotebookPageInfoList
* >( &m_pageInfos
) ;
231 WX_CLEAR_LIST( wxNotebookPageInfoList
, *list
) ;
232 for( size_t i
= 0 ; i
< GetPageCount() ; ++i
)
234 wxNotebookPageInfo
*info
= new wxNotebookPageInfo() ;
235 info
->Create( const_cast<wxNotebook
*>(this)->GetPage(i
) , GetPageText(i
) , GetSelection() == int(i
) , GetPageImage(i
) ) ;
236 list
->Append( info
) ;
241 // common part of all ctors
242 void wxNotebook::Init()
245 m_nSelection
= wxNOT_FOUND
;
248 m_hbrBackground
= NULL
;
249 #endif // wxUSE_UXTHEME
251 #if USE_NOTEBOOK_ANTIFLICKER
252 m_hasSubclassedUpdown
= false;
253 #endif // USE_NOTEBOOK_ANTIFLICKER
256 // default for dynamic class
257 wxNotebook::wxNotebook()
262 // the same arguments as for wxControl
263 wxNotebook::wxNotebook(wxWindow
*parent
,
268 const wxString
& name
)
272 Create(parent
, id
, pos
, size
, style
, name
);
276 bool wxNotebook::Create(wxWindow
*parent
,
281 const wxString
& name
)
283 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
285 #if defined(__POCKETPC__)
286 style
|= wxBK_BOTTOM
| wxNB_FLAT
;
293 // Not sure why, but without this style, there is no border
294 // around the notebook tabs.
295 if (style
& wxNB_FLAT
)
296 style
|= wxBORDER_SUNKEN
;
300 // ComCtl32 notebook tabs simply don't work unless they're on top if we
301 // have uxtheme, we can work around it later (after control creation), but
302 // if we have been compiled without uxtheme support, we have to clear those
304 if ( HasTroubleWithNonTopTabs() )
306 style
&= ~(wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
);
308 #endif //wxUSE_UXTHEME
310 #if defined(__WINE__) && wxUSE_UNICODE
311 LPCTSTR className
= L
"SysTabControl32";
313 LPCTSTR className
= WC_TABCONTROL
;
316 #if USE_NOTEBOOK_ANTIFLICKER
317 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
318 // causes horrible flicker when resizing notebook, so get rid of it by
319 // using a class without these styles (but otherwise identical to it)
320 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
322 static ClassRegistrar s_clsNotebook
;
323 if ( !s_clsNotebook
.IsInitialized() )
325 // get a copy of standard class and modify it
328 if ( ::GetClassInfo(NULL
, WC_TABCONTROL
, &wc
) )
331 wx_reinterpret_cast(WXFARPROC
, wc
.lpfnWndProc
);
332 wc
.lpszClassName
= wxT("_wx_SysTabCtl32");
333 wc
.style
&= ~(CS_HREDRAW
| CS_VREDRAW
);
334 wc
.hInstance
= wxGetInstance();
335 wc
.lpfnWndProc
= wxNotebookWndProc
;
336 s_clsNotebook
.Register(wc
);
340 wxLogLastError(_T("GetClassInfoEx(SysTabCtl32)"));
344 // use our custom class if available but fall back to the standard
345 // notebook if we failed to register it
346 if ( s_clsNotebook
.IsRegistered() )
348 // it's ok to use c_str() here as the static s_clsNotebook object
349 // has sufficiently long lifetime
350 className
= s_clsNotebook
.GetName().c_str();
353 #endif // USE_NOTEBOOK_ANTIFLICKER
355 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
356 wxDefaultValidator
, name
) )
359 if ( !MSWCreateControl(className
, wxEmptyString
, pos
, size
) )
363 if ( HasFlag(wxNB_NOPAGETHEME
) ||
364 wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) )
366 SetBackgroundColour(GetThemeBackgroundColour());
368 else // use themed background by default
370 // create backing store
374 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
375 // control is simply not rendered correctly), so we disable themes
376 // if possible, otherwise we simply clear the styles.
377 if ( HasTroubleWithNonTopTabs() &&
378 (style
& (wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
)) )
380 // check if we use themes at all -- if we don't, we're still okay
381 if ( wxUxThemeEngine::GetIfActive() )
383 wxUxThemeEngine::GetIfActive()->SetWindowTheme(GetHwnd(), L
"", L
"");
385 // correct the background color for the new non-themed control
386 SetBackgroundColour(GetThemeBackgroundColour());
389 #endif // wxUSE_UXTHEME
391 // Undocumented hack to get flat notebook style
392 // In fact, we should probably only do this in some
393 // curcumstances, i.e. if we know we will have a border
394 // at the bottom (the tab control doesn't draw it itself)
395 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
396 if (HasFlag(wxNB_FLAT
))
398 SendMessage(GetHwnd(), CCM_SETVERSION
, COMCTL32_VERSION
, 0);
400 SetBackgroundColour(*wxWHITE
);
406 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
408 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
410 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
412 if ( style
& wxNB_MULTILINE
)
413 tabStyle
|= TCS_MULTILINE
;
414 if ( style
& wxNB_FIXEDWIDTH
)
415 tabStyle
|= TCS_FIXEDWIDTH
;
417 if ( style
& wxBK_BOTTOM
)
418 tabStyle
|= TCS_RIGHT
;
419 else if ( style
& wxBK_LEFT
)
420 tabStyle
|= TCS_VERTICAL
;
421 else if ( style
& wxBK_RIGHT
)
422 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
427 // note that we never want to have the default WS_EX_CLIENTEDGE style
428 // as it looks too ugly for the notebooks
435 wxNotebook::~wxNotebook()
438 if ( m_hbrBackground
)
439 ::DeleteObject((HBRUSH
)m_hbrBackground
);
440 #endif // wxUSE_UXTHEME
443 // ----------------------------------------------------------------------------
444 // wxNotebook accessors
445 // ----------------------------------------------------------------------------
447 size_t wxNotebook::GetPageCount() const
450 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(GetHwnd()) );
452 return m_pages
.Count();
455 int wxNotebook::GetRowCount() const
457 return TabCtrl_GetRowCount(GetHwnd());
460 int wxNotebook::SetSelection(size_t nPage
)
462 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
464 if ( m_nSelection
== wxNOT_FOUND
|| nPage
!= (size_t)m_nSelection
)
466 if ( SendPageChangingEvent(nPage
) )
468 // program allows the page change
469 SendPageChangedEvent(m_nSelection
, nPage
);
471 TabCtrl_SetCurSel(GetHwnd(), nPage
);
478 void wxNotebook::UpdateSelection(int selNew
)
480 if ( m_nSelection
!= wxNOT_FOUND
)
481 m_pages
[m_nSelection
]->Show(false);
483 if ( selNew
!= wxNOT_FOUND
)
485 wxNotebookPage
*pPage
= m_pages
[selNew
];
489 // Changing the page should give the focus to it but, as per bug report
490 // http://sf.net/tracker/index.php?func=detail&aid=1150659&group_id=9863&atid=109863,
491 // we should not set the focus to it directly since it erroneously
492 // selects radio buttons and breaks keyboard handling for a notebook's
493 // scroll buttons. So give focus to the notebook and not the page.
495 // but don't do this is the notebook is hidden
496 if ( ::IsWindowVisible(GetHwnd()) )
499 m_nSelection
= selNew
;
502 int wxNotebook::ChangeSelection(size_t nPage
)
504 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
506 if ( m_nSelection
== wxNOT_FOUND
|| nPage
!= (size_t)m_nSelection
)
508 TabCtrl_SetCurSel(GetHwnd(), nPage
);
510 UpdateSelection(nPage
);
516 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
518 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
521 tcItem
.mask
= TCIF_TEXT
;
522 tcItem
.pszText
= (wxChar
*)strText
.wx_str();
524 if ( !HasFlag(wxNB_MULTILINE
) )
525 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
527 // multiline - we need to set new page size if a line is added or removed
528 int rows
= GetRowCount();
529 bool ret
= TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
531 if ( ret
&& rows
!= GetRowCount() )
533 const wxRect r
= GetPageSize();
534 const size_t count
= m_pages
.Count();
535 for ( size_t page
= 0; page
< count
; page
++ )
536 m_pages
[page
]->SetSize(r
);
542 wxString
wxNotebook::GetPageText(size_t nPage
) const
544 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
548 tcItem
.mask
= TCIF_TEXT
;
549 tcItem
.pszText
= buf
;
550 tcItem
.cchTextMax
= WXSIZEOF(buf
);
553 if ( TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) )
554 str
= tcItem
.pszText
;
559 int wxNotebook::GetPageImage(size_t nPage
) const
561 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
564 tcItem
.mask
= TCIF_IMAGE
;
566 return TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) ? tcItem
.iImage
570 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
572 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
575 tcItem
.mask
= TCIF_IMAGE
;
576 tcItem
.iImage
= nImage
;
578 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
581 void wxNotebook::SetImageList(wxImageList
* imageList
)
583 wxNotebookBase::SetImageList(imageList
);
587 (void) TabCtrl_SetImageList(GetHwnd(), GetHimagelistOf(imageList
));
591 // ----------------------------------------------------------------------------
592 // wxNotebook size settings
593 // ----------------------------------------------------------------------------
595 wxRect
wxNotebook::GetPageSize() const
600 ::GetClientRect(GetHwnd(), &rc
);
602 // This check is to work around a bug in TabCtrl_AdjustRect which will
603 // cause a crash on win2k or on XP with themes disabled if either
604 // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle
607 // The value of 20 is chosen arbitrarily but seems to work
608 if ( rc
.right
> 20 && rc
.bottom
> 20 )
610 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
612 wxCopyRECTToRect(rc
, r
);
618 void wxNotebook::SetPageSize(const wxSize
& size
)
620 // transform the page size into the notebook size
627 TabCtrl_AdjustRect(GetHwnd(), true, &rc
);
630 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
633 void wxNotebook::SetPadding(const wxSize
& padding
)
635 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
638 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
640 void wxNotebook::SetTabSize(const wxSize
& sz
)
642 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
645 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
647 // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP
648 wxSize sizeTotal
= sizePage
;
651 if ( GetPageCount() > 0 )
654 TabCtrl_GetItemRect(GetHwnd(), 0, &rect
);
655 tabSize
.x
= rect
.right
- rect
.left
;
656 tabSize
.y
= rect
.bottom
- rect
.top
;
659 // add an extra margin in both directions
660 const int MARGIN
= 8;
663 sizeTotal
.x
+= MARGIN
;
664 sizeTotal
.y
+= tabSize
.y
+ MARGIN
;
666 else // horizontal layout
668 sizeTotal
.x
+= tabSize
.x
+ MARGIN
;
669 sizeTotal
.y
+= MARGIN
;
675 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
677 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
679 const wxRect r
= GetPageSize();
686 // ----------------------------------------------------------------------------
687 // wxNotebook operations
688 // ----------------------------------------------------------------------------
690 // remove one page from the notebook, without deleting
691 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
693 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
697 TabCtrl_DeleteItem(GetHwnd(), nPage
);
699 if ( m_pages
.IsEmpty() )
701 // no selection any more, the notebook becamse empty
702 m_nSelection
= wxNOT_FOUND
;
704 else // notebook still not empty
706 int selNew
= TabCtrl_GetCurSel(GetHwnd());
707 if ( selNew
!= wxNOT_FOUND
)
709 // No selection change, just refresh the current selection.
710 // Because it could be that the slection index changed
711 // we need to update it.
712 // Note: this does not mean the selection it self changed.
713 m_nSelection
= selNew
;
714 m_pages
[m_nSelection
]->Refresh();
716 else if (int(nPage
) == m_nSelection
)
718 // The selection was deleted.
720 // Determine new selection.
721 if (m_nSelection
== int(GetPageCount()))
722 selNew
= m_nSelection
- 1;
724 selNew
= m_nSelection
;
726 // m_nSelection must be always valid so reset it before calling
728 m_nSelection
= wxNOT_FOUND
;
729 SetSelection(selNew
);
733 wxFAIL
; // Windows did not behave ok.
741 bool wxNotebook::DeleteAllPages()
743 size_t nPageCount
= GetPageCount();
745 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
746 delete m_pages
[nPage
];
750 TabCtrl_DeleteAllItems(GetHwnd());
752 m_nSelection
= wxNOT_FOUND
;
754 InvalidateBestSize();
758 // same as AddPage() but does it at given position
759 bool wxNotebook::InsertPage(size_t nPage
,
760 wxNotebookPage
*pPage
,
761 const wxString
& strText
,
765 wxCHECK_MSG( pPage
!= NULL
, false, _T("NULL page in wxNotebook::InsertPage") );
766 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false,
767 _T("invalid index in wxNotebook::InsertPage") );
769 wxASSERT_MSG( pPage
->GetParent() == this,
770 _T("notebook pages must have notebook as parent") );
772 // add a new tab to the control
773 // ----------------------------
775 // init all fields to 0
777 wxZeroMemory(tcItem
);
779 // set the image, if any
782 tcItem
.mask
|= TCIF_IMAGE
;
783 tcItem
.iImage
= imageId
;
787 if ( !strText
.empty() )
789 tcItem
.mask
|= TCIF_TEXT
;
790 tcItem
.pszText
= (wxChar
*)strText
.wx_str(); // const_cast
793 // hide the page: unless it is selected, it shouldn't be shown (and if it
794 // is selected it will be shown later)
795 HWND hwnd
= GetWinHwnd(pPage
);
796 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
798 // this updates internal flag too -- otherwise it would get out of sync
799 // with the real state
803 // fit the notebook page to the tab control's display area: this should be
804 // done before adding it to the notebook or TabCtrl_InsertItem() will
805 // change the notebooks size itself!
806 AdjustPageSize(pPage
);
808 // finally do insert it
809 if ( TabCtrl_InsertItem(GetHwnd(), nPage
, &tcItem
) == -1 )
811 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
816 // need to update the bg brush when the first page is added
817 // so the first panel gets the correct themed background
818 if ( m_pages
.empty() )
822 #endif // wxUSE_UXTHEME
825 // succeeded: save the pointer to the page
826 m_pages
.Insert(pPage
, nPage
);
828 // we may need to adjust the size again if the notebook size changed:
829 // normally this only happens for the first page we add (the tabs which
830 // hadn't been there before are now shown) but for a multiline notebook it
831 // can happen for any page at all as a new row could have been started
832 if ( m_pages
.GetCount() == 1 || HasFlag(wxNB_MULTILINE
) )
834 AdjustPageSize(pPage
);
837 // now deal with the selection
838 // ---------------------------
840 // if the inserted page is before the selected one, we must update the
841 // index of the selected page
842 if ( int(nPage
) <= m_nSelection
)
844 // one extra page added
848 // some page should be selected: either this one or the first one if there
849 // is still no selection
850 int selNew
= wxNOT_FOUND
;
853 else if ( m_nSelection
== wxNOT_FOUND
)
856 if ( selNew
!= wxNOT_FOUND
)
857 SetSelection(selNew
);
859 InvalidateBestSize();
864 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
866 TC_HITTESTINFO hitTestInfo
;
867 hitTestInfo
.pt
.x
= pt
.x
;
868 hitTestInfo
.pt
.y
= pt
.y
;
869 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
875 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
876 *flags
|= wxBK_HITTEST_NOWHERE
;
877 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
878 *flags
|= wxBK_HITTEST_ONITEM
;
879 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
880 *flags
|= wxBK_HITTEST_ONICON
;
881 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
882 *flags
|= wxBK_HITTEST_ONLABEL
;
883 if ( item
== wxNOT_FOUND
&& GetPageSize().Contains(pt
) )
884 *flags
|= wxBK_HITTEST_ONPAGE
;
890 // ----------------------------------------------------------------------------
891 // flicker-less notebook redraw
892 // ----------------------------------------------------------------------------
894 #if USE_NOTEBOOK_ANTIFLICKER
896 // wnd proc for the spin button
897 LRESULT APIENTRY _EXPORT
wxNotebookSpinBtnWndProc(HWND hwnd
,
902 if ( message
== WM_ERASEBKGND
)
905 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn
,
906 hwnd
, message
, wParam
, lParam
);
909 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
914 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook
,
915 hwnd
, message
, wParam
, lParam
);
918 void wxNotebook::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
923 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
))
928 ::GetClientRect(GetHwnd(), &rc
);
929 wxBitmap
bmp(rc
.right
, rc
.bottom
);
930 memdc
.SelectObject(bmp
);
932 const wxLayoutDirection dir
= dc
.GetLayoutDirection();
933 memdc
.SetLayoutDirection(dir
);
935 // if there is no special brush just use the solid background colour
937 HBRUSH hbr
= (HBRUSH
)m_hbrBackground
;
944 brush
= wxBrush(GetBackgroundColour());
945 hbr
= GetHbrushOf(brush
);
948 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) memdc
.GetImpl();
950 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
952 MSWDefWindowProc(WM_PAINT
, (WPARAM
)(impl
->GetHDC()), 0);
954 // For some reason in RTL mode, source offset has to be -1, otherwise the
955 // right border (physical) remains unpainted.
956 const wxCoord ofs
= dir
== wxLayout_RightToLeft
? -1 : 0;
957 dc
.Blit(ofs
, 0, rc
.right
, rc
.bottom
, &memdc
, ofs
, 0);
960 #endif // USE_NOTEBOOK_ANTIFLICKER
962 // ----------------------------------------------------------------------------
963 // wxNotebook callbacks
964 // ----------------------------------------------------------------------------
966 void wxNotebook::OnSize(wxSizeEvent
& event
)
968 if ( GetPageCount() == 0 )
970 // Prevents droppings on resize, but does cause some flicker
971 // when there are no pages.
979 // Without this, we can sometimes get droppings at the edges
980 // of a notebook, for example a notebook in a splitter window.
981 // This needs to be reconciled with the RefreshRect calls
982 // at the end of this function, which weren't enough to prevent
985 wxSize sz
= GetClientSize();
987 // Refresh right side
988 wxRect
rect(sz
.x
-4, 0, 4, sz
.y
);
991 // Refresh bottom side
992 rect
= wxRect(0, sz
.y
-4, sz
.x
, 4);
996 rect
= wxRect(0, 0, 4, sz
.y
);
999 #endif // !__WXWINCE__
1001 // fit all the notebook pages to the tab control's display area
1004 rc
.left
= rc
.top
= 0;
1005 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
1007 // save the total size, we'll use it below
1008 int widthNbook
= rc
.right
- rc
.left
,
1009 heightNbook
= rc
.bottom
- rc
.top
;
1011 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
1012 // returns completely false values for multiline tab controls after the tabs
1013 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
1015 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
1017 // and the only work around I could find was this ugly hack... without it
1018 // simply toggling the "multiline" checkbox in the notebook sample resulted
1019 // in a noticeable page displacement
1020 if ( HasFlag(wxNB_MULTILINE
) )
1022 // avoid an infinite recursion: we get another notification too!
1023 static bool s_isInOnSize
= false;
1025 if ( !s_isInOnSize
)
1027 s_isInOnSize
= true;
1028 SendMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
,
1029 MAKELPARAM(rc
.right
, rc
.bottom
));
1030 s_isInOnSize
= false;
1035 // background bitmap size has changed, update the brush using it too
1037 #endif // wxUSE_UXTHEME
1039 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
1041 int width
= rc
.right
- rc
.left
,
1042 height
= rc
.bottom
- rc
.top
;
1043 size_t nCount
= m_pages
.Count();
1044 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
1045 wxNotebookPage
*pPage
= m_pages
[nPage
];
1046 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
1050 // unless we had already repainted everything, we now need to refresh
1051 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
1053 // invalidate areas not covered by pages
1054 RefreshRect(wxRect(0, 0, widthNbook
, rc
.top
), false);
1055 RefreshRect(wxRect(0, rc
.top
, rc
.left
, height
), false);
1056 RefreshRect(wxRect(0, rc
.bottom
, widthNbook
, heightNbook
- rc
.bottom
),
1058 RefreshRect(wxRect(rc
.right
, rc
.top
, widthNbook
- rc
.right
, height
),
1062 #if USE_NOTEBOOK_ANTIFLICKER
1063 // subclass the spin control used by the notebook to scroll pages to
1064 // prevent it from flickering on resize
1065 if ( !m_hasSubclassedUpdown
)
1067 // iterate over all child windows to find spin button
1068 for ( HWND child
= ::GetWindow(GetHwnd(), GW_CHILD
);
1070 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
1072 wxWindow
*childWindow
= wxFindWinFromHandle((WXHWND
)child
);
1074 // see if it exists, if no wxWindow found then assume it's the spin
1078 // subclass the spin button to override WM_ERASEBKGND
1079 if ( !gs_wndprocNotebookSpinBtn
)
1080 gs_wndprocNotebookSpinBtn
= (WXFARPROC
)wxGetWindowProc(child
);
1082 wxSetWindowProc(child
, wxNotebookSpinBtnWndProc
);
1083 m_hasSubclassedUpdown
= true;
1088 #endif // USE_NOTEBOOK_ANTIFLICKER
1093 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
1095 // is it our tab control?
1096 if ( event
.GetEventObject() == this )
1098 UpdateSelection(event
.GetSelection());
1101 // we want to give others a chance to process this message as well
1105 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
1107 if ( event
.IsWindowChange() ) {
1109 AdvanceSelection(event
.GetDirection());
1112 // we get this event in 3 cases
1114 // a) one of our pages might have generated it because the user TABbed
1115 // out from it in which case we should propagate the event upwards and
1116 // our parent will take care of setting the focus to prev/next sibling
1120 // b) the parent panel wants to give the focus to us so that we
1121 // forward it to our selected page. We can't deal with this in
1122 // OnSetFocus() because we don't know which direction the focus came
1123 // from in this case and so can't choose between setting the focus to
1124 // first or last panel child
1128 // c) we ourselves (see MSWTranslateMessage) generated the event
1130 wxWindow
* const parent
= GetParent();
1132 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
1133 const bool isFromParent
= event
.GetEventObject() == (wxObject
*) parent
;
1134 const bool isFromSelf
= event
.GetEventObject() == (wxObject
*) this;
1136 if ( isFromParent
|| isFromSelf
)
1138 // no, it doesn't come from child, case (b) or (c): forward to a
1139 // page but only if direction is backwards (TAB) or from ourselves,
1140 if ( m_nSelection
!= wxNOT_FOUND
&&
1141 (!event
.GetDirection() || isFromSelf
) )
1143 // so that the page knows that the event comes from it's parent
1144 // and is being propagated downwards
1145 event
.SetEventObject(this);
1147 wxWindow
*page
= m_pages
[m_nSelection
];
1148 if ( !page
->HandleWindowEvent(event
) )
1152 //else: page manages focus inside it itself
1154 else // otherwise set the focus to the notebook itself
1161 // it comes from our child, case (a), pass to the parent, but only
1162 // if the direction is forwards. Otherwise set the focus to the
1163 // notebook itself. The notebook is always the 'first' control of a
1165 if ( !event
.GetDirection() )
1171 event
.SetCurrentFocus(this);
1172 parent
->HandleWindowEvent(event
);
1180 bool wxNotebook::DoDrawBackground(WXHDC hDC
, wxWindow
*child
)
1182 wxUxThemeHandle
theme(child
? child
: this, L
"TAB");
1186 // get the notebook client rect (we're not interested in drawing tabs
1188 wxRect r
= GetPageSize();
1193 wxCopyRectToRECT(r
, rc
);
1195 // map rect to the coords of the window we're drawing in
1197 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1199 // we have the content area (page size), but we need to draw all of the
1200 // background for it to be aligned correctly
1201 wxUxThemeEngine::Get()->GetThemeBackgroundExtent
1210 wxUxThemeEngine::Get()->DrawThemeBackground
1223 WXHBRUSH
wxNotebook::QueryBgBitmap()
1225 wxRect r
= GetPageSize();
1229 WindowHDC
hDC(GetHwnd());
1230 MemoryHDC
hDCMem(hDC
);
1231 CompatibleBitmap
hBmp(hDC
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1233 SelectInHDC
selectBmp(hDCMem
, hBmp
);
1235 if ( !DoDrawBackground((WXHDC
)(HDC
)hDCMem
) )
1238 return (WXHBRUSH
)::CreatePatternBrush(hBmp
);
1241 void wxNotebook::UpdateBgBrush()
1243 if ( m_hbrBackground
)
1244 ::DeleteObject((HBRUSH
)m_hbrBackground
);
1246 if ( !m_hasBgCol
&& wxUxThemeEngine::GetIfActive() )
1248 m_hbrBackground
= QueryBgBitmap();
1250 else // no themes or we've got user-defined solid colour
1252 m_hbrBackground
= NULL
;
1256 WXHBRUSH
wxNotebook::MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
)
1258 if ( m_hbrBackground
)
1260 // before drawing with the background brush, we need to position it
1263 ::GetWindowRect((HWND
)hWnd
, &rc
);
1265 ::MapWindowPoints(NULL
, GetHwnd(), (POINT
*)&rc
, 1);
1267 if ( !::SetBrushOrgEx((HDC
)hDC
, -rc
.left
, -rc
.top
, NULL
) )
1269 wxLogLastError(_T("SetBrushOrgEx(notebook bg brush)"));
1272 return m_hbrBackground
;
1275 return wxNotebookBase::MSWGetBgBrushForChild(hDC
, hWnd
);
1278 bool wxNotebook::MSWPrintChild(WXHDC hDC
, wxWindow
*child
)
1280 // solid background colour overrides themed background drawing
1281 if ( !UseBgCol() && DoDrawBackground(hDC
, child
) )
1284 // If we're using a solid colour (for example if we've switched off
1285 // theming for this notebook), paint it
1288 wxRect r
= GetPageSize();
1293 wxCopyRectToRECT(r
, rc
);
1295 // map rect to the coords of the window we're drawing in
1297 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1299 wxBrush
brush(GetBackgroundColour());
1300 HBRUSH hbr
= GetHbrushOf(brush
);
1302 ::FillRect((HDC
) hDC
, &rc
, hbr
);
1307 return wxNotebookBase::MSWPrintChild(hDC
, child
);
1310 #endif // wxUSE_UXTHEME
1312 // Windows only: attempts to get colour for UX theme page background
1313 wxColour
wxNotebook::GetThemeBackgroundColour() const
1316 if (wxUxThemeEngine::Get())
1318 wxUxThemeHandle
hTheme((wxNotebook
*) this, L
"TAB");
1321 // This is total guesswork.
1322 // See PlatformSDK\Include\Tmschema.h for values.
1323 // JACS: can also use 9 (TABP_PANE)
1324 COLORREF themeColor
;
1325 bool success
= (S_OK
== wxUxThemeEngine::Get()->GetThemeColor(
1329 3821 /* FILLCOLORHINT */,
1332 return GetBackgroundColour();
1335 [DS] Workaround for WindowBlinds:
1336 Some themes return a near black theme color using FILLCOLORHINT,
1337 this makes notebook pages have an ugly black background and makes
1338 text (usually black) unreadable. Retry again with FILLCOLOR.
1340 This workaround potentially breaks appearance of some themes,
1341 but in practice it already fixes some themes.
1343 if (themeColor
== 1)
1345 wxUxThemeEngine::Get()->GetThemeColor(
1349 3802 /* FILLCOLOR */,
1353 wxColour colour
= wxRGBToColour(themeColor
);
1355 // Under Vista, the tab background colour is reported incorrectly.
1356 // So for the default theme at least, hard-code the colour to something
1357 // that will blend in.
1359 static int s_AeroStatus
= -1;
1360 if (s_AeroStatus
== -1)
1362 WCHAR szwThemeFile
[1024];
1363 WCHAR szwThemeColor
[256];
1364 if (S_OK
== wxUxThemeEngine::Get()->GetCurrentThemeName(szwThemeFile
, 1024, szwThemeColor
, 256, NULL
, 0))
1366 wxString
themeFile(szwThemeFile
), themeColor(szwThemeColor
);
1367 if (themeFile
.Find(wxT("Aero")) != -1 && themeColor
== wxT("NormalColor"))
1376 if (s_AeroStatus
== 1)
1377 colour
= wxColour(255, 255, 255);
1382 #endif // wxUSE_UXTHEME
1384 return GetBackgroundColour();
1387 // ----------------------------------------------------------------------------
1388 // wxNotebook base class virtuals
1389 // ----------------------------------------------------------------------------
1391 #if wxUSE_CONSTRAINTS
1393 // override these 2 functions to do nothing: everything is done in OnSize
1395 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
1397 // don't set the sizes of the pages - their correct size is not yet known
1398 wxControl::SetConstraintSizes(false);
1401 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
1406 #endif // wxUSE_CONSTRAINTS
1408 // ----------------------------------------------------------------------------
1409 // wxNotebook Windows message handlers
1410 // ----------------------------------------------------------------------------
1412 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
1413 WXWORD pos
, WXHWND control
)
1415 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1420 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
1423 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
1425 wxBookCtrlEvent
event(wxEVT_NULL
, m_windowId
);
1427 NMHDR
* hdr
= (NMHDR
*)lParam
;
1428 switch ( hdr
->code
) {
1430 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
1433 case TCN_SELCHANGING
:
1434 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
1438 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1441 event
.SetSelection(TabCtrl_GetCurSel(GetHwnd()));
1442 event
.SetOldSelection(m_nSelection
);
1443 event
.SetEventObject(this);
1444 event
.SetInt(idCtrl
);
1446 bool processed
= HandleWindowEvent(event
);
1447 *result
= !event
.IsAllowed();
1451 #endif // wxUSE_NOTEBOOK