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 // ----------------------------------------------------------------------------
21 #pragma implementation "univnotebook.h"
24 #include "wx/wxprec.h"
32 #include "wx/imaglist.h"
33 #include "wx/notebook.h"
34 #include "wx/spinbutt.h"
35 #include "wx/dcmemory.h"
37 #include "wx/univ/renderer.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 static const size_t INVALID_PAGE
= (size_t)-1;
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 class wxNotebookSpinBtn
: public wxSpinButton
61 wxNotebookSpinBtn(wxNotebook
*nb
)
62 : wxSpinButton(nb
, -1,
63 wxDefaultPosition
, wxDefaultSize
,
64 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
70 void OnSpin(wxSpinEvent
& event
)
72 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
81 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
82 EVT_SPIN(-1, wxNotebookSpinBtn::OnSpin
)
85 // ============================================================================
87 // ============================================================================
89 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
90 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
92 // ----------------------------------------------------------------------------
93 // wxNotebook creation
94 // ----------------------------------------------------------------------------
96 void wxNotebook::Init()
105 m_lastFullyVisible
= 0;
112 bool wxNotebook::Create(wxWindow
*parent
,
117 const wxString
& name
)
119 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
120 wxDefaultValidator
, name
) )
123 m_sizePad
= GetRenderer()->GetTabPadding();
127 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
132 // ----------------------------------------------------------------------------
133 // wxNotebook page titles and images
134 // ----------------------------------------------------------------------------
136 wxString
wxNotebook::GetPageText(int nPage
) const
138 wxCHECK_MSG( IS_VALID_PAGE(nPage
), _T(""), _T("invalid notebook page") );
140 return m_titles
[nPage
];
143 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
145 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
147 if ( strText
!= m_titles
[nPage
] )
149 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
151 if ( FixedSizeTabs() )
153 // it's enough to just reresh this one
156 else // var width tabs
158 // we need to resize the tab to fit the new string
166 int wxNotebook::GetPageImage(int nPage
) const
168 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
170 return m_images
[nPage
];
173 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
175 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("invalid notebook page") );
177 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), FALSE
,
178 _T("invalid image index in SetPageImage()") );
180 if ( nImage
!= m_images
[nPage
] )
182 // if the item didn't have an icon before or, on the contrary, did have
183 // it but has lost it now, its size will change - but if the icon just
185 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
186 m_images
[nPage
] = nImage
;
188 if ( tabSizeChanges
)
197 wxNotebook::~wxNotebook()
201 // ----------------------------------------------------------------------------
202 // wxNotebook page switching
203 // ----------------------------------------------------------------------------
205 int wxNotebook::SetSelection(int nPage
)
207 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("invalid notebook page") );
209 if ( (size_t)nPage
== m_sel
)
211 // don't do anything if there is nothing to do
215 // we need to change m_sel first, before calling RefreshTab() below as
216 // otherwise the previously selected tab wouldn't be redrawn properly under
217 // wxGTK which calls Refresh() immediately and not during the next event
218 // loop iteration as wxMSW does and as it should
219 size_t selOld
= m_sel
;
223 if ( selOld
!= INVALID_PAGE
)
225 RefreshTab(selOld
, TRUE
/* this tab was selected */);
227 m_pages
[selOld
]->Hide();
230 if ( m_sel
!= INVALID_PAGE
) // this is impossible - but test nevertheless
235 m_spinbtn
->SetValue(m_sel
);
238 if ( m_sel
< m_firstVisible
)
240 // selection is to the left of visible part of tabs
243 else if ( m_sel
> m_lastFullyVisible
)
245 // selection is to the right of visible part of tabs
248 else // we already see this tab
254 m_pages
[m_sel
]->SetSize(GetPageRect());
255 m_pages
[m_sel
]->Show();
261 void wxNotebook::ChangePage(int nPage
)
263 wxCHECK_RET( IS_VALID_PAGE(nPage
), _T("invalid notebook page") );
265 if ( (size_t)nPage
== m_sel
)
271 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
272 event
.SetSelection(nPage
);
273 event
.SetOldSelection(m_sel
);
274 event
.SetEventObject(this);
275 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
277 // program doesn't allow the page change
283 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
284 GetEventHandler()->ProcessEvent(event
);
287 // ----------------------------------------------------------------------------
288 // wxNotebook pages adding/deleting
289 // ----------------------------------------------------------------------------
291 bool wxNotebook::InsertPage(int nPage
,
292 wxNotebookPage
*pPage
,
293 const wxString
& strText
,
297 int nPages
= GetPageCount();
298 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), FALSE
,
299 _T("invalid notebook page in InsertPage()") );
302 m_pages
.Insert(pPage
, nPage
);
305 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
306 m_titles
.Insert(label
, nPage
);
308 m_images
.Insert(imageId
, nPage
);
310 // cache the tab geometry here
311 wxSize sizeTab
= CalcTabSize(nPage
);
313 if ( sizeTab
.y
> m_heightTab
)
314 m_heightTab
= sizeTab
.y
;
316 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
317 m_widthMax
= sizeTab
.x
;
319 m_widths
.Insert(sizeTab
.x
, nPage
);
321 // spin button may appear if we didn't have it before - but even if we did,
322 // its range should change, so update it unconditionally
325 // if the tab has just appeared, we have to relayout everything, otherwise
326 // it's enough to just redraw the tabs
329 // always select the first tab to have at least some selection
334 else // not the first tab
343 else // pages added to the notebook are initially hidden
351 bool wxNotebook::DeleteAllPages()
353 if ( !wxNotebookBase::DeleteAllPages() )
356 // clear the other arrays as well
362 // it is not valid any longer
363 m_sel
= INVALID_PAGE
;
365 // spin button is not needed any more
373 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
375 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
377 wxNotebookPage
*page
= m_pages
[nPage
];
378 m_pages
.RemoveAt(nPage
);
379 m_titles
.RemoveAt(nPage
);
380 m_accels
.RemoveAt(nPage
);
381 m_widths
.RemoveAt(nPage
);
382 m_images
.RemoveAt(nPage
);
384 // the spin button might not be needed any more
385 // 2002-08-12 'if' commented out by JACS on behalf
386 // of Hans Van Leemputten <Hansvl@softhome.net> who
387 // points out that UpdateSpinBtn should always be called,
388 // to ensure m_lastVisible is up to date.
389 // if ( HasSpinBtn() )
394 int count
= GetPageCount();
397 if ( m_sel
== (size_t)nPage
)
399 // avoid sending event to this page which doesn't exist in the
401 m_sel
= INVALID_PAGE
;
403 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
405 else if ( m_sel
> (size_t)nPage
)
407 // no need to change selection, just adjust the index
411 else // no more tabs left
413 m_sel
= INVALID_PAGE
;
416 // have to refresh everything
422 // ----------------------------------------------------------------------------
423 // wxNotebook drawing
424 // ----------------------------------------------------------------------------
426 void wxNotebook::RefreshCurrent()
428 if ( m_sel
!= INVALID_PAGE
)
434 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
436 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
438 wxRect rect
= GetTabRect(page
);
439 if ( forceSelected
|| ((size_t)page
== m_sel
) )
441 const wxSize indent
= GetRenderer()->GetTabIndent();
442 rect
.Inflate(indent
.x
, indent
.y
);
448 void wxNotebook::RefreshAllTabs()
450 wxRect rect
= GetAllTabsRect();
451 if ( rect
.width
|| rect
.height
)
455 //else: we don't have tabs at all
458 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
463 int image
= m_images
[n
];
465 #ifdef __WXMSW__ // FIXME
467 m_imageList
->GetSize(n
, w
, h
);
470 dc
.SelectObject(bmp
);
471 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
472 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, TRUE
);
473 dc
.SelectObject(wxNullBitmap
);
475 bmp
= *m_imageList
->GetBitmap(image
);
482 flags
|= wxCONTROL_SELECTED
;
485 flags
|= wxCONTROL_FOCUSED
;
488 GetRenderer()->DrawTab
500 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
502 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
504 wxDC
& dc
= renderer
->GetDC();
505 dc
.SetFont(GetFont());
506 dc
.SetTextForeground(GetForegroundColour());
508 // redraw the border - it's simpler to always do it instead of checking
509 // whether this needs to be done
510 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
512 // avoid overwriting the spin button
515 wxRect rectTabs
= GetAllTabsRect();
516 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
519 rectTabs
.height
-= sizeSpinBtn
.y
;
521 rectTabs
.width
-= sizeSpinBtn
.x
;
523 dc
.SetClippingRegion(rectTabs
);
526 wxRect rect
= GetTabsPart();
527 bool isVertical
= IsVertical();
530 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
532 GetTabSize(n
, &rect
.width
, &rect
.height
);
536 // don't redraw it now as this tab has to be drawn over the other
537 // ones as it takes more place and spills over to them
540 else // not selected tab
542 // unfortunately we can't do this because the selected tab hangs
543 // over its neighbours and so we might need to refresh more tabs -
544 // of course, we could still avoid rereshing some of them with more
545 // complicated checks, but it doesn't seem too bad to refresh all
546 // of them, I still don't see flicker, so leaving as is for now
548 //if ( rectUpdate.Intersects(rect) )
550 DoDrawTab(dc
, rect
, n
);
552 //else: doesn't need to be refreshed
555 // move the rect to the next tab
557 rect
.y
+= rect
.height
;
559 rect
.x
+= rect
.width
;
562 // now redraw the selected tab
565 DoDrawTab(dc
, rectSel
, m_sel
);
569 // ----------------------------------------------------------------------------
570 // wxNotebook geometry
571 // ----------------------------------------------------------------------------
573 int wxNotebook::HitTest(const wxPoint
& pt
) const
575 // first check that it is in this window at all
576 if ( !GetClientRect().Inside(pt
) )
581 wxRect rectTabs
= GetAllTabsRect();
583 switch ( GetTabOrientation() )
586 wxFAIL_MSG(_T("unknown tab orientation"));
590 if ( pt
.y
> rectTabs
.GetBottom() )
595 if ( pt
.y
< rectTabs
.y
)
600 if ( pt
.x
> rectTabs
.GetRight() )
605 if ( pt
.x
< rectTabs
.x
)
610 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
612 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
614 if ( rectTabs
.Inside(pt
) )
617 // move the rectTabs to the next tab
619 rectTabs
.y
+= rectTabs
.height
;
621 rectTabs
.x
+= rectTabs
.width
;
627 bool wxNotebook::IsVertical() const
629 wxDirection dir
= GetTabOrientation();
631 return dir
== wxLEFT
|| dir
== wxRIGHT
;
634 wxDirection
wxNotebook::GetTabOrientation() const
636 long style
= GetWindowStyle();
637 if ( style
& wxNB_BOTTOM
)
639 else if ( style
& wxNB_RIGHT
)
641 else if ( style
& wxNB_LEFT
)
644 // wxNB_TOP == 0 so we don't have to test for it
648 wxRect
wxNotebook::GetTabRect(int page
) const
651 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
653 // calc the size of this tab and of the preceding ones
654 wxCoord widthThis
, widthBefore
;
655 if ( FixedSizeTabs() )
657 widthThis
= m_widthMax
;
658 widthBefore
= page
*m_widthMax
;
663 for ( int n
= 0; n
< page
; n
++ )
665 widthBefore
+= m_widths
[n
];
668 widthThis
= m_widths
[page
];
671 rect
= GetTabsPart();
674 rect
.y
+= widthBefore
- m_offset
;
675 rect
.height
= widthThis
;
679 rect
.x
+= widthBefore
- m_offset
;
680 rect
.width
= widthThis
;
686 wxRect
wxNotebook::GetAllTabsRect() const
690 if ( GetPageCount() )
692 const wxSize indent
= GetRenderer()->GetTabIndent();
693 wxSize size
= GetClientSize();
697 rect
.width
= m_heightTab
+ indent
.x
;
698 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
700 rect
.height
= size
.y
;
706 rect
.height
= m_heightTab
+ indent
.y
;
707 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
715 wxRect
wxNotebook::GetTabsPart() const
717 wxRect rect
= GetAllTabsRect();
719 wxDirection dir
= GetTabOrientation();
721 const wxSize indent
= GetRenderer()->GetTabIndent();
733 rect
.height
-= indent
.y
;
737 rect
.height
-= indent
.y
;
744 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
746 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
750 // width and height have inverted meaning
756 // height is always fixed
759 // width may also be fixed and be the same for all tabs
760 *w
= GetTabWidth(page
);
763 void wxNotebook::SetTabSize(const wxSize
& sz
)
765 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
779 wxSize
wxNotebook::CalcTabSize(int page
) const
781 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
782 // method is called to calculate them
786 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
788 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
790 if ( HasImage(page
) )
793 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
795 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
797 if ( sizeImage
.y
> size
.y
)
798 size
.y
= sizeImage
.y
;
801 size
.x
+= 2*m_sizePad
.x
;
802 size
.y
+= 2*m_sizePad
.y
;
807 void wxNotebook::ResizeTab(int page
)
809 wxSize sizeTab
= CalcTabSize(page
);
811 // we only need full relayout if the page size changes
812 bool needsRelayout
= FALSE
;
817 wxCoord tmp
= sizeTab
.x
;
818 sizeTab
.x
= sizeTab
.y
;
822 if ( sizeTab
.y
> m_heightTab
)
824 needsRelayout
= TRUE
;
826 m_heightTab
= sizeTab
.y
;
829 m_widths
[page
] = sizeTab
.x
;
831 if ( sizeTab
.x
> m_widthMax
)
832 m_widthMax
= sizeTab
.x
;
834 // the total of the tabs has changed too
843 void wxNotebook::SetPadding(const wxSize
& padding
)
845 if ( padding
!= m_sizePad
)
853 void wxNotebook::Relayout()
855 if ( GetPageCount() )
861 if ( m_sel
!= INVALID_PAGE
)
863 // resize the currently shown page
864 wxRect rectPage
= GetPageRect();
866 m_pages
[m_sel
]->SetSize(rectPage
);
868 // also scroll it into view if needed (note that m_lastVisible
869 // was updated by the call to UpdateSpinBtn() above, this is why it
873 if ( m_sel
< m_firstVisible
)
875 // selection is to the left of visible part of tabs
878 else if ( m_sel
> m_lastFullyVisible
)
880 // selection is to the right of visible part of tabs
886 else // we have no pages
888 // just refresh everything
893 wxRect
wxNotebook::GetPagePart() const
895 wxRect rectPage
= GetClientRect();
897 if ( GetPageCount() )
899 wxRect rectTabs
= GetAllTabsRect();
900 wxDirection dir
= GetTabOrientation();
903 rectPage
.width
-= rectTabs
.width
;
905 rectPage
.x
+= rectTabs
.width
;
909 rectPage
.height
-= rectTabs
.height
;
911 rectPage
.y
+= rectTabs
.height
;
914 //else: no pages at all
919 wxRect
wxNotebook::GetPageRect() const
921 wxRect rect
= GetPagePart();
923 // leave space for the border
924 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
926 // FIXME: hardcoded +2!
927 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
928 -(rectBorder
.y
+ rectBorder
.height
+ 2));
933 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
935 wxSize sizeNb
= size
;
936 wxRect rect
= GetAllTabsRect();
938 sizeNb
.x
+= rect
.width
;
940 sizeNb
.y
+= rect
.height
;
945 void wxNotebook::SetPageSize(const wxSize
& size
)
947 SetClientSize(GetSizeForPage(size
));
950 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
)
952 return AdjustSize(GetSizeForPage(sizePage
));
955 // ----------------------------------------------------------------------------
956 // wxNotebook spin button
957 // ----------------------------------------------------------------------------
959 bool wxNotebook::HasSpinBtn() const
961 return m_spinbtn
&& m_spinbtn
->IsShown();
964 void wxNotebook::CalcLastVisibleTab()
966 bool isVertical
= IsVertical();
968 wxCoord width
= GetClientSize().x
;
970 wxRect rect
= GetTabsPart();
972 size_t count
= GetPageCount();
974 wxCoord widthLast
= 0;
976 for ( n
= m_firstVisible
; n
< count
; n
++ )
978 GetTabSize(n
, &rect
.width
, &rect
.height
);
979 if ( rect
.GetRight() > width
)
984 // remember it to use below
985 widthLast
= rect
.GetRight();
987 // move the rect to the next tab
989 rect
.y
+= rect
.height
;
991 rect
.x
+= rect
.width
;
994 if ( n
== m_firstVisible
)
996 // even the first tab isn't fully visible - but still pretend it is as
997 // we have to show something
998 m_lastFullyVisible
= m_firstVisible
;
1000 else // more than one tab visible
1002 m_lastFullyVisible
= n
- 1;
1004 // but is it really fully visible? it shouldn't overlap with the spin
1005 // button if it is present (again, even if the first button does
1006 // overlap with it, we pretend that it doesn't as there is not much
1008 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1010 // adjust width to be the width before the spin button
1011 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1013 width
-= sizeSpinBtn
.y
;
1015 width
-= sizeSpinBtn
.x
;
1017 if ( widthLast
> width
)
1019 // the last button overlaps with spin button, so take he
1021 m_lastFullyVisible
--;
1028 // everything is visible
1033 // this tab is still (partially) visible, so m_lastVisible is the
1034 // next tab (remember, this is "exclusive" last)
1035 m_lastVisible
= n
+ 1;
1040 void wxNotebook::UpdateSpinBtn()
1042 // first decide if we need a spin button
1045 size_t count
= (size_t)GetPageCount();
1048 // this case is special, get rid of it immediately: everything is
1049 // visible and we don't need any spin buttons
1050 allTabsShown
= TRUE
;
1052 // have to reset them manually as we don't call CalcLastVisibleTab()
1055 m_lastFullyVisible
= 0;
1059 CalcLastVisibleTab();
1061 // if all tabs after the first visible one are shown, it doesn't yet
1062 // mean that all tabs are shown - so we go backwards until we arrive to
1063 // the beginning (then all tabs are indeed shown) or find a tab such
1064 // that not all tabs after it are shown
1065 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1067 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1069 m_offset
-= GetTabWidth(m_firstVisible
--);
1071 // reclaculate after m_firstVisible change
1072 CalcLastVisibleTab();
1075 allTabsShown
= m_lastFullyVisible
== count
- 1;
1078 if ( !allTabsShown
)
1082 // create it once only
1083 m_spinbtn
= new wxNotebookSpinBtn(this);
1085 // set the correct value to keep it in sync
1086 m_spinbtn
->SetValue(m_sel
);
1089 // position it correctly
1095 // also set/update the range
1096 m_spinbtn
->SetRange(0, count
- 1);
1098 // update m_lastFullyVisible once again as it might have changed
1099 // because the spin button appeared
1101 // FIXME: might do it more efficiently
1102 CalcLastVisibleTab();
1104 else // all tabs are visible, we don't need spin button
1113 void wxNotebook::PositionSpinBtn()
1119 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1121 wxRect rectTabs
= GetAllTabsRect();
1124 switch ( GetTabOrientation() )
1127 wxFAIL_MSG(_T("unknown tab orientation"));
1131 x
= rectTabs
.GetRight() - wBtn
;
1132 y
= rectTabs
.GetBottom() - hBtn
;
1136 x
= rectTabs
.GetRight() - wBtn
;
1137 y
= rectTabs
.GetTop();
1141 x
= rectTabs
.GetRight() - wBtn
;
1142 y
= rectTabs
.GetBottom() - hBtn
;
1146 x
= rectTabs
.GetLeft();
1147 y
= rectTabs
.GetBottom() - hBtn
;
1151 m_spinbtn
->Move(x
, y
);
1154 // ----------------------------------------------------------------------------
1155 // wxNotebook scrolling
1156 // ----------------------------------------------------------------------------
1158 void wxNotebook::ScrollTo(int page
)
1160 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1162 // set the first visible tab and offset (easy)
1163 m_firstVisible
= (size_t)page
;
1165 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1167 m_offset
+= GetTabWidth(n
);
1170 // find the last visible tab too
1171 CalcLastVisibleTab();
1176 void wxNotebook::ScrollLastTo(int page
)
1178 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1180 // go backwards until we find the first tab which can be made visible
1181 // without hiding the given one
1182 wxSize size
= GetClientSize();
1183 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1184 widthTabs
= GetTabWidth(page
);
1186 // the total width is less than the width of the window if we have the spin
1190 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1192 widthAll
-= sizeSpinBtn
.y
;
1194 widthAll
-= sizeSpinBtn
.x
;
1197 m_firstVisible
= page
;
1198 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1200 widthTabs
+= GetTabWidth(--m_firstVisible
);
1203 if ( widthTabs
> widthAll
)
1205 // take one step back (that it is forward) if we can
1206 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1211 ScrollTo(m_firstVisible
);
1213 // consitency check: the page we were asked to show should be shown
1214 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1217 // ----------------------------------------------------------------------------
1218 // wxNotebook sizing/moving
1219 // ----------------------------------------------------------------------------
1221 wxSize
wxNotebook::DoGetBestClientSize() const
1223 // calculate the max page size
1226 size_t count
= GetPageCount();
1229 for ( size_t n
= 0; n
< count
; n
++ )
1231 wxSize sizePage
= m_pages
[n
]->GetSize();
1233 if ( size
.x
< sizePage
.x
)
1234 size
.x
= sizePage
.x
;
1235 if ( size
.y
< sizePage
.y
)
1236 size
.y
= sizePage
.y
;
1241 // use some arbitrary default size
1246 return GetSizeForPage(size
);
1249 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1251 wxControl::DoMoveWindow(x
, y
, width
, height
);
1253 // move the spin ctrl too (NOP if it doesn't exist)
1257 void wxNotebook::DoSetSize(int x
, int y
,
1258 int width
, int height
,
1261 wxSize old_client_size
= GetClientSize();
1263 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1265 wxSize new_client_size
= GetClientSize();
1267 if (old_client_size
!= new_client_size
)
1271 // ----------------------------------------------------------------------------
1272 // wxNotebook input processing
1273 // ----------------------------------------------------------------------------
1275 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1277 const wxString
& strArg
)
1279 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1280 ChangePage(GetNextPage(TRUE
));
1281 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1282 ChangePage(GetNextPage(FALSE
));
1283 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1284 ChangePage((int)numArg
);
1286 return wxControl::PerformAction(action
, numArg
, strArg
);
1291 // ----------------------------------------------------------------------------
1292 // wxStdNotebookInputHandler
1293 // ----------------------------------------------------------------------------
1295 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1296 : wxStdInputHandler(inphand
)
1300 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1301 const wxKeyEvent
& event
,
1304 // ignore the key releases
1307 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1310 wxControlAction action
;
1311 switch ( event
.GetKeyCode() )
1314 if ( notebook
->IsVertical() )
1315 action
= wxACTION_NOTEBOOK_PREV
;
1319 if ( !notebook
->IsVertical() )
1320 action
= wxACTION_NOTEBOOK_PREV
;
1324 if ( notebook
->IsVertical() )
1325 action
= wxACTION_NOTEBOOK_NEXT
;
1329 if ( !notebook
->IsVertical() )
1330 action
= wxACTION_NOTEBOOK_NEXT
;
1334 action
= wxACTION_NOTEBOOK_GOTO
;
1335 // page = 0; -- already has this value
1339 action
= wxACTION_NOTEBOOK_GOTO
;
1340 page
= notebook
->GetPageCount() - 1;
1346 return consumer
->PerformAction(action
, page
);
1350 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1353 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1354 const wxMouseEvent
& event
)
1356 if ( event
.ButtonDown(1) )
1358 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1359 int page
= notebook
->HitTest(event
.GetPosition());
1362 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1368 return wxStdInputHandler::HandleMouse(consumer
, event
);
1371 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1372 const wxMouseEvent
& event
)
1374 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1377 bool wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1378 const wxFocusEvent
& event
)
1380 HandleFocusChange(consumer
);
1385 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1386 bool WXUNUSED(activated
))
1388 // we react to the focus change in the same way as to the [de]activation
1389 HandleFocusChange(consumer
);
1394 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1396 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1397 notebook
->RefreshCurrent();
1400 #endif // wxUSE_NOTEBOOK