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/imaglist.h"
29 #include "wx/notebook.h"
30 #include "wx/spinbutt.h"
31 #include "wx/dcmemory.h"
33 #include "wx/univ/renderer.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
40 // due to unsigned type nPage is always >= 0
41 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
43 #define IS_VALID_PAGE(nPage) (((size_t)nPage) < GetPageCount())
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 static const size_t INVALID_PAGE
= (size_t)-1;
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 class wxNotebookSpinBtn
: public wxSpinButton
62 wxNotebookSpinBtn(wxNotebook
*nb
)
63 : wxSpinButton(nb
, wxID_ANY
,
64 wxDefaultPosition
, wxDefaultSize
,
65 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
71 void OnSpin(wxSpinEvent
& event
)
73 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
82 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
83 EVT_SPIN(wxID_ANY
, wxNotebookSpinBtn::OnSpin
)
86 // ============================================================================
88 // ============================================================================
90 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
91 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
93 // ----------------------------------------------------------------------------
94 // wxNotebook creation
95 // ----------------------------------------------------------------------------
97 void wxNotebook::Init()
106 m_lastFullyVisible
= 0;
113 bool wxNotebook::Create(wxWindow
*parent
,
118 const wxString
& name
)
120 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
121 wxDefaultValidator
, name
) )
124 m_sizePad
= GetRenderer()->GetTabPadding();
128 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
133 // ----------------------------------------------------------------------------
134 // wxNotebook page titles and images
135 // ----------------------------------------------------------------------------
137 wxString
wxNotebook::GetPageText(size_t nPage
) const
139 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, _T("invalid notebook page") );
141 return m_titles
[nPage
];
144 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
146 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
148 if ( strText
!= m_titles
[nPage
] )
150 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
152 if ( FixedSizeTabs() )
154 // it's enough to just reresh this one
157 else // var width tabs
159 // we need to resize the tab to fit the new string
167 int wxNotebook::GetPageImage(size_t nPage
) const
169 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, _T("invalid notebook page") );
171 return m_images
[nPage
];
174 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
176 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
178 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
179 _T("invalid image index in SetPageImage()") );
181 if ( nImage
!= m_images
[nPage
] )
183 // if the item didn't have an icon before or, on the contrary, did have
184 // it but has lost it now, its size will change - but if the icon just
186 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
187 m_images
[nPage
] = nImage
;
189 if ( tabSizeChanges
)
198 wxNotebook::~wxNotebook()
202 // ----------------------------------------------------------------------------
203 // wxNotebook page switching
204 // ----------------------------------------------------------------------------
206 int wxNotebook::SetSelection(size_t nPage
)
208 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, _T("invalid notebook page") );
210 if ( (size_t)nPage
== m_sel
)
212 // don't do anything if there is nothing to do
217 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
218 event
.SetSelection(nPage
);
219 event
.SetOldSelection(m_sel
);
220 event
.SetEventObject(this);
221 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
223 // program doesn't allow the page change
227 // we need to change m_sel first, before calling RefreshTab() below as
228 // otherwise the previously selected tab wouldn't be redrawn properly under
229 // wxGTK which calls Refresh() immediately and not during the next event
230 // loop iteration as wxMSW does and as it should
231 size_t selOld
= m_sel
;
235 if ( selOld
!= INVALID_PAGE
)
237 RefreshTab(selOld
, true /* this tab was selected */);
239 m_pages
[selOld
]->Hide();
242 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
247 m_spinbtn
->SetValue(m_sel
);
250 if ( m_sel
< m_firstVisible
)
252 // selection is to the left of visible part of tabs
255 else if ( m_sel
> m_lastFullyVisible
)
257 // selection is to the right of visible part of tabs
260 else // we already see this tab
266 m_pages
[m_sel
]->SetSize(GetPageRect());
267 m_pages
[m_sel
]->Show();
271 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
272 GetEventHandler()->ProcessEvent(event
);
277 // ----------------------------------------------------------------------------
278 // wxNotebook pages adding/deleting
279 // ----------------------------------------------------------------------------
281 bool wxNotebook::InsertPage(size_t nPage
,
282 wxNotebookPage
*pPage
,
283 const wxString
& strText
,
287 size_t nPages
= GetPageCount();
288 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), false,
289 _T("invalid notebook page in InsertPage()") );
292 m_pages
.Insert(pPage
, nPage
);
295 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
296 m_titles
.Insert(label
, nPage
);
298 m_images
.Insert(imageId
, nPage
);
300 // cache the tab geometry here
301 wxSize sizeTab
= CalcTabSize(nPage
);
303 if ( sizeTab
.y
> m_heightTab
)
304 m_heightTab
= sizeTab
.y
;
306 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
307 m_widthMax
= sizeTab
.x
;
309 m_widths
.Insert(sizeTab
.x
, nPage
);
311 // spin button may appear if we didn't have it before - but even if we did,
312 // its range should change, so update it unconditionally
315 // if the tab has just appeared, we have to relayout everything, otherwise
316 // it's enough to just redraw the tabs
319 // 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(size_t 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
376 // 2002-08-12 'if' commented out by JACS on behalf
377 // of Hans Van Leemputten <Hansvl@softhome.net> who
378 // points out that UpdateSpinBtn should always be called,
379 // to ensure m_lastVisible is up to date.
380 // if ( HasSpinBtn() )
385 size_t count
= GetPageCount();
388 if ( m_sel
== (size_t)nPage
)
390 // avoid sending event to this page which doesn't exist in the
392 m_sel
= INVALID_PAGE
;
394 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
396 else if ( m_sel
> (size_t)nPage
)
398 // no need to change selection, just adjust the index
402 else // no more tabs left
404 m_sel
= INVALID_PAGE
;
407 // have to refresh everything
413 // ----------------------------------------------------------------------------
414 // wxNotebook drawing
415 // ----------------------------------------------------------------------------
417 void wxNotebook::RefreshCurrent()
419 if ( m_sel
!= INVALID_PAGE
)
425 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
427 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
429 wxRect rect
= GetTabRect(page
);
430 if ( forceSelected
|| ((size_t)page
== m_sel
) )
432 const wxSize indent
= GetRenderer()->GetTabIndent();
433 rect
.Inflate(indent
.x
, indent
.y
);
439 void wxNotebook::RefreshAllTabs()
441 wxRect rect
= GetAllTabsRect();
442 if ( rect
.width
|| rect
.height
)
446 //else: we don't have tabs at all
449 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
454 int image
= m_images
[n
];
456 // Not needed now that wxGenericImageList is being
457 // used for wxUniversal under MSW
458 #if 0 // def __WXMSW__ // FIXME
460 m_imageList
->GetSize(n
, w
, h
);
463 dc
.SelectObject(bmp
);
464 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
465 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, true);
466 dc
.SelectObject(wxNullBitmap
);
468 bmp
= m_imageList
->GetBitmap(image
);
475 flags
|= wxCONTROL_SELECTED
;
478 flags
|= wxCONTROL_FOCUSED
;
481 GetRenderer()->DrawTab
493 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
495 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
497 wxDC
& dc
= renderer
->GetDC();
498 dc
.SetFont(GetFont());
499 dc
.SetTextForeground(GetForegroundColour());
501 // redraw the border - it's simpler to always do it instead of checking
502 // whether this needs to be done
503 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
505 // avoid overwriting the spin button
508 wxRect rectTabs
= GetAllTabsRect();
509 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
513 rectTabs
.height
-= sizeSpinBtn
.y
;
515 // Allow for erasing the line under selected tab
520 rectTabs
.width
-= sizeSpinBtn
.x
;
522 // Allow for erasing the line under selected tab
523 rectTabs
.height
+= 2;
526 dc
.SetClippingRegion(rectTabs
);
529 wxRect rect
= GetTabsPart();
530 bool isVertical
= IsVertical();
533 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
535 GetTabSize(n
, &rect
.width
, &rect
.height
);
539 // don't redraw it now as this tab has to be drawn over the other
540 // ones as it takes more place and spills over to them
543 else // not selected tab
545 // unfortunately we can't do this because the selected tab hangs
546 // over its neighbours and so we might need to refresh more tabs -
547 // of course, we could still avoid rereshing some of them with more
548 // complicated checks, but it doesn't seem too bad to refresh all
549 // of them, I still don't see flicker, so leaving as is for now
551 //if ( rectUpdate.Intersects(rect) )
553 DoDrawTab(dc
, rect
, n
);
555 //else: doesn't need to be refreshed
558 // move the rect to the next tab
560 rect
.y
+= rect
.height
;
562 rect
.x
+= rect
.width
;
565 // now redraw the selected tab
568 DoDrawTab(dc
, rectSel
, m_sel
);
571 dc
.DestroyClippingRegion();
574 // ----------------------------------------------------------------------------
575 // wxNotebook geometry
576 // ----------------------------------------------------------------------------
578 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
581 *flags
= wxNB_HITTEST_NOWHERE
;
583 // first check that it is in this window at all
584 if ( !GetClientRect().Inside(pt
) )
589 wxRect rectTabs
= GetAllTabsRect();
591 switch ( GetTabOrientation() )
594 wxFAIL_MSG(_T("unknown tab orientation"));
598 if ( pt
.y
> rectTabs
.GetBottom() )
603 if ( pt
.y
< rectTabs
.y
)
608 if ( pt
.x
> rectTabs
.GetRight() )
613 if ( pt
.x
< rectTabs
.x
)
618 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
620 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
622 if ( rectTabs
.Inside(pt
) )
626 // TODO: be more precise
627 *flags
= wxNB_HITTEST_ONITEM
;
633 // move the rectTabs to the next tab
635 rectTabs
.y
+= rectTabs
.height
;
637 rectTabs
.x
+= rectTabs
.width
;
643 bool wxNotebook::IsVertical() const
645 wxDirection dir
= GetTabOrientation();
647 return dir
== wxLEFT
|| dir
== wxRIGHT
;
650 wxDirection
wxNotebook::GetTabOrientation() const
652 long style
= GetWindowStyle();
653 if ( style
& wxBK_BOTTOM
)
655 else if ( style
& wxBK_RIGHT
)
657 else if ( style
& wxBK_LEFT
)
660 // wxBK_TOP == 0 so we don't have to test for it
664 wxRect
wxNotebook::GetTabRect(int page
) const
667 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
669 // calc the size of this tab and of the preceding ones
670 wxCoord widthThis
, widthBefore
;
671 if ( FixedSizeTabs() )
673 widthThis
= m_widthMax
;
674 widthBefore
= page
*m_widthMax
;
679 for ( int n
= 0; n
< page
; n
++ )
681 widthBefore
+= m_widths
[n
];
684 widthThis
= m_widths
[page
];
687 rect
= GetTabsPart();
690 rect
.y
+= widthBefore
- m_offset
;
691 rect
.height
= widthThis
;
695 rect
.x
+= widthBefore
- m_offset
;
696 rect
.width
= widthThis
;
702 wxRect
wxNotebook::GetAllTabsRect() const
706 if ( GetPageCount() )
708 const wxSize indent
= GetRenderer()->GetTabIndent();
709 wxSize size
= GetClientSize();
713 rect
.width
= m_heightTab
+ indent
.x
;
714 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
716 rect
.height
= size
.y
;
722 rect
.height
= m_heightTab
+ indent
.y
;
723 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
731 wxRect
wxNotebook::GetTabsPart() const
733 wxRect rect
= GetAllTabsRect();
735 wxDirection dir
= GetTabOrientation();
737 const wxSize indent
= GetRenderer()->GetTabIndent();
744 rect
.width
-= indent
.y
;
748 rect
.width
-= indent
.y
;
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