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