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