]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
also reset errno iin ToDouble()
[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 230 m_imageList = NULL;
5482ee07 231 m_nSelection = wxNOT_FOUND;
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
a570e8f8 446 if ( m_nSelection == wxNOT_FOUND || nPage != (size_t)m_nSelection )
2b5f62a0 447 {
a570e8f8 448 if ( SendPageChangingEvent(nPage) )
8ecd5de2
WS
449 {
450 // program allows the page change
a570e8f8 451 SendPageChangedEvent(m_nSelection, nPage);
2b5f62a0 452
a570e8f8 453 TabCtrl_SetCurSel(GetHwnd(), nPage);
8ecd5de2 454 }
2b5f62a0 455 }
88310e2e 456
8ecd5de2 457 return m_nSelection;
88310e2e
VZ
458}
459
5482ee07 460void wxNotebook::UpdateSelection(int selNew)
1d6fcbcc 461{
5482ee07 462 if ( m_nSelection != wxNOT_FOUND )
1d6fcbcc
VZ
463 m_pages[m_nSelection]->Show(false);
464
5482ee07 465 if ( selNew != wxNOT_FOUND )
1d6fcbcc 466 {
5482ee07 467 wxNotebookPage *pPage = m_pages[selNew];
1d6fcbcc
VZ
468 pPage->Show(true);
469 }
470
471 // Changing the page should give the focus to it but, as per bug report
472 // http://sf.net/tracker/index.php?func=detail&aid=1150659&group_id=9863&atid=109863,
473 // we should not set the focus to it directly since it erroneously
474 // selects radio buttons and breaks keyboard handling for a notebook's
475 // scroll buttons. So give focus to the notebook and not the page.
476
477 // but don't do this is the notebook is hidden
478 if ( ::IsWindowVisible(GetHwnd()) )
479 SetFocus();
480
5482ee07 481 m_nSelection = selNew;
1d6fcbcc
VZ
482}
483
484int wxNotebook::ChangeSelection(size_t nPage)
485{
486 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") );
487
5482ee07 488 if ( m_nSelection == wxNOT_FOUND || nPage != (size_t)m_nSelection )
1d6fcbcc
VZ
489 {
490 TabCtrl_SetCurSel(GetHwnd(), nPage);
491
492 UpdateSelection(nPage);
493 }
494
495 return m_nSelection;
496}
497
8d34bf5c 498bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
88310e2e 499{
8ecd5de2 500 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
88310e2e 501
8ecd5de2
WS
502 TC_ITEM tcItem;
503 tcItem.mask = TCIF_TEXT;
504 tcItem.pszText = (wxChar *)strText.c_str();
88310e2e 505
88a01064
JS
506 if ( !HasFlag(wxNB_MULTILINE) )
507 return TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0;
508
509 // multiline - we need to set new page size if a line is added or removed
510 int rows = GetRowCount();
511 bool ret = TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0;
512
513 if ( ret && rows != GetRowCount() )
514 {
515 const wxRect r = GetPageSize();
516 const size_t count = m_pages.Count();
517 for ( size_t page = 0; page < count; page++ )
518 m_pages[page]->SetSize(r);
519 }
520
521 return ret;
88310e2e
VZ
522}
523
8d34bf5c 524wxString wxNotebook::GetPageText(size_t nPage) const
88310e2e 525{
8ecd5de2 526 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
88310e2e 527
8ecd5de2
WS
528 wxChar buf[256];
529 TC_ITEM tcItem;
530 tcItem.mask = TCIF_TEXT;
531 tcItem.pszText = buf;
532 tcItem.cchTextMax = WXSIZEOF(buf);
88310e2e 533
8ecd5de2
WS
534 wxString str;
535 if ( TabCtrl_GetItem(GetHwnd(), nPage, &tcItem) )
536 str = tcItem.pszText;
88310e2e 537
8ecd5de2 538 return str;
88310e2e
VZ
539}
540
8d34bf5c 541int wxNotebook::GetPageImage(size_t nPage) const
88310e2e 542{
8ecd5de2 543 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") );
88310e2e 544
8ecd5de2
WS
545 TC_ITEM tcItem;
546 tcItem.mask = TCIF_IMAGE;
88310e2e 547
5482ee07
VZ
548 return TabCtrl_GetItem(GetHwnd(), nPage, &tcItem) ? tcItem.iImage
549 : wxNOT_FOUND;
88310e2e
VZ
550}
551
8d34bf5c 552bool wxNotebook::SetPageImage(size_t nPage, int nImage)
88310e2e 553{
8ecd5de2 554 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
88310e2e 555
8ecd5de2
WS
556 TC_ITEM tcItem;
557 tcItem.mask = TCIF_IMAGE;
558 tcItem.iImage = nImage;
88310e2e 559
8ecd5de2 560 return TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0;
88310e2e
VZ
561}
562
563void wxNotebook::SetImageList(wxImageList* imageList)
907f37b3 564{
8ecd5de2 565 wxNotebookBase::SetImageList(imageList);
07b8d7ec 566
8ecd5de2
WS
567 if ( imageList )
568 {
5482ee07 569 (void) TabCtrl_SetImageList(GetHwnd(), GetHimagelistOf(imageList));
8ecd5de2 570 }
b656febd
VS
571}
572
d9506e77
VZ
573// ----------------------------------------------------------------------------
574// wxNotebook size settings
575// ----------------------------------------------------------------------------
576
c3732409
VZ
577wxRect wxNotebook::GetPageSize() const
578{
579 wxRect r;
580
581 RECT rc;
582 ::GetClientRect(GetHwnd(), &rc);
583
584 // This check is to work around a bug in TabCtrl_AdjustRect which will
628eae0b
VZ
585 // cause a crash on win2k or on XP with themes disabled if either
586 // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle
587 // is too small.
588 //
589 // The value of 20 is chosen arbitrarily but seems to work
590 if ( rc.right > 20 && rc.bottom > 20 )
c3732409 591 {
01c8cbf5 592 TabCtrl_AdjustRect(GetHwnd(), false, &rc);
c3732409
VZ
593
594 wxCopyRECTToRect(rc, r);
595 }
596
597 return r;
598}
599
d9506e77
VZ
600void wxNotebook::SetPageSize(const wxSize& size)
601{
602 // transform the page size into the notebook size
603 RECT rc;
604 rc.left =
605 rc.top = 0;
606 rc.right = size.x;
607 rc.bottom = size.y;
608
b8bdaa7c 609 TabCtrl_AdjustRect(GetHwnd(), true, &rc);
d9506e77
VZ
610
611 // and now set it
612 SetSize(rc.right - rc.left, rc.bottom - rc.top);
613}
614
615void wxNotebook::SetPadding(const wxSize& padding)
616{
617 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
618}
42e69d6b
VZ
619
620// Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
621// style.
622void wxNotebook::SetTabSize(const wxSize& sz)
623{
624 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
625}
626
2ce7af35
JS
627wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
628{
669c595d 629 // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP
2ce7af35 630 wxSize sizeTotal = sizePage;
078cf5cb 631
8b5d5223 632 wxSize tabSize;
669c595d 633 if ( GetPageCount() > 0 )
2ce7af35
JS
634 {
635 RECT rect;
669c595d 636 TabCtrl_GetItemRect(GetHwnd(), 0, &rect);
2ce7af35
JS
637 tabSize.x = rect.right - rect.left;
638 tabSize.y = rect.bottom - rect.top;
639 }
669c595d
VZ
640
641 // add an extra margin in both directions
642 const int MARGIN = 8;
643 if ( IsVertical() )
2ce7af35 644 {
669c595d
VZ
645 sizeTotal.x += MARGIN;
646 sizeTotal.y += tabSize.y + MARGIN;
2ce7af35 647 }
669c595d 648 else // horizontal layout
2ce7af35 649 {
669c595d
VZ
650 sizeTotal.x += tabSize.x + MARGIN;
651 sizeTotal.y += MARGIN;
2ce7af35
JS
652 }
653
654 return sizeTotal;
655}
656
2015f2b3
VZ
657void wxNotebook::AdjustPageSize(wxNotebookPage *page)
658{
659 wxCHECK_RET( page, _T("NULL page in wxNotebook::AdjustPageSize") );
660
c3732409
VZ
661 const wxRect r = GetPageSize();
662 if ( !r.IsEmpty() )
14a6b6e5 663 {
c3732409 664 page->SetSize(r);
14a6b6e5 665 }
2015f2b3
VZ
666}
667
88310e2e
VZ
668// ----------------------------------------------------------------------------
669// wxNotebook operations
670// ----------------------------------------------------------------------------
671
621793f4 672// remove one page from the notebook, without deleting
8d34bf5c 673wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
621793f4 674{
df7145da
VZ
675 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
676 if ( !pageRemoved )
677 return NULL;
621793f4 678
01c8cbf5 679 TabCtrl_DeleteItem(GetHwnd(), nPage);
621793f4 680
df7145da
VZ
681 if ( m_pages.IsEmpty() )
682 {
683 // no selection any more, the notebook becamse empty
5482ee07 684 m_nSelection = wxNOT_FOUND;
df7145da
VZ
685 }
686 else // notebook still not empty
687 {
01c8cbf5 688 int selNew = TabCtrl_GetCurSel(GetHwnd());
5482ee07 689 if ( selNew != wxNOT_FOUND )
df7145da 690 {
623f5f70 691 // No selection change, just refresh the current selection.
078cf5cb
WS
692 // Because it could be that the slection index changed
693 // we need to update it.
623f5f70
JS
694 // Note: this does not mean the selection it self changed.
695 m_nSelection = selNew;
696 m_pages[m_nSelection]->Refresh();
df7145da 697 }
623f5f70 698 else if (int(nPage) == m_nSelection)
43a997b6 699 {
623f5f70 700 // The selection was deleted.
078cf5cb 701
623f5f70
JS
702 // Determine new selection.
703 if (m_nSelection == int(GetPageCount()))
704 selNew = m_nSelection - 1;
705 else
706 selNew = m_nSelection;
078cf5cb 707
43a997b6
VZ
708 // m_nSelection must be always valid so reset it before calling
709 // SetSelection()
5482ee07 710 m_nSelection = wxNOT_FOUND;
43a997b6
VZ
711 SetSelection(selNew);
712 }
623f5f70
JS
713 else
714 {
715 wxFAIL; // Windows did not behave ok.
716 }
df7145da 717 }
47f12f58 718
df7145da 719 return pageRemoved;
621793f4
JS
720}
721
88310e2e
VZ
722// remove all pages
723bool wxNotebook::DeleteAllPages()
724{
7ec69821
WS
725 size_t nPageCount = GetPageCount();
726 size_t nPage;
727 for ( nPage = 0; nPage < nPageCount; nPage++ )
728 delete m_pages[nPage];
88310e2e 729
7ec69821 730 m_pages.Clear();
88310e2e 731
7ec69821 732 TabCtrl_DeleteAllItems(GetHwnd());
907f37b3 733
5482ee07 734 m_nSelection = wxNOT_FOUND;
47f12f58 735
7ec69821
WS
736 InvalidateBestSize();
737 return true;
88310e2e
VZ
738}
739
88310e2e 740// same as AddPage() but does it at given position
8d34bf5c 741bool wxNotebook::InsertPage(size_t nPage,
88310e2e
VZ
742 wxNotebookPage *pPage,
743 const wxString& strText,
744 bool bSelect,
745 int imageId)
746{
b8bdaa7c
VZ
747 wxCHECK_MSG( pPage != NULL, false, _T("NULL page in wxNotebook::InsertPage") );
748 wxCHECK_MSG( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false,
22f3361e 749 _T("invalid index in wxNotebook::InsertPage") );
88310e2e 750
efa14cf2
VZ
751 wxASSERT_MSG( pPage->GetParent() == this,
752 _T("notebook pages must have notebook as parent") );
43427087 753
22f3361e
VZ
754 // add a new tab to the control
755 // ----------------------------
58a8ab88 756
22f3361e
VZ
757 // init all fields to 0
758 TC_ITEM tcItem;
759 wxZeroMemory(tcItem);
58a8ab88 760
22f3361e
VZ
761 // set the image, if any
762 if ( imageId != -1 )
763 {
764 tcItem.mask |= TCIF_IMAGE;
765 tcItem.iImage = imageId;
766 }
88310e2e 767
22f3361e 768 // and the text
8b5d5223 769 if ( !strText.empty() )
22f3361e
VZ
770 {
771 tcItem.mask |= TCIF_TEXT;
772 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
773 }
43427087 774
e830a6a6
RD
775 // hide the page: unless it is selected, it shouldn't be shown (and if it
776 // is selected it will be shown later)
777 HWND hwnd = GetWinHwnd(pPage);
778 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
779
780 // this updates internal flag too -- otherwise it would get out of sync
781 // with the real state
782 pPage->Show(false);
783
784
22f3361e
VZ
785 // fit the notebook page to the tab control's display area: this should be
786 // done before adding it to the notebook or TabCtrl_InsertItem() will
787 // change the notebooks size itself!
2015f2b3 788 AdjustPageSize(pPage);
43427087 789
22f3361e 790 // finally do insert it
01c8cbf5 791 if ( TabCtrl_InsertItem(GetHwnd(), nPage, &tcItem) == -1 )
2015f2b3 792 {
22f3361e 793 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
88310e2e 794
b8bdaa7c 795 return false;
22f3361e 796 }
88310e2e 797
ebd9ec29
JG
798 // need to update the bg brush when the first page is added
799 // so the first panel gets the correct themed background
800 if ( m_pages.empty() )
801 {
11e70ae6 802#if wxUSE_UXTHEME
ebd9ec29 803 UpdateBgBrush();
11e70ae6 804#endif // wxUSE_UXTHEME
ebd9ec29
JG
805 }
806
22f3361e
VZ
807 // succeeded: save the pointer to the page
808 m_pages.Insert(pPage, nPage);
42e69d6b 809
56b9925b
VZ
810 // we may need to adjust the size again if the notebook size changed:
811 // normally this only happens for the first page we add (the tabs which
812 // hadn't been there before are now shown) but for a multiline notebook it
813 // can happen for any page at all as a new row could have been started
814 if ( m_pages.GetCount() == 1 || HasFlag(wxNB_MULTILINE) )
2015f2b3
VZ
815 {
816 AdjustPageSize(pPage);
817 }
818
22f3361e
VZ
819 // now deal with the selection
820 // ---------------------------
821
822 // if the inserted page is before the selected one, we must update the
823 // index of the selected page
34a0c9f4 824 if ( int(nPage) <= m_nSelection )
22f3361e
VZ
825 {
826 // one extra page added
827 m_nSelection++;
828 }
829
830 // some page should be selected: either this one or the first one if there
831 // is still no selection
5482ee07 832 int selNew = wxNOT_FOUND;
22f3361e
VZ
833 if ( bSelect )
834 selNew = nPage;
5482ee07 835 else if ( m_nSelection == wxNOT_FOUND )
22f3361e
VZ
836 selNew = 0;
837
5482ee07 838 if ( selNew != wxNOT_FOUND )
22f3361e
VZ
839 SetSelection(selNew);
840
37144cf0 841 InvalidateBestSize();
25057aba 842
b8bdaa7c 843 return true;
88310e2e
VZ
844}
845
e450aa69 846int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
ef094fa0
JS
847{
848 TC_HITTESTINFO hitTestInfo;
849 hitTestInfo.pt.x = pt.x;
850 hitTestInfo.pt.y = pt.y;
e450aa69 851 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
ef094fa0 852
e450aa69
VZ
853 if ( flags )
854 {
855 *flags = 0;
856
857 if ((hitTestInfo.flags & TCHT_NOWHERE) == TCHT_NOWHERE)
9804d540 858 *flags |= wxBK_HITTEST_NOWHERE;
e450aa69 859 if ((hitTestInfo.flags & TCHT_ONITEM) == TCHT_ONITEM)
9804d540 860 *flags |= wxBK_HITTEST_ONITEM;
e450aa69 861 if ((hitTestInfo.flags & TCHT_ONITEMICON) == TCHT_ONITEMICON)
9804d540 862 *flags |= wxBK_HITTEST_ONICON;
e450aa69 863 if ((hitTestInfo.flags & TCHT_ONITEMLABEL) == TCHT_ONITEMLABEL)
9804d540 864 *flags |= wxBK_HITTEST_ONLABEL;
22a35096 865 if ( item == wxNOT_FOUND && GetPageSize().Contains(pt) )
9804d540 866 *flags |= wxBK_HITTEST_ONPAGE;
e450aa69 867 }
ef094fa0
JS
868
869 return item;
870}
871
014ee083
VZ
872// ----------------------------------------------------------------------------
873// flicker-less notebook redraw
874// ----------------------------------------------------------------------------
875
876#if USE_NOTEBOOK_ANTIFLICKER
877
878// wnd proc for the spin button
879LRESULT APIENTRY _EXPORT wxNotebookSpinBtnWndProc(HWND hwnd,
880 UINT message,
881 WPARAM wParam,
882 LPARAM lParam)
883{
884 if ( message == WM_ERASEBKGND )
885 return 0;
886
887 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn,
888 hwnd, message, wParam, lParam);
889}
890
6143d3b6
VZ
891LRESULT APIENTRY _EXPORT wxNotebookWndProc(HWND hwnd,
892 UINT message,
893 WPARAM wParam,
894 LPARAM lParam)
895{
896 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook,
897 hwnd, message, wParam, lParam);
898}
899
014ee083
VZ
900void wxNotebook::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
901{
902 // do nothing here
903}
904
905void wxNotebook::OnPaint(wxPaintEvent& WXUNUSED(event))
906{
907 wxPaintDC dc(this);
908 wxMemoryDC memdc;
909 RECT rc;
910 ::GetClientRect(GetHwnd(), &rc);
911 wxBitmap bmp(rc.right, rc.bottom);
912 memdc.SelectObject(bmp);
913
6614aa49
VZ
914 const wxLayoutDirection dir = dc.GetLayoutDirection();
915 memdc.SetLayoutDirection(dir);
916
014ee083 917 // if there is no special brush just use the solid background colour
64c288fa 918#if wxUSE_UXTHEME
014ee083 919 HBRUSH hbr = (HBRUSH)m_hbrBackground;
64c288fa
JS
920#else
921 HBRUSH hbr = 0;
e4db172a 922#endif
e32703a9 923 wxBrush brush;
014ee083 924 if ( !hbr )
e32703a9
VZ
925 {
926 brush = wxBrush(GetBackgroundColour());
927 hbr = GetHbrushOf(brush);
928 }
014ee083
VZ
929
930 ::FillRect(GetHdcOf(memdc), &rc, hbr);
931
932 MSWDefWindowProc(WM_PAINT, (WPARAM)memdc.GetHDC(), 0);
933
6614aa49
VZ
934 // For some reason in RTL mode, source offset has to be -1, otherwise the
935 // right border (physical) remains unpainted.
936 const wxCoord ofs = dir == wxLayout_RightToLeft ? -1 : 0;
937 dc.Blit(ofs, 0, rc.right, rc.bottom, &memdc, ofs, 0);
014ee083
VZ
938}
939
940#endif // USE_NOTEBOOK_ANTIFLICKER
e450aa69 941
88310e2e
VZ
942// ----------------------------------------------------------------------------
943// wxNotebook callbacks
944// ----------------------------------------------------------------------------
945
9026ad85 946void wxNotebook::OnSize(wxSizeEvent& event)
88310e2e 947{
7dccdf81 948 if ( GetPageCount() == 0 )
f7eaa62f
JS
949 {
950 // Prevents droppings on resize, but does cause some flicker
951 // when there are no pages.
2aa24b60 952 Refresh();
f7eaa62f
JS
953 event.Skip();
954 return;
955 }
254fbd14
JS
956#ifndef __WXWINCE__
957 else
958 {
959 // Without this, we can sometimes get droppings at the edges
960 // of a notebook, for example a notebook in a splitter window.
961 // This needs to be reconciled with the RefreshRect calls
962 // at the end of this function, which weren't enough to prevent
963 // the droppings.
01c8cbf5 964
254fbd14
JS
965 wxSize sz = GetClientSize();
966
967 // Refresh right side
968 wxRect rect(sz.x-4, 0, 4, sz.y);
969 RefreshRect(rect);
01c8cbf5 970
254fbd14
JS
971 // Refresh bottom side
972 rect = wxRect(0, sz.y-4, sz.x, 4);
973 RefreshRect(rect);
01c8cbf5 974
254fbd14
JS
975 // Refresh left side
976 rect = wxRect(0, 0, 4, sz.y);
977 RefreshRect(rect);
978 }
014ee083 979#endif // !__WXWINCE__
f7eaa62f 980
c1637c89 981 // fit all the notebook pages to the tab control's display area
56b9925b 982
c1637c89
VZ
983 RECT rc;
984 rc.left = rc.top = 0;
985 GetSize((int *)&rc.right, (int *)&rc.bottom);
56b9925b 986
c1637c89
VZ
987 // save the total size, we'll use it below
988 int widthNbook = rc.right - rc.left,
989 heightNbook = rc.bottom - rc.top;
990
991 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
992 // returns completely false values for multiline tab controls after the tabs
993 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
994 //
995 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
996 //
997 // and the only work around I could find was this ugly hack... without it
998 // simply toggling the "multiline" checkbox in the notebook sample resulted
999 // in a noticeable page displacement
1000 if ( HasFlag(wxNB_MULTILINE) )
1001 {
1002 // avoid an infinite recursion: we get another notification too!
1003 static bool s_isInOnSize = false;
4b7f2165 1004
c1637c89
VZ
1005 if ( !s_isInOnSize )
1006 {
1007 s_isInOnSize = true;
1008 SendMessage(GetHwnd(), WM_SIZE, SIZE_RESTORED,
1009 MAKELPARAM(rc.right, rc.bottom));
1010 s_isInOnSize = false;
1011 }
1012 }
b5c3b538 1013
e2f6f8da
RD
1014#if wxUSE_UXTHEME
1015 // background bitmap size has changed, update the brush using it too
1016 UpdateBgBrush();
1017#endif // wxUSE_UXTHEME
1018
01c8cbf5 1019 TabCtrl_AdjustRect(GetHwnd(), false, &rc);
c1637c89
VZ
1020
1021 int width = rc.right - rc.left,
1022 height = rc.bottom - rc.top;
1023 size_t nCount = m_pages.Count();
1024 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
1025 wxNotebookPage *pPage = m_pages[nPage];
1026 pPage->SetSize(rc.left, rc.top, width, height);
1027 }
1028
1029
1030 // unless we had already repainted everything, we now need to refresh
1031 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) )
1032 {
1033 // invalidate areas not covered by pages
1034 RefreshRect(wxRect(0, 0, widthNbook, rc.top), false);
1035 RefreshRect(wxRect(0, rc.top, rc.left, height), false);
1036 RefreshRect(wxRect(0, rc.bottom, widthNbook, heightNbook - rc.bottom),
1037 false);
3550706d 1038 RefreshRect(wxRect(rc.right, rc.top, widthNbook - rc.right, height),
c1637c89
VZ
1039 false);
1040 }
1041
014ee083
VZ
1042#if USE_NOTEBOOK_ANTIFLICKER
1043 // subclass the spin control used by the notebook to scroll pages to
1044 // prevent it from flickering on resize
dfb47d83 1045 if ( !m_hasSubclassedUpdown )
014ee083
VZ
1046 {
1047 // iterate over all child windows to find spin button
1048 for ( HWND child = ::GetWindow(GetHwnd(), GW_CHILD);
1049 child;
1050 child = ::GetWindow(child, GW_HWNDNEXT) )
1051 {
1052 wxWindow *childWindow = wxFindWinFromHandle((WXHWND)child);
1053
1054 // see if it exists, if no wxWindow found then assume it's the spin
1055 // btn
1056 if ( !childWindow )
1057 {
1058 // subclass the spin button to override WM_ERASEBKGND
dfb47d83
VZ
1059 if ( !gs_wndprocNotebookSpinBtn )
1060 gs_wndprocNotebookSpinBtn = (WXFARPROC)wxGetWindowProc(child);
014ee083
VZ
1061
1062 wxSetWindowProc(child, wxNotebookSpinBtnWndProc);
dfb47d83 1063 m_hasSubclassedUpdown = true;
014ee083
VZ
1064 break;
1065 }
1066 }
1067 }
1068#endif // USE_NOTEBOOK_ANTIFLICKER
1069
c1637c89 1070 event.Skip();
88310e2e
VZ
1071}
1072
1073void wxNotebook::OnSelChange(wxNotebookEvent& event)
1074{
1d6fcbcc
VZ
1075 // is it our tab control?
1076 if ( event.GetEventObject() == this )
1077 {
1078 UpdateSelection(event.GetSelection());
1079 }
88310e2e 1080
1d6fcbcc
VZ
1081 // we want to give others a chance to process this message as well
1082 event.Skip();
88310e2e
VZ
1083}
1084
88310e2e
VZ
1085void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
1086{
d9506e77
VZ
1087 if ( event.IsWindowChange() ) {
1088 // change pages
1089 AdvanceSelection(event.GetDirection());
1090 }
1091 else {
b8bdaa7c 1092 // we get this event in 3 cases
d9506e77
VZ
1093 //
1094 // a) one of our pages might have generated it because the user TABbed
1095 // out from it in which case we should propagate the event upwards and
1096 // our parent will take care of setting the focus to prev/next sibling
1097 //
1098 // or
1099 //
1100 // b) the parent panel wants to give the focus to us so that we
1101 // forward it to our selected page. We can't deal with this in
1102 // OnSetFocus() because we don't know which direction the focus came
1103 // from in this case and so can't choose between setting the focus to
1104 // first or last panel child
b8bdaa7c
VZ
1105 //
1106 // or
1107 //
1108 // c) we ourselves (see MSWTranslateMessage) generated the event
1109 //
1110 wxWindow * const parent = GetParent();
1111
3c99602b
MB
1112 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
1113 const bool isFromParent = event.GetEventObject() == (wxObject*) parent;
1114 const bool isFromSelf = event.GetEventObject() == (wxObject*) this;
b8bdaa7c
VZ
1115
1116 if ( isFromParent || isFromSelf )
d9506e77 1117 {
b8bdaa7c
VZ
1118 // no, it doesn't come from child, case (b) or (c): forward to a
1119 // page but only if direction is backwards (TAB) or from ourselves,
5482ee07 1120 if ( m_nSelection != wxNOT_FOUND &&
b8bdaa7c 1121 (!event.GetDirection() || isFromSelf) )
d9506e77
VZ
1122 {
1123 // so that the page knows that the event comes from it's parent
1124 // and is being propagated downwards
1125 event.SetEventObject(this);
1126
1e6feb95 1127 wxWindow *page = m_pages[m_nSelection];
d9506e77
VZ
1128 if ( !page->GetEventHandler()->ProcessEvent(event) )
1129 {
1130 page->SetFocus();
1131 }
1132 //else: page manages focus inside it itself
1133 }
b8bdaa7c 1134 else // otherwise set the focus to the notebook itself
d9506e77 1135 {
d9506e77
VZ
1136 SetFocus();
1137 }
1138 }
1139 else
1140 {
b8bdaa7c
VZ
1141 // it comes from our child, case (a), pass to the parent, but only
1142 // if the direction is forwards. Otherwise set the focus to the
1143 // notebook itself. The notebook is always the 'first' control of a
1144 // page.
1145 if ( !event.GetDirection() )
1146 {
1147 SetFocus();
1148 }
1149 else if ( parent )
1150 {
d9506e77
VZ
1151 event.SetCurrentFocus(this);
1152 parent->GetEventHandler()->ProcessEvent(event);
1153 }
1154 }
88310e2e 1155 }
88310e2e
VZ
1156}
1157
caf95d2a
VZ
1158#if wxUSE_UXTHEME
1159
c3732409 1160bool wxNotebook::DoDrawBackground(WXHDC hDC, wxWindow *child)
caf95d2a 1161{
c3732409
VZ
1162 wxUxThemeHandle theme(child ? child : this, L"TAB");
1163 if ( !theme )
1164 return false;
1165
1166 // get the notebook client rect (we're not interested in drawing tabs
1167 // themselves)
1168 wxRect r = GetPageSize();
1169 if ( r.IsEmpty() )
1170 return false;
1171
c4a95f6f 1172 RECT rc;
c3732409
VZ
1173 wxCopyRectToRECT(r, rc);
1174
1175 // map rect to the coords of the window we're drawing in
1176 if ( child )
1177 ::MapWindowPoints(GetHwnd(), GetHwndOf(child), (POINT *)&rc, 2);
1178
2ff53fd3
JS
1179 // we have the content area (page size), but we need to draw all of the
1180 // background for it to be aligned correctly
1181 wxUxThemeEngine::Get()->GetThemeBackgroundExtent
1182 (
1183 theme,
1184 (HDC) hDC,
1185 9 /* TABP_PANE */,
1186 0,
1187 &rc,
1188 &rc
1189 );
c3732409
VZ
1190 wxUxThemeEngine::Get()->DrawThemeBackground
1191 (
1192 theme,
92199f4c 1193 (HDC) hDC,
c3732409
VZ
1194 9 /* TABP_PANE */,
1195 0,
1196 &rc,
1197 NULL
1198 );
1199
1200 return true;
1201}
de359565 1202
c3732409
VZ
1203WXHBRUSH wxNotebook::QueryBgBitmap()
1204{
1205 wxRect r = GetPageSize();
1206 if ( r.IsEmpty() )
1207 return 0;
c4a95f6f
VZ
1208
1209 WindowHDC hDC(GetHwnd());
1210 MemoryHDC hDCMem(hDC);
c3732409 1211 CompatibleBitmap hBmp(hDC, r.x + r.width, r.y + r.height);
c4a95f6f
VZ
1212
1213 SelectInHDC selectBmp(hDCMem, hBmp);
caf95d2a 1214
c3732409
VZ
1215 if ( !DoDrawBackground((WXHDC)(HDC)hDCMem) )
1216 return 0;
0f770734 1217
de359565 1218 return (WXHBRUSH)::CreatePatternBrush(hBmp);
c4a95f6f 1219}
caf95d2a 1220
c4a95f6f
VZ
1221void wxNotebook::UpdateBgBrush()
1222{
1223 if ( m_hbrBackground )
1224 ::DeleteObject((HBRUSH)m_hbrBackground);
caf95d2a 1225
c4a95f6f
VZ
1226 if ( !m_hasBgCol && wxUxThemeEngine::GetIfActive() )
1227 {
de359565 1228 m_hbrBackground = QueryBgBitmap();
caf95d2a 1229 }
c3732409 1230 else // no themes or we've got user-defined solid colour
caf95d2a
VZ
1231 {
1232 m_hbrBackground = NULL;
1233 }
1234}
1235
2bae4332 1236WXHBRUSH wxNotebook::MSWGetBgBrushForChild(WXHDC hDC, WXHWND hWnd)
caf95d2a 1237{
caf95d2a
VZ
1238 if ( m_hbrBackground )
1239 {
1240 // before drawing with the background brush, we need to position it
1241 // correctly
caf95d2a 1242 RECT rc;
2bae4332 1243 ::GetWindowRect((HWND)hWnd, &rc);
caf95d2a
VZ
1244
1245 ::MapWindowPoints(NULL, GetHwnd(), (POINT *)&rc, 1);
1246
5c836c46 1247 if ( !::SetBrushOrgEx((HDC)hDC, -rc.left, -rc.top, NULL) )
caf95d2a
VZ
1248 {
1249 wxLogLastError(_T("SetBrushOrgEx(notebook bg brush)"));
1250 }
c4a95f6f
VZ
1251
1252 return m_hbrBackground;
5c836c46
VZ
1253 }
1254
2bae4332 1255 return wxNotebookBase::MSWGetBgBrushForChild(hDC, hWnd);
5c836c46 1256}
caf95d2a 1257
c3732409 1258bool wxNotebook::MSWPrintChild(WXHDC hDC, wxWindow *child)
07c19327 1259{
c3732409
VZ
1260 // solid background colour overrides themed background drawing
1261 if ( !UseBgCol() && DoDrawBackground(hDC, child) )
1262 return true;
de359565 1263
4dab5279
JS
1264 // If we're using a solid colour (for example if we've switched off
1265 // theming for this notebook), paint it
1266 if (UseBgCol())
1267 {
1268 wxRect r = GetPageSize();
1269 if ( r.IsEmpty() )
1270 return false;
1271
1272 RECT rc;
1273 wxCopyRectToRECT(r, rc);
1274
1275 // map rect to the coords of the window we're drawing in
1276 if ( child )
1277 ::MapWindowPoints(GetHwnd(), GetHwndOf(child), (POINT *)&rc, 2);
1278
1279 wxBrush brush(GetBackgroundColour());
1280 HBRUSH hbr = GetHbrushOf(brush);
01c8cbf5 1281
4dab5279
JS
1282 ::FillRect((HDC) hDC, &rc, hbr);
1283
1284 return true;
1285 }
1286
c3732409 1287 return wxNotebookBase::MSWPrintChild(hDC, child);
07c19327
VZ
1288}
1289
caf95d2a
VZ
1290#endif // wxUSE_UXTHEME
1291
25057aba
JS
1292// Windows only: attempts to get colour for UX theme page background
1293wxColour wxNotebook::GetThemeBackgroundColour() const
1294{
1295#if wxUSE_UXTHEME
1296 if (wxUxThemeEngine::Get())
1297 {
1298 wxUxThemeHandle hTheme((wxNotebook*) this, L"TAB");
1299 if (hTheme)
1300 {
1301 // This is total guesswork.
1302 // See PlatformSDK\Include\Tmschema.h for values
1303 COLORREF themeColor;
1304 wxUxThemeEngine::Get()->GetThemeColor(
1305 hTheme,
1306 10 /* TABP_BODY */,
1307 1 /* NORMAL */,
1308 3821 /* FILLCOLORHINT */,
1309 &themeColor);
1310
1311 /*
1312 [DS] Workaround for WindowBlinds:
1313 Some themes return a near black theme color using FILLCOLORHINT,
1314 this makes notebook pages have an ugly black background and makes
1315 text (usually black) unreadable. Retry again with FILLCOLOR.
1316
1317 This workaround potentially breaks appearance of some themes,
1318 but in practice it already fixes some themes.
1319 */
1320 if (themeColor == 1)
1321 {
1322 wxUxThemeEngine::Get()->GetThemeColor(
1323 hTheme,
1324 10 /* TABP_BODY */,
1325 1 /* NORMAL */,
1326 3802 /* FILLCOLOR */,
1327 &themeColor);
1328 }
1329
7dccdf81 1330 return wxRGBToColour(themeColor);
25057aba
JS
1331 }
1332 }
1333#endif // wxUSE_UXTHEME
1334
1335 return GetBackgroundColour();
1336}
1337
88310e2e
VZ
1338// ----------------------------------------------------------------------------
1339// wxNotebook base class virtuals
1340// ----------------------------------------------------------------------------
b5c3b538 1341
0b481c72
VZ
1342#if wxUSE_CONSTRAINTS
1343
b5c3b538
VZ
1344// override these 2 functions to do nothing: everything is done in OnSize
1345
4b7f2165 1346void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
b5c3b538
VZ
1347{
1348 // don't set the sizes of the pages - their correct size is not yet known
b8bdaa7c 1349 wxControl::SetConstraintSizes(false);
b5c3b538
VZ
1350}
1351
4b7f2165 1352bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
b5c3b538 1353{
b8bdaa7c 1354 return true;
b5c3b538
VZ
1355}
1356
0b481c72
VZ
1357#endif // wxUSE_CONSTRAINTS
1358
0df3fbd7
VZ
1359// ----------------------------------------------------------------------------
1360// wxNotebook Windows message handlers
1361// ----------------------------------------------------------------------------
1362
1363bool wxNotebook::MSWOnScroll(int orientation, WXWORD nSBCode,
1364 WXWORD pos, WXHWND control)
1365{
1366 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1367 // up-down control
1368 if ( control )
b8bdaa7c 1369 return false;
0df3fbd7
VZ
1370
1371 return wxNotebookBase::MSWOnScroll(orientation, nSBCode, pos, control);
1372}
1373
a23fd0e1 1374bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 1375{
93a19f17 1376 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
1377
1378 NMHDR* hdr = (NMHDR *)lParam;
1379 switch ( hdr->code ) {
1380 case TCN_SELCHANGE:
1381 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
1382 break;
1383
1384 case TCN_SELCHANGING:
1385 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
1386 break;
1387
fd3f686c 1388 default:
a23fd0e1 1389 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
1390 }
1391
01c8cbf5 1392 event.SetSelection(TabCtrl_GetCurSel(GetHwnd()));
93a19f17 1393 event.SetOldSelection(m_nSelection);
88310e2e 1394 event.SetEventObject(this);
a23fd0e1 1395 event.SetInt(idCtrl);
88310e2e 1396
fd3f686c
VZ
1397 bool processed = GetEventHandler()->ProcessEvent(event);
1398 *result = !event.IsAllowed();
1399 return processed;
88310e2e
VZ
1400}
1401
1e6feb95 1402#endif // wxUSE_NOTEBOOK