1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/notebook.cpp
3 // Purpose: wxNotebook implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
27 #include "wx/notebook.h"
30 #include "wx/dcmemory.h"
33 #include "wx/imaglist.h"
34 #include "wx/spinbutt.h"
36 #include "wx/univ/renderer.h"
38 // ----------------------------------------------------------------------------
39 // wxStdNotebookInputHandler: translates SPACE and ENTER keys and the left mouse
40 // click into button press/release actions
41 // ----------------------------------------------------------------------------
43 class WXDLLEXPORT wxStdNotebookInputHandler
: public wxStdInputHandler
46 wxStdNotebookInputHandler(wxInputHandler
*inphand
);
48 virtual bool HandleKey(wxInputConsumer
*consumer
,
49 const wxKeyEvent
& event
,
51 virtual bool HandleMouse(wxInputConsumer
*consumer
,
52 const wxMouseEvent
& event
);
53 virtual bool HandleMouseMove(wxInputConsumer
*consumer
, const wxMouseEvent
& event
);
54 virtual bool HandleFocus(wxInputConsumer
*consumer
, const wxFocusEvent
& event
);
55 virtual bool HandleActivation(wxInputConsumer
*consumer
, bool activated
);
58 void HandleFocusChange(wxInputConsumer
*consumer
);
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
66 // due to unsigned type nPage is always >= 0
67 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
69 #define IS_VALID_PAGE(nPage) (((size_t)nPage) < GetPageCount())
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 class wxNotebookSpinBtn
: public wxSpinButton
79 wxNotebookSpinBtn(wxNotebook
*nb
)
80 : wxSpinButton(nb
, wxID_ANY
,
81 wxDefaultPosition
, wxDefaultSize
,
82 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
88 void OnSpin(wxSpinEvent
& event
)
90 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
99 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
100 EVT_SPIN(wxID_ANY
, wxNotebookSpinBtn::OnSpin
)
103 // ============================================================================
105 // ============================================================================
107 // ----------------------------------------------------------------------------
108 // wxNotebook creation
109 // ----------------------------------------------------------------------------
111 void wxNotebook::Init()
118 m_lastFullyVisible
= 0;
125 bool wxNotebook::Create(wxWindow
*parent
,
130 const wxString
& name
)
132 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
135 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
136 wxDefaultValidator
, name
) )
139 m_sizePad
= GetRenderer()->GetTabPadding();
141 SetInitialSize(size
);
143 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
148 // ----------------------------------------------------------------------------
149 // wxNotebook page titles and images
150 // ----------------------------------------------------------------------------
152 wxString
wxNotebook::GetPageText(size_t nPage
) const
154 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("invalid notebook page") );
156 return m_titles
[nPage
];
159 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
161 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("invalid notebook page") );
163 if ( strText
!= m_titles
[nPage
] )
165 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
167 if ( FixedSizeTabs() )
169 // it's enough to just reresh this one
172 else // var width tabs
174 // we need to resize the tab to fit the new string
182 int wxNotebook::GetPageImage(size_t nPage
) const
184 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("invalid notebook page") );
186 return m_images
[nPage
];
189 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
191 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("invalid notebook page") );
193 wxCHECK_MSG( HasImageList() && nImage
< GetImageList()->GetImageCount(),
194 false, wxT("invalid image index in SetPageImage()") );
196 if ( nImage
!= m_images
[nPage
] )
198 // if the item didn't have an icon before or, on the contrary, did have
199 // it but has lost it now, its size will change - but if the icon just
201 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
202 m_images
[nPage
] = nImage
;
204 if ( tabSizeChanges
)
213 wxNotebook::~wxNotebook()
217 // ----------------------------------------------------------------------------
218 // wxNotebook page switching
219 // ----------------------------------------------------------------------------
221 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
223 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("invalid notebook page") );
225 if ( (int)nPage
== m_selection
)
227 // don't do anything if there is nothing to do
231 if ( flags
& SetSelection_SendEvent
)
233 if ( !SendPageChangingEvent(nPage
) )
235 // program doesn't allow the page change
240 // we need to change m_selection first, before calling RefreshTab() below as
241 // otherwise the previously selected tab wouldn't be redrawn properly under
242 // wxGTK which calls Refresh() immediately and not during the next event
243 // loop iteration as wxMSW does and as it should
244 int selOld
= m_selection
;
248 if ( selOld
!= wxNOT_FOUND
)
250 RefreshTab(selOld
, true /* this tab was selected */);
252 m_pages
[selOld
]->Hide();
255 if ( m_selection
!= wxNOT_FOUND
) // this is impossible - but test nevertheless
260 m_spinbtn
->SetValue(m_selection
);
263 if ( nPage
< m_firstVisible
)
265 // selection is to the left of visible part of tabs
268 else if ( nPage
> m_lastFullyVisible
)
270 // selection is to the right of visible part of tabs
273 else // we already see this tab
279 m_pages
[nPage
]->SetSize(GetPageRect());
280 m_pages
[nPage
]->Show();
283 if ( flags
& SetSelection_SendEvent
)
286 SendPageChangedEvent(selOld
);
292 // ----------------------------------------------------------------------------
293 // wxNotebook pages adding/deleting
294 // ----------------------------------------------------------------------------
296 bool wxNotebook::InsertPage(size_t nPage
,
297 wxNotebookPage
*pPage
,
298 const wxString
& strText
,
302 size_t nPages
= GetPageCount();
303 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), false,
304 wxT("invalid notebook page in InsertPage()") );
307 m_pages
.Insert(pPage
, nPage
);
310 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
311 m_titles
.Insert(label
, nPage
);
313 m_images
.Insert(imageId
, nPage
);
315 // cache the tab geometry here
316 wxSize sizeTab
= CalcTabSize(nPage
);
318 if ( sizeTab
.y
> m_heightTab
)
319 m_heightTab
= sizeTab
.y
;
321 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
322 m_widthMax
= sizeTab
.x
;
324 m_widths
.Insert(sizeTab
.x
, nPage
);
326 // spin button may appear if we didn't have it before - but even if we did,
327 // its range should change, so update it unconditionally
330 // if the tab has just appeared, we have to relayout everything, otherwise
331 // it's enough to just redraw the tabs
334 // always select the first tab to have at least some selection
340 else // not the first tab
349 else // pages added to the notebook are initially hidden
357 bool wxNotebook::DeleteAllPages()
359 if ( !wxNotebookBase::DeleteAllPages() )
362 // clear the other arrays as well
368 // spin button is not needed any more
376 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
378 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, wxT("invalid notebook page") );
380 wxNotebookPage
*page
= m_pages
[nPage
];
381 m_pages
.RemoveAt(nPage
);
382 m_titles
.RemoveAt(nPage
);
383 m_accels
.RemoveAt(nPage
);
384 m_widths
.RemoveAt(nPage
);
385 m_images
.RemoveAt(nPage
);
387 // the spin button might not be needed any more
388 // 2002-08-12 'if' commented out by JACS on behalf
389 // of Hans Van Leemputten <Hansvl@softhome.net> who
390 // points out that UpdateSpinBtn should always be called,
391 // to ensure m_lastVisible is up to date.
392 // if ( HasSpinBtn() )
397 size_t count
= GetPageCount();
400 wxASSERT_MSG( m_selection
!= wxNOT_FOUND
, "should have selection" );
402 if ( (size_t)m_selection
== nPage
)
404 // avoid sending event to this page which doesn't exist in the
406 m_selection
= wxNOT_FOUND
;
408 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
410 else if ( (size_t)m_selection
> nPage
)
412 // no need to change selection, just adjust the index
416 else // no more tabs left
418 m_selection
= wxNOT_FOUND
;
421 // have to refresh everything
427 // ----------------------------------------------------------------------------
428 // wxNotebook drawing
429 // ----------------------------------------------------------------------------
431 void wxNotebook::RefreshCurrent()
433 if ( m_selection
!= wxNOT_FOUND
)
435 RefreshTab(m_selection
);
439 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
441 wxCHECK_RET( IS_VALID_PAGE(page
), wxT("invalid notebook page") );
443 wxRect rect
= GetTabRect(page
);
444 if ( forceSelected
|| (page
== m_selection
) )
446 const wxSize indent
= GetRenderer()->GetTabIndent();
447 rect
.Inflate(indent
.x
, indent
.y
);
453 void wxNotebook::RefreshAllTabs()
455 wxRect rect
= GetAllTabsRect();
456 if ( rect
.width
|| rect
.height
)
460 //else: we don't have tabs at all
463 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
468 int image
= m_images
[n
];
470 // Not needed now that wxGenericImageList is being
471 // used for wxUniversal under MSW
472 #if 0 // def __WXMSW__ // FIXME
474 GetImageList()->GetSize(n
, w
, h
);
477 dc
.SelectObject(bmp
);
478 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
479 GetImageList()->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, true);
480 dc
.SelectObject(wxNullBitmap
);
482 bmp
= GetImageList()->GetBitmap(image
);
487 if ( (int)n
== m_selection
)
489 flags
|= wxCONTROL_SELECTED
;
492 flags
|= wxCONTROL_FOCUSED
;
495 GetRenderer()->DrawTab
507 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
509 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
511 wxDC
& dc
= renderer
->GetDC();
512 dc
.SetFont(GetFont());
513 dc
.SetTextForeground(GetForegroundColour());
515 // redraw the border - it's simpler to always do it instead of checking
516 // whether this needs to be done
517 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
519 // avoid overwriting the spin button
522 wxRect rectTabs
= GetAllTabsRect();
523 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
527 rectTabs
.height
-= sizeSpinBtn
.y
;
529 // Allow for erasing the line under selected tab
534 rectTabs
.width
-= sizeSpinBtn
.x
;
536 // Allow for erasing the line under selected tab
537 rectTabs
.height
+= 2;
540 dc
.SetClippingRegion(rectTabs
);
543 wxRect rect
= GetTabsPart();
544 bool isVertical
= IsVertical();
547 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
549 GetTabSize(n
, &rect
.width
, &rect
.height
);
551 if ( (int)n
== m_selection
)
553 // don't redraw it now as this tab has to be drawn over the other
554 // ones as it takes more place and spills over to them
557 else // not selected tab
559 // unfortunately we can't do this because the selected tab hangs
560 // over its neighbours and so we might need to refresh more tabs -
561 // of course, we could still avoid rereshing some of them with more
562 // complicated checks, but it doesn't seem too bad to refresh all
563 // of them, I still don't see flicker, so leaving as is for now
565 //if ( rectUpdate.Intersects(rect) )
567 DoDrawTab(dc
, rect
, n
);
569 //else: doesn't need to be refreshed
572 // move the rect to the next tab
574 rect
.y
+= rect
.height
;
576 rect
.x
+= rect
.width
;
579 // now redraw the selected tab
582 DoDrawTab(dc
, rectSel
, m_selection
);
585 dc
.DestroyClippingRegion();
588 // ----------------------------------------------------------------------------
589 // wxNotebook geometry
590 // ----------------------------------------------------------------------------
592 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
595 *flags
= wxBK_HITTEST_NOWHERE
;
597 // first check that it is in this window at all
598 if ( !GetClientRect().Contains(pt
) )
603 wxRect rectTabs
= GetAllTabsRect();
605 switch ( GetTabOrientation() )
608 wxFAIL_MSG(wxT("unknown tab orientation"));
612 if ( pt
.y
> rectTabs
.GetBottom() )
617 if ( pt
.y
< rectTabs
.y
)
622 if ( pt
.x
> rectTabs
.GetRight() )
627 if ( pt
.x
< rectTabs
.x
)
632 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
634 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
636 if ( rectTabs
.Contains(pt
) )
640 // TODO: be more precise
641 *flags
= wxBK_HITTEST_ONITEM
;
647 // move the rectTabs to the next tab
649 rectTabs
.y
+= rectTabs
.height
;
651 rectTabs
.x
+= rectTabs
.width
;
657 bool wxNotebook::IsVertical() const
659 wxDirection dir
= GetTabOrientation();
661 return dir
== wxLEFT
|| dir
== wxRIGHT
;
664 wxDirection
wxNotebook::GetTabOrientation() const
666 long style
= GetWindowStyle();
667 if ( style
& wxBK_BOTTOM
)
669 else if ( style
& wxBK_RIGHT
)
671 else if ( style
& wxBK_LEFT
)
674 // wxBK_TOP == 0 so we don't have to test for it
678 wxRect
wxNotebook::GetTabRect(int page
) const
681 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, wxT("invalid notebook page") );
683 // calc the size of this tab and of the preceding ones
684 wxCoord widthThis
, widthBefore
;
685 if ( FixedSizeTabs() )
687 widthThis
= m_widthMax
;
688 widthBefore
= page
*m_widthMax
;
693 for ( int n
= 0; n
< page
; n
++ )
695 widthBefore
+= m_widths
[n
];
698 widthThis
= m_widths
[page
];
701 rect
= GetTabsPart();
704 rect
.y
+= widthBefore
- m_offset
;
705 rect
.height
= widthThis
;
709 rect
.x
+= widthBefore
- m_offset
;
710 rect
.width
= widthThis
;
716 wxRect
wxNotebook::GetAllTabsRect() const
720 if ( GetPageCount() )
722 const wxSize indent
= GetRenderer()->GetTabIndent();
723 wxSize size
= GetClientSize();
727 rect
.width
= m_heightTab
+ indent
.x
;
728 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
730 rect
.height
= size
.y
;
736 rect
.height
= m_heightTab
+ indent
.y
;
737 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
745 wxRect
wxNotebook::GetTabsPart() const
747 wxRect rect
= GetAllTabsRect();
749 wxDirection dir
= GetTabOrientation();
751 const wxSize indent
= GetRenderer()->GetTabIndent();
758 rect
.width
-= indent
.y
;
762 rect
.width
-= indent
.y
;
771 rect
.height
-= indent
.y
;
775 rect
.height
-= indent
.y
;
782 void wxNotebook::GetTabSize(int page
, wxCoord
*w
, wxCoord
*h
) const
784 wxCHECK_RET( w
&& h
, wxT("NULL pointer in GetTabSize") );
788 // width and height have inverted meaning
794 // height is always fixed
797 // width may also be fixed and be the same for all tabs
798 *w
= GetTabWidth(page
);
801 void wxNotebook::SetTabSize(const wxSize
& sz
)
803 wxCHECK_RET( FixedSizeTabs(), wxT("SetTabSize() ignored") );
817 wxSize
wxNotebook::CalcTabSize(int page
) const
819 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
820 // method is called to calculate them
824 wxCHECK_MSG( IS_VALID_PAGE(page
), size
, wxT("invalid notebook page") );
826 GetTextExtent(m_titles
[page
], &size
.x
, &size
.y
);
828 if ( HasImage(page
) )
831 GetImageList()->GetSize(m_images
[page
], sizeImage
.x
, sizeImage
.y
);
833 size
.x
+= sizeImage
.x
+ 5; // FIXME: hard coded margin
835 if ( sizeImage
.y
> size
.y
)
836 size
.y
= sizeImage
.y
;
839 size
.x
+= 2*m_sizePad
.x
;
840 size
.y
+= 2*m_sizePad
.y
;
845 void wxNotebook::ResizeTab(int page
)
847 wxSize sizeTab
= CalcTabSize(page
);
849 // we only need full relayout if the page size changes
850 bool needsRelayout
= false;
855 wxCoord tmp
= sizeTab
.x
;
856 sizeTab
.x
= sizeTab
.y
;
860 if ( sizeTab
.y
> m_heightTab
)
862 needsRelayout
= true;
864 m_heightTab
= sizeTab
.y
;
867 m_widths
[page
] = sizeTab
.x
;
869 if ( sizeTab
.x
> m_widthMax
)
870 m_widthMax
= sizeTab
.x
;
872 // the total of the tabs has changed too
881 void wxNotebook::SetPadding(const wxSize
& padding
)
883 if ( padding
!= m_sizePad
)
891 void wxNotebook::Relayout()
893 if ( GetPageCount() )
899 if ( m_selection
!= wxNOT_FOUND
)
901 // resize the currently shown page
902 wxRect rectPage
= GetPageRect();
904 m_pages
[m_selection
]->SetSize(rectPage
);
906 // also scroll it into view if needed (note that m_lastVisible
907 // was updated by the call to UpdateSpinBtn() above, this is why it
911 const size_t selection
= m_selection
;
912 if ( selection
< m_firstVisible
)
914 // selection is to the left of visible part of tabs
917 else if ( selection
> m_lastFullyVisible
)
919 // selection is to the right of visible part of tabs
920 ScrollLastTo(selection
);
925 else // we have no pages
927 // just refresh everything
932 wxRect
wxNotebook::GetPagePart() const
934 wxRect rectPage
= GetClientRect();
936 if ( GetPageCount() )
938 wxRect rectTabs
= GetAllTabsRect();
939 wxDirection dir
= GetTabOrientation();
942 rectPage
.width
-= rectTabs
.width
;
944 rectPage
.x
+= rectTabs
.width
;
948 rectPage
.height
-= rectTabs
.height
;
950 rectPage
.y
+= rectTabs
.height
;
953 //else: no pages at all
958 wxRect
wxNotebook::GetPageRect() const
960 wxRect rect
= GetPagePart();
962 // leave space for the border
963 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
965 // FIXME: hardcoded +2!
966 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
967 -(rectBorder
.y
+ rectBorder
.height
+ 2));
972 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
974 wxSize sizeNb
= size
;
975 wxRect rect
= GetAllTabsRect();
977 sizeNb
.x
+= rect
.width
;
979 sizeNb
.y
+= rect
.height
;
984 void wxNotebook::SetPageSize(const wxSize
& size
)
986 SetClientSize(GetSizeForPage(size
));
989 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
991 return AdjustSize(GetSizeForPage(sizePage
));
994 // ----------------------------------------------------------------------------
995 // wxNotebook spin button
996 // ----------------------------------------------------------------------------
998 bool wxNotebook::HasSpinBtn() const
1000 return m_spinbtn
&& m_spinbtn
->IsShown();
1003 void wxNotebook::CalcLastVisibleTab()
1005 bool isVertical
= IsVertical();
1007 wxCoord width
= GetClientSize().x
;
1009 wxRect rect
= GetTabsPart();
1011 size_t count
= GetPageCount();
1013 wxCoord widthLast
= 0;
1015 for ( n
= m_firstVisible
; n
< count
; n
++ )
1017 GetTabSize(n
, &rect
.width
, &rect
.height
);
1018 if ( rect
.GetRight() > width
)
1023 // remember it to use below
1024 widthLast
= rect
.GetRight();
1026 // move the rect to the next tab
1028 rect
.y
+= rect
.height
;
1030 rect
.x
+= rect
.width
;
1033 if ( n
== m_firstVisible
)
1035 // even the first tab isn't fully visible - but still pretend it is as
1036 // we have to show something
1037 m_lastFullyVisible
= m_firstVisible
;
1039 else // more than one tab visible
1041 m_lastFullyVisible
= n
- 1;
1043 // but is it really fully visible? it shouldn't overlap with the spin
1044 // button if it is present (again, even if the first button does
1045 // overlap with it, we pretend that it doesn't as there is not much
1047 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1049 // adjust width to be the width before the spin button
1050 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1052 width
-= sizeSpinBtn
.y
;
1054 width
-= sizeSpinBtn
.x
;
1056 if ( widthLast
> width
)
1058 // the last button overlaps with spin button, so take he
1060 m_lastFullyVisible
--;
1067 // everything is visible
1072 // this tab is still (partially) visible, so m_lastVisible is the
1073 // next tab (remember, this is "exclusive" last)
1074 m_lastVisible
= n
+ 1;
1079 void wxNotebook::UpdateSpinBtn()
1081 // first decide if we need a spin button
1084 size_t count
= (size_t)GetPageCount();
1087 // this case is special, get rid of it immediately: everything is
1088 // visible and we don't need any spin buttons
1089 allTabsShown
= true;
1091 // have to reset them manually as we don't call CalcLastVisibleTab()
1094 m_lastFullyVisible
= 0;
1098 CalcLastVisibleTab();
1100 // if all tabs after the first visible one are shown, it doesn't yet
1101 // mean that all tabs are shown - so we go backwards until we arrive to
1102 // the beginning (then all tabs are indeed shown) or find a tab such
1103 // that not all tabs after it are shown
1104 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1106 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1108 m_offset
-= GetTabWidth(m_firstVisible
--);
1110 // reclaculate after m_firstVisible change
1111 CalcLastVisibleTab();
1114 allTabsShown
= m_lastFullyVisible
== count
- 1;
1117 if ( !allTabsShown
)
1121 // create it once only
1122 m_spinbtn
= new wxNotebookSpinBtn(this);
1124 // set the correct value to keep it in sync
1125 m_spinbtn
->SetValue(m_selection
);
1128 // position it correctly
1134 // also set/update the range
1135 m_spinbtn
->SetRange(0, count
- 1);
1137 // update m_lastFullyVisible once again as it might have changed
1138 // because the spin button appeared
1140 // FIXME: might do it more efficiently
1141 CalcLastVisibleTab();
1143 else // all tabs are visible, we don't need spin button
1145 if ( m_spinbtn
&& m_spinbtn
-> IsShown() )
1152 void wxNotebook::PositionSpinBtn()
1158 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1160 wxRect rectTabs
= GetAllTabsRect();
1163 switch ( GetTabOrientation() )
1166 wxFAIL_MSG(wxT("unknown tab orientation"));
1170 x
= rectTabs
.GetRight() - wBtn
;
1171 y
= rectTabs
.GetBottom() - hBtn
;
1175 x
= rectTabs
.GetRight() - wBtn
;
1176 y
= rectTabs
.GetTop();
1180 x
= rectTabs
.GetRight() - wBtn
;
1181 y
= rectTabs
.GetBottom() - hBtn
;
1185 x
= rectTabs
.GetLeft();
1186 y
= rectTabs
.GetBottom() - hBtn
;
1190 m_spinbtn
->Move(x
, y
);
1193 // ----------------------------------------------------------------------------
1194 // wxNotebook scrolling
1195 // ----------------------------------------------------------------------------
1197 void wxNotebook::ScrollTo(size_t page
)
1199 wxCHECK_RET( IS_VALID_PAGE(page
), wxT("invalid notebook page") );
1201 // set the first visible tab and offset (easy)
1202 m_firstVisible
= page
;
1204 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1206 m_offset
+= GetTabWidth(n
);
1209 // find the last visible tab too
1210 CalcLastVisibleTab();
1215 void wxNotebook::ScrollLastTo(size_t page
)
1217 wxCHECK_RET( IS_VALID_PAGE(page
), wxT("invalid notebook page") );
1219 // go backwards until we find the first tab which can be made visible
1220 // without hiding the given one
1221 wxSize size
= GetClientSize();
1222 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1223 widthTabs
= GetTabWidth(page
);
1225 // the total width is less than the width of the window if we have the spin
1229 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1231 widthAll
-= sizeSpinBtn
.y
;
1233 widthAll
-= sizeSpinBtn
.x
;
1236 m_firstVisible
= page
;
1237 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1239 widthTabs
+= GetTabWidth(--m_firstVisible
);
1242 if ( widthTabs
> widthAll
)
1244 // take one step back (that it is forward) if we can
1245 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1250 ScrollTo(m_firstVisible
);
1252 // consitency check: the page we were asked to show should be shown
1253 wxASSERT_MSG( (size_t)page
< m_lastVisible
, wxT("bug in ScrollLastTo") );
1256 // ----------------------------------------------------------------------------
1257 // wxNotebook sizing/moving
1258 // ----------------------------------------------------------------------------
1260 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1262 wxControl::DoMoveWindow(x
, y
, width
, height
);
1264 // move the spin ctrl too (NOP if it doesn't exist)
1268 void wxNotebook::DoSetSize(int x
, int y
,
1269 int width
, int height
,
1272 wxSize old_client_size
= GetClientSize();
1274 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1276 wxSize new_client_size
= GetClientSize();
1278 if (old_client_size
!= new_client_size
)
1282 // ----------------------------------------------------------------------------
1283 // wxNotebook input processing
1284 // ----------------------------------------------------------------------------
1286 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1288 const wxString
& strArg
)
1290 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1291 SetSelection(GetNextPage(true));
1292 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1293 SetSelection(GetNextPage(false));
1294 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1295 SetSelection((int)numArg
);
1297 return wxControl::PerformAction(action
, numArg
, strArg
);
1303 wxInputHandler
*wxNotebook::GetStdInputHandler(wxInputHandler
*handlerDef
)
1305 static wxStdNotebookInputHandler
s_handler(handlerDef
);
1310 // ----------------------------------------------------------------------------
1311 // wxStdNotebookInputHandler
1312 // ----------------------------------------------------------------------------
1314 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1315 : wxStdInputHandler(inphand
)
1319 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1320 const wxKeyEvent
& event
,
1323 // ignore the key releases
1326 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1329 wxControlAction action
;
1330 switch ( event
.GetKeyCode() )
1333 if ( notebook
->IsVertical() )
1334 action
= wxACTION_NOTEBOOK_PREV
;
1338 if ( !notebook
->IsVertical() )
1339 action
= wxACTION_NOTEBOOK_PREV
;
1343 if ( notebook
->IsVertical() )
1344 action
= wxACTION_NOTEBOOK_NEXT
;
1348 if ( !notebook
->IsVertical() )
1349 action
= wxACTION_NOTEBOOK_NEXT
;
1353 action
= wxACTION_NOTEBOOK_GOTO
;
1354 // page = 0; -- already has this value
1358 action
= wxACTION_NOTEBOOK_GOTO
;
1359 page
= notebook
->GetPageCount() - 1;
1363 if ( !action
.IsEmpty() )
1365 return consumer
->PerformAction(action
, page
);
1369 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1372 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1373 const wxMouseEvent
& event
)
1375 if ( event
.ButtonDown(1) )
1377 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1378 int page
= notebook
->HitTest(event
.GetPosition());
1381 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1387 return wxStdInputHandler::HandleMouse(consumer
, event
);
1390 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1391 const wxMouseEvent
& event
)
1393 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1397 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1398 const wxFocusEvent
& WXUNUSED(event
))
1400 HandleFocusChange(consumer
);
1405 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1406 bool WXUNUSED(activated
))
1408 // we react to the focus change in the same way as to the [de]activation
1409 HandleFocusChange(consumer
);
1414 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1416 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1417 notebook
->RefreshCurrent();
1420 #endif // wxUSE_NOTEBOOK