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 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
124 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
125 EVT_SIZE(wxNotebook::OnSize
)
126 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
128 #if USE_NOTEBOOK_ANTIFLICKER
129 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
)
130 EVT_PAINT(wxNotebook::OnPaint
)
131 #endif // USE_NOTEBOOK_ANTIFLICKER
134 #if wxUSE_EXTENDED_RTTI
135 WX_DEFINE_FLAGS( wxNotebookStyle
)
137 wxBEGIN_FLAGS( wxNotebookStyle
)
138 // new style border flags, we put them first to
139 // use them for streaming out
140 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
141 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
142 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
143 wxFLAGS_MEMBER(wxBORDER_RAISED
)
144 wxFLAGS_MEMBER(wxBORDER_STATIC
)
145 wxFLAGS_MEMBER(wxBORDER_NONE
)
147 // old style border flags
148 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
149 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
150 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
151 wxFLAGS_MEMBER(wxRAISED_BORDER
)
152 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
153 wxFLAGS_MEMBER(wxBORDER
)
155 // standard window styles
156 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
157 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
158 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
159 wxFLAGS_MEMBER(wxWANTS_CHARS
)
160 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
161 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
162 wxFLAGS_MEMBER(wxVSCROLL
)
163 wxFLAGS_MEMBER(wxHSCROLL
)
165 wxFLAGS_MEMBER(wxNB_FIXEDWIDTH
)
166 wxFLAGS_MEMBER(wxBK_DEFAULT
)
167 wxFLAGS_MEMBER(wxBK_TOP
)
168 wxFLAGS_MEMBER(wxBK_LEFT
)
169 wxFLAGS_MEMBER(wxBK_RIGHT
)
170 wxFLAGS_MEMBER(wxBK_BOTTOM
)
171 wxFLAGS_MEMBER(wxNB_NOPAGETHEME
)
172 wxFLAGS_MEMBER(wxNB_FLAT
)
174 wxEND_FLAGS( wxNotebookStyle
)
176 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook
, wxBookCtrlBase
,"wx/notebook.h")
177 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo
, wxObject
, "wx/notebook.h" )
179 wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo
* , wxNotebookPageInfoList
) ;
181 template<> void wxCollectionToVariantArray( wxNotebookPageInfoList
const &theList
, wxxVariantArray
&value
)
183 wxListCollectionToVariantArray
<wxNotebookPageInfoList::compatibility_iterator
>( theList
, value
) ;
186 wxBEGIN_PROPERTIES_TABLE(wxNotebook
)
187 wxEVENT_PROPERTY( PageChanging
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, wxBookCtrlEvent
)
188 wxEVENT_PROPERTY( PageChanged
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, wxBookCtrlEvent
)
190 wxPROPERTY_COLLECTION( PageInfos
, wxNotebookPageInfoList
, wxNotebookPageInfo
* , AddPageInfo
, GetPageInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
191 wxPROPERTY_FLAGS( WindowStyle
, wxNotebookStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
192 wxEND_PROPERTIES_TABLE()
194 wxBEGIN_HANDLERS_TABLE(wxNotebook
)
195 wxEND_HANDLERS_TABLE()
197 wxCONSTRUCTOR_5( wxNotebook
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
200 wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo
)
201 wxREADONLY_PROPERTY( Page
, wxNotebookPage
* , GetPage
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
202 wxREADONLY_PROPERTY( Text
, wxString
, GetText
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
203 wxREADONLY_PROPERTY( Selected
, bool , GetSelected
, false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
204 wxREADONLY_PROPERTY( ImageId
, int , GetImageId
, -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
205 wxEND_PROPERTIES_TABLE()
207 wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo
)
208 wxEND_HANDLERS_TABLE()
210 wxCONSTRUCTOR_4( wxNotebookPageInfo
, wxNotebookPage
* , Page
, wxString
, Text
, bool , Selected
, int , ImageId
)
213 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
214 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo
, wxObject
)
217 // ============================================================================
219 // ============================================================================
221 // ----------------------------------------------------------------------------
222 // wxNotebook construction
223 // ----------------------------------------------------------------------------
225 const wxNotebookPageInfoList
& wxNotebook::GetPageInfos() const
227 wxNotebookPageInfoList
* list
= const_cast< wxNotebookPageInfoList
* >( &m_pageInfos
) ;
228 WX_CLEAR_LIST( wxNotebookPageInfoList
, *list
) ;
229 for( size_t i
= 0 ; i
< GetPageCount() ; ++i
)
231 wxNotebookPageInfo
*info
= new wxNotebookPageInfo() ;
232 info
->Create( const_cast<wxNotebook
*>(this)->GetPage(i
) , GetPageText(i
) , GetSelection() == int(i
) , GetPageImage(i
) ) ;
233 list
->Append( info
) ;
238 // common part of all ctors
239 void wxNotebook::Init()
242 m_nSelection
= wxNOT_FOUND
;
245 m_hbrBackground
= NULL
;
246 #endif // wxUSE_UXTHEME
248 #if USE_NOTEBOOK_ANTIFLICKER
249 m_hasSubclassedUpdown
= false;
250 #endif // USE_NOTEBOOK_ANTIFLICKER
253 // default for dynamic class
254 wxNotebook::wxNotebook()
259 // the same arguments as for wxControl
260 wxNotebook::wxNotebook(wxWindow
*parent
,
265 const wxString
& name
)
269 Create(parent
, id
, pos
, size
, style
, name
);
273 bool wxNotebook::Create(wxWindow
*parent
,
278 const wxString
& name
)
280 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
282 #if defined(__POCKETPC__)
283 style
|= wxBK_BOTTOM
| wxNB_FLAT
;
290 // Not sure why, but without this style, there is no border
291 // around the notebook tabs.
292 if (style
& wxNB_FLAT
)
293 style
|= wxBORDER_SUNKEN
;
297 // ComCtl32 notebook tabs simply don't work unless they're on top if we
298 // have uxtheme, we can work around it later (after control creation), but
299 // if we have been compiled without uxtheme support, we have to clear those
301 if ( HasTroubleWithNonTopTabs() )
303 style
&= ~(wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
);
305 #endif //wxUSE_UXTHEME
307 #if defined(__WINE__) && wxUSE_UNICODE
308 LPCTSTR className
= L
"SysTabControl32";
310 LPCTSTR className
= WC_TABCONTROL
;
313 #if USE_NOTEBOOK_ANTIFLICKER
314 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
315 // causes horrible flicker when resizing notebook, so get rid of it by
316 // using a class without these styles (but otherwise identical to it)
317 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
319 static ClassRegistrar s_clsNotebook
;
320 if ( !s_clsNotebook
.IsInitialized() )
322 // get a copy of standard class and modify it
325 if ( ::GetClassInfo(NULL
, WC_TABCONTROL
, &wc
) )
328 reinterpret_cast<WXFARPROC
>(wc
.lpfnWndProc
);
329 wc
.lpszClassName
= wxT("_wx_SysTabCtl32");
330 wc
.style
&= ~(CS_HREDRAW
| CS_VREDRAW
);
331 wc
.hInstance
= wxGetInstance();
332 wc
.lpfnWndProc
= wxNotebookWndProc
;
333 s_clsNotebook
.Register(wc
);
337 wxLogLastError(wxT("GetClassInfoEx(SysTabCtl32)"));
341 // use our custom class if available but fall back to the standard
342 // notebook if we failed to register it
343 if ( s_clsNotebook
.IsRegistered() )
345 // it's ok to use c_str() here as the static s_clsNotebook object
346 // has sufficiently long lifetime
347 className
= s_clsNotebook
.GetName().c_str();
350 #endif // USE_NOTEBOOK_ANTIFLICKER
352 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
353 wxDefaultValidator
, name
) )
356 if ( !MSWCreateControl(className
, wxEmptyString
, pos
, size
) )
360 if ( HasFlag(wxNB_NOPAGETHEME
) ||
361 wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) )
363 SetBackgroundColour(GetThemeBackgroundColour());
365 else // use themed background by default
367 // create backing store
371 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
372 // control is simply not rendered correctly), so we disable themes
373 // if possible, otherwise we simply clear the styles.
374 if ( HasTroubleWithNonTopTabs() &&
375 (style
& (wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
)) )
377 // check if we use themes at all -- if we don't, we're still okay
378 if ( wxUxThemeEngine::GetIfActive() )
380 wxUxThemeEngine::GetIfActive()->SetWindowTheme(GetHwnd(), L
"", L
"");
382 // correct the background color for the new non-themed control
383 SetBackgroundColour(GetThemeBackgroundColour());
386 #endif // wxUSE_UXTHEME
388 // Undocumented hack to get flat notebook style
389 // In fact, we should probably only do this in some
390 // curcumstances, i.e. if we know we will have a border
391 // at the bottom (the tab control doesn't draw it itself)
392 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
393 if (HasFlag(wxNB_FLAT
))
395 SendMessage(GetHwnd(), CCM_SETVERSION
, COMCTL32_VERSION
, 0);
397 SetBackgroundColour(*wxWHITE
);
403 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
405 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
407 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
409 if ( style
& wxNB_MULTILINE
)
410 tabStyle
|= TCS_MULTILINE
;
411 if ( style
& wxNB_FIXEDWIDTH
)
412 tabStyle
|= TCS_FIXEDWIDTH
;
414 if ( style
& wxBK_BOTTOM
)
415 tabStyle
|= TCS_RIGHT
;
416 else if ( style
& wxBK_LEFT
)
417 tabStyle
|= TCS_VERTICAL
;
418 else if ( style
& wxBK_RIGHT
)
419 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
424 wxNotebook::~wxNotebook()
427 if ( m_hbrBackground
)
428 ::DeleteObject((HBRUSH
)m_hbrBackground
);
429 #endif // wxUSE_UXTHEME
432 // ----------------------------------------------------------------------------
433 // wxNotebook accessors
434 // ----------------------------------------------------------------------------
436 size_t wxNotebook::GetPageCount() const
439 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(GetHwnd()) );
441 return m_pages
.Count();
444 int wxNotebook::GetRowCount() const
446 return TabCtrl_GetRowCount(GetHwnd());
449 int wxNotebook::SetSelection(size_t nPage
)
451 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
453 if ( m_nSelection
== wxNOT_FOUND
|| nPage
!= (size_t)m_nSelection
)
455 if ( SendPageChangingEvent(nPage
) )
457 // program allows the page change
458 SendPageChangedEvent(m_nSelection
, nPage
);
460 TabCtrl_SetCurSel(GetHwnd(), nPage
);
467 void wxNotebook::UpdateSelection(int selNew
)
469 if ( m_nSelection
!= wxNOT_FOUND
)
470 m_pages
[m_nSelection
]->Show(false);
472 if ( selNew
!= wxNOT_FOUND
)
474 wxNotebookPage
*pPage
= m_pages
[selNew
];
478 // Changing the page should give the focus to it but, as per bug report
479 // http://sf.net/tracker/index.php?func=detail&aid=1150659&group_id=9863&atid=109863,
480 // we should not set the focus to it directly since it erroneously
481 // selects radio buttons and breaks keyboard handling for a notebook's
482 // scroll buttons. So give focus to the notebook and not the page.
484 // but don't do this is the notebook is hidden
485 if ( ::IsWindowVisible(GetHwnd()) )
488 m_nSelection
= selNew
;
491 int wxNotebook::ChangeSelection(size_t nPage
)
493 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
495 const int selOld
= m_nSelection
;
497 if ( m_nSelection
== wxNOT_FOUND
|| nPage
!= (size_t)m_nSelection
)
499 TabCtrl_SetCurSel(GetHwnd(), nPage
);
501 UpdateSelection(nPage
);
507 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
509 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
512 tcItem
.mask
= TCIF_TEXT
;
513 tcItem
.pszText
= (wxChar
*)strText
.wx_str();
515 if ( !HasFlag(wxNB_MULTILINE
) )
516 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
518 // multiline - we need to set new page size if a line is added or removed
519 int rows
= GetRowCount();
520 bool ret
= TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
522 if ( ret
&& rows
!= GetRowCount() )
524 const wxRect r
= GetPageSize();
525 const size_t count
= m_pages
.Count();
526 for ( size_t page
= 0; page
< count
; page
++ )
527 m_pages
[page
]->SetSize(r
);
533 wxString
wxNotebook::GetPageText(size_t nPage
) const
535 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
539 tcItem
.mask
= TCIF_TEXT
;
540 tcItem
.pszText
= buf
;
541 tcItem
.cchTextMax
= WXSIZEOF(buf
);
544 if ( TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) )
545 str
= tcItem
.pszText
;
550 int wxNotebook::GetPageImage(size_t nPage
) const
552 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
555 tcItem
.mask
= TCIF_IMAGE
;
557 return TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) ? tcItem
.iImage
561 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
563 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
566 tcItem
.mask
= TCIF_IMAGE
;
567 tcItem
.iImage
= nImage
;
569 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
572 void wxNotebook::SetImageList(wxImageList
* imageList
)
574 wxNotebookBase::SetImageList(imageList
);
578 (void) TabCtrl_SetImageList(GetHwnd(), GetHimagelistOf(imageList
));
582 // ----------------------------------------------------------------------------
583 // wxNotebook size settings
584 // ----------------------------------------------------------------------------
586 wxRect
wxNotebook::GetPageSize() const
591 ::GetClientRect(GetHwnd(), &rc
);
593 // This check is to work around a bug in TabCtrl_AdjustRect which will
594 // cause a crash on win2k or on XP with themes disabled if either
595 // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle
598 // The value of 20 is chosen arbitrarily but seems to work
599 if ( rc
.right
> 20 && rc
.bottom
> 20 )
601 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
603 wxCopyRECTToRect(rc
, r
);
609 void wxNotebook::SetPageSize(const wxSize
& size
)
611 // transform the page size into the notebook size
618 TabCtrl_AdjustRect(GetHwnd(), true, &rc
);
621 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
624 void wxNotebook::SetPadding(const wxSize
& padding
)
626 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
629 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
631 void wxNotebook::SetTabSize(const wxSize
& sz
)
633 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
636 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
638 // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP
639 wxSize sizeTotal
= sizePage
;
642 if ( GetPageCount() > 0 )
645 TabCtrl_GetItemRect(GetHwnd(), 0, &rect
);
646 tabSize
.x
= rect
.right
- rect
.left
;
647 tabSize
.y
= rect
.bottom
- rect
.top
;
650 const int rows
= GetRowCount();
652 // add an extra margin in both directions
653 const int MARGIN
= 8;
656 sizeTotal
.x
+= MARGIN
;
657 sizeTotal
.y
+= tabSize
.y
* rows
+ MARGIN
;
659 else // horizontal layout
661 sizeTotal
.x
+= tabSize
.x
* rows
+ MARGIN
;
662 sizeTotal
.y
+= MARGIN
;
668 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
670 wxCHECK_RET( page
, wxT("NULL page in wxNotebook::AdjustPageSize") );
672 const wxRect r
= GetPageSize();
679 // ----------------------------------------------------------------------------
680 // wxNotebook operations
681 // ----------------------------------------------------------------------------
683 // remove one page from the notebook, without deleting
684 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
686 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
690 TabCtrl_DeleteItem(GetHwnd(), nPage
);
692 if ( m_pages
.IsEmpty() )
694 // no selection any more, the notebook becamse empty
695 m_nSelection
= wxNOT_FOUND
;
697 else // notebook still not empty
699 int selNew
= TabCtrl_GetCurSel(GetHwnd());
700 if ( selNew
!= wxNOT_FOUND
)
702 // No selection change, just refresh the current selection.
703 // Because it could be that the slection index changed
704 // we need to update it.
705 // Note: this does not mean the selection it self changed.
706 m_nSelection
= selNew
;
707 m_pages
[m_nSelection
]->Refresh();
709 else if (int(nPage
) == m_nSelection
)
711 // The selection was deleted.
713 // Determine new selection.
714 if (m_nSelection
== int(GetPageCount()))
715 selNew
= m_nSelection
- 1;
717 selNew
= m_nSelection
;
719 // m_nSelection must be always valid so reset it before calling
721 m_nSelection
= wxNOT_FOUND
;
722 SetSelection(selNew
);
726 wxFAIL
; // Windows did not behave ok.
734 bool wxNotebook::DeleteAllPages()
736 size_t nPageCount
= GetPageCount();
738 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
739 delete m_pages
[nPage
];
743 TabCtrl_DeleteAllItems(GetHwnd());
745 m_nSelection
= wxNOT_FOUND
;
747 InvalidateBestSize();
751 // same as AddPage() but does it at given position
752 bool wxNotebook::InsertPage(size_t nPage
,
753 wxNotebookPage
*pPage
,
754 const wxString
& strText
,
758 wxCHECK_MSG( pPage
!= NULL
, false, wxT("NULL page in wxNotebook::InsertPage") );
759 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false,
760 wxT("invalid index in wxNotebook::InsertPage") );
762 wxASSERT_MSG( pPage
->GetParent() == this,
763 wxT("notebook pages must have notebook as parent") );
765 // add a new tab to the control
766 // ----------------------------
768 // init all fields to 0
770 wxZeroMemory(tcItem
);
772 // set the image, if any
775 tcItem
.mask
|= TCIF_IMAGE
;
776 tcItem
.iImage
= imageId
;
780 if ( !strText
.empty() )
782 tcItem
.mask
|= TCIF_TEXT
;
783 tcItem
.pszText
= (wxChar
*)strText
.wx_str(); // const_cast
786 // hide the page: unless it is selected, it shouldn't be shown (and if it
787 // is selected it will be shown later)
788 HWND hwnd
= GetWinHwnd(pPage
);
789 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
791 // this updates internal flag too -- otherwise it would get out of sync
792 // with the real state
796 // fit the notebook page to the tab control's display area: this should be
797 // done before adding it to the notebook or TabCtrl_InsertItem() will
798 // change the notebooks size itself!
799 AdjustPageSize(pPage
);
801 // finally do insert it
802 if ( TabCtrl_InsertItem(GetHwnd(), nPage
, &tcItem
) == -1 )
804 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
809 // need to update the bg brush when the first page is added
810 // so the first panel gets the correct themed background
811 if ( m_pages
.empty() )
815 #endif // wxUSE_UXTHEME
818 // succeeded: save the pointer to the page
819 m_pages
.Insert(pPage
, nPage
);
821 // we may need to adjust the size again if the notebook size changed:
822 // normally this only happens for the first page we add (the tabs which
823 // hadn't been there before are now shown) but for a multiline notebook it
824 // can happen for any page at all as a new row could have been started
825 if ( m_pages
.GetCount() == 1 || HasFlag(wxNB_MULTILINE
) )
827 AdjustPageSize(pPage
);
830 // now deal with the selection
831 // ---------------------------
833 // if the inserted page is before the selected one, we must update the
834 // index of the selected page
835 if ( int(nPage
) <= m_nSelection
)
837 // one extra page added
841 // some page should be selected: either this one or the first one if there
842 // is still no selection
843 int selNew
= wxNOT_FOUND
;
846 else if ( m_nSelection
== wxNOT_FOUND
)
849 if ( selNew
!= wxNOT_FOUND
)
850 SetSelection(selNew
);
852 InvalidateBestSize();
857 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
859 TC_HITTESTINFO hitTestInfo
;
860 hitTestInfo
.pt
.x
= pt
.x
;
861 hitTestInfo
.pt
.y
= pt
.y
;
862 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
868 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
869 *flags
|= wxBK_HITTEST_NOWHERE
;
870 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
871 *flags
|= wxBK_HITTEST_ONITEM
;
872 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
873 *flags
|= wxBK_HITTEST_ONICON
;
874 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
875 *flags
|= wxBK_HITTEST_ONLABEL
;
876 if ( item
== wxNOT_FOUND
&& GetPageSize().Contains(pt
) )
877 *flags
|= wxBK_HITTEST_ONPAGE
;
883 // ----------------------------------------------------------------------------
884 // flicker-less notebook redraw
885 // ----------------------------------------------------------------------------
887 #if USE_NOTEBOOK_ANTIFLICKER
889 // wnd proc for the spin button
890 LRESULT APIENTRY _EXPORT
wxNotebookSpinBtnWndProc(HWND hwnd
,
895 if ( message
== WM_ERASEBKGND
)
898 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn
,
899 hwnd
, message
, wParam
, lParam
);
902 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
907 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook
,
908 hwnd
, message
, wParam
, lParam
);
911 void wxNotebook::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
916 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
))
921 ::GetClientRect(GetHwnd(), &rc
);
922 wxBitmap
bmp(rc
.right
, rc
.bottom
);
923 memdc
.SelectObject(bmp
);
925 const wxLayoutDirection dir
= dc
.GetLayoutDirection();
926 memdc
.SetLayoutDirection(dir
);
928 // if there is no special brush just use the solid background colour
930 HBRUSH hbr
= (HBRUSH
)m_hbrBackground
;
937 brush
= wxBrush(GetBackgroundColour());
938 hbr
= GetHbrushOf(brush
);
941 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) memdc
.GetImpl();
943 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
945 MSWDefWindowProc(WM_PAINT
, (WPARAM
)(impl
->GetHDC()), 0);
947 // For some reason in RTL mode, source offset has to be -1, otherwise the
948 // right border (physical) remains unpainted.
949 const wxCoord ofs
= dir
== wxLayout_RightToLeft
? -1 : 0;
950 dc
.Blit(ofs
, 0, rc
.right
, rc
.bottom
, &memdc
, ofs
, 0);
953 #endif // USE_NOTEBOOK_ANTIFLICKER
955 // ----------------------------------------------------------------------------
956 // wxNotebook callbacks
957 // ----------------------------------------------------------------------------
959 void wxNotebook::OnSize(wxSizeEvent
& event
)
961 if ( GetPageCount() == 0 )
963 // Prevents droppings on resize, but does cause some flicker
964 // when there are no pages.
972 // Without this, we can sometimes get droppings at the edges
973 // of a notebook, for example a notebook in a splitter window.
974 // This needs to be reconciled with the RefreshRect calls
975 // at the end of this function, which weren't enough to prevent
978 wxSize sz
= GetClientSize();
980 // Refresh right side
981 wxRect
rect(sz
.x
-4, 0, 4, sz
.y
);
984 // Refresh bottom side
985 rect
= wxRect(0, sz
.y
-4, sz
.x
, 4);
989 rect
= wxRect(0, 0, 4, sz
.y
);
992 #endif // !__WXWINCE__
994 // fit all the notebook pages to the tab control's display area
997 rc
.left
= rc
.top
= 0;
998 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
1000 // save the total size, we'll use it below
1001 int widthNbook
= rc
.right
- rc
.left
,
1002 heightNbook
= rc
.bottom
- rc
.top
;
1004 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
1005 // returns completely false values for multiline tab controls after the tabs
1006 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
1008 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
1010 // and the only work around I could find was this ugly hack... without it
1011 // simply toggling the "multiline" checkbox in the notebook sample resulted
1012 // in a noticeable page displacement
1013 if ( HasFlag(wxNB_MULTILINE
) )
1015 // avoid an infinite recursion: we get another notification too!
1016 static bool s_isInOnSize
= false;
1018 if ( !s_isInOnSize
)
1020 s_isInOnSize
= true;
1021 SendMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
,
1022 MAKELPARAM(rc
.right
, rc
.bottom
));
1023 s_isInOnSize
= false;
1026 // The best size depends on the number of rows of tabs, which can
1027 // change when the notepad is resized.
1028 InvalidateBestSize();
1032 // background bitmap size has changed, update the brush using it too
1034 #endif // wxUSE_UXTHEME
1036 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
1038 int width
= rc
.right
- rc
.left
,
1039 height
= rc
.bottom
- rc
.top
;
1040 size_t nCount
= m_pages
.Count();
1041 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
1042 wxNotebookPage
*pPage
= m_pages
[nPage
];
1043 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
1047 // unless we had already repainted everything, we now need to refresh
1048 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
1050 // invalidate areas not covered by pages
1051 RefreshRect(wxRect(0, 0, widthNbook
, rc
.top
), false);
1052 RefreshRect(wxRect(0, rc
.top
, rc
.left
, height
), false);
1053 RefreshRect(wxRect(0, rc
.bottom
, widthNbook
, heightNbook
- rc
.bottom
),
1055 RefreshRect(wxRect(rc
.right
, rc
.top
, widthNbook
- rc
.right
, height
),
1059 #if USE_NOTEBOOK_ANTIFLICKER
1060 // subclass the spin control used by the notebook to scroll pages to
1061 // prevent it from flickering on resize
1062 if ( !m_hasSubclassedUpdown
)
1064 // iterate over all child windows to find spin button
1065 for ( HWND child
= ::GetWindow(GetHwnd(), GW_CHILD
);
1067 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
1069 wxWindow
*childWindow
= wxFindWinFromHandle((WXHWND
)child
);
1071 // see if it exists, if no wxWindow found then assume it's the spin
1075 // subclass the spin button to override WM_ERASEBKGND
1076 if ( !gs_wndprocNotebookSpinBtn
)
1077 gs_wndprocNotebookSpinBtn
= (WXFARPROC
)wxGetWindowProc(child
);
1079 wxSetWindowProc(child
, wxNotebookSpinBtnWndProc
);
1080 m_hasSubclassedUpdown
= true;
1085 #endif // USE_NOTEBOOK_ANTIFLICKER
1090 void wxNotebook::OnSelChange(wxBookCtrlEvent
& event
)
1092 // is it our tab control?
1093 if ( event
.GetEventObject() == this )
1095 UpdateSelection(event
.GetSelection());
1098 // we want to give others a chance to process this message as well
1102 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
1104 if ( event
.IsWindowChange() ) {
1106 AdvanceSelection(event
.GetDirection());
1109 // we get this event in 3 cases
1111 // a) one of our pages might have generated it because the user TABbed
1112 // out from it in which case we should propagate the event upwards and
1113 // our parent will take care of setting the focus to prev/next sibling
1117 // b) the parent panel wants to give the focus to us so that we
1118 // forward it to our selected page. We can't deal with this in
1119 // OnSetFocus() because we don't know which direction the focus came
1120 // from in this case and so can't choose between setting the focus to
1121 // first or last panel child
1125 // c) we ourselves (see MSWTranslateMessage) generated the event
1127 wxWindow
* const parent
= GetParent();
1129 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
1130 const bool isFromParent
= event
.GetEventObject() == (wxObject
*) parent
;
1131 const bool isFromSelf
= event
.GetEventObject() == (wxObject
*) this;
1133 if ( isFromParent
|| isFromSelf
)
1135 // no, it doesn't come from child, case (b) or (c): forward to a
1136 // page but only if direction is backwards (TAB) or from ourselves,
1137 if ( m_nSelection
!= wxNOT_FOUND
&&
1138 (!event
.GetDirection() || isFromSelf
) )
1140 // so that the page knows that the event comes from it's parent
1141 // and is being propagated downwards
1142 event
.SetEventObject(this);
1144 wxWindow
*page
= m_pages
[m_nSelection
];
1145 if ( !page
->HandleWindowEvent(event
) )
1149 //else: page manages focus inside it itself
1151 else // otherwise set the focus to the notebook itself
1158 // it comes from our child, case (a), pass to the parent, but only
1159 // if the direction is forwards. Otherwise set the focus to the
1160 // notebook itself. The notebook is always the 'first' control of a
1162 if ( !event
.GetDirection() )
1168 event
.SetCurrentFocus(this);
1169 parent
->HandleWindowEvent(event
);
1177 bool wxNotebook::DoDrawBackground(WXHDC hDC
, wxWindow
*child
)
1179 wxUxThemeHandle
theme(child
? child
: this, L
"TAB");
1183 // get the notebook client rect (we're not interested in drawing tabs
1185 wxRect r
= GetPageSize();
1190 wxCopyRectToRECT(r
, rc
);
1192 // map rect to the coords of the window we're drawing in
1194 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1196 // we have the content area (page size), but we need to draw all of the
1197 // background for it to be aligned correctly
1198 wxUxThemeEngine::Get()->GetThemeBackgroundExtent
1207 wxUxThemeEngine::Get()->DrawThemeBackground
1220 WXHBRUSH
wxNotebook::QueryBgBitmap()
1222 wxRect r
= GetPageSize();
1226 WindowHDC
hDC(GetHwnd());
1227 MemoryHDC
hDCMem(hDC
);
1228 CompatibleBitmap
hBmp(hDC
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1230 SelectInHDC
selectBmp(hDCMem
, hBmp
);
1232 if ( !DoDrawBackground((WXHDC
)(HDC
)hDCMem
) )
1235 return (WXHBRUSH
)::CreatePatternBrush(hBmp
);
1238 void wxNotebook::UpdateBgBrush()
1240 if ( m_hbrBackground
)
1241 ::DeleteObject((HBRUSH
)m_hbrBackground
);
1243 if ( !m_hasBgCol
&& wxUxThemeEngine::GetIfActive() )
1245 m_hbrBackground
= QueryBgBitmap();
1247 else // no themes or we've got user-defined solid colour
1249 m_hbrBackground
= NULL
;
1253 WXHBRUSH
wxNotebook::MSWGetBgBrushForChild(WXHDC hDC
, wxWindow
*child
)
1255 // Only apply to notebook pages and transparent children, see
1256 // wxWindow::MSWGetBgBrushForChild() for explanation
1258 if ( child
->GetParent() == this )
1260 // notebook page -- apply background
1265 // controls in a notebook page with transparent background should
1267 shouldApply
= child
->HasTransparentBackground() &&
1268 child
->GetParent()->GetParent() == this;
1271 if ( m_hbrBackground
&& shouldApply
)
1273 // before drawing with the background brush, we need to position it
1276 ::GetWindowRect(GetHwndOf(child
), &rc
);
1278 ::MapWindowPoints(NULL
, GetHwnd(), (POINT
*)&rc
, 1);
1280 if ( !::SetBrushOrgEx((HDC
)hDC
, -rc
.left
, -rc
.top
, NULL
) )
1282 wxLogLastError(wxT("SetBrushOrgEx(notebook bg brush)"));
1285 return m_hbrBackground
;
1288 return wxNotebookBase::MSWGetBgBrushForChild(hDC
, child
);
1291 bool wxNotebook::MSWPrintChild(WXHDC hDC
, wxWindow
*child
)
1293 // solid background colour overrides themed background drawing
1294 if ( !UseBgCol() && DoDrawBackground(hDC
, child
) )
1297 // If we're using a solid colour (for example if we've switched off
1298 // theming for this notebook), paint it
1301 wxRect r
= GetPageSize();
1306 wxCopyRectToRECT(r
, rc
);
1308 // map rect to the coords of the window we're drawing in
1310 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1312 wxBrush
brush(GetBackgroundColour());
1313 HBRUSH hbr
= GetHbrushOf(brush
);
1315 ::FillRect((HDC
) hDC
, &rc
, hbr
);
1320 return wxNotebookBase::MSWPrintChild(hDC
, child
);
1323 #endif // wxUSE_UXTHEME
1325 // Windows only: attempts to get colour for UX theme page background
1326 wxColour
wxNotebook::GetThemeBackgroundColour() const
1329 if (wxUxThemeEngine::Get())
1331 wxUxThemeHandle
hTheme((wxNotebook
*) this, L
"TAB");
1334 // This is total guesswork.
1335 // See PlatformSDK\Include\Tmschema.h for values.
1336 // JACS: can also use 9 (TABP_PANE)
1337 COLORREF themeColor
;
1338 bool success
= (S_OK
== wxUxThemeEngine::Get()->GetThemeColor(
1342 3821 /* FILLCOLORHINT */,
1345 return GetBackgroundColour();
1348 [DS] Workaround for WindowBlinds:
1349 Some themes return a near black theme color using FILLCOLORHINT,
1350 this makes notebook pages have an ugly black background and makes
1351 text (usually black) unreadable. Retry again with FILLCOLOR.
1353 This workaround potentially breaks appearance of some themes,
1354 but in practice it already fixes some themes.
1356 if (themeColor
== 1)
1358 wxUxThemeEngine::Get()->GetThemeColor(
1362 3802 /* FILLCOLOR */,
1366 wxColour colour
= wxRGBToColour(themeColor
);
1368 // Under Vista, the tab background colour is reported incorrectly.
1369 // So for the default theme at least, hard-code the colour to something
1370 // that will blend in.
1372 static int s_AeroStatus
= -1;
1373 if (s_AeroStatus
== -1)
1375 WCHAR szwThemeFile
[1024];
1376 WCHAR szwThemeColor
[256];
1377 if (S_OK
== wxUxThemeEngine::Get()->GetCurrentThemeName(szwThemeFile
, 1024, szwThemeColor
, 256, NULL
, 0))
1379 wxString
themeFile(szwThemeFile
), themeColor(szwThemeColor
);
1380 if (themeFile
.Find(wxT("Aero")) != -1 && themeColor
== wxT("NormalColor"))
1389 if (s_AeroStatus
== 1)
1390 colour
= wxColour(255, 255, 255);
1395 #endif // wxUSE_UXTHEME
1397 return GetBackgroundColour();
1400 // ----------------------------------------------------------------------------
1401 // wxNotebook base class virtuals
1402 // ----------------------------------------------------------------------------
1404 #if wxUSE_CONSTRAINTS
1406 // override these 2 functions to do nothing: everything is done in OnSize
1408 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
1410 // don't set the sizes of the pages - their correct size is not yet known
1411 wxControl::SetConstraintSizes(false);
1414 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
1419 #endif // wxUSE_CONSTRAINTS
1421 // ----------------------------------------------------------------------------
1422 // wxNotebook Windows message handlers
1423 // ----------------------------------------------------------------------------
1425 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
1426 WXWORD pos
, WXHWND control
)
1428 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1433 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
1436 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
1438 wxBookCtrlEvent
event(wxEVT_NULL
, m_windowId
);
1440 NMHDR
* hdr
= (NMHDR
*)lParam
;
1441 switch ( hdr
->code
) {
1443 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
1446 case TCN_SELCHANGING
:
1447 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
1451 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1454 event
.SetSelection(TabCtrl_GetCurSel(GetHwnd()));
1455 event
.SetOldSelection(m_nSelection
);
1456 event
.SetEventObject(this);
1457 event
.SetInt(idCtrl
);
1459 bool processed
= HandleWindowEvent(event
);
1460 *result
= !event
.IsAllowed();
1464 #endif // wxUSE_NOTEBOOK