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
335 else // not the first tab
344 else // pages added to the notebook are initially hidden
352 bool wxNotebook::DeleteAllPages()
354 if ( !wxNotebookBase::DeleteAllPages() )
357 // clear the other arrays as well
363 // it is not valid any longer
364 m_sel
= INVALID_PAGE
;
366 // spin button is not needed any more
374 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
376 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, _T("invalid notebook page") );
378 wxNotebookPage
*page
= m_pages
[nPage
];
379 m_pages
.RemoveAt(nPage
);
380 m_titles
.RemoveAt(nPage
);
381 m_accels
.RemoveAt(nPage
);
382 m_widths
.RemoveAt(nPage
);
383 m_images
.RemoveAt(nPage
);
385 // the spin button might not be needed any more
386 // 2002-08-12 'if' commented out by JACS on behalf
387 // of Hans Van Leemputten <Hansvl@softhome.net> who
388 // points out that UpdateSpinBtn should always be called,
389 // to ensure m_lastVisible is up to date.
390 // if ( HasSpinBtn() )
395 int count
= GetPageCount();
398 if ( m_sel
== (size_t)nPage
)
400 // avoid sending event to this page which doesn't exist in the
402 m_sel
= INVALID_PAGE
;
404 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
406 else if ( m_sel
> (size_t)nPage
)
408 // no need to change selection, just adjust the index
412 else // no more tabs left
414 m_sel
= INVALID_PAGE
;
417 // have to refresh everything
423 // ----------------------------------------------------------------------------
424 // wxNotebook drawing
425 // ----------------------------------------------------------------------------
427 void wxNotebook::RefreshCurrent()
429 if ( m_sel
!= INVALID_PAGE
)
435 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
437 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
439 wxRect rect
= GetTabRect(page
);
440 if ( forceSelected
|| ((size_t)page
== m_sel
) )
442 const wxSize indent
= GetRenderer()->GetTabIndent();
443 rect
.Inflate(indent
.x
, indent
.y
);
449 void wxNotebook::RefreshAllTabs()
451 wxRect rect
= GetAllTabsRect();
452 if ( rect
.width
|| rect
.height
)
456 //else: we don't have tabs at all
459 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
464 int image
= m_images
[n
];
466 // Not needed now that wxGenericImageList is being
467 // used for wxUniversal under MSW
468 #if 0 // def __WXMSW__ // FIXME
470 m_imageList
->GetSize(n
, w
, h
);
473 dc
.SelectObject(bmp
);
474 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
475 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, TRUE
);
476 dc
.SelectObject(wxNullBitmap
);
478 bmp
= *m_imageList
->GetBitmap(image
);
485 flags
|= wxCONTROL_SELECTED
;
488 flags
|= wxCONTROL_FOCUSED
;
491 GetRenderer()->DrawTab
503 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
505 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
507 wxDC
& dc
= renderer
->GetDC();
508 dc
.SetFont(GetFont());
509 dc
.SetTextForeground(GetForegroundColour());
511 // redraw the border - it's simpler to always do it instead of checking
512 // whether this needs to be done
513 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
515 // avoid overwriting the spin button
518 wxRect rectTabs
= GetAllTabsRect();
519 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
523 rectTabs
.height
-= sizeSpinBtn
.y
;
525 // Allow for erasing the line under selected tab
530 rectTabs
.width
-= sizeSpinBtn
.x
;
532 // Allow for erasing the line under selected tab
533 rectTabs
.height
+= 2;
536 dc
.SetClippingRegion(rectTabs
);
539 wxRect rect
= GetTabsPart();
540 bool isVertical
= IsVertical();
543 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
545 GetTabSize(n
, &rect
.width
, &rect
.height
);
549 // don't redraw it now as this tab has to be drawn over the other
550 // ones as it takes more place and spills over to them
553 else // not selected tab
555 // unfortunately we can't do this because the selected tab hangs
556 // over its neighbours and so we might need to refresh more tabs -
557 // of course, we could still avoid rereshing some of them with more
558 // complicated checks, but it doesn't seem too bad to refresh all
559 // of them, I still don't see flicker, so leaving as is for now
561 //if ( rectUpdate.Intersects(rect) )
563 DoDrawTab(dc
, rect
, n
);
565 //else: doesn't need to be refreshed
568 // move the rect to the next tab
570 rect
.y
+= rect
.height
;
572 rect
.x
+= rect
.width
;
575 // now redraw the selected tab
578 DoDrawTab(dc
, rectSel
, m_sel
);
581 dc
.DestroyClippingRegion();
584 // ----------------------------------------------------------------------------
585 // wxNotebook geometry
586 // ----------------------------------------------------------------------------
588 int wxNotebook::HitTest(const wxPoint
& pt
) const
590 // first check that it is in this window at all
591 if ( !GetClientRect().Inside(pt
) )
596 wxRect rectTabs
= GetAllTabsRect();
598 switch ( GetTabOrientation() )
601 wxFAIL_MSG(_T("unknown tab orientation"));
605 if ( pt
.y
> rectTabs
.GetBottom() )
610 if ( pt
.y
< rectTabs
.y
)
615 if ( pt
.x
> rectTabs
.GetRight() )
620 if ( pt
.x
< rectTabs
.x
)
625 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
627 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
629 if ( rectTabs
.Inside(pt
) )
632 // move the rectTabs to the next tab
634 rectTabs
.y
+= rectTabs
.height
;
636 rectTabs
.x
+= rectTabs
.width
;
642 bool wxNotebook::IsVertical() const
644 wxDirection dir
= GetTabOrientation();
646 return dir
== wxLEFT
|| dir
== wxRIGHT
;
649 wxDirection
wxNotebook::GetTabOrientation() const
651 long style
= GetWindowStyle();
652 if ( style
& wxNB_BOTTOM
)
654 else if ( style
& wxNB_RIGHT
)
656 else if ( style
& wxNB_LEFT
)
659 // wxNB_TOP == 0 so we don't have to test for it
663 wxRect
wxNotebook::GetTabRect(int page
) const
666 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, _T("invalid notebook page") );
668 // calc the size of this tab and of the preceding ones
669 wxCoord widthThis
, widthBefore
;
670 if ( FixedSizeTabs() )
672 widthThis
= m_widthMax
;
673 widthBefore
= page
*m_widthMax
;
678 for ( int n
= 0; n
< page
; n
++ )
680 widthBefore
+= m_widths
[n
];
683 widthThis
= m_widths
[page
];
686 rect
= GetTabsPart();
689 rect
.y
+= widthBefore
- m_offset
;
690 rect
.height
= widthThis
;
694 rect
.x
+= widthBefore
- m_offset
;
695 rect
.width
= widthThis
;
701 wxRect
wxNotebook::GetAllTabsRect() const
705 if ( GetPageCount() )
707 const wxSize indent
= GetRenderer()->GetTabIndent();
708 wxSize size
= GetClientSize();
712 rect
.width
= m_heightTab
+ indent
.x
;
713 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
715 rect
.height
= size
.y
;
721 rect
.height
= m_heightTab
+ indent
.y
;
722 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
730 wxRect
wxNotebook::GetTabsPart() const
732 wxRect rect
= GetAllTabsRect();
734 wxDirection dir
= GetTabOrientation();
736 const wxSize indent
= GetRenderer()->GetTabIndent();
748 rect
.height
-= indent
.y
;
752 rect
.height
-= indent
.y
;
759 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
761 wxCHECK_RET( w
&& h
, _T("NULL pointer in GetTabSize") );
765 // width and height have inverted meaning
771 // height is always fixed
774 // width may also be fixed and be the same for all tabs
775 *w
= GetTabWidth(page
);
778 void wxNotebook::SetTabSize(const wxSize
& sz
)
780 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
794 wxSize
wxNotebook::CalcTabSize(int page
) const
796 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
797 // method is called to calculate them
801 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, _T("invalid notebook page") );
803 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
805 if ( HasImage(page
) )
808 m_imageList
->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
810 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
812 if ( sizeImage
.y
> size
.y
)
813 size
.y
= sizeImage
.y
;
816 size
.x
+= 2*m_sizePad
.x
;
817 size
.y
+= 2*m_sizePad
.y
;
822 void wxNotebook::ResizeTab(int page
)
824 wxSize sizeTab
= CalcTabSize(page
);
826 // we only need full relayout if the page size changes
827 bool needsRelayout
= FALSE
;
832 wxCoord tmp
= sizeTab
.x
;
833 sizeTab
.x
= sizeTab
.y
;
837 if ( sizeTab
.y
> m_heightTab
)
839 needsRelayout
= TRUE
;
841 m_heightTab
= sizeTab
.y
;
844 m_widths
[page
] = sizeTab
.x
;
846 if ( sizeTab
.x
> m_widthMax
)
847 m_widthMax
= sizeTab
.x
;
849 // the total of the tabs has changed too
858 void wxNotebook::SetPadding(const wxSize
& padding
)
860 if ( padding
!= m_sizePad
)
868 void wxNotebook::Relayout()
870 if ( GetPageCount() )
876 if ( m_sel
!= INVALID_PAGE
)
878 // resize the currently shown page
879 wxRect rectPage
= GetPageRect();
881 m_pages
[m_sel
]->SetSize(rectPage
);
883 // also scroll it into view if needed (note that m_lastVisible
884 // was updated by the call to UpdateSpinBtn() above, this is why it
888 if ( m_sel
< m_firstVisible
)
890 // selection is to the left of visible part of tabs
893 else if ( m_sel
> m_lastFullyVisible
)
895 // selection is to the right of visible part of tabs
901 else // we have no pages
903 // just refresh everything
908 wxRect
wxNotebook::GetPagePart() const
910 wxRect rectPage
= GetClientRect();
912 if ( GetPageCount() )
914 wxRect rectTabs
= GetAllTabsRect();
915 wxDirection dir
= GetTabOrientation();
918 rectPage
.width
-= rectTabs
.width
;
920 rectPage
.x
+= rectTabs
.width
;
924 rectPage
.height
-= rectTabs
.height
;
926 rectPage
.y
+= rectTabs
.height
;
929 //else: no pages at all
934 wxRect
wxNotebook::GetPageRect() const
936 wxRect rect
= GetPagePart();
938 // leave space for the border
939 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
941 // FIXME: hardcoded +2!
942 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
943 -(rectBorder
.y
+ rectBorder
.height
+ 2));
948 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
950 wxSize sizeNb
= size
;
951 wxRect rect
= GetAllTabsRect();
953 sizeNb
.x
+= rect
.width
;
955 sizeNb
.y
+= rect
.height
;
960 void wxNotebook::SetPageSize(const wxSize
& size
)
962 SetClientSize(GetSizeForPage(size
));
965 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
967 return AdjustSize(GetSizeForPage(sizePage
));
970 // ----------------------------------------------------------------------------
971 // wxNotebook spin button
972 // ----------------------------------------------------------------------------
974 bool wxNotebook::HasSpinBtn() const
976 return m_spinbtn
&& m_spinbtn
->IsShown();
979 void wxNotebook::CalcLastVisibleTab()
981 bool isVertical
= IsVertical();
983 wxCoord width
= GetClientSize().x
;
985 wxRect rect
= GetTabsPart();
987 size_t count
= GetPageCount();
989 wxCoord widthLast
= 0;
991 for ( n
= m_firstVisible
; n
< count
; n
++ )
993 GetTabSize(n
, &rect
.width
, &rect
.height
);
994 if ( rect
.GetRight() > width
)
999 // remember it to use below
1000 widthLast
= rect
.GetRight();
1002 // move the rect to the next tab
1004 rect
.y
+= rect
.height
;
1006 rect
.x
+= rect
.width
;
1009 if ( n
== m_firstVisible
)
1011 // even the first tab isn't fully visible - but still pretend it is as
1012 // we have to show something
1013 m_lastFullyVisible
= m_firstVisible
;
1015 else // more than one tab visible
1017 m_lastFullyVisible
= n
- 1;
1019 // but is it really fully visible? it shouldn't overlap with the spin
1020 // button if it is present (again, even if the first button does
1021 // overlap with it, we pretend that it doesn't as there is not much
1023 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1025 // adjust width to be the width before the spin button
1026 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1028 width
-= sizeSpinBtn
.y
;
1030 width
-= sizeSpinBtn
.x
;
1032 if ( widthLast
> width
)
1034 // the last button overlaps with spin button, so take he
1036 m_lastFullyVisible
--;
1043 // everything is visible
1048 // this tab is still (partially) visible, so m_lastVisible is the
1049 // next tab (remember, this is "exclusive" last)
1050 m_lastVisible
= n
+ 1;
1055 void wxNotebook::UpdateSpinBtn()
1057 // first decide if we need a spin button
1060 size_t count
= (size_t)GetPageCount();
1063 // this case is special, get rid of it immediately: everything is
1064 // visible and we don't need any spin buttons
1065 allTabsShown
= TRUE
;
1067 // have to reset them manually as we don't call CalcLastVisibleTab()
1070 m_lastFullyVisible
= 0;
1074 CalcLastVisibleTab();
1076 // if all tabs after the first visible one are shown, it doesn't yet
1077 // mean that all tabs are shown - so we go backwards until we arrive to
1078 // the beginning (then all tabs are indeed shown) or find a tab such
1079 // that not all tabs after it are shown
1080 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1082 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1084 m_offset
-= GetTabWidth(m_firstVisible
--);
1086 // reclaculate after m_firstVisible change
1087 CalcLastVisibleTab();
1090 allTabsShown
= m_lastFullyVisible
== count
- 1;
1093 if ( !allTabsShown
)
1097 // create it once only
1098 m_spinbtn
= new wxNotebookSpinBtn(this);
1100 // set the correct value to keep it in sync
1101 m_spinbtn
->SetValue(m_sel
);
1104 // position it correctly
1110 // also set/update the range
1111 m_spinbtn
->SetRange(0, count
- 1);
1113 // update m_lastFullyVisible once again as it might have changed
1114 // because the spin button appeared
1116 // FIXME: might do it more efficiently
1117 CalcLastVisibleTab();
1119 else // all tabs are visible, we don't need spin button
1128 void wxNotebook::PositionSpinBtn()
1134 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1136 wxRect rectTabs
= GetAllTabsRect();
1139 switch ( GetTabOrientation() )
1142 wxFAIL_MSG(_T("unknown tab orientation"));
1146 x
= rectTabs
.GetRight() - wBtn
;
1147 y
= rectTabs
.GetBottom() - hBtn
;
1151 x
= rectTabs
.GetRight() - wBtn
;
1152 y
= rectTabs
.GetTop();
1156 x
= rectTabs
.GetRight() - wBtn
;
1157 y
= rectTabs
.GetBottom() - hBtn
;
1161 x
= rectTabs
.GetLeft();
1162 y
= rectTabs
.GetBottom() - hBtn
;
1166 m_spinbtn
->Move(x
, y
);
1169 // ----------------------------------------------------------------------------
1170 // wxNotebook scrolling
1171 // ----------------------------------------------------------------------------
1173 void wxNotebook::ScrollTo(int page
)
1175 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1177 // set the first visible tab and offset (easy)
1178 m_firstVisible
= (size_t)page
;
1180 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1182 m_offset
+= GetTabWidth(n
);
1185 // find the last visible tab too
1186 CalcLastVisibleTab();
1191 void wxNotebook::ScrollLastTo(int page
)
1193 wxCHECK_RET( IS_VALID_PAGE(page
), _T("invalid notebook page") );
1195 // go backwards until we find the first tab which can be made visible
1196 // without hiding the given one
1197 wxSize size
= GetClientSize();
1198 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1199 widthTabs
= GetTabWidth(page
);
1201 // the total width is less than the width of the window if we have the spin
1205 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1207 widthAll
-= sizeSpinBtn
.y
;
1209 widthAll
-= sizeSpinBtn
.x
;
1212 m_firstVisible
= page
;
1213 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1215 widthTabs
+= GetTabWidth(--m_firstVisible
);
1218 if ( widthTabs
> widthAll
)
1220 // take one step back (that it is forward) if we can
1221 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1226 ScrollTo(m_firstVisible
);
1228 // consitency check: the page we were asked to show should be shown
1229 wxASSERT_MSG( (size_t)page
< m_lastVisible
, _T("bug in ScrollLastTo") );
1232 // ----------------------------------------------------------------------------
1233 // wxNotebook sizing/moving
1234 // ----------------------------------------------------------------------------
1236 wxSize
wxNotebook::DoGetBestClientSize() const
1238 // calculate the max page size
1241 size_t count
= GetPageCount();
1244 for ( size_t n
= 0; n
< count
; n
++ )
1246 wxSize sizePage
= m_pages
[n
]->GetSize();
1248 if ( size
.x
< sizePage
.x
)
1249 size
.x
= sizePage
.x
;
1250 if ( size
.y
< sizePage
.y
)
1251 size
.y
= sizePage
.y
;
1256 // use some arbitrary default size
1261 return GetSizeForPage(size
);
1264 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1266 wxControl::DoMoveWindow(x
, y
, width
, height
);
1268 // move the spin ctrl too (NOP if it doesn't exist)
1272 void wxNotebook::DoSetSize(int x
, int y
,
1273 int width
, int height
,
1276 wxSize old_client_size
= GetClientSize();
1278 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1280 wxSize new_client_size
= GetClientSize();
1282 if (old_client_size
!= new_client_size
)
1286 // ----------------------------------------------------------------------------
1287 // wxNotebook input processing
1288 // ----------------------------------------------------------------------------
1290 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1292 const wxString
& strArg
)
1294 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1295 ChangePage(GetNextPage(TRUE
));
1296 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1297 ChangePage(GetNextPage(FALSE
));
1298 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1299 ChangePage((int)numArg
);
1301 return wxControl::PerformAction(action
, numArg
, strArg
);
1306 // ----------------------------------------------------------------------------
1307 // wxStdNotebookInputHandler
1308 // ----------------------------------------------------------------------------
1310 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1311 : wxStdInputHandler(inphand
)
1315 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1316 const wxKeyEvent
& event
,
1319 // ignore the key releases
1322 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1325 wxControlAction action
;
1326 switch ( event
.GetKeyCode() )
1329 if ( notebook
->IsVertical() )
1330 action
= wxACTION_NOTEBOOK_PREV
;
1334 if ( !notebook
->IsVertical() )
1335 action
= wxACTION_NOTEBOOK_PREV
;
1339 if ( notebook
->IsVertical() )
1340 action
= wxACTION_NOTEBOOK_NEXT
;
1344 if ( !notebook
->IsVertical() )
1345 action
= wxACTION_NOTEBOOK_NEXT
;
1349 action
= wxACTION_NOTEBOOK_GOTO
;
1350 // page = 0; -- already has this value
1354 action
= wxACTION_NOTEBOOK_GOTO
;
1355 page
= notebook
->GetPageCount() - 1;
1361 return consumer
->PerformAction(action
, page
);
1365 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1368 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1369 const wxMouseEvent
& event
)
1371 if ( event
.ButtonDown(1) )
1373 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1374 int page
= notebook
->HitTest(event
.GetPosition());
1377 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1383 return wxStdInputHandler::HandleMouse(consumer
, event
);
1386 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1387 const wxMouseEvent
& event
)
1389 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1392 bool wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1393 const wxFocusEvent
& event
)
1395 HandleFocusChange(consumer
);
1400 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1401 bool WXUNUSED(activated
))
1403 // we react to the focus change in the same way as to the [de]activation
1404 HandleFocusChange(consumer
);
1409 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1411 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1412 notebook
->RefreshCurrent();
1415 #endif // wxUSE_NOTEBOOK