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