1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/notebook.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/notebook.h"
24 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
25 #include "wx/string.h"
30 #include "wx/dcclient.h"
31 #include "wx/dcmemory.h"
32 #include "wx/control.h"
36 #include "wx/imaglist.h"
37 #include "wx/sysopt.h"
39 #include "wx/msw/private.h"
40 #include "wx/msw/dc.h"
43 #include "wx/msw/winundef.h"
46 #include "wx/msw/uxtheme.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // check that the page index is valid
54 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
56 // you can set USE_NOTEBOOK_ANTIFLICKER to 0 for desktop Windows versions too
57 // to disable code whih results in flicker-less notebook redrawing at the
58 // expense of some extra GDI resource consumption
60 // notebooks are never resized under CE anyhow
61 #define USE_NOTEBOOK_ANTIFLICKER 0
63 #define USE_NOTEBOOK_ANTIFLICKER 1
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // This is a work-around for missing defines in gcc-2.95 headers
72 #define TCS_RIGHT 0x0002
76 #define TCS_VERTICAL 0x0080
80 #define TCS_BOTTOM TCS_RIGHT
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 #if USE_NOTEBOOK_ANTIFLICKER
89 // the pointer to standard spin button wnd proc
90 static WXFARPROC gs_wndprocNotebookSpinBtn
= (WXFARPROC
)NULL
;
92 // the pointer to standard tab control wnd proc
93 static WXFARPROC gs_wndprocNotebook
= (WXFARPROC
)NULL
;
95 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
100 #endif // USE_NOTEBOOK_ANTIFLICKER
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 static bool HasTroubleWithNonTopTabs()
108 const int verComCtl32
= wxApp::GetComCtl32Version();
110 // 600 is XP, 616 is Vista -- and both have a problem with tabs not on top
111 // (but don't just test for >= 600 as Microsoft might decide to fix it in
112 // later versions, who knows...)
113 return verComCtl32
>= 600 && verComCtl32
<= 616;
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 #include "wx/listimpl.cpp"
122 WX_DEFINE_LIST( wxNotebookPageInfoList
)
124 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
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()
244 m_hbrBackground
= NULL
;
245 #endif // wxUSE_UXTHEME
247 #if USE_NOTEBOOK_ANTIFLICKER
248 m_hasSubclassedUpdown
= false;
249 #endif // USE_NOTEBOOK_ANTIFLICKER
252 // default for dynamic class
253 wxNotebook::wxNotebook()
258 // the same arguments as for wxControl
259 wxNotebook::wxNotebook(wxWindow
*parent
,
264 const wxString
& name
)
268 Create(parent
, id
, pos
, size
, style
, name
);
272 bool wxNotebook::Create(wxWindow
*parent
,
277 const wxString
& name
)
279 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
281 #if defined(__POCKETPC__)
282 style
|= wxBK_BOTTOM
| wxNB_FLAT
;
289 // Not sure why, but without this style, there is no border
290 // around the notebook tabs.
291 if (style
& wxNB_FLAT
)
292 style
|= wxBORDER_SUNKEN
;
296 // ComCtl32 notebook tabs simply don't work unless they're on top if we
297 // have uxtheme, we can work around it later (after control creation), but
298 // if we have been compiled without uxtheme support, we have to clear those
300 if ( HasTroubleWithNonTopTabs() )
302 style
&= ~(wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
);
304 #endif //wxUSE_UXTHEME
306 #if defined(__WINE__) && wxUSE_UNICODE
307 LPCTSTR className
= L
"SysTabControl32";
309 LPCTSTR className
= WC_TABCONTROL
;
312 #if USE_NOTEBOOK_ANTIFLICKER
313 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
314 // causes horrible flicker when resizing notebook, so get rid of it by
315 // using a class without these styles (but otherwise identical to it)
316 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
318 static ClassRegistrar s_clsNotebook
;
319 if ( !s_clsNotebook
.IsInitialized() )
321 // get a copy of standard class and modify it
324 if ( ::GetClassInfo(NULL
, WC_TABCONTROL
, &wc
) )
327 reinterpret_cast<WXFARPROC
>(wc
.lpfnWndProc
);
328 wc
.lpszClassName
= wxT("_wx_SysTabCtl32");
329 wc
.style
&= ~(CS_HREDRAW
| CS_VREDRAW
);
330 wc
.hInstance
= wxGetInstance();
331 wc
.lpfnWndProc
= wxNotebookWndProc
;
332 s_clsNotebook
.Register(wc
);
336 wxLogLastError(wxT("GetClassInfoEx(SysTabCtl32)"));
340 // use our custom class if available but fall back to the standard
341 // notebook if we failed to register it
342 if ( s_clsNotebook
.IsRegistered() )
344 // it's ok to use c_str() here as the static s_clsNotebook object
345 // has sufficiently long lifetime
346 className
= s_clsNotebook
.GetName().c_str();
349 #endif // USE_NOTEBOOK_ANTIFLICKER
351 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
352 wxDefaultValidator
, name
) )
355 if ( !MSWCreateControl(className
, wxEmptyString
, pos
, size
) )
359 if ( HasFlag(wxNB_NOPAGETHEME
) ||
360 wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) )
362 SetBackgroundColour(GetThemeBackgroundColour());
364 else // use themed background by default
366 // create backing store
370 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
371 // control is simply not rendered correctly), so we disable themes
372 // if possible, otherwise we simply clear the styles.
373 if ( HasTroubleWithNonTopTabs() &&
374 (style
& (wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
)) )
376 // check if we use themes at all -- if we don't, we're still okay
377 if ( wxUxThemeEngine::GetIfActive() )
379 wxUxThemeEngine::GetIfActive()->SetWindowTheme(GetHwnd(), L
"", L
"");
381 // correct the background color for the new non-themed control
382 SetBackgroundColour(GetThemeBackgroundColour());
385 #endif // wxUSE_UXTHEME
387 // Undocumented hack to get flat notebook style
388 // In fact, we should probably only do this in some
389 // curcumstances, i.e. if we know we will have a border
390 // at the bottom (the tab control doesn't draw it itself)
391 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
392 if (HasFlag(wxNB_FLAT
))
394 SendMessage(GetHwnd(), CCM_SETVERSION
, COMCTL32_VERSION
, 0);
396 SetBackgroundColour(*wxWHITE
);
402 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
404 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
406 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
408 if ( style
& wxNB_MULTILINE
)
409 tabStyle
|= TCS_MULTILINE
;
410 if ( style
& wxNB_FIXEDWIDTH
)
411 tabStyle
|= TCS_FIXEDWIDTH
;
413 if ( style
& wxBK_BOTTOM
)
414 tabStyle
|= TCS_RIGHT
;
415 else if ( style
& wxBK_LEFT
)
416 tabStyle
|= TCS_VERTICAL
;
417 else if ( style
& wxBK_RIGHT
)
418 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
423 wxNotebook::~wxNotebook()
426 if ( m_hbrBackground
)
427 ::DeleteObject((HBRUSH
)m_hbrBackground
);
428 #endif // wxUSE_UXTHEME
431 // ----------------------------------------------------------------------------
432 // wxNotebook accessors
433 // ----------------------------------------------------------------------------
435 size_t wxNotebook::GetPageCount() const
438 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(GetHwnd()) );
440 return m_pages
.Count();
443 int wxNotebook::GetRowCount() const
445 return TabCtrl_GetRowCount(GetHwnd());
448 int wxNotebook::SetSelection(size_t nPage
)
450 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
452 if ( m_selection
== wxNOT_FOUND
|| nPage
!= (size_t)m_selection
)
454 if ( SendPageChangingEvent(nPage
) )
456 // program allows the page change
457 const int selectionOld
= m_selection
;
459 UpdateSelection(nPage
);
461 TabCtrl_SetCurSel(GetHwnd(), nPage
);
463 SendPageChangedEvent(selectionOld
, nPage
);
470 void wxNotebook::UpdateSelection(int selNew
)
472 if ( m_selection
!= wxNOT_FOUND
)
473 m_pages
[m_selection
]->Show(false);
475 if ( selNew
!= wxNOT_FOUND
)
477 wxNotebookPage
*pPage
= m_pages
[selNew
];
481 // Changing the page should give the focus to it but, as per bug report
482 // http://sf.net/tracker/index.php?func=detail&aid=1150659&group_id=9863&atid=109863,
483 // we should not set the focus to it directly since it erroneously
484 // selects radio buttons and breaks keyboard handling for a notebook's
485 // scroll buttons. So give focus to the notebook and not the page.
487 // but don't do this is the notebook is hidden
488 if ( ::IsWindowVisible(GetHwnd()) )
491 m_selection
= selNew
;
494 int wxNotebook::ChangeSelection(size_t nPage
)
496 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
498 const int selOld
= m_selection
;
500 if ( m_selection
== wxNOT_FOUND
|| nPage
!= (size_t)m_selection
)
502 TabCtrl_SetCurSel(GetHwnd(), nPage
);
504 UpdateSelection(nPage
);
510 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
512 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
515 tcItem
.mask
= TCIF_TEXT
;
516 tcItem
.pszText
= (wxChar
*)strText
.wx_str();
518 if ( !HasFlag(wxNB_MULTILINE
) )
519 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
521 // multiline - we need to set new page size if a line is added or removed
522 int rows
= GetRowCount();
523 bool ret
= TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
525 if ( ret
&& rows
!= GetRowCount() )
527 const wxRect r
= GetPageSize();
528 const size_t count
= m_pages
.Count();
529 for ( size_t page
= 0; page
< count
; page
++ )
530 m_pages
[page
]->SetSize(r
);
536 wxString
wxNotebook::GetPageText(size_t nPage
) const
538 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
542 tcItem
.mask
= TCIF_TEXT
;
543 tcItem
.pszText
= buf
;
544 tcItem
.cchTextMax
= WXSIZEOF(buf
);
547 if ( TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) )
548 str
= tcItem
.pszText
;
553 int wxNotebook::GetPageImage(size_t nPage
) const
555 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
558 tcItem
.mask
= TCIF_IMAGE
;
560 return TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) ? tcItem
.iImage
564 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
566 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
569 tcItem
.mask
= TCIF_IMAGE
;
570 tcItem
.iImage
= nImage
;
572 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
575 void wxNotebook::SetImageList(wxImageList
* imageList
)
577 wxNotebookBase::SetImageList(imageList
);
581 (void) TabCtrl_SetImageList(GetHwnd(), GetHimagelistOf(imageList
));
585 // ----------------------------------------------------------------------------
586 // wxNotebook size settings
587 // ----------------------------------------------------------------------------
589 wxRect
wxNotebook::GetPageSize() const
594 ::GetClientRect(GetHwnd(), &rc
);
596 // This check is to work around a bug in TabCtrl_AdjustRect which will
597 // cause a crash on win2k or on XP with themes disabled if either
598 // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle
601 // The value of 20 is chosen arbitrarily but seems to work
602 if ( rc
.right
> 20 && rc
.bottom
> 20 )
604 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
606 wxCopyRECTToRect(rc
, r
);
612 void wxNotebook::SetPageSize(const wxSize
& size
)
614 // transform the page size into the notebook size
621 TabCtrl_AdjustRect(GetHwnd(), true, &rc
);
624 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
627 void wxNotebook::SetPadding(const wxSize
& padding
)
629 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
632 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
634 void wxNotebook::SetTabSize(const wxSize
& sz
)
636 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
639 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
641 // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP
642 wxSize sizeTotal
= sizePage
;
645 if ( GetPageCount() > 0 )
648 TabCtrl_GetItemRect(GetHwnd(), 0, &rect
);
649 tabSize
.x
= rect
.right
- rect
.left
;
650 tabSize
.y
= rect
.bottom
- rect
.top
;
653 const int rows
= GetRowCount();
655 // add an extra margin in both directions
656 const int MARGIN
= 8;
659 sizeTotal
.x
+= MARGIN
;
660 sizeTotal
.y
+= tabSize
.y
* rows
+ MARGIN
;
662 else // horizontal layout
664 sizeTotal
.x
+= tabSize
.x
* rows
+ MARGIN
;
665 sizeTotal
.y
+= MARGIN
;
671 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
673 wxCHECK_RET( page
, wxT("NULL page in wxNotebook::AdjustPageSize") );
675 const wxRect r
= GetPageSize();
682 // ----------------------------------------------------------------------------
683 // wxNotebook operations
684 // ----------------------------------------------------------------------------
686 // remove one page from the notebook, without deleting
687 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
689 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
693 // hide the removed page to maintain the invariant that only the
694 // selected page is visible and others are hidden:
695 pageRemoved
->Show(false);
697 TabCtrl_DeleteItem(GetHwnd(), nPage
);
699 if ( m_pages
.IsEmpty() )
701 // no selection any more, the notebook becamse empty
702 m_selection
= 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_selection
= selNew
;
714 m_pages
[m_selection
]->Refresh();
716 else if (int(nPage
) == m_selection
)
718 // The selection was deleted.
720 // Determine new selection.
721 if (m_selection
== int(GetPageCount()))
722 selNew
= m_selection
- 1;
724 selNew
= m_selection
;
726 // m_selection must be always valid so reset it before calling
728 m_selection
= 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_selection
= 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, wxT("NULL page in wxNotebook::InsertPage") );
766 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false,
767 wxT("invalid index in wxNotebook::InsertPage") );
769 wxASSERT_MSG( pPage
->GetParent() == this,
770 wxT("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
= const_cast<wxChar
*>(strText
.wx_str());
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_selection
)
844 // one extra page added
848 DoSetSelectionAfterInsertion(nPage
, bSelect
);
850 InvalidateBestSize();
855 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
857 TC_HITTESTINFO hitTestInfo
;
858 hitTestInfo
.pt
.x
= pt
.x
;
859 hitTestInfo
.pt
.y
= pt
.y
;
860 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
866 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
867 *flags
|= wxBK_HITTEST_NOWHERE
;
868 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
869 *flags
|= wxBK_HITTEST_ONITEM
;
870 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
871 *flags
|= wxBK_HITTEST_ONICON
;
872 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
873 *flags
|= wxBK_HITTEST_ONLABEL
;
874 if ( item
== wxNOT_FOUND
&& GetPageSize().Contains(pt
) )
875 *flags
|= wxBK_HITTEST_ONPAGE
;
881 // ----------------------------------------------------------------------------
882 // flicker-less notebook redraw
883 // ----------------------------------------------------------------------------
885 #if USE_NOTEBOOK_ANTIFLICKER
887 // wnd proc for the spin button
888 LRESULT APIENTRY _EXPORT
wxNotebookSpinBtnWndProc(HWND hwnd
,
893 if ( message
== WM_ERASEBKGND
)
896 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn
,
897 hwnd
, message
, wParam
, lParam
);
900 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
905 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook
,
906 hwnd
, message
, wParam
, lParam
);
909 void wxNotebook::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
914 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
))
919 ::GetClientRect(GetHwnd(), &rc
);
920 wxBitmap
bmp(rc
.right
, rc
.bottom
);
921 memdc
.SelectObject(bmp
);
923 const wxLayoutDirection dir
= dc
.GetLayoutDirection();
924 memdc
.SetLayoutDirection(dir
);
926 // if there is no special brush just use the solid background colour
928 HBRUSH hbr
= (HBRUSH
)m_hbrBackground
;
935 brush
= wxBrush(GetBackgroundColour());
936 hbr
= GetHbrushOf(brush
);
939 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) memdc
.GetImpl();
941 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
943 MSWDefWindowProc(WM_PAINT
, (WPARAM
)(impl
->GetHDC()), 0);
945 // For some reason in RTL mode, source offset has to be -1, otherwise the
946 // right border (physical) remains unpainted.
947 const wxCoord ofs
= dir
== wxLayout_RightToLeft
? -1 : 0;
948 dc
.Blit(ofs
, 0, rc
.right
, rc
.bottom
, &memdc
, ofs
, 0);
951 #endif // USE_NOTEBOOK_ANTIFLICKER
953 // ----------------------------------------------------------------------------
954 // wxNotebook callbacks
955 // ----------------------------------------------------------------------------
957 void wxNotebook::OnSize(wxSizeEvent
& event
)
959 if ( GetPageCount() == 0 )
961 // Prevents droppings on resize, but does cause some flicker
962 // when there are no pages.
970 // Without this, we can sometimes get droppings at the edges
971 // of a notebook, for example a notebook in a splitter window.
972 // This needs to be reconciled with the RefreshRect calls
973 // at the end of this function, which weren't enough to prevent
976 wxSize sz
= GetClientSize();
978 // Refresh right side
979 wxRect
rect(sz
.x
-4, 0, 4, sz
.y
);
982 // Refresh bottom side
983 rect
= wxRect(0, sz
.y
-4, sz
.x
, 4);
987 rect
= wxRect(0, 0, 4, sz
.y
);
990 #endif // !__WXWINCE__
992 // fit all the notebook pages to the tab control's display area
995 rc
.left
= rc
.top
= 0;
996 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
998 // save the total size, we'll use it below
999 int widthNbook
= rc
.right
- rc
.left
,
1000 heightNbook
= rc
.bottom
- rc
.top
;
1002 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
1003 // returns completely false values for multiline tab controls after the tabs
1004 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
1006 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
1008 // and the only work around I could find was this ugly hack... without it
1009 // simply toggling the "multiline" checkbox in the notebook sample resulted
1010 // in a noticeable page displacement
1011 if ( HasFlag(wxNB_MULTILINE
) )
1013 // avoid an infinite recursion: we get another notification too!
1014 static bool s_isInOnSize
= false;
1016 if ( !s_isInOnSize
)
1018 s_isInOnSize
= true;
1019 SendMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
,
1020 MAKELPARAM(rc
.right
, rc
.bottom
));
1021 s_isInOnSize
= false;
1024 // The best size depends on the number of rows of tabs, which can
1025 // change when the notepad is resized.
1026 InvalidateBestSize();
1030 // background bitmap size has changed, update the brush using it too
1032 #endif // wxUSE_UXTHEME
1034 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
1036 int width
= rc
.right
- rc
.left
,
1037 height
= rc
.bottom
- rc
.top
;
1038 size_t nCount
= m_pages
.Count();
1039 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
1040 wxNotebookPage
*pPage
= m_pages
[nPage
];
1041 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
1045 // unless we had already repainted everything, we now need to refresh
1046 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
1048 // invalidate areas not covered by pages
1049 RefreshRect(wxRect(0, 0, widthNbook
, rc
.top
), false);
1050 RefreshRect(wxRect(0, rc
.top
, rc
.left
, height
), false);
1051 RefreshRect(wxRect(0, rc
.bottom
, widthNbook
, heightNbook
- rc
.bottom
),
1053 RefreshRect(wxRect(rc
.right
, rc
.top
, widthNbook
- rc
.right
, height
),
1057 #if USE_NOTEBOOK_ANTIFLICKER
1058 // subclass the spin control used by the notebook to scroll pages to
1059 // prevent it from flickering on resize
1060 if ( !m_hasSubclassedUpdown
)
1062 // iterate over all child windows to find spin button
1063 for ( HWND child
= ::GetWindow(GetHwnd(), GW_CHILD
);
1065 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
1067 wxWindow
*childWindow
= wxFindWinFromHandle((WXHWND
)child
);
1069 // see if it exists, if no wxWindow found then assume it's the spin
1073 // subclass the spin button to override WM_ERASEBKGND
1074 if ( !gs_wndprocNotebookSpinBtn
)
1075 gs_wndprocNotebookSpinBtn
= (WXFARPROC
)wxGetWindowProc(child
);
1077 wxSetWindowProc(child
, wxNotebookSpinBtnWndProc
);
1078 m_hasSubclassedUpdown
= true;
1083 #endif // USE_NOTEBOOK_ANTIFLICKER
1088 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
1090 if ( event
.IsWindowChange() ) {
1092 AdvanceSelection(event
.GetDirection());
1095 // we get this event in 3 cases
1097 // a) one of our pages might have generated it because the user TABbed
1098 // out from it in which case we should propagate the event upwards and
1099 // our parent will take care of setting the focus to prev/next sibling
1103 // b) the parent panel wants to give the focus to us so that we
1104 // forward it to our selected page. We can't deal with this in
1105 // OnSetFocus() because we don't know which direction the focus came
1106 // from in this case and so can't choose between setting the focus to
1107 // first or last panel child
1111 // c) we ourselves (see MSWTranslateMessage) generated the event
1113 wxWindow
* const parent
= GetParent();
1115 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
1116 const bool isFromParent
= event
.GetEventObject() == (wxObject
*) parent
;
1117 const bool isFromSelf
= event
.GetEventObject() == (wxObject
*) this;
1118 const bool isForward
= event
.GetDirection();
1120 if ( isFromSelf
&& !isForward
)
1122 // focus is currently on notebook tab and should leave
1123 // it backwards (Shift-TAB)
1124 event
.SetCurrentFocus(this);
1125 parent
->HandleWindowEvent(event
);
1127 else if ( isFromParent
|| isFromSelf
)
1129 // no, it doesn't come from child, case (b) or (c): forward to a
1130 // page but only if entering notebook page (i.e. direction is
1131 // backwards (Shift-TAB) comething from out-of-notebook, or
1132 // direction is forward (TAB) from ourselves),
1133 if ( m_selection
!= wxNOT_FOUND
&&
1134 (!event
.GetDirection() || isFromSelf
) )
1136 // so that the page knows that the event comes from it's parent
1137 // and is being propagated downwards
1138 event
.SetEventObject(this);
1140 wxWindow
*page
= m_pages
[m_selection
];
1141 if ( !page
->HandleWindowEvent(event
) )
1145 //else: page manages focus inside it itself
1147 else // otherwise set the focus to the notebook itself
1154 // it comes from our child, case (a), pass to the parent, but only
1155 // if the direction is forwards. Otherwise set the focus to the
1156 // notebook itself. The notebook is always the 'first' control of a
1164 event
.SetCurrentFocus(this);
1165 parent
->HandleWindowEvent(event
);
1173 bool wxNotebook::DoDrawBackground(WXHDC hDC
, wxWindow
*child
)
1175 wxUxThemeHandle
theme(child
? child
: this, L
"TAB");
1179 // get the notebook client rect (we're not interested in drawing tabs
1181 wxRect r
= GetPageSize();
1186 wxCopyRectToRECT(r
, rc
);
1188 // map rect to the coords of the window we're drawing in
1190 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1192 // we have the content area (page size), but we need to draw all of the
1193 // background for it to be aligned correctly
1194 wxUxThemeEngine::Get()->GetThemeBackgroundExtent
1203 wxUxThemeEngine::Get()->DrawThemeBackground
1216 WXHBRUSH
wxNotebook::QueryBgBitmap()
1218 wxRect r
= GetPageSize();
1222 WindowHDC
hDC(GetHwnd());
1223 MemoryHDC
hDCMem(hDC
);
1224 CompatibleBitmap
hBmp(hDC
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1226 SelectInHDC
selectBmp(hDCMem
, hBmp
);
1228 if ( !DoDrawBackground((WXHDC
)(HDC
)hDCMem
) )
1231 return (WXHBRUSH
)::CreatePatternBrush(hBmp
);
1234 void wxNotebook::UpdateBgBrush()
1236 if ( m_hbrBackground
)
1237 ::DeleteObject((HBRUSH
)m_hbrBackground
);
1239 if ( !m_hasBgCol
&& wxUxThemeEngine::GetIfActive() )
1241 m_hbrBackground
= QueryBgBitmap();
1243 else // no themes or we've got user-defined solid colour
1245 m_hbrBackground
= NULL
;
1249 WXHBRUSH
wxNotebook::MSWGetBgBrushForChild(WXHDC hDC
, wxWindow
*child
)
1251 if ( m_hbrBackground
)
1253 // before drawing with the background brush, we need to position it
1256 ::GetWindowRect(GetHwndOf(child
), &rc
);
1258 ::MapWindowPoints(NULL
, GetHwnd(), (POINT
*)&rc
, 1);
1260 if ( !::SetBrushOrgEx((HDC
)hDC
, -rc
.left
, -rc
.top
, NULL
) )
1262 wxLogLastError(wxT("SetBrushOrgEx(notebook bg brush)"));
1265 return m_hbrBackground
;
1268 return wxNotebookBase::MSWGetBgBrushForChild(hDC
, child
);
1271 bool wxNotebook::MSWPrintChild(WXHDC hDC
, wxWindow
*child
)
1273 // solid background colour overrides themed background drawing
1274 if ( !UseBgCol() && DoDrawBackground(hDC
, child
) )
1277 // If we're using a solid colour (for example if we've switched off
1278 // theming for this notebook), paint it
1281 wxRect r
= GetPageSize();
1286 wxCopyRectToRECT(r
, rc
);
1288 // map rect to the coords of the window we're drawing in
1290 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1292 wxBrush
brush(GetBackgroundColour());
1293 HBRUSH hbr
= GetHbrushOf(brush
);
1295 ::FillRect((HDC
) hDC
, &rc
, hbr
);
1300 return wxNotebookBase::MSWPrintChild(hDC
, child
);
1303 #endif // wxUSE_UXTHEME
1305 // Windows only: attempts to get colour for UX theme page background
1306 wxColour
wxNotebook::GetThemeBackgroundColour() const
1309 if (wxUxThemeEngine::Get())
1311 wxUxThemeHandle
hTheme((wxNotebook
*) this, L
"TAB");
1314 // This is total guesswork.
1315 // See PlatformSDK\Include\Tmschema.h for values.
1316 // JACS: can also use 9 (TABP_PANE)
1317 COLORREF themeColor
;
1318 bool success
= (S_OK
== wxUxThemeEngine::Get()->GetThemeColor(
1322 3821 /* FILLCOLORHINT */,
1325 return GetBackgroundColour();
1328 [DS] Workaround for WindowBlinds:
1329 Some themes return a near black theme color using FILLCOLORHINT,
1330 this makes notebook pages have an ugly black background and makes
1331 text (usually black) unreadable. Retry again with FILLCOLOR.
1333 This workaround potentially breaks appearance of some themes,
1334 but in practice it already fixes some themes.
1336 if (themeColor
== 1)
1338 wxUxThemeEngine::Get()->GetThemeColor(
1342 3802 /* FILLCOLOR */,
1346 wxColour colour
= wxRGBToColour(themeColor
);
1348 // Under Vista, the tab background colour is reported incorrectly.
1349 // So for the default theme at least, hard-code the colour to something
1350 // that will blend in.
1352 static int s_AeroStatus
= -1;
1353 if (s_AeroStatus
== -1)
1355 WCHAR szwThemeFile
[1024];
1356 WCHAR szwThemeColor
[256];
1357 if (S_OK
== wxUxThemeEngine::Get()->GetCurrentThemeName(szwThemeFile
, 1024, szwThemeColor
, 256, NULL
, 0))
1359 wxString
themeFile(szwThemeFile
), themeColor(szwThemeColor
);
1360 if (themeFile
.Find(wxT("Aero")) != -1 && themeColor
== wxT("NormalColor"))
1369 if (s_AeroStatus
== 1)
1370 colour
= wxColour(255, 255, 255);
1375 #endif // wxUSE_UXTHEME
1377 return GetBackgroundColour();
1380 // ----------------------------------------------------------------------------
1381 // wxNotebook base class virtuals
1382 // ----------------------------------------------------------------------------
1384 #if wxUSE_CONSTRAINTS
1386 // override these 2 functions to do nothing: everything is done in OnSize
1388 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
1390 // don't set the sizes of the pages - their correct size is not yet known
1391 wxControl::SetConstraintSizes(false);
1394 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
1399 #endif // wxUSE_CONSTRAINTS
1401 // ----------------------------------------------------------------------------
1402 // wxNotebook Windows message handlers
1403 // ----------------------------------------------------------------------------
1405 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
1406 WXWORD pos
, WXHWND control
)
1408 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1413 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
1416 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
1418 wxBookCtrlEvent
event(wxEVT_NULL
, m_windowId
);
1420 NMHDR
* hdr
= (NMHDR
*)lParam
;
1421 switch ( hdr
->code
) {
1423 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
1426 case TCN_SELCHANGING
:
1427 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
1431 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1434 event
.SetSelection(TabCtrl_GetCurSel(GetHwnd()));
1435 event
.SetOldSelection(m_selection
);
1436 event
.SetEventObject(this);
1437 event
.SetInt(idCtrl
);
1439 bool processed
= HandleWindowEvent(event
);
1440 if ( hdr
->code
== TCN_SELCHANGE
)
1441 UpdateSelection(event
.GetSelection());
1443 *result
= !event
.IsAllowed();
1447 #endif // wxUSE_NOTEBOOK