]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
renamed wxRect::Inside() to wxRect::Contains(), wxRect::Inside(wxRect) is too confusing
[wxWidgets.git] / src / msw / notebook.cpp
CommitLineData
88310e2e 1///////////////////////////////////////////////////////////////////////////////
2ddb4d13 2// Name: src/msw/notebook.cpp
88310e2e
VZ
3// Purpose: implementation of wxNotebook
4// Author: Vadim Zeitlin
907f37b3 5// Modified by:
88310e2e
VZ
6// Created: 11.06.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
88310e2e
VZ
10///////////////////////////////////////////////////////////////////////////////
11
88310e2e
VZ
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
1e6feb95 16 #pragma hdrstop
88310e2e
VZ
17#endif
18
1e6feb95
VZ
19#if wxUSE_NOTEBOOK
20
57bd4c60 21#include "wx/notebook.h"
e4db172a 22
88310e2e 23#ifndef WX_PRECOMP
57bd4c60
WS
24 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
25 #include "wx/string.h"
26 #include "wx/dc.h"
27 #include "wx/log.h"
28 #include "wx/event.h"
29 #include "wx/app.h"
30 #include "wx/dcclient.h"
31 #include "wx/dcmemory.h"
32 #include "wx/control.h"
88310e2e
VZ
33#endif // WX_PRECOMP
34
57bd4c60
WS
35#include "wx/imaglist.h"
36#include "wx/sysopt.h"
88310e2e 37
57bd4c60 38#include "wx/msw/private.h"
88310e2e 39
57bd4c60 40#include <windowsx.h>
85b43fbf
JS
41#include "wx/msw/winundef.h"
42
43#if wxUSE_UXTHEME
caf95d2a 44 #include "wx/msw/uxtheme.h"
85b43fbf
JS
45#endif
46
88310e2e
VZ
47// ----------------------------------------------------------------------------
48// macros
49// ----------------------------------------------------------------------------
50
51// check that the page index is valid
8d34bf5c 52#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
88310e2e 53
014ee083
VZ
54// you can set USE_NOTEBOOK_ANTIFLICKER to 0 for desktop Windows versions too
55// to disable code whih results in flicker-less notebook redrawing at the
56// expense of some extra GDI resource consumption
254fbd14 57#ifdef __WXWINCE__
014ee083
VZ
58 // notebooks are never resized under CE anyhow
59 #define USE_NOTEBOOK_ANTIFLICKER 0
254fbd14 60#else
014ee083 61 #define USE_NOTEBOOK_ANTIFLICKER 1
254fbd14
JS
62#endif
63
74b31181
VZ
64// ----------------------------------------------------------------------------
65// constants
66// ----------------------------------------------------------------------------
67
68// This is a work-around for missing defines in gcc-2.95 headers
69#ifndef TCS_RIGHT
70 #define TCS_RIGHT 0x0002
71#endif
72
73#ifndef TCS_VERTICAL
74 #define TCS_VERTICAL 0x0080
75#endif
76
77#ifndef TCS_BOTTOM
78 #define TCS_BOTTOM TCS_RIGHT
79#endif
80
014ee083
VZ
81// ----------------------------------------------------------------------------
82// global variables
83// ----------------------------------------------------------------------------
84
85#if USE_NOTEBOOK_ANTIFLICKER
86
87// the pointer to standard spin button wnd proc
88static WXFARPROC gs_wndprocNotebookSpinBtn = (WXFARPROC)NULL;
89
6143d3b6 90// the pointer to standard tab control wnd proc
e1cffcd6 91static WXFARPROC gs_wndprocNotebook = (WXFARPROC)NULL;
6143d3b6
VZ
92
93LRESULT APIENTRY _EXPORT wxNotebookWndProc(HWND hwnd,
94 UINT message,
95 WPARAM wParam,
96 LPARAM lParam);
97
014ee083
VZ
98#endif // USE_NOTEBOOK_ANTIFLICKER
99
88310e2e
VZ
100// ----------------------------------------------------------------------------
101// event table
102// ----------------------------------------------------------------------------
103
7ec69821 104#include "wx/listimpl.cpp"
51741307 105
259c43f6 106WX_DEFINE_LIST( wxNotebookPageInfoList )
51741307 107
2e4df4bf
VZ
108DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
109DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
110
d9317fd4 111BEGIN_EVENT_TABLE(wxNotebook, wxControl)
7ec69821 112 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxNotebook::OnSelChange)
9026ad85 113 EVT_SIZE(wxNotebook::OnSize)
88310e2e 114 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
014ee083
VZ
115
116#if USE_NOTEBOOK_ANTIFLICKER
117 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground)
118 EVT_PAINT(wxNotebook::OnPaint)
119#endif // USE_NOTEBOOK_ANTIFLICKER
d9317fd4 120END_EVENT_TABLE()
88310e2e 121
51596bcb 122#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
123WX_DEFINE_FLAGS( wxNotebookStyle )
124
3ff066a4 125wxBEGIN_FLAGS( wxNotebookStyle )
bc9fb572
JS
126 // new style border flags, we put them first to
127 // use them for streaming out
3ff066a4
SC
128 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
129 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
130 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
131 wxFLAGS_MEMBER(wxBORDER_RAISED)
132 wxFLAGS_MEMBER(wxBORDER_STATIC)
133 wxFLAGS_MEMBER(wxBORDER_NONE)
078cf5cb 134
bc9fb572 135 // old style border flags
3ff066a4
SC
136 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
137 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
138 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
139 wxFLAGS_MEMBER(wxRAISED_BORDER)
140 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 141 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
142
143 // standard window styles
3ff066a4
SC
144 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
145 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
146 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
147 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 148 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
149 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
150 wxFLAGS_MEMBER(wxVSCROLL)
151 wxFLAGS_MEMBER(wxHSCROLL)
152
153 wxFLAGS_MEMBER(wxNB_FIXEDWIDTH)
2ddb4d13
WS
154 wxFLAGS_MEMBER(wxBK_DEFAULT)
155 wxFLAGS_MEMBER(wxBK_TOP)
156 wxFLAGS_MEMBER(wxBK_LEFT)
157 wxFLAGS_MEMBER(wxBK_RIGHT)
158 wxFLAGS_MEMBER(wxBK_BOTTOM)
b554cf63
JS
159 wxFLAGS_MEMBER(wxNB_NOPAGETHEME)
160 wxFLAGS_MEMBER(wxNB_FLAT)
3ff066a4
SC
161
162wxEND_FLAGS( wxNotebookStyle )
51741307 163
51596bcb 164IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook, wxControl,"wx/notebook.h")
51741307
SC
165IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo, wxObject , "wx/notebook.h" )
166
3ff066a4 167wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo * , wxNotebookPageInfoList ) ;
51596bcb 168
f0a126fe
SC
169template<> void wxCollectionToVariantArray( wxNotebookPageInfoList const &theList, wxxVariantArray &value)
170{
0c6b0084 171 wxListCollectionToVariantArray<wxNotebookPageInfoList::compatibility_iterator>( theList , value ) ;
f0a126fe
SC
172}
173
3ff066a4
SC
174wxBEGIN_PROPERTIES_TABLE(wxNotebook)
175 wxEVENT_PROPERTY( PageChanging , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING , wxNotebookEvent )
176 wxEVENT_PROPERTY( PageChanged , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED , wxNotebookEvent )
c5ca409b 177
3ff066a4 178 wxPROPERTY_COLLECTION( PageInfos , wxNotebookPageInfoList , wxNotebookPageInfo* , AddPageInfo , GetPageInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
af498247 179 wxPROPERTY_FLAGS( WindowStyle , wxNotebookStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 180wxEND_PROPERTIES_TABLE()
51596bcb 181
3ff066a4
SC
182wxBEGIN_HANDLERS_TABLE(wxNotebook)
183wxEND_HANDLERS_TABLE()
51596bcb 184
078cf5cb 185wxCONSTRUCTOR_5( wxNotebook , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle)
51596bcb 186
51741307 187
3ff066a4 188wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo)
af498247 189 wxREADONLY_PROPERTY( Page , wxNotebookPage* , GetPage , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4
SC
190 wxREADONLY_PROPERTY( Text , wxString , GetText , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
191 wxREADONLY_PROPERTY( Selected , bool , GetSelected , false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
192 wxREADONLY_PROPERTY( ImageId , int , GetImageId , -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
193wxEND_PROPERTIES_TABLE()
51741307 194
3ff066a4
SC
195wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo)
196wxEND_HANDLERS_TABLE()
51741307 197
078cf5cb 198wxCONSTRUCTOR_4( wxNotebookPageInfo , wxNotebookPage* , Page , wxString , Text , bool , Selected , int , ImageId )
51741307 199
51596bcb 200#else
d9317fd4 201IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
51741307 202IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo, wxObject )
51596bcb 203#endif
d9317fd4 204IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
88310e2e
VZ
205
206// ============================================================================
207// implementation
208// ============================================================================
209
210// ----------------------------------------------------------------------------
211// wxNotebook construction
212// ----------------------------------------------------------------------------
213
51741307
SC
214const wxNotebookPageInfoList& wxNotebook::GetPageInfos() const
215{
216 wxNotebookPageInfoList* list = const_cast< wxNotebookPageInfoList* >( &m_pageInfos ) ;
217 WX_CLEAR_LIST( wxNotebookPageInfoList , *list ) ;
34a0c9f4 218 for( size_t i = 0 ; i < GetPageCount() ; ++i )
51741307
SC
219 {
220 wxNotebookPageInfo *info = new wxNotebookPageInfo() ;
34a0c9f4 221 info->Create( const_cast<wxNotebook*>(this)->GetPage(i) , GetPageText(i) , GetSelection() == int(i) , GetPageImage(i) ) ;
51741307
SC
222 list->Append( info ) ;
223 }
224 return m_pageInfos ;
225}
226
88310e2e
VZ
227// common part of all ctors
228void wxNotebook::Init()
229{
7ec69821
WS
230 m_imageList = NULL;
231 m_nSelection = -1;
caf95d2a
VZ
232
233#if wxUSE_UXTHEME
7ec69821 234 m_hbrBackground = NULL;
caf95d2a 235#endif // wxUSE_UXTHEME
dfb47d83
VZ
236
237#if USE_NOTEBOOK_ANTIFLICKER
7ec69821 238 m_hasSubclassedUpdown = false;
dfb47d83 239#endif // USE_NOTEBOOK_ANTIFLICKER
88310e2e
VZ
240}
241
242// default for dynamic class
243wxNotebook::wxNotebook()
244{
245 Init();
246}
247
248// the same arguments as for wxControl
249wxNotebook::wxNotebook(wxWindow *parent,
8b9518ee 250 wxWindowID id,
88310e2e
VZ
251 const wxPoint& pos,
252 const wxSize& size,
8b9518ee 253 long style,
88310e2e
VZ
254 const wxString& name)
255{
256 Init();
257
258 Create(parent, id, pos, size, style, name);
259}
260
261// Create() function
262bool wxNotebook::Create(wxWindow *parent,
8b9518ee 263 wxWindowID id,
88310e2e
VZ
264 const wxPoint& pos,
265 const wxSize& size,
8b9518ee 266 long style,
88310e2e
VZ
267 const wxString& name)
268{
895cc205
WS
269 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
270 {
271#if defined(__POCKETPC__)
272 style |= wxBK_BOTTOM | wxNB_FLAT;
273#else
274 style |= wxBK_TOP;
275#endif
276 }
277
45418059
JS
278#ifdef __WXWINCE__
279 // Not sure why, but without this style, there is no border
280 // around the notebook tabs.
281 if (style & wxNB_FLAT)
282 style |= wxBORDER_SUNKEN;
283#endif
c3732409 284
e4db172a 285#if !wxUSE_UXTHEME
7cc1ad29
JS
286 // ComCtl32 notebook tabs simply don't work unless they're on top if we have uxtheme, we can
287 // work around it later (after control creation), but if we don't have uxtheme, we have to clear
288 // those styles
df10208f
VZ
289 const int verComCtl32 = wxApp::GetComCtl32Version();
290 if ( verComCtl32 == 600 )
04eb05b0 291 {
5483756f 292 style &= ~(wxBK_BOTTOM | wxBK_LEFT | wxBK_RIGHT);
04eb05b0 293 }
7cc1ad29 294#endif //wxUSE_UXTHEME
078cf5cb 295
c1637c89
VZ
296 LPCTSTR className = WC_TABCONTROL;
297
6143d3b6 298#if USE_NOTEBOOK_ANTIFLICKER
c1637c89
VZ
299 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
300 // causes horrible flicker when resizing notebook, so get rid of it by
301 // using a class without these styles (but otherwise identical to it)
302 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) )
303 {
304 static ClassRegistrar s_clsNotebook;
305 if ( !s_clsNotebook.IsInitialized() )
306 {
307 // get a copy of standard class and modify it
308 WNDCLASS wc;
309
6143d3b6 310 if ( ::GetClassInfo(NULL, WC_TABCONTROL, &wc) )
c1637c89 311 {
6143d3b6
VZ
312 gs_wndprocNotebook =
313 wx_reinterpret_cast(WXFARPROC, wc.lpfnWndProc);
c1637c89
VZ
314 wc.lpszClassName = wxT("_wx_SysTabCtl32");
315 wc.style &= ~(CS_HREDRAW | CS_VREDRAW);
6143d3b6
VZ
316 wc.hInstance = wxGetInstance();
317 wc.lpfnWndProc = wxNotebookWndProc;
c1637c89
VZ
318 s_clsNotebook.Register(wc);
319 }
320 else
321 {
322 wxLogLastError(_T("GetClassInfoEx(SysTabCtl32)"));
323 }
324 }
325
326 // use our custom class if available but fall back to the standard
327 // notebook if we failed to register it
328 if ( s_clsNotebook.IsRegistered() )
329 {
330 // it's ok to use c_str() here as the static s_clsNotebook object
331 // has sufficiently long lifetime
332 className = s_clsNotebook.GetName().c_str();
333 }
334 }
6143d3b6 335#endif // USE_NOTEBOOK_ANTIFLICKER
c1637c89 336
6dd16e4f
VZ
337 if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL,
338 wxDefaultValidator, name) )
b8bdaa7c 339 return false;
88310e2e 340
c1637c89 341 if ( !MSWCreateControl(className, wxEmptyString, pos, size) )
b8bdaa7c 342 return false;
907f37b3 343
7dccdf81
VZ
344#if wxUSE_UXTHEME
345 if ( HasFlag(wxNB_NOPAGETHEME) ||
346 wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) )
f2b7be7a 347 {
7dccdf81 348 SetBackgroundColour(GetThemeBackgroundColour());
f2b7be7a 349 }
30556182
VZ
350 else // use themed background by default
351 {
352 // create backing store
353 UpdateBgBrush();
354 }
e4db172a 355
7cc1ad29
JS
356 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
357 // control is simply not rendered correctly), so we disable themes
358 // if possible, otherwise we simply clear the styles.
359 // It's probably not possible to have UXTHEME without ComCtl32 6 or better, but lets
360 // check it anyway.
361 const int verComCtl32 = wxApp::GetComCtl32Version();
e4db172a 362 if ( verComCtl32 == 600 )
7cc1ad29
JS
363 {
364 // check if we use themes at all -- if we don't, we're still okay
5483756f 365 if ( wxUxThemeEngine::GetIfActive() && (style & (wxBK_BOTTOM|wxBK_LEFT|wxBK_RIGHT)))
7cc1ad29
JS
366 {
367 wxUxThemeEngine::GetIfActive()->SetWindowTheme((HWND)this->GetHandle(), L"", L"");
368 SetBackgroundColour(GetThemeBackgroundColour()); //correct the background color for the new non-themed control
369 }
370 }
7dccdf81 371#endif // wxUSE_UXTHEME
b554cf63
JS
372
373 // Undocumented hack to get flat notebook style
374 // In fact, we should probably only do this in some
375 // curcumstances, i.e. if we know we will have a border
376 // at the bottom (the tab control doesn't draw it itself)
377#if defined(__POCKETPC__) || defined(__SMARTPHONE__)
378 if (HasFlag(wxNB_FLAT))
379 {
01c8cbf5 380 SendMessage(GetHwnd(), CCM_SETVERSION, COMCTL32_VERSION, 0);
b554cf63
JS
381 if (!m_hasBgCol)
382 SetBackgroundColour(*wxWHITE);
383 }
384#endif
b8bdaa7c 385 return true;
0df3fbd7
VZ
386}
387
388WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const
389{
390 WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle);
391
392 tabStyle |= WS_TABSTOP | TCS_TABS;
393
2b5f62a0 394 if ( style & wxNB_MULTILINE )
0df3fbd7
VZ
395 tabStyle |= TCS_MULTILINE;
396 if ( style & wxNB_FIXEDWIDTH )
397 tabStyle |= TCS_FIXEDWIDTH;
398
2ddb4d13 399 if ( style & wxBK_BOTTOM )
0df3fbd7 400 tabStyle |= TCS_RIGHT;
2ddb4d13 401 else if ( style & wxBK_LEFT )
0df3fbd7 402 tabStyle |= TCS_VERTICAL;
2ddb4d13 403 else if ( style & wxBK_RIGHT )
c3732409 404 tabStyle |= TCS_VERTICAL | TCS_RIGHT;
0df3fbd7
VZ
405
406 // ex style
407 if ( exstyle )
408 {
409 // note that we never want to have the default WS_EX_CLIENTEDGE style
410 // as it looks too ugly for the notebooks
411 *exstyle = 0;
412 }
413
414 return tabStyle;
88310e2e
VZ
415}
416
caf95d2a
VZ
417wxNotebook::~wxNotebook()
418{
419#if wxUSE_UXTHEME
420 if ( m_hbrBackground )
421 ::DeleteObject((HBRUSH)m_hbrBackground);
422#endif // wxUSE_UXTHEME
423}
424
88310e2e
VZ
425// ----------------------------------------------------------------------------
426// wxNotebook accessors
427// ----------------------------------------------------------------------------
07b8d7ec 428
8d34bf5c 429size_t wxNotebook::GetPageCount() const
88310e2e 430{
8ecd5de2
WS
431 // consistency check
432 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(GetHwnd()) );
88310e2e 433
8ecd5de2 434 return m_pages.Count();
88310e2e
VZ
435}
436
437int wxNotebook::GetRowCount() const
438{
8ecd5de2 439 return TabCtrl_GetRowCount(GetHwnd());
88310e2e
VZ
440}
441
8d34bf5c 442int wxNotebook::SetSelection(size_t nPage)
88310e2e 443{
8ecd5de2
WS
444 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") );
445
446 if ( int(nPage) != m_nSelection )
2b5f62a0 447 {
8ecd5de2
WS
448 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
449 event.SetSelection(nPage);
450 event.SetOldSelection(m_nSelection);
451 event.SetEventObject(this);
452 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
453 {
454 // program allows the page change
455 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
456 (void)GetEventHandler()->ProcessEvent(event);
2b5f62a0 457
8ecd5de2
WS
458 TabCtrl_SetCurSel(GetHwnd(), nPage);
459 }
2b5f62a0 460 }
88310e2e 461
8ecd5de2 462 return m_nSelection;
88310e2e
VZ
463}
464
8d34bf5c 465bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
88310e2e 466{
8ecd5de2 467 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
88310e2e 468
8ecd5de2
WS
469 TC_ITEM tcItem;
470 tcItem.mask = TCIF_TEXT;
471 tcItem.pszText = (wxChar *)strText.c_str();
88310e2e 472
88a01064
JS
473 if ( !HasFlag(wxNB_MULTILINE) )
474 return TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0;
475
476 // multiline - we need to set new page size if a line is added or removed
477 int rows = GetRowCount();
478 bool ret = TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0;
479
480 if ( ret && rows != GetRowCount() )
481 {
482 const wxRect r = GetPageSize();
483 const size_t count = m_pages.Count();
484 for ( size_t page = 0; page < count; page++ )
485 m_pages[page]->SetSize(r);
486 }
487
488 return ret;
88310e2e
VZ
489}
490
8d34bf5c 491wxString wxNotebook::GetPageText(size_t nPage) const
88310e2e 492{
8ecd5de2 493 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
88310e2e 494
8ecd5de2
WS
495 wxChar buf[256];
496 TC_ITEM tcItem;
497 tcItem.mask = TCIF_TEXT;
498 tcItem.pszText = buf;
499 tcItem.cchTextMax = WXSIZEOF(buf);
88310e2e 500
8ecd5de2
WS
501 wxString str;
502 if ( TabCtrl_GetItem(GetHwnd(), nPage, &tcItem) )
503 str = tcItem.pszText;
88310e2e 504
8ecd5de2 505 return str;
88310e2e
VZ
506}
507
8d34bf5c 508int wxNotebook::GetPageImage(size_t nPage) const
88310e2e 509{
8ecd5de2 510 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") );
88310e2e 511
8ecd5de2
WS
512 TC_ITEM tcItem;
513 tcItem.mask = TCIF_IMAGE;
88310e2e 514
8ecd5de2 515 return TabCtrl_GetItem(GetHwnd(), nPage, &tcItem) ? tcItem.iImage : wxNOT_FOUND;
88310e2e
VZ
516}
517
8d34bf5c 518bool wxNotebook::SetPageImage(size_t nPage, int nImage)
88310e2e 519{
8ecd5de2 520 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
88310e2e 521
8ecd5de2
WS
522 TC_ITEM tcItem;
523 tcItem.mask = TCIF_IMAGE;
524 tcItem.iImage = nImage;
88310e2e 525
8ecd5de2 526 return TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0;
88310e2e
VZ
527}
528
529void wxNotebook::SetImageList(wxImageList* imageList)
907f37b3 530{
8ecd5de2 531 wxNotebookBase::SetImageList(imageList);
07b8d7ec 532
8ecd5de2
WS
533 if ( imageList )
534 {
535 (void) TabCtrl_SetImageList(GetHwnd(), (HIMAGELIST)imageList->GetHIMAGELIST());
536 }
b656febd
VS
537}
538
d9506e77
VZ
539// ----------------------------------------------------------------------------
540// wxNotebook size settings
541// ----------------------------------------------------------------------------
542
c3732409
VZ
543wxRect wxNotebook::GetPageSize() const
544{
545 wxRect r;
546
547 RECT rc;
548 ::GetClientRect(GetHwnd(), &rc);
549
550 // This check is to work around a bug in TabCtrl_AdjustRect which will
628eae0b
VZ
551 // cause a crash on win2k or on XP with themes disabled if either
552 // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle
553 // is too small.
554 //
555 // The value of 20 is chosen arbitrarily but seems to work
556 if ( rc.right > 20 && rc.bottom > 20 )
c3732409 557 {
01c8cbf5 558 TabCtrl_AdjustRect(GetHwnd(), false, &rc);
c3732409
VZ
559
560 wxCopyRECTToRect(rc, r);
561 }
562
563 return r;
564}
565
d9506e77
VZ
566void wxNotebook::SetPageSize(const wxSize& size)
567{
568 // transform the page size into the notebook size
569 RECT rc;
570 rc.left =
571 rc.top = 0;
572 rc.right = size.x;
573 rc.bottom = size.y;
574
b8bdaa7c 575 TabCtrl_AdjustRect(GetHwnd(), true, &rc);
d9506e77
VZ
576
577 // and now set it
578 SetSize(rc.right - rc.left, rc.bottom - rc.top);
579}
580
581void wxNotebook::SetPadding(const wxSize& padding)
582{
583 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
584}
42e69d6b
VZ
585
586// Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
587// style.
588void wxNotebook::SetTabSize(const wxSize& sz)
589{
590 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
591}
592
2ce7af35
JS
593wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
594{
669c595d 595 // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP
2ce7af35 596 wxSize sizeTotal = sizePage;
078cf5cb 597
8b5d5223 598 wxSize tabSize;
669c595d 599 if ( GetPageCount() > 0 )
2ce7af35
JS
600 {
601 RECT rect;
669c595d 602 TabCtrl_GetItemRect(GetHwnd(), 0, &rect);
2ce7af35
JS
603 tabSize.x = rect.right - rect.left;
604 tabSize.y = rect.bottom - rect.top;
605 }
669c595d
VZ
606
607 // add an extra margin in both directions
608 const int MARGIN = 8;
609 if ( IsVertical() )
2ce7af35 610 {
669c595d
VZ
611 sizeTotal.x += MARGIN;
612 sizeTotal.y += tabSize.y + MARGIN;
2ce7af35 613 }
669c595d 614 else // horizontal layout
2ce7af35 615 {
669c595d
VZ
616 sizeTotal.x += tabSize.x + MARGIN;
617 sizeTotal.y += MARGIN;
2ce7af35
JS
618 }
619
620 return sizeTotal;
621}
622
2015f2b3
VZ
623void wxNotebook::AdjustPageSize(wxNotebookPage *page)
624{
625 wxCHECK_RET( page, _T("NULL page in wxNotebook::AdjustPageSize") );
626
c3732409
VZ
627 const wxRect r = GetPageSize();
628 if ( !r.IsEmpty() )
14a6b6e5 629 {
c3732409 630 page->SetSize(r);
14a6b6e5 631 }
2015f2b3
VZ
632}
633
88310e2e
VZ
634// ----------------------------------------------------------------------------
635// wxNotebook operations
636// ----------------------------------------------------------------------------
637
621793f4 638// remove one page from the notebook, without deleting
8d34bf5c 639wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
621793f4 640{
df7145da
VZ
641 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
642 if ( !pageRemoved )
643 return NULL;
621793f4 644
01c8cbf5 645 TabCtrl_DeleteItem(GetHwnd(), nPage);
621793f4 646
df7145da
VZ
647 if ( m_pages.IsEmpty() )
648 {
649 // no selection any more, the notebook becamse empty
650 m_nSelection = -1;
651 }
652 else // notebook still not empty
653 {
01c8cbf5 654 int selNew = TabCtrl_GetCurSel(GetHwnd());
623f5f70 655 if (selNew != -1)
df7145da 656 {
623f5f70 657 // No selection change, just refresh the current selection.
078cf5cb
WS
658 // Because it could be that the slection index changed
659 // we need to update it.
623f5f70
JS
660 // Note: this does not mean the selection it self changed.
661 m_nSelection = selNew;
662 m_pages[m_nSelection]->Refresh();
df7145da 663 }
623f5f70 664 else if (int(nPage) == m_nSelection)
43a997b6 665 {
623f5f70 666 // The selection was deleted.
078cf5cb 667
623f5f70
JS
668 // Determine new selection.
669 if (m_nSelection == int(GetPageCount()))
670 selNew = m_nSelection - 1;
671 else
672 selNew = m_nSelection;
078cf5cb 673
43a997b6
VZ
674 // m_nSelection must be always valid so reset it before calling
675 // SetSelection()
676 m_nSelection = -1;
677 SetSelection(selNew);
678 }
623f5f70
JS
679 else
680 {
681 wxFAIL; // Windows did not behave ok.
682 }
df7145da 683 }
47f12f58 684
df7145da 685 return pageRemoved;
621793f4
JS
686}
687
88310e2e
VZ
688// remove all pages
689bool wxNotebook::DeleteAllPages()
690{
7ec69821
WS
691 size_t nPageCount = GetPageCount();
692 size_t nPage;
693 for ( nPage = 0; nPage < nPageCount; nPage++ )
694 delete m_pages[nPage];
88310e2e 695
7ec69821 696 m_pages.Clear();
88310e2e 697
7ec69821 698 TabCtrl_DeleteAllItems(GetHwnd());
907f37b3 699
7ec69821 700 m_nSelection = -1;
47f12f58 701
7ec69821
WS
702 InvalidateBestSize();
703 return true;
88310e2e
VZ
704}
705
88310e2e 706// same as AddPage() but does it at given position
8d34bf5c 707bool wxNotebook::InsertPage(size_t nPage,
88310e2e
VZ
708 wxNotebookPage *pPage,
709 const wxString& strText,
710 bool bSelect,
711 int imageId)
712{
b8bdaa7c
VZ
713 wxCHECK_MSG( pPage != NULL, false, _T("NULL page in wxNotebook::InsertPage") );
714 wxCHECK_MSG( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false,
22f3361e 715 _T("invalid index in wxNotebook::InsertPage") );
88310e2e 716
efa14cf2
VZ
717 wxASSERT_MSG( pPage->GetParent() == this,
718 _T("notebook pages must have notebook as parent") );
43427087 719
22f3361e
VZ
720 // add a new tab to the control
721 // ----------------------------
58a8ab88 722
22f3361e
VZ
723 // init all fields to 0
724 TC_ITEM tcItem;
725 wxZeroMemory(tcItem);
58a8ab88 726
22f3361e
VZ
727 // set the image, if any
728 if ( imageId != -1 )
729 {
730 tcItem.mask |= TCIF_IMAGE;
731 tcItem.iImage = imageId;
732 }
88310e2e 733
22f3361e 734 // and the text
8b5d5223 735 if ( !strText.empty() )
22f3361e
VZ
736 {
737 tcItem.mask |= TCIF_TEXT;
738 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
739 }
43427087 740
e830a6a6
RD
741 // hide the page: unless it is selected, it shouldn't be shown (and if it
742 // is selected it will be shown later)
743 HWND hwnd = GetWinHwnd(pPage);
744 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
745
746 // this updates internal flag too -- otherwise it would get out of sync
747 // with the real state
748 pPage->Show(false);
749
750
22f3361e
VZ
751 // fit the notebook page to the tab control's display area: this should be
752 // done before adding it to the notebook or TabCtrl_InsertItem() will
753 // change the notebooks size itself!
2015f2b3 754 AdjustPageSize(pPage);
43427087 755
22f3361e 756 // finally do insert it
01c8cbf5 757 if ( TabCtrl_InsertItem(GetHwnd(), nPage, &tcItem) == -1 )
2015f2b3 758 {
22f3361e 759 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
88310e2e 760
b8bdaa7c 761 return false;
22f3361e 762 }
88310e2e 763
22f3361e
VZ
764 // succeeded: save the pointer to the page
765 m_pages.Insert(pPage, nPage);
42e69d6b 766
56b9925b
VZ
767 // we may need to adjust the size again if the notebook size changed:
768 // normally this only happens for the first page we add (the tabs which
769 // hadn't been there before are now shown) but for a multiline notebook it
770 // can happen for any page at all as a new row could have been started
771 if ( m_pages.GetCount() == 1 || HasFlag(wxNB_MULTILINE) )
2015f2b3
VZ
772 {
773 AdjustPageSize(pPage);
774 }
775
22f3361e
VZ
776 // now deal with the selection
777 // ---------------------------
778
779 // if the inserted page is before the selected one, we must update the
780 // index of the selected page
34a0c9f4 781 if ( int(nPage) <= m_nSelection )
22f3361e
VZ
782 {
783 // one extra page added
784 m_nSelection++;
785 }
786
787 // some page should be selected: either this one or the first one if there
788 // is still no selection
789 int selNew = -1;
790 if ( bSelect )
791 selNew = nPage;
792 else if ( m_nSelection == -1 )
793 selNew = 0;
794
795 if ( selNew != -1 )
796 SetSelection(selNew);
797
37144cf0 798 InvalidateBestSize();
25057aba 799
b8bdaa7c 800 return true;
88310e2e
VZ
801}
802
e450aa69 803int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
ef094fa0
JS
804{
805 TC_HITTESTINFO hitTestInfo;
806 hitTestInfo.pt.x = pt.x;
807 hitTestInfo.pt.y = pt.y;
e450aa69 808 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
ef094fa0 809
e450aa69
VZ
810 if ( flags )
811 {
812 *flags = 0;
813
814 if ((hitTestInfo.flags & TCHT_NOWHERE) == TCHT_NOWHERE)
9804d540 815 *flags |= wxBK_HITTEST_NOWHERE;
e450aa69 816 if ((hitTestInfo.flags & TCHT_ONITEM) == TCHT_ONITEM)
9804d540 817 *flags |= wxBK_HITTEST_ONITEM;
e450aa69 818 if ((hitTestInfo.flags & TCHT_ONITEMICON) == TCHT_ONITEMICON)
9804d540 819 *flags |= wxBK_HITTEST_ONICON;
e450aa69 820 if ((hitTestInfo.flags & TCHT_ONITEMLABEL) == TCHT_ONITEMLABEL)
9804d540 821 *flags |= wxBK_HITTEST_ONLABEL;
d0a84b63 822 if ( item == wxNOT_FOUND && GetPageSize().Inside(pt) )
9804d540 823 *flags |= wxBK_HITTEST_ONPAGE;
e450aa69 824 }
ef094fa0
JS
825
826 return item;
827}
828
014ee083
VZ
829// ----------------------------------------------------------------------------
830// flicker-less notebook redraw
831// ----------------------------------------------------------------------------
832
833#if USE_NOTEBOOK_ANTIFLICKER
834
835// wnd proc for the spin button
836LRESULT APIENTRY _EXPORT wxNotebookSpinBtnWndProc(HWND hwnd,
837 UINT message,
838 WPARAM wParam,
839 LPARAM lParam)
840{
841 if ( message == WM_ERASEBKGND )
842 return 0;
843
844 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn,
845 hwnd, message, wParam, lParam);
846}
847
6143d3b6
VZ
848LRESULT APIENTRY _EXPORT wxNotebookWndProc(HWND hwnd,
849 UINT message,
850 WPARAM wParam,
851 LPARAM lParam)
852{
853 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook,
854 hwnd, message, wParam, lParam);
855}
856
014ee083
VZ
857void wxNotebook::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
858{
859 // do nothing here
860}
861
862void wxNotebook::OnPaint(wxPaintEvent& WXUNUSED(event))
863{
864 wxPaintDC dc(this);
865 wxMemoryDC memdc;
866 RECT rc;
867 ::GetClientRect(GetHwnd(), &rc);
868 wxBitmap bmp(rc.right, rc.bottom);
869 memdc.SelectObject(bmp);
870
871 // if there is no special brush just use the solid background colour
64c288fa 872#if wxUSE_UXTHEME
014ee083 873 HBRUSH hbr = (HBRUSH)m_hbrBackground;
64c288fa
JS
874#else
875 HBRUSH hbr = 0;
e4db172a 876#endif
e32703a9 877 wxBrush brush;
014ee083 878 if ( !hbr )
e32703a9
VZ
879 {
880 brush = wxBrush(GetBackgroundColour());
881 hbr = GetHbrushOf(brush);
882 }
014ee083
VZ
883
884 ::FillRect(GetHdcOf(memdc), &rc, hbr);
885
886 MSWDefWindowProc(WM_PAINT, (WPARAM)memdc.GetHDC(), 0);
887
888 dc.Blit(0, 0, rc.right, rc.bottom, &memdc, 0, 0);
889}
890
891#endif // USE_NOTEBOOK_ANTIFLICKER
e450aa69 892
88310e2e
VZ
893// ----------------------------------------------------------------------------
894// wxNotebook callbacks
895// ----------------------------------------------------------------------------
896
9026ad85 897void wxNotebook::OnSize(wxSizeEvent& event)
88310e2e 898{
7dccdf81 899 if ( GetPageCount() == 0 )
f7eaa62f
JS
900 {
901 // Prevents droppings on resize, but does cause some flicker
902 // when there are no pages.
2aa24b60 903 Refresh();
f7eaa62f
JS
904 event.Skip();
905 return;
906 }
254fbd14
JS
907#ifndef __WXWINCE__
908 else
909 {
910 // Without this, we can sometimes get droppings at the edges
911 // of a notebook, for example a notebook in a splitter window.
912 // This needs to be reconciled with the RefreshRect calls
913 // at the end of this function, which weren't enough to prevent
914 // the droppings.
01c8cbf5 915
254fbd14
JS
916 wxSize sz = GetClientSize();
917
918 // Refresh right side
919 wxRect rect(sz.x-4, 0, 4, sz.y);
920 RefreshRect(rect);
01c8cbf5 921
254fbd14
JS
922 // Refresh bottom side
923 rect = wxRect(0, sz.y-4, sz.x, 4);
924 RefreshRect(rect);
01c8cbf5 925
254fbd14
JS
926 // Refresh left side
927 rect = wxRect(0, 0, 4, sz.y);
928 RefreshRect(rect);
929 }
014ee083 930#endif // !__WXWINCE__
f7eaa62f 931
c1637c89 932 // fit all the notebook pages to the tab control's display area
56b9925b 933
c1637c89
VZ
934 RECT rc;
935 rc.left = rc.top = 0;
936 GetSize((int *)&rc.right, (int *)&rc.bottom);
56b9925b 937
c1637c89
VZ
938 // save the total size, we'll use it below
939 int widthNbook = rc.right - rc.left,
940 heightNbook = rc.bottom - rc.top;
941
942 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
943 // returns completely false values for multiline tab controls after the tabs
944 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
945 //
946 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
947 //
948 // and the only work around I could find was this ugly hack... without it
949 // simply toggling the "multiline" checkbox in the notebook sample resulted
950 // in a noticeable page displacement
951 if ( HasFlag(wxNB_MULTILINE) )
952 {
953 // avoid an infinite recursion: we get another notification too!
954 static bool s_isInOnSize = false;
4b7f2165 955
c1637c89
VZ
956 if ( !s_isInOnSize )
957 {
958 s_isInOnSize = true;
959 SendMessage(GetHwnd(), WM_SIZE, SIZE_RESTORED,
960 MAKELPARAM(rc.right, rc.bottom));
961 s_isInOnSize = false;
962 }
963 }
b5c3b538 964
e2f6f8da
RD
965#if wxUSE_UXTHEME
966 // background bitmap size has changed, update the brush using it too
967 UpdateBgBrush();
968#endif // wxUSE_UXTHEME
969
01c8cbf5 970 TabCtrl_AdjustRect(GetHwnd(), false, &rc);
c1637c89
VZ
971
972 int width = rc.right - rc.left,
973 height = rc.bottom - rc.top;
974 size_t nCount = m_pages.Count();
975 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
976 wxNotebookPage *pPage = m_pages[nPage];
977 pPage->SetSize(rc.left, rc.top, width, height);
978 }
979
980
981 // unless we had already repainted everything, we now need to refresh
982 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) )
983 {
984 // invalidate areas not covered by pages
985 RefreshRect(wxRect(0, 0, widthNbook, rc.top), false);
986 RefreshRect(wxRect(0, rc.top, rc.left, height), false);
987 RefreshRect(wxRect(0, rc.bottom, widthNbook, heightNbook - rc.bottom),
988 false);
3550706d 989 RefreshRect(wxRect(rc.right, rc.top, widthNbook - rc.right, height),
c1637c89
VZ
990 false);
991 }
992
014ee083
VZ
993#if USE_NOTEBOOK_ANTIFLICKER
994 // subclass the spin control used by the notebook to scroll pages to
995 // prevent it from flickering on resize
dfb47d83 996 if ( !m_hasSubclassedUpdown )
014ee083
VZ
997 {
998 // iterate over all child windows to find spin button
999 for ( HWND child = ::GetWindow(GetHwnd(), GW_CHILD);
1000 child;
1001 child = ::GetWindow(child, GW_HWNDNEXT) )
1002 {
1003 wxWindow *childWindow = wxFindWinFromHandle((WXHWND)child);
1004
1005 // see if it exists, if no wxWindow found then assume it's the spin
1006 // btn
1007 if ( !childWindow )
1008 {
1009 // subclass the spin button to override WM_ERASEBKGND
dfb47d83
VZ
1010 if ( !gs_wndprocNotebookSpinBtn )
1011 gs_wndprocNotebookSpinBtn = (WXFARPROC)wxGetWindowProc(child);
014ee083
VZ
1012
1013 wxSetWindowProc(child, wxNotebookSpinBtnWndProc);
dfb47d83 1014 m_hasSubclassedUpdown = true;
014ee083
VZ
1015 break;
1016 }
1017 }
1018 }
1019#endif // USE_NOTEBOOK_ANTIFLICKER
1020
c1637c89 1021 event.Skip();
88310e2e
VZ
1022}
1023
1024void wxNotebook::OnSelChange(wxNotebookEvent& event)
1025{
1026 // is it our tab control?
1027 if ( event.GetEventObject() == this )
5d1d2d46 1028 {
5d1d2d46
VZ
1029 int sel = event.GetOldSelection();
1030 if ( sel != -1 )
b8bdaa7c 1031 m_pages[sel]->Show(false);
0398b1d6 1032
5d1d2d46
VZ
1033 sel = event.GetSelection();
1034 if ( sel != -1 )
1035 {
1e6feb95 1036 wxNotebookPage *pPage = m_pages[sel];
b8bdaa7c 1037 pPage->Show(true);
e1cffcd6 1038 }
2ff53fd3 1039
e1cffcd6
VZ
1040 // Changing the page should give the focus to it but, as per bug report
1041 // http://sf.net/tracker/index.php?func=detail&aid=1150659&group_id=9863&atid=109863,
1042 // we should not set the focus to it directly since it erroneously
1043 // selects radio buttons and breaks keyboard handling for a notebook's
1044 // scroll buttons. So give focus to the notebook and not the page.
1d5b3bf0 1045
e1cffcd6
VZ
1046 // but don't do this is the notebook is hidden
1047 if ( ::IsWindowVisible(GetHwnd()) )
1048 SetFocus();
0398b1d6 1049
5d1d2d46
VZ
1050 m_nSelection = sel;
1051 }
88310e2e
VZ
1052
1053 // we want to give others a chance to process this message as well
1054 event.Skip();
1055}
1056
88310e2e
VZ
1057void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
1058{
d9506e77
VZ
1059 if ( event.IsWindowChange() ) {
1060 // change pages
1061 AdvanceSelection(event.GetDirection());
1062 }
1063 else {
b8bdaa7c 1064 // we get this event in 3 cases
d9506e77
VZ
1065 //
1066 // a) one of our pages might have generated it because the user TABbed
1067 // out from it in which case we should propagate the event upwards and
1068 // our parent will take care of setting the focus to prev/next sibling
1069 //
1070 // or
1071 //
1072 // b) the parent panel wants to give the focus to us so that we
1073 // forward it to our selected page. We can't deal with this in
1074 // OnSetFocus() because we don't know which direction the focus came
1075 // from in this case and so can't choose between setting the focus to
1076 // first or last panel child
b8bdaa7c
VZ
1077 //
1078 // or
1079 //
1080 // c) we ourselves (see MSWTranslateMessage) generated the event
1081 //
1082 wxWindow * const parent = GetParent();
1083
3c99602b
MB
1084 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
1085 const bool isFromParent = event.GetEventObject() == (wxObject*) parent;
1086 const bool isFromSelf = event.GetEventObject() == (wxObject*) this;
b8bdaa7c
VZ
1087
1088 if ( isFromParent || isFromSelf )
d9506e77 1089 {
b8bdaa7c
VZ
1090 // no, it doesn't come from child, case (b) or (c): forward to a
1091 // page but only if direction is backwards (TAB) or from ourselves,
1092 if ( m_nSelection != -1 &&
1093 (!event.GetDirection() || isFromSelf) )
d9506e77
VZ
1094 {
1095 // so that the page knows that the event comes from it's parent
1096 // and is being propagated downwards
1097 event.SetEventObject(this);
1098
1e6feb95 1099 wxWindow *page = m_pages[m_nSelection];
d9506e77
VZ
1100 if ( !page->GetEventHandler()->ProcessEvent(event) )
1101 {
1102 page->SetFocus();
1103 }
1104 //else: page manages focus inside it itself
1105 }
b8bdaa7c 1106 else // otherwise set the focus to the notebook itself
d9506e77 1107 {
d9506e77
VZ
1108 SetFocus();
1109 }
1110 }
1111 else
1112 {
b8bdaa7c
VZ
1113 // it comes from our child, case (a), pass to the parent, but only
1114 // if the direction is forwards. Otherwise set the focus to the
1115 // notebook itself. The notebook is always the 'first' control of a
1116 // page.
1117 if ( !event.GetDirection() )
1118 {
1119 SetFocus();
1120 }
1121 else if ( parent )
1122 {
d9506e77
VZ
1123 event.SetCurrentFocus(this);
1124 parent->GetEventHandler()->ProcessEvent(event);
1125 }
1126 }
88310e2e 1127 }
88310e2e
VZ
1128}
1129
caf95d2a
VZ
1130#if wxUSE_UXTHEME
1131
c3732409 1132bool wxNotebook::DoDrawBackground(WXHDC hDC, wxWindow *child)
caf95d2a 1133{
c3732409
VZ
1134 wxUxThemeHandle theme(child ? child : this, L"TAB");
1135 if ( !theme )
1136 return false;
1137
1138 // get the notebook client rect (we're not interested in drawing tabs
1139 // themselves)
1140 wxRect r = GetPageSize();
1141 if ( r.IsEmpty() )
1142 return false;
1143
c4a95f6f 1144 RECT rc;
c3732409
VZ
1145 wxCopyRectToRECT(r, rc);
1146
1147 // map rect to the coords of the window we're drawing in
1148 if ( child )
1149 ::MapWindowPoints(GetHwnd(), GetHwndOf(child), (POINT *)&rc, 2);
1150
2ff53fd3
JS
1151 // we have the content area (page size), but we need to draw all of the
1152 // background for it to be aligned correctly
1153 wxUxThemeEngine::Get()->GetThemeBackgroundExtent
1154 (
1155 theme,
1156 (HDC) hDC,
1157 9 /* TABP_PANE */,
1158 0,
1159 &rc,
1160 &rc
1161 );
c3732409
VZ
1162 wxUxThemeEngine::Get()->DrawThemeBackground
1163 (
1164 theme,
92199f4c 1165 (HDC) hDC,
c3732409
VZ
1166 9 /* TABP_PANE */,
1167 0,
1168 &rc,
1169 NULL
1170 );
1171
1172 return true;
1173}
de359565 1174
c3732409
VZ
1175WXHBRUSH wxNotebook::QueryBgBitmap()
1176{
1177 wxRect r = GetPageSize();
1178 if ( r.IsEmpty() )
1179 return 0;
c4a95f6f
VZ
1180
1181 WindowHDC hDC(GetHwnd());
1182 MemoryHDC hDCMem(hDC);
c3732409 1183 CompatibleBitmap hBmp(hDC, r.x + r.width, r.y + r.height);
c4a95f6f
VZ
1184
1185 SelectInHDC selectBmp(hDCMem, hBmp);
caf95d2a 1186
c3732409
VZ
1187 if ( !DoDrawBackground((WXHDC)(HDC)hDCMem) )
1188 return 0;
0f770734 1189
de359565 1190 return (WXHBRUSH)::CreatePatternBrush(hBmp);
c4a95f6f 1191}
caf95d2a 1192
c4a95f6f
VZ
1193void wxNotebook::UpdateBgBrush()
1194{
1195 if ( m_hbrBackground )
1196 ::DeleteObject((HBRUSH)m_hbrBackground);
caf95d2a 1197
c4a95f6f
VZ
1198 if ( !m_hasBgCol && wxUxThemeEngine::GetIfActive() )
1199 {
de359565 1200 m_hbrBackground = QueryBgBitmap();
caf95d2a 1201 }
c3732409 1202 else // no themes or we've got user-defined solid colour
caf95d2a
VZ
1203 {
1204 m_hbrBackground = NULL;
1205 }
1206}
1207
2bae4332 1208WXHBRUSH wxNotebook::MSWGetBgBrushForChild(WXHDC hDC, WXHWND hWnd)
caf95d2a 1209{
caf95d2a
VZ
1210 if ( m_hbrBackground )
1211 {
1212 // before drawing with the background brush, we need to position it
1213 // correctly
caf95d2a 1214 RECT rc;
2bae4332 1215 ::GetWindowRect((HWND)hWnd, &rc);
caf95d2a
VZ
1216
1217 ::MapWindowPoints(NULL, GetHwnd(), (POINT *)&rc, 1);
1218
5c836c46 1219 if ( !::SetBrushOrgEx((HDC)hDC, -rc.left, -rc.top, NULL) )
caf95d2a
VZ
1220 {
1221 wxLogLastError(_T("SetBrushOrgEx(notebook bg brush)"));
1222 }
c4a95f6f
VZ
1223
1224 return m_hbrBackground;
5c836c46
VZ
1225 }
1226
2bae4332 1227 return wxNotebookBase::MSWGetBgBrushForChild(hDC, hWnd);
5c836c46 1228}
caf95d2a 1229
c3732409 1230bool wxNotebook::MSWPrintChild(WXHDC hDC, wxWindow *child)
07c19327 1231{
c3732409
VZ
1232 // solid background colour overrides themed background drawing
1233 if ( !UseBgCol() && DoDrawBackground(hDC, child) )
1234 return true;
de359565 1235
4dab5279
JS
1236 // If we're using a solid colour (for example if we've switched off
1237 // theming for this notebook), paint it
1238 if (UseBgCol())
1239 {
1240 wxRect r = GetPageSize();
1241 if ( r.IsEmpty() )
1242 return false;
1243
1244 RECT rc;
1245 wxCopyRectToRECT(r, rc);
1246
1247 // map rect to the coords of the window we're drawing in
1248 if ( child )
1249 ::MapWindowPoints(GetHwnd(), GetHwndOf(child), (POINT *)&rc, 2);
1250
1251 wxBrush brush(GetBackgroundColour());
1252 HBRUSH hbr = GetHbrushOf(brush);
01c8cbf5 1253
4dab5279
JS
1254 ::FillRect((HDC) hDC, &rc, hbr);
1255
1256 return true;
1257 }
1258
c3732409 1259 return wxNotebookBase::MSWPrintChild(hDC, child);
07c19327
VZ
1260}
1261
caf95d2a
VZ
1262#endif // wxUSE_UXTHEME
1263
25057aba
JS
1264// Windows only: attempts to get colour for UX theme page background
1265wxColour wxNotebook::GetThemeBackgroundColour() const
1266{
1267#if wxUSE_UXTHEME
1268 if (wxUxThemeEngine::Get())
1269 {
1270 wxUxThemeHandle hTheme((wxNotebook*) this, L"TAB");
1271 if (hTheme)
1272 {
1273 // This is total guesswork.
1274 // See PlatformSDK\Include\Tmschema.h for values
1275 COLORREF themeColor;
1276 wxUxThemeEngine::Get()->GetThemeColor(
1277 hTheme,
1278 10 /* TABP_BODY */,
1279 1 /* NORMAL */,
1280 3821 /* FILLCOLORHINT */,
1281 &themeColor);
1282
1283 /*
1284 [DS] Workaround for WindowBlinds:
1285 Some themes return a near black theme color using FILLCOLORHINT,
1286 this makes notebook pages have an ugly black background and makes
1287 text (usually black) unreadable. Retry again with FILLCOLOR.
1288
1289 This workaround potentially breaks appearance of some themes,
1290 but in practice it already fixes some themes.
1291 */
1292 if (themeColor == 1)
1293 {
1294 wxUxThemeEngine::Get()->GetThemeColor(
1295 hTheme,
1296 10 /* TABP_BODY */,
1297 1 /* NORMAL */,
1298 3802 /* FILLCOLOR */,
1299 &themeColor);
1300 }
1301
7dccdf81 1302 return wxRGBToColour(themeColor);
25057aba
JS
1303 }
1304 }
1305#endif // wxUSE_UXTHEME
1306
1307 return GetBackgroundColour();
1308}
1309
88310e2e
VZ
1310// ----------------------------------------------------------------------------
1311// wxNotebook base class virtuals
1312// ----------------------------------------------------------------------------
b5c3b538 1313
0b481c72
VZ
1314#if wxUSE_CONSTRAINTS
1315
b5c3b538
VZ
1316// override these 2 functions to do nothing: everything is done in OnSize
1317
4b7f2165 1318void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
b5c3b538
VZ
1319{
1320 // don't set the sizes of the pages - their correct size is not yet known
b8bdaa7c 1321 wxControl::SetConstraintSizes(false);
b5c3b538
VZ
1322}
1323
4b7f2165 1324bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
b5c3b538 1325{
b8bdaa7c 1326 return true;
b5c3b538
VZ
1327}
1328
0b481c72
VZ
1329#endif // wxUSE_CONSTRAINTS
1330
0df3fbd7
VZ
1331// ----------------------------------------------------------------------------
1332// wxNotebook Windows message handlers
1333// ----------------------------------------------------------------------------
1334
1335bool wxNotebook::MSWOnScroll(int orientation, WXWORD nSBCode,
1336 WXWORD pos, WXHWND control)
1337{
1338 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1339 // up-down control
1340 if ( control )
b8bdaa7c 1341 return false;
0df3fbd7
VZ
1342
1343 return wxNotebookBase::MSWOnScroll(orientation, nSBCode, pos, control);
1344}
1345
a23fd0e1 1346bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 1347{
93a19f17 1348 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
1349
1350 NMHDR* hdr = (NMHDR *)lParam;
1351 switch ( hdr->code ) {
1352 case TCN_SELCHANGE:
1353 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
1354 break;
1355
1356 case TCN_SELCHANGING:
1357 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
1358 break;
1359
fd3f686c 1360 default:
a23fd0e1 1361 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
1362 }
1363
01c8cbf5 1364 event.SetSelection(TabCtrl_GetCurSel(GetHwnd()));
93a19f17 1365 event.SetOldSelection(m_nSelection);
88310e2e 1366 event.SetEventObject(this);
a23fd0e1 1367 event.SetInt(idCtrl);
88310e2e 1368
fd3f686c
VZ
1369 bool processed = GetEventHandler()->ProcessEvent(event);
1370 *result = !event.IsAllowed();
1371 return processed;
88310e2e
VZ
1372}
1373
1e6feb95 1374#endif // wxUSE_NOTEBOOK