]> git.saurik.com Git - wxWidgets.git/blob - src/msw/notebook.cpp
wxStrdup(NULL) doesn't work
[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 template<> void wxCollectionToVariantArray( wxNotebookPageInfoList const &theList, wxxVariantArray &value)
131 {
132 wxListCollectionToVariantArray( theList , value ) ;
133 }
134
135 WX_BEGIN_PROPERTIES_TABLE(wxNotebook)
136 WX_PROPERTY_COLLECTION( PageInfos , wxNotebookPageInfoList , wxNotebookPageInfo* , AddPageInfo , GetPageInfos )
137 /*
138 notebookpage
139 object
140 object_ref
141 label
142 selected
143 style
144 usenotebooksizer
145 */
146 WX_END_PROPERTIES_TABLE()
147
148 WX_BEGIN_HANDLERS_TABLE(wxNotebook)
149 WX_END_HANDLERS_TABLE()
150
151 WX_CONSTRUCTOR_4( wxNotebook , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
152
153
154 WX_BEGIN_PROPERTIES_TABLE(wxNotebookPageInfo)
155 WX_READONLY_PROPERTY( Page , wxNotebookPage* , GetPage , )
156 WX_READONLY_PROPERTY( Text , wxString , GetText , wxEmptyString )
157 WX_READONLY_PROPERTY( Selected , bool , GetSelected , false )
158 WX_READONLY_PROPERTY( ImageId , int , GetImageId , -1 )
159 WX_END_PROPERTIES_TABLE()
160
161 WX_BEGIN_HANDLERS_TABLE(wxNotebookPageInfo)
162 WX_END_HANDLERS_TABLE()
163
164 WX_CONSTRUCTOR_4( wxNotebookPageInfo , wxNotebookPage* , Page , wxString , Text , bool , Selected , int , ImageId )
165
166 #else
167 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
168 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo, wxObject )
169 #endif
170 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
171
172 // ============================================================================
173 // implementation
174 // ============================================================================
175
176 // ----------------------------------------------------------------------------
177 // wxNotebook construction
178 // ----------------------------------------------------------------------------
179
180 const 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
193 // common part of all ctors
194 void wxNotebook::Init()
195 {
196 m_imageList = NULL;
197 m_nSelection = -1;
198 }
199
200 // default for dynamic class
201 wxNotebook::wxNotebook()
202 {
203 Init();
204 }
205
206 // the same arguments as for wxControl
207 wxNotebook::wxNotebook(wxWindow *parent,
208 wxWindowID id,
209 const wxPoint& pos,
210 const wxSize& size,
211 long style,
212 const wxString& name)
213 {
214 Init();
215
216 Create(parent, id, pos, size, style, name);
217 }
218
219 // Create() function
220 bool wxNotebook::Create(wxWindow *parent,
221 wxWindowID id,
222 const wxPoint& pos,
223 const wxSize& size,
224 long style,
225 const wxString& name)
226 {
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
241 if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL,
242 wxDefaultValidator, name) )
243 return FALSE;
244
245 if ( !MSWCreateControl(WC_TABCONTROL, wxEmptyString, pos, size) )
246 return FALSE;
247
248 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE)));
249
250 return TRUE;
251 }
252
253 WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const
254 {
255 WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle);
256
257 tabStyle |= WS_TABSTOP | TCS_TABS;
258
259 if ( style & wxNB_MULTILINE )
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;
280 }
281
282 // ----------------------------------------------------------------------------
283 // wxNotebook accessors
284 // ----------------------------------------------------------------------------
285
286 int wxNotebook::GetPageCount() const
287 {
288 // consistency check
289 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) );
290
291 return m_pages.Count();
292 }
293
294 int wxNotebook::GetRowCount() const
295 {
296 return TabCtrl_GetRowCount(m_hwnd);
297 }
298
299 int wxNotebook::SetSelection(int nPage)
300 {
301 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
302
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 }
318
319 return m_nSelection;
320 }
321
322 bool wxNotebook::SetPageText(int nPage, const wxString& strText)
323 {
324 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
325
326 TC_ITEM tcItem;
327 tcItem.mask = TCIF_TEXT;
328 tcItem.pszText = (wxChar *)strText.c_str();
329
330 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
331 }
332
333 wxString wxNotebook::GetPageText(int nPage) const
334 {
335 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
336
337 wxChar buf[256];
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
350 int wxNotebook::GetPageImage(int nPage) const
351 {
352 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
353
354 TC_ITEM tcItem;
355 tcItem.mask = TCIF_IMAGE;
356
357 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
358 }
359
360 bool wxNotebook::SetPageImage(int nPage, int nImage)
361 {
362 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
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
371 void wxNotebook::SetImageList(wxImageList* imageList)
372 {
373 wxNotebookBase::SetImageList(imageList);
374
375 if ( imageList )
376 {
377 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
378 }
379 }
380
381 // ----------------------------------------------------------------------------
382 // wxNotebook size settings
383 // ----------------------------------------------------------------------------
384
385 void 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
400 void wxNotebook::SetPadding(const wxSize& padding)
401 {
402 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
403 }
404
405 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
406 // style.
407 void wxNotebook::SetTabSize(const wxSize& sz)
408 {
409 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
410 }
411
412 wxSize 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
439 void 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
454 // ----------------------------------------------------------------------------
455 // wxNotebook operations
456 // ----------------------------------------------------------------------------
457
458 // remove one page from the notebook, without deleting
459 wxNotebookPage *wxNotebook::DoRemovePage(int nPage)
460 {
461 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
462 if ( !pageRemoved )
463 return NULL;
464
465 TabCtrl_DeleteItem(m_hwnd, nPage);
466
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 {
474 // change the selected page if it was deleted or became invalid
475 int selNew;
476 if ( m_nSelection == GetPageCount() )
477 {
478 // last page deleted, make the new last page the new selection
479 selNew = m_nSelection - 1;
480 }
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;
489
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
494 if (m_nSelection >= 0)
495 m_pages[m_nSelection]->Refresh();
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 }
505 }
506
507 return pageRemoved;
508 }
509
510 // remove all pages
511 bool wxNotebook::DeleteAllPages()
512 {
513 int nPageCount = GetPageCount();
514 int nPage;
515 for ( nPage = 0; nPage < nPageCount; nPage++ )
516 delete m_pages[nPage];
517
518 m_pages.Clear();
519
520 TabCtrl_DeleteAllItems(m_hwnd);
521
522 m_nSelection = -1;
523
524 return TRUE;
525 }
526
527 // same as AddPage() but does it at given position
528 bool wxNotebook::InsertPage(int nPage,
529 wxNotebookPage *pPage,
530 const wxString& strText,
531 bool bSelect,
532 int imageId)
533 {
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") );
537
538 wxASSERT_MSG( pPage->GetParent() == this,
539 _T("notebook pages must have notebook as parent") );
540
541 #if wxUSE_UXTHEME && wxUSE_UXTHEME_AUTO
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
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.
556 if (g_UseTheme && wxUxThemeEngine::Get() && pPage->IsKindOf(CLASSINFO(wxPanel)))
557 {
558 ApplyThemeBackground(pPage, GetThemeBackgroundColour());
559 }
560 #endif
561
562 // add a new tab to the control
563 // ----------------------------
564
565 // init all fields to 0
566 TC_ITEM tcItem;
567 wxZeroMemory(tcItem);
568
569 // set the image, if any
570 if ( imageId != -1 )
571 {
572 tcItem.mask |= TCIF_IMAGE;
573 tcItem.iImage = imageId;
574 }
575
576 // and the text
577 if ( !strText.IsEmpty() )
578 {
579 tcItem.mask |= TCIF_TEXT;
580 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
581 }
582
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!
586 AdjustPageSize(pPage);
587
588 // finally do insert it
589 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 )
590 {
591 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
592
593 return FALSE;
594 }
595
596 // succeeded: save the pointer to the page
597 m_pages.Insert(pPage, nPage);
598
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
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);
611
612 // this updates internal flag too -- otherwise it would get out of sync
613 // with the real state
614 pPage->Show(FALSE);
615
616
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;
640 }
641
642 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
643 {
644 TC_HITTESTINFO hitTestInfo;
645 hitTestInfo.pt.x = pt.x;
646 hitTestInfo.pt.y = pt.y;
647 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
648
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 }
662
663 return item;
664 }
665
666
667 // ----------------------------------------------------------------------------
668 // wxNotebook callbacks
669 // ----------------------------------------------------------------------------
670
671 void wxNotebook::OnSize(wxSizeEvent& event)
672 {
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);
679
680 int width = rc.right - rc.left,
681 height = rc.bottom - rc.top;
682 size_t nCount = m_pages.Count();
683 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
684 wxNotebookPage *pPage = m_pages[nPage];
685 pPage->SetSize(rc.left, rc.top, width, height);
686 }
687
688 event.Skip();
689 }
690
691 void wxNotebook::OnSelChange(wxNotebookEvent& event)
692 {
693 // is it our tab control?
694 if ( event.GetEventObject() == this )
695 {
696 int sel = event.GetOldSelection();
697 if ( sel != -1 )
698 m_pages[sel]->Show(FALSE);
699
700 sel = event.GetSelection();
701 if ( sel != -1 )
702 {
703 wxNotebookPage *pPage = m_pages[sel];
704 pPage->Show(TRUE);
705 pPage->SetFocus();
706 }
707
708 m_nSelection = sel;
709 }
710
711 // we want to give others a chance to process this message as well
712 event.Skip();
713 }
714
715 void wxNotebook::OnSetFocus(wxFocusEvent& event)
716 {
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
721
722 // set focus to the currently selected page if any
723 if ( m_nSelection != -1 )
724 m_pages[m_nSelection]->SetFocus();
725
726 event.Skip();
727 }
728
729 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
730 {
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
749 wxWindow *parent = GetParent();
750 // the cast is here to fic a GCC ICE
751 if ( ((wxWindow*)event.GetEventObject()) == parent )
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
760 wxWindow *page = m_pages[m_nSelection];
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 }
781 }
782 }
783
784 // ----------------------------------------------------------------------------
785 // wxNotebook base class virtuals
786 // ----------------------------------------------------------------------------
787
788 #if wxUSE_CONSTRAINTS
789
790 // override these 2 functions to do nothing: everything is done in OnSize
791
792 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
793 {
794 // don't set the sizes of the pages - their correct size is not yet known
795 wxControl::SetConstraintSizes(FALSE);
796 }
797
798 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
799 {
800 return TRUE;
801 }
802
803 #endif // wxUSE_CONSTRAINTS
804
805 // ----------------------------------------------------------------------------
806 // wxNotebook Windows message handlers
807 // ----------------------------------------------------------------------------
808
809 bool 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
820 bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
821 {
822 wxNotebookEvent event(wxEVT_NULL, m_windowId);
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
834 default:
835 return wxControl::MSWOnNotify(idCtrl, lParam, result);
836 }
837
838 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
839 event.SetOldSelection(m_nSelection);
840 event.SetEventObject(this);
841 event.SetInt(idCtrl);
842
843 bool processed = GetEventHandler()->ProcessEvent(event);
844 *result = !event.IsAllowed();
845 return processed;
846 }
847
848 // Windows only: attempts to get colour for UX theme page background
849 wxColour wxNotebook::GetThemeBackgroundColour()
850 {
851 #if wxUSE_UXTHEME
852 if (wxUxThemeEngine::Get())
853 {
854 wxUxThemeHandle hTheme(this, L"TAB");
855 if (hTheme)
856 {
857 // This is total guesswork.
858 // See PlatformSDK\Include\Tmschema.h for values
859 COLORREF themeColor;
860 wxUxThemeEngine::Get()->GetThemeColor
861 (
862 hTheme,
863 10 /* TABP_BODY */,
864 1 /* NORMAL */,
865 3821, /* FILLCOLORHINT */
866 & themeColor
867 );
868
869 wxColour colour(GetRValue(themeColor), GetGValue(themeColor), GetBValue(themeColor));
870 return colour;
871 }
872 }
873 #endif // wxUSE_UXTHEME
874
875 return GetBackgroundColour();
876 }
877
878 // Windows only: attempts to apply the UX theme page background to this page
879 #if wxUSE_UXTHEME
880 void wxNotebook::ApplyThemeBackground(wxWindow* window, const wxColour& colour)
881 #else
882 void wxNotebook::ApplyThemeBackground(wxWindow*, const wxColour&)
883 #endif
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
908 for ( wxWindowList::compatibility_iterator node = window->GetChildren().GetFirst(); node; node = node->GetNext() )
909 {
910 wxWindow *child = node->GetData();
911 ApplyThemeBackground(child, colour);
912 }
913 #endif
914 }
915
916 long 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
943 #endif // wxUSE_NOTEBOOK