Committing in .
[wxWidgets.git] / src / univ / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/notebook.cpp
3 // Purpose: wxNotebook implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 01.02.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_NOTEBOOK
27
28 #include "wx/imaglist.h"
29 #include "wx/notebook.h"
30 #include "wx/spinbutt.h"
31 #include "wx/dcmemory.h"
32
33 #include "wx/univ/renderer.h"
34
35 // ----------------------------------------------------------------------------
36 // macros
37 // ----------------------------------------------------------------------------
38
39 #if 0
40 // due to unsigned type nPage is always >= 0
41 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
42 #else
43 #define IS_VALID_PAGE(nPage) (((size_t)nPage) < GetPageCount())
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 static const size_t INVALID_PAGE = (size_t)-1;
51
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
54
55 // ----------------------------------------------------------------------------
56 // private classes
57 // ----------------------------------------------------------------------------
58
59 class wxNotebookSpinBtn : public wxSpinButton
60 {
61 public:
62 wxNotebookSpinBtn(wxNotebook *nb)
63 : wxSpinButton(nb, wxID_ANY,
64 wxDefaultPosition, wxDefaultSize,
65 nb->IsVertical() ? wxSP_VERTICAL : wxSP_HORIZONTAL)
66 {
67 m_nb = nb;
68 }
69
70 protected:
71 void OnSpin(wxSpinEvent& event)
72 {
73 m_nb->PerformAction(wxACTION_NOTEBOOK_GOTO, event.GetPosition());
74 }
75
76 private:
77 wxNotebook *m_nb;
78
79 DECLARE_EVENT_TABLE()
80 };
81
82 BEGIN_EVENT_TABLE(wxNotebookSpinBtn, wxSpinButton)
83 EVT_SPIN(wxID_ANY, wxNotebookSpinBtn::OnSpin)
84 END_EVENT_TABLE()
85
86 // ============================================================================
87 // implementation
88 // ============================================================================
89
90 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
91 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
92
93 // ----------------------------------------------------------------------------
94 // wxNotebook creation
95 // ----------------------------------------------------------------------------
96
97 void wxNotebook::Init()
98 {
99 m_sel = INVALID_PAGE;
100
101 m_heightTab =
102 m_widthMax = 0;
103
104 m_firstVisible =
105 m_lastVisible =
106 m_lastFullyVisible = 0;
107
108 m_offset = 0;
109
110 m_spinbtn = NULL;
111 }
112
113 bool wxNotebook::Create(wxWindow *parent,
114 wxWindowID id,
115 const wxPoint& pos,
116 const wxSize& size,
117 long style,
118 const wxString& name)
119 {
120 if ( !wxControl::Create(parent, id, pos, size, style,
121 wxDefaultValidator, name) )
122 return false;
123
124 m_sizePad = GetRenderer()->GetTabPadding();
125
126 SetBestSize(size);
127
128 CreateInputHandler(wxINP_HANDLER_NOTEBOOK);
129
130 return true;
131 }
132
133 // ----------------------------------------------------------------------------
134 // wxNotebook page titles and images
135 // ----------------------------------------------------------------------------
136
137 wxString wxNotebook::GetPageText(size_t nPage) const
138 {
139 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, _T("invalid notebook page") );
140
141 return m_titles[nPage];
142 }
143
144 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
145 {
146 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, _T("invalid notebook page") );
147
148 if ( strText != m_titles[nPage] )
149 {
150 m_accels[nPage] = FindAccelIndex(strText, &m_titles[nPage]);
151
152 if ( FixedSizeTabs() )
153 {
154 // it's enough to just reresh this one
155 RefreshTab(nPage);
156 }
157 else // var width tabs
158 {
159 // we need to resize the tab to fit the new string
160 ResizeTab(nPage);
161 }
162 }
163
164 return true;
165 }
166
167 int wxNotebook::GetPageImage(size_t nPage) const
168 {
169 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, _T("invalid notebook page") );
170
171 return m_images[nPage];
172 }
173
174 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
175 {
176 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, _T("invalid notebook page") );
177
178 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), false,
179 _T("invalid image index in SetPageImage()") );
180
181 if ( nImage != m_images[nPage] )
182 {
183 // if the item didn't have an icon before or, on the contrary, did have
184 // it but has lost it now, its size will change - but if the icon just
185 // changes, it won't
186 bool tabSizeChanges = nImage == -1 || m_images[nPage] == -1;
187 m_images[nPage] = nImage;
188
189 if ( tabSizeChanges )
190 RefreshAllTabs();
191 else
192 RefreshTab(nPage);
193 }
194
195 return true;
196 }
197
198 wxNotebook::~wxNotebook()
199 {
200 }
201
202 // ----------------------------------------------------------------------------
203 // wxNotebook page switching
204 // ----------------------------------------------------------------------------
205
206 int wxNotebook::SetSelection(size_t nPage)
207 {
208 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, _T("invalid notebook page") );
209
210 if ( (size_t)nPage == m_sel )
211 {
212 // don't do anything if there is nothing to do
213 return m_sel;
214 }
215
216 // event handling
217 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
218 event.SetSelection(nPage);
219 event.SetOldSelection(m_sel);
220 event.SetEventObject(this);
221 if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
222 {
223 // program doesn't allow the page change
224 return m_sel;
225 }
226
227 // we need to change m_sel first, before calling RefreshTab() below as
228 // otherwise the previously selected tab wouldn't be redrawn properly under
229 // wxGTK which calls Refresh() immediately and not during the next event
230 // loop iteration as wxMSW does and as it should
231 size_t selOld = m_sel;
232
233 m_sel = nPage;
234
235 if ( selOld != INVALID_PAGE )
236 {
237 RefreshTab(selOld, true /* this tab was selected */);
238
239 m_pages[selOld]->Hide();
240 }
241
242 if ( m_sel != INVALID_PAGE ) // this is impossible - but test nevertheless
243 {
244 if ( HasSpinBtn() )
245 {
246 // keep it in sync
247 m_spinbtn->SetValue(m_sel);
248 }
249
250 if ( m_sel < m_firstVisible )
251 {
252 // selection is to the left of visible part of tabs
253 ScrollTo(m_sel);
254 }
255 else if ( m_sel > m_lastFullyVisible )
256 {
257 // selection is to the right of visible part of tabs
258 ScrollLastTo(m_sel);
259 }
260 else // we already see this tab
261 {
262 // no need to scroll
263 RefreshTab(m_sel);
264 }
265
266 m_pages[m_sel]->SetSize(GetPageRect());
267 m_pages[m_sel]->Show();
268 }
269
270 // event handling
271 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
272 GetEventHandler()->ProcessEvent(event);
273
274 return selOld;
275 }
276
277 // ----------------------------------------------------------------------------
278 // wxNotebook pages adding/deleting
279 // ----------------------------------------------------------------------------
280
281 bool wxNotebook::InsertPage(size_t nPage,
282 wxNotebookPage *pPage,
283 const wxString& strText,
284 bool bSelect,
285 int imageId)
286 {
287 size_t nPages = GetPageCount();
288 wxCHECK_MSG( nPage == nPages || IS_VALID_PAGE(nPage), false,
289 _T("invalid notebook page in InsertPage()") );
290
291 // modify the data
292 m_pages.Insert(pPage, nPage);
293
294 wxString label;
295 m_accels.Insert(FindAccelIndex(strText, &label), nPage);
296 m_titles.Insert(label, nPage);
297
298 m_images.Insert(imageId, nPage);
299
300 // cache the tab geometry here
301 wxSize sizeTab = CalcTabSize(nPage);
302
303 if ( sizeTab.y > m_heightTab )
304 m_heightTab = sizeTab.y;
305
306 if ( FixedSizeTabs() && sizeTab.x > m_widthMax )
307 m_widthMax = sizeTab.x;
308
309 m_widths.Insert(sizeTab.x, nPage);
310
311 // spin button may appear if we didn't have it before - but even if we did,
312 // its range should change, so update it unconditionally
313 UpdateSpinBtn();
314
315 // if the tab has just appeared, we have to relayout everything, otherwise
316 // it's enough to just redraw the tabs
317 if ( nPages == 0 )
318 {
319 // always select the first tab to have at least some selection
320 bSelect = true;
321
322 Relayout();
323 Refresh();
324 }
325 else // not the first tab
326 {
327 RefreshAllTabs();
328 }
329
330 if ( bSelect )
331 {
332 SetSelection(nPage);
333 }
334 else // pages added to the notebook are initially hidden
335 {
336 pPage->Hide();
337 }
338
339 return true;
340 }
341
342 bool wxNotebook::DeleteAllPages()
343 {
344 if ( !wxNotebookBase::DeleteAllPages() )
345 return false;
346
347 // clear the other arrays as well
348 m_titles.Clear();
349 m_images.Clear();
350 m_accels.Clear();
351 m_widths.Clear();
352
353 // it is not valid any longer
354 m_sel = INVALID_PAGE;
355
356 // spin button is not needed any more
357 UpdateSpinBtn();
358
359 Relayout();
360
361 return true;
362 }
363
364 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
365 {
366 wxCHECK_MSG( IS_VALID_PAGE(nPage), NULL, _T("invalid notebook page") );
367
368 wxNotebookPage *page = m_pages[nPage];
369 m_pages.RemoveAt(nPage);
370 m_titles.RemoveAt(nPage);
371 m_accels.RemoveAt(nPage);
372 m_widths.RemoveAt(nPage);
373 m_images.RemoveAt(nPage);
374
375 // the spin button might not be needed any more
376 // 2002-08-12 'if' commented out by JACS on behalf
377 // of Hans Van Leemputten <Hansvl@softhome.net> who
378 // points out that UpdateSpinBtn should always be called,
379 // to ensure m_lastVisible is up to date.
380 // if ( HasSpinBtn() )
381 {
382 UpdateSpinBtn();
383 }
384
385 size_t count = GetPageCount();
386 if ( count )
387 {
388 if ( m_sel == (size_t)nPage )
389 {
390 // avoid sending event to this page which doesn't exist in the
391 // notebook any more
392 m_sel = INVALID_PAGE;
393
394 SetSelection(nPage == count ? nPage - 1 : nPage);
395 }
396 else if ( m_sel > (size_t)nPage )
397 {
398 // no need to change selection, just adjust the index
399 m_sel--;
400 }
401 }
402 else // no more tabs left
403 {
404 m_sel = INVALID_PAGE;
405 }
406
407 // have to refresh everything
408 Relayout();
409
410 return page;
411 }
412
413 // ----------------------------------------------------------------------------
414 // wxNotebook drawing
415 // ----------------------------------------------------------------------------
416
417 void wxNotebook::RefreshCurrent()
418 {
419 if ( m_sel != INVALID_PAGE )
420 {
421 RefreshTab(m_sel);
422 }
423 }
424
425 void wxNotebook::RefreshTab(int page, bool forceSelected)
426 {
427 wxCHECK_RET( IS_VALID_PAGE(page), _T("invalid notebook page") );
428
429 wxRect rect = GetTabRect(page);
430 if ( forceSelected || ((size_t)page == m_sel) )
431 {
432 const wxSize indent = GetRenderer()->GetTabIndent();
433 rect.Inflate(indent.x, indent.y);
434 }
435
436 RefreshRect(rect);
437 }
438
439 void wxNotebook::RefreshAllTabs()
440 {
441 wxRect rect = GetAllTabsRect();
442 if ( rect.width || rect.height )
443 {
444 RefreshRect(rect);
445 }
446 //else: we don't have tabs at all
447 }
448
449 void wxNotebook::DoDrawTab(wxDC& dc, const wxRect& rect, size_t n)
450 {
451 wxBitmap bmp;
452 if ( HasImage(n) )
453 {
454 int image = m_images[n];
455
456 // Not needed now that wxGenericImageList is being
457 // used for wxUniversal under MSW
458 #if 0 // def __WXMSW__ // FIXME
459 int w, h;
460 m_imageList->GetSize(n, w, h);
461 bmp.Create(w, h);
462 wxMemoryDC dc;
463 dc.SelectObject(bmp);
464 dc.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID));
465 m_imageList->Draw(image, dc, 0, 0, wxIMAGELIST_DRAW_NORMAL, true);
466 dc.SelectObject(wxNullBitmap);
467 #else
468 bmp = m_imageList->GetBitmap(image);
469 #endif
470 }
471
472 int flags = 0;
473 if ( n == m_sel )
474 {
475 flags |= wxCONTROL_SELECTED;
476
477 if ( IsFocused() )
478 flags |= wxCONTROL_FOCUSED;
479 }
480
481 GetRenderer()->DrawTab
482 (
483 dc,
484 rect,
485 GetTabOrientation(),
486 m_titles[n],
487 bmp,
488 flags,
489 m_accels[n]
490 );
491 }
492
493 void wxNotebook::DoDraw(wxControlRenderer *renderer)
494 {
495 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
496
497 wxDC& dc = renderer->GetDC();
498 dc.SetFont(GetFont());
499 dc.SetTextForeground(GetForegroundColour());
500
501 // redraw the border - it's simpler to always do it instead of checking
502 // whether this needs to be done
503 GetRenderer()->DrawBorder(dc, wxBORDER_RAISED, GetPagePart());
504
505 // avoid overwriting the spin button
506 if ( HasSpinBtn() )
507 {
508 wxRect rectTabs = GetAllTabsRect();
509 wxSize sizeSpinBtn = m_spinbtn->GetSize();
510
511 if ( IsVertical() )
512 {
513 rectTabs.height -= sizeSpinBtn.y;
514
515 // Allow for erasing the line under selected tab
516 rectTabs.width += 2;
517 }
518 else
519 {
520 rectTabs.width -= sizeSpinBtn.x;
521
522 // Allow for erasing the line under selected tab
523 rectTabs.height += 2;
524 }
525
526 dc.SetClippingRegion(rectTabs);
527 }
528
529 wxRect rect = GetTabsPart();
530 bool isVertical = IsVertical();
531
532 wxRect rectSel;
533 for ( size_t n = m_firstVisible; n < m_lastVisible; n++ )
534 {
535 GetTabSize(n, &rect.width, &rect.height);
536
537 if ( n == m_sel )
538 {
539 // don't redraw it now as this tab has to be drawn over the other
540 // ones as it takes more place and spills over to them
541 rectSel = rect;
542 }
543 else // not selected tab
544 {
545 // unfortunately we can't do this because the selected tab hangs
546 // over its neighbours and so we might need to refresh more tabs -
547 // of course, we could still avoid rereshing some of them with more
548 // complicated checks, but it doesn't seem too bad to refresh all
549 // of them, I still don't see flicker, so leaving as is for now
550
551 //if ( rectUpdate.Intersects(rect) )
552 {
553 DoDrawTab(dc, rect, n);
554 }
555 //else: doesn't need to be refreshed
556 }
557
558 // move the rect to the next tab
559 if ( isVertical )
560 rect.y += rect.height;
561 else
562 rect.x += rect.width;
563 }
564
565 // now redraw the selected tab
566 if ( rectSel.width )
567 {
568 DoDrawTab(dc, rectSel, m_sel);
569 }
570
571 dc.DestroyClippingRegion();
572 }
573
574 // ----------------------------------------------------------------------------
575 // wxNotebook geometry
576 // ----------------------------------------------------------------------------
577
578 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
579 {
580 if ( flags )
581 *flags = wxNB_HITTEST_NOWHERE;
582
583 // first check that it is in this window at all
584 if ( !GetClientRect().Inside(pt) )
585 {
586 return -1;
587 }
588
589 wxRect rectTabs = GetAllTabsRect();
590
591 switch ( GetTabOrientation() )
592 {
593 default:
594 wxFAIL_MSG(_T("unknown tab orientation"));
595 // fall through
596
597 case wxTOP:
598 if ( pt.y > rectTabs.GetBottom() )
599 return -1;
600 break;
601
602 case wxBOTTOM:
603 if ( pt.y < rectTabs.y )
604 return -1;
605 break;
606
607 case wxLEFT:
608 if ( pt.x > rectTabs.GetRight() )
609 return -1;
610 break;
611
612 case wxRIGHT:
613 if ( pt.x < rectTabs.x )
614 return -1;
615 break;
616 }
617
618 for ( size_t n = m_firstVisible; n < m_lastVisible; n++ )
619 {
620 GetTabSize(n, &rectTabs.width, &rectTabs.height);
621
622 if ( rectTabs.Inside(pt) )
623 {
624 if ( flags )
625 {
626 // TODO: be more precise
627 *flags = wxNB_HITTEST_ONITEM;
628 }
629
630 return n;
631 }
632
633 // move the rectTabs to the next tab
634 if ( IsVertical() )
635 rectTabs.y += rectTabs.height;
636 else
637 rectTabs.x += rectTabs.width;
638 }
639
640 return -1;
641 }
642
643 bool wxNotebook::IsVertical() const
644 {
645 wxDirection dir = GetTabOrientation();
646
647 return dir == wxLEFT || dir == wxRIGHT;
648 }
649
650 wxDirection wxNotebook::GetTabOrientation() const
651 {
652 long style = GetWindowStyle();
653 if ( style & wxBK_BOTTOM )
654 return wxBOTTOM;
655 else if ( style & wxBK_RIGHT )
656 return wxRIGHT;
657 else if ( style & wxBK_LEFT )
658 return wxLEFT;
659
660 // wxBK_TOP == 0 so we don't have to test for it
661 return wxTOP;
662 }
663
664 wxRect wxNotebook::GetTabRect(int page) const
665 {
666 wxRect rect;
667 wxCHECK_MSG( IS_VALID_PAGE(page), rect, _T("invalid notebook page") );
668
669 // calc the size of this tab and of the preceding ones
670 wxCoord widthThis, widthBefore;
671 if ( FixedSizeTabs() )
672 {
673 widthThis = m_widthMax;
674 widthBefore = page*m_widthMax;
675 }
676 else
677 {
678 widthBefore = 0;
679 for ( int n = 0; n < page; n++ )
680 {
681 widthBefore += m_widths[n];
682 }
683
684 widthThis = m_widths[page];
685 }
686
687 rect = GetTabsPart();
688 if ( IsVertical() )
689 {
690 rect.y += widthBefore - m_offset;
691 rect.height = widthThis;
692 }
693 else // horz
694 {
695 rect.x += widthBefore - m_offset;
696 rect.width = widthThis;
697 }
698
699 return rect;
700 }
701
702 wxRect wxNotebook::GetAllTabsRect() const
703 {
704 wxRect rect;
705
706 if ( GetPageCount() )
707 {
708 const wxSize indent = GetRenderer()->GetTabIndent();
709 wxSize size = GetClientSize();
710
711 if ( IsVertical() )
712 {
713 rect.width = m_heightTab + indent.x;
714 rect.x = GetTabOrientation() == wxLEFT ? 0 : size.x - rect.width;
715 rect.y = 0;
716 rect.height = size.y;
717 }
718 else // horz
719 {
720 rect.x = 0;
721 rect.width = size.x;
722 rect.height = m_heightTab + indent.y;
723 rect.y = GetTabOrientation() == wxTOP ? 0 : size.y - rect.height;
724 }
725 }
726 //else: no pages
727
728 return rect;
729 }
730
731 wxRect wxNotebook::GetTabsPart() const
732 {
733 wxRect rect = GetAllTabsRect();
734
735 wxDirection dir = GetTabOrientation();
736
737 const wxSize indent = GetRenderer()->GetTabIndent();
738 if ( IsVertical() )
739 {
740 rect.y += indent.x;
741 if ( dir == wxLEFT )
742 {
743 rect.x += indent.y;
744 rect.width -= indent.y;
745 }
746 else // wxRIGHT
747 {
748 rect.width -= indent.y;
749 }
750 }
751 else // horz
752 {
753 rect.x += indent.x;
754 if ( dir == wxTOP )
755 {
756 rect.y += indent.y;
757 rect.height -= indent.y;
758 }
759 else // wxBOTTOM
760 {
761 rect.height -= indent.y;
762 }
763 }
764
765 return rect;
766 }
767
768 void wxNotebook::GetTabSize(int page, wxCoord *w, wxCoord *h) const
769 {
770 wxCHECK_RET( w && h, _T("NULL pointer in GetTabSize") );
771
772 if ( IsVertical() )
773 {
774 // width and height have inverted meaning
775 wxCoord *tmp = w;
776 w = h;
777 h = tmp;
778 }
779
780 // height is always fixed
781 *h = m_heightTab;
782
783 // width may also be fixed and be the same for all tabs
784 *w = GetTabWidth(page);
785 }
786
787 void wxNotebook::SetTabSize(const wxSize& sz)
788 {
789 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
790
791 if ( IsVertical() )
792 {
793 m_heightTab = sz.x;
794 m_widthMax = sz.y;
795 }
796 else // horz
797 {
798 m_widthMax = sz.x;
799 m_heightTab = sz.y;
800 }
801 }
802
803 wxSize wxNotebook::CalcTabSize(int page) const
804 {
805 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
806 // method is called to calculate them
807
808 wxSize size;
809
810 wxCHECK_MSG( IS_VALID_PAGE(page), size, _T("invalid notebook page") );
811
812 GetTextExtent(m_titles[page], &size.x, &size.y);
813
814 if ( HasImage(page) )
815 {
816 wxSize sizeImage;
817 m_imageList->GetSize(m_images[page], sizeImage.x, sizeImage.y);
818
819 size.x += sizeImage.x + 5; // FIXME: hard coded margin
820
821 if ( sizeImage.y > size.y )
822 size.y = sizeImage.y;
823 }
824
825 size.x += 2*m_sizePad.x;
826 size.y += 2*m_sizePad.y;
827
828 return size;
829 }
830
831 void wxNotebook::ResizeTab(int page)
832 {
833 wxSize sizeTab = CalcTabSize(page);
834
835 // we only need full relayout if the page size changes
836 bool needsRelayout = false;
837
838 if ( IsVertical() )
839 {
840 // swap coordinates
841 wxCoord tmp = sizeTab.x;
842 sizeTab.x = sizeTab.y;
843 sizeTab.y = tmp;
844 }
845
846 if ( sizeTab.y > m_heightTab )
847 {
848 needsRelayout = true;
849
850 m_heightTab = sizeTab.y;
851 }
852
853 m_widths[page] = sizeTab.x;
854
855 if ( sizeTab.x > m_widthMax )
856 m_widthMax = sizeTab.x;
857
858 // the total of the tabs has changed too
859 UpdateSpinBtn();
860
861 if ( needsRelayout )
862 Relayout();
863 else
864 RefreshAllTabs();
865 }
866
867 void wxNotebook::SetPadding(const wxSize& padding)
868 {
869 if ( padding != m_sizePad )
870 {
871 m_sizePad = padding;
872
873 Relayout();
874 }
875 }
876
877 void wxNotebook::Relayout()
878 {
879 if ( GetPageCount() )
880 {
881 RefreshAllTabs();
882
883 UpdateSpinBtn();
884
885 if ( m_sel != INVALID_PAGE )
886 {
887 // resize the currently shown page
888 wxRect rectPage = GetPageRect();
889
890 m_pages[m_sel]->SetSize(rectPage);
891
892 // also scroll it into view if needed (note that m_lastVisible
893 // was updated by the call to UpdateSpinBtn() above, this is why it
894 // is needed here)
895 if ( HasSpinBtn() )
896 {
897 if ( m_sel < m_firstVisible )
898 {
899 // selection is to the left of visible part of tabs
900 ScrollTo(m_sel);
901 }
902 else if ( m_sel > m_lastFullyVisible )
903 {
904 // selection is to the right of visible part of tabs
905 ScrollLastTo(m_sel);
906 }
907 }
908 }
909 }
910 else // we have no pages
911 {
912 // just refresh everything
913 Refresh();
914 }
915 }
916
917 wxRect wxNotebook::GetPagePart() const
918 {
919 wxRect rectPage = GetClientRect();
920
921 if ( GetPageCount() )
922 {
923 wxRect rectTabs = GetAllTabsRect();
924 wxDirection dir = GetTabOrientation();
925 if ( IsVertical() )
926 {
927 rectPage.width -= rectTabs.width;
928 if ( dir == wxLEFT )
929 rectPage.x += rectTabs.width;
930 }
931 else // horz
932 {
933 rectPage.height -= rectTabs.height;
934 if ( dir == wxTOP )
935 rectPage.y += rectTabs.height;
936 }
937 }
938 //else: no pages at all
939
940 return rectPage;
941 }
942
943 wxRect wxNotebook::GetPageRect() const
944 {
945 wxRect rect = GetPagePart();
946
947 // leave space for the border
948 wxRect rectBorder = GetRenderer()->GetBorderDimensions(wxBORDER_RAISED);
949
950 // FIXME: hardcoded +2!
951 rect.Inflate(-(rectBorder.x + rectBorder.width + 2),
952 -(rectBorder.y + rectBorder.height + 2));
953
954 return rect;
955 }
956
957 wxSize wxNotebook::GetSizeForPage(const wxSize& size) const
958 {
959 wxSize sizeNb = size;
960 wxRect rect = GetAllTabsRect();
961 if ( IsVertical() )
962 sizeNb.x += rect.width;
963 else
964 sizeNb.y += rect.height;
965
966 return sizeNb;
967 }
968
969 void wxNotebook::SetPageSize(const wxSize& size)
970 {
971 SetClientSize(GetSizeForPage(size));
972 }
973
974 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
975 {
976 return AdjustSize(GetSizeForPage(sizePage));
977 }
978
979 // ----------------------------------------------------------------------------
980 // wxNotebook spin button
981 // ----------------------------------------------------------------------------
982
983 bool wxNotebook::HasSpinBtn() const
984 {
985 return m_spinbtn && m_spinbtn->IsShown();
986 }
987
988 void wxNotebook::CalcLastVisibleTab()
989 {
990 bool isVertical = IsVertical();
991
992 wxCoord width = GetClientSize().x;
993
994 wxRect rect = GetTabsPart();
995
996 size_t count = GetPageCount();
997
998 wxCoord widthLast = 0;
999 size_t n;
1000 for ( n = m_firstVisible; n < count; n++ )
1001 {
1002 GetTabSize(n, &rect.width, &rect.height);
1003 if ( rect.GetRight() > width )
1004 {
1005 break;
1006 }
1007
1008 // remember it to use below
1009 widthLast = rect.GetRight();
1010
1011 // move the rect to the next tab
1012 if ( isVertical )
1013 rect.y += rect.height;
1014 else
1015 rect.x += rect.width;
1016 }
1017
1018 if ( n == m_firstVisible )
1019 {
1020 // even the first tab isn't fully visible - but still pretend it is as
1021 // we have to show something
1022 m_lastFullyVisible = m_firstVisible;
1023 }
1024 else // more than one tab visible
1025 {
1026 m_lastFullyVisible = n - 1;
1027
1028 // but is it really fully visible? it shouldn't overlap with the spin
1029 // button if it is present (again, even if the first button does
1030 // overlap with it, we pretend that it doesn't as there is not much
1031 // else we can do)
1032 if ( (m_lastFullyVisible > m_firstVisible) && HasSpinBtn() )
1033 {
1034 // adjust width to be the width before the spin button
1035 wxSize sizeSpinBtn = m_spinbtn->GetSize();
1036 if ( IsVertical() )
1037 width -= sizeSpinBtn.y;
1038 else
1039 width -= sizeSpinBtn.x;
1040
1041 if ( widthLast > width )
1042 {
1043 // the last button overlaps with spin button, so take he
1044 // previous one
1045 m_lastFullyVisible--;
1046 }
1047 }
1048 }
1049
1050 if ( n == count )
1051 {
1052 // everything is visible
1053 m_lastVisible = n;
1054 }
1055 else
1056 {
1057 // this tab is still (partially) visible, so m_lastVisible is the
1058 // next tab (remember, this is "exclusive" last)
1059 m_lastVisible = n + 1;
1060
1061 }
1062 }
1063
1064 void wxNotebook::UpdateSpinBtn()
1065 {
1066 // first decide if we need a spin button
1067 bool allTabsShown;
1068
1069 size_t count = (size_t)GetPageCount();
1070 if ( count == 0 )
1071 {
1072 // this case is special, get rid of it immediately: everything is
1073 // visible and we don't need any spin buttons
1074 allTabsShown = true;
1075
1076 // have to reset them manually as we don't call CalcLastVisibleTab()
1077 m_firstVisible =
1078 m_lastVisible =
1079 m_lastFullyVisible = 0;
1080 }
1081 else
1082 {
1083 CalcLastVisibleTab();
1084
1085 // if all tabs after the first visible one are shown, it doesn't yet
1086 // mean that all tabs are shown - so we go backwards until we arrive to
1087 // the beginning (then all tabs are indeed shown) or find a tab such
1088 // that not all tabs after it are shown
1089 while ( (m_lastFullyVisible == count - 1) && (m_firstVisible > 0) )
1090 {
1091 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1092 // efficient
1093 m_offset -= GetTabWidth(m_firstVisible--);
1094
1095 // reclaculate after m_firstVisible change
1096 CalcLastVisibleTab();
1097 }
1098
1099 allTabsShown = m_lastFullyVisible == count - 1;
1100 }
1101
1102 if ( !allTabsShown )
1103 {
1104 if ( !m_spinbtn )
1105 {
1106 // create it once only
1107 m_spinbtn = new wxNotebookSpinBtn(this);
1108
1109 // set the correct value to keep it in sync
1110 m_spinbtn->SetValue(m_sel);
1111 }
1112
1113 // position it correctly
1114 PositionSpinBtn();
1115
1116 // and show it
1117 m_spinbtn->Show();
1118
1119 // also set/update the range
1120 m_spinbtn->SetRange(0, count - 1);
1121
1122 // update m_lastFullyVisible once again as it might have changed
1123 // because the spin button appeared
1124 //
1125 // FIXME: might do it more efficiently
1126 CalcLastVisibleTab();
1127 }
1128 else // all tabs are visible, we don't need spin button
1129 {
1130 if ( m_spinbtn && m_spinbtn -> IsShown() )
1131 {
1132 m_spinbtn->Hide();
1133 }
1134 }
1135 }
1136
1137 void wxNotebook::PositionSpinBtn()
1138 {
1139 if ( !m_spinbtn )
1140 return;
1141
1142 wxCoord wBtn, hBtn;
1143 m_spinbtn->GetSize(&wBtn, &hBtn);
1144
1145 wxRect rectTabs = GetAllTabsRect();
1146
1147 wxCoord x, y;
1148 switch ( GetTabOrientation() )
1149 {
1150 default:
1151 wxFAIL_MSG(_T("unknown tab orientation"));
1152 // fall through
1153
1154 case wxTOP:
1155 x = rectTabs.GetRight() - wBtn;
1156 y = rectTabs.GetBottom() - hBtn;
1157 break;
1158
1159 case wxBOTTOM:
1160 x = rectTabs.GetRight() - wBtn;
1161 y = rectTabs.GetTop();
1162 break;
1163
1164 case wxLEFT:
1165 x = rectTabs.GetRight() - wBtn;
1166 y = rectTabs.GetBottom() - hBtn;
1167 break;
1168
1169 case wxRIGHT:
1170 x = rectTabs.GetLeft();
1171 y = rectTabs.GetBottom() - hBtn;
1172 break;
1173 }
1174
1175 m_spinbtn->Move(x, y);
1176 }
1177
1178 // ----------------------------------------------------------------------------
1179 // wxNotebook scrolling
1180 // ----------------------------------------------------------------------------
1181
1182 void wxNotebook::ScrollTo(int page)
1183 {
1184 wxCHECK_RET( IS_VALID_PAGE(page), _T("invalid notebook page") );
1185
1186 // set the first visible tab and offset (easy)
1187 m_firstVisible = (size_t)page;
1188 m_offset = 0;
1189 for ( size_t n = 0; n < m_firstVisible; n++ )
1190 {
1191 m_offset += GetTabWidth(n);
1192 }
1193
1194 // find the last visible tab too
1195 CalcLastVisibleTab();
1196
1197 RefreshAllTabs();
1198 }
1199
1200 void wxNotebook::ScrollLastTo(int page)
1201 {
1202 wxCHECK_RET( IS_VALID_PAGE(page), _T("invalid notebook page") );
1203
1204 // go backwards until we find the first tab which can be made visible
1205 // without hiding the given one
1206 wxSize size = GetClientSize();
1207 wxCoord widthAll = IsVertical() ? size.y : size.x,
1208 widthTabs = GetTabWidth(page);
1209
1210 // the total width is less than the width of the window if we have the spin
1211 // button
1212 if ( HasSpinBtn() )
1213 {
1214 wxSize sizeSpinBtn = m_spinbtn->GetSize();
1215 if ( IsVertical() )
1216 widthAll -= sizeSpinBtn.y;
1217 else
1218 widthAll -= sizeSpinBtn.x;
1219 }
1220
1221 m_firstVisible = page;
1222 while ( (m_firstVisible > 0) && (widthTabs <= widthAll) )
1223 {
1224 widthTabs += GetTabWidth(--m_firstVisible);
1225 }
1226
1227 if ( widthTabs > widthAll )
1228 {
1229 // take one step back (that it is forward) if we can
1230 if ( m_firstVisible < (size_t)GetPageCount() - 1 )
1231 m_firstVisible++;
1232 }
1233
1234 // go to it
1235 ScrollTo(m_firstVisible);
1236
1237 // consitency check: the page we were asked to show should be shown
1238 wxASSERT_MSG( (size_t)page < m_lastVisible, _T("bug in ScrollLastTo") );
1239 }
1240
1241 // ----------------------------------------------------------------------------
1242 // wxNotebook sizing/moving
1243 // ----------------------------------------------------------------------------
1244
1245 wxSize wxNotebook::DoGetBestClientSize() const
1246 {
1247 // calculate the max page size
1248 wxSize size;
1249
1250 size_t count = GetPageCount();
1251 if ( count )
1252 {
1253 for ( size_t n = 0; n < count; n++ )
1254 {
1255 wxSize sizePage = m_pages[n]->GetSize();
1256
1257 if ( size.x < sizePage.x )
1258 size.x = sizePage.x;
1259 if ( size.y < sizePage.y )
1260 size.y = sizePage.y;
1261 }
1262 }
1263 else // no pages
1264 {
1265 // use some arbitrary default size
1266 size.x =
1267 size.y = 100;
1268 }
1269
1270 return GetSizeForPage(size);
1271 }
1272
1273 void wxNotebook::DoMoveWindow(int x, int y, int width, int height)
1274 {
1275 wxControl::DoMoveWindow(x, y, width, height);
1276
1277 // move the spin ctrl too (NOP if it doesn't exist)
1278 PositionSpinBtn();
1279 }
1280
1281 void wxNotebook::DoSetSize(int x, int y,
1282 int width, int height,
1283 int sizeFlags)
1284 {
1285 wxSize old_client_size = GetClientSize();
1286
1287 wxControl::DoSetSize(x, y, width, height, sizeFlags);
1288
1289 wxSize new_client_size = GetClientSize();
1290
1291 if (old_client_size != new_client_size)
1292 Relayout();
1293 }
1294
1295 // ----------------------------------------------------------------------------
1296 // wxNotebook input processing
1297 // ----------------------------------------------------------------------------
1298
1299 bool wxNotebook::PerformAction(const wxControlAction& action,
1300 long numArg,
1301 const wxString& strArg)
1302 {
1303 if ( action == wxACTION_NOTEBOOK_NEXT )
1304 SetSelection(GetNextPage(true));
1305 else if ( action == wxACTION_NOTEBOOK_PREV )
1306 SetSelection(GetNextPage(false));
1307 else if ( action == wxACTION_NOTEBOOK_GOTO )
1308 SetSelection((int)numArg);
1309 else
1310 return wxControl::PerformAction(action, numArg, strArg);
1311
1312 return true;
1313 }
1314
1315 // ----------------------------------------------------------------------------
1316 // wxStdNotebookInputHandler
1317 // ----------------------------------------------------------------------------
1318
1319 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler *inphand)
1320 : wxStdInputHandler(inphand)
1321 {
1322 }
1323
1324 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer *consumer,
1325 const wxKeyEvent& event,
1326 bool pressed)
1327 {
1328 // ignore the key releases
1329 if ( pressed )
1330 {
1331 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1332
1333 int page = 0;
1334 wxControlAction action;
1335 switch ( event.GetKeyCode() )
1336 {
1337 case WXK_UP:
1338 if ( notebook->IsVertical() )
1339 action = wxACTION_NOTEBOOK_PREV;
1340 break;
1341
1342 case WXK_LEFT:
1343 if ( !notebook->IsVertical() )
1344 action = wxACTION_NOTEBOOK_PREV;
1345 break;
1346
1347 case WXK_DOWN:
1348 if ( notebook->IsVertical() )
1349 action = wxACTION_NOTEBOOK_NEXT;
1350 break;
1351
1352 case WXK_RIGHT:
1353 if ( !notebook->IsVertical() )
1354 action = wxACTION_NOTEBOOK_NEXT;
1355 break;
1356
1357 case WXK_HOME:
1358 action = wxACTION_NOTEBOOK_GOTO;
1359 // page = 0; -- already has this value
1360 break;
1361
1362 case WXK_END:
1363 action = wxACTION_NOTEBOOK_GOTO;
1364 page = notebook->GetPageCount() - 1;
1365 break;
1366 }
1367
1368 if ( !action.IsEmpty() )
1369 {
1370 return consumer->PerformAction(action, page);
1371 }
1372 }
1373
1374 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1375 }
1376
1377 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer *consumer,
1378 const wxMouseEvent& event)
1379 {
1380 if ( event.ButtonDown(1) )
1381 {
1382 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1383 int page = notebook->HitTest(event.GetPosition());
1384 if ( page != -1 )
1385 {
1386 consumer->PerformAction(wxACTION_NOTEBOOK_GOTO, page);
1387
1388 return false;
1389 }
1390 }
1391
1392 return wxStdInputHandler::HandleMouse(consumer, event);
1393 }
1394
1395 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer *consumer,
1396 const wxMouseEvent& event)
1397 {
1398 return wxStdInputHandler::HandleMouseMove(consumer, event);
1399 }
1400
1401 bool
1402 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer *consumer,
1403 const wxFocusEvent& WXUNUSED(event))
1404 {
1405 HandleFocusChange(consumer);
1406
1407 return false;
1408 }
1409
1410 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer *consumer,
1411 bool WXUNUSED(activated))
1412 {
1413 // we react to the focus change in the same way as to the [de]activation
1414 HandleFocusChange(consumer);
1415
1416 return false;
1417 }
1418
1419 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer *consumer)
1420 {
1421 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1422 notebook->RefreshCurrent();
1423 }
1424
1425 #endif // wxUSE_NOTEBOOK