1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 #include "wx/wxprec.h"
28 #include "wx/notebook.h"
31 #include "wx/dcmemory.h"
34 #include "wx/imaglist.h"
35 #include "wx/spinbutt.h"
37 #include "wx/univ/renderer.h"
39 // ----------------------------------------------------------------------------
40 // wxStdNotebookInputHandler: translates SPACE and ENTER keys and the left mouse
41 // click into button press/release actions
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxStdNotebookInputHandler
: public wxStdInputHandler
47 wxStdNotebookInputHandler(wxInputHandler
*inphand
);
49 virtual bool HandleKey(wxInputConsumer
*consumer
,
50 const wxKeyEvent
& event
,
52 virtual bool HandleMouse(wxInputConsumer
*consumer
,
53 const wxMouseEvent
& event
);
54 virtual bool HandleMouseMove(wxInputConsumer
*consumer
, const wxMouseEvent
& event
);
55 virtual bool HandleFocus(wxInputConsumer
*consumer
, const wxFocusEvent
& event
);
56 virtual bool HandleActivation(wxInputConsumer
*consumer
, bool activated
);
59 void HandleFocusChange(wxInputConsumer
*consumer
);
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
67 // due to unsigned type nPage is always >= 0
68 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
70 #define IS_VALID_PAGE(nPage) (((size_t)nPage) < GetPageCount())
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 class wxNotebookSpinBtn
: public wxSpinButton
80 wxNotebookSpinBtn(wxNotebook
*nb
)
81 : wxSpinButton(nb
, wxID_ANY
,
82 wxDefaultPosition
, wxDefaultSize
,
83 nb
->IsVertical() ? wxSP_VERTICAL
: wxSP_HORIZONTAL
)
89 void OnSpin(wxSpinEvent
& event
)
91 m_nb
->PerformAction(wxACTION_NOTEBOOK_GOTO
, event
.GetPosition());
100 BEGIN_EVENT_TABLE(wxNotebookSpinBtn
, wxSpinButton
)
101 EVT_SPIN(wxID_ANY
, wxNotebookSpinBtn::OnSpin
)
104 // ============================================================================
106 // ============================================================================
108 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
110 // ----------------------------------------------------------------------------
111 // wxNotebook creation
112 // ----------------------------------------------------------------------------
114 void wxNotebook::Init()
121 m_lastFullyVisible
= 0;
128 bool wxNotebook::Create(wxWindow
*parent
,
133 const wxString
& name
)
135 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
138 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
139 wxDefaultValidator
, name
) )
142 m_sizePad
= GetRenderer()->GetTabPadding();
144 SetInitialSize(size
);
146 CreateInputHandler(wxINP_HANDLER_NOTEBOOK
);
151 // ----------------------------------------------------------------------------
152 // wxNotebook page titles and images
153 // ----------------------------------------------------------------------------
155 wxString
wxNotebook::GetPageText(size_t nPage
) const
157 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("invalid notebook page") );
159 return m_titles
[nPage
];
162 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
164 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("invalid notebook page") );
166 if ( strText
!= m_titles
[nPage
] )
168 m_accels
[nPage
] = FindAccelIndex(strText
, &m_titles
[nPage
]);
170 if ( FixedSizeTabs() )
172 // it's enough to just reresh this one
175 else // var width tabs
177 // we need to resize the tab to fit the new string
185 int wxNotebook::GetPageImage(size_t nPage
) const
187 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("invalid notebook page") );
189 return m_images
[nPage
];
192 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
194 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("invalid notebook page") );
196 wxCHECK_MSG( m_imageList
&& nImage
< m_imageList
->GetImageCount(), false,
197 wxT("invalid image index in SetPageImage()") );
199 if ( nImage
!= m_images
[nPage
] )
201 // if the item didn't have an icon before or, on the contrary, did have
202 // it but has lost it now, its size will change - but if the icon just
204 bool tabSizeChanges
= nImage
== -1 || m_images
[nPage
] == -1;
205 m_images
[nPage
] = nImage
;
207 if ( tabSizeChanges
)
216 wxNotebook::~wxNotebook()
220 // ----------------------------------------------------------------------------
221 // wxNotebook page switching
222 // ----------------------------------------------------------------------------
224 int wxNotebook::DoSetSelection(size_t nPage
, int flags
)
226 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("invalid notebook page") );
228 if ( (int)nPage
== m_selection
)
230 // don't do anything if there is nothing to do
234 if ( flags
& SetSelection_SendEvent
)
236 if ( !SendPageChangingEvent(nPage
) )
238 // program doesn't allow the page change
243 // we need to change m_selection first, before calling RefreshTab() below as
244 // otherwise the previously selected tab wouldn't be redrawn properly under
245 // wxGTK which calls Refresh() immediately and not during the next event
246 // loop iteration as wxMSW does and as it should
247 int selOld
= m_selection
;
251 if ( selOld
!= wxNOT_FOUND
)
253 RefreshTab(selOld
, true /* this tab was selected */);
255 m_pages
[selOld
]->Hide();
258 if ( m_selection
!= wxNOT_FOUND
) // this is impossible - but test nevertheless
263 m_spinbtn
->SetValue(m_selection
);
266 if ( nPage
< m_firstVisible
)
268 // selection is to the left of visible part of tabs
271 else if ( nPage
> m_lastFullyVisible
)
273 // selection is to the right of visible part of tabs
276 else // we already see this tab
282 m_pages
[nPage
]->SetSize(GetPageRect());
283 m_pages
[nPage
]->Show();
286 if ( flags
& SetSelection_SendEvent
)
289 SendPageChangedEvent(selOld
);
295 // ----------------------------------------------------------------------------
296 // wxNotebook pages adding/deleting
297 // ----------------------------------------------------------------------------
299 bool wxNotebook::InsertPage(size_t nPage
,
300 wxNotebookPage
*pPage
,
301 const wxString
& strText
,
305 size_t nPages
= GetPageCount();
306 wxCHECK_MSG( nPage
== nPages
|| IS_VALID_PAGE(nPage
), false,
307 wxT("invalid notebook page in InsertPage()") );
310 m_pages
.Insert(pPage
, nPage
);
313 m_accels
.Insert(FindAccelIndex(strText
, &label
), nPage
);
314 m_titles
.Insert(label
, nPage
);
316 m_images
.Insert(imageId
, nPage
);
318 // cache the tab geometry here
319 wxSize sizeTab
= CalcTabSize(nPage
);
321 if ( sizeTab
.y
> m_heightTab
)
322 m_heightTab
= sizeTab
.y
;
324 if ( FixedSizeTabs() && sizeTab
.x
> m_widthMax
)
325 m_widthMax
= sizeTab
.x
;
327 m_widths
.Insert(sizeTab
.x
, nPage
);
329 // spin button may appear if we didn't have it before - but even if we did,
330 // its range should change, so update it unconditionally
333 // if the tab has just appeared, we have to relayout everything, otherwise
334 // it's enough to just redraw the tabs
337 // always select the first tab to have at least some selection
343 else // not the first tab
352 else // pages added to the notebook are initially hidden
360 bool wxNotebook::DeleteAllPages()
362 if ( !wxNotebookBase::DeleteAllPages() )
365 // clear the other arrays as well
371 // spin button is not needed any more
379 wxNotebookPage
*wxNotebook::DoRemovePage(size_t nPage
)
381 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, wxT("invalid notebook page") );
383 wxNotebookPage
*page
= m_pages
[nPage
];
384 m_pages
.RemoveAt(nPage
);
385 m_titles
.RemoveAt(nPage
);
386 m_accels
.RemoveAt(nPage
);
387 m_widths
.RemoveAt(nPage
);
388 m_images
.RemoveAt(nPage
);
390 // the spin button might not be needed any more
391 // 2002-08-12 'if' commented out by JACS on behalf
392 // of Hans Van Leemputten <Hansvl@softhome.net> who
393 // points out that UpdateSpinBtn should always be called,
394 // to ensure m_lastVisible is up to date.
395 // if ( HasSpinBtn() )
400 size_t count
= GetPageCount();
403 wxASSERT_MSG( m_selection
!= wxNOT_FOUND
, "should have selection" );
405 if ( (size_t)m_selection
== nPage
)
407 // avoid sending event to this page which doesn't exist in the
409 m_selection
= wxNOT_FOUND
;
411 SetSelection(nPage
== count
? nPage
- 1 : nPage
);
413 else if ( (size_t)m_selection
> nPage
)
415 // no need to change selection, just adjust the index
419 else // no more tabs left
421 m_selection
= wxNOT_FOUND
;
424 // have to refresh everything
430 // ----------------------------------------------------------------------------
431 // wxNotebook drawing
432 // ----------------------------------------------------------------------------
434 void wxNotebook::RefreshCurrent()
436 if ( m_selection
!= wxNOT_FOUND
)
438 RefreshTab(m_selection
);
442 void wxNotebook::RefreshTab(int page
, bool forceSelected
)
444 wxCHECK_RET( IS_VALID_PAGE(page
), wxT("invalid notebook page") );
446 wxRect rect
= GetTabRect(page
);
447 if ( forceSelected
|| (page
== m_selection
) )
449 const wxSize indent
= GetRenderer()->GetTabIndent();
450 rect
.Inflate(indent
.x
, indent
.y
);
456 void wxNotebook::RefreshAllTabs()
458 wxRect rect
= GetAllTabsRect();
459 if ( rect
.width
|| rect
.height
)
463 //else: we don't have tabs at all
466 void wxNotebook::DoDrawTab(wxDC
& dc
, const wxRect
& rect
, size_t n
)
471 int image
= m_images
[n
];
473 // Not needed now that wxGenericImageList is being
474 // used for wxUniversal under MSW
475 #if 0 // def __WXMSW__ // FIXME
477 m_imageList
->GetSize(n
, w
, h
);
480 dc
.SelectObject(bmp
);
481 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
482 m_imageList
->Draw(image
, dc
, 0, 0, wxIMAGELIST_DRAW_NORMAL
, true);
483 dc
.SelectObject(wxNullBitmap
);
485 bmp
= m_imageList
->GetBitmap(image
);
490 if ( (int)n
== m_selection
)
492 flags
|= wxCONTROL_SELECTED
;
495 flags
|= wxCONTROL_FOCUSED
;
498 GetRenderer()->DrawTab
510 void wxNotebook::DoDraw(wxControlRenderer
*renderer
)
512 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
514 wxDC
& dc
= renderer
->GetDC();
515 dc
.SetFont(GetFont());
516 dc
.SetTextForeground(GetForegroundColour());
518 // redraw the border - it's simpler to always do it instead of checking
519 // whether this needs to be done
520 GetRenderer()->DrawBorder(dc
, wxBORDER_RAISED
, GetPagePart());
522 // avoid overwriting the spin button
525 wxRect rectTabs
= GetAllTabsRect();
526 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
530 rectTabs
.height
-= sizeSpinBtn
.y
;
532 // Allow for erasing the line under selected tab
537 rectTabs
.width
-= sizeSpinBtn
.x
;
539 // Allow for erasing the line under selected tab
540 rectTabs
.height
+= 2;
543 dc
.SetClippingRegion(rectTabs
);
546 wxRect rect
= GetTabsPart();
547 bool isVertical
= IsVertical();
550 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
552 GetTabSize(n
, &rect
.width
, &rect
.height
);
554 if ( (int)n
== m_selection
)
556 // don't redraw it now as this tab has to be drawn over the other
557 // ones as it takes more place and spills over to them
560 else // not selected tab
562 // unfortunately we can't do this because the selected tab hangs
563 // over its neighbours and so we might need to refresh more tabs -
564 // of course, we could still avoid rereshing some of them with more
565 // complicated checks, but it doesn't seem too bad to refresh all
566 // of them, I still don't see flicker, so leaving as is for now
568 //if ( rectUpdate.Intersects(rect) )
570 DoDrawTab(dc
, rect
, n
);
572 //else: doesn't need to be refreshed
575 // move the rect to the next tab
577 rect
.y
+= rect
.height
;
579 rect
.x
+= rect
.width
;
582 // now redraw the selected tab
585 DoDrawTab(dc
, rectSel
, m_selection
);
588 dc
.DestroyClippingRegion();
591 // ----------------------------------------------------------------------------
592 // wxNotebook geometry
593 // ----------------------------------------------------------------------------
595 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
598 *flags
= wxBK_HITTEST_NOWHERE
;
600 // first check that it is in this window at all
601 if ( !GetClientRect().Contains(pt
) )
606 wxRect rectTabs
= GetAllTabsRect();
608 switch ( GetTabOrientation() )
611 wxFAIL_MSG(wxT("unknown tab orientation"));
615 if ( pt
.y
> rectTabs
.GetBottom() )
620 if ( pt
.y
< rectTabs
.y
)
625 if ( pt
.x
> rectTabs
.GetRight() )
630 if ( pt
.x
< rectTabs
.x
)
635 for ( size_t n
= m_firstVisible
; n
< m_lastVisible
; n
++ )
637 GetTabSize(n
, &rectTabs
.width
, &rectTabs
.height
);
639 if ( rectTabs
.Contains(pt
) )
643 // TODO: be more precise
644 *flags
= wxBK_HITTEST_ONITEM
;
650 // move the rectTabs to the next tab
652 rectTabs
.y
+= rectTabs
.height
;
654 rectTabs
.x
+= rectTabs
.width
;
660 bool wxNotebook::IsVertical() const
662 wxDirection dir
= GetTabOrientation();
664 return dir
== wxLEFT
|| dir
== wxRIGHT
;
667 wxDirection
wxNotebook::GetTabOrientation() const
669 long style
= GetWindowStyle();
670 if ( style
& wxBK_BOTTOM
)
672 else if ( style
& wxBK_RIGHT
)
674 else if ( style
& wxBK_LEFT
)
677 // wxBK_TOP == 0 so we don't have to test for it
681 wxRect
wxNotebook::GetTabRect(int page
) const
684 wxCHECK_MSG( IS_VALID_PAGE(page
), rect
, wxT("invalid notebook page") );
686 // calc the size of this tab and of the preceding ones
687 wxCoord widthThis
, widthBefore
;
688 if ( FixedSizeTabs() )
690 widthThis
= m_widthMax
;
691 widthBefore
= page
*m_widthMax
;
696 for ( int n
= 0; n
< page
; n
++ )
698 widthBefore
+= m_widths
[n
];
701 widthThis
= m_widths
[page
];
704 rect
= GetTabsPart();
707 rect
.y
+= widthBefore
- m_offset
;
708 rect
.height
= widthThis
;
712 rect
.x
+= widthBefore
- m_offset
;
713 rect
.width
= widthThis
;
719 wxRect
wxNotebook::GetAllTabsRect() const
723 if ( GetPageCount() )
725 const wxSize indent
= GetRenderer()->GetTabIndent();
726 wxSize size
= GetClientSize();
730 rect
.width
= m_heightTab
+ indent
.x
;
731 rect
.x
= GetTabOrientation() == wxLEFT
? 0 : size
.x
- rect
.width
;
733 rect
.height
= size
.y
;
739 rect
.height
= m_heightTab
+ indent
.y
;
740 rect
.y
= GetTabOrientation() == wxTOP
? 0 : size
.y
- rect
.height
;
748 wxRect
wxNotebook::GetTabsPart() const
750 wxRect rect
= GetAllTabsRect();
752 wxDirection dir
= GetTabOrientation();
754 const wxSize indent
= GetRenderer()->GetTabIndent();
761 rect
.width
-= indent
.y
;
765 rect
.width
-= indent
.y
;
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
, wxT("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(), wxT("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
, wxT("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_selection
!= wxNOT_FOUND
)
904 // resize the currently shown page
905 wxRect rectPage
= GetPageRect();
907 m_pages
[m_selection
]->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 const size_t selection
= m_selection
;
915 if ( selection
< m_firstVisible
)
917 // selection is to the left of visible part of tabs
920 else if ( selection
> m_lastFullyVisible
)
922 // selection is to the right of visible part of tabs
923 ScrollLastTo(selection
);
928 else // we have no pages
930 // just refresh everything
935 wxRect
wxNotebook::GetPagePart() const
937 wxRect rectPage
= GetClientRect();
939 if ( GetPageCount() )
941 wxRect rectTabs
= GetAllTabsRect();
942 wxDirection dir
= GetTabOrientation();
945 rectPage
.width
-= rectTabs
.width
;
947 rectPage
.x
+= rectTabs
.width
;
951 rectPage
.height
-= rectTabs
.height
;
953 rectPage
.y
+= rectTabs
.height
;
956 //else: no pages at all
961 wxRect
wxNotebook::GetPageRect() const
963 wxRect rect
= GetPagePart();
965 // leave space for the border
966 wxRect rectBorder
= GetRenderer()->GetBorderDimensions(wxBORDER_RAISED
);
968 // FIXME: hardcoded +2!
969 rect
.Inflate(-(rectBorder
.x
+ rectBorder
.width
+ 2),
970 -(rectBorder
.y
+ rectBorder
.height
+ 2));
975 wxSize
wxNotebook::GetSizeForPage(const wxSize
& size
) const
977 wxSize sizeNb
= size
;
978 wxRect rect
= GetAllTabsRect();
980 sizeNb
.x
+= rect
.width
;
982 sizeNb
.y
+= rect
.height
;
987 void wxNotebook::SetPageSize(const wxSize
& size
)
989 SetClientSize(GetSizeForPage(size
));
992 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
994 return AdjustSize(GetSizeForPage(sizePage
));
997 // ----------------------------------------------------------------------------
998 // wxNotebook spin button
999 // ----------------------------------------------------------------------------
1001 bool wxNotebook::HasSpinBtn() const
1003 return m_spinbtn
&& m_spinbtn
->IsShown();
1006 void wxNotebook::CalcLastVisibleTab()
1008 bool isVertical
= IsVertical();
1010 wxCoord width
= GetClientSize().x
;
1012 wxRect rect
= GetTabsPart();
1014 size_t count
= GetPageCount();
1016 wxCoord widthLast
= 0;
1018 for ( n
= m_firstVisible
; n
< count
; n
++ )
1020 GetTabSize(n
, &rect
.width
, &rect
.height
);
1021 if ( rect
.GetRight() > width
)
1026 // remember it to use below
1027 widthLast
= rect
.GetRight();
1029 // move the rect to the next tab
1031 rect
.y
+= rect
.height
;
1033 rect
.x
+= rect
.width
;
1036 if ( n
== m_firstVisible
)
1038 // even the first tab isn't fully visible - but still pretend it is as
1039 // we have to show something
1040 m_lastFullyVisible
= m_firstVisible
;
1042 else // more than one tab visible
1044 m_lastFullyVisible
= n
- 1;
1046 // but is it really fully visible? it shouldn't overlap with the spin
1047 // button if it is present (again, even if the first button does
1048 // overlap with it, we pretend that it doesn't as there is not much
1050 if ( (m_lastFullyVisible
> m_firstVisible
) && HasSpinBtn() )
1052 // adjust width to be the width before the spin button
1053 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1055 width
-= sizeSpinBtn
.y
;
1057 width
-= sizeSpinBtn
.x
;
1059 if ( widthLast
> width
)
1061 // the last button overlaps with spin button, so take he
1063 m_lastFullyVisible
--;
1070 // everything is visible
1075 // this tab is still (partially) visible, so m_lastVisible is the
1076 // next tab (remember, this is "exclusive" last)
1077 m_lastVisible
= n
+ 1;
1082 void wxNotebook::UpdateSpinBtn()
1084 // first decide if we need a spin button
1087 size_t count
= (size_t)GetPageCount();
1090 // this case is special, get rid of it immediately: everything is
1091 // visible and we don't need any spin buttons
1092 allTabsShown
= true;
1094 // have to reset them manually as we don't call CalcLastVisibleTab()
1097 m_lastFullyVisible
= 0;
1101 CalcLastVisibleTab();
1103 // if all tabs after the first visible one are shown, it doesn't yet
1104 // mean that all tabs are shown - so we go backwards until we arrive to
1105 // the beginning (then all tabs are indeed shown) or find a tab such
1106 // that not all tabs after it are shown
1107 while ( (m_lastFullyVisible
== count
- 1) && (m_firstVisible
> 0) )
1109 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1111 m_offset
-= GetTabWidth(m_firstVisible
--);
1113 // reclaculate after m_firstVisible change
1114 CalcLastVisibleTab();
1117 allTabsShown
= m_lastFullyVisible
== count
- 1;
1120 if ( !allTabsShown
)
1124 // create it once only
1125 m_spinbtn
= new wxNotebookSpinBtn(this);
1127 // set the correct value to keep it in sync
1128 m_spinbtn
->SetValue(m_selection
);
1131 // position it correctly
1137 // also set/update the range
1138 m_spinbtn
->SetRange(0, count
- 1);
1140 // update m_lastFullyVisible once again as it might have changed
1141 // because the spin button appeared
1143 // FIXME: might do it more efficiently
1144 CalcLastVisibleTab();
1146 else // all tabs are visible, we don't need spin button
1148 if ( m_spinbtn
&& m_spinbtn
-> IsShown() )
1155 void wxNotebook::PositionSpinBtn()
1161 m_spinbtn
->GetSize(&wBtn
, &hBtn
);
1163 wxRect rectTabs
= GetAllTabsRect();
1166 switch ( GetTabOrientation() )
1169 wxFAIL_MSG(wxT("unknown tab orientation"));
1173 x
= rectTabs
.GetRight() - wBtn
;
1174 y
= rectTabs
.GetBottom() - hBtn
;
1178 x
= rectTabs
.GetRight() - wBtn
;
1179 y
= rectTabs
.GetTop();
1183 x
= rectTabs
.GetRight() - wBtn
;
1184 y
= rectTabs
.GetBottom() - hBtn
;
1188 x
= rectTabs
.GetLeft();
1189 y
= rectTabs
.GetBottom() - hBtn
;
1193 m_spinbtn
->Move(x
, y
);
1196 // ----------------------------------------------------------------------------
1197 // wxNotebook scrolling
1198 // ----------------------------------------------------------------------------
1200 void wxNotebook::ScrollTo(size_t page
)
1202 wxCHECK_RET( IS_VALID_PAGE(page
), wxT("invalid notebook page") );
1204 // set the first visible tab and offset (easy)
1205 m_firstVisible
= page
;
1207 for ( size_t n
= 0; n
< m_firstVisible
; n
++ )
1209 m_offset
+= GetTabWidth(n
);
1212 // find the last visible tab too
1213 CalcLastVisibleTab();
1218 void wxNotebook::ScrollLastTo(size_t page
)
1220 wxCHECK_RET( IS_VALID_PAGE(page
), wxT("invalid notebook page") );
1222 // go backwards until we find the first tab which can be made visible
1223 // without hiding the given one
1224 wxSize size
= GetClientSize();
1225 wxCoord widthAll
= IsVertical() ? size
.y
: size
.x
,
1226 widthTabs
= GetTabWidth(page
);
1228 // the total width is less than the width of the window if we have the spin
1232 wxSize sizeSpinBtn
= m_spinbtn
->GetSize();
1234 widthAll
-= sizeSpinBtn
.y
;
1236 widthAll
-= sizeSpinBtn
.x
;
1239 m_firstVisible
= page
;
1240 while ( (m_firstVisible
> 0) && (widthTabs
<= widthAll
) )
1242 widthTabs
+= GetTabWidth(--m_firstVisible
);
1245 if ( widthTabs
> widthAll
)
1247 // take one step back (that it is forward) if we can
1248 if ( m_firstVisible
< (size_t)GetPageCount() - 1 )
1253 ScrollTo(m_firstVisible
);
1255 // consitency check: the page we were asked to show should be shown
1256 wxASSERT_MSG( (size_t)page
< m_lastVisible
, wxT("bug in ScrollLastTo") );
1259 // ----------------------------------------------------------------------------
1260 // wxNotebook sizing/moving
1261 // ----------------------------------------------------------------------------
1263 wxSize
wxNotebook::DoGetBestClientSize() const
1265 // calculate the max page size
1268 size_t count
= GetPageCount();
1271 for ( size_t n
= 0; n
< count
; n
++ )
1273 wxSize sizePage
= m_pages
[n
]->GetSize();
1275 if ( size
.x
< sizePage
.x
)
1276 size
.x
= sizePage
.x
;
1277 if ( size
.y
< sizePage
.y
)
1278 size
.y
= sizePage
.y
;
1283 // use some arbitrary default size
1288 return GetSizeForPage(size
);
1291 void wxNotebook::DoMoveWindow(int x
, int y
, int width
, int height
)
1293 wxControl::DoMoveWindow(x
, y
, width
, height
);
1295 // move the spin ctrl too (NOP if it doesn't exist)
1299 void wxNotebook::DoSetSize(int x
, int y
,
1300 int width
, int height
,
1303 wxSize old_client_size
= GetClientSize();
1305 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1307 wxSize new_client_size
= GetClientSize();
1309 if (old_client_size
!= new_client_size
)
1313 // ----------------------------------------------------------------------------
1314 // wxNotebook input processing
1315 // ----------------------------------------------------------------------------
1317 bool wxNotebook::PerformAction(const wxControlAction
& action
,
1319 const wxString
& strArg
)
1321 if ( action
== wxACTION_NOTEBOOK_NEXT
)
1322 SetSelection(GetNextPage(true));
1323 else if ( action
== wxACTION_NOTEBOOK_PREV
)
1324 SetSelection(GetNextPage(false));
1325 else if ( action
== wxACTION_NOTEBOOK_GOTO
)
1326 SetSelection((int)numArg
);
1328 return wxControl::PerformAction(action
, numArg
, strArg
);
1334 wxInputHandler
*wxNotebook::GetStdInputHandler(wxInputHandler
*handlerDef
)
1336 static wxStdNotebookInputHandler
s_handler(handlerDef
);
1341 // ----------------------------------------------------------------------------
1342 // wxStdNotebookInputHandler
1343 // ----------------------------------------------------------------------------
1345 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler
*inphand
)
1346 : wxStdInputHandler(inphand
)
1350 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer
*consumer
,
1351 const wxKeyEvent
& event
,
1354 // ignore the key releases
1357 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1360 wxControlAction action
;
1361 switch ( event
.GetKeyCode() )
1364 if ( notebook
->IsVertical() )
1365 action
= wxACTION_NOTEBOOK_PREV
;
1369 if ( !notebook
->IsVertical() )
1370 action
= wxACTION_NOTEBOOK_PREV
;
1374 if ( notebook
->IsVertical() )
1375 action
= wxACTION_NOTEBOOK_NEXT
;
1379 if ( !notebook
->IsVertical() )
1380 action
= wxACTION_NOTEBOOK_NEXT
;
1384 action
= wxACTION_NOTEBOOK_GOTO
;
1385 // page = 0; -- already has this value
1389 action
= wxACTION_NOTEBOOK_GOTO
;
1390 page
= notebook
->GetPageCount() - 1;
1394 if ( !action
.IsEmpty() )
1396 return consumer
->PerformAction(action
, page
);
1400 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
1403 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer
*consumer
,
1404 const wxMouseEvent
& event
)
1406 if ( event
.ButtonDown(1) )
1408 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1409 int page
= notebook
->HitTest(event
.GetPosition());
1412 consumer
->PerformAction(wxACTION_NOTEBOOK_GOTO
, page
);
1418 return wxStdInputHandler::HandleMouse(consumer
, event
);
1421 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
1422 const wxMouseEvent
& event
)
1424 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
1428 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer
*consumer
,
1429 const wxFocusEvent
& WXUNUSED(event
))
1431 HandleFocusChange(consumer
);
1436 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer
*consumer
,
1437 bool WXUNUSED(activated
))
1439 // we react to the focus change in the same way as to the [de]activation
1440 HandleFocusChange(consumer
);
1445 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer
*consumer
)
1447 wxNotebook
*notebook
= wxStaticCast(consumer
->GetInputWindow(), wxNotebook
);
1448 notebook
->RefreshCurrent();
1451 #endif // wxUSE_NOTEBOOK