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