]> git.saurik.com Git - wxWidgets.git/blob - src/msw/notebook.cpp
rtti informations
[wxWidgets.git] / src / msw / notebook.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/notebook.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 11.06.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "notebook.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_NOTEBOOK
24
25 // wxWindows
26 #ifndef WX_PRECOMP
27 #include "wx/string.h"
28 #endif // WX_PRECOMP
29
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"
35 #include "wx/app.h"
36
37 #include "wx/msw/private.h"
38
39 // Windows standard headers
40 #ifndef __WIN95__
41 #error "wxNotebook is only supported Windows 95 and above"
42 #endif //Win95
43
44 #include <windowsx.h> // for SetWindowFont
45
46 #ifdef __GNUWIN32_OLD__
47 #include "wx/msw/gnuwin32/extra.h"
48 #endif
49
50 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
51 #include <commctrl.h>
52 #endif
53
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
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
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
98 // ----------------------------------------------------------------------------
99 // event table
100 // ----------------------------------------------------------------------------
101
102 #include <wx/listimpl.cpp>
103
104 WX_DEFINE_LIST( wxNotebookPageInfoList ) ;
105
106 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
107 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
108
109 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
110 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
111
112 EVT_SIZE(wxNotebook::OnSize)
113
114 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
115
116 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
117 END_EVENT_TABLE()
118
119 #if wxUSE_EXTENDED_RTTI
120
121 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook, wxControl,"wx/notebook.h")
122 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo, wxObject , "wx/notebook.h" )
123
124 template<> const wxTypeInfo* wxGetTypeInfo( wxNotebookPageInfoList * )
125 {
126 static wxCollectionTypeInfo s_typeInfo( (wxTypeInfo*) wxGetTypeInfo( (wxNotebookPageInfo **) NULL) ) ;
127 return &s_typeInfo ;
128 }
129
130 WX_BEGIN_PROPERTIES_TABLE(wxNotebook)
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 */
141 WX_END_PROPERTIES_TABLE()
142
143 WX_BEGIN_HANDLERS_TABLE(wxNotebook)
144 WX_END_HANDLERS_TABLE()
145
146 WX_CONSTRUCTOR_4( wxNotebook , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
147
148
149 WX_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 )
154 WX_END_PROPERTIES_TABLE()
155
156 WX_BEGIN_HANDLERS_TABLE(wxNotebookPageInfo)
157 WX_END_HANDLERS_TABLE()
158
159 WX_CONSTRUCTOR_4( wxNotebookPageInfo , wxNotebookPage* , Page , wxString , Text , bool , Selected , int , ImageId )
160
161 #else
162 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
163 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo, wxObject )
164 #endif
165 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
166
167 // ============================================================================
168 // implementation
169 // ============================================================================
170
171 // ----------------------------------------------------------------------------
172 // wxNotebook construction
173 // ----------------------------------------------------------------------------
174
175 const 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
188 // common part of all ctors
189 void wxNotebook::Init()
190 {
191 m_imageList = NULL;
192 m_nSelection = -1;
193 }
194
195 // default for dynamic class
196 wxNotebook::wxNotebook()
197 {
198 Init();
199 }
200
201 // the same arguments as for wxControl
202 wxNotebook::wxNotebook(wxWindow *parent,
203 wxWindowID id,
204 const wxPoint& pos,
205 const wxSize& size,
206 long style,
207 const wxString& name)
208 {
209 Init();
210
211 Create(parent, id, pos, size, style, name);
212 }
213
214 // Create() function
215 bool wxNotebook::Create(wxWindow *parent,
216 wxWindowID id,
217 const wxPoint& pos,
218 const wxSize& size,
219 long style,
220 const wxString& name)
221 {
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
236 if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL,
237 wxDefaultValidator, name) )
238 return FALSE;
239
240 if ( !MSWCreateControl(WC_TABCONTROL, wxEmptyString, pos, size) )
241 return FALSE;
242
243 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE)));
244
245 return TRUE;
246 }
247
248 WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const
249 {
250 WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle);
251
252 tabStyle |= WS_TABSTOP | TCS_TABS;
253
254 if ( style & wxNB_MULTILINE )
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;
275 }
276
277 // ----------------------------------------------------------------------------
278 // wxNotebook accessors
279 // ----------------------------------------------------------------------------
280
281 int wxNotebook::GetPageCount() const
282 {
283 // consistency check
284 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) );
285
286 return m_pages.Count();
287 }
288
289 int wxNotebook::GetRowCount() const
290 {
291 return TabCtrl_GetRowCount(m_hwnd);
292 }
293
294 int wxNotebook::SetSelection(int nPage)
295 {
296 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
297
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 }
313
314 return m_nSelection;
315 }
316
317 bool wxNotebook::SetPageText(int nPage, const wxString& strText)
318 {
319 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
320
321 TC_ITEM tcItem;
322 tcItem.mask = TCIF_TEXT;
323 tcItem.pszText = (wxChar *)strText.c_str();
324
325 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
326 }
327
328 wxString wxNotebook::GetPageText(int nPage) const
329 {
330 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
331
332 wxChar buf[256];
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
345 int wxNotebook::GetPageImage(int nPage) const
346 {
347 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
348
349 TC_ITEM tcItem;
350 tcItem.mask = TCIF_IMAGE;
351
352 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
353 }
354
355 bool wxNotebook::SetPageImage(int nPage, int nImage)
356 {
357 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
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
366 void wxNotebook::SetImageList(wxImageList* imageList)
367 {
368 wxNotebookBase::SetImageList(imageList);
369
370 if ( imageList )
371 {
372 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
373 }
374 }
375
376 // ----------------------------------------------------------------------------
377 // wxNotebook size settings
378 // ----------------------------------------------------------------------------
379
380 void 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
395 void wxNotebook::SetPadding(const wxSize& padding)
396 {
397 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
398 }
399
400 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
401 // style.
402 void wxNotebook::SetTabSize(const wxSize& sz)
403 {
404 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
405 }
406
407 wxSize 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
434 void 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
449 // ----------------------------------------------------------------------------
450 // wxNotebook operations
451 // ----------------------------------------------------------------------------
452
453 // remove one page from the notebook, without deleting
454 wxNotebookPage *wxNotebook::DoRemovePage(int nPage)
455 {
456 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
457 if ( !pageRemoved )
458 return NULL;
459
460 TabCtrl_DeleteItem(m_hwnd, nPage);
461
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 {
469 // change the selected page if it was deleted or became invalid
470 int selNew;
471 if ( m_nSelection == GetPageCount() )
472 {
473 // last page deleted, make the new last page the new selection
474 selNew = m_nSelection - 1;
475 }
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;
484
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
489 if (m_nSelection >= 0)
490 m_pages[m_nSelection]->Refresh();
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 }
500 }
501
502 return pageRemoved;
503 }
504
505 // remove all pages
506 bool wxNotebook::DeleteAllPages()
507 {
508 int nPageCount = GetPageCount();
509 int nPage;
510 for ( nPage = 0; nPage < nPageCount; nPage++ )
511 delete m_pages[nPage];
512
513 m_pages.Clear();
514
515 TabCtrl_DeleteAllItems(m_hwnd);
516
517 m_nSelection = -1;
518
519 return TRUE;
520 }
521
522 // same as AddPage() but does it at given position
523 bool wxNotebook::InsertPage(int nPage,
524 wxNotebookPage *pPage,
525 const wxString& strText,
526 bool bSelect,
527 int imageId)
528 {
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") );
532
533 wxASSERT_MSG( pPage->GetParent() == this,
534 _T("notebook pages must have notebook as parent") );
535
536 #if wxUSE_UXTHEME && wxUSE_UXTHEME_AUTO
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
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.
551 if (g_UseTheme && wxUxThemeEngine::Get() && pPage->IsKindOf(CLASSINFO(wxPanel)))
552 {
553 ApplyThemeBackground(pPage, GetThemeBackgroundColour());
554 }
555 #endif
556
557 // add a new tab to the control
558 // ----------------------------
559
560 // init all fields to 0
561 TC_ITEM tcItem;
562 wxZeroMemory(tcItem);
563
564 // set the image, if any
565 if ( imageId != -1 )
566 {
567 tcItem.mask |= TCIF_IMAGE;
568 tcItem.iImage = imageId;
569 }
570
571 // and the text
572 if ( !strText.IsEmpty() )
573 {
574 tcItem.mask |= TCIF_TEXT;
575 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
576 }
577
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!
581 AdjustPageSize(pPage);
582
583 // finally do insert it
584 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 )
585 {
586 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
587
588 return FALSE;
589 }
590
591 // succeeded: save the pointer to the page
592 m_pages.Insert(pPage, nPage);
593
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
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);
606
607 // this updates internal flag too -- otherwise it would get out of sync
608 // with the real state
609 pPage->Show(FALSE);
610
611
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;
635 }
636
637 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
638 {
639 TC_HITTESTINFO hitTestInfo;
640 hitTestInfo.pt.x = pt.x;
641 hitTestInfo.pt.y = pt.y;
642 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
643
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 }
657
658 return item;
659 }
660
661
662 // ----------------------------------------------------------------------------
663 // wxNotebook callbacks
664 // ----------------------------------------------------------------------------
665
666 void wxNotebook::OnSize(wxSizeEvent& event)
667 {
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);
674
675 int width = rc.right - rc.left,
676 height = rc.bottom - rc.top;
677 size_t nCount = m_pages.Count();
678 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
679 wxNotebookPage *pPage = m_pages[nPage];
680 pPage->SetSize(rc.left, rc.top, width, height);
681 }
682
683 event.Skip();
684 }
685
686 void wxNotebook::OnSelChange(wxNotebookEvent& event)
687 {
688 // is it our tab control?
689 if ( event.GetEventObject() == this )
690 {
691 int sel = event.GetOldSelection();
692 if ( sel != -1 )
693 m_pages[sel]->Show(FALSE);
694
695 sel = event.GetSelection();
696 if ( sel != -1 )
697 {
698 wxNotebookPage *pPage = m_pages[sel];
699 pPage->Show(TRUE);
700 pPage->SetFocus();
701 }
702
703 m_nSelection = sel;
704 }
705
706 // we want to give others a chance to process this message as well
707 event.Skip();
708 }
709
710 void wxNotebook::OnSetFocus(wxFocusEvent& event)
711 {
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
716
717 // set focus to the currently selected page if any
718 if ( m_nSelection != -1 )
719 m_pages[m_nSelection]->SetFocus();
720
721 event.Skip();
722 }
723
724 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
725 {
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
744 wxWindow *parent = GetParent();
745 // the cast is here to fic a GCC ICE
746 if ( ((wxWindow*)event.GetEventObject()) == parent )
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
755 wxWindow *page = m_pages[m_nSelection];
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 }
776 }
777 }
778
779 // ----------------------------------------------------------------------------
780 // wxNotebook base class virtuals
781 // ----------------------------------------------------------------------------
782
783 #if wxUSE_CONSTRAINTS
784
785 // override these 2 functions to do nothing: everything is done in OnSize
786
787 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
788 {
789 // don't set the sizes of the pages - their correct size is not yet known
790 wxControl::SetConstraintSizes(FALSE);
791 }
792
793 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
794 {
795 return TRUE;
796 }
797
798 #endif // wxUSE_CONSTRAINTS
799
800 // ----------------------------------------------------------------------------
801 // wxNotebook Windows message handlers
802 // ----------------------------------------------------------------------------
803
804 bool 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
815 bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
816 {
817 wxNotebookEvent event(wxEVT_NULL, m_windowId);
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
829 default:
830 return wxControl::MSWOnNotify(idCtrl, lParam, result);
831 }
832
833 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
834 event.SetOldSelection(m_nSelection);
835 event.SetEventObject(this);
836 event.SetInt(idCtrl);
837
838 bool processed = GetEventHandler()->ProcessEvent(event);
839 *result = !event.IsAllowed();
840 return processed;
841 }
842
843 // Windows only: attempts to get colour for UX theme page background
844 wxColour wxNotebook::GetThemeBackgroundColour()
845 {
846 #if wxUSE_UXTHEME
847 if (wxUxThemeEngine::Get())
848 {
849 wxUxThemeHandle hTheme(this, L"TAB");
850 if (hTheme)
851 {
852 // This is total guesswork.
853 // See PlatformSDK\Include\Tmschema.h for values
854 COLORREF themeColor;
855 wxUxThemeEngine::Get()->GetThemeColor
856 (
857 hTheme,
858 10 /* TABP_BODY */,
859 1 /* NORMAL */,
860 3821, /* FILLCOLORHINT */
861 & themeColor
862 );
863
864 wxColour colour(GetRValue(themeColor), GetGValue(themeColor), GetBValue(themeColor));
865 return colour;
866 }
867 }
868 #endif // wxUSE_UXTHEME
869
870 return GetBackgroundColour();
871 }
872
873 // Windows only: attempts to apply the UX theme page background to this page
874 #if wxUSE_UXTHEME
875 void wxNotebook::ApplyThemeBackground(wxWindow* window, const wxColour& colour)
876 #else
877 void wxNotebook::ApplyThemeBackground(wxWindow*, const wxColour&)
878 #endif
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
903 for ( wxWindowList::compatibility_iterator node = window->GetChildren().GetFirst(); node; node = node->GetNext() )
904 {
905 wxWindow *child = node->GetData();
906 ApplyThemeBackground(child, colour);
907 }
908 #endif
909 }
910
911 long 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
938 #endif // wxUSE_NOTEBOOK