]> git.saurik.com Git - wxWidgets.git/blob - src/msw/notebook.cpp
9e67f1c74c7ac1bab42c466fa413b7a537a8abb4
[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) < 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 WX_COLLECTION_TYPE_INFO( wxNotebookPageInfo * , wxNotebookPageInfoList ) ;
125
126 template<> void wxCollectionToVariantArray( wxNotebookPageInfoList const &theList, wxxVariantArray &value)
127 {
128 wxListCollectionToVariantArray( theList , value ) ;
129 }
130
131 WX_BEGIN_PROPERTIES_TABLE(wxNotebook)
132 WX_PROPERTY_COLLECTION( PageInfos , wxNotebookPageInfoList , wxNotebookPageInfo* , AddPageInfo , GetPageInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
133 /*
134 notebookpage
135 object
136 object_ref
137 label
138 selected
139 style
140 usenotebooksizer
141 */
142 WX_END_PROPERTIES_TABLE()
143
144 WX_BEGIN_HANDLERS_TABLE(wxNotebook)
145 WX_END_HANDLERS_TABLE()
146
147 WX_CONSTRUCTOR_4( wxNotebook , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
148
149
150 WX_BEGIN_PROPERTIES_TABLE(wxNotebookPageInfo)
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"))
155 WX_END_PROPERTIES_TABLE()
156
157 WX_BEGIN_HANDLERS_TABLE(wxNotebookPageInfo)
158 WX_END_HANDLERS_TABLE()
159
160 WX_CONSTRUCTOR_4( wxNotebookPageInfo , wxNotebookPage* , Page , wxString , Text , bool , Selected , int , ImageId )
161
162 #else
163 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
164 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo, wxObject )
165 #endif
166 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
167
168 // ============================================================================
169 // implementation
170 // ============================================================================
171
172 // ----------------------------------------------------------------------------
173 // wxNotebook construction
174 // ----------------------------------------------------------------------------
175
176 const wxNotebookPageInfoList& wxNotebook::GetPageInfos() const
177 {
178 wxNotebookPageInfoList* list = const_cast< wxNotebookPageInfoList* >( &m_pageInfos ) ;
179 WX_CLEAR_LIST( wxNotebookPageInfoList , *list ) ;
180 for( size_t i = 0 ; i < GetPageCount() ; ++i )
181 {
182 wxNotebookPageInfo *info = new wxNotebookPageInfo() ;
183 info->Create( const_cast<wxNotebook*>(this)->GetPage(i) , GetPageText(i) , GetSelection() == int(i) , GetPageImage(i) ) ;
184 list->Append( info ) ;
185 }
186 return m_pageInfos ;
187 }
188
189 // common part of all ctors
190 void wxNotebook::Init()
191 {
192 m_imageList = NULL;
193 m_nSelection = -1;
194 }
195
196 // default for dynamic class
197 wxNotebook::wxNotebook()
198 {
199 Init();
200 }
201
202 // the same arguments as for wxControl
203 wxNotebook::wxNotebook(wxWindow *parent,
204 wxWindowID id,
205 const wxPoint& pos,
206 const wxSize& size,
207 long style,
208 const wxString& name)
209 {
210 Init();
211
212 Create(parent, id, pos, size, style, name);
213 }
214
215 // Create() function
216 bool wxNotebook::Create(wxWindow *parent,
217 wxWindowID id,
218 const wxPoint& pos,
219 const wxSize& size,
220 long style,
221 const wxString& name)
222 {
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
237 if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL,
238 wxDefaultValidator, name) )
239 return FALSE;
240
241 if ( !MSWCreateControl(WC_TABCONTROL, wxEmptyString, pos, size) )
242 return FALSE;
243
244 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE)));
245
246 return TRUE;
247 }
248
249 WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const
250 {
251 WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle);
252
253 tabStyle |= WS_TABSTOP | TCS_TABS;
254
255 if ( style & wxNB_MULTILINE )
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;
276 }
277
278 // ----------------------------------------------------------------------------
279 // wxNotebook accessors
280 // ----------------------------------------------------------------------------
281
282 size_t wxNotebook::GetPageCount() const
283 {
284 // consistency check
285 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) );
286
287 return m_pages.Count();
288 }
289
290 int wxNotebook::GetRowCount() const
291 {
292 return TabCtrl_GetRowCount(m_hwnd);
293 }
294
295 int wxNotebook::SetSelection(size_t nPage)
296 {
297 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
298
299 if ( int(nPage) != m_nSelection )
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 }
314
315 return m_nSelection;
316 }
317
318 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
319 {
320 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
321
322 TC_ITEM tcItem;
323 tcItem.mask = TCIF_TEXT;
324 tcItem.pszText = (wxChar *)strText.c_str();
325
326 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
327 }
328
329 wxString wxNotebook::GetPageText(size_t nPage) const
330 {
331 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
332
333 wxChar buf[256];
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
346 int wxNotebook::GetPageImage(size_t nPage) const
347 {
348 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
349
350 TC_ITEM tcItem;
351 tcItem.mask = TCIF_IMAGE;
352
353 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
354 }
355
356 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
357 {
358 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
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
367 void wxNotebook::SetImageList(wxImageList* imageList)
368 {
369 wxNotebookBase::SetImageList(imageList);
370
371 if ( imageList )
372 {
373 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
374 }
375 }
376
377 // ----------------------------------------------------------------------------
378 // wxNotebook size settings
379 // ----------------------------------------------------------------------------
380
381 void 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
396 void wxNotebook::SetPadding(const wxSize& padding)
397 {
398 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
399 }
400
401 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
402 // style.
403 void wxNotebook::SetTabSize(const wxSize& sz)
404 {
405 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
406 }
407
408 wxSize 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
435 void 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
450 // ----------------------------------------------------------------------------
451 // wxNotebook operations
452 // ----------------------------------------------------------------------------
453
454 // remove one page from the notebook, without deleting
455 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
456 {
457 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
458 if ( !pageRemoved )
459 return NULL;
460
461 TabCtrl_DeleteItem(m_hwnd, nPage);
462
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 {
470 // change the selected page if it was deleted or became invalid
471 int selNew;
472 if ( m_nSelection == int(GetPageCount()) )
473 {
474 // last page deleted, make the new last page the new selection
475 selNew = m_nSelection - 1;
476 }
477 else if ( int(nPage) <= m_nSelection )
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;
485
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
490 if (m_nSelection >= 0)
491 m_pages[m_nSelection]->Refresh();
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 }
501 }
502
503 return pageRemoved;
504 }
505
506 // remove all pages
507 bool wxNotebook::DeleteAllPages()
508 {
509 size_t nPageCount = GetPageCount();
510 size_t nPage;
511 for ( nPage = 0; nPage < nPageCount; nPage++ )
512 delete m_pages[nPage];
513
514 m_pages.Clear();
515
516 TabCtrl_DeleteAllItems(m_hwnd);
517
518 m_nSelection = -1;
519
520 return TRUE;
521 }
522
523 // same as AddPage() but does it at given position
524 bool wxNotebook::InsertPage(size_t nPage,
525 wxNotebookPage *pPage,
526 const wxString& strText,
527 bool bSelect,
528 int imageId)
529 {
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") );
533
534 wxASSERT_MSG( pPage->GetParent() == this,
535 _T("notebook pages must have notebook as parent") );
536
537 #if wxUSE_UXTHEME && wxUSE_UXTHEME_AUTO
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
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.
552 if (g_UseTheme && wxUxThemeEngine::Get() && pPage->IsKindOf(CLASSINFO(wxPanel)))
553 {
554 ApplyThemeBackground(pPage, GetThemeBackgroundColour());
555 }
556 #endif
557
558 // add a new tab to the control
559 // ----------------------------
560
561 // init all fields to 0
562 TC_ITEM tcItem;
563 wxZeroMemory(tcItem);
564
565 // set the image, if any
566 if ( imageId != -1 )
567 {
568 tcItem.mask |= TCIF_IMAGE;
569 tcItem.iImage = imageId;
570 }
571
572 // and the text
573 if ( !strText.IsEmpty() )
574 {
575 tcItem.mask |= TCIF_TEXT;
576 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
577 }
578
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!
582 AdjustPageSize(pPage);
583
584 // finally do insert it
585 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 )
586 {
587 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
588
589 return FALSE;
590 }
591
592 // succeeded: save the pointer to the page
593 m_pages.Insert(pPage, nPage);
594
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
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);
607
608 // this updates internal flag too -- otherwise it would get out of sync
609 // with the real state
610 pPage->Show(FALSE);
611
612
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
618 if ( int(nPage) <= m_nSelection )
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;
636 }
637
638 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
639 {
640 TC_HITTESTINFO hitTestInfo;
641 hitTestInfo.pt.x = pt.x;
642 hitTestInfo.pt.y = pt.y;
643 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
644
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 }
658
659 return item;
660 }
661
662
663 // ----------------------------------------------------------------------------
664 // wxNotebook callbacks
665 // ----------------------------------------------------------------------------
666
667 void wxNotebook::OnSize(wxSizeEvent& event)
668 {
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);
675
676 int width = rc.right - rc.left,
677 height = rc.bottom - rc.top;
678 size_t nCount = m_pages.Count();
679 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
680 wxNotebookPage *pPage = m_pages[nPage];
681 pPage->SetSize(rc.left, rc.top, width, height);
682 }
683
684 event.Skip();
685 }
686
687 void wxNotebook::OnSelChange(wxNotebookEvent& event)
688 {
689 // is it our tab control?
690 if ( event.GetEventObject() == this )
691 {
692 int sel = event.GetOldSelection();
693 if ( sel != -1 )
694 m_pages[sel]->Show(FALSE);
695
696 sel = event.GetSelection();
697 if ( sel != -1 )
698 {
699 wxNotebookPage *pPage = m_pages[sel];
700 pPage->Show(TRUE);
701 pPage->SetFocus();
702 }
703
704 m_nSelection = sel;
705 }
706
707 // we want to give others a chance to process this message as well
708 event.Skip();
709 }
710
711 void wxNotebook::OnSetFocus(wxFocusEvent& event)
712 {
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
717
718 // set focus to the currently selected page if any
719 if ( m_nSelection != -1 )
720 m_pages[m_nSelection]->SetFocus();
721
722 event.Skip();
723 }
724
725 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
726 {
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
745 wxWindow *parent = GetParent();
746 // the cast is here to fic a GCC ICE
747 if ( ((wxWindow*)event.GetEventObject()) == parent )
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
756 wxWindow *page = m_pages[m_nSelection];
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 }
777 }
778 }
779
780 // ----------------------------------------------------------------------------
781 // wxNotebook base class virtuals
782 // ----------------------------------------------------------------------------
783
784 #if wxUSE_CONSTRAINTS
785
786 // override these 2 functions to do nothing: everything is done in OnSize
787
788 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
789 {
790 // don't set the sizes of the pages - their correct size is not yet known
791 wxControl::SetConstraintSizes(FALSE);
792 }
793
794 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
795 {
796 return TRUE;
797 }
798
799 #endif // wxUSE_CONSTRAINTS
800
801 // ----------------------------------------------------------------------------
802 // wxNotebook Windows message handlers
803 // ----------------------------------------------------------------------------
804
805 bool 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
816 bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
817 {
818 wxNotebookEvent event(wxEVT_NULL, m_windowId);
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
830 default:
831 return wxControl::MSWOnNotify(idCtrl, lParam, result);
832 }
833
834 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
835 event.SetOldSelection(m_nSelection);
836 event.SetEventObject(this);
837 event.SetInt(idCtrl);
838
839 bool processed = GetEventHandler()->ProcessEvent(event);
840 *result = !event.IsAllowed();
841 return processed;
842 }
843
844 // Windows only: attempts to get colour for UX theme page background
845 wxColour wxNotebook::GetThemeBackgroundColour()
846 {
847 #if wxUSE_UXTHEME
848 if (wxUxThemeEngine::Get())
849 {
850 wxUxThemeHandle hTheme(this, L"TAB");
851 if (hTheme)
852 {
853 // This is total guesswork.
854 // See PlatformSDK\Include\Tmschema.h for values
855 COLORREF themeColor;
856 wxUxThemeEngine::Get()->GetThemeColor
857 (
858 hTheme,
859 10 /* TABP_BODY */,
860 1 /* NORMAL */,
861 3821, /* FILLCOLORHINT */
862 & themeColor
863 );
864
865 wxColour colour(GetRValue(themeColor), GetGValue(themeColor), GetBValue(themeColor));
866 return colour;
867 }
868 }
869 #endif // wxUSE_UXTHEME
870
871 return GetBackgroundColour();
872 }
873
874 // Windows only: attempts to apply the UX theme page background to this page
875 #if wxUSE_UXTHEME
876 void wxNotebook::ApplyThemeBackground(wxWindow* window, const wxColour& colour)
877 #else
878 void wxNotebook::ApplyThemeBackground(wxWindow*, const wxColour&)
879 #endif
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
904 for ( wxWindowList::compatibility_iterator node = window->GetChildren().GetFirst(); node; node = node->GetNext() )
905 {
906 wxWindow *child = node->GetData();
907 ApplyThemeBackground(child, colour);
908 }
909 #endif
910 }
911
912 long 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
939 #endif // wxUSE_NOTEBOOK