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