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/string.h"
29 #include "wx/dcclient.h"
30 #include "wx/dcmemory.h"
31 #include "wx/control.h"
34 #include "wx/imaglist.h"
35 #include "wx/sysopt.h"
37 #include "wx/msw/private.h"
40 #include "wx/msw/winundef.h"
42 // include <commctrl.h> "properly"
43 #include "wx/msw/wrapcctl.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 #include "wx/listimpl.cpp"
108 WX_DEFINE_LIST( wxNotebookPageInfoList
)
110 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
111 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
113 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
114 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
115 EVT_SIZE(wxNotebook::OnSize
)
116 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
118 #if USE_NOTEBOOK_ANTIFLICKER
119 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
)
120 EVT_PAINT(wxNotebook::OnPaint
)
121 #endif // USE_NOTEBOOK_ANTIFLICKER
124 #if wxUSE_EXTENDED_RTTI
125 WX_DEFINE_FLAGS( wxNotebookStyle
)
127 wxBEGIN_FLAGS( wxNotebookStyle
)
128 // new style border flags, we put them first to
129 // use them for streaming out
130 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
131 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
132 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
133 wxFLAGS_MEMBER(wxBORDER_RAISED
)
134 wxFLAGS_MEMBER(wxBORDER_STATIC
)
135 wxFLAGS_MEMBER(wxBORDER_NONE
)
137 // old style border flags
138 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
139 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
140 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
141 wxFLAGS_MEMBER(wxRAISED_BORDER
)
142 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
143 wxFLAGS_MEMBER(wxBORDER
)
145 // standard window styles
146 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
147 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
148 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
149 wxFLAGS_MEMBER(wxWANTS_CHARS
)
150 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
151 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
152 wxFLAGS_MEMBER(wxVSCROLL
)
153 wxFLAGS_MEMBER(wxHSCROLL
)
155 wxFLAGS_MEMBER(wxNB_FIXEDWIDTH
)
156 wxFLAGS_MEMBER(wxBK_DEFAULT
)
157 wxFLAGS_MEMBER(wxBK_TOP
)
158 wxFLAGS_MEMBER(wxBK_LEFT
)
159 wxFLAGS_MEMBER(wxBK_RIGHT
)
160 wxFLAGS_MEMBER(wxBK_BOTTOM
)
161 wxFLAGS_MEMBER(wxNB_NOPAGETHEME
)
162 wxFLAGS_MEMBER(wxNB_FLAT
)
164 wxEND_FLAGS( wxNotebookStyle
)
166 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook
, wxControl
,"wx/notebook.h")
167 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo
, wxObject
, "wx/notebook.h" )
169 wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo
* , wxNotebookPageInfoList
) ;
171 template<> void wxCollectionToVariantArray( wxNotebookPageInfoList
const &theList
, wxxVariantArray
&value
)
173 wxListCollectionToVariantArray
<wxNotebookPageInfoList::compatibility_iterator
>( theList
, value
) ;
176 wxBEGIN_PROPERTIES_TABLE(wxNotebook
)
177 wxEVENT_PROPERTY( PageChanging
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, wxNotebookEvent
)
178 wxEVENT_PROPERTY( PageChanged
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, wxNotebookEvent
)
180 wxPROPERTY_COLLECTION( PageInfos
, wxNotebookPageInfoList
, wxNotebookPageInfo
* , AddPageInfo
, GetPageInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
181 wxPROPERTY_FLAGS( WindowStyle
, wxNotebookStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
182 wxEND_PROPERTIES_TABLE()
184 wxBEGIN_HANDLERS_TABLE(wxNotebook
)
185 wxEND_HANDLERS_TABLE()
187 wxCONSTRUCTOR_5( wxNotebook
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
190 wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo
)
191 wxREADONLY_PROPERTY( Page
, wxNotebookPage
* , GetPage
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
192 wxREADONLY_PROPERTY( Text
, wxString
, GetText
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
193 wxREADONLY_PROPERTY( Selected
, bool , GetSelected
, false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
194 wxREADONLY_PROPERTY( ImageId
, int , GetImageId
, -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
195 wxEND_PROPERTIES_TABLE()
197 wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo
)
198 wxEND_HANDLERS_TABLE()
200 wxCONSTRUCTOR_4( wxNotebookPageInfo
, wxNotebookPage
* , Page
, wxString
, Text
, bool , Selected
, int , ImageId
)
203 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
204 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo
, wxObject
)
206 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
208 // ============================================================================
210 // ============================================================================
212 // ----------------------------------------------------------------------------
213 // wxNotebook construction
214 // ----------------------------------------------------------------------------
216 const wxNotebookPageInfoList
& wxNotebook::GetPageInfos() const
218 wxNotebookPageInfoList
* list
= const_cast< wxNotebookPageInfoList
* >( &m_pageInfos
) ;
219 WX_CLEAR_LIST( wxNotebookPageInfoList
, *list
) ;
220 for( size_t i
= 0 ; i
< GetPageCount() ; ++i
)
222 wxNotebookPageInfo
*info
= new wxNotebookPageInfo() ;
223 info
->Create( const_cast<wxNotebook
*>(this)->GetPage(i
) , GetPageText(i
) , GetSelection() == int(i
) , GetPageImage(i
) ) ;
224 list
->Append( info
) ;
229 // common part of all ctors
230 void wxNotebook::Init()
236 m_hbrBackground
= NULL
;
237 #endif // wxUSE_UXTHEME
239 #if USE_NOTEBOOK_ANTIFLICKER
240 m_hasSubclassedUpdown
= false;
241 #endif // USE_NOTEBOOK_ANTIFLICKER
244 // default for dynamic class
245 wxNotebook::wxNotebook()
250 // the same arguments as for wxControl
251 wxNotebook::wxNotebook(wxWindow
*parent
,
256 const wxString
& name
)
260 Create(parent
, id
, pos
, size
, style
, name
);
264 bool wxNotebook::Create(wxWindow
*parent
,
269 const wxString
& name
)
271 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
273 #if defined(__POCKETPC__)
274 style
|= wxBK_BOTTOM
| wxNB_FLAT
;
281 // Not sure why, but without this style, there is no border
282 // around the notebook tabs.
283 if (style
& wxNB_FLAT
)
284 style
|= wxBORDER_SUNKEN
;
288 // ComCtl32 notebook tabs simply don't work unless they're on top if we have uxtheme, we can
289 // work around it later (after control creation), but if we don't have uxtheme, we have to clear
291 const int verComCtl32
= wxApp::GetComCtl32Version();
292 if ( verComCtl32
== 600 )
294 style
&= ~(wxBK_BOTTOM
| wxBK_LEFT
| wxBK_RIGHT
);
296 #endif //wxUSE_UXTHEME
298 LPCTSTR className
= WC_TABCONTROL
;
300 #if USE_NOTEBOOK_ANTIFLICKER
301 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
302 // causes horrible flicker when resizing notebook, so get rid of it by
303 // using a class without these styles (but otherwise identical to it)
304 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
306 static ClassRegistrar s_clsNotebook
;
307 if ( !s_clsNotebook
.IsInitialized() )
309 // get a copy of standard class and modify it
312 if ( ::GetClassInfo(NULL
, WC_TABCONTROL
, &wc
) )
315 wx_reinterpret_cast(WXFARPROC
, wc
.lpfnWndProc
);
316 wc
.lpszClassName
= wxT("_wx_SysTabCtl32");
317 wc
.style
&= ~(CS_HREDRAW
| CS_VREDRAW
);
318 wc
.hInstance
= wxGetInstance();
319 wc
.lpfnWndProc
= wxNotebookWndProc
;
320 s_clsNotebook
.Register(wc
);
324 wxLogLastError(_T("GetClassInfoEx(SysTabCtl32)"));
328 // use our custom class if available but fall back to the standard
329 // notebook if we failed to register it
330 if ( s_clsNotebook
.IsRegistered() )
332 // it's ok to use c_str() here as the static s_clsNotebook object
333 // has sufficiently long lifetime
334 className
= s_clsNotebook
.GetName().c_str();
337 #endif // USE_NOTEBOOK_ANTIFLICKER
339 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
340 wxDefaultValidator
, name
) )
343 if ( !MSWCreateControl(className
, wxEmptyString
, pos
, size
) )
347 if ( HasFlag(wxNB_NOPAGETHEME
) ||
348 wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) )
350 SetBackgroundColour(GetThemeBackgroundColour());
352 else // use themed background by default
354 // create backing store
358 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
359 // control is simply not rendered correctly), so we disable themes
360 // if possible, otherwise we simply clear the styles.
361 // It's probably not possible to have UXTHEME without ComCtl32 6 or better, but lets
363 const int verComCtl32
= wxApp::GetComCtl32Version();
364 if ( verComCtl32
== 600 )
366 // check if we use themes at all -- if we don't, we're still okay
367 if ( wxUxThemeEngine::GetIfActive() && (style
& (wxBK_BOTTOM
|wxBK_LEFT
|wxBK_RIGHT
)))
369 wxUxThemeEngine::GetIfActive()->SetWindowTheme((HWND
)this->GetHandle(), L
"", L
"");
370 SetBackgroundColour(GetThemeBackgroundColour()); //correct the background color for the new non-themed control
373 #endif // wxUSE_UXTHEME
375 // Undocumented hack to get flat notebook style
376 // In fact, we should probably only do this in some
377 // curcumstances, i.e. if we know we will have a border
378 // at the bottom (the tab control doesn't draw it itself)
379 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
380 if (HasFlag(wxNB_FLAT
))
382 SendMessage(GetHwnd(), CCM_SETVERSION
, COMCTL32_VERSION
, 0);
384 SetBackgroundColour(*wxWHITE
);
390 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
392 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
394 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
396 if ( style
& wxNB_MULTILINE
)
397 tabStyle
|= TCS_MULTILINE
;
398 if ( style
& wxNB_FIXEDWIDTH
)
399 tabStyle
|= TCS_FIXEDWIDTH
;
401 if ( style
& wxBK_BOTTOM
)
402 tabStyle
|= TCS_RIGHT
;
403 else if ( style
& wxBK_LEFT
)
404 tabStyle
|= TCS_VERTICAL
;
405 else if ( style
& wxBK_RIGHT
)
406 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
411 // note that we never want to have the default WS_EX_CLIENTEDGE style
412 // as it looks too ugly for the notebooks
419 wxNotebook::~wxNotebook()
422 if ( m_hbrBackground
)
423 ::DeleteObject((HBRUSH
)m_hbrBackground
);
424 #endif // wxUSE_UXTHEME
427 // ----------------------------------------------------------------------------
428 // wxNotebook accessors
429 // ----------------------------------------------------------------------------
431 size_t wxNotebook::GetPageCount() const
434 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(GetHwnd()) );
436 return m_pages
.Count();
439 int wxNotebook::GetRowCount() const
441 return TabCtrl_GetRowCount(GetHwnd());
444 int wxNotebook::SetSelection(size_t nPage
)
446 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
448 if ( int(nPage
) != m_nSelection
)
450 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
451 event
.SetSelection(nPage
);
452 event
.SetOldSelection(m_nSelection
);
453 event
.SetEventObject(this);
454 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
456 // program allows the page change
457 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
458 (void)GetEventHandler()->ProcessEvent(event
);
460 TabCtrl_SetCurSel(GetHwnd(), nPage
);
467 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
469 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
472 tcItem
.mask
= TCIF_TEXT
;
473 tcItem
.pszText
= (wxChar
*)strText
.c_str();
475 if ( !HasFlag(wxNB_MULTILINE
) )
476 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
478 // multiline - we need to set new page size if a line is added or removed
479 int rows
= GetRowCount();
480 bool ret
= TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
482 if ( ret
&& rows
!= GetRowCount() )
484 const wxRect r
= GetPageSize();
485 const size_t count
= m_pages
.Count();
486 for ( size_t page
= 0; page
< count
; page
++ )
487 m_pages
[page
]->SetSize(r
);
493 wxString
wxNotebook::GetPageText(size_t nPage
) const
495 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
499 tcItem
.mask
= TCIF_TEXT
;
500 tcItem
.pszText
= buf
;
501 tcItem
.cchTextMax
= WXSIZEOF(buf
);
504 if ( TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) )
505 str
= tcItem
.pszText
;
510 int wxNotebook::GetPageImage(size_t nPage
) const
512 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
515 tcItem
.mask
= TCIF_IMAGE
;
517 return TabCtrl_GetItem(GetHwnd(), nPage
, &tcItem
) ? tcItem
.iImage
: wxNOT_FOUND
;
520 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
522 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
525 tcItem
.mask
= TCIF_IMAGE
;
526 tcItem
.iImage
= nImage
;
528 return TabCtrl_SetItem(GetHwnd(), nPage
, &tcItem
) != 0;
531 void wxNotebook::SetImageList(wxImageList
* imageList
)
533 wxNotebookBase::SetImageList(imageList
);
537 (void) TabCtrl_SetImageList(GetHwnd(), (HIMAGELIST
)imageList
->GetHIMAGELIST());
541 // ----------------------------------------------------------------------------
542 // wxNotebook size settings
543 // ----------------------------------------------------------------------------
545 wxRect
wxNotebook::GetPageSize() const
550 ::GetClientRect(GetHwnd(), &rc
);
552 // This check is to work around a bug in TabCtrl_AdjustRect which will
553 // cause a crash on win2k or on XP with themes disabled if either
554 // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle
557 // The value of 20 is chosen arbitrarily but seems to work
558 if ( rc
.right
> 20 && rc
.bottom
> 20 )
560 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
562 wxCopyRECTToRect(rc
, r
);
568 void wxNotebook::SetPageSize(const wxSize
& size
)
570 // transform the page size into the notebook size
577 TabCtrl_AdjustRect(GetHwnd(), true, &rc
);
580 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
583 void wxNotebook::SetPadding(const wxSize
& padding
)
585 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
588 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
590 void wxNotebook::SetTabSize(const wxSize
& sz
)
592 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
595 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
597 // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP
598 wxSize sizeTotal
= sizePage
;
601 if ( GetPageCount() > 0 )
604 TabCtrl_GetItemRect(GetHwnd(), 0, &rect
);
605 tabSize
.x
= rect
.right
- rect
.left
;
606 tabSize
.y
= rect
.bottom
- rect
.top
;
609 // add an extra margin in both directions
610 const int MARGIN
= 8;
613 sizeTotal
.x
+= MARGIN
;
614 sizeTotal
.y
+= tabSize
.y
+ MARGIN
;
616 else // horizontal layout
618 sizeTotal
.x
+= tabSize
.x
+ MARGIN
;
619 sizeTotal
.y
+= MARGIN
;
625 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
627 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
629 const wxRect r
= GetPageSize();
636 // ----------------------------------------------------------------------------
637 // wxNotebook operations
638 // ----------------------------------------------------------------------------
640 // remove one page from the notebook, without deleting
641 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
643 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
647 TabCtrl_DeleteItem(GetHwnd(), nPage
);
649 if ( m_pages
.IsEmpty() )
651 // no selection any more, the notebook becamse empty
654 else // notebook still not empty
656 int selNew
= TabCtrl_GetCurSel(GetHwnd());
659 // No selection change, just refresh the current selection.
660 // Because it could be that the slection index changed
661 // we need to update it.
662 // Note: this does not mean the selection it self changed.
663 m_nSelection
= selNew
;
664 m_pages
[m_nSelection
]->Refresh();
666 else if (int(nPage
) == m_nSelection
)
668 // The selection was deleted.
670 // Determine new selection.
671 if (m_nSelection
== int(GetPageCount()))
672 selNew
= m_nSelection
- 1;
674 selNew
= m_nSelection
;
676 // m_nSelection must be always valid so reset it before calling
679 SetSelection(selNew
);
683 wxFAIL
; // Windows did not behave ok.
691 bool wxNotebook::DeleteAllPages()
693 size_t nPageCount
= GetPageCount();
695 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
696 delete m_pages
[nPage
];
700 TabCtrl_DeleteAllItems(GetHwnd());
704 InvalidateBestSize();
708 // same as AddPage() but does it at given position
709 bool wxNotebook::InsertPage(size_t nPage
,
710 wxNotebookPage
*pPage
,
711 const wxString
& strText
,
715 wxCHECK_MSG( pPage
!= NULL
, false, _T("NULL page in wxNotebook::InsertPage") );
716 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false,
717 _T("invalid index in wxNotebook::InsertPage") );
719 wxASSERT_MSG( pPage
->GetParent() == this,
720 _T("notebook pages must have notebook as parent") );
722 // add a new tab to the control
723 // ----------------------------
725 // init all fields to 0
727 wxZeroMemory(tcItem
);
729 // set the image, if any
732 tcItem
.mask
|= TCIF_IMAGE
;
733 tcItem
.iImage
= imageId
;
737 if ( !strText
.empty() )
739 tcItem
.mask
|= TCIF_TEXT
;
740 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
743 // hide the page: unless it is selected, it shouldn't be shown (and if it
744 // is selected it will be shown later)
745 HWND hwnd
= GetWinHwnd(pPage
);
746 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
748 // this updates internal flag too -- otherwise it would get out of sync
749 // with the real state
753 // fit the notebook page to the tab control's display area: this should be
754 // done before adding it to the notebook or TabCtrl_InsertItem() will
755 // change the notebooks size itself!
756 AdjustPageSize(pPage
);
758 // finally do insert it
759 if ( TabCtrl_InsertItem(GetHwnd(), nPage
, &tcItem
) == -1 )
761 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
766 // succeeded: save the pointer to the page
767 m_pages
.Insert(pPage
, nPage
);
769 // we may need to adjust the size again if the notebook size changed:
770 // normally this only happens for the first page we add (the tabs which
771 // hadn't been there before are now shown) but for a multiline notebook it
772 // can happen for any page at all as a new row could have been started
773 if ( m_pages
.GetCount() == 1 || HasFlag(wxNB_MULTILINE
) )
775 AdjustPageSize(pPage
);
778 // now deal with the selection
779 // ---------------------------
781 // if the inserted page is before the selected one, we must update the
782 // index of the selected page
783 if ( int(nPage
) <= m_nSelection
)
785 // one extra page added
789 // some page should be selected: either this one or the first one if there
790 // is still no selection
794 else if ( m_nSelection
== -1 )
798 SetSelection(selNew
);
800 InvalidateBestSize();
805 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
807 TC_HITTESTINFO hitTestInfo
;
808 hitTestInfo
.pt
.x
= pt
.x
;
809 hitTestInfo
.pt
.y
= pt
.y
;
810 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
816 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
817 *flags
|= wxBK_HITTEST_NOWHERE
;
818 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
819 *flags
|= wxBK_HITTEST_ONITEM
;
820 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
821 *flags
|= wxBK_HITTEST_ONICON
;
822 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
823 *flags
|= wxBK_HITTEST_ONLABEL
;
824 if ( item
== wxNOT_FOUND
&& GetPageSize().Inside(pt
) )
825 *flags
|= wxBK_HITTEST_ONPAGE
;
831 // ----------------------------------------------------------------------------
832 // flicker-less notebook redraw
833 // ----------------------------------------------------------------------------
835 #if USE_NOTEBOOK_ANTIFLICKER
837 // wnd proc for the spin button
838 LRESULT APIENTRY _EXPORT
wxNotebookSpinBtnWndProc(HWND hwnd
,
843 if ( message
== WM_ERASEBKGND
)
846 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn
,
847 hwnd
, message
, wParam
, lParam
);
850 LRESULT APIENTRY _EXPORT
wxNotebookWndProc(HWND hwnd
,
855 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook
,
856 hwnd
, message
, wParam
, lParam
);
859 void wxNotebook::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
864 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
))
869 ::GetClientRect(GetHwnd(), &rc
);
870 wxBitmap
bmp(rc
.right
, rc
.bottom
);
871 memdc
.SelectObject(bmp
);
873 // if there is no special brush just use the solid background colour
875 HBRUSH hbr
= (HBRUSH
)m_hbrBackground
;
882 brush
= wxBrush(GetBackgroundColour());
883 hbr
= GetHbrushOf(brush
);
886 ::FillRect(GetHdcOf(memdc
), &rc
, hbr
);
888 MSWDefWindowProc(WM_PAINT
, (WPARAM
)memdc
.GetHDC(), 0);
890 dc
.Blit(0, 0, rc
.right
, rc
.bottom
, &memdc
, 0, 0);
893 #endif // USE_NOTEBOOK_ANTIFLICKER
895 // ----------------------------------------------------------------------------
896 // wxNotebook callbacks
897 // ----------------------------------------------------------------------------
899 void wxNotebook::OnSize(wxSizeEvent
& event
)
901 if ( GetPageCount() == 0 )
903 // Prevents droppings on resize, but does cause some flicker
904 // when there are no pages.
912 // Without this, we can sometimes get droppings at the edges
913 // of a notebook, for example a notebook in a splitter window.
914 // This needs to be reconciled with the RefreshRect calls
915 // at the end of this function, which weren't enough to prevent
918 wxSize sz
= GetClientSize();
920 // Refresh right side
921 wxRect
rect(sz
.x
-4, 0, 4, sz
.y
);
924 // Refresh bottom side
925 rect
= wxRect(0, sz
.y
-4, sz
.x
, 4);
929 rect
= wxRect(0, 0, 4, sz
.y
);
932 #endif // !__WXWINCE__
934 // fit all the notebook pages to the tab control's display area
937 rc
.left
= rc
.top
= 0;
938 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
940 // save the total size, we'll use it below
941 int widthNbook
= rc
.right
- rc
.left
,
942 heightNbook
= rc
.bottom
- rc
.top
;
944 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
945 // returns completely false values for multiline tab controls after the tabs
946 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
948 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
950 // and the only work around I could find was this ugly hack... without it
951 // simply toggling the "multiline" checkbox in the notebook sample resulted
952 // in a noticeable page displacement
953 if ( HasFlag(wxNB_MULTILINE
) )
955 // avoid an infinite recursion: we get another notification too!
956 static bool s_isInOnSize
= false;
961 SendMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
,
962 MAKELPARAM(rc
.right
, rc
.bottom
));
963 s_isInOnSize
= false;
968 // background bitmap size has changed, update the brush using it too
970 #endif // wxUSE_UXTHEME
972 TabCtrl_AdjustRect(GetHwnd(), false, &rc
);
974 int width
= rc
.right
- rc
.left
,
975 height
= rc
.bottom
- rc
.top
;
976 size_t nCount
= m_pages
.Count();
977 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
978 wxNotebookPage
*pPage
= m_pages
[nPage
];
979 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
983 // unless we had already repainted everything, we now need to refresh
984 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
986 // invalidate areas not covered by pages
987 RefreshRect(wxRect(0, 0, widthNbook
, rc
.top
), false);
988 RefreshRect(wxRect(0, rc
.top
, rc
.left
, height
), false);
989 RefreshRect(wxRect(0, rc
.bottom
, widthNbook
, heightNbook
- rc
.bottom
),
991 RefreshRect(wxRect(rc
.right
, rc
.top
, widthNbook
- rc
.right
, height
),
995 #if USE_NOTEBOOK_ANTIFLICKER
996 // subclass the spin control used by the notebook to scroll pages to
997 // prevent it from flickering on resize
998 if ( !m_hasSubclassedUpdown
)
1000 // iterate over all child windows to find spin button
1001 for ( HWND child
= ::GetWindow(GetHwnd(), GW_CHILD
);
1003 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
1005 wxWindow
*childWindow
= wxFindWinFromHandle((WXHWND
)child
);
1007 // see if it exists, if no wxWindow found then assume it's the spin
1011 // subclass the spin button to override WM_ERASEBKGND
1012 if ( !gs_wndprocNotebookSpinBtn
)
1013 gs_wndprocNotebookSpinBtn
= (WXFARPROC
)wxGetWindowProc(child
);
1015 wxSetWindowProc(child
, wxNotebookSpinBtnWndProc
);
1016 m_hasSubclassedUpdown
= true;
1021 #endif // USE_NOTEBOOK_ANTIFLICKER
1026 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
1028 // is it our tab control?
1029 if ( event
.GetEventObject() == this )
1031 int sel
= event
.GetOldSelection();
1033 m_pages
[sel
]->Show(false);
1035 sel
= event
.GetSelection();
1038 wxNotebookPage
*pPage
= m_pages
[sel
];
1042 // Changing the page should give the focus to it but, as per bug report
1043 // http://sf.net/tracker/index.php?func=detail&aid=1150659&group_id=9863&atid=109863,
1044 // we should not set the focus to it directly since it erroneously
1045 // selects radio buttons and breaks keyboard handling for a notebook's
1046 // scroll buttons. So give focus to the notebook and not the page.
1048 // but don't do this is the notebook is hidden
1049 if ( ::IsWindowVisible(GetHwnd()) )
1055 // we want to give others a chance to process this message as well
1059 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
1061 if ( event
.IsWindowChange() ) {
1063 AdvanceSelection(event
.GetDirection());
1066 // we get this event in 3 cases
1068 // a) one of our pages might have generated it because the user TABbed
1069 // out from it in which case we should propagate the event upwards and
1070 // our parent will take care of setting the focus to prev/next sibling
1074 // b) the parent panel wants to give the focus to us so that we
1075 // forward it to our selected page. We can't deal with this in
1076 // OnSetFocus() because we don't know which direction the focus came
1077 // from in this case and so can't choose between setting the focus to
1078 // first or last panel child
1082 // c) we ourselves (see MSWTranslateMessage) generated the event
1084 wxWindow
* const parent
= GetParent();
1086 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
1087 const bool isFromParent
= event
.GetEventObject() == (wxObject
*) parent
;
1088 const bool isFromSelf
= event
.GetEventObject() == (wxObject
*) this;
1090 if ( isFromParent
|| isFromSelf
)
1092 // no, it doesn't come from child, case (b) or (c): forward to a
1093 // page but only if direction is backwards (TAB) or from ourselves,
1094 if ( m_nSelection
!= -1 &&
1095 (!event
.GetDirection() || isFromSelf
) )
1097 // so that the page knows that the event comes from it's parent
1098 // and is being propagated downwards
1099 event
.SetEventObject(this);
1101 wxWindow
*page
= m_pages
[m_nSelection
];
1102 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
1106 //else: page manages focus inside it itself
1108 else // otherwise set the focus to the notebook itself
1115 // it comes from our child, case (a), pass to the parent, but only
1116 // if the direction is forwards. Otherwise set the focus to the
1117 // notebook itself. The notebook is always the 'first' control of a
1119 if ( !event
.GetDirection() )
1125 event
.SetCurrentFocus(this);
1126 parent
->GetEventHandler()->ProcessEvent(event
);
1134 bool wxNotebook::DoDrawBackground(WXHDC hDC
, wxWindow
*child
)
1136 wxUxThemeHandle
theme(child
? child
: this, L
"TAB");
1140 // get the notebook client rect (we're not interested in drawing tabs
1142 wxRect r
= GetPageSize();
1147 wxCopyRectToRECT(r
, rc
);
1149 // map rect to the coords of the window we're drawing in
1151 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1153 // we have the content area (page size), but we need to draw all of the
1154 // background for it to be aligned correctly
1155 wxUxThemeEngine::Get()->GetThemeBackgroundExtent
1164 wxUxThemeEngine::Get()->DrawThemeBackground
1177 WXHBRUSH
wxNotebook::QueryBgBitmap()
1179 wxRect r
= GetPageSize();
1183 WindowHDC
hDC(GetHwnd());
1184 MemoryHDC
hDCMem(hDC
);
1185 CompatibleBitmap
hBmp(hDC
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1187 SelectInHDC
selectBmp(hDCMem
, hBmp
);
1189 if ( !DoDrawBackground((WXHDC
)(HDC
)hDCMem
) )
1192 return (WXHBRUSH
)::CreatePatternBrush(hBmp
);
1195 void wxNotebook::UpdateBgBrush()
1197 if ( m_hbrBackground
)
1198 ::DeleteObject((HBRUSH
)m_hbrBackground
);
1200 if ( !m_hasBgCol
&& wxUxThemeEngine::GetIfActive() )
1202 m_hbrBackground
= QueryBgBitmap();
1204 else // no themes or we've got user-defined solid colour
1206 m_hbrBackground
= NULL
;
1210 WXHBRUSH
wxNotebook::MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
)
1212 if ( m_hbrBackground
)
1214 // before drawing with the background brush, we need to position it
1217 ::GetWindowRect((HWND
)hWnd
, &rc
);
1219 ::MapWindowPoints(NULL
, GetHwnd(), (POINT
*)&rc
, 1);
1221 if ( !::SetBrushOrgEx((HDC
)hDC
, -rc
.left
, -rc
.top
, NULL
) )
1223 wxLogLastError(_T("SetBrushOrgEx(notebook bg brush)"));
1226 return m_hbrBackground
;
1229 return wxNotebookBase::MSWGetBgBrushForChild(hDC
, hWnd
);
1232 bool wxNotebook::MSWPrintChild(WXHDC hDC
, wxWindow
*child
)
1234 // solid background colour overrides themed background drawing
1235 if ( !UseBgCol() && DoDrawBackground(hDC
, child
) )
1238 // If we're using a solid colour (for example if we've switched off
1239 // theming for this notebook), paint it
1242 wxRect r
= GetPageSize();
1247 wxCopyRectToRECT(r
, rc
);
1249 // map rect to the coords of the window we're drawing in
1251 ::MapWindowPoints(GetHwnd(), GetHwndOf(child
), (POINT
*)&rc
, 2);
1253 wxBrush
brush(GetBackgroundColour());
1254 HBRUSH hbr
= GetHbrushOf(brush
);
1256 ::FillRect((HDC
) hDC
, &rc
, hbr
);
1261 return wxNotebookBase::MSWPrintChild(hDC
, child
);
1264 #endif // wxUSE_UXTHEME
1266 // Windows only: attempts to get colour for UX theme page background
1267 wxColour
wxNotebook::GetThemeBackgroundColour() const
1270 if (wxUxThemeEngine::Get())
1272 wxUxThemeHandle
hTheme((wxNotebook
*) this, L
"TAB");
1275 // This is total guesswork.
1276 // See PlatformSDK\Include\Tmschema.h for values
1277 COLORREF themeColor
;
1278 wxUxThemeEngine::Get()->GetThemeColor(
1282 3821 /* FILLCOLORHINT */,
1286 [DS] Workaround for WindowBlinds:
1287 Some themes return a near black theme color using FILLCOLORHINT,
1288 this makes notebook pages have an ugly black background and makes
1289 text (usually black) unreadable. Retry again with FILLCOLOR.
1291 This workaround potentially breaks appearance of some themes,
1292 but in practice it already fixes some themes.
1294 if (themeColor
== 1)
1296 wxUxThemeEngine::Get()->GetThemeColor(
1300 3802 /* FILLCOLOR */,
1304 return wxRGBToColour(themeColor
);
1307 #endif // wxUSE_UXTHEME
1309 return GetBackgroundColour();
1312 // ----------------------------------------------------------------------------
1313 // wxNotebook base class virtuals
1314 // ----------------------------------------------------------------------------
1316 #if wxUSE_CONSTRAINTS
1318 // override these 2 functions to do nothing: everything is done in OnSize
1320 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
1322 // don't set the sizes of the pages - their correct size is not yet known
1323 wxControl::SetConstraintSizes(false);
1326 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
1331 #endif // wxUSE_CONSTRAINTS
1333 // ----------------------------------------------------------------------------
1334 // wxNotebook Windows message handlers
1335 // ----------------------------------------------------------------------------
1337 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
1338 WXWORD pos
, WXHWND control
)
1340 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1345 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
1348 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
1350 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
1352 NMHDR
* hdr
= (NMHDR
*)lParam
;
1353 switch ( hdr
->code
) {
1355 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
1358 case TCN_SELCHANGING
:
1359 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
1363 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1366 event
.SetSelection(TabCtrl_GetCurSel(GetHwnd()));
1367 event
.SetOldSelection(m_nSelection
);
1368 event
.SetEventObject(this);
1369 event
.SetInt(idCtrl
);
1371 bool processed
= GetEventHandler()->ProcessEvent(event
);
1372 *result
= !event
.IsAllowed();
1376 #endif // wxUSE_NOTEBOOK