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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 class wxNotebookSpinBtn
: public wxSpinButton
89 wxNotebookSpinBtn(wxNotebook
*nb
)
90 : wxSpinButton(nb
, wxID_ANY
,
91 wxDefaultPosition
, wxDefaultSize
,
92 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
98 void OnSpin(wxSpinEvent
& event
)
100 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
106 DECLARE_EVENT_TABLE()
109 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
110 EVT_SPIN(wxID_ANY
, wxNotebookSpinBtn::OnSpin
)
113 // ============================================================================
115 // ============================================================================
117 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
119 // ----------------------------------------------------------------------------
120 // wxNotebook creation
121 // ----------------------------------------------------------------------------
123 void wxNotebook::Init()
125 m_sel
= INVALID_PAGE
;
132 m_lastFullyVisible
= 0;
139 bool wxNotebook::Create(wxWindow
*parent
,
144 const wxString
& name
)
146 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
149 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
150 wxDefaultValidator
, name
) )
153 m_sizePad
= GetRenderer()->GetTabPadding();
155 SetInitialSize(size
);
157 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
162 // ----------------------------------------------------------------------------
163 // wxNotebook page titles and images
164 // ----------------------------------------------------------------------------
166 wxString
wxNotebook::GetPageText(size_t nPage
) const
168 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, _T("invalid notebook page") );
170 return m_titles
[nPage
];
173 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
175 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
177 if ( strText
!= m_titles
[nPage
] )
179 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
181 if ( FixedSizeTabs() )
183 // it's enough to just reresh this one
186 else // var width tabs
188 // we need to resize the tab to fit the new string
196 int wxNotebook::GetPageImage(size_t nPage
) const
198 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, _T("invalid notebook page") );
200 return m_images
[nPage
];
203 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
205 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
207 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
208 _T("invalid image index in SetPageImage()") );
210 if ( nImage
!= m_images
[nPage
] )
212 // if the item didn't have an icon before or, on the contrary, did have
213 // it but has lost it now, its size will change - but if the icon just
215 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
216 m_images
[nPage
] = nImage
;
218 if ( tabSizeChanges
)
227 wxNotebook::~wxNotebook()
231 // ----------------------------------------------------------------------------
232 // wxNotebook page switching
233 // ----------------------------------------------------------------------------
235 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
237 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, _T("invalid notebook page") );
239 if ( (size_t)nPage
== m_sel
)
241 // don't do anything if there is nothing to do
245 if ( flags
& SetSelection_SendEvent
)
247 if ( !SendPageChangingEvent(nPage
) )
249 // program doesn't allow the page change
254 // we need to change m_sel first, before calling RefreshTab() below as
255 // otherwise the previously selected tab wouldn't be redrawn properly under
256 // wxGTK which calls Refresh() immediately and not during the next event
257 // loop iteration as wxMSW does and as it should
258 size_t selOld
= m_sel
;
262 if ( selOld
!= INVALID_PAGE
)
264 RefreshTab(selOld
, true /* this tab was selected */);
266 m_pages
[selOld
]->Hide();
269 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
274 m_spinbtn
->SetValue(m_sel
);
277 if ( m_sel
< m_firstVisible
)
279 // selection is to the left of visible part of tabs
282 else if ( m_sel
> m_lastFullyVisible
)
284 // selection is to the right of visible part of tabs
287 else // we already see this tab
293 m_pages
[m_sel
]->SetSize(GetPageRect());
294 m_pages
[m_sel
]->Show();
297 if ( flags
& SetSelection_SendEvent
)
300 SendPageChangedEvent(selOld
);
306 // ----------------------------------------------------------------------------
307 // wxNotebook pages adding/deleting
308 // ----------------------------------------------------------------------------
310 bool wxNotebook::InsertPage(size_t nPage
,
311 wxNotebookPage
*pPage
,
312 const wxString
& strText
,
316 size_t nPages
= GetPageCount();
317 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), false,
318 _T("invalid notebook page in InsertPage()") );
321 m_pages
.Insert(pPage
, nPage
);
324 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
325 m_titles
.Insert(label
, nPage
);
327 m_images
.Insert(imageId
, nPage
);
329 // cache the tab geometry here
330 wxSize sizeTab
= CalcTabSize(nPage
);
332 if ( sizeTab
.y
> m_heightTab
)
333 m_heightTab
= sizeTab
.y
;
335 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
336 m_widthMax
= sizeTab
.x
;
338 m_widths
.Insert(sizeTab
.x
, nPage
);
340 // spin button may appear if we didn't have it before - but even if we did,
341 // its range should change, so update it unconditionally
344 // if the tab has just appeared, we have to relayout everything, otherwise
345 // it's enough to just redraw the tabs
348 // always select the first tab to have at least some selection
354 else // not the first tab
363 else // pages added to the notebook are initially hidden
371 bool wxNotebook::DeleteAllPages()
373 if ( !wxNotebookBase::DeleteAllPages() )
376 // clear the other arrays as well
382 // it is not valid any longer
383 m_sel
= INVALID_PAGE
;
385 // spin button is not needed any more
393 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
395 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
397 wxNotebookPage
*page
= m_pages
[nPage
];
398 m_pages
.RemoveAt(nPage
);
399 m_titles
.RemoveAt(nPage
);
400 m_accels
.RemoveAt(nPage
);
401 m_widths
.RemoveAt(nPage
);
402 m_images
.RemoveAt(nPage
);
404 // the spin button might not be needed any more
405 // 2002-08-12 'if' commented out by JACS on behalf
406 // of Hans Van Leemputten <Hansvl@softhome.net> who
407 // points out that UpdateSpinBtn should always be called,
408 // to ensure m_lastVisible is up to date.
409 // if ( HasSpinBtn() )
414 size_t count
= GetPageCount();
417 if ( m_sel
== (size_t)nPage
)
419 // avoid sending event to this page which doesn't exist in the
421 m_sel
= INVALID_PAGE
;
423 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
425 else if ( m_sel
> (size_t)nPage
)
427 // no need to change selection, just adjust the index
431 else // no more tabs left
433 m_sel
= INVALID_PAGE
;
436 // have to refresh everything
442 // ----------------------------------------------------------------------------
443 // wxNotebook drawing
444 // ----------------------------------------------------------------------------
446 void wxNotebook::RefreshCurrent()
448 if ( m_sel
!= INVALID_PAGE
)
454 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
456 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
458 wxRect rect
= GetTabRect(page
);
459 if ( forceSelected
|| ((size_t)page
== m_sel
) )
461 const wxSize indent
= GetRenderer()->GetTabIndent();
462 rect
.Inflate(indent
.x
, indent
.y
);
468 void wxNotebook::RefreshAllTabs()
470 wxRect rect
= GetAllTabsRect();
471 if ( rect
.width
|| rect
.height
)
475 //else: we don't have tabs at all
478 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
483 int image
= m_images
[n
];
485 // Not needed now that wxGenericImageList is being
486 // used for wxUniversal under MSW
487 #if 0 // def __WXMSW__ // FIXME
489 m_imageList
->GetSize(n
, w
, h
);
492 dc
.SelectObject(bmp
);
493 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
494 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, true);
495 dc
.SelectObject(wxNullBitmap
);
497 bmp
= m_imageList
->GetBitmap(image
);
504 flags
|= wxCONTROL_SELECTED
;
507 flags
|= wxCONTROL_FOCUSED
;
510 GetRenderer()->DrawTab
522 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
524 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
526 wxDC
& dc
= renderer
->GetDC();
527 dc
.SetFont(GetFont());
528 dc
.SetTextForeground(GetForegroundColour());
530 // redraw the border - it's simpler to always do it instead of checking
531 // whether this needs to be done
532 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
534 // avoid overwriting the spin button
537 wxRect rectTabs
= GetAllTabsRect();
538 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
542 rectTabs
.height
-= sizeSpinBtn
.y
;
544 // Allow for erasing the line under selected tab
549 rectTabs
.width
-= sizeSpinBtn
.x
;
551 // Allow for erasing the line under selected tab
552 rectTabs
.height
+= 2;
555 dc
.SetClippingRegion(rectTabs
);
558 wxRect rect
= GetTabsPart();
559 bool isVertical
= IsVertical();
562 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
564 GetTabSize(n
, &rect
.width
, &rect
.height
);
568 // don't redraw it now as this tab has to be drawn over the other
569 // ones as it takes more place and spills over to them
572 else // not selected tab
574 // unfortunately we can't do this because the selected tab hangs
575 // over its neighbours and so we might need to refresh more tabs -
576 // of course, we could still avoid rereshing some of them with more
577 // complicated checks, but it doesn't seem too bad to refresh all
578 // of them, I still don't see flicker, so leaving as is for now
580 //if ( rectUpdate.Intersects(rect) )
582 DoDrawTab(dc
, rect
, n
);
584 //else: doesn't need to be refreshed
587 // move the rect to the next tab
589 rect
.y
+= rect
.height
;
591 rect
.x
+= rect
.width
;
594 // now redraw the selected tab
597 DoDrawTab(dc
, rectSel
, m_sel
);
600 dc
.DestroyClippingRegion();
603 // ----------------------------------------------------------------------------
604 // wxNotebook geometry
605 // ----------------------------------------------------------------------------
607 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
610 *flags
= wxBK_HITTEST_NOWHERE
;
612 // first check that it is in this window at all
613 if ( !GetClientRect().Contains(pt
) )
618 wxRect rectTabs
= GetAllTabsRect();
620 switch ( GetTabOrientation() )
623 wxFAIL_MSG(_T("unknown tab orientation"));
627 if ( pt
.y
> rectTabs
.GetBottom() )
632 if ( pt
.y
< rectTabs
.y
)
637 if ( pt
.x
> rectTabs
.GetRight() )
642 if ( pt
.x
< rectTabs
.x
)
647 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
649 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
651 if ( rectTabs
.Contains(pt
) )
655 // TODO: be more precise
656 *flags
= wxBK_HITTEST_ONITEM
;
662 // move the rectTabs to the next tab
664 rectTabs
.y
+= rectTabs
.height
;
666 rectTabs
.x
+= rectTabs
.width
;
672 bool wxNotebook::IsVertical() const
674 wxDirection dir
= GetTabOrientation();
676 return dir
== wxLEFT
|| dir
== wxRIGHT
;
679 wxDirection
wxNotebook::GetTabOrientation() const
681 long style
= GetWindowStyle();
682 if ( style
& wxBK_BOTTOM
)
684 else if ( style
& wxBK_RIGHT
)
686 else if ( style
& wxBK_LEFT
)
689 // wxBK_TOP == 0 so we don't have to test for it
693 wxRect
wxNotebook::GetTabRect(int page
) const
696 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
698 // calc the size of this tab and of the preceding ones
699 wxCoord widthThis
, widthBefore
;
700 if ( FixedSizeTabs() )
702 widthThis
= m_widthMax
;
703 widthBefore
= page
*m_widthMax
;
708 for ( int n
= 0; n
< page
; n
++ )
710 widthBefore
+= m_widths
[n
];
713 widthThis
= m_widths
[page
];
716 rect
= GetTabsPart();
719 rect
.y
+= widthBefore
- m_offset
;
720 rect
.height
= widthThis
;
724 rect
.x
+= widthBefore
- m_offset
;
725 rect
.width
= widthThis
;
731 wxRect
wxNotebook::GetAllTabsRect() const
735 if ( GetPageCount() )
737 const wxSize indent
= GetRenderer()->GetTabIndent();
738 wxSize size
= GetClientSize();
742 rect
.width
= m_heightTab
+ indent
.x
;
743 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
745 rect
.height
= size
.y
;
751 rect
.height
= m_heightTab
+ indent
.y
;
752 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
760 wxRect
wxNotebook::GetTabsPart() const
762 wxRect rect
= GetAllTabsRect();
764 wxDirection dir
= GetTabOrientation();
766 const wxSize indent
= GetRenderer()->GetTabIndent();
773 rect
.width
-= indent
.y
;
777 rect
.width
-= indent
.y
;
786 rect
.height
-= indent
.y
;
790 rect
.height
-= indent
.y
;
797 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
799 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
803 // width and height have inverted meaning
809 // height is always fixed
812 // width may also be fixed and be the same for all tabs
813 *w
= GetTabWidth(page
);
816 void wxNotebook::SetTabSize(const wxSize
& sz
)
818 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
832 wxSize
wxNotebook::CalcTabSize(int page
) const
834 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
835 // method is called to calculate them
839 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
841 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
843 if ( HasImage(page
) )
846 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
848 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
850 if ( sizeImage
.y
> size
.y
)
851 size
.y
= sizeImage
.y
;
854 size
.x
+= 2*m_sizePad
.x
;
855 size
.y
+= 2*m_sizePad
.y
;
860 void wxNotebook::ResizeTab(int page
)
862 wxSize sizeTab
= CalcTabSize(page
);
864 // we only need full relayout if the page size changes
865 bool needsRelayout
= false;
870 wxCoord tmp
= sizeTab
.x
;
871 sizeTab
.x
= sizeTab
.y
;
875 if ( sizeTab
.y
> m_heightTab
)
877 needsRelayout
= true;
879 m_heightTab
= sizeTab
.y
;
882 m_widths
[page
] = sizeTab
.x
;
884 if ( sizeTab
.x
> m_widthMax
)
885 m_widthMax
= sizeTab
.x
;
887 // the total of the tabs has changed too
896 void wxNotebook::SetPadding(const wxSize
& padding
)
898 if ( padding
!= m_sizePad
)
906 void wxNotebook::Relayout()
908 if ( GetPageCount() )
914 if ( m_sel
!= INVALID_PAGE
)
916 // resize the currently shown page
917 wxRect rectPage
= GetPageRect();
919 m_pages
[m_sel
]->SetSize(rectPage
);
921 // also scroll it into view if needed (note that m_lastVisible
922 // was updated by the call to UpdateSpinBtn() above, this is why it
926 if ( m_sel
< m_firstVisible
)
928 // selection is to the left of visible part of tabs
931 else if ( m_sel
> m_lastFullyVisible
)
933 // selection is to the right of visible part of tabs
939 else // we have no pages
941 // just refresh everything
946 wxRect
wxNotebook::GetPagePart() const
948 wxRect rectPage
= GetClientRect();
950 if ( GetPageCount() )
952 wxRect rectTabs
= GetAllTabsRect();
953 wxDirection dir
= GetTabOrientation();
956 rectPage
.width
-= rectTabs
.width
;
958 rectPage
.x
+= rectTabs
.width
;
962 rectPage
.height
-= rectTabs
.height
;
964 rectPage
.y
+= rectTabs
.height
;
967 //else: no pages at all
972 wxRect
wxNotebook::GetPageRect() const
974 wxRect rect
= GetPagePart();
976 // leave space for the border
977 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
979 // FIXME: hardcoded +2!
980 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
981 -(rectBorder
.y
+ rectBorder
.height
+ 2));
986 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
988 wxSize sizeNb
= size
;
989 wxRect rect
= GetAllTabsRect();
991 sizeNb
.x
+= rect
.width
;
993 sizeNb
.y
+= rect
.height
;
998 void wxNotebook::SetPageSize(const wxSize
& size
)
1000 SetClientSize(GetSizeForPage(size
));
1003 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
1005 return AdjustSize(GetSizeForPage(sizePage
));
1008 // ----------------------------------------------------------------------------
1009 // wxNotebook spin button
1010 // ----------------------------------------------------------------------------
1012 bool wxNotebook::HasSpinBtn() const
1014 return m_spinbtn
&& m_spinbtn
->IsShown();
1017 void wxNotebook::CalcLastVisibleTab()
1019 bool isVertical
= IsVertical();
1021 wxCoord width
= GetClientSize().x
;
1023 wxRect rect
= GetTabsPart();
1025 size_t count
= GetPageCount();
1027 wxCoord widthLast
= 0;
1029 for ( n
= m_firstVisible
; n
< count
; n
++ )
1031 GetTabSize(n
, &rect
.width
, &rect
.height
);
1032 if ( rect
.GetRight() > width
)
1037 // remember it to use below
1038 widthLast
= rect
.GetRight();
1040 // move the rect to the next tab
1042 rect
.y
+= rect
.height
;
1044 rect
.x
+= rect
.width
;
1047 if ( n
== m_firstVisible
)
1049 // even the first tab isn't fully visible - but still pretend it is as
1050 // we have to show something
1051 m_lastFullyVisible
= m_firstVisible
;
1053 else // more than one tab visible
1055 m_lastFullyVisible
= n
- 1;
1057 // but is it really fully visible? it shouldn't overlap with the spin
1058 // button if it is present (again, even if the first button does
1059 // overlap with it, we pretend that it doesn't as there is not much
1061 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1063 // adjust width to be the width before the spin button
1064 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1066 width
-= sizeSpinBtn
.y
;
1068 width
-= sizeSpinBtn
.x
;
1070 if ( widthLast
> width
)
1072 // the last button overlaps with spin button, so take he
1074 m_lastFullyVisible
--;
1081 // everything is visible
1086 // this tab is still (partially) visible, so m_lastVisible is the
1087 // next tab (remember, this is "exclusive" last)
1088 m_lastVisible
= n
+ 1;
1093 void wxNotebook::UpdateSpinBtn()
1095 // first decide if we need a spin button
1098 size_t count
= (size_t)GetPageCount();
1101 // this case is special, get rid of it immediately: everything is
1102 // visible and we don't need any spin buttons
1103 allTabsShown
= true;
1105 // have to reset them manually as we don't call CalcLastVisibleTab()
1108 m_lastFullyVisible
= 0;
1112 CalcLastVisibleTab();
1114 // if all tabs after the first visible one are shown, it doesn't yet
1115 // mean that all tabs are shown - so we go backwards until we arrive to
1116 // the beginning (then all tabs are indeed shown) or find a tab such
1117 // that not all tabs after it are shown
1118 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1120 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1122 m_offset
-= GetTabWidth(m_firstVisible
--);
1124 // reclaculate after m_firstVisible change
1125 CalcLastVisibleTab();
1128 allTabsShown
= m_lastFullyVisible
== count
- 1;
1131 if ( !allTabsShown
)
1135 // create it once only
1136 m_spinbtn
= new wxNotebookSpinBtn(this);
1138 // set the correct value to keep it in sync
1139 m_spinbtn
->SetValue(m_sel
);
1142 // position it correctly
1148 // also set/update the range
1149 m_spinbtn
->SetRange(0, count
- 1);
1151 // update m_lastFullyVisible once again as it might have changed
1152 // because the spin button appeared
1154 // FIXME: might do it more efficiently
1155 CalcLastVisibleTab();
1157 else // all tabs are visible, we don't need spin button
1159 if ( m_spinbtn
&& m_spinbtn
-> IsShown() )
1166 void wxNotebook::PositionSpinBtn()
1172 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1174 wxRect rectTabs
= GetAllTabsRect();
1177 switch ( GetTabOrientation() )
1180 wxFAIL_MSG(_T("unknown tab orientation"));
1184 x
= rectTabs
.GetRight() - wBtn
;
1185 y
= rectTabs
.GetBottom() - hBtn
;
1189 x
= rectTabs
.GetRight() - wBtn
;
1190 y
= rectTabs
.GetTop();
1194 x
= rectTabs
.GetRight() - wBtn
;
1195 y
= rectTabs
.GetBottom() - hBtn
;
1199 x
= rectTabs
.GetLeft();
1200 y
= rectTabs
.GetBottom() - hBtn
;
1204 m_spinbtn
->Move(x
, y
);
1207 // ----------------------------------------------------------------------------
1208 // wxNotebook scrolling
1209 // ----------------------------------------------------------------------------
1211 void wxNotebook::ScrollTo(int page
)
1213 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1215 // set the first visible tab and offset (easy)
1216 m_firstVisible
= (size_t)page
;
1218 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1220 m_offset
+= GetTabWidth(n
);
1223 // find the last visible tab too
1224 CalcLastVisibleTab();
1229 void wxNotebook::ScrollLastTo(int page
)
1231 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1233 // go backwards until we find the first tab which can be made visible
1234 // without hiding the given one
1235 wxSize size
= GetClientSize();
1236 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1237 widthTabs
= GetTabWidth(page
);
1239 // the total width is less than the width of the window if we have the spin
1243 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1245 widthAll
-= sizeSpinBtn
.y
;
1247 widthAll
-= sizeSpinBtn
.x
;
1250 m_firstVisible
= page
;
1251 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1253 widthTabs
+= GetTabWidth(--m_firstVisible
);
1256 if ( widthTabs
> widthAll
)
1258 // take one step back (that it is forward) if we can
1259 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1264 ScrollTo(m_firstVisible
);
1266 // consitency check: the page we were asked to show should be shown
1267 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1270 // ----------------------------------------------------------------------------
1271 // wxNotebook sizing/moving
1272 // ----------------------------------------------------------------------------
1274 wxSize
wxNotebook::DoGetBestClientSize() const
1276 // calculate the max page size
1279 size_t count
= GetPageCount();
1282 for ( size_t n
= 0; n
< count
; n
++ )
1284 wxSize sizePage
= m_pages
[n
]->GetSize();
1286 if ( size
.x
< sizePage
.x
)
1287 size
.x
= sizePage
.x
;
1288 if ( size
.y
< sizePage
.y
)
1289 size
.y
= sizePage
.y
;
1294 // use some arbitrary default size
1299 return GetSizeForPage(size
);
1302 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1304 wxControl::DoMoveWindow(x
, y
, width
, height
);
1306 // move the spin ctrl too (NOP if it doesn't exist)
1310 void wxNotebook::DoSetSize(int x
, int y
,
1311 int width
, int height
,
1314 wxSize old_client_size
= GetClientSize();
1316 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1318 wxSize new_client_size
= GetClientSize();
1320 if (old_client_size
!= new_client_size
)
1324 // ----------------------------------------------------------------------------
1325 // wxNotebook input processing
1326 // ----------------------------------------------------------------------------
1328 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1330 const wxString
& strArg
)
1332 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1333 SetSelection(GetNextPage(true));
1334 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1335 SetSelection(GetNextPage(false));
1336 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1337 SetSelection((int)numArg
);
1339 return wxControl::PerformAction(action
, numArg
, strArg
);
1345 wxInputHandler
*wxNotebook::GetStdInputHandler(wxInputHandler
*handlerDef
)
1347 static wxStdNotebookInputHandler
s_handler(handlerDef
);
1352 // ----------------------------------------------------------------------------
1353 // wxStdNotebookInputHandler
1354 // ----------------------------------------------------------------------------
1356 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1357 : wxStdInputHandler(inphand
)
1361 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1362 const wxKeyEvent
& event
,
1365 // ignore the key releases
1368 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1371 wxControlAction action
;
1372 switch ( event
.GetKeyCode() )
1375 if ( notebook
->IsVertical() )
1376 action
= wxACTION_NOTEBOOK_PREV
;
1380 if ( !notebook
->IsVertical() )
1381 action
= wxACTION_NOTEBOOK_PREV
;
1385 if ( notebook
->IsVertical() )
1386 action
= wxACTION_NOTEBOOK_NEXT
;
1390 if ( !notebook
->IsVertical() )
1391 action
= wxACTION_NOTEBOOK_NEXT
;
1395 action
= wxACTION_NOTEBOOK_GOTO
;
1396 // page = 0; -- already has this value
1400 action
= wxACTION_NOTEBOOK_GOTO
;
1401 page
= notebook
->GetPageCount() - 1;
1405 if ( !action
.IsEmpty() )
1407 return consumer
->PerformAction(action
, page
);
1411 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1414 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1415 const wxMouseEvent
& event
)
1417 if ( event
.ButtonDown(1) )
1419 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1420 int page
= notebook
->HitTest(event
.GetPosition());
1423 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1429 return wxStdInputHandler::HandleMouse(consumer
, event
);
1432 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1433 const wxMouseEvent
& event
)
1435 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1439 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1440 const wxFocusEvent
& WXUNUSED(event
))
1442 HandleFocusChange(consumer
);
1447 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1448 bool WXUNUSED(activated
))
1450 // we react to the focus change in the same way as to the [de]activation
1451 HandleFocusChange(consumer
);
1456 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1458 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1459 notebook
->RefreshCurrent();
1462 #endif // wxUSE_NOTEBOOK