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
, -1,
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(-1, wxNotebookSpinBtn::OnSpin
)
94 // ============================================================================
96 // ============================================================================
98 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
99 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
101 // ----------------------------------------------------------------------------
102 // wxNotebook creation
103 // ----------------------------------------------------------------------------
105 wxNotebook::wxNotebook()
110 wxNotebook::wxNotebook(wxWindow
*parent
,
115 const wxString
& name
)
119 (void)Create(parent
, id
, pos
, size
, style
, name
);
122 void wxNotebook::Init()
124 m_sel
= INVALID_PAGE
;
131 m_lastFullyVisible
= 0;
138 bool wxNotebook::Create(wxWindow
*parent
,
143 const wxString
& name
)
145 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
146 wxDefaultValidator
, name
) )
149 m_sizePad
= GetRenderer()->GetTabPadding();
153 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
158 // ----------------------------------------------------------------------------
159 // wxNotebook page titles and images
160 // ----------------------------------------------------------------------------
162 wxString
wxNotebook::GetPageText(size_t nPage
) const
164 wxCHECK_MSG( IS_VALID_PAGE(nPage
), _T(""), _T("invalid notebook page") );
166 return m_titles
[nPage
];
169 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
171 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
173 if ( strText
!= m_titles
[nPage
] )
175 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
177 if ( FixedSizeTabs() )
179 // it's enough to just reresh this one
182 else // var width tabs
184 // we need to resize the tab to fit the new string
192 int wxNotebook::GetPageImage(size_t nPage
) const
194 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
196 return m_images
[nPage
];
199 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
201 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
203 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
204 _T("invalid image index in SetPageImage()") );
206 if ( nImage
!= m_images
[nPage
] )
208 // if the item didn't have an icon before or, on the contrary, did have
209 // it but has lost it now, its size will change - but if the icon just
211 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
212 m_images
[nPage
] = nImage
;
214 if ( tabSizeChanges
)
223 wxNotebook::~wxNotebook()
227 // ----------------------------------------------------------------------------
228 // wxNotebook page switching
229 // ----------------------------------------------------------------------------
231 int wxNotebook::SetSelection(size_t nPage
)
233 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
235 if ( (size_t)nPage
== m_sel
)
237 // don't do anything if there is nothing to do
242 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
243 event
.SetSelection(nPage
);
244 event
.SetOldSelection(m_sel
);
245 event
.SetEventObject(this);
246 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
248 // program doesn't allow the page change
252 // we need to change m_sel first, before calling RefreshTab() below as
253 // otherwise the previously selected tab wouldn't be redrawn properly under
254 // wxGTK which calls Refresh() immediately and not during the next event
255 // loop iteration as wxMSW does and as it should
256 size_t selOld
= m_sel
;
260 if ( selOld
!= INVALID_PAGE
)
262 RefreshTab(selOld
, TRUE
/* this tab was selected */);
264 m_pages
[selOld
]->Hide();
267 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
272 m_spinbtn
->SetValue(m_sel
);
275 if ( m_sel
< m_firstVisible
)
277 // selection is to the left of visible part of tabs
280 else if ( m_sel
> m_lastFullyVisible
)
282 // selection is to the right of visible part of tabs
285 else // we already see this tab
291 m_pages
[m_sel
]->SetSize(GetPageRect());
292 m_pages
[m_sel
]->Show();
296 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
297 GetEventHandler()->ProcessEvent(event
);
302 // ----------------------------------------------------------------------------
303 // wxNotebook pages adding/deleting
304 // ----------------------------------------------------------------------------
306 bool wxNotebook::InsertPage(size_t nPage
,
307 wxNotebookPage
*pPage
,
308 const wxString
& strText
,
312 size_t nPages
= GetPageCount();
313 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), FALSE
,
314 _T("invalid notebook page in InsertPage()") );
317 m_pages
.Insert(pPage
, nPage
);
320 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
321 m_titles
.Insert(label
, nPage
);
323 m_images
.Insert(imageId
, nPage
);
325 // cache the tab geometry here
326 wxSize sizeTab
= CalcTabSize(nPage
);
328 if ( sizeTab
.y
> m_heightTab
)
329 m_heightTab
= sizeTab
.y
;
331 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
332 m_widthMax
= sizeTab
.x
;
334 m_widths
.Insert(sizeTab
.x
, nPage
);
336 // spin button may appear if we didn't have it before - but even if we did,
337 // its range should change, so update it unconditionally
340 // if the tab has just appeared, we have to relayout everything, otherwise
341 // it's enough to just redraw the tabs
344 // always select the first tab to have at least some selection
350 else // not the first tab
359 else // pages added to the notebook are initially hidden
367 bool wxNotebook::DeleteAllPages()
369 if ( !wxNotebookBase::DeleteAllPages() )
372 // clear the other arrays as well
378 // it is not valid any longer
379 m_sel
= INVALID_PAGE
;
381 // spin button is not needed any more
389 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
391 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
393 wxNotebookPage
*page
= m_pages
[nPage
];
394 m_pages
.RemoveAt(nPage
);
395 m_titles
.RemoveAt(nPage
);
396 m_accels
.RemoveAt(nPage
);
397 m_widths
.RemoveAt(nPage
);
398 m_images
.RemoveAt(nPage
);
400 // the spin button might not be needed any more
401 // 2002-08-12 'if' commented out by JACS on behalf
402 // of Hans Van Leemputten <Hansvl@softhome.net> who
403 // points out that UpdateSpinBtn should always be called,
404 // to ensure m_lastVisible is up to date.
405 // if ( HasSpinBtn() )
410 size_t count
= GetPageCount();
413 if ( m_sel
== (size_t)nPage
)
415 // avoid sending event to this page which doesn't exist in the
417 m_sel
= INVALID_PAGE
;
419 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
421 else if ( m_sel
> (size_t)nPage
)
423 // no need to change selection, just adjust the index
427 else // no more tabs left
429 m_sel
= INVALID_PAGE
;
432 // have to refresh everything
438 // ----------------------------------------------------------------------------
439 // wxNotebook drawing
440 // ----------------------------------------------------------------------------
442 void wxNotebook::RefreshCurrent()
444 if ( m_sel
!= INVALID_PAGE
)
450 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
452 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
454 wxRect rect
= GetTabRect(page
);
455 if ( forceSelected
|| ((size_t)page
== m_sel
) )
457 const wxSize indent
= GetRenderer()->GetTabIndent();
458 rect
.Inflate(indent
.x
, indent
.y
);
464 void wxNotebook::RefreshAllTabs()
466 wxRect rect
= GetAllTabsRect();
467 if ( rect
.width
|| rect
.height
)
471 //else: we don't have tabs at all
474 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
479 int image
= m_images
[n
];
481 // Not needed now that wxGenericImageList is being
482 // used for wxUniversal under MSW
483 #if 0 // def __WXMSW__ // FIXME
485 m_imageList
->GetSize(n
, w
, h
);
488 dc
.SelectObject(bmp
);
489 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
490 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, TRUE
);
491 dc
.SelectObject(wxNullBitmap
);
493 bmp
= *m_imageList
->GetBitmap(image
);
500 flags
|= wxCONTROL_SELECTED
;
503 flags
|= wxCONTROL_FOCUSED
;
506 GetRenderer()->DrawTab
518 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
520 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
522 wxDC
& dc
= renderer
->GetDC();
523 dc
.SetFont(GetFont());
524 dc
.SetTextForeground(GetForegroundColour());
526 // redraw the border - it's simpler to always do it instead of checking
527 // whether this needs to be done
528 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
530 // avoid overwriting the spin button
533 wxRect rectTabs
= GetAllTabsRect();
534 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
538 rectTabs
.height
-= sizeSpinBtn
.y
;
540 // Allow for erasing the line under selected tab
545 rectTabs
.width
-= sizeSpinBtn
.x
;
547 // Allow for erasing the line under selected tab
548 rectTabs
.height
+= 2;
551 dc
.SetClippingRegion(rectTabs
);
554 wxRect rect
= GetTabsPart();
555 bool isVertical
= IsVertical();
558 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
560 GetTabSize(n
, &rect
.width
, &rect
.height
);
564 // don't redraw it now as this tab has to be drawn over the other
565 // ones as it takes more place and spills over to them
568 else // not selected tab
570 // unfortunately we can't do this because the selected tab hangs
571 // over its neighbours and so we might need to refresh more tabs -
572 // of course, we could still avoid rereshing some of them with more
573 // complicated checks, but it doesn't seem too bad to refresh all
574 // of them, I still don't see flicker, so leaving as is for now
576 //if ( rectUpdate.Intersects(rect) )
578 DoDrawTab(dc
, rect
, n
);
580 //else: doesn't need to be refreshed
583 // move the rect to the next tab
585 rect
.y
+= rect
.height
;
587 rect
.x
+= rect
.width
;
590 // now redraw the selected tab
593 DoDrawTab(dc
, rectSel
, m_sel
);
596 dc
.DestroyClippingRegion();
599 // ----------------------------------------------------------------------------
600 // wxNotebook geometry
601 // ----------------------------------------------------------------------------
603 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
606 *flags
= wxNB_HITTEST_NOWHERE
;
608 // first check that it is in this window at all
609 if ( !GetClientRect().Inside(pt
) )
614 wxRect rectTabs
= GetAllTabsRect();
616 switch ( GetTabOrientation() )
619 wxFAIL_MSG(_T("unknown tab orientation"));
623 if ( pt
.y
> rectTabs
.GetBottom() )
628 if ( pt
.y
< rectTabs
.y
)
633 if ( pt
.x
> rectTabs
.GetRight() )
638 if ( pt
.x
< rectTabs
.x
)
643 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
645 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
647 if ( rectTabs
.Inside(pt
) )
651 // TODO: be more precise
652 *flags
= wxNB_HITTEST_ONITEM
;
658 // move the rectTabs to the next tab
660 rectTabs
.y
+= rectTabs
.height
;
662 rectTabs
.x
+= rectTabs
.width
;
668 bool wxNotebook::IsVertical() const
670 wxDirection dir
= GetTabOrientation();
672 return dir
== wxLEFT
|| dir
== wxRIGHT
;
675 wxDirection
wxNotebook::GetTabOrientation() const
677 long style
= GetWindowStyle();
678 if ( style
& wxNB_BOTTOM
)
680 else if ( style
& wxNB_RIGHT
)
682 else if ( style
& wxNB_LEFT
)
685 // wxNB_TOP == 0 so we don't have to test for it
689 wxRect
wxNotebook::GetTabRect(int page
) const
692 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
694 // calc the size of this tab and of the preceding ones
695 wxCoord widthThis
, widthBefore
;
696 if ( FixedSizeTabs() )
698 widthThis
= m_widthMax
;
699 widthBefore
= page
*m_widthMax
;
704 for ( int n
= 0; n
< page
; n
++ )
706 widthBefore
+= m_widths
[n
];
709 widthThis
= m_widths
[page
];
712 rect
= GetTabsPart();
715 rect
.y
+= widthBefore
- m_offset
;
716 rect
.height
= widthThis
;
720 rect
.x
+= widthBefore
- m_offset
;
721 rect
.width
= widthThis
;
727 wxRect
wxNotebook::GetAllTabsRect() const
731 if ( GetPageCount() )
733 const wxSize indent
= GetRenderer()->GetTabIndent();
734 wxSize size
= GetClientSize();
738 rect
.width
= m_heightTab
+ indent
.x
;
739 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
741 rect
.height
= size
.y
;
747 rect
.height
= m_heightTab
+ indent
.y
;
748 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
756 wxRect
wxNotebook::GetTabsPart() const
758 wxRect rect
= GetAllTabsRect();
760 wxDirection dir
= GetTabOrientation();
762 const wxSize indent
= GetRenderer()->GetTabIndent();
774 rect
.height
-= indent
.y
;
778 rect
.height
-= indent
.y
;
785 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
787 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
791 // width and height have inverted meaning
797 // height is always fixed
800 // width may also be fixed and be the same for all tabs
801 *w
= GetTabWidth(page
);
804 void wxNotebook::SetTabSize(const wxSize
& sz
)
806 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
820 wxSize
wxNotebook::CalcTabSize(int page
) const
822 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
823 // method is called to calculate them
827 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
829 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
831 if ( HasImage(page
) )
834 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
836 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
838 if ( sizeImage
.y
> size
.y
)
839 size
.y
= sizeImage
.y
;
842 size
.x
+= 2*m_sizePad
.x
;
843 size
.y
+= 2*m_sizePad
.y
;
848 void wxNotebook::ResizeTab(int page
)
850 wxSize sizeTab
= CalcTabSize(page
);
852 // we only need full relayout if the page size changes
853 bool needsRelayout
= FALSE
;
858 wxCoord tmp
= sizeTab
.x
;
859 sizeTab
.x
= sizeTab
.y
;
863 if ( sizeTab
.y
> m_heightTab
)
865 needsRelayout
= TRUE
;
867 m_heightTab
= sizeTab
.y
;
870 m_widths
[page
] = sizeTab
.x
;
872 if ( sizeTab
.x
> m_widthMax
)
873 m_widthMax
= sizeTab
.x
;
875 // the total of the tabs has changed too
884 void wxNotebook::SetPadding(const wxSize
& padding
)
886 if ( padding
!= m_sizePad
)
894 void wxNotebook::Relayout()
896 if ( GetPageCount() )
902 if ( m_sel
!= INVALID_PAGE
)
904 // resize the currently shown page
905 wxRect rectPage
= GetPageRect();
907 m_pages
[m_sel
]->SetSize(rectPage
);
909 // also scroll it into view if needed (note that m_lastVisible
910 // was updated by the call to UpdateSpinBtn() above, this is why it
914 if ( m_sel
< m_firstVisible
)
916 // selection is to the left of visible part of tabs
919 else if ( m_sel
> m_lastFullyVisible
)
921 // selection is to the right of visible part of tabs
927 else // we have no pages
929 // just refresh everything
934 wxRect
wxNotebook::GetPagePart() const
936 wxRect rectPage
= GetClientRect();
938 if ( GetPageCount() )
940 wxRect rectTabs
= GetAllTabsRect();
941 wxDirection dir
= GetTabOrientation();
944 rectPage
.width
-= rectTabs
.width
;
946 rectPage
.x
+= rectTabs
.width
;
950 rectPage
.height
-= rectTabs
.height
;
952 rectPage
.y
+= rectTabs
.height
;
955 //else: no pages at all
960 wxRect
wxNotebook::GetPageRect() const
962 wxRect rect
= GetPagePart();
964 // leave space for the border
965 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
967 // FIXME: hardcoded +2!
968 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
969 -(rectBorder
.y
+ rectBorder
.height
+ 2));
974 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
976 wxSize sizeNb
= size
;
977 wxRect rect
= GetAllTabsRect();
979 sizeNb
.x
+= rect
.width
;
981 sizeNb
.y
+= rect
.height
;
986 void wxNotebook::SetPageSize(const wxSize
& size
)
988 SetClientSize(GetSizeForPage(size
));
991 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
993 return AdjustSize(GetSizeForPage(sizePage
));
996 // ----------------------------------------------------------------------------
997 // wxNotebook spin button
998 // ----------------------------------------------------------------------------
1000 bool wxNotebook::HasSpinBtn() const
1002 return m_spinbtn
&& m_spinbtn
->IsShown();
1005 void wxNotebook::CalcLastVisibleTab()
1007 bool isVertical
= IsVertical();
1009 wxCoord width
= GetClientSize().x
;
1011 wxRect rect
= GetTabsPart();
1013 size_t count
= GetPageCount();
1015 wxCoord widthLast
= 0;
1017 for ( n
= m_firstVisible
; n
< count
; n
++ )
1019 GetTabSize(n
, &rect
.width
, &rect
.height
);
1020 if ( rect
.GetRight() > width
)
1025 // remember it to use below
1026 widthLast
= rect
.GetRight();
1028 // move the rect to the next tab
1030 rect
.y
+= rect
.height
;
1032 rect
.x
+= rect
.width
;
1035 if ( n
== m_firstVisible
)
1037 // even the first tab isn't fully visible - but still pretend it is as
1038 // we have to show something
1039 m_lastFullyVisible
= m_firstVisible
;
1041 else // more than one tab visible
1043 m_lastFullyVisible
= n
- 1;
1045 // but is it really fully visible? it shouldn't overlap with the spin
1046 // button if it is present (again, even if the first button does
1047 // overlap with it, we pretend that it doesn't as there is not much
1049 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1051 // adjust width to be the width before the spin button
1052 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1054 width
-= sizeSpinBtn
.y
;
1056 width
-= sizeSpinBtn
.x
;
1058 if ( widthLast
> width
)
1060 // the last button overlaps with spin button, so take he
1062 m_lastFullyVisible
--;
1069 // everything is visible
1074 // this tab is still (partially) visible, so m_lastVisible is the
1075 // next tab (remember, this is "exclusive" last)
1076 m_lastVisible
= n
+ 1;
1081 void wxNotebook::UpdateSpinBtn()
1083 // first decide if we need a spin button
1086 size_t count
= (size_t)GetPageCount();
1089 // this case is special, get rid of it immediately: everything is
1090 // visible and we don't need any spin buttons
1091 allTabsShown
= TRUE
;
1093 // have to reset them manually as we don't call CalcLastVisibleTab()
1096 m_lastFullyVisible
= 0;
1100 CalcLastVisibleTab();
1102 // if all tabs after the first visible one are shown, it doesn't yet
1103 // mean that all tabs are shown - so we go backwards until we arrive to
1104 // the beginning (then all tabs are indeed shown) or find a tab such
1105 // that not all tabs after it are shown
1106 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1108 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1110 m_offset
-= GetTabWidth(m_firstVisible
--);
1112 // reclaculate after m_firstVisible change
1113 CalcLastVisibleTab();
1116 allTabsShown
= m_lastFullyVisible
== count
- 1;
1119 if ( !allTabsShown
)
1123 // create it once only
1124 m_spinbtn
= new wxNotebookSpinBtn(this);
1126 // set the correct value to keep it in sync
1127 m_spinbtn
->SetValue(m_sel
);
1130 // position it correctly
1136 // also set/update the range
1137 m_spinbtn
->SetRange(0, count
- 1);
1139 // update m_lastFullyVisible once again as it might have changed
1140 // because the spin button appeared
1142 // FIXME: might do it more efficiently
1143 CalcLastVisibleTab();
1145 else // all tabs are visible, we don't need spin button
1154 void wxNotebook::PositionSpinBtn()
1160 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1162 wxRect rectTabs
= GetAllTabsRect();
1165 switch ( GetTabOrientation() )
1168 wxFAIL_MSG(_T("unknown tab orientation"));
1172 x
= rectTabs
.GetRight() - wBtn
;
1173 y
= rectTabs
.GetBottom() - hBtn
;
1177 x
= rectTabs
.GetRight() - wBtn
;
1178 y
= rectTabs
.GetTop();
1182 x
= rectTabs
.GetRight() - wBtn
;
1183 y
= rectTabs
.GetBottom() - hBtn
;
1187 x
= rectTabs
.GetLeft();
1188 y
= rectTabs
.GetBottom() - hBtn
;
1192 m_spinbtn
->Move(x
, y
);
1195 // ----------------------------------------------------------------------------
1196 // wxNotebook scrolling
1197 // ----------------------------------------------------------------------------
1199 void wxNotebook::ScrollTo(int page
)
1201 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1203 // set the first visible tab and offset (easy)
1204 m_firstVisible
= (size_t)page
;
1206 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1208 m_offset
+= GetTabWidth(n
);
1211 // find the last visible tab too
1212 CalcLastVisibleTab();
1217 void wxNotebook::ScrollLastTo(int page
)
1219 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1221 // go backwards until we find the first tab which can be made visible
1222 // without hiding the given one
1223 wxSize size
= GetClientSize();
1224 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1225 widthTabs
= GetTabWidth(page
);
1227 // the total width is less than the width of the window if we have the spin
1231 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1233 widthAll
-= sizeSpinBtn
.y
;
1235 widthAll
-= sizeSpinBtn
.x
;
1238 m_firstVisible
= page
;
1239 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1241 widthTabs
+= GetTabWidth(--m_firstVisible
);
1244 if ( widthTabs
> widthAll
)
1246 // take one step back (that it is forward) if we can
1247 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1252 ScrollTo(m_firstVisible
);
1254 // consitency check: the page we were asked to show should be shown
1255 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1258 // ----------------------------------------------------------------------------
1259 // wxNotebook sizing/moving
1260 // ----------------------------------------------------------------------------
1262 wxSize
wxNotebook::DoGetBestClientSize() const
1264 // calculate the max page size
1267 size_t count
= GetPageCount();
1270 for ( size_t n
= 0; n
< count
; n
++ )
1272 wxSize sizePage
= m_pages
[n
]->GetSize();
1274 if ( size
.x
< sizePage
.x
)
1275 size
.x
= sizePage
.x
;
1276 if ( size
.y
< sizePage
.y
)
1277 size
.y
= sizePage
.y
;
1282 // use some arbitrary default size
1287 return GetSizeForPage(size
);
1290 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1292 wxControl::DoMoveWindow(x
, y
, width
, height
);
1294 // move the spin ctrl too (NOP if it doesn't exist)
1298 void wxNotebook::DoSetSize(int x
, int y
,
1299 int width
, int height
,
1302 wxSize old_client_size
= GetClientSize();
1304 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1306 wxSize new_client_size
= GetClientSize();
1308 if (old_client_size
!= new_client_size
)
1312 // ----------------------------------------------------------------------------
1313 // wxNotebook input processing
1314 // ----------------------------------------------------------------------------
1316 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1318 const wxString
& strArg
)
1320 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1321 SetSelection(GetNextPage(TRUE
));
1322 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1323 SetSelection(GetNextPage(FALSE
));
1324 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1325 SetSelection((int)numArg
);
1327 return wxControl::PerformAction(action
, numArg
, strArg
);
1332 // ----------------------------------------------------------------------------
1333 // wxStdNotebookInputHandler
1334 // ----------------------------------------------------------------------------
1336 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1337 : wxStdInputHandler(inphand
)
1341 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1342 const wxKeyEvent
& event
,
1345 // ignore the key releases
1348 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1351 wxControlAction action
;
1352 switch ( event
.GetKeyCode() )
1355 if ( notebook
->IsVertical() )
1356 action
= wxACTION_NOTEBOOK_PREV
;
1360 if ( !notebook
->IsVertical() )
1361 action
= wxACTION_NOTEBOOK_PREV
;
1365 if ( notebook
->IsVertical() )
1366 action
= wxACTION_NOTEBOOK_NEXT
;
1370 if ( !notebook
->IsVertical() )
1371 action
= wxACTION_NOTEBOOK_NEXT
;
1375 action
= wxACTION_NOTEBOOK_GOTO
;
1376 // page = 0; -- already has this value
1380 action
= wxACTION_NOTEBOOK_GOTO
;
1381 page
= notebook
->GetPageCount() - 1;
1387 return consumer
->PerformAction(action
, page
);
1391 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1394 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1395 const wxMouseEvent
& event
)
1397 if ( event
.ButtonDown(1) )
1399 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1400 int page
= notebook
->HitTest(event
.GetPosition());
1403 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1409 return wxStdInputHandler::HandleMouse(consumer
, event
);
1412 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1413 const wxMouseEvent
& event
)
1415 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1419 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1420 const wxFocusEvent
& WXUNUSED(event
))
1422 HandleFocusChange(consumer
);
1427 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1428 bool WXUNUSED(activated
))
1430 // we react to the focus change in the same way as to the [de]activation
1431 HandleFocusChange(consumer
);
1436 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1438 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1439 notebook
->RefreshCurrent();
1442 #endif // wxUSE_NOTEBOOK