1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/notebook.cpp
3 // Purpose: wxNotebook implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "univnotebook.h"
25 #pragma message disable unscomzer
28 #include "wx/wxprec.h"
36 #include "wx/imaglist.h"
37 #include "wx/notebook.h"
38 #include "wx/spinbutt.h"
39 #include "wx/dcmemory.h"
41 #include "wx/univ/renderer.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 static const size_t INVALID_PAGE
= (size_t)-1;
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class wxNotebookSpinBtn
: public wxSpinButton
65 wxNotebookSpinBtn(wxNotebook
*nb
)
66 : wxSpinButton(nb
, -1,
67 wxDefaultPosition
, wxDefaultSize
,
68 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
74 void OnSpin(wxSpinEvent
& event
)
76 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
85 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
86 EVT_SPIN(-1, wxNotebookSpinBtn::OnSpin
)
89 // ============================================================================
91 // ============================================================================
93 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
94 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
96 // ----------------------------------------------------------------------------
97 // wxNotebook creation
98 // ----------------------------------------------------------------------------
100 void wxNotebook::Init()
102 m_sel
= INVALID_PAGE
;
109 m_lastFullyVisible
= 0;
116 bool wxNotebook::Create(wxWindow
*parent
,
121 const wxString
& name
)
123 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
124 wxDefaultValidator
, name
) )
127 m_sizePad
= GetRenderer()->GetTabPadding();
131 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
136 // ----------------------------------------------------------------------------
137 // wxNotebook page titles and images
138 // ----------------------------------------------------------------------------
140 wxString
wxNotebook::GetPageText(size_t nPage
) const
142 wxCHECK_MSG( IS_VALID_PAGE(nPage
), _T(""), _T("invalid notebook page") );
144 return m_titles
[nPage
];
147 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
149 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
151 if ( strText
!= m_titles
[nPage
] )
153 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
155 if ( FixedSizeTabs() )
157 // it's enough to just reresh this one
160 else // var width tabs
162 // we need to resize the tab to fit the new string
170 int wxNotebook::GetPageImage(size_t nPage
) const
172 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
174 return m_images
[nPage
];
177 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
179 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
181 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
182 _T("invalid image index in SetPageImage()") );
184 if ( nImage
!= m_images
[nPage
] )
186 // if the item didn't have an icon before or, on the contrary, did have
187 // it but has lost it now, its size will change - but if the icon just
189 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
190 m_images
[nPage
] = nImage
;
192 if ( tabSizeChanges
)
201 wxNotebook::~wxNotebook()
205 // ----------------------------------------------------------------------------
206 // wxNotebook page switching
207 // ----------------------------------------------------------------------------
209 int wxNotebook::SetSelection(size_t nPage
)
211 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
213 if ( (size_t)nPage
== m_sel
)
215 // don't do anything if there is nothing to do
220 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
221 event
.SetSelection(nPage
);
222 event
.SetOldSelection(m_sel
);
223 event
.SetEventObject(this);
224 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
226 // program doesn't allow the page change
230 // we need to change m_sel first, before calling RefreshTab() below as
231 // otherwise the previously selected tab wouldn't be redrawn properly under
232 // wxGTK which calls Refresh() immediately and not during the next event
233 // loop iteration as wxMSW does and as it should
234 size_t selOld
= m_sel
;
238 if ( selOld
!= INVALID_PAGE
)
240 RefreshTab(selOld
, TRUE
/* this tab was selected */);
242 m_pages
[selOld
]->Hide();
245 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
250 m_spinbtn
->SetValue(m_sel
);
253 if ( m_sel
< m_firstVisible
)
255 // selection is to the left of visible part of tabs
258 else if ( m_sel
> m_lastFullyVisible
)
260 // selection is to the right of visible part of tabs
263 else // we already see this tab
269 m_pages
[m_sel
]->SetSize(GetPageRect());
270 m_pages
[m_sel
]->Show();
274 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
275 GetEventHandler()->ProcessEvent(event
);
280 // ----------------------------------------------------------------------------
281 // wxNotebook pages adding/deleting
282 // ----------------------------------------------------------------------------
284 bool wxNotebook::InsertPage(size_t nPage
,
285 wxNotebookPage
*pPage
,
286 const wxString
& strText
,
290 size_t nPages
= GetPageCount();
291 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), FALSE
,
292 _T("invalid notebook page in InsertPage()") );
295 m_pages
.Insert(pPage
, nPage
);
298 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
299 m_titles
.Insert(label
, nPage
);
301 m_images
.Insert(imageId
, nPage
);
303 // cache the tab geometry here
304 wxSize sizeTab
= CalcTabSize(nPage
);
306 if ( sizeTab
.y
> m_heightTab
)
307 m_heightTab
= sizeTab
.y
;
309 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
310 m_widthMax
= sizeTab
.x
;
312 m_widths
.Insert(sizeTab
.x
, nPage
);
314 // spin button may appear if we didn't have it before - but even if we did,
315 // its range should change, so update it unconditionally
318 // if the tab has just appeared, we have to relayout everything, otherwise
319 // it's enough to just redraw the tabs
322 // always select the first tab to have at least some selection
328 else // not the first tab
337 else // pages added to the notebook are initially hidden
345 bool wxNotebook::DeleteAllPages()
347 if ( !wxNotebookBase::DeleteAllPages() )
350 // clear the other arrays as well
356 // it is not valid any longer
357 m_sel
= INVALID_PAGE
;
359 // spin button is not needed any more
367 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
369 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
371 wxNotebookPage
*page
= m_pages
[nPage
];
372 m_pages
.RemoveAt(nPage
);
373 m_titles
.RemoveAt(nPage
);
374 m_accels
.RemoveAt(nPage
);
375 m_widths
.RemoveAt(nPage
);
376 m_images
.RemoveAt(nPage
);
378 // the spin button might not be needed any more
379 // 2002-08-12 'if' commented out by JACS on behalf
380 // of Hans Van Leemputten <Hansvl@softhome.net> who
381 // points out that UpdateSpinBtn should always be called,
382 // to ensure m_lastVisible is up to date.
383 // if ( HasSpinBtn() )
388 size_t count
= GetPageCount();
391 if ( m_sel
== (size_t)nPage
)
393 // avoid sending event to this page which doesn't exist in the
395 m_sel
= INVALID_PAGE
;
397 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
399 else if ( m_sel
> (size_t)nPage
)
401 // no need to change selection, just adjust the index
405 else // no more tabs left
407 m_sel
= INVALID_PAGE
;
410 // have to refresh everything
416 // ----------------------------------------------------------------------------
417 // wxNotebook drawing
418 // ----------------------------------------------------------------------------
420 void wxNotebook::RefreshCurrent()
422 if ( m_sel
!= INVALID_PAGE
)
428 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
430 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
432 wxRect rect
= GetTabRect(page
);
433 if ( forceSelected
|| ((size_t)page
== m_sel
) )
435 const wxSize indent
= GetRenderer()->GetTabIndent();
436 rect
.Inflate(indent
.x
, indent
.y
);
442 void wxNotebook::RefreshAllTabs()
444 wxRect rect
= GetAllTabsRect();
445 if ( rect
.width
|| rect
.height
)
449 //else: we don't have tabs at all
452 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
457 int image
= m_images
[n
];
459 // Not needed now that wxGenericImageList is being
460 // used for wxUniversal under MSW
461 #if 0 // def __WXMSW__ // FIXME
463 m_imageList
->GetSize(n
, w
, h
);
466 dc
.SelectObject(bmp
);
467 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
468 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, TRUE
);
469 dc
.SelectObject(wxNullBitmap
);
471 bmp
= *m_imageList
->GetBitmap(image
);
478 flags
|= wxCONTROL_SELECTED
;
481 flags
|= wxCONTROL_FOCUSED
;
484 GetRenderer()->DrawTab
496 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
498 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
500 wxDC
& dc
= renderer
->GetDC();
501 dc
.SetFont(GetFont());
502 dc
.SetTextForeground(GetForegroundColour());
504 // redraw the border - it's simpler to always do it instead of checking
505 // whether this needs to be done
506 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
508 // avoid overwriting the spin button
511 wxRect rectTabs
= GetAllTabsRect();
512 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
516 rectTabs
.height
-= sizeSpinBtn
.y
;
518 // Allow for erasing the line under selected tab
523 rectTabs
.width
-= sizeSpinBtn
.x
;
525 // Allow for erasing the line under selected tab
526 rectTabs
.height
+= 2;
529 dc
.SetClippingRegion(rectTabs
);
532 wxRect rect
= GetTabsPart();
533 bool isVertical
= IsVertical();
536 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
538 GetTabSize(n
, &rect
.width
, &rect
.height
);
542 // don't redraw it now as this tab has to be drawn over the other
543 // ones as it takes more place and spills over to them
546 else // not selected tab
548 // unfortunately we can't do this because the selected tab hangs
549 // over its neighbours and so we might need to refresh more tabs -
550 // of course, we could still avoid rereshing some of them with more
551 // complicated checks, but it doesn't seem too bad to refresh all
552 // of them, I still don't see flicker, so leaving as is for now
554 //if ( rectUpdate.Intersects(rect) )
556 DoDrawTab(dc
, rect
, n
);
558 //else: doesn't need to be refreshed
561 // move the rect to the next tab
563 rect
.y
+= rect
.height
;
565 rect
.x
+= rect
.width
;
568 // now redraw the selected tab
571 DoDrawTab(dc
, rectSel
, m_sel
);
574 dc
.DestroyClippingRegion();
577 // ----------------------------------------------------------------------------
578 // wxNotebook geometry
579 // ----------------------------------------------------------------------------
581 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
584 *flags
= wxNB_HITTEST_NOWHERE
;
586 // first check that it is in this window at all
587 if ( !GetClientRect().Inside(pt
) )
592 wxRect rectTabs
= GetAllTabsRect();
594 switch ( GetTabOrientation() )
597 wxFAIL_MSG(_T("unknown tab orientation"));
601 if ( pt
.y
> rectTabs
.GetBottom() )
606 if ( pt
.y
< rectTabs
.y
)
611 if ( pt
.x
> rectTabs
.GetRight() )
616 if ( pt
.x
< rectTabs
.x
)
621 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
623 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
625 if ( rectTabs
.Inside(pt
) )
629 // TODO: be more precise
630 *flags
= wxNB_HITTEST_ONITEM
;
636 // move the rectTabs to the next tab
638 rectTabs
.y
+= rectTabs
.height
;
640 rectTabs
.x
+= rectTabs
.width
;
646 bool wxNotebook::IsVertical() const
648 wxDirection dir
= GetTabOrientation();
650 return dir
== wxLEFT
|| dir
== wxRIGHT
;
653 wxDirection
wxNotebook::GetTabOrientation() const
655 long style
= GetWindowStyle();
656 if ( style
& wxNB_BOTTOM
)
658 else if ( style
& wxNB_RIGHT
)
660 else if ( style
& wxNB_LEFT
)
663 // wxNB_TOP == 0 so we don't have to test for it
667 wxRect
wxNotebook::GetTabRect(int page
) const
670 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
672 // calc the size of this tab and of the preceding ones
673 wxCoord widthThis
, widthBefore
;
674 if ( FixedSizeTabs() )
676 widthThis
= m_widthMax
;
677 widthBefore
= page
*m_widthMax
;
682 for ( int n
= 0; n
< page
; n
++ )
684 widthBefore
+= m_widths
[n
];
687 widthThis
= m_widths
[page
];
690 rect
= GetTabsPart();
693 rect
.y
+= widthBefore
- m_offset
;
694 rect
.height
= widthThis
;
698 rect
.x
+= widthBefore
- m_offset
;
699 rect
.width
= widthThis
;
705 wxRect
wxNotebook::GetAllTabsRect() const
709 if ( GetPageCount() )
711 const wxSize indent
= GetRenderer()->GetTabIndent();
712 wxSize size
= GetClientSize();
716 rect
.width
= m_heightTab
+ indent
.x
;
717 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
719 rect
.height
= size
.y
;
725 rect
.height
= m_heightTab
+ indent
.y
;
726 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
734 wxRect
wxNotebook::GetTabsPart() const
736 wxRect rect
= GetAllTabsRect();
738 wxDirection dir
= GetTabOrientation();
740 const wxSize indent
= GetRenderer()->GetTabIndent();
752 rect
.height
-= indent
.y
;
756 rect
.height
-= indent
.y
;
763 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
765 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
769 // width and height have inverted meaning
775 // height is always fixed
778 // width may also be fixed and be the same for all tabs
779 *w
= GetTabWidth(page
);
782 void wxNotebook::SetTabSize(const wxSize
& sz
)
784 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
798 wxSize
wxNotebook::CalcTabSize(int page
) const
800 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
801 // method is called to calculate them
805 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
807 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
809 if ( HasImage(page
) )
812 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
814 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
816 if ( sizeImage
.y
> size
.y
)
817 size
.y
= sizeImage
.y
;
820 size
.x
+= 2*m_sizePad
.x
;
821 size
.y
+= 2*m_sizePad
.y
;
826 void wxNotebook::ResizeTab(int page
)
828 wxSize sizeTab
= CalcTabSize(page
);
830 // we only need full relayout if the page size changes
831 bool needsRelayout
= FALSE
;
836 wxCoord tmp
= sizeTab
.x
;
837 sizeTab
.x
= sizeTab
.y
;
841 if ( sizeTab
.y
> m_heightTab
)
843 needsRelayout
= TRUE
;
845 m_heightTab
= sizeTab
.y
;
848 m_widths
[page
] = sizeTab
.x
;
850 if ( sizeTab
.x
> m_widthMax
)
851 m_widthMax
= sizeTab
.x
;
853 // the total of the tabs has changed too
862 void wxNotebook::SetPadding(const wxSize
& padding
)
864 if ( padding
!= m_sizePad
)
872 void wxNotebook::Relayout()
874 if ( GetPageCount() )
880 if ( m_sel
!= INVALID_PAGE
)
882 // resize the currently shown page
883 wxRect rectPage
= GetPageRect();
885 m_pages
[m_sel
]->SetSize(rectPage
);
887 // also scroll it into view if needed (note that m_lastVisible
888 // was updated by the call to UpdateSpinBtn() above, this is why it
892 if ( m_sel
< m_firstVisible
)
894 // selection is to the left of visible part of tabs
897 else if ( m_sel
> m_lastFullyVisible
)
899 // selection is to the right of visible part of tabs
905 else // we have no pages
907 // just refresh everything
912 wxRect
wxNotebook::GetPagePart() const
914 wxRect rectPage
= GetClientRect();
916 if ( GetPageCount() )
918 wxRect rectTabs
= GetAllTabsRect();
919 wxDirection dir
= GetTabOrientation();
922 rectPage
.width
-= rectTabs
.width
;
924 rectPage
.x
+= rectTabs
.width
;
928 rectPage
.height
-= rectTabs
.height
;
930 rectPage
.y
+= rectTabs
.height
;
933 //else: no pages at all
938 wxRect
wxNotebook::GetPageRect() const
940 wxRect rect
= GetPagePart();
942 // leave space for the border
943 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
945 // FIXME: hardcoded +2!
946 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
947 -(rectBorder
.y
+ rectBorder
.height
+ 2));
952 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
954 wxSize sizeNb
= size
;
955 wxRect rect
= GetAllTabsRect();
957 sizeNb
.x
+= rect
.width
;
959 sizeNb
.y
+= rect
.height
;
964 void wxNotebook::SetPageSize(const wxSize
& size
)
966 SetClientSize(GetSizeForPage(size
));
969 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
971 return AdjustSize(GetSizeForPage(sizePage
));
974 // ----------------------------------------------------------------------------
975 // wxNotebook spin button
976 // ----------------------------------------------------------------------------
978 bool wxNotebook::HasSpinBtn() const
980 return m_spinbtn
&& m_spinbtn
->IsShown();
983 void wxNotebook::CalcLastVisibleTab()
985 bool isVertical
= IsVertical();
987 wxCoord width
= GetClientSize().x
;
989 wxRect rect
= GetTabsPart();
991 size_t count
= GetPageCount();
993 wxCoord widthLast
= 0;
995 for ( n
= m_firstVisible
; n
< count
; n
++ )
997 GetTabSize(n
, &rect
.width
, &rect
.height
);
998 if ( rect
.GetRight() > width
)
1003 // remember it to use below
1004 widthLast
= rect
.GetRight();
1006 // move the rect to the next tab
1008 rect
.y
+= rect
.height
;
1010 rect
.x
+= rect
.width
;
1013 if ( n
== m_firstVisible
)
1015 // even the first tab isn't fully visible - but still pretend it is as
1016 // we have to show something
1017 m_lastFullyVisible
= m_firstVisible
;
1019 else // more than one tab visible
1021 m_lastFullyVisible
= n
- 1;
1023 // but is it really fully visible? it shouldn't overlap with the spin
1024 // button if it is present (again, even if the first button does
1025 // overlap with it, we pretend that it doesn't as there is not much
1027 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1029 // adjust width to be the width before the spin button
1030 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1032 width
-= sizeSpinBtn
.y
;
1034 width
-= sizeSpinBtn
.x
;
1036 if ( widthLast
> width
)
1038 // the last button overlaps with spin button, so take he
1040 m_lastFullyVisible
--;
1047 // everything is visible
1052 // this tab is still (partially) visible, so m_lastVisible is the
1053 // next tab (remember, this is "exclusive" last)
1054 m_lastVisible
= n
+ 1;
1059 void wxNotebook::UpdateSpinBtn()
1061 // first decide if we need a spin button
1064 size_t count
= (size_t)GetPageCount();
1067 // this case is special, get rid of it immediately: everything is
1068 // visible and we don't need any spin buttons
1069 allTabsShown
= TRUE
;
1071 // have to reset them manually as we don't call CalcLastVisibleTab()
1074 m_lastFullyVisible
= 0;
1078 CalcLastVisibleTab();
1080 // if all tabs after the first visible one are shown, it doesn't yet
1081 // mean that all tabs are shown - so we go backwards until we arrive to
1082 // the beginning (then all tabs are indeed shown) or find a tab such
1083 // that not all tabs after it are shown
1084 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1086 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1088 m_offset
-= GetTabWidth(m_firstVisible
--);
1090 // reclaculate after m_firstVisible change
1091 CalcLastVisibleTab();
1094 allTabsShown
= m_lastFullyVisible
== count
- 1;
1097 if ( !allTabsShown
)
1101 // create it once only
1102 m_spinbtn
= new wxNotebookSpinBtn(this);
1104 // set the correct value to keep it in sync
1105 m_spinbtn
->SetValue(m_sel
);
1108 // position it correctly
1114 // also set/update the range
1115 m_spinbtn
->SetRange(0, count
- 1);
1117 // update m_lastFullyVisible once again as it might have changed
1118 // because the spin button appeared
1120 // FIXME: might do it more efficiently
1121 CalcLastVisibleTab();
1123 else // all tabs are visible, we don't need spin button
1132 void wxNotebook::PositionSpinBtn()
1138 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1140 wxRect rectTabs
= GetAllTabsRect();
1143 switch ( GetTabOrientation() )
1146 wxFAIL_MSG(_T("unknown tab orientation"));
1150 x
= rectTabs
.GetRight() - wBtn
;
1151 y
= rectTabs
.GetBottom() - hBtn
;
1155 x
= rectTabs
.GetRight() - wBtn
;
1156 y
= rectTabs
.GetTop();
1160 x
= rectTabs
.GetRight() - wBtn
;
1161 y
= rectTabs
.GetBottom() - hBtn
;
1165 x
= rectTabs
.GetLeft();
1166 y
= rectTabs
.GetBottom() - hBtn
;
1170 m_spinbtn
->Move(x
, y
);
1173 // ----------------------------------------------------------------------------
1174 // wxNotebook scrolling
1175 // ----------------------------------------------------------------------------
1177 void wxNotebook::ScrollTo(int page
)
1179 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1181 // set the first visible tab and offset (easy)
1182 m_firstVisible
= (size_t)page
;
1184 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1186 m_offset
+= GetTabWidth(n
);
1189 // find the last visible tab too
1190 CalcLastVisibleTab();
1195 void wxNotebook::ScrollLastTo(int page
)
1197 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1199 // go backwards until we find the first tab which can be made visible
1200 // without hiding the given one
1201 wxSize size
= GetClientSize();
1202 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1203 widthTabs
= GetTabWidth(page
);
1205 // the total width is less than the width of the window if we have the spin
1209 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1211 widthAll
-= sizeSpinBtn
.y
;
1213 widthAll
-= sizeSpinBtn
.x
;
1216 m_firstVisible
= page
;
1217 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1219 widthTabs
+= GetTabWidth(--m_firstVisible
);
1222 if ( widthTabs
> widthAll
)
1224 // take one step back (that it is forward) if we can
1225 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1230 ScrollTo(m_firstVisible
);
1232 // consitency check: the page we were asked to show should be shown
1233 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1236 // ----------------------------------------------------------------------------
1237 // wxNotebook sizing/moving
1238 // ----------------------------------------------------------------------------
1240 wxSize
wxNotebook::DoGetBestClientSize() const
1242 // calculate the max page size
1245 size_t count
= GetPageCount();
1248 for ( size_t n
= 0; n
< count
; n
++ )
1250 wxSize sizePage
= m_pages
[n
]->GetSize();
1252 if ( size
.x
< sizePage
.x
)
1253 size
.x
= sizePage
.x
;
1254 if ( size
.y
< sizePage
.y
)
1255 size
.y
= sizePage
.y
;
1260 // use some arbitrary default size
1265 return GetSizeForPage(size
);
1268 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1270 wxControl::DoMoveWindow(x
, y
, width
, height
);
1272 // move the spin ctrl too (NOP if it doesn't exist)
1276 void wxNotebook::DoSetSize(int x
, int y
,
1277 int width
, int height
,
1280 wxSize old_client_size
= GetClientSize();
1282 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1284 wxSize new_client_size
= GetClientSize();
1286 if (old_client_size
!= new_client_size
)
1290 // ----------------------------------------------------------------------------
1291 // wxNotebook input processing
1292 // ----------------------------------------------------------------------------
1294 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1296 const wxString
& strArg
)
1298 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1299 SetSelection(GetNextPage(TRUE
));
1300 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1301 SetSelection(GetNextPage(FALSE
));
1302 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1303 SetSelection((int)numArg
);
1305 return wxControl::PerformAction(action
, numArg
, strArg
);
1310 // ----------------------------------------------------------------------------
1311 // wxStdNotebookInputHandler
1312 // ----------------------------------------------------------------------------
1314 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1315 : wxStdInputHandler(inphand
)
1319 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1320 const wxKeyEvent
& event
,
1323 // ignore the key releases
1326 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1329 wxControlAction action
;
1330 switch ( event
.GetKeyCode() )
1333 if ( notebook
->IsVertical() )
1334 action
= wxACTION_NOTEBOOK_PREV
;
1338 if ( !notebook
->IsVertical() )
1339 action
= wxACTION_NOTEBOOK_PREV
;
1343 if ( notebook
->IsVertical() )
1344 action
= wxACTION_NOTEBOOK_NEXT
;
1348 if ( !notebook
->IsVertical() )
1349 action
= wxACTION_NOTEBOOK_NEXT
;
1353 action
= wxACTION_NOTEBOOK_GOTO
;
1354 // page = 0; -- already has this value
1358 action
= wxACTION_NOTEBOOK_GOTO
;
1359 page
= notebook
->GetPageCount() - 1;
1365 return consumer
->PerformAction(action
, page
);
1369 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1372 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1373 const wxMouseEvent
& event
)
1375 if ( event
.ButtonDown(1) )
1377 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1378 int page
= notebook
->HitTest(event
.GetPosition());
1381 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1387 return wxStdInputHandler::HandleMouse(consumer
, event
);
1390 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1391 const wxMouseEvent
& event
)
1393 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1397 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1398 const wxFocusEvent
& WXUNUSED(event
))
1400 HandleFocusChange(consumer
);
1405 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1406 bool WXUNUSED(activated
))
1408 // we react to the focus change in the same way as to the [de]activation
1409 HandleFocusChange(consumer
);
1414 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1416 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1417 notebook
->RefreshCurrent();
1420 #endif // wxUSE_NOTEBOOK