1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "univnotebook.h"
25 #pragma message disable unscomzer
28 #include "wx/wxprec.h"
36 #include "wx/imaglist.h"
37 #include "wx/notebook.h"
38 #include "wx/spinbutt.h"
39 #include "wx/dcmemory.h"
41 #include "wx/univ/renderer.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
48 // due to unsigned type nPage is always >= 0
49 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
51 #define IS_VALID_PAGE(nPage) (((size_t)nPage) < GetPageCount())
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 static const size_t INVALID_PAGE
= (size_t)-1;
60 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
61 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 class wxNotebookSpinBtn
: public wxSpinButton
70 wxNotebookSpinBtn(wxNotebook
*nb
)
71 : wxSpinButton(nb
, wxID_ANY
,
72 wxDefaultPosition
, wxDefaultSize
,
73 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
79 void OnSpin(wxSpinEvent
& event
)
81 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
90 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
91 EVT_SPIN(wxID_ANY
, wxNotebookSpinBtn::OnSpin
)
94 // ============================================================================
96 // ============================================================================
98 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
99 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
101 // ----------------------------------------------------------------------------
102 // wxNotebook creation
103 // ----------------------------------------------------------------------------
105 void wxNotebook::Init()
107 m_sel
= INVALID_PAGE
;
114 m_lastFullyVisible
= 0;
121 bool wxNotebook::Create(wxWindow
*parent
,
126 const wxString
& name
)
128 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
129 wxDefaultValidator
, name
) )
132 m_sizePad
= GetRenderer()->GetTabPadding();
136 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
141 // ----------------------------------------------------------------------------
142 // wxNotebook page titles and images
143 // ----------------------------------------------------------------------------
145 wxString
wxNotebook::GetPageText(size_t nPage
) const
147 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, _T("invalid notebook page") );
149 return m_titles
[nPage
];
152 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
154 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
156 if ( strText
!= m_titles
[nPage
] )
158 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
160 if ( FixedSizeTabs() )
162 // it's enough to just reresh this one
165 else // var width tabs
167 // we need to resize the tab to fit the new string
175 int wxNotebook::GetPageImage(size_t nPage
) const
177 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
179 return m_images
[nPage
];
182 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
184 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, _T("invalid notebook page") );
186 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
187 _T("invalid image index in SetPageImage()") );
189 if ( nImage
!= m_images
[nPage
] )
191 // if the item didn't have an icon before or, on the contrary, did have
192 // it but has lost it now, its size will change - but if the icon just
194 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
195 m_images
[nPage
] = nImage
;
197 if ( tabSizeChanges
)
206 wxNotebook::~wxNotebook()
210 // ----------------------------------------------------------------------------
211 // wxNotebook page switching
212 // ----------------------------------------------------------------------------
214 int wxNotebook::SetSelection(size_t nPage
)
216 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
218 if ( (size_t)nPage
== m_sel
)
220 // don't do anything if there is nothing to do
225 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
226 event
.SetSelection(nPage
);
227 event
.SetOldSelection(m_sel
);
228 event
.SetEventObject(this);
229 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
231 // program doesn't allow the page change
235 // we need to change m_sel first, before calling RefreshTab() below as
236 // otherwise the previously selected tab wouldn't be redrawn properly under
237 // wxGTK which calls Refresh() immediately and not during the next event
238 // loop iteration as wxMSW does and as it should
239 size_t selOld
= m_sel
;
243 if ( selOld
!= INVALID_PAGE
)
245 RefreshTab(selOld
, true /* this tab was selected */);
247 m_pages
[selOld
]->Hide();
250 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
255 m_spinbtn
->SetValue(m_sel
);
258 if ( m_sel
< m_firstVisible
)
260 // selection is to the left of visible part of tabs
263 else if ( m_sel
> m_lastFullyVisible
)
265 // selection is to the right of visible part of tabs
268 else // we already see this tab
274 m_pages
[m_sel
]->SetSize(GetPageRect());
275 m_pages
[m_sel
]->Show();
279 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
280 GetEventHandler()->ProcessEvent(event
);
285 // ----------------------------------------------------------------------------
286 // wxNotebook pages adding/deleting
287 // ----------------------------------------------------------------------------
289 bool wxNotebook::InsertPage(size_t nPage
,
290 wxNotebookPage
*pPage
,
291 const wxString
& strText
,
295 size_t nPages
= GetPageCount();
296 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), false,
297 _T("invalid notebook page in InsertPage()") );
300 m_pages
.Insert(pPage
, nPage
);
303 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
304 m_titles
.Insert(label
, nPage
);
306 m_images
.Insert(imageId
, nPage
);
308 // cache the tab geometry here
309 wxSize sizeTab
= CalcTabSize(nPage
);
311 if ( sizeTab
.y
> m_heightTab
)
312 m_heightTab
= sizeTab
.y
;
314 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
315 m_widthMax
= sizeTab
.x
;
317 m_widths
.Insert(sizeTab
.x
, nPage
);
319 // spin button may appear if we didn't have it before - but even if we did,
320 // its range should change, so update it unconditionally
323 // if the tab has just appeared, we have to relayout everything, otherwise
324 // it's enough to just redraw the tabs
327 // always select the first tab to have at least some selection
333 else // not the first tab
342 else // pages added to the notebook are initially hidden
350 bool wxNotebook::DeleteAllPages()
352 if ( !wxNotebookBase::DeleteAllPages() )
355 // clear the other arrays as well
361 // it is not valid any longer
362 m_sel
= INVALID_PAGE
;
364 // spin button is not needed any more
372 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
374 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
376 wxNotebookPage
*page
= m_pages
[nPage
];
377 m_pages
.RemoveAt(nPage
);
378 m_titles
.RemoveAt(nPage
);
379 m_accels
.RemoveAt(nPage
);
380 m_widths
.RemoveAt(nPage
);
381 m_images
.RemoveAt(nPage
);
383 // the spin button might not be needed any more
384 // 2002-08-12 'if' commented out by JACS on behalf
385 // of Hans Van Leemputten <Hansvl@softhome.net> who
386 // points out that UpdateSpinBtn should always be called,
387 // to ensure m_lastVisible is up to date.
388 // if ( HasSpinBtn() )
393 size_t count
= GetPageCount();
396 if ( m_sel
== (size_t)nPage
)
398 // avoid sending event to this page which doesn't exist in the
400 m_sel
= INVALID_PAGE
;
402 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
404 else if ( m_sel
> (size_t)nPage
)
406 // no need to change selection, just adjust the index
410 else // no more tabs left
412 m_sel
= INVALID_PAGE
;
415 // have to refresh everything
421 // ----------------------------------------------------------------------------
422 // wxNotebook drawing
423 // ----------------------------------------------------------------------------
425 void wxNotebook::RefreshCurrent()
427 if ( m_sel
!= INVALID_PAGE
)
433 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
435 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
437 wxRect rect
= GetTabRect(page
);
438 if ( forceSelected
|| ((size_t)page
== m_sel
) )
440 const wxSize indent
= GetRenderer()->GetTabIndent();
441 rect
.Inflate(indent
.x
, indent
.y
);
447 void wxNotebook::RefreshAllTabs()
449 wxRect rect
= GetAllTabsRect();
450 if ( rect
.width
|| rect
.height
)
454 //else: we don't have tabs at all
457 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
462 int image
= m_images
[n
];
464 // Not needed now that wxGenericImageList is being
465 // used for wxUniversal under MSW
466 #if 0 // def __WXMSW__ // FIXME
468 m_imageList
->GetSize(n
, w
, h
);
471 dc
.SelectObject(bmp
);
472 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
473 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, true);
474 dc
.SelectObject(wxNullBitmap
);
476 bmp
= *m_imageList
->GetBitmap(image
);
483 flags
|= wxCONTROL_SELECTED
;
486 flags
|= wxCONTROL_FOCUSED
;
489 GetRenderer()->DrawTab
501 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
503 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
505 wxDC
& dc
= renderer
->GetDC();
506 dc
.SetFont(GetFont());
507 dc
.SetTextForeground(GetForegroundColour());
509 // redraw the border - it's simpler to always do it instead of checking
510 // whether this needs to be done
511 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
513 // avoid overwriting the spin button
516 wxRect rectTabs
= GetAllTabsRect();
517 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
521 rectTabs
.height
-= sizeSpinBtn
.y
;
523 // Allow for erasing the line under selected tab
528 rectTabs
.width
-= sizeSpinBtn
.x
;
530 // Allow for erasing the line under selected tab
531 rectTabs
.height
+= 2;
534 dc
.SetClippingRegion(rectTabs
);
537 wxRect rect
= GetTabsPart();
538 bool isVertical
= IsVertical();
541 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
543 GetTabSize(n
, &rect
.width
, &rect
.height
);
547 // don't redraw it now as this tab has to be drawn over the other
548 // ones as it takes more place and spills over to them
551 else // not selected tab
553 // unfortunately we can't do this because the selected tab hangs
554 // over its neighbours and so we might need to refresh more tabs -
555 // of course, we could still avoid rereshing some of them with more
556 // complicated checks, but it doesn't seem too bad to refresh all
557 // of them, I still don't see flicker, so leaving as is for now
559 //if ( rectUpdate.Intersects(rect) )
561 DoDrawTab(dc
, rect
, n
);
563 //else: doesn't need to be refreshed
566 // move the rect to the next tab
568 rect
.y
+= rect
.height
;
570 rect
.x
+= rect
.width
;
573 // now redraw the selected tab
576 DoDrawTab(dc
, rectSel
, m_sel
);
579 dc
.DestroyClippingRegion();
582 // ----------------------------------------------------------------------------
583 // wxNotebook geometry
584 // ----------------------------------------------------------------------------
586 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
589 *flags
= wxNB_HITTEST_NOWHERE
;
591 // first check that it is in this window at all
592 if ( !GetClientRect().Inside(pt
) )
597 wxRect rectTabs
= GetAllTabsRect();
599 switch ( GetTabOrientation() )
602 wxFAIL_MSG(_T("unknown tab orientation"));
606 if ( pt
.y
> rectTabs
.GetBottom() )
611 if ( pt
.y
< rectTabs
.y
)
616 if ( pt
.x
> rectTabs
.GetRight() )
621 if ( pt
.x
< rectTabs
.x
)
626 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
628 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
630 if ( rectTabs
.Inside(pt
) )
634 // TODO: be more precise
635 *flags
= wxNB_HITTEST_ONITEM
;
641 // move the rectTabs to the next tab
643 rectTabs
.y
+= rectTabs
.height
;
645 rectTabs
.x
+= rectTabs
.width
;
651 bool wxNotebook::IsVertical() const
653 wxDirection dir
= GetTabOrientation();
655 return dir
== wxLEFT
|| dir
== wxRIGHT
;
658 wxDirection
wxNotebook::GetTabOrientation() const
660 long style
= GetWindowStyle();
661 if ( style
& wxNB_BOTTOM
)
663 else if ( style
& wxNB_RIGHT
)
665 else if ( style
& wxNB_LEFT
)
668 // wxNB_TOP == 0 so we don't have to test for it
672 wxRect
wxNotebook::GetTabRect(int page
) const
675 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
677 // calc the size of this tab and of the preceding ones
678 wxCoord widthThis
, widthBefore
;
679 if ( FixedSizeTabs() )
681 widthThis
= m_widthMax
;
682 widthBefore
= page
*m_widthMax
;
687 for ( int n
= 0; n
< page
; n
++ )
689 widthBefore
+= m_widths
[n
];
692 widthThis
= m_widths
[page
];
695 rect
= GetTabsPart();
698 rect
.y
+= widthBefore
- m_offset
;
699 rect
.height
= widthThis
;
703 rect
.x
+= widthBefore
- m_offset
;
704 rect
.width
= widthThis
;
710 wxRect
wxNotebook::GetAllTabsRect() const
714 if ( GetPageCount() )
716 const wxSize indent
= GetRenderer()->GetTabIndent();
717 wxSize size
= GetClientSize();
721 rect
.width
= m_heightTab
+ indent
.x
;
722 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
724 rect
.height
= size
.y
;
730 rect
.height
= m_heightTab
+ indent
.y
;
731 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
739 wxRect
wxNotebook::GetTabsPart() const
741 wxRect rect
= GetAllTabsRect();
743 wxDirection dir
= GetTabOrientation();
745 const wxSize indent
= GetRenderer()->GetTabIndent();
752 rect
.width
-= indent
.y
;
756 rect
.width
-= indent
.y
;
765 rect
.height
-= indent
.y
;
769 rect
.height
-= indent
.y
;
776 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
778 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
782 // width and height have inverted meaning
788 // height is always fixed
791 // width may also be fixed and be the same for all tabs
792 *w
= GetTabWidth(page
);
795 void wxNotebook::SetTabSize(const wxSize
& sz
)
797 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
811 wxSize
wxNotebook::CalcTabSize(int page
) const
813 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
814 // method is called to calculate them
818 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
820 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
822 if ( HasImage(page
) )
825 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
827 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
829 if ( sizeImage
.y
> size
.y
)
830 size
.y
= sizeImage
.y
;
833 size
.x
+= 2*m_sizePad
.x
;
834 size
.y
+= 2*m_sizePad
.y
;
839 void wxNotebook::ResizeTab(int page
)
841 wxSize sizeTab
= CalcTabSize(page
);
843 // we only need full relayout if the page size changes
844 bool needsRelayout
= false;
849 wxCoord tmp
= sizeTab
.x
;
850 sizeTab
.x
= sizeTab
.y
;
854 if ( sizeTab
.y
> m_heightTab
)
856 needsRelayout
= true;
858 m_heightTab
= sizeTab
.y
;
861 m_widths
[page
] = sizeTab
.x
;
863 if ( sizeTab
.x
> m_widthMax
)
864 m_widthMax
= sizeTab
.x
;
866 // the total of the tabs has changed too
875 void wxNotebook::SetPadding(const wxSize
& padding
)
877 if ( padding
!= m_sizePad
)
885 void wxNotebook::Relayout()
887 if ( GetPageCount() )
893 if ( m_sel
!= INVALID_PAGE
)
895 // resize the currently shown page
896 wxRect rectPage
= GetPageRect();
898 m_pages
[m_sel
]->SetSize(rectPage
);
900 // also scroll it into view if needed (note that m_lastVisible
901 // was updated by the call to UpdateSpinBtn() above, this is why it
905 if ( m_sel
< m_firstVisible
)
907 // selection is to the left of visible part of tabs
910 else if ( m_sel
> m_lastFullyVisible
)
912 // selection is to the right of visible part of tabs
918 else // we have no pages
920 // just refresh everything
925 wxRect
wxNotebook::GetPagePart() const
927 wxRect rectPage
= GetClientRect();
929 if ( GetPageCount() )
931 wxRect rectTabs
= GetAllTabsRect();
932 wxDirection dir
= GetTabOrientation();
935 rectPage
.width
-= rectTabs
.width
;
937 rectPage
.x
+= rectTabs
.width
;
941 rectPage
.height
-= rectTabs
.height
;
943 rectPage
.y
+= rectTabs
.height
;
946 //else: no pages at all
951 wxRect
wxNotebook::GetPageRect() const
953 wxRect rect
= GetPagePart();
955 // leave space for the border
956 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
958 // FIXME: hardcoded +2!
959 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
960 -(rectBorder
.y
+ rectBorder
.height
+ 2));
965 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
967 wxSize sizeNb
= size
;
968 wxRect rect
= GetAllTabsRect();
970 sizeNb
.x
+= rect
.width
;
972 sizeNb
.y
+= rect
.height
;
977 void wxNotebook::SetPageSize(const wxSize
& size
)
979 SetClientSize(GetSizeForPage(size
));
982 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
984 return AdjustSize(GetSizeForPage(sizePage
));
987 // ----------------------------------------------------------------------------
988 // wxNotebook spin button
989 // ----------------------------------------------------------------------------
991 bool wxNotebook::HasSpinBtn() const
993 return m_spinbtn
&& m_spinbtn
->IsShown();
996 void wxNotebook::CalcLastVisibleTab()
998 bool isVertical
= IsVertical();
1000 wxCoord width
= GetClientSize().x
;
1002 wxRect rect
= GetTabsPart();
1004 size_t count
= GetPageCount();
1006 wxCoord widthLast
= 0;
1008 for ( n
= m_firstVisible
; n
< count
; n
++ )
1010 GetTabSize(n
, &rect
.width
, &rect
.height
);
1011 if ( rect
.GetRight() > width
)
1016 // remember it to use below
1017 widthLast
= rect
.GetRight();
1019 // move the rect to the next tab
1021 rect
.y
+= rect
.height
;
1023 rect
.x
+= rect
.width
;
1026 if ( n
== m_firstVisible
)
1028 // even the first tab isn't fully visible - but still pretend it is as
1029 // we have to show something
1030 m_lastFullyVisible
= m_firstVisible
;
1032 else // more than one tab visible
1034 m_lastFullyVisible
= n
- 1;
1036 // but is it really fully visible? it shouldn't overlap with the spin
1037 // button if it is present (again, even if the first button does
1038 // overlap with it, we pretend that it doesn't as there is not much
1040 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1042 // adjust width to be the width before the spin button
1043 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1045 width
-= sizeSpinBtn
.y
;
1047 width
-= sizeSpinBtn
.x
;
1049 if ( widthLast
> width
)
1051 // the last button overlaps with spin button, so take he
1053 m_lastFullyVisible
--;
1060 // everything is visible
1065 // this tab is still (partially) visible, so m_lastVisible is the
1066 // next tab (remember, this is "exclusive" last)
1067 m_lastVisible
= n
+ 1;
1072 void wxNotebook::UpdateSpinBtn()
1074 // first decide if we need a spin button
1077 size_t count
= (size_t)GetPageCount();
1080 // this case is special, get rid of it immediately: everything is
1081 // visible and we don't need any spin buttons
1082 allTabsShown
= true;
1084 // have to reset them manually as we don't call CalcLastVisibleTab()
1087 m_lastFullyVisible
= 0;
1091 CalcLastVisibleTab();
1093 // if all tabs after the first visible one are shown, it doesn't yet
1094 // mean that all tabs are shown - so we go backwards until we arrive to
1095 // the beginning (then all tabs are indeed shown) or find a tab such
1096 // that not all tabs after it are shown
1097 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1099 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1101 m_offset
-= GetTabWidth(m_firstVisible
--);
1103 // reclaculate after m_firstVisible change
1104 CalcLastVisibleTab();
1107 allTabsShown
= m_lastFullyVisible
== count
- 1;
1110 if ( !allTabsShown
)
1114 // create it once only
1115 m_spinbtn
= new wxNotebookSpinBtn(this);
1117 // set the correct value to keep it in sync
1118 m_spinbtn
->SetValue(m_sel
);
1121 // position it correctly
1127 // also set/update the range
1128 m_spinbtn
->SetRange(0, count
- 1);
1130 // update m_lastFullyVisible once again as it might have changed
1131 // because the spin button appeared
1133 // FIXME: might do it more efficiently
1134 CalcLastVisibleTab();
1136 else // all tabs are visible, we don't need spin button
1138 if ( m_spinbtn
&& m_spinbtn
-> IsShown() )
1145 void wxNotebook::PositionSpinBtn()
1151 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1153 wxRect rectTabs
= GetAllTabsRect();
1156 switch ( GetTabOrientation() )
1159 wxFAIL_MSG(_T("unknown tab orientation"));
1163 x
= rectTabs
.GetRight() - wBtn
;
1164 y
= rectTabs
.GetBottom() - hBtn
;
1168 x
= rectTabs
.GetRight() - wBtn
;
1169 y
= rectTabs
.GetTop();
1173 x
= rectTabs
.GetRight() - wBtn
;
1174 y
= rectTabs
.GetBottom() - hBtn
;
1178 x
= rectTabs
.GetLeft();
1179 y
= rectTabs
.GetBottom() - hBtn
;
1183 m_spinbtn
->Move(x
, y
);
1186 // ----------------------------------------------------------------------------
1187 // wxNotebook scrolling
1188 // ----------------------------------------------------------------------------
1190 void wxNotebook::ScrollTo(int page
)
1192 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1194 // set the first visible tab and offset (easy)
1195 m_firstVisible
= (size_t)page
;
1197 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1199 m_offset
+= GetTabWidth(n
);
1202 // find the last visible tab too
1203 CalcLastVisibleTab();
1208 void wxNotebook::ScrollLastTo(int page
)
1210 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1212 // go backwards until we find the first tab which can be made visible
1213 // without hiding the given one
1214 wxSize size
= GetClientSize();
1215 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1216 widthTabs
= GetTabWidth(page
);
1218 // the total width is less than the width of the window if we have the spin
1222 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1224 widthAll
-= sizeSpinBtn
.y
;
1226 widthAll
-= sizeSpinBtn
.x
;
1229 m_firstVisible
= page
;
1230 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1232 widthTabs
+= GetTabWidth(--m_firstVisible
);
1235 if ( widthTabs
> widthAll
)
1237 // take one step back (that it is forward) if we can
1238 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1243 ScrollTo(m_firstVisible
);
1245 // consitency check: the page we were asked to show should be shown
1246 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1249 // ----------------------------------------------------------------------------
1250 // wxNotebook sizing/moving
1251 // ----------------------------------------------------------------------------
1253 wxSize
wxNotebook::DoGetBestClientSize() const
1255 // calculate the max page size
1258 size_t count
= GetPageCount();
1261 for ( size_t n
= 0; n
< count
; n
++ )
1263 wxSize sizePage
= m_pages
[n
]->GetSize();
1265 if ( size
.x
< sizePage
.x
)
1266 size
.x
= sizePage
.x
;
1267 if ( size
.y
< sizePage
.y
)
1268 size
.y
= sizePage
.y
;
1273 // use some arbitrary default size
1278 return GetSizeForPage(size
);
1281 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1283 wxControl::DoMoveWindow(x
, y
, width
, height
);
1285 // move the spin ctrl too (NOP if it doesn't exist)
1289 void wxNotebook::DoSetSize(int x
, int y
,
1290 int width
, int height
,
1293 wxSize old_client_size
= GetClientSize();
1295 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1297 wxSize new_client_size
= GetClientSize();
1299 if (old_client_size
!= new_client_size
)
1303 // ----------------------------------------------------------------------------
1304 // wxNotebook input processing
1305 // ----------------------------------------------------------------------------
1307 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1309 const wxString
& strArg
)
1311 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1312 SetSelection(GetNextPage(true));
1313 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1314 SetSelection(GetNextPage(false));
1315 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1316 SetSelection((int)numArg
);
1318 return wxControl::PerformAction(action
, numArg
, strArg
);
1323 // ----------------------------------------------------------------------------
1324 // wxStdNotebookInputHandler
1325 // ----------------------------------------------------------------------------
1327 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1328 : wxStdInputHandler(inphand
)
1332 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1333 const wxKeyEvent
& event
,
1336 // ignore the key releases
1339 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1342 wxControlAction action
;
1343 switch ( event
.GetKeyCode() )
1346 if ( notebook
->IsVertical() )
1347 action
= wxACTION_NOTEBOOK_PREV
;
1351 if ( !notebook
->IsVertical() )
1352 action
= wxACTION_NOTEBOOK_PREV
;
1356 if ( notebook
->IsVertical() )
1357 action
= wxACTION_NOTEBOOK_NEXT
;
1361 if ( !notebook
->IsVertical() )
1362 action
= wxACTION_NOTEBOOK_NEXT
;
1366 action
= wxACTION_NOTEBOOK_GOTO
;
1367 // page = 0; -- already has this value
1371 action
= wxACTION_NOTEBOOK_GOTO
;
1372 page
= notebook
->GetPageCount() - 1;
1376 if ( !action
.IsEmpty() )
1378 return consumer
->PerformAction(action
, page
);
1382 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1385 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1386 const wxMouseEvent
& event
)
1388 if ( event
.ButtonDown(1) )
1390 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1391 int page
= notebook
->HitTest(event
.GetPosition());
1394 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1400 return wxStdInputHandler::HandleMouse(consumer
, event
);
1403 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1404 const wxMouseEvent
& event
)
1406 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1410 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1411 const wxFocusEvent
& WXUNUSED(event
))
1413 HandleFocusChange(consumer
);
1418 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1419 bool WXUNUSED(activated
))
1421 // we react to the focus change in the same way as to the [de]activation
1422 HandleFocusChange(consumer
);
1427 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1429 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1430 notebook
->RefreshCurrent();
1433 #endif // wxUSE_NOTEBOOK