1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/notebook.cpp
3 // Purpose: wxNotebook implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "univnotebook.h"
24 #include "wx/wxprec.h"
32 #include "wx/imaglist.h"
33 #include "wx/notebook.h"
35 #include "wx/spinbutt.h"
37 #include "wx/univ/renderer.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 static const size_t INVALID_PAGE
= (size_t)-1;
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 class wxNotebookSpinBtn
: public wxSpinButton
58 wxNotebookSpinBtn(wxNotebook
*nb
)
59 : wxSpinButton(nb
, -1,
60 wxDefaultPosition
, wxDefaultSize
,
61 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
67 void OnSpin(wxSpinEvent
& event
)
69 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
78 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
79 EVT_SPIN(-1, wxNotebookSpinBtn::OnSpin
)
82 // ============================================================================
84 // ============================================================================
86 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
87 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
89 // ----------------------------------------------------------------------------
90 // wxNotebook creation
91 // ----------------------------------------------------------------------------
93 void wxNotebook::Init()
102 m_lastFullyVisible
= 0;
109 bool wxNotebook::Create(wxWindow
*parent
,
114 const wxString
& name
)
116 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
117 wxDefaultValidator
, name
) )
120 m_sizePad
= GetRenderer()->GetTabPadding();
124 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
129 // ----------------------------------------------------------------------------
130 // wxNotebook page titles and images
131 // ----------------------------------------------------------------------------
133 wxString
wxNotebook::GetPageText(int nPage
) const
135 wxCHECK_MSG( IS_VALID_PAGE(nPage
), _T(""), _T("invalid notebook page") );
137 return m_titles
[nPage
];
140 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
142 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
144 if ( strText
!= m_titles
[nPage
] )
146 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
148 if ( FixedSizeTabs() )
150 // it's enough to just reresh this one
153 else // var width tabs
155 // we need to resize the tab to fit the new string
163 int wxNotebook::GetPageImage(int nPage
) const
165 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
167 return m_images
[nPage
];
170 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
172 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
174 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
175 _T("invalid image index in SetPageImage()") );
177 if ( nImage
!= m_images
[nPage
] )
179 // if the item didn't have an icon before or, on the contrary, did have
180 // it but has lost it now, its size will change - but if the icon just
182 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
183 m_images
[nPage
] = nImage
;
185 if ( tabSizeChanges
)
194 wxNotebook::~wxNotebook()
198 // ----------------------------------------------------------------------------
199 // wxNotebook page switching
200 // ----------------------------------------------------------------------------
202 int wxNotebook::SetSelection(int nPage
)
204 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
206 if ( (size_t)nPage
== m_sel
)
208 // don't do anything if there is nothing to do
212 if ( m_sel
!= INVALID_PAGE
)
216 m_pages
[m_sel
]->Hide();
221 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
226 m_spinbtn
->SetValue(m_sel
);
229 if ( m_sel
< m_firstVisible
)
231 // selection is to the left of visible part of tabs
234 else if ( m_sel
> m_lastFullyVisible
)
236 // selection is to the right of visible part of tabs
239 else // we already see this tab
245 m_pages
[m_sel
]->SetSize(GetPageRect());
246 m_pages
[m_sel
]->Show();
252 void wxNotebook::ChangePage(int nPage
)
254 wxCHECK_RET( IS_VALID_PAGE(nPage
), _T("invalid notebook page") );
256 if ( (size_t)nPage
== m_sel
)
262 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
263 event
.SetSelection(nPage
);
264 event
.SetOldSelection(m_sel
);
265 event
.SetEventObject(this);
266 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
268 // program doesn't allow the page change
274 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
275 GetEventHandler()->ProcessEvent(event
);
278 // ----------------------------------------------------------------------------
279 // wxNotebook pages adding/deleting
280 // ----------------------------------------------------------------------------
282 bool wxNotebook::InsertPage(int nPage
,
283 wxNotebookPage
*pPage
,
284 const wxString
& strText
,
288 int nPages
= GetPageCount();
289 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), FALSE
,
290 _T("invalid notebook page in InsertPage()") );
293 m_pages
.Insert(pPage
, nPage
);
296 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
297 m_titles
.Insert(label
, nPage
);
299 m_images
.Insert(imageId
, nPage
);
301 // cache the tab geometry here
302 wxSize sizeTab
= CalcTabSize(nPage
);
304 if ( sizeTab
.y
> m_heightTab
)
305 m_heightTab
= sizeTab
.y
;
307 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
308 m_widthMax
= sizeTab
.x
;
310 m_widths
.Insert(sizeTab
.x
, nPage
);
312 // spin button may appear if we didn't have it before - but even if we did,
313 // its range should change, so update it unconditionally
316 // if the tab has just appeared, we have to relayout everything, otherwise
317 // it's enough to just redraw the tabs
320 // always select the first tab to have at least some selection
325 else // not the first tab
334 else // pages added to the notebook are initially hidden
342 bool wxNotebook::DeleteAllPages()
344 if ( !wxNotebookBase::DeleteAllPages() )
347 // clear the other arrays as well
353 // it is not valid any longer
354 m_sel
= INVALID_PAGE
;
356 // spin button is not needed any more
364 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
366 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
368 wxNotebookPage
*page
= m_pages
[nPage
];
369 m_pages
.RemoveAt(nPage
);
370 m_titles
.RemoveAt(nPage
);
371 m_accels
.RemoveAt(nPage
);
372 m_widths
.RemoveAt(nPage
);
373 m_images
.RemoveAt(nPage
);
375 // the spin button might not be needed any more
381 int count
= GetPageCount();
384 if ( m_sel
== (size_t)nPage
)
386 // avoid sending event to this page which doesn't exist in the
388 m_sel
= INVALID_PAGE
;
390 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
392 else if ( m_sel
> (size_t)nPage
)
394 // no need to change selection, just adjust the index
398 else // no more tabs left
400 m_sel
= INVALID_PAGE
;
403 // have to refresh everything
409 // ----------------------------------------------------------------------------
410 // wxNotebook drawing
411 // ----------------------------------------------------------------------------
413 void wxNotebook::RefreshCurrent()
415 if ( m_sel
!= INVALID_PAGE
)
421 void wxNotebook::RefreshTab(int page
)
423 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
425 wxRect rect
= GetTabRect(page
);
426 if ( (size_t)page
== m_sel
)
428 const wxSize indent
= GetRenderer()->GetTabIndent();
429 rect
.Inflate(indent
.x
, indent
.y
);
435 void wxNotebook::RefreshAllTabs()
437 wxRect rect
= GetAllTabsRect();
438 if ( rect
.width
|| rect
.height
)
442 //else: we don't have tabs at all
445 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
450 int image
= m_images
[n
];
452 #ifdef __WXMSW__ // FIXME
454 m_imageList
->GetSize(n
, w
, h
);
457 dc
.SelectObject(bmp
);
458 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
459 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, TRUE
);
461 bmp
= *m_imageList
->GetBitmap(image
);
468 flags
|= wxCONTROL_SELECTED
;
471 flags
|= wxCONTROL_FOCUSED
;
474 GetRenderer()->DrawTab
486 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
488 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
490 wxDC
& dc
= renderer
->GetDC();
491 dc
.SetFont(GetFont());
492 dc
.SetTextForeground(GetForegroundColour());
494 // redraw the border - it's simpler to always do it instead of checking
495 // whether this needs to be done
496 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
498 // avoid overwriting the spin button
501 wxRect rectTabs
= GetAllTabsRect();
502 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
505 rectTabs
.height
-= sizeSpinBtn
.y
;
507 rectTabs
.width
-= sizeSpinBtn
.x
;
509 dc
.SetClippingRegion(rectTabs
);
512 wxRect rect
= GetTabsPart();
513 bool isVertical
= IsVertical();
516 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
518 GetTabSize(n
, &rect
.width
, &rect
.height
);
522 // don't redraw it now as this tab has to be drawn over the other
523 // ones as it takes more place and spills over to them
526 else // not selected tab
528 // unfortunately we can't do this because the selected tab hangs
529 // over its neighbours and so we might need to refresh more tabs -
530 // of course, we could still avoid rereshing some of them with more
531 // complicated checks, but it doesn't seem too bad to refresh all
532 // of them, I still don't see flicker, so leaving as is for now
534 //if ( rectUpdate.Intersects(rect) )
536 DoDrawTab(dc
, rect
, n
);
538 //else: doesn't need to be refreshed
541 // move the rect to the next tab
543 rect
.y
+= rect
.height
;
545 rect
.x
+= rect
.width
;
548 // now redraw the selected tab
551 DoDrawTab(dc
, rectSel
, m_sel
);
555 // ----------------------------------------------------------------------------
556 // wxNotebook geometry
557 // ----------------------------------------------------------------------------
559 int wxNotebook::HitTest(const wxPoint
& pt
) const
561 // first check that it is in this window at all
562 if ( !GetClientRect().Inside(pt
) )
567 wxRect rectTabs
= GetAllTabsRect();
569 switch ( GetTabOrientation() )
572 wxFAIL_MSG(_T("unknown tab orientation"));
576 if ( pt
.y
> rectTabs
.GetBottom() )
581 if ( pt
.y
< rectTabs
.y
)
586 if ( pt
.x
> rectTabs
.GetRight() )
591 if ( pt
.x
< rectTabs
.x
)
596 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
598 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
600 if ( rectTabs
.Inside(pt
) )
603 // move the rectTabs to the next tab
605 rectTabs
.y
+= rectTabs
.height
;
607 rectTabs
.x
+= rectTabs
.width
;
613 bool wxNotebook::IsVertical() const
615 wxDirection dir
= GetTabOrientation();
617 return dir
== wxLEFT
|| dir
== wxRIGHT
;
620 wxDirection
wxNotebook::GetTabOrientation() const
622 long style
= GetWindowStyle();
623 if ( style
& wxNB_BOTTOM
)
625 else if ( style
& wxNB_RIGHT
)
627 else if ( style
& wxNB_LEFT
)
630 // wxNB_TOP == 0 so we don't have to test for it
634 wxRect
wxNotebook::GetTabRect(int page
) const
637 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
639 // calc the size of this tab and of the preceding ones
640 wxCoord widthThis
, widthBefore
;
641 if ( FixedSizeTabs() )
643 widthThis
= m_widthMax
;
644 widthBefore
= page
*m_widthMax
;
649 for ( int n
= 0; n
< page
; n
++ )
651 widthBefore
+= m_widths
[n
];
654 widthThis
= m_widths
[page
];
657 rect
= GetTabsPart();
660 rect
.y
+= widthBefore
- m_offset
;
661 rect
.height
= widthThis
;
665 rect
.x
+= widthBefore
- m_offset
;
666 rect
.width
= widthThis
;
672 wxRect
wxNotebook::GetAllTabsRect() const
676 if ( GetPageCount() )
678 const wxSize indent
= GetRenderer()->GetTabIndent();
679 wxSize size
= GetClientSize();
683 rect
.width
= m_heightTab
+ indent
.x
;
684 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
686 rect
.height
= size
.y
;
692 rect
.height
= m_heightTab
+ indent
.y
;
693 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
701 wxRect
wxNotebook::GetTabsPart() const
703 wxRect rect
= GetAllTabsRect();
705 wxDirection dir
= GetTabOrientation();
707 const wxSize indent
= GetRenderer()->GetTabIndent();
719 rect
.height
-= indent
.y
;
723 rect
.height
-= indent
.y
;
730 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
732 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
736 // width and height have inverted meaning
742 // height is always fixed
745 // width may also be fixed and be the same for all tabs
746 *w
= GetTabWidth(page
);
749 void wxNotebook::SetTabSize(const wxSize
& sz
)
751 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
765 wxSize
wxNotebook::CalcTabSize(int page
) const
767 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
768 // method is called to calculate them
772 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
774 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
776 if ( HasImage(page
) )
779 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
781 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
783 if ( sizeImage
.y
> size
.y
)
784 size
.y
= sizeImage
.y
;
787 size
.x
+= 2*m_sizePad
.x
;
788 size
.y
+= 2*m_sizePad
.y
;
793 void wxNotebook::ResizeTab(int page
)
795 wxSize sizeTab
= CalcTabSize(page
);
797 // we only need full relayout if the page size changes
798 bool needsRelayout
= FALSE
;
803 wxCoord tmp
= sizeTab
.x
;
804 sizeTab
.x
= sizeTab
.y
;
808 if ( sizeTab
.y
> m_heightTab
)
810 needsRelayout
= TRUE
;
812 m_heightTab
= sizeTab
.y
;
815 m_widths
[page
] = sizeTab
.x
;
817 if ( sizeTab
.x
> m_widthMax
)
818 m_widthMax
= sizeTab
.x
;
820 // the total of the tabs has changed too
829 void wxNotebook::SetPadding(const wxSize
& padding
)
831 if ( padding
!= m_sizePad
)
839 void wxNotebook::Relayout()
841 if ( GetPageCount() )
847 if ( m_sel
!= INVALID_PAGE
)
849 // resize the currently shown page
850 wxRect rectPage
= GetPageRect();
852 m_pages
[m_sel
]->SetSize(rectPage
);
854 // also scroll it into view if needed (note that m_lastVisible
855 // was updated by the call to UpdateSpinBtn() above, this is why it
859 if ( m_sel
< m_firstVisible
)
861 // selection is to the left of visible part of tabs
864 else if ( m_sel
> m_lastFullyVisible
)
866 // selection is to the right of visible part of tabs
872 else // we have no pages
874 // just refresh everything
879 wxRect
wxNotebook::GetPagePart() const
881 wxRect rectPage
= GetClientRect();
883 if ( GetPageCount() )
885 wxRect rectTabs
= GetAllTabsRect();
886 wxDirection dir
= GetTabOrientation();
889 rectPage
.width
-= rectTabs
.width
;
891 rectPage
.x
+= rectTabs
.width
;
895 rectPage
.height
-= rectTabs
.height
;
897 rectPage
.y
+= rectTabs
.height
;
900 //else: no pages at all
905 wxRect
wxNotebook::GetPageRect() const
907 wxRect rect
= GetPagePart();
909 // leave space for the border
910 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
912 // FIXME: hardcoded +2!
913 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
914 -(rectBorder
.y
+ rectBorder
.height
+ 2));
919 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
921 wxSize sizeNb
= size
;
922 wxRect rect
= GetAllTabsRect();
924 sizeNb
.x
+= rect
.width
;
926 sizeNb
.y
+= rect
.height
;
931 void wxNotebook::SetPageSize(const wxSize
& size
)
933 SetClientSize(GetSizeForPage(size
));
936 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
)
938 return AdjustSize(GetSizeForPage(sizePage
));
941 // ----------------------------------------------------------------------------
942 // wxNotebook spin button
943 // ----------------------------------------------------------------------------
945 bool wxNotebook::HasSpinBtn() const
947 return m_spinbtn
&& m_spinbtn
->IsShown();
950 void wxNotebook::CalcLastVisibleTab()
952 bool isVertical
= IsVertical();
954 wxCoord width
= GetClientSize().x
;
956 wxRect rect
= GetTabsPart();
958 size_t count
= GetPageCount();
960 wxCoord widthLast
= 0;
962 for ( n
= m_firstVisible
; n
< count
; n
++ )
964 GetTabSize(n
, &rect
.width
, &rect
.height
);
965 if ( rect
.GetRight() > width
)
970 // remember it to use below
971 widthLast
= rect
.GetRight();
973 // move the rect to the next tab
975 rect
.y
+= rect
.height
;
977 rect
.x
+= rect
.width
;
980 if ( n
== m_firstVisible
)
982 // even the first tab isn't fully visible - but still pretend it is as
983 // we have to show something
984 m_lastFullyVisible
= m_firstVisible
;
986 else // more than one tab visible
988 m_lastFullyVisible
= n
- 1;
990 // but is it really fully visible? it shouldn't overlap with the spin
991 // button if it is present (again, even if the first button does
992 // overlap with it, we pretend that it doesn't as there is not much
994 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
996 // adjust width to be the width before the spin button
997 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
999 width
-= sizeSpinBtn
.y
;
1001 width
-= sizeSpinBtn
.x
;
1003 if ( widthLast
> width
)
1005 // the last button overlaps with spin button, so take he
1007 m_lastFullyVisible
--;
1014 // everything is visible
1019 // this tab is still (partially) visible, so m_lastVisible is the
1020 // next tab (remember, this is "exclusive" last)
1021 m_lastVisible
= n
+ 1;
1026 void wxNotebook::UpdateSpinBtn()
1028 // first decide if we need a spin button
1031 size_t count
= (size_t)GetPageCount();
1034 // this case is special, get rid of it immediately: everything is
1035 // visible and we don't need any spin buttons
1036 allTabsShown
= TRUE
;
1038 // have to reset them manually as we don't call CalcLastVisibleTab()
1041 m_lastFullyVisible
= 0;
1045 CalcLastVisibleTab();
1047 // if all tabs after the first visible one are shown, it doesn't yet
1048 // mean that all tabs are shown - so we go backwards until we arrive to
1049 // the beginning (then all tabs are indeed shown) or find a tab such
1050 // that not all tabs after it are shown
1051 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1053 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1055 m_offset
-= GetTabWidth(m_firstVisible
--);
1057 // reclaculate after m_firstVisible change
1058 CalcLastVisibleTab();
1061 allTabsShown
= m_lastFullyVisible
== count
- 1;
1064 if ( !allTabsShown
)
1068 // create it once only
1069 m_spinbtn
= new wxNotebookSpinBtn(this);
1071 // set the correct value to keep it in sync
1072 m_spinbtn
->SetValue(m_sel
);
1075 // position it correctly
1081 // also set/update the range
1082 m_spinbtn
->SetRange(0, count
- 1);
1084 // update m_lastFullyVisible once again as it might have changed
1085 // because the spin button appeared
1087 // FIXME: might do it more efficiently
1088 CalcLastVisibleTab();
1090 else // all tabs are visible, we don't need spin button
1099 void wxNotebook::PositionSpinBtn()
1105 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1107 wxRect rectTabs
= GetAllTabsRect();
1110 switch ( GetTabOrientation() )
1113 wxFAIL_MSG(_T("unknown tab orientation"));
1117 x
= rectTabs
.GetRight() - wBtn
;
1118 y
= rectTabs
.GetBottom() - hBtn
;
1122 x
= rectTabs
.GetRight() - wBtn
;
1123 y
= rectTabs
.GetTop();
1127 x
= rectTabs
.GetRight() - wBtn
;
1128 y
= rectTabs
.GetBottom() - hBtn
;
1132 x
= rectTabs
.GetLeft();
1133 y
= rectTabs
.GetBottom() - hBtn
;
1137 m_spinbtn
->Move(x
, y
);
1140 // ----------------------------------------------------------------------------
1141 // wxNotebook scrolling
1142 // ----------------------------------------------------------------------------
1144 void wxNotebook::ScrollTo(int page
)
1146 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1148 // set the first visible tab and offset (easy)
1149 m_firstVisible
= (size_t)page
;
1151 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1153 m_offset
+= GetTabWidth(n
);
1156 // find the last visible tab too
1157 CalcLastVisibleTab();
1162 void wxNotebook::ScrollLastTo(int page
)
1164 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1166 // go backwards until we find the first tab which can be made visible
1167 // without hiding the given one
1168 wxSize size
= GetClientSize();
1169 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1170 widthTabs
= GetTabWidth(page
);
1172 // the total width is less than the width of the window if we have the spin
1176 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1178 widthAll
-= sizeSpinBtn
.y
;
1180 widthAll
-= sizeSpinBtn
.x
;
1183 m_firstVisible
= page
;
1184 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1186 widthTabs
+= GetTabWidth(--m_firstVisible
);
1189 if ( widthTabs
> widthAll
)
1191 // take one step back (that it is forward) if we can
1192 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1197 ScrollTo(m_firstVisible
);
1199 // consitency check: the page we were asked to show should be shown
1200 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1203 // ----------------------------------------------------------------------------
1204 // wxNotebook sizing/moving
1205 // ----------------------------------------------------------------------------
1207 wxSize
wxNotebook::DoGetBestClientSize() const
1209 // calculate the max page size
1212 size_t count
= GetPageCount();
1215 for ( size_t n
= 0; n
< count
; n
++ )
1217 wxSize sizePage
= m_pages
[n
]->GetSize();
1219 if ( size
.x
< sizePage
.x
)
1220 size
.x
= sizePage
.x
;
1221 if ( size
.y
< sizePage
.y
)
1222 size
.y
= sizePage
.y
;
1227 // use some arbitrary default size
1232 return GetSizeForPage(size
);
1235 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1237 wxControl::DoMoveWindow(x
, y
, width
, height
);
1239 // move the spin ctrl too (NOP if it doesn't exist)
1243 void wxNotebook::DoSetSize(int x
, int y
,
1244 int width
, int height
,
1247 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1252 // ----------------------------------------------------------------------------
1253 // wxNotebook input processing
1254 // ----------------------------------------------------------------------------
1256 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1258 const wxString
& strArg
)
1260 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1261 ChangePage(GetNextPage(TRUE
));
1262 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1263 ChangePage(GetNextPage(FALSE
));
1264 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1265 ChangePage((int)numArg
);
1267 return wxControl::PerformAction(action
, numArg
, strArg
);
1272 // ----------------------------------------------------------------------------
1273 // wxStdNotebookInputHandler
1274 // ----------------------------------------------------------------------------
1276 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1277 : wxStdInputHandler(inphand
)
1281 bool wxStdNotebookInputHandler::HandleKey(wxControl
*control
,
1282 const wxKeyEvent
& event
,
1285 // ignore the key releases
1288 wxNotebook
*notebook
= wxStaticCast(control
, wxNotebook
);
1291 wxControlAction action
;
1292 switch ( event
.GetKeyCode() )
1295 if ( notebook
->IsVertical() )
1296 action
= wxACTION_NOTEBOOK_PREV
;
1300 if ( !notebook
->IsVertical() )
1301 action
= wxACTION_NOTEBOOK_PREV
;
1305 if ( notebook
->IsVertical() )
1306 action
= wxACTION_NOTEBOOK_NEXT
;
1310 if ( !notebook
->IsVertical() )
1311 action
= wxACTION_NOTEBOOK_NEXT
;
1315 action
= wxACTION_NOTEBOOK_GOTO
;
1316 // page = 0; -- already has this value
1320 action
= wxACTION_NOTEBOOK_GOTO
;
1321 page
= notebook
->GetPageCount() - 1;
1327 return control
->PerformAction(action
, page
);
1331 return wxStdInputHandler::HandleKey(control
, event
, pressed
);
1334 bool wxStdNotebookInputHandler::HandleMouse(wxControl
*control
,
1335 const wxMouseEvent
& event
)
1337 if ( event
.ButtonDown(1) )
1339 wxNotebook
*notebook
= wxStaticCast(control
, wxNotebook
);
1340 int page
= notebook
->HitTest(event
.GetPosition());
1343 control
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1349 return wxStdInputHandler::HandleMouse(control
, event
);
1352 bool wxStdNotebookInputHandler::HandleMouseMove(wxControl
*control
,
1353 const wxMouseEvent
& event
)
1355 return wxStdInputHandler::HandleMouseMove(control
, event
);
1358 bool wxStdNotebookInputHandler::HandleFocus(wxControl
*control
,
1359 const wxFocusEvent
& event
)
1361 HandleFocusChange(control
);
1366 bool wxStdNotebookInputHandler::HandleActivation(wxControl
*control
,
1367 bool WXUNUSED(activated
))
1369 // we react to the focus change in the same way as to the [de]activation
1370 HandleFocusChange(control
);
1375 void wxStdNotebookInputHandler::HandleFocusChange(wxControl
*control
)
1377 wxNotebook
*notebook
= wxStaticCast(control
, wxNotebook
);
1378 notebook
->RefreshCurrent();
1381 #endif // wxUSE_NOTEBOOK