1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 #include "wx/wxprec.h"
28 #include "wx/notebook.h"
31 #include "wx/dcmemory.h"
34 #include "wx/imaglist.h"
35 #include "wx/spinbutt.h"
37 #include "wx/univ/renderer.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
44 // due to unsigned type nPage is always >= 0
45 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
47 #define IS_VALID_PAGE(nPage) (((size_t)nPage) < GetPageCount())
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const size_t INVALID_PAGE
= (size_t)-1;
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 class wxNotebookSpinBtn
: public wxSpinButton
66 wxNotebookSpinBtn(wxNotebook
*nb
)
67 : wxSpinButton(nb
, wxID_ANY
,
68 wxDefaultPosition
, wxDefaultSize
,
69 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
75 void OnSpin(wxSpinEvent
& event
)
77 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
86 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
87 EVT_SPIN(wxID_ANY
, wxNotebookSpinBtn::OnSpin
)
90 // ============================================================================
92 // ============================================================================
94 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
95 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
97 // ----------------------------------------------------------------------------
98 // wxNotebook creation
99 // ----------------------------------------------------------------------------
101 void wxNotebook::Init()
103 m_sel
= INVALID_PAGE
;
110 m_lastFullyVisible
= 0;
117 bool wxNotebook::Create(wxWindow
*parent
,
122 const wxString
& name
)
124 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
127 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
128 wxDefaultValidator
, name
) )
131 m_sizePad
= GetRenderer()->GetTabPadding();
135 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
140 // ----------------------------------------------------------------------------
141 // wxNotebook page titles and images
142 // ----------------------------------------------------------------------------
144 wxString
wxNotebook::GetPageText(size_t nPage
) const
146 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, _T("invalid notebook page") );
148 return m_titles
[nPage
];
151 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
153 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
155 if ( strText
!= m_titles
[nPage
] )
157 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
159 if ( FixedSizeTabs() )
161 // it's enough to just reresh this one
164 else // var width tabs
166 // we need to resize the tab to fit the new string
174 int wxNotebook::GetPageImage(size_t nPage
) const
176 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, _T("invalid notebook page") );
178 return m_images
[nPage
];
181 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
183 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
185 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
186 _T("invalid image index in SetPageImage()") );
188 if ( nImage
!= m_images
[nPage
] )
190 // if the item didn't have an icon before or, on the contrary, did have
191 // it but has lost it now, its size will change - but if the icon just
193 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
194 m_images
[nPage
] = nImage
;
196 if ( tabSizeChanges
)
205 wxNotebook::~wxNotebook()
209 // ----------------------------------------------------------------------------
210 // wxNotebook page switching
211 // ----------------------------------------------------------------------------
213 int wxNotebook::SetSelection(size_t nPage
)
215 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, _T("invalid notebook page") );
217 if ( (size_t)nPage
== m_sel
)
219 // don't do anything if there is nothing to do
224 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
225 event
.SetSelection(nPage
);
226 event
.SetOldSelection(m_sel
);
227 event
.SetEventObject(this);
228 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
230 // program doesn't allow the page change
234 // we need to change m_sel first, before calling RefreshTab() below as
235 // otherwise the previously selected tab wouldn't be redrawn properly under
236 // wxGTK which calls Refresh() immediately and not during the next event
237 // loop iteration as wxMSW does and as it should
238 size_t selOld
= m_sel
;
242 if ( selOld
!= INVALID_PAGE
)
244 RefreshTab(selOld
, true /* this tab was selected */);
246 m_pages
[selOld
]->Hide();
249 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
254 m_spinbtn
->SetValue(m_sel
);
257 if ( m_sel
< m_firstVisible
)
259 // selection is to the left of visible part of tabs
262 else if ( m_sel
> m_lastFullyVisible
)
264 // selection is to the right of visible part of tabs
267 else // we already see this tab
273 m_pages
[m_sel
]->SetSize(GetPageRect());
274 m_pages
[m_sel
]->Show();
278 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
279 GetEventHandler()->ProcessEvent(event
);
284 // ----------------------------------------------------------------------------
285 // wxNotebook pages adding/deleting
286 // ----------------------------------------------------------------------------
288 bool wxNotebook::InsertPage(size_t nPage
,
289 wxNotebookPage
*pPage
,
290 const wxString
& strText
,
294 size_t nPages
= GetPageCount();
295 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), false,
296 _T("invalid notebook page in InsertPage()") );
299 m_pages
.Insert(pPage
, nPage
);
302 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
303 m_titles
.Insert(label
, nPage
);
305 m_images
.Insert(imageId
, nPage
);
307 // cache the tab geometry here
308 wxSize sizeTab
= CalcTabSize(nPage
);
310 if ( sizeTab
.y
> m_heightTab
)
311 m_heightTab
= sizeTab
.y
;
313 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
314 m_widthMax
= sizeTab
.x
;
316 m_widths
.Insert(sizeTab
.x
, nPage
);
318 // spin button may appear if we didn't have it before - but even if we did,
319 // its range should change, so update it unconditionally
322 // if the tab has just appeared, we have to relayout everything, otherwise
323 // it's enough to just redraw the tabs
326 // always select the first tab to have at least some selection
332 else // not the first tab
341 else // pages added to the notebook are initially hidden
349 bool wxNotebook::DeleteAllPages()
351 if ( !wxNotebookBase::DeleteAllPages() )
354 // clear the other arrays as well
360 // it is not valid any longer
361 m_sel
= INVALID_PAGE
;
363 // spin button is not needed any more
371 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
373 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
375 wxNotebookPage
*page
= m_pages
[nPage
];
376 m_pages
.RemoveAt(nPage
);
377 m_titles
.RemoveAt(nPage
);
378 m_accels
.RemoveAt(nPage
);
379 m_widths
.RemoveAt(nPage
);
380 m_images
.RemoveAt(nPage
);
382 // the spin button might not be needed any more
383 // 2002-08-12 'if' commented out by JACS on behalf
384 // of Hans Van Leemputten <Hansvl@softhome.net> who
385 // points out that UpdateSpinBtn should always be called,
386 // to ensure m_lastVisible is up to date.
387 // if ( HasSpinBtn() )
392 size_t count
= GetPageCount();
395 if ( m_sel
== (size_t)nPage
)
397 // avoid sending event to this page which doesn't exist in the
399 m_sel
= INVALID_PAGE
;
401 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
403 else if ( m_sel
> (size_t)nPage
)
405 // no need to change selection, just adjust the index
409 else // no more tabs left
411 m_sel
= INVALID_PAGE
;
414 // have to refresh everything
420 // ----------------------------------------------------------------------------
421 // wxNotebook drawing
422 // ----------------------------------------------------------------------------
424 void wxNotebook::RefreshCurrent()
426 if ( m_sel
!= INVALID_PAGE
)
432 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
434 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
436 wxRect rect
= GetTabRect(page
);
437 if ( forceSelected
|| ((size_t)page
== m_sel
) )
439 const wxSize indent
= GetRenderer()->GetTabIndent();
440 rect
.Inflate(indent
.x
, indent
.y
);
446 void wxNotebook::RefreshAllTabs()
448 wxRect rect
= GetAllTabsRect();
449 if ( rect
.width
|| rect
.height
)
453 //else: we don't have tabs at all
456 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
461 int image
= m_images
[n
];
463 // Not needed now that wxGenericImageList is being
464 // used for wxUniversal under MSW
465 #if 0 // def __WXMSW__ // FIXME
467 m_imageList
->GetSize(n
, w
, h
);
470 dc
.SelectObject(bmp
);
471 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
472 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, true);
473 dc
.SelectObject(wxNullBitmap
);
475 bmp
= m_imageList
->GetBitmap(image
);
482 flags
|= wxCONTROL_SELECTED
;
485 flags
|= wxCONTROL_FOCUSED
;
488 GetRenderer()->DrawTab
500 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
502 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
504 wxDC
& dc
= renderer
->GetDC();
505 dc
.SetFont(GetFont());
506 dc
.SetTextForeground(GetForegroundColour());
508 // redraw the border - it's simpler to always do it instead of checking
509 // whether this needs to be done
510 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
512 // avoid overwriting the spin button
515 wxRect rectTabs
= GetAllTabsRect();
516 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
520 rectTabs
.height
-= sizeSpinBtn
.y
;
522 // Allow for erasing the line under selected tab
527 rectTabs
.width
-= sizeSpinBtn
.x
;
529 // Allow for erasing the line under selected tab
530 rectTabs
.height
+= 2;
533 dc
.SetClippingRegion(rectTabs
);
536 wxRect rect
= GetTabsPart();
537 bool isVertical
= IsVertical();
540 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
542 GetTabSize(n
, &rect
.width
, &rect
.height
);
546 // don't redraw it now as this tab has to be drawn over the other
547 // ones as it takes more place and spills over to them
550 else // not selected tab
552 // unfortunately we can't do this because the selected tab hangs
553 // over its neighbours and so we might need to refresh more tabs -
554 // of course, we could still avoid rereshing some of them with more
555 // complicated checks, but it doesn't seem too bad to refresh all
556 // of them, I still don't see flicker, so leaving as is for now
558 //if ( rectUpdate.Intersects(rect) )
560 DoDrawTab(dc
, rect
, n
);
562 //else: doesn't need to be refreshed
565 // move the rect to the next tab
567 rect
.y
+= rect
.height
;
569 rect
.x
+= rect
.width
;
572 // now redraw the selected tab
575 DoDrawTab(dc
, rectSel
, m_sel
);
578 dc
.DestroyClippingRegion();
581 // ----------------------------------------------------------------------------
582 // wxNotebook geometry
583 // ----------------------------------------------------------------------------
585 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
588 *flags
= wxBK_HITTEST_NOWHERE
;
590 // first check that it is in this window at all
591 if ( !GetClientRect().Inside(pt
) )
596 wxRect rectTabs
= GetAllTabsRect();
598 switch ( GetTabOrientation() )
601 wxFAIL_MSG(_T("unknown tab orientation"));
605 if ( pt
.y
> rectTabs
.GetBottom() )
610 if ( pt
.y
< rectTabs
.y
)
615 if ( pt
.x
> rectTabs
.GetRight() )
620 if ( pt
.x
< rectTabs
.x
)
625 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
627 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
629 if ( rectTabs
.Inside(pt
) )
633 // TODO: be more precise
634 *flags
= wxBK_HITTEST_ONITEM
;
640 // move the rectTabs to the next tab
642 rectTabs
.y
+= rectTabs
.height
;
644 rectTabs
.x
+= rectTabs
.width
;
650 bool wxNotebook::IsVertical() const
652 wxDirection dir
= GetTabOrientation();
654 return dir
== wxLEFT
|| dir
== wxRIGHT
;
657 wxDirection
wxNotebook::GetTabOrientation() const
659 long style
= GetWindowStyle();
660 if ( style
& wxBK_BOTTOM
)
662 else if ( style
& wxBK_RIGHT
)
664 else if ( style
& wxBK_LEFT
)
667 // wxBK_TOP == 0 so we don't have to test for it
671 wxRect
wxNotebook::GetTabRect(int page
) const
674 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
676 // calc the size of this tab and of the preceding ones
677 wxCoord widthThis
, widthBefore
;
678 if ( FixedSizeTabs() )
680 widthThis
= m_widthMax
;
681 widthBefore
= page
*m_widthMax
;
686 for ( int n
= 0; n
< page
; n
++ )
688 widthBefore
+= m_widths
[n
];
691 widthThis
= m_widths
[page
];
694 rect
= GetTabsPart();
697 rect
.y
+= widthBefore
- m_offset
;
698 rect
.height
= widthThis
;
702 rect
.x
+= widthBefore
- m_offset
;
703 rect
.width
= widthThis
;
709 wxRect
wxNotebook::GetAllTabsRect() const
713 if ( GetPageCount() )
715 const wxSize indent
= GetRenderer()->GetTabIndent();
716 wxSize size
= GetClientSize();
720 rect
.width
= m_heightTab
+ indent
.x
;
721 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
723 rect
.height
= size
.y
;
729 rect
.height
= m_heightTab
+ indent
.y
;
730 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
738 wxRect
wxNotebook::GetTabsPart() const
740 wxRect rect
= GetAllTabsRect();
742 wxDirection dir
= GetTabOrientation();
744 const wxSize indent
= GetRenderer()->GetTabIndent();
751 rect
.width
-= indent
.y
;
755 rect
.width
-= indent
.y
;
764 rect
.height
-= indent
.y
;
768 rect
.height
-= indent
.y
;
775 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
777 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
781 // width and height have inverted meaning
787 // height is always fixed
790 // width may also be fixed and be the same for all tabs
791 *w
= GetTabWidth(page
);
794 void wxNotebook::SetTabSize(const wxSize
& sz
)
796 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
810 wxSize
wxNotebook::CalcTabSize(int page
) const
812 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
813 // method is called to calculate them
817 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
819 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
821 if ( HasImage(page
) )
824 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
826 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
828 if ( sizeImage
.y
> size
.y
)
829 size
.y
= sizeImage
.y
;
832 size
.x
+= 2*m_sizePad
.x
;
833 size
.y
+= 2*m_sizePad
.y
;
838 void wxNotebook::ResizeTab(int page
)
840 wxSize sizeTab
= CalcTabSize(page
);
842 // we only need full relayout if the page size changes
843 bool needsRelayout
= false;
848 wxCoord tmp
= sizeTab
.x
;
849 sizeTab
.x
= sizeTab
.y
;
853 if ( sizeTab
.y
> m_heightTab
)
855 needsRelayout
= true;
857 m_heightTab
= sizeTab
.y
;
860 m_widths
[page
] = sizeTab
.x
;
862 if ( sizeTab
.x
> m_widthMax
)
863 m_widthMax
= sizeTab
.x
;
865 // the total of the tabs has changed too
874 void wxNotebook::SetPadding(const wxSize
& padding
)
876 if ( padding
!= m_sizePad
)
884 void wxNotebook::Relayout()
886 if ( GetPageCount() )
892 if ( m_sel
!= INVALID_PAGE
)
894 // resize the currently shown page
895 wxRect rectPage
= GetPageRect();
897 m_pages
[m_sel
]->SetSize(rectPage
);
899 // also scroll it into view if needed (note that m_lastVisible
900 // was updated by the call to UpdateSpinBtn() above, this is why it
904 if ( m_sel
< m_firstVisible
)
906 // selection is to the left of visible part of tabs
909 else if ( m_sel
> m_lastFullyVisible
)
911 // selection is to the right of visible part of tabs
917 else // we have no pages
919 // just refresh everything
924 wxRect
wxNotebook::GetPagePart() const
926 wxRect rectPage
= GetClientRect();
928 if ( GetPageCount() )
930 wxRect rectTabs
= GetAllTabsRect();
931 wxDirection dir
= GetTabOrientation();
934 rectPage
.width
-= rectTabs
.width
;
936 rectPage
.x
+= rectTabs
.width
;
940 rectPage
.height
-= rectTabs
.height
;
942 rectPage
.y
+= rectTabs
.height
;
945 //else: no pages at all
950 wxRect
wxNotebook::GetPageRect() const
952 wxRect rect
= GetPagePart();
954 // leave space for the border
955 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
957 // FIXME: hardcoded +2!
958 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
959 -(rectBorder
.y
+ rectBorder
.height
+ 2));
964 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
966 wxSize sizeNb
= size
;
967 wxRect rect
= GetAllTabsRect();
969 sizeNb
.x
+= rect
.width
;
971 sizeNb
.y
+= rect
.height
;
976 void wxNotebook::SetPageSize(const wxSize
& size
)
978 SetClientSize(GetSizeForPage(size
));
981 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
983 return AdjustSize(GetSizeForPage(sizePage
));
986 // ----------------------------------------------------------------------------
987 // wxNotebook spin button
988 // ----------------------------------------------------------------------------
990 bool wxNotebook::HasSpinBtn() const
992 return m_spinbtn
&& m_spinbtn
->IsShown();
995 void wxNotebook::CalcLastVisibleTab()
997 bool isVertical
= IsVertical();
999 wxCoord width
= GetClientSize().x
;
1001 wxRect rect
= GetTabsPart();
1003 size_t count
= GetPageCount();
1005 wxCoord widthLast
= 0;
1007 for ( n
= m_firstVisible
; n
< count
; n
++ )
1009 GetTabSize(n
, &rect
.width
, &rect
.height
);
1010 if ( rect
.GetRight() > width
)
1015 // remember it to use below
1016 widthLast
= rect
.GetRight();
1018 // move the rect to the next tab
1020 rect
.y
+= rect
.height
;
1022 rect
.x
+= rect
.width
;
1025 if ( n
== m_firstVisible
)
1027 // even the first tab isn't fully visible - but still pretend it is as
1028 // we have to show something
1029 m_lastFullyVisible
= m_firstVisible
;
1031 else // more than one tab visible
1033 m_lastFullyVisible
= n
- 1;
1035 // but is it really fully visible? it shouldn't overlap with the spin
1036 // button if it is present (again, even if the first button does
1037 // overlap with it, we pretend that it doesn't as there is not much
1039 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1041 // adjust width to be the width before the spin button
1042 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1044 width
-= sizeSpinBtn
.y
;
1046 width
-= sizeSpinBtn
.x
;
1048 if ( widthLast
> width
)
1050 // the last button overlaps with spin button, so take he
1052 m_lastFullyVisible
--;
1059 // everything is visible
1064 // this tab is still (partially) visible, so m_lastVisible is the
1065 // next tab (remember, this is "exclusive" last)
1066 m_lastVisible
= n
+ 1;
1071 void wxNotebook::UpdateSpinBtn()
1073 // first decide if we need a spin button
1076 size_t count
= (size_t)GetPageCount();
1079 // this case is special, get rid of it immediately: everything is
1080 // visible and we don't need any spin buttons
1081 allTabsShown
= true;
1083 // have to reset them manually as we don't call CalcLastVisibleTab()
1086 m_lastFullyVisible
= 0;
1090 CalcLastVisibleTab();
1092 // if all tabs after the first visible one are shown, it doesn't yet
1093 // mean that all tabs are shown - so we go backwards until we arrive to
1094 // the beginning (then all tabs are indeed shown) or find a tab such
1095 // that not all tabs after it are shown
1096 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1098 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1100 m_offset
-= GetTabWidth(m_firstVisible
--);
1102 // reclaculate after m_firstVisible change
1103 CalcLastVisibleTab();
1106 allTabsShown
= m_lastFullyVisible
== count
- 1;
1109 if ( !allTabsShown
)
1113 // create it once only
1114 m_spinbtn
= new wxNotebookSpinBtn(this);
1116 // set the correct value to keep it in sync
1117 m_spinbtn
->SetValue(m_sel
);
1120 // position it correctly
1126 // also set/update the range
1127 m_spinbtn
->SetRange(0, count
- 1);
1129 // update m_lastFullyVisible once again as it might have changed
1130 // because the spin button appeared
1132 // FIXME: might do it more efficiently
1133 CalcLastVisibleTab();
1135 else // all tabs are visible, we don't need spin button
1137 if ( m_spinbtn
&& m_spinbtn
-> IsShown() )
1144 void wxNotebook::PositionSpinBtn()
1150 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1152 wxRect rectTabs
= GetAllTabsRect();
1155 switch ( GetTabOrientation() )
1158 wxFAIL_MSG(_T("unknown tab orientation"));
1162 x
= rectTabs
.GetRight() - wBtn
;
1163 y
= rectTabs
.GetBottom() - hBtn
;
1167 x
= rectTabs
.GetRight() - wBtn
;
1168 y
= rectTabs
.GetTop();
1172 x
= rectTabs
.GetRight() - wBtn
;
1173 y
= rectTabs
.GetBottom() - hBtn
;
1177 x
= rectTabs
.GetLeft();
1178 y
= rectTabs
.GetBottom() - hBtn
;
1182 m_spinbtn
->Move(x
, y
);
1185 // ----------------------------------------------------------------------------
1186 // wxNotebook scrolling
1187 // ----------------------------------------------------------------------------
1189 void wxNotebook::ScrollTo(int page
)
1191 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1193 // set the first visible tab and offset (easy)
1194 m_firstVisible
= (size_t)page
;
1196 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1198 m_offset
+= GetTabWidth(n
);
1201 // find the last visible tab too
1202 CalcLastVisibleTab();
1207 void wxNotebook::ScrollLastTo(int page
)
1209 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1211 // go backwards until we find the first tab which can be made visible
1212 // without hiding the given one
1213 wxSize size
= GetClientSize();
1214 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1215 widthTabs
= GetTabWidth(page
);
1217 // the total width is less than the width of the window if we have the spin
1221 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1223 widthAll
-= sizeSpinBtn
.y
;
1225 widthAll
-= sizeSpinBtn
.x
;
1228 m_firstVisible
= page
;
1229 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1231 widthTabs
+= GetTabWidth(--m_firstVisible
);
1234 if ( widthTabs
> widthAll
)
1236 // take one step back (that it is forward) if we can
1237 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1242 ScrollTo(m_firstVisible
);
1244 // consitency check: the page we were asked to show should be shown
1245 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1248 // ----------------------------------------------------------------------------
1249 // wxNotebook sizing/moving
1250 // ----------------------------------------------------------------------------
1252 wxSize
wxNotebook::DoGetBestClientSize() const
1254 // calculate the max page size
1257 size_t count
= GetPageCount();
1260 for ( size_t n
= 0; n
< count
; n
++ )
1262 wxSize sizePage
= m_pages
[n
]->GetSize();
1264 if ( size
.x
< sizePage
.x
)
1265 size
.x
= sizePage
.x
;
1266 if ( size
.y
< sizePage
.y
)
1267 size
.y
= sizePage
.y
;
1272 // use some arbitrary default size
1277 return GetSizeForPage(size
);
1280 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1282 wxControl::DoMoveWindow(x
, y
, width
, height
);
1284 // move the spin ctrl too (NOP if it doesn't exist)
1288 void wxNotebook::DoSetSize(int x
, int y
,
1289 int width
, int height
,
1292 wxSize old_client_size
= GetClientSize();
1294 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1296 wxSize new_client_size
= GetClientSize();
1298 if (old_client_size
!= new_client_size
)
1302 // ----------------------------------------------------------------------------
1303 // wxNotebook input processing
1304 // ----------------------------------------------------------------------------
1306 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1308 const wxString
& strArg
)
1310 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1311 SetSelection(GetNextPage(true));
1312 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1313 SetSelection(GetNextPage(false));
1314 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1315 SetSelection((int)numArg
);
1317 return wxControl::PerformAction(action
, numArg
, strArg
);
1322 // ----------------------------------------------------------------------------
1323 // wxStdNotebookInputHandler
1324 // ----------------------------------------------------------------------------
1326 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1327 : wxStdInputHandler(inphand
)
1331 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1332 const wxKeyEvent
& event
,
1335 // ignore the key releases
1338 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1341 wxControlAction action
;
1342 switch ( event
.GetKeyCode() )
1345 if ( notebook
->IsVertical() )
1346 action
= wxACTION_NOTEBOOK_PREV
;
1350 if ( !notebook
->IsVertical() )
1351 action
= wxACTION_NOTEBOOK_PREV
;
1355 if ( notebook
->IsVertical() )
1356 action
= wxACTION_NOTEBOOK_NEXT
;
1360 if ( !notebook
->IsVertical() )
1361 action
= wxACTION_NOTEBOOK_NEXT
;
1365 action
= wxACTION_NOTEBOOK_GOTO
;
1366 // page = 0; -- already has this value
1370 action
= wxACTION_NOTEBOOK_GOTO
;
1371 page
= notebook
->GetPageCount() - 1;
1375 if ( !action
.IsEmpty() )
1377 return consumer
->PerformAction(action
, page
);
1381 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1384 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1385 const wxMouseEvent
& event
)
1387 if ( event
.ButtonDown(1) )
1389 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1390 int page
= notebook
->HitTest(event
.GetPosition());
1393 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1399 return wxStdInputHandler::HandleMouse(consumer
, event
);
1402 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1403 const wxMouseEvent
& event
)
1405 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1409 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1410 const wxFocusEvent
& WXUNUSED(event
))
1412 HandleFocusChange(consumer
);
1417 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1418 bool WXUNUSED(activated
))
1420 // we react to the focus change in the same way as to the [de]activation
1421 HandleFocusChange(consumer
);
1426 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1428 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1429 notebook
->RefreshCurrent();
1432 #endif // wxUSE_NOTEBOOK