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