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 // ----------------------------------------------------------------------------
48 // due to unsigned type nPage is always >= 0
49 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
51 #define IS_VALID_PAGE(nPage) (((size_t)nPage) < GetPageCount())
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 static const size_t INVALID_PAGE
= (size_t)-1;
60 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
61 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 class wxNotebookSpinBtn
: public wxSpinButton
70 wxNotebookSpinBtn(wxNotebook
*nb
)
71 : wxSpinButton(nb
, wxID_ANY
,
72 wxDefaultPosition
, wxDefaultSize
,
73 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
79 void OnSpin(wxSpinEvent
& event
)
81 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
90 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
91 EVT_SPIN(wxID_ANY
, wxNotebookSpinBtn::OnSpin
)
94 // ============================================================================
96 // ============================================================================
98 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
99 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
101 // ----------------------------------------------------------------------------
102 // wxNotebook creation
103 // ----------------------------------------------------------------------------
105 void wxNotebook::Init()
107 m_sel
= INVALID_PAGE
;
114 m_lastFullyVisible
= 0;
121 bool wxNotebook::Create(wxWindow
*parent
,
126 const wxString
& name
)
128 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
129 wxDefaultValidator
, name
) )
132 m_sizePad
= GetRenderer()->GetTabPadding();
136 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
141 // ----------------------------------------------------------------------------
142 // wxNotebook page titles and images
143 // ----------------------------------------------------------------------------
145 wxString
wxNotebook::GetPageText(size_t nPage
) const
147 wxCHECK_MSG( IS_VALID_PAGE(nPage
), _T(""), _T("invalid notebook page") );
149 return m_titles
[nPage
];
152 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
154 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
156 if ( strText
!= m_titles
[nPage
] )
158 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
160 if ( FixedSizeTabs() )
162 // it's enough to just reresh this one
165 else // var width tabs
167 // we need to resize the tab to fit the new string
175 int wxNotebook::GetPageImage(size_t nPage
) const
177 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
179 return m_images
[nPage
];
182 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
184 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
186 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
187 _T("invalid image index in SetPageImage()") );
189 if ( nImage
!= m_images
[nPage
] )
191 // if the item didn't have an icon before or, on the contrary, did have
192 // it but has lost it now, its size will change - but if the icon just
194 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
195 m_images
[nPage
] = nImage
;
197 if ( tabSizeChanges
)
206 wxNotebook::~wxNotebook()
210 // ----------------------------------------------------------------------------
211 // wxNotebook page switching
212 // ----------------------------------------------------------------------------
214 int wxNotebook::SetSelection(size_t nPage
)
216 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
218 if ( (size_t)nPage
== m_sel
)
220 // don't do anything if there is nothing to do
225 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
226 event
.SetSelection(nPage
);
227 event
.SetOldSelection(m_sel
);
228 event
.SetEventObject(this);
229 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
231 // program doesn't allow the page change
235 // we need to change m_sel first, before calling RefreshTab() below as
236 // otherwise the previously selected tab wouldn't be redrawn properly under
237 // wxGTK which calls Refresh() immediately and not during the next event
238 // loop iteration as wxMSW does and as it should
239 size_t selOld
= m_sel
;
243 if ( selOld
!= INVALID_PAGE
)
245 RefreshTab(selOld
, true /* this tab was selected */);
247 m_pages
[selOld
]->Hide();
250 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
255 m_spinbtn
->SetValue(m_sel
);
258 if ( m_sel
< m_firstVisible
)
260 // selection is to the left of visible part of tabs
263 else if ( m_sel
> m_lastFullyVisible
)
265 // selection is to the right of visible part of tabs
268 else // we already see this tab
274 m_pages
[m_sel
]->SetSize(GetPageRect());
275 m_pages
[m_sel
]->Show();
279 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
280 GetEventHandler()->ProcessEvent(event
);
285 // ----------------------------------------------------------------------------
286 // wxNotebook pages adding/deleting
287 // ----------------------------------------------------------------------------
289 bool wxNotebook::InsertPage(size_t nPage
,
290 wxNotebookPage
*pPage
,
291 const wxString
& strText
,
295 size_t nPages
= GetPageCount();
296 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), false,
297 _T("invalid notebook page in InsertPage()") );
300 m_pages
.Insert(pPage
, nPage
);
303 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
304 m_titles
.Insert(label
, nPage
);
306 m_images
.Insert(imageId
, nPage
);
308 // cache the tab geometry here
309 wxSize sizeTab
= CalcTabSize(nPage
);
311 if ( sizeTab
.y
> m_heightTab
)
312 m_heightTab
= sizeTab
.y
;
314 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
315 m_widthMax
= sizeTab
.x
;
317 m_widths
.Insert(sizeTab
.x
, nPage
);
319 // spin button may appear if we didn't have it before - but even if we did,
320 // its range should change, so update it unconditionally
323 // if the tab has just appeared, we have to relayout everything, otherwise
324 // it's enough to just redraw the tabs
327 // always select the first tab to have at least some selection
333 else // not the first tab
342 else // pages added to the notebook are initially hidden
350 bool wxNotebook::DeleteAllPages()
352 if ( !wxNotebookBase::DeleteAllPages() )
355 // clear the other arrays as well
361 // it is not valid any longer
362 m_sel
= INVALID_PAGE
;
364 // spin button is not needed any more
372 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
374 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
376 wxNotebookPage
*page
= m_pages
[nPage
];
377 m_pages
.RemoveAt(nPage
);
378 m_titles
.RemoveAt(nPage
);
379 m_accels
.RemoveAt(nPage
);
380 m_widths
.RemoveAt(nPage
);
381 m_images
.RemoveAt(nPage
);
383 // the spin button might not be needed any more
384 // 2002-08-12 'if' commented out by JACS on behalf
385 // of Hans Van Leemputten <Hansvl@softhome.net> who
386 // points out that UpdateSpinBtn should always be called,
387 // to ensure m_lastVisible is up to date.
388 // if ( HasSpinBtn() )
393 size_t count
= GetPageCount();
396 if ( m_sel
== (size_t)nPage
)
398 // avoid sending event to this page which doesn't exist in the
400 m_sel
= INVALID_PAGE
;
402 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
404 else if ( m_sel
> (size_t)nPage
)
406 // no need to change selection, just adjust the index
410 else // no more tabs left
412 m_sel
= INVALID_PAGE
;
415 // have to refresh everything
421 // ----------------------------------------------------------------------------
422 // wxNotebook drawing
423 // ----------------------------------------------------------------------------
425 void wxNotebook::RefreshCurrent()
427 if ( m_sel
!= INVALID_PAGE
)
433 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
435 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
437 wxRect rect
= GetTabRect(page
);
438 if ( forceSelected
|| ((size_t)page
== m_sel
) )
440 const wxSize indent
= GetRenderer()->GetTabIndent();
441 rect
.Inflate(indent
.x
, indent
.y
);
447 void wxNotebook::RefreshAllTabs()
449 wxRect rect
= GetAllTabsRect();
450 if ( rect
.width
|| rect
.height
)
454 //else: we don't have tabs at all
457 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
462 int image
= m_images
[n
];
464 // Not needed now that wxGenericImageList is being
465 // used for wxUniversal under MSW
466 #if 0 // def __WXMSW__ // FIXME
468 m_imageList
->GetSize(n
, w
, h
);
471 dc
.SelectObject(bmp
);
472 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
473 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, true);
474 dc
.SelectObject(wxNullBitmap
);
476 bmp
= *m_imageList
->GetBitmap(image
);
483 flags
|= wxCONTROL_SELECTED
;
486 flags
|= wxCONTROL_FOCUSED
;
489 GetRenderer()->DrawTab
501 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
503 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
505 wxDC
& dc
= renderer
->GetDC();
506 dc
.SetFont(GetFont());
507 dc
.SetTextForeground(GetForegroundColour());
509 // redraw the border - it's simpler to always do it instead of checking
510 // whether this needs to be done
511 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
513 // avoid overwriting the spin button
516 wxRect rectTabs
= GetAllTabsRect();
517 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
521 rectTabs
.height
-= sizeSpinBtn
.y
;
523 // Allow for erasing the line under selected tab
528 rectTabs
.width
-= sizeSpinBtn
.x
;
530 // Allow for erasing the line under selected tab
531 rectTabs
.height
+= 2;
534 dc
.SetClippingRegion(rectTabs
);
537 wxRect rect
= GetTabsPart();
538 bool isVertical
= IsVertical();
541 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
543 GetTabSize(n
, &rect
.width
, &rect
.height
);
547 // don't redraw it now as this tab has to be drawn over the other
548 // ones as it takes more place and spills over to them
551 else // not selected tab
553 // unfortunately we can't do this because the selected tab hangs
554 // over its neighbours and so we might need to refresh more tabs -
555 // of course, we could still avoid rereshing some of them with more
556 // complicated checks, but it doesn't seem too bad to refresh all
557 // of them, I still don't see flicker, so leaving as is for now
559 //if ( rectUpdate.Intersects(rect) )
561 DoDrawTab(dc
, rect
, n
);
563 //else: doesn't need to be refreshed
566 // move the rect to the next tab
568 rect
.y
+= rect
.height
;
570 rect
.x
+= rect
.width
;
573 // now redraw the selected tab
576 DoDrawTab(dc
, rectSel
, m_sel
);
579 dc
.DestroyClippingRegion();
582 // ----------------------------------------------------------------------------
583 // wxNotebook geometry
584 // ----------------------------------------------------------------------------
586 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
589 *flags
= wxNB_HITTEST_NOWHERE
;
591 // first check that it is in this window at all
592 if ( !GetClientRect().Inside(pt
) )
597 wxRect rectTabs
= GetAllTabsRect();
599 switch ( GetTabOrientation() )
602 wxFAIL_MSG(_T("unknown tab orientation"));
606 if ( pt
.y
> rectTabs
.GetBottom() )
611 if ( pt
.y
< rectTabs
.y
)
616 if ( pt
.x
> rectTabs
.GetRight() )
621 if ( pt
.x
< rectTabs
.x
)
626 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
628 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
630 if ( rectTabs
.Inside(pt
) )
634 // TODO: be more precise
635 *flags
= wxNB_HITTEST_ONITEM
;
641 // move the rectTabs to the next tab
643 rectTabs
.y
+= rectTabs
.height
;
645 rectTabs
.x
+= rectTabs
.width
;
651 bool wxNotebook::IsVertical() const
653 wxDirection dir
= GetTabOrientation();
655 return dir
== wxLEFT
|| dir
== wxRIGHT
;
658 wxDirection
wxNotebook::GetTabOrientation() const
660 long style
= GetWindowStyle();
661 if ( style
& wxNB_BOTTOM
)
663 else if ( style
& wxNB_RIGHT
)
665 else if ( style
& wxNB_LEFT
)
668 // wxNB_TOP == 0 so we don't have to test for it
672 wxRect
wxNotebook::GetTabRect(int page
) const
675 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
677 // calc the size of this tab and of the preceding ones
678 wxCoord widthThis
, widthBefore
;
679 if ( FixedSizeTabs() )
681 widthThis
= m_widthMax
;
682 widthBefore
= page
*m_widthMax
;
687 for ( int n
= 0; n
< page
; n
++ )
689 widthBefore
+= m_widths
[n
];
692 widthThis
= m_widths
[page
];
695 rect
= GetTabsPart();
698 rect
.y
+= widthBefore
- m_offset
;
699 rect
.height
= widthThis
;
703 rect
.x
+= widthBefore
- m_offset
;
704 rect
.width
= widthThis
;
710 wxRect
wxNotebook::GetAllTabsRect() const
714 if ( GetPageCount() )
716 const wxSize indent
= GetRenderer()->GetTabIndent();
717 wxSize size
= GetClientSize();
721 rect
.width
= m_heightTab
+ indent
.x
;
722 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
724 rect
.height
= size
.y
;
730 rect
.height
= m_heightTab
+ indent
.y
;
731 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
739 wxRect
wxNotebook::GetTabsPart() const
741 wxRect rect
= GetAllTabsRect();
743 wxDirection dir
= GetTabOrientation();
745 const wxSize indent
= GetRenderer()->GetTabIndent();
757 rect
.height
-= indent
.y
;
761 rect
.height
-= indent
.y
;
768 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
770 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
774 // width and height have inverted meaning
780 // height is always fixed
783 // width may also be fixed and be the same for all tabs
784 *w
= GetTabWidth(page
);
787 void wxNotebook::SetTabSize(const wxSize
& sz
)
789 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
803 wxSize
wxNotebook::CalcTabSize(int page
) const
805 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
806 // method is called to calculate them
810 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
812 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
814 if ( HasImage(page
) )
817 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
819 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
821 if ( sizeImage
.y
> size
.y
)
822 size
.y
= sizeImage
.y
;
825 size
.x
+= 2*m_sizePad
.x
;
826 size
.y
+= 2*m_sizePad
.y
;
831 void wxNotebook::ResizeTab(int page
)
833 wxSize sizeTab
= CalcTabSize(page
);
835 // we only need full relayout if the page size changes
836 bool needsRelayout
= false;
841 wxCoord tmp
= sizeTab
.x
;
842 sizeTab
.x
= sizeTab
.y
;
846 if ( sizeTab
.y
> m_heightTab
)
848 needsRelayout
= true;
850 m_heightTab
= sizeTab
.y
;
853 m_widths
[page
] = sizeTab
.x
;
855 if ( sizeTab
.x
> m_widthMax
)
856 m_widthMax
= sizeTab
.x
;
858 // the total of the tabs has changed too
867 void wxNotebook::SetPadding(const wxSize
& padding
)
869 if ( padding
!= m_sizePad
)
877 void wxNotebook::Relayout()
879 if ( GetPageCount() )
885 if ( m_sel
!= INVALID_PAGE
)
887 // resize the currently shown page
888 wxRect rectPage
= GetPageRect();
890 m_pages
[m_sel
]->SetSize(rectPage
);
892 // also scroll it into view if needed (note that m_lastVisible
893 // was updated by the call to UpdateSpinBtn() above, this is why it
897 if ( m_sel
< m_firstVisible
)
899 // selection is to the left of visible part of tabs
902 else if ( m_sel
> m_lastFullyVisible
)
904 // selection is to the right of visible part of tabs
910 else // we have no pages
912 // just refresh everything
917 wxRect
wxNotebook::GetPagePart() const
919 wxRect rectPage
= GetClientRect();
921 if ( GetPageCount() )
923 wxRect rectTabs
= GetAllTabsRect();
924 wxDirection dir
= GetTabOrientation();
927 rectPage
.width
-= rectTabs
.width
;
929 rectPage
.x
+= rectTabs
.width
;
933 rectPage
.height
-= rectTabs
.height
;
935 rectPage
.y
+= rectTabs
.height
;
938 //else: no pages at all
943 wxRect
wxNotebook::GetPageRect() const
945 wxRect rect
= GetPagePart();
947 // leave space for the border
948 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
950 // FIXME: hardcoded +2!
951 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
952 -(rectBorder
.y
+ rectBorder
.height
+ 2));
957 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
959 wxSize sizeNb
= size
;
960 wxRect rect
= GetAllTabsRect();
962 sizeNb
.x
+= rect
.width
;
964 sizeNb
.y
+= rect
.height
;
969 void wxNotebook::SetPageSize(const wxSize
& size
)
971 SetClientSize(GetSizeForPage(size
));
974 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
976 return AdjustSize(GetSizeForPage(sizePage
));
979 // ----------------------------------------------------------------------------
980 // wxNotebook spin button
981 // ----------------------------------------------------------------------------
983 bool wxNotebook::HasSpinBtn() const
985 return m_spinbtn
&& m_spinbtn
->IsShown();
988 void wxNotebook::CalcLastVisibleTab()
990 bool isVertical
= IsVertical();
992 wxCoord width
= GetClientSize().x
;
994 wxRect rect
= GetTabsPart();
996 size_t count
= GetPageCount();
998 wxCoord widthLast
= 0;
1000 for ( n
= m_firstVisible
; n
< count
; n
++ )
1002 GetTabSize(n
, &rect
.width
, &rect
.height
);
1003 if ( rect
.GetRight() > width
)
1008 // remember it to use below
1009 widthLast
= rect
.GetRight();
1011 // move the rect to the next tab
1013 rect
.y
+= rect
.height
;
1015 rect
.x
+= rect
.width
;
1018 if ( n
== m_firstVisible
)
1020 // even the first tab isn't fully visible - but still pretend it is as
1021 // we have to show something
1022 m_lastFullyVisible
= m_firstVisible
;
1024 else // more than one tab visible
1026 m_lastFullyVisible
= n
- 1;
1028 // but is it really fully visible? it shouldn't overlap with the spin
1029 // button if it is present (again, even if the first button does
1030 // overlap with it, we pretend that it doesn't as there is not much
1032 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1034 // adjust width to be the width before the spin button
1035 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1037 width
-= sizeSpinBtn
.y
;
1039 width
-= sizeSpinBtn
.x
;
1041 if ( widthLast
> width
)
1043 // the last button overlaps with spin button, so take he
1045 m_lastFullyVisible
--;
1052 // everything is visible
1057 // this tab is still (partially) visible, so m_lastVisible is the
1058 // next tab (remember, this is "exclusive" last)
1059 m_lastVisible
= n
+ 1;
1064 void wxNotebook::UpdateSpinBtn()
1066 // first decide if we need a spin button
1069 size_t count
= (size_t)GetPageCount();
1072 // this case is special, get rid of it immediately: everything is
1073 // visible and we don't need any spin buttons
1074 allTabsShown
= true;
1076 // have to reset them manually as we don't call CalcLastVisibleTab()
1079 m_lastFullyVisible
= 0;
1083 CalcLastVisibleTab();
1085 // if all tabs after the first visible one are shown, it doesn't yet
1086 // mean that all tabs are shown - so we go backwards until we arrive to
1087 // the beginning (then all tabs are indeed shown) or find a tab such
1088 // that not all tabs after it are shown
1089 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1091 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1093 m_offset
-= GetTabWidth(m_firstVisible
--);
1095 // reclaculate after m_firstVisible change
1096 CalcLastVisibleTab();
1099 allTabsShown
= m_lastFullyVisible
== count
- 1;
1102 if ( !allTabsShown
)
1106 // create it once only
1107 m_spinbtn
= new wxNotebookSpinBtn(this);
1109 // set the correct value to keep it in sync
1110 m_spinbtn
->SetValue(m_sel
);
1113 // position it correctly
1119 // also set/update the range
1120 m_spinbtn
->SetRange(0, count
- 1);
1122 // update m_lastFullyVisible once again as it might have changed
1123 // because the spin button appeared
1125 // FIXME: might do it more efficiently
1126 CalcLastVisibleTab();
1128 else // all tabs are visible, we don't need spin button
1130 if ( m_spinbtn
&& m_spinbtn
-> IsShown() )
1137 void wxNotebook::PositionSpinBtn()
1143 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1145 wxRect rectTabs
= GetAllTabsRect();
1148 switch ( GetTabOrientation() )
1151 wxFAIL_MSG(_T("unknown tab orientation"));
1155 x
= rectTabs
.GetRight() - wBtn
;
1156 y
= rectTabs
.GetBottom() - hBtn
;
1160 x
= rectTabs
.GetRight() - wBtn
;
1161 y
= rectTabs
.GetTop();
1165 x
= rectTabs
.GetRight() - wBtn
;
1166 y
= rectTabs
.GetBottom() - hBtn
;
1170 x
= rectTabs
.GetLeft();
1171 y
= rectTabs
.GetBottom() - hBtn
;
1175 m_spinbtn
->Move(x
, y
);
1178 // ----------------------------------------------------------------------------
1179 // wxNotebook scrolling
1180 // ----------------------------------------------------------------------------
1182 void wxNotebook::ScrollTo(int page
)
1184 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1186 // set the first visible tab and offset (easy)
1187 m_firstVisible
= (size_t)page
;
1189 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1191 m_offset
+= GetTabWidth(n
);
1194 // find the last visible tab too
1195 CalcLastVisibleTab();
1200 void wxNotebook::ScrollLastTo(int page
)
1202 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1204 // go backwards until we find the first tab which can be made visible
1205 // without hiding the given one
1206 wxSize size
= GetClientSize();
1207 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1208 widthTabs
= GetTabWidth(page
);
1210 // the total width is less than the width of the window if we have the spin
1214 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1216 widthAll
-= sizeSpinBtn
.y
;
1218 widthAll
-= sizeSpinBtn
.x
;
1221 m_firstVisible
= page
;
1222 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1224 widthTabs
+= GetTabWidth(--m_firstVisible
);
1227 if ( widthTabs
> widthAll
)
1229 // take one step back (that it is forward) if we can
1230 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1235 ScrollTo(m_firstVisible
);
1237 // consitency check: the page we were asked to show should be shown
1238 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1241 // ----------------------------------------------------------------------------
1242 // wxNotebook sizing/moving
1243 // ----------------------------------------------------------------------------
1245 wxSize
wxNotebook::DoGetBestClientSize() const
1247 // calculate the max page size
1250 size_t count
= GetPageCount();
1253 for ( size_t n
= 0; n
< count
; n
++ )
1255 wxSize sizePage
= m_pages
[n
]->GetSize();
1257 if ( size
.x
< sizePage
.x
)
1258 size
.x
= sizePage
.x
;
1259 if ( size
.y
< sizePage
.y
)
1260 size
.y
= sizePage
.y
;
1265 // use some arbitrary default size
1270 return GetSizeForPage(size
);
1273 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1275 wxControl::DoMoveWindow(x
, y
, width
, height
);
1277 // move the spin ctrl too (NOP if it doesn't exist)
1281 void wxNotebook::DoSetSize(int x
, int y
,
1282 int width
, int height
,
1285 wxSize old_client_size
= GetClientSize();
1287 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1289 wxSize new_client_size
= GetClientSize();
1291 if (old_client_size
!= new_client_size
)
1295 // ----------------------------------------------------------------------------
1296 // wxNotebook input processing
1297 // ----------------------------------------------------------------------------
1299 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1301 const wxString
& strArg
)
1303 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1304 SetSelection(GetNextPage(true));
1305 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1306 SetSelection(GetNextPage(false));
1307 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1308 SetSelection((int)numArg
);
1310 return wxControl::PerformAction(action
, numArg
, strArg
);
1315 // ----------------------------------------------------------------------------
1316 // wxStdNotebookInputHandler
1317 // ----------------------------------------------------------------------------
1319 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1320 : wxStdInputHandler(inphand
)
1324 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1325 const wxKeyEvent
& event
,
1328 // ignore the key releases
1331 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1334 wxControlAction action
;
1335 switch ( event
.GetKeyCode() )
1338 if ( notebook
->IsVertical() )
1339 action
= wxACTION_NOTEBOOK_PREV
;
1343 if ( !notebook
->IsVertical() )
1344 action
= wxACTION_NOTEBOOK_PREV
;
1348 if ( notebook
->IsVertical() )
1349 action
= wxACTION_NOTEBOOK_NEXT
;
1353 if ( !notebook
->IsVertical() )
1354 action
= wxACTION_NOTEBOOK_NEXT
;
1358 action
= wxACTION_NOTEBOOK_GOTO
;
1359 // page = 0; -- already has this value
1363 action
= wxACTION_NOTEBOOK_GOTO
;
1364 page
= notebook
->GetPageCount() - 1;
1368 if ( !action
.IsEmpty() )
1370 return consumer
->PerformAction(action
, page
);
1374 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1377 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1378 const wxMouseEvent
& event
)
1380 if ( event
.ButtonDown(1) )
1382 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1383 int page
= notebook
->HitTest(event
.GetPosition());
1386 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1392 return wxStdInputHandler::HandleMouse(consumer
, event
);
1395 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1396 const wxMouseEvent
& event
)
1398 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1402 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1403 const wxFocusEvent
& WXUNUSED(event
))
1405 HandleFocusChange(consumer
);
1410 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1411 bool WXUNUSED(activated
))
1413 // we react to the focus change in the same way as to the [de]activation
1414 HandleFocusChange(consumer
);
1419 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1421 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1422 notebook
->RefreshCurrent();
1425 #endif // wxUSE_NOTEBOOK