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