]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
fixes for new lib dirs for wxMSW
[wxWidgets.git] / src / msw / notebook.cpp
CommitLineData
88310e2e
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/notebook.cpp
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>
6c9a19aa 9// Licence: wxWindows licence
88310e2e
VZ
10///////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a3b46648 13#pragma implementation "notebook.h"
88310e2e
VZ
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
1e6feb95 20 #pragma hdrstop
88310e2e
VZ
21#endif
22
1e6feb95
VZ
23#if wxUSE_NOTEBOOK
24
88310e2e
VZ
25// wxWindows
26#ifndef WX_PRECOMP
3096bd2f 27 #include "wx/string.h"
88310e2e
VZ
28#endif // WX_PRECOMP
29
3096bd2f
VZ
30#include "wx/log.h"
31#include "wx/imaglist.h"
32#include "wx/event.h"
33#include "wx/control.h"
34#include "wx/notebook.h"
04eb05b0 35#include "wx/app.h"
88310e2e 36
3096bd2f 37#include "wx/msw/private.h"
88310e2e
VZ
38
39// Windows standard headers
40#ifndef __WIN95__
2432b92d 41 #error "wxNotebook is only supported Windows 95 and above"
88310e2e
VZ
42#endif //Win95
43
aaab7c01
VZ
44#include <windowsx.h> // for SetWindowFont
45
b39dbf34
JS
46#ifdef __GNUWIN32_OLD__
47 #include "wx/msw/gnuwin32/extra.h"
65fd5cb0 48#endif
57c208c5 49
b39dbf34 50#if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
c42404a5 51 #include <commctrl.h>
88310e2e
VZ
52#endif
53
85b43fbf
JS
54#include "wx/msw/winundef.h"
55
56#if wxUSE_UXTHEME
57#include "wx/msw/uxtheme.h"
58
59#include "wx/radiobut.h"
60#include "wx/radiobox.h"
61#include "wx/checkbox.h"
62#include "wx/bmpbuttn.h"
63#include "wx/statline.h"
64#include "wx/statbox.h"
65#include "wx/stattext.h"
66#include "wx/slider.h"
67#include "wx/scrolwin.h"
68#include "wx/panel.h"
69#endif
70
88310e2e
VZ
71// ----------------------------------------------------------------------------
72// macros
73// ----------------------------------------------------------------------------
74
75// check that the page index is valid
8d34bf5c 76#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
88310e2e
VZ
77
78// hide the ugly cast
79#define m_hwnd (HWND)GetHWND()
80
74b31181
VZ
81// ----------------------------------------------------------------------------
82// constants
83// ----------------------------------------------------------------------------
84
85// This is a work-around for missing defines in gcc-2.95 headers
86#ifndef TCS_RIGHT
87 #define TCS_RIGHT 0x0002
88#endif
89
90#ifndef TCS_VERTICAL
91 #define TCS_VERTICAL 0x0080
92#endif
93
94#ifndef TCS_BOTTOM
95 #define TCS_BOTTOM TCS_RIGHT
96#endif
97
88310e2e
VZ
98// ----------------------------------------------------------------------------
99// event table
100// ----------------------------------------------------------------------------
101
51741307
SC
102#include <wx/listimpl.cpp>
103
104WX_DEFINE_LIST( wxNotebookPageInfoList ) ;
105
2e4df4bf
VZ
106DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
107DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
108
d9317fd4 109BEGIN_EVENT_TABLE(wxNotebook, wxControl)
88310e2e
VZ
110 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
111
9026ad85 112 EVT_SIZE(wxNotebook::OnSize)
42e69d6b 113
88310e2e 114 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
42e69d6b 115
88310e2e 116 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
d9317fd4 117END_EVENT_TABLE()
88310e2e 118
51596bcb 119#if wxUSE_EXTENDED_RTTI
51741307 120
51596bcb 121IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook, wxControl,"wx/notebook.h")
51741307
SC
122IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo, wxObject , "wx/notebook.h" )
123
067e9be6 124WX_COLLECTION_TYPE_INFO( wxNotebookPageInfo * , wxNotebookPageInfoList ) ;
51596bcb 125
f0a126fe
SC
126template<> void wxCollectionToVariantArray( wxNotebookPageInfoList const &theList, wxxVariantArray &value)
127{
128 wxListCollectionToVariantArray( theList , value ) ;
129}
130
51596bcb 131WX_BEGIN_PROPERTIES_TABLE(wxNotebook)
067e9be6 132 WX_PROPERTY_COLLECTION( PageInfos , wxNotebookPageInfoList , wxNotebookPageInfo* , AddPageInfo , GetPageInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
51741307
SC
133/*
134 notebookpage
135 object
136 object_ref
137 label
138 selected
139 style
140 usenotebooksizer
141*/
51596bcb
SC
142WX_END_PROPERTIES_TABLE()
143
144WX_BEGIN_HANDLERS_TABLE(wxNotebook)
145WX_END_HANDLERS_TABLE()
146
147WX_CONSTRUCTOR_4( wxNotebook , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
148
51741307
SC
149
150WX_BEGIN_PROPERTIES_TABLE(wxNotebookPageInfo)
067e9be6
SC
151 WX_READONLY_PROPERTY( Page , wxNotebookPage* , GetPage , , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
152 WX_READONLY_PROPERTY( Text , wxString , GetText , wxEmptyString , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
153 WX_READONLY_PROPERTY( Selected , bool , GetSelected , false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
154 WX_READONLY_PROPERTY( ImageId , int , GetImageId , -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
51741307
SC
155WX_END_PROPERTIES_TABLE()
156
157WX_BEGIN_HANDLERS_TABLE(wxNotebookPageInfo)
158WX_END_HANDLERS_TABLE()
159
160WX_CONSTRUCTOR_4( wxNotebookPageInfo , wxNotebookPage* , Page , wxString , Text , bool , Selected , int , ImageId )
161
51596bcb 162#else
d9317fd4 163IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
51741307 164IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo, wxObject )
51596bcb 165#endif
d9317fd4 166IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
88310e2e
VZ
167
168// ============================================================================
169// implementation
170// ============================================================================
171
172// ----------------------------------------------------------------------------
173// wxNotebook construction
174// ----------------------------------------------------------------------------
175
51741307
SC
176const wxNotebookPageInfoList& wxNotebook::GetPageInfos() const
177{
178 wxNotebookPageInfoList* list = const_cast< wxNotebookPageInfoList* >( &m_pageInfos ) ;
179 WX_CLEAR_LIST( wxNotebookPageInfoList , *list ) ;
34a0c9f4 180 for( size_t i = 0 ; i < GetPageCount() ; ++i )
51741307
SC
181 {
182 wxNotebookPageInfo *info = new wxNotebookPageInfo() ;
34a0c9f4 183 info->Create( const_cast<wxNotebook*>(this)->GetPage(i) , GetPageText(i) , GetSelection() == int(i) , GetPageImage(i) ) ;
51741307
SC
184 list->Append( info ) ;
185 }
186 return m_pageInfos ;
187}
188
88310e2e
VZ
189// common part of all ctors
190void wxNotebook::Init()
191{
1e6feb95 192 m_imageList = NULL;
88310e2e
VZ
193 m_nSelection = -1;
194}
195
196// default for dynamic class
197wxNotebook::wxNotebook()
198{
199 Init();
200}
201
202// the same arguments as for wxControl
203wxNotebook::wxNotebook(wxWindow *parent,
8b9518ee 204 wxWindowID id,
88310e2e
VZ
205 const wxPoint& pos,
206 const wxSize& size,
8b9518ee 207 long style,
88310e2e
VZ
208 const wxString& name)
209{
210 Init();
211
212 Create(parent, id, pos, size, style, name);
213}
214
215// Create() function
216bool wxNotebook::Create(wxWindow *parent,
8b9518ee 217 wxWindowID id,
88310e2e
VZ
218 const wxPoint& pos,
219 const wxSize& size,
8b9518ee 220 long style,
88310e2e
VZ
221 const wxString& name)
222{
04eb05b0
JS
223 // Does ComCtl32 support non-top tabs?
224 int verComCtl32 = wxApp::GetComCtl32Version();
225 if ( verComCtl32 < 470 || verComCtl32 >= 600 )
226 {
227 if (style & wxNB_BOTTOM)
228 style &= ~wxNB_BOTTOM;
229
230 if (style & wxNB_LEFT)
231 style &= ~wxNB_LEFT;
232
233 if (style & wxNB_RIGHT)
234 style &= ~wxNB_RIGHT;
235 }
236
6dd16e4f
VZ
237 if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL,
238 wxDefaultValidator, name) )
0df3fbd7 239 return FALSE;
88310e2e 240
fda7962d 241 if ( !MSWCreateControl(WC_TABCONTROL, wxEmptyString, pos, size) )
0df3fbd7 242 return FALSE;
907f37b3 243
0df3fbd7
VZ
244 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE)));
245
246 return TRUE;
247}
248
249WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const
250{
251 WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle);
252
253 tabStyle |= WS_TABSTOP | TCS_TABS;
254
2b5f62a0 255 if ( style & wxNB_MULTILINE )
0df3fbd7
VZ
256 tabStyle |= TCS_MULTILINE;
257 if ( style & wxNB_FIXEDWIDTH )
258 tabStyle |= TCS_FIXEDWIDTH;
259
260 if ( style & wxNB_BOTTOM )
261 tabStyle |= TCS_RIGHT;
262 else if ( style & wxNB_LEFT )
263 tabStyle |= TCS_VERTICAL;
264 else if ( style & wxNB_RIGHT )
265 tabStyle |= TCS_VERTICAL | TCS_RIGHT;
266
267 // ex style
268 if ( exstyle )
269 {
270 // note that we never want to have the default WS_EX_CLIENTEDGE style
271 // as it looks too ugly for the notebooks
272 *exstyle = 0;
273 }
274
275 return tabStyle;
88310e2e
VZ
276}
277
88310e2e
VZ
278// ----------------------------------------------------------------------------
279// wxNotebook accessors
280// ----------------------------------------------------------------------------
07b8d7ec 281
8d34bf5c 282size_t wxNotebook::GetPageCount() const
88310e2e
VZ
283{
284 // consistency check
1e6feb95 285 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) );
88310e2e 286
1e6feb95 287 return m_pages.Count();
88310e2e
VZ
288}
289
290int wxNotebook::GetRowCount() const
291{
292 return TabCtrl_GetRowCount(m_hwnd);
293}
294
8d34bf5c 295int wxNotebook::SetSelection(size_t nPage)
88310e2e 296{
223d09f6 297 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
88310e2e 298
34a0c9f4 299 if ( int(nPage) != m_nSelection )
2b5f62a0
VZ
300 {
301 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
302 event.SetSelection(nPage);
303 event.SetOldSelection(m_nSelection);
304 event.SetEventObject(this);
305 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
306 {
307 // program allows the page change
308 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
309 (void)GetEventHandler()->ProcessEvent(event);
310
311 TabCtrl_SetCurSel(m_hwnd, nPage);
312 }
313 }
88310e2e 314
2b5f62a0 315 return m_nSelection;
88310e2e
VZ
316}
317
8d34bf5c 318bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
88310e2e 319{
223d09f6 320 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
88310e2e
VZ
321
322 TC_ITEM tcItem;
323 tcItem.mask = TCIF_TEXT;
837e5743 324 tcItem.pszText = (wxChar *)strText.c_str();
88310e2e
VZ
325
326 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
327}
328
8d34bf5c 329wxString wxNotebook::GetPageText(size_t nPage) const
88310e2e 330{
fda7962d 331 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
88310e2e 332
837e5743 333 wxChar buf[256];
88310e2e
VZ
334 TC_ITEM tcItem;
335 tcItem.mask = TCIF_TEXT;
336 tcItem.pszText = buf;
337 tcItem.cchTextMax = WXSIZEOF(buf);
338
339 wxString str;
340 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
341 str = tcItem.pszText;
342
343 return str;
344}
345
8d34bf5c 346int wxNotebook::GetPageImage(size_t nPage) const
88310e2e 347{
223d09f6 348 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
88310e2e
VZ
349
350 TC_ITEM tcItem;
351 tcItem.mask = TCIF_IMAGE;
352
353 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
354}
355
8d34bf5c 356bool wxNotebook::SetPageImage(size_t nPage, int nImage)
88310e2e 357{
223d09f6 358 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
88310e2e
VZ
359
360 TC_ITEM tcItem;
361 tcItem.mask = TCIF_IMAGE;
362 tcItem.iImage = nImage;
363
364 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
365}
366
367void wxNotebook::SetImageList(wxImageList* imageList)
907f37b3 368{
07b8d7ec
VZ
369 wxNotebookBase::SetImageList(imageList);
370
371 if ( imageList )
1e6feb95 372 {
07b8d7ec 373 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
1e6feb95 374 }
b656febd
VS
375}
376
d9506e77
VZ
377// ----------------------------------------------------------------------------
378// wxNotebook size settings
379// ----------------------------------------------------------------------------
380
381void wxNotebook::SetPageSize(const wxSize& size)
382{
383 // transform the page size into the notebook size
384 RECT rc;
385 rc.left =
386 rc.top = 0;
387 rc.right = size.x;
388 rc.bottom = size.y;
389
390 TabCtrl_AdjustRect(GetHwnd(), TRUE, &rc);
391
392 // and now set it
393 SetSize(rc.right - rc.left, rc.bottom - rc.top);
394}
395
396void wxNotebook::SetPadding(const wxSize& padding)
397{
398 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
399}
42e69d6b
VZ
400
401// Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
402// style.
403void wxNotebook::SetTabSize(const wxSize& sz)
404{
405 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
406}
407
2ce7af35
JS
408wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
409{
410 wxSize sizeTotal = sizePage;
411
412 // We need to make getting tab size part of the wxWindows API.
413 wxSize tabSize(0, 0);
414 if (GetPageCount() > 0)
415 {
416 RECT rect;
417 TabCtrl_GetItemRect((HWND) GetHWND(), 0, & rect);
418 tabSize.x = rect.right - rect.left;
419 tabSize.y = rect.bottom - rect.top;
420 }
421 if ( HasFlag(wxNB_LEFT) || HasFlag(wxNB_RIGHT) )
422 {
423 sizeTotal.x += tabSize.x + 7;
424 sizeTotal.y += 7;
425 }
426 else
427 {
428 sizeTotal.x += 7;
429 sizeTotal.y += tabSize.y + 7;
430 }
431
432 return sizeTotal;
433}
434
2015f2b3
VZ
435void wxNotebook::AdjustPageSize(wxNotebookPage *page)
436{
437 wxCHECK_RET( page, _T("NULL page in wxNotebook::AdjustPageSize") );
438
439 RECT rc;
440 rc.left =
441 rc.top = 0;
442
443 // get the page size from the notebook size
444 GetSize((int *)&rc.right, (int *)&rc.bottom);
445 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
446
447 page->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
448}
449
88310e2e
VZ
450// ----------------------------------------------------------------------------
451// wxNotebook operations
452// ----------------------------------------------------------------------------
453
621793f4 454// remove one page from the notebook, without deleting
8d34bf5c 455wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
621793f4 456{
df7145da
VZ
457 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
458 if ( !pageRemoved )
459 return NULL;
621793f4 460
df7145da 461 TabCtrl_DeleteItem(m_hwnd, nPage);
621793f4 462
df7145da
VZ
463 if ( m_pages.IsEmpty() )
464 {
465 // no selection any more, the notebook becamse empty
466 m_nSelection = -1;
467 }
468 else // notebook still not empty
469 {
43a997b6
VZ
470 // change the selected page if it was deleted or became invalid
471 int selNew;
34a0c9f4 472 if ( m_nSelection == int(GetPageCount()) )
df7145da 473 {
43a997b6
VZ
474 // last page deleted, make the new last page the new selection
475 selNew = m_nSelection - 1;
df7145da 476 }
34a0c9f4 477 else if ( int(nPage) <= m_nSelection )
43a997b6
VZ
478 {
479 // we must show another page, even if it has the same index
480 selNew = m_nSelection;
481 }
482 else // nothing changes for the currently selected page
483 {
484 selNew = -1;
df7145da 485
43a997b6
VZ
486 // we still must refresh the current page: this needs to be done
487 // for some unknown reason if the tab control shows the up-down
488 // control (i.e. when there are too many pages) -- otherwise after
489 // deleting a page nothing at all is shown
68d31398
JS
490 if (m_nSelection >= 0)
491 m_pages[m_nSelection]->Refresh();
43a997b6
VZ
492 }
493
494 if ( selNew != -1 )
495 {
496 // m_nSelection must be always valid so reset it before calling
497 // SetSelection()
498 m_nSelection = -1;
499 SetSelection(selNew);
500 }
df7145da 501 }
47f12f58 502
df7145da 503 return pageRemoved;
621793f4
JS
504}
505
88310e2e
VZ
506// remove all pages
507bool wxNotebook::DeleteAllPages()
508{
8d34bf5c
VZ
509 size_t nPageCount = GetPageCount();
510 size_t nPage;
88310e2e 511 for ( nPage = 0; nPage < nPageCount; nPage++ )
1e6feb95 512 delete m_pages[nPage];
88310e2e 513
1e6feb95 514 m_pages.Clear();
88310e2e 515
907f37b3
VZ
516 TabCtrl_DeleteAllItems(m_hwnd);
517
47f12f58
JS
518 m_nSelection = -1;
519
88310e2e
VZ
520 return TRUE;
521}
522
88310e2e 523// same as AddPage() but does it at given position
8d34bf5c 524bool wxNotebook::InsertPage(size_t nPage,
88310e2e
VZ
525 wxNotebookPage *pPage,
526 const wxString& strText,
527 bool bSelect,
528 int imageId)
529{
22f3361e
VZ
530 wxCHECK_MSG( pPage != NULL, FALSE, _T("NULL page in wxNotebook::InsertPage") );
531 wxCHECK_MSG( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE,
532 _T("invalid index in wxNotebook::InsertPage") );
88310e2e 533
efa14cf2
VZ
534 wxASSERT_MSG( pPage->GetParent() == this,
535 _T("notebook pages must have notebook as parent") );
43427087 536
85b43fbf 537#if wxUSE_UXTHEME && wxUSE_UXTHEME_AUTO
c02e5a31
JS
538 static bool g_TestedForTheme = FALSE;
539 static bool g_UseTheme = FALSE;
540 if (!g_TestedForTheme)
541 {
542 int commCtrlVersion = wxTheApp->GetComCtl32Version() ;
543
544 g_UseTheme = (commCtrlVersion >= 600);
545 g_TestedForTheme = TRUE;
546 }
547
85b43fbf
JS
548 // Automatically apply the theme background,
549 // changing the colour of the panel to match the
550 // tab page colour. This won't work well with all
551 // themes but it's a start.
c02e5a31 552 if (g_UseTheme && wxUxThemeEngine::Get() && pPage->IsKindOf(CLASSINFO(wxPanel)))
85b43fbf
JS
553 {
554 ApplyThemeBackground(pPage, GetThemeBackgroundColour());
555 }
556#endif
557
22f3361e
VZ
558 // add a new tab to the control
559 // ----------------------------
58a8ab88 560
22f3361e
VZ
561 // init all fields to 0
562 TC_ITEM tcItem;
563 wxZeroMemory(tcItem);
58a8ab88 564
22f3361e
VZ
565 // set the image, if any
566 if ( imageId != -1 )
567 {
568 tcItem.mask |= TCIF_IMAGE;
569 tcItem.iImage = imageId;
570 }
88310e2e 571
22f3361e
VZ
572 // and the text
573 if ( !strText.IsEmpty() )
574 {
575 tcItem.mask |= TCIF_TEXT;
576 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
577 }
43427087 578
22f3361e
VZ
579 // fit the notebook page to the tab control's display area: this should be
580 // done before adding it to the notebook or TabCtrl_InsertItem() will
581 // change the notebooks size itself!
2015f2b3 582 AdjustPageSize(pPage);
43427087 583
22f3361e 584 // finally do insert it
2015f2b3
VZ
585 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 )
586 {
22f3361e 587 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
88310e2e 588
22f3361e
VZ
589 return FALSE;
590 }
88310e2e 591
22f3361e
VZ
592 // succeeded: save the pointer to the page
593 m_pages.Insert(pPage, nPage);
42e69d6b 594
2015f2b3
VZ
595 // for the first page (only) we need to adjust the size again because the
596 // notebook size changed: the tabs which hadn't been there before are now
597 // shown
598 if ( m_pages.GetCount() == 1 )
599 {
600 AdjustPageSize(pPage);
601 }
602
22f3361e
VZ
603 // hide the page: unless it is selected, it shouldn't be shown (and if it
604 // is selected it will be shown later)
605 HWND hwnd = GetWinHwnd(pPage);
606 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
0398b1d6 607
22f3361e
VZ
608 // this updates internal flag too -- otherwise it would get out of sync
609 // with the real state
610 pPage->Show(FALSE);
43427087 611
96d37807 612
22f3361e
VZ
613 // now deal with the selection
614 // ---------------------------
615
616 // if the inserted page is before the selected one, we must update the
617 // index of the selected page
34a0c9f4 618 if ( int(nPage) <= m_nSelection )
22f3361e
VZ
619 {
620 // one extra page added
621 m_nSelection++;
622 }
623
624 // some page should be selected: either this one or the first one if there
625 // is still no selection
626 int selNew = -1;
627 if ( bSelect )
628 selNew = nPage;
629 else if ( m_nSelection == -1 )
630 selNew = 0;
631
632 if ( selNew != -1 )
633 SetSelection(selNew);
634
635 return TRUE;
88310e2e
VZ
636}
637
e450aa69 638int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
ef094fa0
JS
639{
640 TC_HITTESTINFO hitTestInfo;
641 hitTestInfo.pt.x = pt.x;
642 hitTestInfo.pt.y = pt.y;
e450aa69 643 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
ef094fa0 644
e450aa69
VZ
645 if ( flags )
646 {
647 *flags = 0;
648
649 if ((hitTestInfo.flags & TCHT_NOWHERE) == TCHT_NOWHERE)
650 *flags |= wxNB_HITTEST_NOWHERE;
651 if ((hitTestInfo.flags & TCHT_ONITEM) == TCHT_ONITEM)
652 *flags |= wxNB_HITTEST_ONITEM;
653 if ((hitTestInfo.flags & TCHT_ONITEMICON) == TCHT_ONITEMICON)
654 *flags |= wxNB_HITTEST_ONICON;
655 if ((hitTestInfo.flags & TCHT_ONITEMLABEL) == TCHT_ONITEMLABEL)
656 *flags |= wxNB_HITTEST_ONLABEL;
657 }
ef094fa0
JS
658
659 return item;
660}
661
e450aa69 662
88310e2e
VZ
663// ----------------------------------------------------------------------------
664// wxNotebook callbacks
665// ----------------------------------------------------------------------------
666
9026ad85 667void wxNotebook::OnSize(wxSizeEvent& event)
88310e2e 668{
b5c3b538
VZ
669 // fit the notebook page to the tab control's display area
670 RECT rc;
671 rc.left = rc.top = 0;
672 GetSize((int *)&rc.right, (int *)&rc.bottom);
673
674 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
4b7f2165
VZ
675
676 int width = rc.right - rc.left,
677 height = rc.bottom - rc.top;
1e6feb95 678 size_t nCount = m_pages.Count();
c86f1403 679 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
1e6feb95 680 wxNotebookPage *pPage = m_pages[nPage];
4b7f2165 681 pPage->SetSize(rc.left, rc.top, width, height);
b5c3b538
VZ
682 }
683
88310e2e
VZ
684 event.Skip();
685}
686
687void wxNotebook::OnSelChange(wxNotebookEvent& event)
688{
689 // is it our tab control?
690 if ( event.GetEventObject() == this )
5d1d2d46 691 {
5d1d2d46
VZ
692 int sel = event.GetOldSelection();
693 if ( sel != -1 )
1e6feb95 694 m_pages[sel]->Show(FALSE);
0398b1d6 695
5d1d2d46
VZ
696 sel = event.GetSelection();
697 if ( sel != -1 )
698 {
1e6feb95 699 wxNotebookPage *pPage = m_pages[sel];
5d1d2d46
VZ
700 pPage->Show(TRUE);
701 pPage->SetFocus();
702 }
0398b1d6 703
5d1d2d46
VZ
704 m_nSelection = sel;
705 }
88310e2e
VZ
706
707 // we want to give others a chance to process this message as well
708 event.Skip();
709}
710
711void wxNotebook::OnSetFocus(wxFocusEvent& event)
712{
d9506e77
VZ
713 // this function is only called when the focus is explicitly set (i.e. from
714 // the program) to the notebook - in this case we don't need the
715 // complicated OnNavigationKey() logic because the programmer knows better
716 // what [s]he wants
88310e2e 717
d9506e77
VZ
718 // set focus to the currently selected page if any
719 if ( m_nSelection != -1 )
1e6feb95 720 m_pages[m_nSelection]->SetFocus();
d9506e77
VZ
721
722 event.Skip();
88310e2e
VZ
723}
724
725void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
726{
d9506e77
VZ
727 if ( event.IsWindowChange() ) {
728 // change pages
729 AdvanceSelection(event.GetDirection());
730 }
731 else {
732 // we get this event in 2 cases
733 //
734 // a) one of our pages might have generated it because the user TABbed
735 // out from it in which case we should propagate the event upwards and
736 // our parent will take care of setting the focus to prev/next sibling
737 //
738 // or
739 //
740 // b) the parent panel wants to give the focus to us so that we
741 // forward it to our selected page. We can't deal with this in
742 // OnSetFocus() because we don't know which direction the focus came
743 // from in this case and so can't choose between setting the focus to
744 // first or last panel child
d9506e77 745 wxWindow *parent = GetParent();
fc10daf3
MB
746 // the cast is here to fic a GCC ICE
747 if ( ((wxWindow*)event.GetEventObject()) == parent )
d9506e77
VZ
748 {
749 // no, it doesn't come from child, case (b): forward to a page
750 if ( m_nSelection != -1 )
751 {
752 // so that the page knows that the event comes from it's parent
753 // and is being propagated downwards
754 event.SetEventObject(this);
755
1e6feb95 756 wxWindow *page = m_pages[m_nSelection];
d9506e77
VZ
757 if ( !page->GetEventHandler()->ProcessEvent(event) )
758 {
759 page->SetFocus();
760 }
761 //else: page manages focus inside it itself
762 }
763 else
764 {
765 // we have no pages - still have to give focus to _something_
766 SetFocus();
767 }
768 }
769 else
770 {
771 // it comes from our child, case (a), pass to the parent
772 if ( parent ) {
773 event.SetCurrentFocus(this);
774 parent->GetEventHandler()->ProcessEvent(event);
775 }
776 }
88310e2e 777 }
88310e2e
VZ
778}
779
780// ----------------------------------------------------------------------------
781// wxNotebook base class virtuals
782// ----------------------------------------------------------------------------
b5c3b538 783
0b481c72
VZ
784#if wxUSE_CONSTRAINTS
785
b5c3b538
VZ
786// override these 2 functions to do nothing: everything is done in OnSize
787
4b7f2165 788void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
b5c3b538
VZ
789{
790 // don't set the sizes of the pages - their correct size is not yet known
791 wxControl::SetConstraintSizes(FALSE);
792}
793
4b7f2165 794bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
b5c3b538
VZ
795{
796 return TRUE;
797}
798
0b481c72
VZ
799#endif // wxUSE_CONSTRAINTS
800
0df3fbd7
VZ
801// ----------------------------------------------------------------------------
802// wxNotebook Windows message handlers
803// ----------------------------------------------------------------------------
804
805bool wxNotebook::MSWOnScroll(int orientation, WXWORD nSBCode,
806 WXWORD pos, WXHWND control)
807{
808 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
809 // up-down control
810 if ( control )
811 return FALSE;
812
813 return wxNotebookBase::MSWOnScroll(orientation, nSBCode, pos, control);
814}
815
a23fd0e1 816bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 817{
93a19f17 818 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
819
820 NMHDR* hdr = (NMHDR *)lParam;
821 switch ( hdr->code ) {
822 case TCN_SELCHANGE:
823 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
824 break;
825
826 case TCN_SELCHANGING:
827 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
828 break;
829
fd3f686c 830 default:
a23fd0e1 831 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
832 }
833
93a19f17
VZ
834 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
835 event.SetOldSelection(m_nSelection);
88310e2e 836 event.SetEventObject(this);
a23fd0e1 837 event.SetInt(idCtrl);
88310e2e 838
fd3f686c
VZ
839 bool processed = GetEventHandler()->ProcessEvent(event);
840 *result = !event.IsAllowed();
841 return processed;
88310e2e
VZ
842}
843
85b43fbf
JS
844// Windows only: attempts to get colour for UX theme page background
845wxColour wxNotebook::GetThemeBackgroundColour()
846{
847#if wxUSE_UXTHEME
848 if (wxUxThemeEngine::Get())
849 {
2142d7ba 850 wxUxThemeHandle hTheme(this, L"TAB");
85b43fbf
JS
851 if (hTheme)
852 {
853 // This is total guesswork.
854 // See PlatformSDK\Include\Tmschema.h for values
855 COLORREF themeColor;
2142d7ba
VZ
856 wxUxThemeEngine::Get()->GetThemeColor
857 (
858 hTheme,
859 10 /* TABP_BODY */,
860 1 /* NORMAL */,
861 3821, /* FILLCOLORHINT */
862 & themeColor
863 );
85b43fbf
JS
864
865 wxColour colour(GetRValue(themeColor), GetGValue(themeColor), GetBValue(themeColor));
866 return colour;
867 }
868 }
2142d7ba
VZ
869#endif // wxUSE_UXTHEME
870
85b43fbf
JS
871 return GetBackgroundColour();
872}
873
874// Windows only: attempts to apply the UX theme page background to this page
840015f0 875#if wxUSE_UXTHEME
85b43fbf 876void wxNotebook::ApplyThemeBackground(wxWindow* window, const wxColour& colour)
840015f0
MB
877#else
878void wxNotebook::ApplyThemeBackground(wxWindow*, const wxColour&)
879#endif
85b43fbf
JS
880{
881#if wxUSE_UXTHEME
882 // Don't set the background for buttons since this will
883 // switch it into ownerdraw mode
884 if (window->IsKindOf(CLASSINFO(wxButton)) && !window->IsKindOf(CLASSINFO(wxBitmapButton)))
885 // This is essential, otherwise you'll see dark grey
886 // corners in the buttons.
887 ((wxButton*)window)->wxControl::SetBackgroundColour(colour);
888 else if (window->IsKindOf(CLASSINFO(wxStaticText)) ||
889 window->IsKindOf(CLASSINFO(wxStaticBox)) ||
890 window->IsKindOf(CLASSINFO(wxStaticLine)) ||
891 window->IsKindOf(CLASSINFO(wxRadioButton)) ||
892 window->IsKindOf(CLASSINFO(wxRadioBox)) ||
893 window->IsKindOf(CLASSINFO(wxCheckBox)) ||
894 window->IsKindOf(CLASSINFO(wxBitmapButton)) ||
895 window->IsKindOf(CLASSINFO(wxSlider)) ||
896 window->IsKindOf(CLASSINFO(wxPanel)) ||
897 (window->IsKindOf(CLASSINFO(wxNotebook)) && (window != this)) ||
898 window->IsKindOf(CLASSINFO(wxScrolledWindow))
899 )
900 {
901 window->SetBackgroundColour(colour);
902 }
903
fc10daf3 904 for ( wxWindowList::compatibility_iterator node = window->GetChildren().GetFirst(); node; node = node->GetNext() )
85b43fbf
JS
905 {
906 wxWindow *child = node->GetData();
907 ApplyThemeBackground(child, colour);
908 }
909#endif
910}
911
c02e5a31
JS
912long wxNotebook::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
913{
914 static bool g_TestedForTheme = FALSE;
915 static bool g_UseTheme = FALSE;
916 switch ( nMsg )
917 {
918 case WM_ERASEBKGND:
919 {
920 if (!g_TestedForTheme)
921 {
922 int commCtrlVersion = wxTheApp->GetComCtl32Version() ;
923
924 g_UseTheme = (commCtrlVersion >= 600);
925 g_TestedForTheme = TRUE;
926 }
927
928 // If using XP themes, it seems we can get away
929 // with not drawing a background, which reduces flicker.
930 if (g_UseTheme)
931 return TRUE;
932 }
933 }
934
935 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
936}
937
938
1e6feb95 939#endif // wxUSE_NOTEBOOK