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