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 ( !wxControl::Create(parent
, id
, pos
, size
, style
,
125 wxDefaultValidator
, name
) )
128 m_sizePad
= GetRenderer()->GetTabPadding();
132 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
137 // ----------------------------------------------------------------------------
138 // wxNotebook page titles and images
139 // ----------------------------------------------------------------------------
141 wxString
wxNotebook::GetPageText(size_t nPage
) const
143 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, _T("invalid notebook page") );
145 return m_titles
[nPage
];
148 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
150 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
152 if ( strText
!= m_titles
[nPage
] )
154 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
156 if ( FixedSizeTabs() )
158 // it's enough to just reresh this one
161 else // var width tabs
163 // we need to resize the tab to fit the new string
171 int wxNotebook::GetPageImage(size_t nPage
) const
173 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, _T("invalid notebook page") );
175 return m_images
[nPage
];
178 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
180 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
182 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
183 _T("invalid image index in SetPageImage()") );
185 if ( nImage
!= m_images
[nPage
] )
187 // if the item didn't have an icon before or, on the contrary, did have
188 // it but has lost it now, its size will change - but if the icon just
190 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
191 m_images
[nPage
] = nImage
;
193 if ( tabSizeChanges
)
202 wxNotebook::~wxNotebook()
206 // ----------------------------------------------------------------------------
207 // wxNotebook page switching
208 // ----------------------------------------------------------------------------
210 int wxNotebook::SetSelection(size_t nPage
)
212 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, _T("invalid notebook page") );
214 if ( (size_t)nPage
== m_sel
)
216 // don't do anything if there is nothing to do
221 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
222 event
.SetSelection(nPage
);
223 event
.SetOldSelection(m_sel
);
224 event
.SetEventObject(this);
225 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
227 // program doesn't allow the page change
231 // we need to change m_sel first, before calling RefreshTab() below as
232 // otherwise the previously selected tab wouldn't be redrawn properly under
233 // wxGTK which calls Refresh() immediately and not during the next event
234 // loop iteration as wxMSW does and as it should
235 size_t selOld
= m_sel
;
239 if ( selOld
!= INVALID_PAGE
)
241 RefreshTab(selOld
, true /* this tab was selected */);
243 m_pages
[selOld
]->Hide();
246 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
251 m_spinbtn
->SetValue(m_sel
);
254 if ( m_sel
< m_firstVisible
)
256 // selection is to the left of visible part of tabs
259 else if ( m_sel
> m_lastFullyVisible
)
261 // selection is to the right of visible part of tabs
264 else // we already see this tab
270 m_pages
[m_sel
]->SetSize(GetPageRect());
271 m_pages
[m_sel
]->Show();
275 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
276 GetEventHandler()->ProcessEvent(event
);
281 // ----------------------------------------------------------------------------
282 // wxNotebook pages adding/deleting
283 // ----------------------------------------------------------------------------
285 bool wxNotebook::InsertPage(size_t nPage
,
286 wxNotebookPage
*pPage
,
287 const wxString
& strText
,
291 size_t nPages
= GetPageCount();
292 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), false,
293 _T("invalid notebook page in InsertPage()") );
296 m_pages
.Insert(pPage
, nPage
);
299 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
300 m_titles
.Insert(label
, nPage
);
302 m_images
.Insert(imageId
, nPage
);
304 // cache the tab geometry here
305 wxSize sizeTab
= CalcTabSize(nPage
);
307 if ( sizeTab
.y
> m_heightTab
)
308 m_heightTab
= sizeTab
.y
;
310 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
311 m_widthMax
= sizeTab
.x
;
313 m_widths
.Insert(sizeTab
.x
, nPage
);
315 // spin button may appear if we didn't have it before - but even if we did,
316 // its range should change, so update it unconditionally
319 // if the tab has just appeared, we have to relayout everything, otherwise
320 // it's enough to just redraw the tabs
323 // always select the first tab to have at least some selection
329 else // not the first tab
338 else // pages added to the notebook are initially hidden
346 bool wxNotebook::DeleteAllPages()
348 if ( !wxNotebookBase::DeleteAllPages() )
351 // clear the other arrays as well
357 // it is not valid any longer
358 m_sel
= INVALID_PAGE
;
360 // spin button is not needed any more
368 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
370 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
372 wxNotebookPage
*page
= m_pages
[nPage
];
373 m_pages
.RemoveAt(nPage
);
374 m_titles
.RemoveAt(nPage
);
375 m_accels
.RemoveAt(nPage
);
376 m_widths
.RemoveAt(nPage
);
377 m_images
.RemoveAt(nPage
);
379 // the spin button might not be needed any more
380 // 2002-08-12 'if' commented out by JACS on behalf
381 // of Hans Van Leemputten <Hansvl@softhome.net> who
382 // points out that UpdateSpinBtn should always be called,
383 // to ensure m_lastVisible is up to date.
384 // if ( HasSpinBtn() )
389 size_t count
= GetPageCount();
392 if ( m_sel
== (size_t)nPage
)
394 // avoid sending event to this page which doesn't exist in the
396 m_sel
= INVALID_PAGE
;
398 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
400 else if ( m_sel
> (size_t)nPage
)
402 // no need to change selection, just adjust the index
406 else // no more tabs left
408 m_sel
= INVALID_PAGE
;
411 // have to refresh everything
417 // ----------------------------------------------------------------------------
418 // wxNotebook drawing
419 // ----------------------------------------------------------------------------
421 void wxNotebook::RefreshCurrent()
423 if ( m_sel
!= INVALID_PAGE
)
429 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
431 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
433 wxRect rect
= GetTabRect(page
);
434 if ( forceSelected
|| ((size_t)page
== m_sel
) )
436 const wxSize indent
= GetRenderer()->GetTabIndent();
437 rect
.Inflate(indent
.x
, indent
.y
);
443 void wxNotebook::RefreshAllTabs()
445 wxRect rect
= GetAllTabsRect();
446 if ( rect
.width
|| rect
.height
)
450 //else: we don't have tabs at all
453 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
458 int image
= m_images
[n
];
460 // Not needed now that wxGenericImageList is being
461 // used for wxUniversal under MSW
462 #if 0 // def __WXMSW__ // FIXME
464 m_imageList
->GetSize(n
, w
, h
);
467 dc
.SelectObject(bmp
);
468 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
469 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, true);
470 dc
.SelectObject(wxNullBitmap
);
472 bmp
= m_imageList
->GetBitmap(image
);
479 flags
|= wxCONTROL_SELECTED
;
482 flags
|= wxCONTROL_FOCUSED
;
485 GetRenderer()->DrawTab
497 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
499 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
501 wxDC
& dc
= renderer
->GetDC();
502 dc
.SetFont(GetFont());
503 dc
.SetTextForeground(GetForegroundColour());
505 // redraw the border - it's simpler to always do it instead of checking
506 // whether this needs to be done
507 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
509 // avoid overwriting the spin button
512 wxRect rectTabs
= GetAllTabsRect();
513 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
517 rectTabs
.height
-= sizeSpinBtn
.y
;
519 // Allow for erasing the line under selected tab
524 rectTabs
.width
-= sizeSpinBtn
.x
;
526 // Allow for erasing the line under selected tab
527 rectTabs
.height
+= 2;
530 dc
.SetClippingRegion(rectTabs
);
533 wxRect rect
= GetTabsPart();
534 bool isVertical
= IsVertical();
537 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
539 GetTabSize(n
, &rect
.width
, &rect
.height
);
543 // don't redraw it now as this tab has to be drawn over the other
544 // ones as it takes more place and spills over to them
547 else // not selected tab
549 // unfortunately we can't do this because the selected tab hangs
550 // over its neighbours and so we might need to refresh more tabs -
551 // of course, we could still avoid rereshing some of them with more
552 // complicated checks, but it doesn't seem too bad to refresh all
553 // of them, I still don't see flicker, so leaving as is for now
555 //if ( rectUpdate.Intersects(rect) )
557 DoDrawTab(dc
, rect
, n
);
559 //else: doesn't need to be refreshed
562 // move the rect to the next tab
564 rect
.y
+= rect
.height
;
566 rect
.x
+= rect
.width
;
569 // now redraw the selected tab
572 DoDrawTab(dc
, rectSel
, m_sel
);
575 dc
.DestroyClippingRegion();
578 // ----------------------------------------------------------------------------
579 // wxNotebook geometry
580 // ----------------------------------------------------------------------------
582 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
585 *flags
= wxNB_HITTEST_NOWHERE
;
587 // first check that it is in this window at all
588 if ( !GetClientRect().Inside(pt
) )
593 wxRect rectTabs
= GetAllTabsRect();
595 switch ( GetTabOrientation() )
598 wxFAIL_MSG(_T("unknown tab orientation"));
602 if ( pt
.y
> rectTabs
.GetBottom() )
607 if ( pt
.y
< rectTabs
.y
)
612 if ( pt
.x
> rectTabs
.GetRight() )
617 if ( pt
.x
< rectTabs
.x
)
622 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
624 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
626 if ( rectTabs
.Inside(pt
) )
630 // TODO: be more precise
631 *flags
= wxNB_HITTEST_ONITEM
;
637 // move the rectTabs to the next tab
639 rectTabs
.y
+= rectTabs
.height
;
641 rectTabs
.x
+= rectTabs
.width
;
647 bool wxNotebook::IsVertical() const
649 wxDirection dir
= GetTabOrientation();
651 return dir
== wxLEFT
|| dir
== wxRIGHT
;
654 wxDirection
wxNotebook::GetTabOrientation() const
656 long style
= GetWindowStyle();
657 if ( style
& wxBK_BOTTOM
)
659 else if ( style
& wxBK_RIGHT
)
661 else if ( style
& wxBK_LEFT
)
664 // wxBK_TOP == 0 so we don't have to test for it
668 wxRect
wxNotebook::GetTabRect(int page
) const
671 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
673 // calc the size of this tab and of the preceding ones
674 wxCoord widthThis
, widthBefore
;
675 if ( FixedSizeTabs() )
677 widthThis
= m_widthMax
;
678 widthBefore
= page
*m_widthMax
;
683 for ( int n
= 0; n
< page
; n
++ )
685 widthBefore
+= m_widths
[n
];
688 widthThis
= m_widths
[page
];
691 rect
= GetTabsPart();
694 rect
.y
+= widthBefore
- m_offset
;
695 rect
.height
= widthThis
;
699 rect
.x
+= widthBefore
- m_offset
;
700 rect
.width
= widthThis
;
706 wxRect
wxNotebook::GetAllTabsRect() const
710 if ( GetPageCount() )
712 const wxSize indent
= GetRenderer()->GetTabIndent();
713 wxSize size
= GetClientSize();
717 rect
.width
= m_heightTab
+ indent
.x
;
718 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
720 rect
.height
= size
.y
;
726 rect
.height
= m_heightTab
+ indent
.y
;
727 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
735 wxRect
wxNotebook::GetTabsPart() const
737 wxRect rect
= GetAllTabsRect();
739 wxDirection dir
= GetTabOrientation();
741 const wxSize indent
= GetRenderer()->GetTabIndent();
748 rect
.width
-= indent
.y
;
752 rect
.width
-= indent
.y
;
761 rect
.height
-= indent
.y
;
765 rect
.height
-= indent
.y
;
772 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
774 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
778 // width and height have inverted meaning
784 // height is always fixed
787 // width may also be fixed and be the same for all tabs
788 *w
= GetTabWidth(page
);
791 void wxNotebook::SetTabSize(const wxSize
& sz
)
793 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
807 wxSize
wxNotebook::CalcTabSize(int page
) const
809 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
810 // method is called to calculate them
814 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
816 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
818 if ( HasImage(page
) )
821 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
823 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
825 if ( sizeImage
.y
> size
.y
)
826 size
.y
= sizeImage
.y
;
829 size
.x
+= 2*m_sizePad
.x
;
830 size
.y
+= 2*m_sizePad
.y
;
835 void wxNotebook::ResizeTab(int page
)
837 wxSize sizeTab
= CalcTabSize(page
);
839 // we only need full relayout if the page size changes
840 bool needsRelayout
= false;
845 wxCoord tmp
= sizeTab
.x
;
846 sizeTab
.x
= sizeTab
.y
;
850 if ( sizeTab
.y
> m_heightTab
)
852 needsRelayout
= true;
854 m_heightTab
= sizeTab
.y
;
857 m_widths
[page
] = sizeTab
.x
;
859 if ( sizeTab
.x
> m_widthMax
)
860 m_widthMax
= sizeTab
.x
;
862 // the total of the tabs has changed too
871 void wxNotebook::SetPadding(const wxSize
& padding
)
873 if ( padding
!= m_sizePad
)
881 void wxNotebook::Relayout()
883 if ( GetPageCount() )
889 if ( m_sel
!= INVALID_PAGE
)
891 // resize the currently shown page
892 wxRect rectPage
= GetPageRect();
894 m_pages
[m_sel
]->SetSize(rectPage
);
896 // also scroll it into view if needed (note that m_lastVisible
897 // was updated by the call to UpdateSpinBtn() above, this is why it
901 if ( m_sel
< m_firstVisible
)
903 // selection is to the left of visible part of tabs
906 else if ( m_sel
> m_lastFullyVisible
)
908 // selection is to the right of visible part of tabs
914 else // we have no pages
916 // just refresh everything
921 wxRect
wxNotebook::GetPagePart() const
923 wxRect rectPage
= GetClientRect();
925 if ( GetPageCount() )
927 wxRect rectTabs
= GetAllTabsRect();
928 wxDirection dir
= GetTabOrientation();
931 rectPage
.width
-= rectTabs
.width
;
933 rectPage
.x
+= rectTabs
.width
;
937 rectPage
.height
-= rectTabs
.height
;
939 rectPage
.y
+= rectTabs
.height
;
942 //else: no pages at all
947 wxRect
wxNotebook::GetPageRect() const
949 wxRect rect
= GetPagePart();
951 // leave space for the border
952 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
954 // FIXME: hardcoded +2!
955 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
956 -(rectBorder
.y
+ rectBorder
.height
+ 2));
961 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
963 wxSize sizeNb
= size
;
964 wxRect rect
= GetAllTabsRect();
966 sizeNb
.x
+= rect
.width
;
968 sizeNb
.y
+= rect
.height
;
973 void wxNotebook::SetPageSize(const wxSize
& size
)
975 SetClientSize(GetSizeForPage(size
));
978 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
980 return AdjustSize(GetSizeForPage(sizePage
));
983 // ----------------------------------------------------------------------------
984 // wxNotebook spin button
985 // ----------------------------------------------------------------------------
987 bool wxNotebook::HasSpinBtn() const
989 return m_spinbtn
&& m_spinbtn
->IsShown();
992 void wxNotebook::CalcLastVisibleTab()
994 bool isVertical
= IsVertical();
996 wxCoord width
= GetClientSize().x
;
998 wxRect rect
= GetTabsPart();
1000 size_t count
= GetPageCount();
1002 wxCoord widthLast
= 0;
1004 for ( n
= m_firstVisible
; n
< count
; n
++ )
1006 GetTabSize(n
, &rect
.width
, &rect
.height
);
1007 if ( rect
.GetRight() > width
)
1012 // remember it to use below
1013 widthLast
= rect
.GetRight();
1015 // move the rect to the next tab
1017 rect
.y
+= rect
.height
;
1019 rect
.x
+= rect
.width
;
1022 if ( n
== m_firstVisible
)
1024 // even the first tab isn't fully visible - but still pretend it is as
1025 // we have to show something
1026 m_lastFullyVisible
= m_firstVisible
;
1028 else // more than one tab visible
1030 m_lastFullyVisible
= n
- 1;
1032 // but is it really fully visible? it shouldn't overlap with the spin
1033 // button if it is present (again, even if the first button does
1034 // overlap with it, we pretend that it doesn't as there is not much
1036 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1038 // adjust width to be the width before the spin button
1039 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1041 width
-= sizeSpinBtn
.y
;
1043 width
-= sizeSpinBtn
.x
;
1045 if ( widthLast
> width
)
1047 // the last button overlaps with spin button, so take he
1049 m_lastFullyVisible
--;
1056 // everything is visible
1061 // this tab is still (partially) visible, so m_lastVisible is the
1062 // next tab (remember, this is "exclusive" last)
1063 m_lastVisible
= n
+ 1;
1068 void wxNotebook::UpdateSpinBtn()
1070 // first decide if we need a spin button
1073 size_t count
= (size_t)GetPageCount();
1076 // this case is special, get rid of it immediately: everything is
1077 // visible and we don't need any spin buttons
1078 allTabsShown
= true;
1080 // have to reset them manually as we don't call CalcLastVisibleTab()
1083 m_lastFullyVisible
= 0;
1087 CalcLastVisibleTab();
1089 // if all tabs after the first visible one are shown, it doesn't yet
1090 // mean that all tabs are shown - so we go backwards until we arrive to
1091 // the beginning (then all tabs are indeed shown) or find a tab such
1092 // that not all tabs after it are shown
1093 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1095 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1097 m_offset
-= GetTabWidth(m_firstVisible
--);
1099 // reclaculate after m_firstVisible change
1100 CalcLastVisibleTab();
1103 allTabsShown
= m_lastFullyVisible
== count
- 1;
1106 if ( !allTabsShown
)
1110 // create it once only
1111 m_spinbtn
= new wxNotebookSpinBtn(this);
1113 // set the correct value to keep it in sync
1114 m_spinbtn
->SetValue(m_sel
);
1117 // position it correctly
1123 // also set/update the range
1124 m_spinbtn
->SetRange(0, count
- 1);
1126 // update m_lastFullyVisible once again as it might have changed
1127 // because the spin button appeared
1129 // FIXME: might do it more efficiently
1130 CalcLastVisibleTab();
1132 else // all tabs are visible, we don't need spin button
1134 if ( m_spinbtn
&& m_spinbtn
-> IsShown() )
1141 void wxNotebook::PositionSpinBtn()
1147 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1149 wxRect rectTabs
= GetAllTabsRect();
1152 switch ( GetTabOrientation() )
1155 wxFAIL_MSG(_T("unknown tab orientation"));
1159 x
= rectTabs
.GetRight() - wBtn
;
1160 y
= rectTabs
.GetBottom() - hBtn
;
1164 x
= rectTabs
.GetRight() - wBtn
;
1165 y
= rectTabs
.GetTop();
1169 x
= rectTabs
.GetRight() - wBtn
;
1170 y
= rectTabs
.GetBottom() - hBtn
;
1174 x
= rectTabs
.GetLeft();
1175 y
= rectTabs
.GetBottom() - hBtn
;
1179 m_spinbtn
->Move(x
, y
);
1182 // ----------------------------------------------------------------------------
1183 // wxNotebook scrolling
1184 // ----------------------------------------------------------------------------
1186 void wxNotebook::ScrollTo(int page
)
1188 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1190 // set the first visible tab and offset (easy)
1191 m_firstVisible
= (size_t)page
;
1193 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1195 m_offset
+= GetTabWidth(n
);
1198 // find the last visible tab too
1199 CalcLastVisibleTab();
1204 void wxNotebook::ScrollLastTo(int page
)
1206 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1208 // go backwards until we find the first tab which can be made visible
1209 // without hiding the given one
1210 wxSize size
= GetClientSize();
1211 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1212 widthTabs
= GetTabWidth(page
);
1214 // the total width is less than the width of the window if we have the spin
1218 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1220 widthAll
-= sizeSpinBtn
.y
;
1222 widthAll
-= sizeSpinBtn
.x
;
1225 m_firstVisible
= page
;
1226 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1228 widthTabs
+= GetTabWidth(--m_firstVisible
);
1231 if ( widthTabs
> widthAll
)
1233 // take one step back (that it is forward) if we can
1234 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1239 ScrollTo(m_firstVisible
);
1241 // consitency check: the page we were asked to show should be shown
1242 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1245 // ----------------------------------------------------------------------------
1246 // wxNotebook sizing/moving
1247 // ----------------------------------------------------------------------------
1249 wxSize
wxNotebook::DoGetBestClientSize() const
1251 // calculate the max page size
1254 size_t count
= GetPageCount();
1257 for ( size_t n
= 0; n
< count
; n
++ )
1259 wxSize sizePage
= m_pages
[n
]->GetSize();
1261 if ( size
.x
< sizePage
.x
)
1262 size
.x
= sizePage
.x
;
1263 if ( size
.y
< sizePage
.y
)
1264 size
.y
= sizePage
.y
;
1269 // use some arbitrary default size
1274 return GetSizeForPage(size
);
1277 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1279 wxControl::DoMoveWindow(x
, y
, width
, height
);
1281 // move the spin ctrl too (NOP if it doesn't exist)
1285 void wxNotebook::DoSetSize(int x
, int y
,
1286 int width
, int height
,
1289 wxSize old_client_size
= GetClientSize();
1291 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1293 wxSize new_client_size
= GetClientSize();
1295 if (old_client_size
!= new_client_size
)
1299 // ----------------------------------------------------------------------------
1300 // wxNotebook input processing
1301 // ----------------------------------------------------------------------------
1303 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1305 const wxString
& strArg
)
1307 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1308 SetSelection(GetNextPage(true));
1309 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1310 SetSelection(GetNextPage(false));
1311 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1312 SetSelection((int)numArg
);
1314 return wxControl::PerformAction(action
, numArg
, strArg
);
1319 // ----------------------------------------------------------------------------
1320 // wxStdNotebookInputHandler
1321 // ----------------------------------------------------------------------------
1323 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1324 : wxStdInputHandler(inphand
)
1328 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1329 const wxKeyEvent
& event
,
1332 // ignore the key releases
1335 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1338 wxControlAction action
;
1339 switch ( event
.GetKeyCode() )
1342 if ( notebook
->IsVertical() )
1343 action
= wxACTION_NOTEBOOK_PREV
;
1347 if ( !notebook
->IsVertical() )
1348 action
= wxACTION_NOTEBOOK_PREV
;
1352 if ( notebook
->IsVertical() )
1353 action
= wxACTION_NOTEBOOK_NEXT
;
1357 if ( !notebook
->IsVertical() )
1358 action
= wxACTION_NOTEBOOK_NEXT
;
1362 action
= wxACTION_NOTEBOOK_GOTO
;
1363 // page = 0; -- already has this value
1367 action
= wxACTION_NOTEBOOK_GOTO
;
1368 page
= notebook
->GetPageCount() - 1;
1372 if ( !action
.IsEmpty() )
1374 return consumer
->PerformAction(action
, page
);
1378 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1381 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1382 const wxMouseEvent
& event
)
1384 if ( event
.ButtonDown(1) )
1386 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1387 int page
= notebook
->HitTest(event
.GetPosition());
1390 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1396 return wxStdInputHandler::HandleMouse(consumer
, event
);
1399 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1400 const wxMouseEvent
& event
)
1402 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1406 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1407 const wxFocusEvent
& WXUNUSED(event
))
1409 HandleFocusChange(consumer
);
1414 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1415 bool WXUNUSED(activated
))
1417 // we react to the focus change in the same way as to the [de]activation
1418 HandleFocusChange(consumer
);
1423 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1425 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1426 notebook
->RefreshCurrent();
1429 #endif // wxUSE_NOTEBOOK