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