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 // ----------------------------------------------------------------------------
40 // wxStdNotebookInputHandler: translates SPACE and ENTER keys and the left mouse
41 // click into button press/release actions
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxStdNotebookInputHandler
: public wxStdInputHandler
47 wxStdNotebookInputHandler(wxInputHandler
*inphand
);
49 virtual bool HandleKey(wxInputConsumer
*consumer
,
50 const wxKeyEvent
& event
,
52 virtual bool HandleMouse(wxInputConsumer
*consumer
,
53 const wxMouseEvent
& event
);
54 virtual bool HandleMouseMove(wxInputConsumer
*consumer
, const wxMouseEvent
& event
);
55 virtual bool HandleFocus(wxInputConsumer
*consumer
, const wxFocusEvent
& event
);
56 virtual bool HandleActivation(wxInputConsumer
*consumer
, bool activated
);
59 void HandleFocusChange(wxInputConsumer
*consumer
);
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
67 // due to unsigned type nPage is always >= 0
68 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
70 #define IS_VALID_PAGE(nPage) (((size_t)nPage) < GetPageCount())
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 static const size_t INVALID_PAGE
= (size_t)-1;
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 class wxNotebookSpinBtn
: public wxSpinButton
86 wxNotebookSpinBtn(wxNotebook
*nb
)
87 : wxSpinButton(nb
, wxID_ANY
,
88 wxDefaultPosition
, wxDefaultSize
,
89 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
95 void OnSpin(wxSpinEvent
& event
)
97 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
103 DECLARE_EVENT_TABLE()
106 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
107 EVT_SPIN(wxID_ANY
, wxNotebookSpinBtn::OnSpin
)
110 // ============================================================================
112 // ============================================================================
114 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
116 // ----------------------------------------------------------------------------
117 // wxNotebook creation
118 // ----------------------------------------------------------------------------
120 void wxNotebook::Init()
122 m_sel
= INVALID_PAGE
;
129 m_lastFullyVisible
= 0;
136 bool wxNotebook::Create(wxWindow
*parent
,
141 const wxString
& name
)
143 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
146 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
147 wxDefaultValidator
, name
) )
150 m_sizePad
= GetRenderer()->GetTabPadding();
152 SetInitialSize(size
);
154 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
159 // ----------------------------------------------------------------------------
160 // wxNotebook page titles and images
161 // ----------------------------------------------------------------------------
163 wxString
wxNotebook::GetPageText(size_t nPage
) const
165 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("invalid notebook page") );
167 return m_titles
[nPage
];
170 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
172 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("invalid notebook page") );
174 if ( strText
!= m_titles
[nPage
] )
176 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
178 if ( FixedSizeTabs() )
180 // it's enough to just reresh this one
183 else // var width tabs
185 // we need to resize the tab to fit the new string
193 int wxNotebook::GetPageImage(size_t nPage
) const
195 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("invalid notebook page") );
197 return m_images
[nPage
];
200 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
202 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("invalid notebook page") );
204 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
205 wxT("invalid image index in SetPageImage()") );
207 if ( nImage
!= m_images
[nPage
] )
209 // if the item didn't have an icon before or, on the contrary, did have
210 // it but has lost it now, its size will change - but if the icon just
212 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
213 m_images
[nPage
] = nImage
;
215 if ( tabSizeChanges
)
224 wxNotebook::~wxNotebook()
228 // ----------------------------------------------------------------------------
229 // wxNotebook page switching
230 // ----------------------------------------------------------------------------
232 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
234 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("invalid notebook page") );
236 if ( (size_t)nPage
== m_sel
)
238 // don't do anything if there is nothing to do
242 if ( flags
& SetSelection_SendEvent
)
244 if ( !SendPageChangingEvent(nPage
) )
246 // program doesn't allow the page change
251 // we need to change m_sel first, before calling RefreshTab() below as
252 // otherwise the previously selected tab wouldn't be redrawn properly under
253 // wxGTK which calls Refresh() immediately and not during the next event
254 // loop iteration as wxMSW does and as it should
255 size_t selOld
= m_sel
;
259 if ( selOld
!= INVALID_PAGE
)
261 RefreshTab(selOld
, true /* this tab was selected */);
263 m_pages
[selOld
]->Hide();
266 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
271 m_spinbtn
->SetValue(m_sel
);
274 if ( m_sel
< m_firstVisible
)
276 // selection is to the left of visible part of tabs
279 else if ( m_sel
> m_lastFullyVisible
)
281 // selection is to the right of visible part of tabs
284 else // we already see this tab
290 m_pages
[m_sel
]->SetSize(GetPageRect());
291 m_pages
[m_sel
]->Show();
294 if ( flags
& SetSelection_SendEvent
)
297 SendPageChangedEvent(selOld
);
303 // ----------------------------------------------------------------------------
304 // wxNotebook pages adding/deleting
305 // ----------------------------------------------------------------------------
307 bool wxNotebook::InsertPage(size_t nPage
,
308 wxNotebookPage
*pPage
,
309 const wxString
& strText
,
313 size_t nPages
= GetPageCount();
314 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), false,
315 wxT("invalid notebook page in InsertPage()") );
318 m_pages
.Insert(pPage
, nPage
);
321 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
322 m_titles
.Insert(label
, nPage
);
324 m_images
.Insert(imageId
, nPage
);
326 // cache the tab geometry here
327 wxSize sizeTab
= CalcTabSize(nPage
);
329 if ( sizeTab
.y
> m_heightTab
)
330 m_heightTab
= sizeTab
.y
;
332 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
333 m_widthMax
= sizeTab
.x
;
335 m_widths
.Insert(sizeTab
.x
, nPage
);
337 // spin button may appear if we didn't have it before - but even if we did,
338 // its range should change, so update it unconditionally
341 // if the tab has just appeared, we have to relayout everything, otherwise
342 // it's enough to just redraw the tabs
345 // always select the first tab to have at least some selection
351 else // not the first tab
360 else // pages added to the notebook are initially hidden
368 bool wxNotebook::DeleteAllPages()
370 if ( !wxNotebookBase::DeleteAllPages() )
373 // clear the other arrays as well
379 // it is not valid any longer
380 m_sel
= INVALID_PAGE
;
382 // spin button is not needed any more
390 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
392 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, wxT("invalid notebook page") );
394 wxNotebookPage
*page
= m_pages
[nPage
];
395 m_pages
.RemoveAt(nPage
);
396 m_titles
.RemoveAt(nPage
);
397 m_accels
.RemoveAt(nPage
);
398 m_widths
.RemoveAt(nPage
);
399 m_images
.RemoveAt(nPage
);
401 // the spin button might not be needed any more
402 // 2002-08-12 'if' commented out by JACS on behalf
403 // of Hans Van Leemputten <Hansvl@softhome.net> who
404 // points out that UpdateSpinBtn should always be called,
405 // to ensure m_lastVisible is up to date.
406 // if ( HasSpinBtn() )
411 size_t count
= GetPageCount();
414 if ( m_sel
== (size_t)nPage
)
416 // avoid sending event to this page which doesn't exist in the
418 m_sel
= INVALID_PAGE
;
420 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
422 else if ( m_sel
> (size_t)nPage
)
424 // no need to change selection, just adjust the index
428 else // no more tabs left
430 m_sel
= INVALID_PAGE
;
433 // have to refresh everything
439 // ----------------------------------------------------------------------------
440 // wxNotebook drawing
441 // ----------------------------------------------------------------------------
443 void wxNotebook::RefreshCurrent()
445 if ( m_sel
!= INVALID_PAGE
)
451 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
453 wxCHECK_RET( IS_VALID_PAGE(page
), wxT("invalid notebook page") );
455 wxRect rect
= GetTabRect(page
);
456 if ( forceSelected
|| ((size_t)page
== m_sel
) )
458 const wxSize indent
= GetRenderer()->GetTabIndent();
459 rect
.Inflate(indent
.x
, indent
.y
);
465 void wxNotebook::RefreshAllTabs()
467 wxRect rect
= GetAllTabsRect();
468 if ( rect
.width
|| rect
.height
)
472 //else: we don't have tabs at all
475 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
480 int image
= m_images
[n
];
482 // Not needed now that wxGenericImageList is being
483 // used for wxUniversal under MSW
484 #if 0 // def __WXMSW__ // FIXME
486 m_imageList
->GetSize(n
, w
, h
);
489 dc
.SelectObject(bmp
);
490 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
491 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, true);
492 dc
.SelectObject(wxNullBitmap
);
494 bmp
= m_imageList
->GetBitmap(image
);
501 flags
|= wxCONTROL_SELECTED
;
504 flags
|= wxCONTROL_FOCUSED
;
507 GetRenderer()->DrawTab
519 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
521 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
523 wxDC
& dc
= renderer
->GetDC();
524 dc
.SetFont(GetFont());
525 dc
.SetTextForeground(GetForegroundColour());
527 // redraw the border - it's simpler to always do it instead of checking
528 // whether this needs to be done
529 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
531 // avoid overwriting the spin button
534 wxRect rectTabs
= GetAllTabsRect();
535 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
539 rectTabs
.height
-= sizeSpinBtn
.y
;
541 // Allow for erasing the line under selected tab
546 rectTabs
.width
-= sizeSpinBtn
.x
;
548 // Allow for erasing the line under selected tab
549 rectTabs
.height
+= 2;
552 dc
.SetClippingRegion(rectTabs
);
555 wxRect rect
= GetTabsPart();
556 bool isVertical
= IsVertical();
559 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
561 GetTabSize(n
, &rect
.width
, &rect
.height
);
565 // don't redraw it now as this tab has to be drawn over the other
566 // ones as it takes more place and spills over to them
569 else // not selected tab
571 // unfortunately we can't do this because the selected tab hangs
572 // over its neighbours and so we might need to refresh more tabs -
573 // of course, we could still avoid rereshing some of them with more
574 // complicated checks, but it doesn't seem too bad to refresh all
575 // of them, I still don't see flicker, so leaving as is for now
577 //if ( rectUpdate.Intersects(rect) )
579 DoDrawTab(dc
, rect
, n
);
581 //else: doesn't need to be refreshed
584 // move the rect to the next tab
586 rect
.y
+= rect
.height
;
588 rect
.x
+= rect
.width
;
591 // now redraw the selected tab
594 DoDrawTab(dc
, rectSel
, m_sel
);
597 dc
.DestroyClippingRegion();
600 // ----------------------------------------------------------------------------
601 // wxNotebook geometry
602 // ----------------------------------------------------------------------------
604 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
607 *flags
= wxBK_HITTEST_NOWHERE
;
609 // first check that it is in this window at all
610 if ( !GetClientRect().Contains(pt
) )
615 wxRect rectTabs
= GetAllTabsRect();
617 switch ( GetTabOrientation() )
620 wxFAIL_MSG(wxT("unknown tab orientation"));
624 if ( pt
.y
> rectTabs
.GetBottom() )
629 if ( pt
.y
< rectTabs
.y
)
634 if ( pt
.x
> rectTabs
.GetRight() )
639 if ( pt
.x
< rectTabs
.x
)
644 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
646 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
648 if ( rectTabs
.Contains(pt
) )
652 // TODO: be more precise
653 *flags
= wxBK_HITTEST_ONITEM
;
659 // move the rectTabs to the next tab
661 rectTabs
.y
+= rectTabs
.height
;
663 rectTabs
.x
+= rectTabs
.width
;
669 bool wxNotebook::IsVertical() const
671 wxDirection dir
= GetTabOrientation();
673 return dir
== wxLEFT
|| dir
== wxRIGHT
;
676 wxDirection
wxNotebook::GetTabOrientation() const
678 long style
= GetWindowStyle();
679 if ( style
& wxBK_BOTTOM
)
681 else if ( style
& wxBK_RIGHT
)
683 else if ( style
& wxBK_LEFT
)
686 // wxBK_TOP == 0 so we don't have to test for it
690 wxRect
wxNotebook::GetTabRect(int page
) const
693 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, wxT("invalid notebook page") );
695 // calc the size of this tab and of the preceding ones
696 wxCoord widthThis
, widthBefore
;
697 if ( FixedSizeTabs() )
699 widthThis
= m_widthMax
;
700 widthBefore
= page
*m_widthMax
;
705 for ( int n
= 0; n
< page
; n
++ )
707 widthBefore
+= m_widths
[n
];
710 widthThis
= m_widths
[page
];
713 rect
= GetTabsPart();
716 rect
.y
+= widthBefore
- m_offset
;
717 rect
.height
= widthThis
;
721 rect
.x
+= widthBefore
- m_offset
;
722 rect
.width
= widthThis
;
728 wxRect
wxNotebook::GetAllTabsRect() const
732 if ( GetPageCount() )
734 const wxSize indent
= GetRenderer()->GetTabIndent();
735 wxSize size
= GetClientSize();
739 rect
.width
= m_heightTab
+ indent
.x
;
740 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
742 rect
.height
= size
.y
;
748 rect
.height
= m_heightTab
+ indent
.y
;
749 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
757 wxRect
wxNotebook::GetTabsPart() const
759 wxRect rect
= GetAllTabsRect();
761 wxDirection dir
= GetTabOrientation();
763 const wxSize indent
= GetRenderer()->GetTabIndent();
770 rect
.width
-= indent
.y
;
774 rect
.width
-= indent
.y
;
783 rect
.height
-= indent
.y
;
787 rect
.height
-= indent
.y
;
794 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
796 wxCHECK_RET( w
&& h
, wxT("NULL pointer in GetTabSize") );
800 // width and height have inverted meaning
806 // height is always fixed
809 // width may also be fixed and be the same for all tabs
810 *w
= GetTabWidth(page
);
813 void wxNotebook::SetTabSize(const wxSize
& sz
)
815 wxCHECK_RET( FixedSizeTabs(), wxT("SetTabSize() ignored") );
829 wxSize
wxNotebook::CalcTabSize(int page
) const
831 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
832 // method is called to calculate them
836 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, wxT("invalid notebook page") );
838 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
840 if ( HasImage(page
) )
843 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
845 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
847 if ( sizeImage
.y
> size
.y
)
848 size
.y
= sizeImage
.y
;
851 size
.x
+= 2*m_sizePad
.x
;
852 size
.y
+= 2*m_sizePad
.y
;
857 void wxNotebook::ResizeTab(int page
)
859 wxSize sizeTab
= CalcTabSize(page
);
861 // we only need full relayout if the page size changes
862 bool needsRelayout
= false;
867 wxCoord tmp
= sizeTab
.x
;
868 sizeTab
.x
= sizeTab
.y
;
872 if ( sizeTab
.y
> m_heightTab
)
874 needsRelayout
= true;
876 m_heightTab
= sizeTab
.y
;
879 m_widths
[page
] = sizeTab
.x
;
881 if ( sizeTab
.x
> m_widthMax
)
882 m_widthMax
= sizeTab
.x
;
884 // the total of the tabs has changed too
893 void wxNotebook::SetPadding(const wxSize
& padding
)
895 if ( padding
!= m_sizePad
)
903 void wxNotebook::Relayout()
905 if ( GetPageCount() )
911 if ( m_sel
!= INVALID_PAGE
)
913 // resize the currently shown page
914 wxRect rectPage
= GetPageRect();
916 m_pages
[m_sel
]->SetSize(rectPage
);
918 // also scroll it into view if needed (note that m_lastVisible
919 // was updated by the call to UpdateSpinBtn() above, this is why it
923 if ( m_sel
< m_firstVisible
)
925 // selection is to the left of visible part of tabs
928 else if ( m_sel
> m_lastFullyVisible
)
930 // selection is to the right of visible part of tabs
936 else // we have no pages
938 // just refresh everything
943 wxRect
wxNotebook::GetPagePart() const
945 wxRect rectPage
= GetClientRect();
947 if ( GetPageCount() )
949 wxRect rectTabs
= GetAllTabsRect();
950 wxDirection dir
= GetTabOrientation();
953 rectPage
.width
-= rectTabs
.width
;
955 rectPage
.x
+= rectTabs
.width
;
959 rectPage
.height
-= rectTabs
.height
;
961 rectPage
.y
+= rectTabs
.height
;
964 //else: no pages at all
969 wxRect
wxNotebook::GetPageRect() const
971 wxRect rect
= GetPagePart();
973 // leave space for the border
974 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
976 // FIXME: hardcoded +2!
977 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
978 -(rectBorder
.y
+ rectBorder
.height
+ 2));
983 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
985 wxSize sizeNb
= size
;
986 wxRect rect
= GetAllTabsRect();
988 sizeNb
.x
+= rect
.width
;
990 sizeNb
.y
+= rect
.height
;
995 void wxNotebook::SetPageSize(const wxSize
& size
)
997 SetClientSize(GetSizeForPage(size
));
1000 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
1002 return AdjustSize(GetSizeForPage(sizePage
));
1005 // ----------------------------------------------------------------------------
1006 // wxNotebook spin button
1007 // ----------------------------------------------------------------------------
1009 bool wxNotebook::HasSpinBtn() const
1011 return m_spinbtn
&& m_spinbtn
->IsShown();
1014 void wxNotebook::CalcLastVisibleTab()
1016 bool isVertical
= IsVertical();
1018 wxCoord width
= GetClientSize().x
;
1020 wxRect rect
= GetTabsPart();
1022 size_t count
= GetPageCount();
1024 wxCoord widthLast
= 0;
1026 for ( n
= m_firstVisible
; n
< count
; n
++ )
1028 GetTabSize(n
, &rect
.width
, &rect
.height
);
1029 if ( rect
.GetRight() > width
)
1034 // remember it to use below
1035 widthLast
= rect
.GetRight();
1037 // move the rect to the next tab
1039 rect
.y
+= rect
.height
;
1041 rect
.x
+= rect
.width
;
1044 if ( n
== m_firstVisible
)
1046 // even the first tab isn't fully visible - but still pretend it is as
1047 // we have to show something
1048 m_lastFullyVisible
= m_firstVisible
;
1050 else // more than one tab visible
1052 m_lastFullyVisible
= n
- 1;
1054 // but is it really fully visible? it shouldn't overlap with the spin
1055 // button if it is present (again, even if the first button does
1056 // overlap with it, we pretend that it doesn't as there is not much
1058 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1060 // adjust width to be the width before the spin button
1061 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1063 width
-= sizeSpinBtn
.y
;
1065 width
-= sizeSpinBtn
.x
;
1067 if ( widthLast
> width
)
1069 // the last button overlaps with spin button, so take he
1071 m_lastFullyVisible
--;
1078 // everything is visible
1083 // this tab is still (partially) visible, so m_lastVisible is the
1084 // next tab (remember, this is "exclusive" last)
1085 m_lastVisible
= n
+ 1;
1090 void wxNotebook::UpdateSpinBtn()
1092 // first decide if we need a spin button
1095 size_t count
= (size_t)GetPageCount();
1098 // this case is special, get rid of it immediately: everything is
1099 // visible and we don't need any spin buttons
1100 allTabsShown
= true;
1102 // have to reset them manually as we don't call CalcLastVisibleTab()
1105 m_lastFullyVisible
= 0;
1109 CalcLastVisibleTab();
1111 // if all tabs after the first visible one are shown, it doesn't yet
1112 // mean that all tabs are shown - so we go backwards until we arrive to
1113 // the beginning (then all tabs are indeed shown) or find a tab such
1114 // that not all tabs after it are shown
1115 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1117 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1119 m_offset
-= GetTabWidth(m_firstVisible
--);
1121 // reclaculate after m_firstVisible change
1122 CalcLastVisibleTab();
1125 allTabsShown
= m_lastFullyVisible
== count
- 1;
1128 if ( !allTabsShown
)
1132 // create it once only
1133 m_spinbtn
= new wxNotebookSpinBtn(this);
1135 // set the correct value to keep it in sync
1136 m_spinbtn
->SetValue(m_sel
);
1139 // position it correctly
1145 // also set/update the range
1146 m_spinbtn
->SetRange(0, count
- 1);
1148 // update m_lastFullyVisible once again as it might have changed
1149 // because the spin button appeared
1151 // FIXME: might do it more efficiently
1152 CalcLastVisibleTab();
1154 else // all tabs are visible, we don't need spin button
1156 if ( m_spinbtn
&& m_spinbtn
-> IsShown() )
1163 void wxNotebook::PositionSpinBtn()
1169 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1171 wxRect rectTabs
= GetAllTabsRect();
1174 switch ( GetTabOrientation() )
1177 wxFAIL_MSG(wxT("unknown tab orientation"));
1181 x
= rectTabs
.GetRight() - wBtn
;
1182 y
= rectTabs
.GetBottom() - hBtn
;
1186 x
= rectTabs
.GetRight() - wBtn
;
1187 y
= rectTabs
.GetTop();
1191 x
= rectTabs
.GetRight() - wBtn
;
1192 y
= rectTabs
.GetBottom() - hBtn
;
1196 x
= rectTabs
.GetLeft();
1197 y
= rectTabs
.GetBottom() - hBtn
;
1201 m_spinbtn
->Move(x
, y
);
1204 // ----------------------------------------------------------------------------
1205 // wxNotebook scrolling
1206 // ----------------------------------------------------------------------------
1208 void wxNotebook::ScrollTo(int page
)
1210 wxCHECK_RET( IS_VALID_PAGE(page
), wxT("invalid notebook page") );
1212 // set the first visible tab and offset (easy)
1213 m_firstVisible
= (size_t)page
;
1215 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1217 m_offset
+= GetTabWidth(n
);
1220 // find the last visible tab too
1221 CalcLastVisibleTab();
1226 void wxNotebook::ScrollLastTo(int page
)
1228 wxCHECK_RET( IS_VALID_PAGE(page
), wxT("invalid notebook page") );
1230 // go backwards until we find the first tab which can be made visible
1231 // without hiding the given one
1232 wxSize size
= GetClientSize();
1233 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1234 widthTabs
= GetTabWidth(page
);
1236 // the total width is less than the width of the window if we have the spin
1240 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1242 widthAll
-= sizeSpinBtn
.y
;
1244 widthAll
-= sizeSpinBtn
.x
;
1247 m_firstVisible
= page
;
1248 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1250 widthTabs
+= GetTabWidth(--m_firstVisible
);
1253 if ( widthTabs
> widthAll
)
1255 // take one step back (that it is forward) if we can
1256 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1261 ScrollTo(m_firstVisible
);
1263 // consitency check: the page we were asked to show should be shown
1264 wxASSERT_MSG( (size_t)page
< m_lastVisible
, wxT("bug in ScrollLastTo") );
1267 // ----------------------------------------------------------------------------
1268 // wxNotebook sizing/moving
1269 // ----------------------------------------------------------------------------
1271 wxSize
wxNotebook::DoGetBestClientSize() const
1273 // calculate the max page size
1276 size_t count
= GetPageCount();
1279 for ( size_t n
= 0; n
< count
; n
++ )
1281 wxSize sizePage
= m_pages
[n
]->GetSize();
1283 if ( size
.x
< sizePage
.x
)
1284 size
.x
= sizePage
.x
;
1285 if ( size
.y
< sizePage
.y
)
1286 size
.y
= sizePage
.y
;
1291 // use some arbitrary default size
1296 return GetSizeForPage(size
);
1299 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1301 wxControl::DoMoveWindow(x
, y
, width
, height
);
1303 // move the spin ctrl too (NOP if it doesn't exist)
1307 void wxNotebook::DoSetSize(int x
, int y
,
1308 int width
, int height
,
1311 wxSize old_client_size
= GetClientSize();
1313 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1315 wxSize new_client_size
= GetClientSize();
1317 if (old_client_size
!= new_client_size
)
1321 // ----------------------------------------------------------------------------
1322 // wxNotebook input processing
1323 // ----------------------------------------------------------------------------
1325 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1327 const wxString
& strArg
)
1329 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1330 SetSelection(GetNextPage(true));
1331 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1332 SetSelection(GetNextPage(false));
1333 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1334 SetSelection((int)numArg
);
1336 return wxControl::PerformAction(action
, numArg
, strArg
);
1342 wxInputHandler
*wxNotebook::GetStdInputHandler(wxInputHandler
*handlerDef
)
1344 static wxStdNotebookInputHandler
s_handler(handlerDef
);
1349 // ----------------------------------------------------------------------------
1350 // wxStdNotebookInputHandler
1351 // ----------------------------------------------------------------------------
1353 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1354 : wxStdInputHandler(inphand
)
1358 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1359 const wxKeyEvent
& event
,
1362 // ignore the key releases
1365 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1368 wxControlAction action
;
1369 switch ( event
.GetKeyCode() )
1372 if ( notebook
->IsVertical() )
1373 action
= wxACTION_NOTEBOOK_PREV
;
1377 if ( !notebook
->IsVertical() )
1378 action
= wxACTION_NOTEBOOK_PREV
;
1382 if ( notebook
->IsVertical() )
1383 action
= wxACTION_NOTEBOOK_NEXT
;
1387 if ( !notebook
->IsVertical() )
1388 action
= wxACTION_NOTEBOOK_NEXT
;
1392 action
= wxACTION_NOTEBOOK_GOTO
;
1393 // page = 0; -- already has this value
1397 action
= wxACTION_NOTEBOOK_GOTO
;
1398 page
= notebook
->GetPageCount() - 1;
1402 if ( !action
.IsEmpty() )
1404 return consumer
->PerformAction(action
, page
);
1408 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1411 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1412 const wxMouseEvent
& event
)
1414 if ( event
.ButtonDown(1) )
1416 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1417 int page
= notebook
->HitTest(event
.GetPosition());
1420 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1426 return wxStdInputHandler::HandleMouse(consumer
, event
);
1429 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1430 const wxMouseEvent
& event
)
1432 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1436 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1437 const wxFocusEvent
& WXUNUSED(event
))
1439 HandleFocusChange(consumer
);
1444 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1445 bool WXUNUSED(activated
))
1447 // we react to the focus change in the same way as to the [de]activation
1448 HandleFocusChange(consumer
);
1453 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1455 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1456 notebook
->RefreshCurrent();
1459 #endif // wxUSE_NOTEBOOK