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