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