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