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