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