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