1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "notebook.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/string.h"
32 #include "wx/imaglist.h"
34 #include "wx/control.h"
35 #include "wx/notebook.h"
37 #include "wx/sysopt.h"
39 #include "wx/msw/private.h"
43 #ifdef __GNUWIN32_OLD__
44 #include "wx/msw/gnuwin32/extra.h"
47 #if !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
51 #include "wx/msw/winundef.h"
54 #include "wx/msw/uxtheme.h"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // check that the page index is valid
62 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
65 #define m_hwnd (HWND)GetHWND()
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // This is a work-around for missing defines in gcc-2.95 headers
73 #define TCS_RIGHT 0x0002
77 #define TCS_VERTICAL 0x0080
81 #define TCS_BOTTOM TCS_RIGHT
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 #include <wx/listimpl.cpp>
90 WX_DEFINE_LIST( wxNotebookPageInfoList
) ;
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
95 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
96 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
97 EVT_SIZE(wxNotebook::OnSize
)
98 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
101 #if wxUSE_EXTENDED_RTTI
102 WX_DEFINE_FLAGS( wxNotebookStyle
)
104 wxBEGIN_FLAGS( wxNotebookStyle
)
105 // new style border flags, we put them first to
106 // use them for streaming out
107 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
108 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
109 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
110 wxFLAGS_MEMBER(wxBORDER_RAISED
)
111 wxFLAGS_MEMBER(wxBORDER_STATIC
)
112 wxFLAGS_MEMBER(wxBORDER_NONE
)
114 // old style border flags
115 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
116 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
117 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
118 wxFLAGS_MEMBER(wxRAISED_BORDER
)
119 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
120 wxFLAGS_MEMBER(wxBORDER
)
122 // standard window styles
123 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
124 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
125 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
126 wxFLAGS_MEMBER(wxWANTS_CHARS
)
127 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
128 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
129 wxFLAGS_MEMBER(wxVSCROLL
)
130 wxFLAGS_MEMBER(wxHSCROLL
)
132 wxFLAGS_MEMBER(wxNB_FIXEDWIDTH
)
133 wxFLAGS_MEMBER(wxNB_LEFT
)
134 wxFLAGS_MEMBER(wxNB_RIGHT
)
135 wxFLAGS_MEMBER(wxNB_BOTTOM
)
137 wxEND_FLAGS( wxNotebookStyle
)
139 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook
, wxControl
,"wx/notebook.h")
140 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo
, wxObject
, "wx/notebook.h" )
142 wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo
* , wxNotebookPageInfoList
) ;
144 template<> void wxCollectionToVariantArray( wxNotebookPageInfoList
const &theList
, wxxVariantArray
&value
)
146 wxListCollectionToVariantArray
<wxNotebookPageInfoList::compatibility_iterator
>( theList
, value
) ;
149 wxBEGIN_PROPERTIES_TABLE(wxNotebook
)
150 wxEVENT_PROPERTY( PageChanging
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, wxNotebookEvent
)
151 wxEVENT_PROPERTY( PageChanged
, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, wxNotebookEvent
)
153 wxPROPERTY_COLLECTION( PageInfos
, wxNotebookPageInfoList
, wxNotebookPageInfo
* , AddPageInfo
, GetPageInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
154 wxPROPERTY_FLAGS( WindowStyle
, wxNotebookStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
155 wxEND_PROPERTIES_TABLE()
157 wxBEGIN_HANDLERS_TABLE(wxNotebook
)
158 wxEND_HANDLERS_TABLE()
160 wxCONSTRUCTOR_5( wxNotebook
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
163 wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo
)
164 wxREADONLY_PROPERTY( Page
, wxNotebookPage
* , GetPage
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
165 wxREADONLY_PROPERTY( Text
, wxString
, GetText
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
166 wxREADONLY_PROPERTY( Selected
, bool , GetSelected
, false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
167 wxREADONLY_PROPERTY( ImageId
, int , GetImageId
, -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
168 wxEND_PROPERTIES_TABLE()
170 wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo
)
171 wxEND_HANDLERS_TABLE()
173 wxCONSTRUCTOR_4( wxNotebookPageInfo
, wxNotebookPage
* , Page
, wxString
, Text
, bool , Selected
, int , ImageId
)
176 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
177 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo
, wxObject
)
179 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
181 // ============================================================================
183 // ============================================================================
185 // ----------------------------------------------------------------------------
186 // wxNotebook construction
187 // ----------------------------------------------------------------------------
189 const wxNotebookPageInfoList
& wxNotebook::GetPageInfos() const
191 wxNotebookPageInfoList
* list
= const_cast< wxNotebookPageInfoList
* >( &m_pageInfos
) ;
192 WX_CLEAR_LIST( wxNotebookPageInfoList
, *list
) ;
193 for( size_t i
= 0 ; i
< GetPageCount() ; ++i
)
195 wxNotebookPageInfo
*info
= new wxNotebookPageInfo() ;
196 info
->Create( const_cast<wxNotebook
*>(this)->GetPage(i
) , GetPageText(i
) , GetSelection() == int(i
) , GetPageImage(i
) ) ;
197 list
->Append( info
) ;
202 // common part of all ctors
203 void wxNotebook::Init()
209 m_hbrBackground
= NULL
;
210 #endif // wxUSE_UXTHEME
213 // default for dynamic class
214 wxNotebook::wxNotebook()
219 // the same arguments as for wxControl
220 wxNotebook::wxNotebook(wxWindow
*parent
,
225 const wxString
& name
)
229 Create(parent
, id
, pos
, size
, style
, name
);
233 bool wxNotebook::Create(wxWindow
*parent
,
238 const wxString
& name
)
240 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
241 // control is simply not rendered correctly), so disable them in this case
242 const int verComCtl32
= wxApp::GetComCtl32Version();
243 if ( verComCtl32
== 600 )
245 // check if we use themes at all -- if we don't, we're still ok
247 if ( wxUxThemeEngine::GetIfActive() )
250 style
&= ~(wxNB_BOTTOM
| wxNB_LEFT
| wxNB_RIGHT
);
254 LPCTSTR className
= WC_TABCONTROL
;
256 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
257 // causes horrible flicker when resizing notebook, so get rid of it by
258 // using a class without these styles (but otherwise identical to it)
259 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
261 static ClassRegistrar s_clsNotebook
;
262 if ( !s_clsNotebook
.IsInitialized() )
264 // get a copy of standard class and modify it
267 if ( ::GetClassInfo(::GetModuleHandle(NULL
), WC_TABCONTROL
, &wc
) )
269 wc
.lpszClassName
= wxT("_wx_SysTabCtl32");
270 wc
.style
&= ~(CS_HREDRAW
| CS_VREDRAW
);
272 s_clsNotebook
.Register(wc
);
276 wxLogLastError(_T("GetClassInfoEx(SysTabCtl32)"));
280 // use our custom class if available but fall back to the standard
281 // notebook if we failed to register it
282 if ( s_clsNotebook
.IsRegistered() )
284 // it's ok to use c_str() here as the static s_clsNotebook object
285 // has sufficiently long lifetime
286 className
= s_clsNotebook
.GetName().c_str();
290 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
291 wxDefaultValidator
, name
) )
294 if ( !MSWCreateControl(className
, wxEmptyString
, pos
, size
) )
297 if (HasFlag(wxNB_NOPAGETHEME
) || (wxSystemOptions::HasOption(wxT("msw.notebook.themed-background")) &&
298 wxSystemOptions::GetOptionInt(wxT("msw.notebook.themed-background")) == 0))
300 wxColour col
= GetThemeBackgroundColour();
303 SetBackgroundColour(col
);
309 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
311 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
313 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
315 if ( style
& wxNB_MULTILINE
)
316 tabStyle
|= TCS_MULTILINE
;
317 if ( style
& wxNB_FIXEDWIDTH
)
318 tabStyle
|= TCS_FIXEDWIDTH
;
320 if ( style
& wxNB_BOTTOM
)
321 tabStyle
|= TCS_RIGHT
;
322 else if ( style
& wxNB_LEFT
)
323 tabStyle
|= TCS_VERTICAL
;
324 else if ( style
& wxNB_RIGHT
)
325 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
330 // note that we never want to have the default WS_EX_CLIENTEDGE style
331 // as it looks too ugly for the notebooks
338 wxNotebook::~wxNotebook()
341 if ( m_hbrBackground
)
342 ::DeleteObject((HBRUSH
)m_hbrBackground
);
343 #endif // wxUSE_UXTHEME
346 // ----------------------------------------------------------------------------
347 // wxNotebook accessors
348 // ----------------------------------------------------------------------------
350 size_t wxNotebook::GetPageCount() const
353 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
355 return m_pages
.Count();
358 int wxNotebook::GetRowCount() const
360 return TabCtrl_GetRowCount(m_hwnd
);
363 int wxNotebook::SetSelection(size_t nPage
)
365 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
367 if ( int(nPage
) != m_nSelection
)
369 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
370 event
.SetSelection(nPage
);
371 event
.SetOldSelection(m_nSelection
);
372 event
.SetEventObject(this);
373 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
375 // program allows the page change
376 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
377 (void)GetEventHandler()->ProcessEvent(event
);
379 TabCtrl_SetCurSel(m_hwnd
, nPage
);
386 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
388 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
391 tcItem
.mask
= TCIF_TEXT
;
392 tcItem
.pszText
= (wxChar
*)strText
.c_str();
394 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
397 wxString
wxNotebook::GetPageText(size_t nPage
) const
399 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
403 tcItem
.mask
= TCIF_TEXT
;
404 tcItem
.pszText
= buf
;
405 tcItem
.cchTextMax
= WXSIZEOF(buf
);
408 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
409 str
= tcItem
.pszText
;
414 int wxNotebook::GetPageImage(size_t nPage
) const
416 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
419 tcItem
.mask
= TCIF_IMAGE
;
421 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
424 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
426 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
429 tcItem
.mask
= TCIF_IMAGE
;
430 tcItem
.iImage
= nImage
;
432 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
435 void wxNotebook::SetImageList(wxImageList
* imageList
)
437 wxNotebookBase::SetImageList(imageList
);
441 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
445 // ----------------------------------------------------------------------------
446 // wxNotebook size settings
447 // ----------------------------------------------------------------------------
449 void wxNotebook::SetPageSize(const wxSize
& size
)
451 // transform the page size into the notebook size
458 TabCtrl_AdjustRect(GetHwnd(), true, &rc
);
461 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
464 void wxNotebook::SetPadding(const wxSize
& padding
)
466 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
469 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
471 void wxNotebook::SetTabSize(const wxSize
& sz
)
473 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
476 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
478 wxSize sizeTotal
= sizePage
;
480 // We need to make getting tab size part of the wxWidgets API.
482 if (GetPageCount() > 0)
485 TabCtrl_GetItemRect((HWND
) GetHWND(), 0, & rect
);
486 tabSize
.x
= rect
.right
- rect
.left
;
487 tabSize
.y
= rect
.bottom
- rect
.top
;
489 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
491 sizeTotal
.x
+= tabSize
.x
+ 7;
497 sizeTotal
.y
+= tabSize
.y
+ 7;
503 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
505 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
511 // get the page size from the notebook size
512 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
514 // This check is to work around a bug in TabCtrl_AdjustRect which will
515 // cause a crash on win2k, or on XP with themes disabled, if the
516 // wxNB_MULTILINE style is used and the rectangle is very small, (such as
517 // when the notebook is first created.) The value of 20 is just
518 // arbitrarily chosen, if there is a better way to determine this value
519 // then please do so. --RD
520 if (rc
.right
> 20 && rc
.bottom
> 20)
522 TabCtrl_AdjustRect(m_hwnd
, false, &rc
);
523 page
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
527 // ----------------------------------------------------------------------------
528 // wxNotebook operations
529 // ----------------------------------------------------------------------------
531 // remove one page from the notebook, without deleting
532 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
534 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
538 TabCtrl_DeleteItem(m_hwnd
, nPage
);
540 if ( m_pages
.IsEmpty() )
542 // no selection any more, the notebook becamse empty
545 else // notebook still not empty
547 int selNew
= TabCtrl_GetCurSel(m_hwnd
);
550 // No selection change, just refresh the current selection.
551 // Because it could be that the slection index changed
552 // we need to update it.
553 // Note: this does not mean the selection it self changed.
554 m_nSelection
= selNew
;
555 m_pages
[m_nSelection
]->Refresh();
557 else if (int(nPage
) == m_nSelection
)
559 // The selection was deleted.
561 // Determine new selection.
562 if (m_nSelection
== int(GetPageCount()))
563 selNew
= m_nSelection
- 1;
565 selNew
= m_nSelection
;
567 // m_nSelection must be always valid so reset it before calling
570 SetSelection(selNew
);
574 wxFAIL
; // Windows did not behave ok.
582 bool wxNotebook::DeleteAllPages()
584 size_t nPageCount
= GetPageCount();
586 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
587 delete m_pages
[nPage
];
591 TabCtrl_DeleteAllItems(m_hwnd
);
595 InvalidateBestSize();
599 // same as AddPage() but does it at given position
600 bool wxNotebook::InsertPage(size_t nPage
,
601 wxNotebookPage
*pPage
,
602 const wxString
& strText
,
606 wxCHECK_MSG( pPage
!= NULL
, false, _T("NULL page in wxNotebook::InsertPage") );
607 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false,
608 _T("invalid index in wxNotebook::InsertPage") );
610 wxASSERT_MSG( pPage
->GetParent() == this,
611 _T("notebook pages must have notebook as parent") );
613 // add a new tab to the control
614 // ----------------------------
616 // init all fields to 0
618 wxZeroMemory(tcItem
);
620 // set the image, if any
623 tcItem
.mask
|= TCIF_IMAGE
;
624 tcItem
.iImage
= imageId
;
628 if ( !strText
.empty() )
630 tcItem
.mask
|= TCIF_TEXT
;
631 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
634 // hide the page: unless it is selected, it shouldn't be shown (and if it
635 // is selected it will be shown later)
636 HWND hwnd
= GetWinHwnd(pPage
);
637 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
639 // this updates internal flag too -- otherwise it would get out of sync
640 // with the real state
644 // fit the notebook page to the tab control's display area: this should be
645 // done before adding it to the notebook or TabCtrl_InsertItem() will
646 // change the notebooks size itself!
647 AdjustPageSize(pPage
);
649 // finally do insert it
650 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 )
652 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
657 // succeeded: save the pointer to the page
658 m_pages
.Insert(pPage
, nPage
);
660 // we may need to adjust the size again if the notebook size changed:
661 // normally this only happens for the first page we add (the tabs which
662 // hadn't been there before are now shown) but for a multiline notebook it
663 // can happen for any page at all as a new row could have been started
664 if ( m_pages
.GetCount() == 1 || HasFlag(wxNB_MULTILINE
) )
666 AdjustPageSize(pPage
);
669 // now deal with the selection
670 // ---------------------------
672 // if the inserted page is before the selected one, we must update the
673 // index of the selected page
674 if ( int(nPage
) <= m_nSelection
)
676 // one extra page added
680 // some page should be selected: either this one or the first one if there
681 // is still no selection
685 else if ( m_nSelection
== -1 )
689 SetSelection(selNew
);
691 InvalidateBestSize();
696 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
698 TC_HITTESTINFO hitTestInfo
;
699 hitTestInfo
.pt
.x
= pt
.x
;
700 hitTestInfo
.pt
.y
= pt
.y
;
701 int item
= TabCtrl_HitTest(GetHwnd(), &hitTestInfo
);
707 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
708 *flags
|= wxNB_HITTEST_NOWHERE
;
709 if ((hitTestInfo
.flags
& TCHT_ONITEM
) == TCHT_ONITEM
)
710 *flags
|= wxNB_HITTEST_ONITEM
;
711 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
712 *flags
|= wxNB_HITTEST_ONICON
;
713 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
714 *flags
|= wxNB_HITTEST_ONLABEL
;
721 // ----------------------------------------------------------------------------
722 // wxNotebook callbacks
723 // ----------------------------------------------------------------------------
725 void wxNotebook::OnSize(wxSizeEvent
& event
)
727 // update the background brush
730 #endif // wxUSE_UXTHEME
732 // fit all the notebook pages to the tab control's display area
735 rc
.left
= rc
.top
= 0;
736 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
738 // save the total size, we'll use it below
739 int widthNbook
= rc
.right
- rc
.left
,
740 heightNbook
= rc
.bottom
- rc
.top
;
742 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
743 // returns completely false values for multiline tab controls after the tabs
744 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
746 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
748 // and the only work around I could find was this ugly hack... without it
749 // simply toggling the "multiline" checkbox in the notebook sample resulted
750 // in a noticeable page displacement
751 if ( HasFlag(wxNB_MULTILINE
) )
753 // avoid an infinite recursion: we get another notification too!
754 static bool s_isInOnSize
= false;
759 SendMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
,
760 MAKELPARAM(rc
.right
, rc
.bottom
));
761 s_isInOnSize
= false;
765 TabCtrl_AdjustRect(m_hwnd
, false, &rc
);
767 int width
= rc
.right
- rc
.left
,
768 height
= rc
.bottom
- rc
.top
;
769 size_t nCount
= m_pages
.Count();
770 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
771 wxNotebookPage
*pPage
= m_pages
[nPage
];
772 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
776 // unless we had already repainted everything, we now need to refresh
777 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
779 // invalidate areas not covered by pages
780 RefreshRect(wxRect(0, 0, widthNbook
, rc
.top
), false);
781 RefreshRect(wxRect(0, rc
.top
, rc
.left
, height
), false);
782 RefreshRect(wxRect(0, rc
.bottom
, widthNbook
, heightNbook
- rc
.bottom
),
784 RefreshRect(wxRect(rc
.right
, rc
.top
, widthNbook
- rc
.bottom
, height
),
791 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
793 // is it our tab control?
794 if ( event
.GetEventObject() == this )
796 int sel
= event
.GetOldSelection();
798 m_pages
[sel
]->Show(false);
800 sel
= event
.GetSelection();
803 wxNotebookPage
*pPage
= m_pages
[sel
];
807 // If the newly focused window is not a child of the new page,
808 // SetFocus was not successful and the notebook itself should be
810 wxWindow
*currentFocus
= FindFocus();
811 wxWindow
*startFocus
= currentFocus
;
812 while ( currentFocus
&& currentFocus
!= pPage
&& currentFocus
!= this )
813 currentFocus
= currentFocus
->GetParent();
815 if ( startFocus
== pPage
|| currentFocus
!= pPage
)
819 else // no pages in the notebook, give the focus to itself
827 // we want to give others a chance to process this message as well
831 bool wxNotebook::MSWTranslateMessage(WXMSG
*wxmsg
)
833 const MSG
* const msg
= (MSG
*)wxmsg
;
835 // intercept TAB, CTRL+TAB and CTRL+SHIFT+TAB for processing by wxNotebook.
836 // TAB will be passed to the currently selected page, CTRL+TAB and
837 // CTRL+SHIFT+TAB will be processed by the notebook itself. do not
838 // intercept SHIFT+TAB. This goes to the parent of the notebook which will
840 if ( msg
->message
== WM_KEYDOWN
&& msg
->wParam
== VK_TAB
&&
841 msg
->hwnd
== m_hwnd
&&
842 (wxIsCtrlDown() || !wxIsShiftDown()) )
844 return MSWProcessMessage(wxmsg
);
850 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
852 if ( event
.IsWindowChange() ) {
854 AdvanceSelection(event
.GetDirection());
857 // we get this event in 3 cases
859 // a) one of our pages might have generated it because the user TABbed
860 // out from it in which case we should propagate the event upwards and
861 // our parent will take care of setting the focus to prev/next sibling
865 // b) the parent panel wants to give the focus to us so that we
866 // forward it to our selected page. We can't deal with this in
867 // OnSetFocus() because we don't know which direction the focus came
868 // from in this case and so can't choose between setting the focus to
869 // first or last panel child
873 // c) we ourselves (see MSWTranslateMessage) generated the event
875 wxWindow
* const parent
= GetParent();
877 const bool isFromParent
= event
.GetEventObject() == parent
;
878 const bool isFromSelf
= event
.GetEventObject() == this;
880 if ( isFromParent
|| isFromSelf
)
882 // no, it doesn't come from child, case (b) or (c): forward to a
883 // page but only if direction is backwards (TAB) or from ourselves,
884 if ( m_nSelection
!= -1 &&
885 (!event
.GetDirection() || isFromSelf
) )
887 // so that the page knows that the event comes from it's parent
888 // and is being propagated downwards
889 event
.SetEventObject(this);
891 wxWindow
*page
= m_pages
[m_nSelection
];
892 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
896 //else: page manages focus inside it itself
898 else // otherwise set the focus to the notebook itself
905 // it comes from our child, case (a), pass to the parent, but only
906 // if the direction is forwards. Otherwise set the focus to the
907 // notebook itself. The notebook is always the 'first' control of a
909 if ( !event
.GetDirection() )
915 event
.SetCurrentFocus(this);
916 parent
->GetEventHandler()->ProcessEvent(event
);
924 WXHANDLE
wxNotebook::QueryBgBitmap(wxWindow
*win
)
927 GetWindowRect(GetHwnd(), &rc
);
929 WindowHDC
hDC(GetHwnd());
930 MemoryHDC
hDCMem(hDC
);
931 CompatibleBitmap
hBmp(hDC
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
933 SelectInHDC
selectBmp(hDCMem
, hBmp
);
935 ::SendMessage(GetHwnd(), WM_PRINTCLIENT
,
937 PRF_ERASEBKGND
| PRF_CLIENT
| PRF_NONCLIENT
);
942 ::GetWindowRect(GetHwndOf(win
), &rc2
);
944 COLORREF c
= ::GetPixel(hDCMem
, rc2
.left
- rc
.left
, rc2
.top
- rc
.top
);
948 //else: we are asked to create the brush
950 return (WXHANDLE
)::CreatePatternBrush(hBmp
);
953 void wxNotebook::UpdateBgBrush()
955 if ( m_hbrBackground
)
956 ::DeleteObject((HBRUSH
)m_hbrBackground
);
958 if ( !m_hasBgCol
&& wxUxThemeEngine::GetIfActive() )
960 m_hbrBackground
= (WXHBRUSH
)QueryBgBitmap();
964 m_hbrBackground
= NULL
;
968 WXHBRUSH
wxNotebook::MSWGetBgBrushForChild(WXHDC hDC
, wxWindow
*win
)
970 if ( m_hbrBackground
)
972 // before drawing with the background brush, we need to position it
975 ::GetWindowRect(GetHwndOf(win
), &rc
);
977 ::MapWindowPoints(NULL
, GetHwnd(), (POINT
*)&rc
, 1);
979 if ( !::SetBrushOrgEx((HDC
)hDC
, -rc
.left
, -rc
.top
, NULL
) )
981 wxLogLastError(_T("SetBrushOrgEx(notebook bg brush)"));
984 return m_hbrBackground
;
987 return wxNotebookBase::MSWGetBgBrushForChild(hDC
, win
);
990 wxColour
wxNotebook::MSWGetBgColourForChild(wxWindow
*win
)
993 return GetBackgroundColour();
995 if ( !wxUxThemeEngine::GetIfActive() )
998 COLORREF c
= (COLORREF
)QueryBgBitmap(win
);
1000 return c
== CLR_INVALID
? wxNullColour
: wxRGBToColour(c
);
1004 wxNotebook::MSWPrintChild(wxWindow
*win
,
1006 WXLPARAM
WXUNUSED(lParam
))
1008 // Don't paint the theme for the child if we have a solid
1010 if (m_hasBgCol
|| HasFlag(wxNB_NOPAGETHEME
) || (wxSystemOptions::HasOption(wxT("msw.notebook.themed-background")) &&
1011 wxSystemOptions::GetOptionInt(wxT("msw.notebook.themed-background")) == 0))
1015 ::GetClientRect(GetHwnd(), &rc
);
1016 TabCtrl_AdjustRect(GetHwnd(), true, &rc
);
1017 ::MapWindowPoints(GetHwnd(), GetHwndOf(win
), (POINT
*)&rc
, 2);
1019 wxUxThemeHandle
theme(win
, L
"TAB");
1022 wxUxThemeEngine::Get()->DrawThemeBackground
1036 #endif // wxUSE_UXTHEME
1038 // Windows only: attempts to get colour for UX theme page background
1039 wxColour
wxNotebook::GetThemeBackgroundColour() const
1042 if (wxUxThemeEngine::Get())
1044 wxUxThemeHandle
hTheme((wxNotebook
*) this, L
"TAB");
1047 // This is total guesswork.
1048 // See PlatformSDK\Include\Tmschema.h for values
1049 COLORREF themeColor
;
1050 wxUxThemeEngine::Get()->GetThemeColor(
1054 3821 /* FILLCOLORHINT */,
1058 [DS] Workaround for WindowBlinds:
1059 Some themes return a near black theme color using FILLCOLORHINT,
1060 this makes notebook pages have an ugly black background and makes
1061 text (usually black) unreadable. Retry again with FILLCOLOR.
1063 This workaround potentially breaks appearance of some themes,
1064 but in practice it already fixes some themes.
1066 if (themeColor
== 1)
1068 wxUxThemeEngine::Get()->GetThemeColor(
1072 3802 /* FILLCOLOR */,
1076 wxColour
colour(GetRValue(themeColor
), GetGValue(themeColor
), GetBValue(themeColor
));
1080 #endif // wxUSE_UXTHEME
1082 return GetBackgroundColour();
1085 // ----------------------------------------------------------------------------
1086 // wxNotebook base class virtuals
1087 // ----------------------------------------------------------------------------
1089 #if wxUSE_CONSTRAINTS
1091 // override these 2 functions to do nothing: everything is done in OnSize
1093 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
1095 // don't set the sizes of the pages - their correct size is not yet known
1096 wxControl::SetConstraintSizes(false);
1099 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
1104 #endif // wxUSE_CONSTRAINTS
1106 // ----------------------------------------------------------------------------
1107 // wxNotebook Windows message handlers
1108 // ----------------------------------------------------------------------------
1110 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
1111 WXWORD pos
, WXHWND control
)
1113 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1118 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
1121 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
1123 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
1125 NMHDR
* hdr
= (NMHDR
*)lParam
;
1126 switch ( hdr
->code
) {
1128 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
1131 case TCN_SELCHANGING
:
1132 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
1136 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1139 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
1140 event
.SetOldSelection(m_nSelection
);
1141 event
.SetEventObject(this);
1142 event
.SetInt(idCtrl
);
1144 bool processed
= GetEventHandler()->ProcessEvent(event
);
1145 *result
= !event
.IsAllowed();
1149 #endif // wxUSE_NOTEBOOK