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