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